tron@3147: /* $Id$ */ tron@3147: belugas@6451: /** @file direction.h */ belugas@6451: tron@3147: #ifndef DIRECTION_H tron@3147: #define DIRECTION_H tron@3147: rubidium@5838: #include "helpers.hpp" rubidium@5838: tron@3147: /* Direction as commonly used in v->direction, 8 way. */ celestar@9895: enum Direction { rubidium@5838: DIR_BEGIN = 0, tron@3147: DIR_N = 0, belugas@6451: DIR_NE = 1, ///< Northeast, upper right on your monitor tron@3147: DIR_E = 2, tron@3147: DIR_SE = 3, tron@3147: DIR_S = 4, tron@3147: DIR_SW = 5, tron@3147: DIR_W = 6, tron@3147: DIR_NW = 7, tron@3147: DIR_END, tron@3147: INVALID_DIR = 0xFF, celestar@9895: }; tron@3147: rubidium@5838: /** Define basic enum properties */ rubidium@5838: template <> struct EnumPropsT : MakeEnumPropsT {}; celestar@9908: typedef TinyEnumT DirectionByte; //typedefing-enumification of Direction rubidium@5838: tron@3157: static inline Direction ReverseDir(Direction d) tron@3157: { tron@3157: return (Direction)(4 ^ d); tron@3157: } tron@3157: tron@3147: celestar@9895: enum DirDiff { tron@3158: DIRDIFF_SAME = 0, tron@3158: DIRDIFF_45RIGHT = 1, tron@3158: DIRDIFF_90RIGHT = 2, tron@3158: DIRDIFF_REVERSE = 4, tron@3158: DIRDIFF_90LEFT = 6, tron@3158: DIRDIFF_45LEFT = 7 celestar@9895: }; tron@3158: tron@3158: static inline DirDiff DirDifference(Direction d0, Direction d1) tron@3158: { tron@3162: return (DirDiff)((d0 + 8 - d1) % 8); tron@3158: } tron@3158: tron@3158: static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta) tron@3158: { tron@3158: return (DirDiff)((d + delta) % 8); tron@3158: } tron@3158: tron@3158: tron@3158: static inline Direction ChangeDir(Direction d, DirDiff delta) tron@3158: { tron@3158: return (Direction)((d + delta) % 8); tron@3158: } tron@3158: tron@3158: tron@3147: /* Direction commonly used as the direction of entering and leaving tiles, 4-way */ celestar@9895: enum DiagDirection { rubidium@5838: DIAGDIR_BEGIN = 0, belugas@6451: DIAGDIR_NE = 0, ///< Northeast, upper right on your monitor tron@3147: DIAGDIR_SE = 1, tron@3147: DIAGDIR_SW = 2, tron@3147: DIAGDIR_NW = 3, tron@3147: DIAGDIR_END, tron@3147: INVALID_DIAGDIR = 0xFF, celestar@9895: }; tron@3147: rubidium@5838: DECLARE_POSTFIX_INCREMENT(DiagDirection); rubidium@5838: rubidium@5838: /** Define basic enum properties */ rubidium@5838: template <> struct EnumPropsT : MakeEnumPropsT {}; celestar@9908: typedef TinyEnumT DiagDirectionByte; //typedefing-enumification of DiagDirection rubidium@5838: tron@3147: static inline DiagDirection ReverseDiagDir(DiagDirection d) tron@3147: { tron@3148: return (DiagDirection)(2 ^ d); tron@3147: } tron@3147: tron@3147: celestar@9895: enum DiagDirDiff { tron@3163: DIAGDIRDIFF_SAME = 0, tron@3163: DIAGDIRDIFF_90RIGHT = 1, tron@3163: DIAGDIRDIFF_REVERSE = 2, tron@3163: DIAGDIRDIFF_90LEFT = 3 celestar@9895: }; tron@3163: tron@3163: static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta) tron@3163: { tron@3163: return (DiagDirection)((d + delta) % 4); tron@3163: } tron@3163: tron@3163: tron@3153: static inline DiagDirection DirToDiagDir(Direction dir) tron@3147: { tron@3147: return (DiagDirection)(dir >> 1); tron@3147: } tron@3147: tron@3147: tron@3153: static inline Direction DiagDirToDir(DiagDirection dir) tron@3153: { tron@3153: return (Direction)(dir * 2 + 1); tron@3153: } tron@3153: tron@3153: tron@3147: /* the 2 axis */ celestar@9895: enum Axis { tron@3147: AXIS_X = 0, matthijs@3699: AXIS_Y = 1, matthijs@3699: AXIS_END celestar@9895: }; tron@3147: tron@3154: tron@4158: static inline Axis OtherAxis(Axis a) tron@4158: { tron@4158: return (Axis)(a ^ 1); tron@4158: } tron@4158: tron@4158: tron@3154: static inline Axis DiagDirToAxis(DiagDirection d) tron@3154: { tron@3154: return (Axis)(d & 1); tron@3154: } tron@3154: tron@3209: tron@3209: /* tron@3209: * Converts an Axis to a DiagDirection tron@3209: * Points always in the positive direction, i.e. S[EW] tron@3209: */ tron@3209: static inline DiagDirection AxisToDiagDir(Axis a) tron@3209: { tron@3209: return (DiagDirection)(2 - a); tron@3209: } tron@3209: tron@3953: /** tron@3953: * Convert an axis and a flag for north/south into a DiagDirection celestar@9908: * @param xy axis to convert tron@3953: * @param ns north -> 0, south -> 1 celestar@9908: * @return the desired DiagDirection tron@3953: */ tron@3953: static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns) tron@3953: { tron@3953: return (DiagDirection)(xy * 3 ^ ns * 2); tron@3953: } tron@3953: tron@3953: matthijs@3699: static inline bool IsValidDiagDirection(DiagDirection d) matthijs@3699: { matthijs@3699: return d < DIAGDIR_END; matthijs@3699: } matthijs@3699: glx@3744: static inline bool IsValidDirection(Direction d) matthijs@3699: { matthijs@3699: return d < DIR_END; matthijs@3699: } matthijs@3699: glx@3744: static inline bool IsValidAxis(Axis d) matthijs@3699: { matthijs@3699: return d < AXIS_END; matthijs@3699: } matthijs@3699: peter1138@4666: #endif /* DIRECTION_H */