GME  13
ArHelper.h
Go to the documentation of this file.
00001 
00002 #ifndef __ARHELPER_H
00003 #define __ARHELPER_H
00004 
00005 #include "../GmeStd.h"
00006 #include "Gme.h"
00007 #include <vector>
00008 
00009 #ifdef _DEBUG
00010 //      #define _DEBUG_DEEP
00011 #endif
00012 
00013 
00014 // --------------------------- RoutingDirection && PathCustomizationType && CustomPathData
00015 
00016 enum RoutingDirection
00017 {
00018         Dir_None        = -1,
00019         Dir_Top         = 0,
00020         Dir_Right       = 1,
00021         Dir_Bottom      = 2,
00022         Dir_Left        = 3,
00023         Dir_Skew        = 4
00024 };
00025 
00026 // Type of edge customization
00027 enum PathCustomizationType
00028 {
00029         Invalid                                         = 0,
00030         SimpleEdgeDisplacement          = 1,
00031         CustomPointCustomization        = 2,
00032         NURBSEdgeCustomization          = 3,
00033         NumOfPathCustomizationTypes     = 4
00034 };
00035 
00036 // Version of edge data structure
00037 #define CONNECTIONCUSTOMIZATIONDATAVERSION                      0
00038 #define EMPTYCONNECTIONCUSTOMIZATIONDATAMAGIC           "-1"
00039 
00040 // Structure for storing edge customization data
00041 class CustomPathData
00042 {
00043 private:
00044         long version;                                   // Version number of structure
00045         long aspect;                                    // Which aspect does customization refers to
00046         long edgeIndex;                                 // The index of the given edge in the edge array
00047         long edgeCount;                                 // The number of the edges in the array
00048         PathCustomizationType type;             // Type of the customization: simple displacement, NURBS curving, etc
00049         bool horizontalOrVerticalEdge;  // Tells if the displacement to an x or y coordinate
00050         long x;                                                 // An x coordinate
00051         long y;                                                 // An y coordinate
00052 
00053         std::vector<long> l;                    // Additional long data
00054         std::vector<double> d;                  // Additional double data
00055 
00056 public:
00057         CustomPathData();
00058         CustomPathData& operator=(const CustomPathData& other);
00059 
00060         void Serialize(CString& outChannel);
00061         bool Deserialize(const CString& inChannel);
00062 
00063         long GetVersion(void) const;
00064         void SetVersion(long ver);
00065         long GetAspect(void) const;
00066         void SetAspect(long asp);
00067         long GetEdgeIndex(void) const;
00068         void SetEdgeIndex(long index);
00069         long GetEdgeCount(void) const;
00070         void SetEdgeCount(long count);
00071         PathCustomizationType GetType(void) const;
00072         void SetType(PathCustomizationType typ);
00073         bool IsHorizontalOrVertical(void) const;
00074         void SetHorizontalOrVertical(bool parity);
00075         long GetX(void) const;
00076         void SetX(long coord);
00077         long GetY(void) const;
00078         void SetY(long coord);
00079 
00080         long GetLongDataCount(void) const;
00081         long GetLongData(long index) const;
00082         void SetLongData(long index, long dat);
00083         void AddLongData(long dat);
00084         long GetDoubleDataCount(void) const;
00085         double GetDoubleData(long index) const;
00086         void SetDoubleData(long index, double dat);
00087         void AddDoubleData(double dat);
00088 };
00089 
00090 
00091 // --------------------------- CRect && CPoint
00092 
00093 static const CPoint emptyPoint = CPoint(-100000, -100000);
00094 
00095 CRect InflatedRect(const CRect& rect, int a);
00096 CRect DeflatedRect(const CRect& rect, int a);
00097 
00098 bool IsPointNear(const CPoint& p1, const CPoint& p2, int nearness = 0);
00099 bool IsPointIn(const CPoint& point, const CRect& rect, int nearness = 0);
00100 bool IsRectIn(const CRect& r1, const CRect& r2);
00101 bool IsRectClip(const CRect& r1, const CRect& r2);
00102 
00103 bool IsPointNearHLine(const CPoint& p, long x1, long x2, long y, int nearness = 0);
00104 bool IsPointNearVLine(const CPoint& p, long y1, long y2, long x, int nearness = 0);
00105 int DistanceFromHLine(const CPoint& p, long x1, long x2, long y);
00106 int DistanceFromVLine(const CPoint& p, long y1, long y2, long x);
00107 double DistanceSquareFromLine(const CPoint& start, const CPoint& end, const CPoint& pt);
00108 bool IsOnEdge(const CPoint& start, const CPoint& end, const CPoint& pt, int nearness = 3);
00109 
00110 bool IsPointNearLine(const CPoint& point, const CPoint& start, const CPoint& end, int nearness = 0);
00111 bool IsLineMeetHLine(const CPoint& start, const CPoint& end, long x1, long x2, long y);
00112 bool IsLineMeetVLine(const CPoint& start, const CPoint& end, long y1, long y2, long x);
00113 bool IsLineClipRect(const CPoint& start, const CPoint& end, const CRect& rect);
00114 bool Intersect(long first_x1, long first_x2, long second_x1, long second_x2);
00115 
00116 
00117 inline bool IsHorizontal(RoutingDirection dir) { return dir == Dir_Right || dir == Dir_Left; }
00118 inline bool IsVertical(RoutingDirection dir) { return dir == Dir_Top || dir == Dir_Bottom; }
00119 inline bool IsRightAngle(RoutingDirection dir) { return Dir_Top <= dir && dir <= Dir_Left; }
00120 inline bool IsTopLeft(RoutingDirection dir) { return dir == Dir_Top || dir == Dir_Left; }
00121 inline bool IsBottomRight(RoutingDirection dir) { return dir == Dir_Bottom || dir == Dir_Right; }
00122 bool AreInRightAngle(RoutingDirection dir1, RoutingDirection dir2);
00123 RoutingDirection NextClockwiseDir(RoutingDirection dir);
00124 RoutingDirection PrevClockwiseDir(RoutingDirection dir);
00125 RoutingDirection ReverseDir(RoutingDirection dir);
00126 
00127 CPoint StepOneInDir(const CPoint& point, RoutingDirection dir);
00128 long& GetRectCoord(CRect& rect, RoutingDirection dir);
00129 long GetRectOuterCoord(const CRect& rect, RoutingDirection dir);
00130 inline long& GetPointCoord(CPoint& point, bool ishorizontal) { return ishorizontal ? point.x : point.y; }
00131 inline long& GetPointCoord(CPoint& point, RoutingDirection dir) { return IsHorizontal(dir) ? point.x : point.y; }
00132 inline long ChooseInDir(long a, long b, RoutingDirection dir) { return IsTopLeft(dir) ? min(a,b) : max(a,b); }
00133 
00134 RoutingDirection GetMajorDir(const CSize& offset);
00135 RoutingDirection GetMinorDir(const CSize& offset);
00136 RoutingDirection ExGetMajorDir(const CSize& offset);
00137 RoutingDirection ExGetMinorDir(const CSize& offset);
00138 RoutingDirection GetDir(const CSize& offset, RoutingDirection nodir = Dir_None);
00139 RoutingDirection GetSkewDir(const CSize& offset, RoutingDirection nodir = Dir_None);
00140 bool IsPointInDirFrom(const CPoint& point, const CPoint& from, RoutingDirection dir);
00141 bool IsPointInDirFrom(const CPoint& point, const CRect& rect, RoutingDirection dir);
00142 bool IsPointBetweenSides(const CPoint& point, const CRect& rect, bool ishorizontal = true);
00143 inline bool IsPointBetweenSides(const CPoint& point, const CRect& rect, RoutingDirection dir) { return IsPointBetweenSides(point, rect, IsHorizontal(dir)); }
00144 RoutingDirection PointOnSide(const CPoint& point, const CRect& rect);
00145 bool IsCoordInDirFrom(long coord, long from, RoutingDirection dir);
00146 
00147 RoutingDirection OnWhichEdge(const CRect& rect, const CPoint& point);
00148 
00149 // --------------------------- CArFindNearestLine
00150 
00151 
00152 class CArFindNearestLine
00153 {
00154 public:
00155         CArFindNearestLine(const CPoint& p): point(p), dist1(INT_MAX), dist2(INT_MAX) { }
00156 
00157         bool HLine(int x1, int x2, int y);
00158         bool VLine(int y1, int y2, int x);
00159         bool Was() { return dist1 < INT_MAX && dist2 < INT_MAX; }
00160 
00161 public:
00162         CPoint point;
00163         int dist1;              // |(x,y)| = max(|x|,|y|)
00164         int dist2;              // |(x,y)| = |x| or |y|
00165 };
00166 
00167 
00168 // --------------------------- CPointListPath
00169 
00170 class CPointListPath : public CPointList
00171 {
00172 public:
00173         POSITION GetHeadEdge(CPoint& start, CPoint& end) const;
00174         POSITION GetTailEdge(CPoint& start, CPoint& end) const;
00175         void GetNextEdge(POSITION& pos, CPoint& start, CPoint& end) const;
00176         void GetPrevEdge(POSITION& pos, CPoint& start, CPoint& end) const;
00177         void GetEdge(POSITION pos, CPoint& start, CPoint& end) const;
00178 
00179         POSITION GetHeadEdgePtrs(CPoint*& start, CPoint*& end);
00180         POSITION GetTailEdgePtrs(CPoint*& start, CPoint*& end);
00181         void GetNextEdgePtrs(POSITION& pos, CPoint*& start, CPoint*& end);
00182         void GetPrevEdgePtrs(POSITION& pos, CPoint*& start, CPoint*& end);
00183         void GetEdgePtrs(POSITION pos, CPoint*& start, CPoint*& end);
00184         CPoint* GetStartPoint(POSITION pos);
00185         CPoint* GetEndPoint(POSITION pos);
00186         CPoint* GetPointBeforeEdge(POSITION pos);
00187         CPoint* GetPointAfterEdge(POSITION pos);
00188 
00189         POSITION GetEdgePosBeforePoint(POSITION pos) const;
00190         POSITION GetEdgePosAfterPoint(POSITION pos) const;
00191         POSITION GetEdgePosForStartPoint(const CPoint& startpoint);
00192 
00193 // --- Debug
00194 
00195 #ifdef _DEBUG
00196 public:
00197         void AssertValidPos(POSITION pos) const;
00198         void DumpPoints(const CString& msg) const;
00199 #endif
00200 };
00201 
00202 
00203 #endif//__ARHELPER_H