line2d.h 13 KB

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