line2d.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef IRR_LINE_2D_H_INCLUDED
  5. #define IRR_LINE_2D_H_INCLUDED
  6. #include "irrTypes.h"
  7. #include "vector2d.h"
  8. namespace irr
  9. {
  10. namespace core
  11. {
  12. //! 2D line between two points with intersection methods.
  13. template <class T>
  14. class line2d
  15. {
  16. public:
  17. //! Default constructor for line going from (0,0) to (1,1).
  18. line2d() : start(0,0), end(1,1) {}
  19. //! Constructor for line between the two points.
  20. line2d(T xa, T ya, T xb, T yb) : start(xa, ya), end(xb, yb) {}
  21. //! Constructor for line between the two points given as vectors.
  22. line2d(const vector2d<T>& start, const vector2d<T>& end) : start(start), end(end) {}
  23. // operators
  24. line2d<T> operator+(const vector2d<T>& point) const { return line2d<T>(start + point, end + point); }
  25. line2d<T>& operator+=(const vector2d<T>& point) { start += point; end += point; return *this; }
  26. line2d<T> operator-(const vector2d<T>& point) const { return line2d<T>(start - point, end - point); }
  27. line2d<T>& operator-=(const vector2d<T>& point) { start -= point; end -= point; return *this; }
  28. bool operator==(const line2d<T>& other) const
  29. { return (start==other.start && end==other.end) || (end==other.start && start==other.end);}
  30. bool operator!=(const line2d<T>& other) const
  31. { return !(start==other.start && end==other.end) || (end==other.start && start==other.end);}
  32. // functions
  33. //! Set this line to new line going through the two points.
  34. void setLine(const T& xa, const T& ya, const T& xb, const T& yb){start.set(xa, ya); end.set(xb, yb);}
  35. //! Set this line to new line going through the two points.
  36. void setLine(const vector2d<T>& nstart, const vector2d<T>& nend){start.set(nstart); end.set(nend);}
  37. //! Set this line to new line given as parameter.
  38. void setLine(const line2d<T>& line){start.set(line.start); end.set(line.end);}
  39. //! Get length of line
  40. /** \return Length of the line. */
  41. T getLength() const { return start.getDistanceFrom(end); }
  42. //! Get squared length of the line
  43. /** \return Squared length of line. */
  44. T getLengthSQ() const { return start.getDistanceFromSQ(end); }
  45. //! Get middle of the line
  46. /** \return center of the line. */
  47. vector2d<T> getMiddle() const
  48. {
  49. return (start + end)/(T)2;
  50. }
  51. //! Get the vector of the line.
  52. /** \return The vector of the line. */
  53. vector2d<T> getVector() const { return vector2d<T>( end.X - start.X, end.Y - start.Y); }
  54. /*! Check if this segment intersects another segment,
  55. or if segments are coincident (colinear). */
  56. bool intersectAsSegments( const line2d<T>& other) const
  57. {
  58. // Taken from:
  59. // http://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
  60. // Find the four orientations needed for general and
  61. // special cases
  62. s32 o1 = start.checkOrientation( end, other.start);
  63. s32 o2 = start.checkOrientation( end, other.end);
  64. s32 o3 = other.start.checkOrientation( other.end, start);
  65. s32 o4 = other.start.checkOrientation( other.end, end);
  66. // General case
  67. if (o1 != o2 && o3 != o4)
  68. return true;
  69. // Special Cases to check if segments are colinear
  70. if (o1 == 0 && other.start.isBetweenPoints( start, end)) return true;
  71. if (o2 == 0 && other.end.isBetweenPoints( start, end)) return true;
  72. if (o3 == 0 && start.isBetweenPoints( other.start, other.end)) return true;
  73. if (o4 == 0 && end.isBetweenPoints( other.start, other.end)) return true;
  74. return false; // Doesn't fall in any of the above cases
  75. }
  76. /*! Check if 2 segments are incident (intersects in exactly 1 point).*/
  77. bool incidentSegments( const line2d<T>& other) const
  78. {
  79. return
  80. start.checkOrientation( end, other.start) != start.checkOrientation( end, other.end)
  81. && other.start.checkOrientation( other.end, start) != other.start.checkOrientation( other.end, end);
  82. }
  83. /*! Check if 2 lines/segments are parallel or nearly parallel.*/
  84. bool nearlyParallel( const line2d<T>& line, const T factor = relativeErrorFactor<T>()) const
  85. {
  86. const vector2d<T> a = getVector();
  87. const vector2d<T> b = line.getVector();
  88. return a.nearlyParallel( b, factor);
  89. }
  90. /*! returns a intersection point of 2 lines (if lines are not parallel). Behaviour
  91. undefined if lines are parallel or coincident.
  92. It's on optimized intersectWith with checkOnlySegments=false and ignoreCoincidentLines=true
  93. */
  94. vector2d<T> fastLinesIntersection( const line2d<T>& l) const
  95. {
  96. const f32 commonDenominator = (f32)((l.end.Y - l.start.Y)*(end.X - start.X) -
  97. (l.end.X - l.start.X)*(end.Y - start.Y));
  98. if ( commonDenominator != 0.f )
  99. {
  100. const f32 numeratorA = (f32)((l.end.X - l.start.X)*(start.Y - l.start.Y) -
  101. (l.end.Y - l.start.Y)*(start.X - l.start.X));
  102. const f32 uA = numeratorA / commonDenominator;
  103. // Calculate the intersection point.
  104. return vector2d<T> (
  105. (T)(start.X + uA * (end.X - start.X)),
  106. (T)(start.Y + uA * (end.Y - start.Y))
  107. );
  108. }
  109. else
  110. return l.start;
  111. }
  112. /*! Check if this line intersect a segment. The eventual intersection point is returned in "out".*/
  113. bool lineIntersectSegment( const line2d<T>& segment, vector2d<T> & out) const
  114. {
  115. if (nearlyParallel( segment))
  116. return false;
  117. out = fastLinesIntersection( segment);
  118. return out.isBetweenPoints( segment.start, segment.end);
  119. }
  120. //! Tests if this line intersects with another line.
  121. /** \param l: Other line to test intersection with.
  122. \param checkOnlySegments: Default is to check intersection between the begin and endpoints.
  123. When set to false the function will check for the first intersection point when extending the lines.
  124. \param out: If there is an intersection, the location of the
  125. intersection will be stored in this vector.
  126. \param ignoreCoincidentLines: When true coincident lines (lines above each other) are never considered as intersecting.
  127. When false the center of the overlapping part is returned.
  128. \return True if there is an intersection, false if not. */
  129. bool intersectWith(const line2d<T>& l, vector2d<T>& out, bool checkOnlySegments=true, bool ignoreCoincidentLines=false) const
  130. {
  131. // Uses the method given at:
  132. // http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
  133. const f64 commonDenominator = (f64)((l.end.Y - l.start.Y)*(end.X - start.X) -
  134. (l.end.X - l.start.X)*(end.Y - start.Y));
  135. const f64 numeratorA = (f64)((l.end.X - l.start.X)*(start.Y - l.start.Y) -
  136. (l.end.Y - l.start.Y)*(start.X -l.start.X));
  137. const f64 numeratorB = (f64)((end.X - start.X)*(start.Y - l.start.Y) -
  138. (end.Y - start.Y)*(start.X -l.start.X));
  139. if(equals(commonDenominator, 0.0))
  140. {
  141. // The lines are either coincident or parallel
  142. // if both numerators are 0, the lines are coincident
  143. if(!ignoreCoincidentLines && equals(numeratorA, 0.0) && equals(numeratorB, 0.0))
  144. {
  145. // Try and find a common endpoint
  146. if(l.start == start || l.end == start)
  147. out = start;
  148. else if(l.end == end || l.start == end)
  149. out = end;
  150. // now check if the two segments are disjunct
  151. else if (l.start.X>start.X && l.end.X>start.X && l.start.X>end.X && l.end.X>end.X)
  152. return false;
  153. else if (l.start.Y>start.Y && l.end.Y>start.Y && l.start.Y>end.Y && l.end.Y>end.Y)
  154. return false;
  155. else if (l.start.X<start.X && l.end.X<start.X && l.start.X<end.X && l.end.X<end.X)
  156. return false;
  157. else if (l.start.Y<start.Y && l.end.Y<start.Y && l.start.Y<end.Y && l.end.Y<end.Y)
  158. return false;
  159. // else the lines are overlapping to some extent
  160. else
  161. {
  162. // find the points which are not contributing to the
  163. // common part
  164. vector2d<T> maxp;
  165. vector2d<T> minp;
  166. if ((start.X>l.start.X && start.X>l.end.X && start.X>end.X) || (start.Y>l.start.Y && start.Y>l.end.Y && start.Y>end.Y))
  167. maxp=start;
  168. else if ((end.X>l.start.X && end.X>l.end.X && end.X>start.X) || (end.Y>l.start.Y && end.Y>l.end.Y && end.Y>start.Y))
  169. maxp=end;
  170. else if ((l.start.X>start.X && l.start.X>l.end.X && l.start.X>end.X) || (l.start.Y>start.Y && l.start.Y>l.end.Y && l.start.Y>end.Y))
  171. maxp=l.start;
  172. else
  173. maxp=l.end;
  174. if (maxp != start && ((start.X<l.start.X && start.X<l.end.X && start.X<end.X) || (start.Y<l.start.Y && start.Y<l.end.Y && start.Y<end.Y)))
  175. minp=start;
  176. else if (maxp != end && ((end.X<l.start.X && end.X<l.end.X && end.X<start.X) || (end.Y<l.start.Y && end.Y<l.end.Y && end.Y<start.Y)))
  177. minp=end;
  178. else if (maxp != l.start && ((l.start.X<start.X && l.start.X<l.end.X && l.start.X<end.X) || (l.start.Y<start.Y && l.start.Y<l.end.Y && l.start.Y<end.Y)))
  179. minp=l.start;
  180. else
  181. minp=l.end;
  182. // one line is contained in the other. Pick the center
  183. // of the remaining points, which overlap for sure
  184. out = core::vector2d<T>();
  185. if (start != maxp && start != minp)
  186. out += start;
  187. if (end != maxp && end != minp)
  188. out += end;
  189. if (l.start != maxp && l.start != minp)
  190. out += l.start;
  191. if (l.end != maxp && l.end != minp)
  192. out += l.end;
  193. out.X = (T)(out.X/2);
  194. out.Y = (T)(out.Y/2);
  195. }
  196. return true; // coincident
  197. }
  198. return false; // parallel
  199. }
  200. // Get the point of intersection on this line, checking that
  201. // it is within the line segment.
  202. const f64 uA = numeratorA / commonDenominator;
  203. if (checkOnlySegments)
  204. {
  205. if(uA < 0.0 || uA > 1.0)
  206. return false; // Outside the line segment
  207. const f64 uB = numeratorB / commonDenominator;
  208. if(uB < 0.0 || uB > 1.0)
  209. return false; // Outside the line segment
  210. }
  211. // Calculate the intersection point.
  212. out.X = (T)(start.X + uA * (end.X - start.X));
  213. out.Y = (T)(start.Y + uA * (end.Y - start.Y));
  214. return true;
  215. }
  216. //! Get unit vector of the line.
  217. /** \return Unit vector of this line. */
  218. vector2d<T> getUnitVector() const
  219. {
  220. T len = (T)(1.0 / getLength());
  221. return vector2d<T>((end.X - start.X) * len, (end.Y - start.Y) * len);
  222. }
  223. //! Get angle between this line and given line.
  224. /** \param l Other line for test.
  225. \return Angle in degrees. */
  226. f64 getAngleWith(const line2d<T>& l) const
  227. {
  228. vector2d<T> vect = getVector();
  229. vector2d<T> vect2 = l.getVector();
  230. return vect.getAngleWith(vect2);
  231. }
  232. //! Tells us if the given point lies to the left, right, or on the line.
  233. /** \return 0 if the point is on the line
  234. <0 if to the left, or >0 if to the right. */
  235. T getPointOrientation(const vector2d<T>& point) const
  236. {
  237. return ( (end.X - start.X) * (point.Y - start.Y) -
  238. (point.X - start.X) * (end.Y - start.Y) );
  239. }
  240. //! Check if the given point is a member of the line
  241. /** \return True if point is between start and end, else false. */
  242. bool isPointOnLine(const vector2d<T>& point) const
  243. {
  244. T d = getPointOrientation(point);
  245. return (d == 0 && point.isBetweenPoints(start, end));
  246. }
  247. //! Check if the given point is between start and end of the line.
  248. /** Assumes that the point is already somewhere on the line. */
  249. bool isPointBetweenStartAndEnd(const vector2d<T>& point) const
  250. {
  251. return point.isBetweenPoints(start, end);
  252. }
  253. //! Get the closest point on this line to a point
  254. /** \param point: Starting search at this point
  255. \param checkOnlySegments: Default (true) is to return a point on the line-segment (between begin and end) of the line.
  256. When set to false the function will check for the first the closest point on the the line even when outside the segment. */
  257. vector2d<T> getClosestPoint(const vector2d<T>& point, bool checkOnlySegments=true) const
  258. {
  259. vector2d<f64> c((f64)(point.X-start.X), (f64)(point.Y- start.Y));
  260. vector2d<f64> v((f64)(end.X-start.X), (f64)(end.Y-start.Y));
  261. f64 d = v.getLength();
  262. if ( d == 0 ) // can't tell much when the line is just a single point
  263. return start;
  264. v /= d;
  265. f64 t = v.dotProduct(c);
  266. if ( checkOnlySegments )
  267. {
  268. if (t < 0) return vector2d<T>((T)start.X, (T)start.Y);
  269. if (t > d) return vector2d<T>((T)end.X, (T)end.Y);
  270. }
  271. v *= t;
  272. return vector2d<T>((T)(start.X + v.X), (T)(start.Y + v.Y));
  273. }
  274. //! Start point of the line.
  275. vector2d<T> start;
  276. //! End point of the line.
  277. vector2d<T> end;
  278. };
  279. // partial specialization to optimize <f32> lines (avoiding casts)
  280. template <>
  281. inline vector2df line2d<irr::f32>::getClosestPoint(const vector2df& point, bool checkOnlySegments) const
  282. {
  283. const vector2df c = point - start;
  284. vector2df v = end - start;
  285. const f32 d = (f32)v.getLength();
  286. if ( d == 0 ) // can't tell much when the line is just a single point
  287. return start;
  288. v /= d;
  289. const f32 t = v.dotProduct(c);
  290. if ( checkOnlySegments )
  291. {
  292. if (t < 0) return start;
  293. if (t > d) return end;
  294. }
  295. v *= t;
  296. return start + v;
  297. }
  298. //! Typedef for an f32 line.
  299. typedef line2d<f32> line2df;
  300. //! Typedef for an integer line.
  301. typedef line2d<s32> line2di;
  302. } // end namespace core
  303. } // end namespace irr
  304. #endif