BSPPolygon2.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Logger.h>
  9. #include <Mathematics/Vector2.h>
  10. #include <Mathematics/EdgeKey.h>
  11. #include <list>
  12. #include <map>
  13. #include <memory>
  14. #include <vector>
  15. #define GTE_BSPPOLYGON2_ENABLE_DEBUG_PRINT
  16. #if defined(GTE_BSPPOLYGON2_ENABLE_DEBUG_PRINT)
  17. #include <fstream>
  18. #endif
  19. namespace WwiseGTE
  20. {
  21. template <typename Real>
  22. class BSPPolygon2
  23. {
  24. public:
  25. // vertices
  26. typedef Vector2<Real> Vertex;
  27. typedef std::map<Vertex, int> VMap;
  28. typedef std::vector<Vertex> VArray;
  29. // edges
  30. typedef EdgeKey<false> Edge;
  31. typedef std::map<Edge, int> EMap;
  32. typedef std::vector<Edge> EArray;
  33. // Construction and destruction.
  34. BSPPolygon2(Real epsilon)
  35. :
  36. mEpsilon(epsilon >= (Real)0 ? epsilon : (Real)0)
  37. {
  38. }
  39. ~BSPPolygon2()
  40. {
  41. }
  42. // Copy semantics.
  43. BSPPolygon2(BSPPolygon2 const& polygon)
  44. {
  45. *this = polygon;
  46. }
  47. BSPPolygon2& operator=(BSPPolygon2 const& polygon)
  48. {
  49. mEpsilon = polygon.mEpsilon;
  50. mVMap = polygon.mVMap;
  51. mVArray = polygon.mVArray;
  52. mEMap = polygon.mEMap;
  53. mEArray = polygon.mEArray;
  54. mTree = (polygon.mTree ? polygon.mTree->GetCopy() : nullptr);
  55. return *this;
  56. }
  57. // Move semantics.
  58. BSPPolygon2(BSPPolygon2&& polygon)
  59. {
  60. *this = std::move(polygon);
  61. }
  62. BSPPolygon2& operator=(BSPPolygon2&& polygon)
  63. {
  64. mEpsilon = polygon.mEpsilon;
  65. mVMap = std::move(polygon.mVMap);
  66. mVArray = std::move(polygon.mVArray);
  67. mEMap = std::move(polygon.mEMap);
  68. mEArray = std::move(polygon.mEArray);
  69. mTree = polygon.mTree;
  70. polygon.mTree = nullptr;
  71. return *this;
  72. }
  73. // Support for deferred construction.
  74. int InsertVertex(Vertex const& vertex)
  75. {
  76. auto iter = mVMap.find(vertex);
  77. if (iter != mVMap.end())
  78. {
  79. // Vertex already in map, just return its unique index.
  80. return iter->second;
  81. }
  82. // Vertex not in map, insert it and assign it a unique index.
  83. int i = static_cast<int>(mVArray.size());
  84. mVMap.insert(std::make_pair(vertex, i));
  85. mVArray.push_back(vertex);
  86. return i;
  87. }
  88. int InsertEdge(Edge const& edge)
  89. {
  90. LogAssert(edge.V[0] != edge.V[1], "Degenerate edges not allowed.");
  91. auto iter = mEMap.find(edge);
  92. if (iter != mEMap.end())
  93. {
  94. // Edge already in map, just return its unique index.
  95. return iter->second;
  96. }
  97. // Edge not in map, insert it and assign it a unique index.
  98. int i = static_cast<int>(mEArray.size());
  99. mEMap.insert(std::make_pair(edge, i));
  100. mEArray.push_back(edge);
  101. return i;
  102. }
  103. void Finalize()
  104. {
  105. mTree = std::make_shared<BSPTree2>(*this, mEArray, mEpsilon);
  106. }
  107. // Member access.
  108. inline int GetNumVertices() const
  109. {
  110. return static_cast<int>(mVMap.size());
  111. }
  112. inline Vertex const& GetVertex(int i) const
  113. {
  114. return mVArray[i];
  115. }
  116. inline int GetNumEdges() const
  117. {
  118. return static_cast<int>(mEMap.size());
  119. }
  120. inline Edge const& GetEdge(int i) const
  121. {
  122. return mEArray[i];
  123. }
  124. // negation
  125. BSPPolygon2 operator~() const
  126. {
  127. LogAssert(mTree != nullptr, "Tree must exist.");
  128. BSPPolygon2 neg(mEpsilon);
  129. neg.mVMap = mVMap;
  130. neg.mVArray = mVArray;
  131. for (auto const& element : mEMap)
  132. {
  133. auto const& edge = element.first;
  134. neg.InsertEdge(Edge(edge.V[1], edge.V[0]));
  135. }
  136. neg.mTree = mTree->GetCopy();
  137. neg.mTree->Negate();
  138. return neg;
  139. }
  140. // intersection
  141. BSPPolygon2 operator&(BSPPolygon2 const& polygon) const
  142. {
  143. LogAssert(mTree != nullptr, "Tree must exist.");
  144. BSPPolygon2 intersect(mEpsilon);
  145. GetInsideEdgesFrom(polygon, intersect);
  146. polygon.GetInsideEdgesFrom(*this, intersect);
  147. intersect.Finalize();
  148. return intersect;
  149. }
  150. // union
  151. BSPPolygon2 operator|(BSPPolygon2 const& polygon) const
  152. {
  153. return ~(~*this & ~polygon);
  154. }
  155. // difference
  156. BSPPolygon2 operator-(BSPPolygon2 const& polygon) const
  157. {
  158. return *this & ~polygon;
  159. }
  160. // exclusive or
  161. BSPPolygon2 operator^(BSPPolygon2 const& polygon) const
  162. {
  163. return (*this - polygon) | (polygon - *this);
  164. }
  165. // point location (-1 inside, 0 on polygon, 1 outside)
  166. int PointLocation(Vertex const& vertex) const
  167. {
  168. LogAssert(mTree != nullptr, "Tree must exist.");
  169. return mTree->PointLocation(*this, vertex);
  170. }
  171. #if defined(GTE_BSPPOLYGON2_ENABLE_DEBUG_PRINT)
  172. // debugging support
  173. void Print(std::string const& filename) const
  174. {
  175. std::ofstream output(filename);
  176. int const numVertices = GetNumVertices();
  177. output << "vquantity = " << numVertices << std::endl;
  178. for (int i = 0; i < numVertices; ++i)
  179. {
  180. output << i << " (" << mVArray[i][0] << ',' << mVArray[i][1] << ')' << std::endl;
  181. }
  182. output << std::endl;
  183. int const numEdges = GetNumEdges();
  184. output << "equantity = " << numEdges << std::endl;
  185. for (int i = 0; i < numEdges; ++i)
  186. {
  187. output << " <" << mEArray[i].V[0] << ',' << mEArray[i].V[1] << '>' << std::endl;
  188. }
  189. output << std::endl;
  190. output << "bsp tree" << std::endl;
  191. if (mTree)
  192. {
  193. mTree->Print(output, 0, 'r');
  194. }
  195. output << std::endl;
  196. output.close();
  197. }
  198. #endif
  199. private:
  200. // Binary space partitioning tree support for polygon Boolean
  201. // operations.
  202. class BSPTree2
  203. {
  204. public:
  205. // Construction and destruction.
  206. BSPTree2(Real epsilon)
  207. :
  208. mEpsilon(epsilon >= (Real)0 ? epsilon : (Real)0)
  209. {
  210. }
  211. BSPTree2(BSPPolygon2& polygon, EArray const& edges, Real epsilon)
  212. :
  213. mEpsilon(epsilon >= (Real)0 ? epsilon : (Real)0)
  214. {
  215. LogAssert(edges.size() > 0, "Invalid input.");
  216. // Construct splitting line from first edge.
  217. Vertex end0 = polygon.GetVertex(edges[0].V[0]);
  218. Vertex end1 = polygon.GetVertex(edges[0].V[1]);
  219. // Add edge to coincident list.
  220. mCoincident.push_back(edges[0]);
  221. // Split remaining edges.
  222. EArray posArray, negArray;
  223. for (size_t i = 1; i < edges.size(); ++i)
  224. {
  225. int v0 = edges[i].V[0];
  226. int v1 = edges[i].V[1];
  227. Vertex vertex0 = polygon.GetVertex(v0);
  228. Vertex vertex1 = polygon.GetVertex(v1);
  229. Vertex intr;
  230. int vmid;
  231. switch (Classify(end0, end1, vertex0, vertex1, intr))
  232. {
  233. case TRANSVERSE_POSITIVE:
  234. // modify edge <V0,V1> to <V0,I>, add new edge <I,V1>
  235. vmid = polygon.InsertVertex(intr);
  236. polygon.SplitEdge(v0, v1, vmid);
  237. posArray.push_back(Edge(vmid, v1));
  238. negArray.push_back(Edge(v0, vmid));
  239. break;
  240. case TRANSVERSE_NEGATIVE:
  241. // modify edge <V0,V1> to <V0,I>, add new edge <I,V1>
  242. vmid = polygon.InsertVertex(intr);
  243. polygon.SplitEdge(v0, v1, vmid);
  244. posArray.push_back(Edge(v0, vmid));
  245. negArray.push_back(Edge(vmid, v1));
  246. break;
  247. case ALL_POSITIVE:
  248. posArray.push_back(edges[i]);
  249. break;
  250. case ALL_NEGATIVE:
  251. negArray.push_back(edges[i]);
  252. break;
  253. default: // COINCIDENT
  254. mCoincident.push_back(edges[i]);
  255. break;
  256. }
  257. }
  258. if (posArray.size() > 0)
  259. {
  260. mPosChild = std::make_shared<BSPTree2>(polygon, posArray, mEpsilon);
  261. }
  262. if (negArray.size() > 0)
  263. {
  264. mNegChild = std::make_shared<BSPTree2>(polygon, negArray, mEpsilon);
  265. }
  266. }
  267. ~BSPTree2()
  268. {
  269. }
  270. // Disallow copying and assignment. Use GetCopy() instead to
  271. // obtain a deep copy of the BSP tree.
  272. BSPTree2(const BSPTree2&) = delete;
  273. BSPTree2& operator= (const BSPTree2&) = delete;
  274. std::shared_ptr<BSPTree2> GetCopy() const
  275. {
  276. auto tree = std::make_shared<BSPTree2>(mEpsilon);
  277. tree->mCoincident = mCoincident;
  278. if (mPosChild)
  279. {
  280. tree->mPosChild = mPosChild->GetCopy();
  281. }
  282. if (mNegChild)
  283. {
  284. tree->mNegChild = mNegChild->GetCopy();
  285. }
  286. return tree;
  287. }
  288. // Polygon Boolean operation support.
  289. void Negate()
  290. {
  291. // Reverse coincident edge directions.
  292. for (auto& edge : mCoincident)
  293. {
  294. std::swap(edge.V[0], edge.V[1]);
  295. }
  296. // Swap positive and negative subtrees.
  297. std::swap(mPosChild, mNegChild);
  298. if (mPosChild)
  299. {
  300. mPosChild->Negate();
  301. }
  302. if (mNegChild)
  303. {
  304. mNegChild->Negate();
  305. }
  306. }
  307. void GetPartition(BSPPolygon2 const& polygon, Vertex const& v0,
  308. Vertex const& v1, BSPPolygon2& pos, BSPPolygon2& neg,
  309. BSPPolygon2& coSame, BSPPolygon2& coDiff) const
  310. {
  311. // Construct splitting line from first coincident edge.
  312. Vertex end0 = polygon.GetVertex(mCoincident[0].V[0]);
  313. Vertex end1 = polygon.GetVertex(mCoincident[0].V[1]);
  314. Vertex intr;
  315. switch (Classify(end0, end1, v0, v1, intr))
  316. {
  317. case TRANSVERSE_POSITIVE:
  318. GetPosPartition(polygon, intr, v1, pos, neg, coSame, coDiff);
  319. GetNegPartition(polygon, v0, intr, pos, neg, coSame, coDiff);
  320. break;
  321. case TRANSVERSE_NEGATIVE:
  322. GetPosPartition(polygon, v0, intr, pos, neg, coSame, coDiff);
  323. GetNegPartition(polygon, intr, v1, pos, neg, coSame, coDiff);
  324. break;
  325. case ALL_POSITIVE:
  326. GetPosPartition(polygon, v0, v1, pos, neg, coSame, coDiff);
  327. break;
  328. case ALL_NEGATIVE:
  329. GetNegPartition(polygon, v0, v1, pos, neg, coSame, coDiff);
  330. break;
  331. default: // COINCIDENT
  332. GetCoPartition(polygon, v0, v1, pos, neg, coSame, coDiff);
  333. break;
  334. }
  335. }
  336. // Point-in-polygon support (-1 outside, 0 on polygon, +1 inside).
  337. int PointLocation(BSPPolygon2 const& polygon, Vertex const& vertex) const
  338. {
  339. // Construct splitting line from first coincident edge.
  340. Vertex end0 = polygon.GetVertex(mCoincident[0].V[0]);
  341. Vertex end1 = polygon.GetVertex(mCoincident[0].V[1]);
  342. switch (Classify(end0, end1, vertex))
  343. {
  344. case ALL_POSITIVE:
  345. if (mPosChild)
  346. {
  347. return mPosChild->PointLocation(polygon, vertex);
  348. }
  349. else
  350. {
  351. return 1;
  352. }
  353. case ALL_NEGATIVE:
  354. if (mNegChild)
  355. {
  356. return mNegChild->PointLocation(polygon, vertex);
  357. }
  358. else
  359. {
  360. return -1;
  361. }
  362. default: // COINCIDENT
  363. return CoPointLocation(polygon, vertex);
  364. }
  365. }
  366. #if defined(GTE_BSPPOLYGON2_ENABLE_DEBUG_PRINT)
  367. void Print(std::ofstream& outFile, int level, char type) const
  368. {
  369. for (size_t i = 0; i < mCoincident.size(); ++i)
  370. {
  371. for (int j = 0; j < 4 * level; ++j)
  372. {
  373. outFile << ' ';
  374. }
  375. outFile << type << " <" << mCoincident[i].V[0] << ',' <<
  376. mCoincident[i].V[1] << ">" << std::endl;
  377. }
  378. if (mPosChild)
  379. {
  380. mPosChild->Print(outFile, level + 1, 'p');
  381. }
  382. if (mNegChild)
  383. {
  384. mNegChild->Print(outFile, level + 1, 'n');
  385. }
  386. }
  387. #endif
  388. private:
  389. enum
  390. {
  391. TRANSVERSE_POSITIVE,
  392. TRANSVERSE_NEGATIVE,
  393. ALL_POSITIVE,
  394. ALL_NEGATIVE,
  395. COINCIDENT
  396. };
  397. int Classify(Vertex const& end0, Vertex const& end1,
  398. Vertex const& v0, Vertex const& v1, Vertex& intr) const
  399. {
  400. Vertex dir = end1 - end0;
  401. Vertex nor = Perp(dir);
  402. Vertex diff0 = v0 - end0;
  403. Vertex diff1 = v1 - end0;
  404. Real d0 = Dot(nor, diff0);
  405. Real d1 = Dot(nor, diff1);
  406. if (d0 * d1 < (Real)0)
  407. {
  408. // Edge <V0,V1> transversely crosses line. Compute point
  409. // of intersection I = V0 + t*(V1 - V0).
  410. Real t = d0 / (d0 - d1);
  411. if (t > mEpsilon)
  412. {
  413. if (t < (Real)1 - mEpsilon)
  414. {
  415. intr = v0 + t * (v1 - v0);
  416. if (d1 > (Real)0)
  417. {
  418. return TRANSVERSE_POSITIVE;
  419. }
  420. else
  421. {
  422. return TRANSVERSE_NEGATIVE;
  423. }
  424. }
  425. else
  426. {
  427. // T is effectively 1 (numerical round-off issue),
  428. // so set d1 = 0 and go on to other cases.
  429. d1 = (Real)0;
  430. }
  431. }
  432. else
  433. {
  434. // T is effectively 0 (numerical round-off issue), so
  435. // set d0 = 0 and go on to other cases.
  436. d0 = (Real)0;
  437. }
  438. }
  439. if (d0 > (Real)0 || d1 > (Real)0)
  440. {
  441. // edge on positive side of line
  442. return ALL_POSITIVE;
  443. }
  444. if (d0 < (Real)0 || d1 < (Real)0)
  445. {
  446. // edge on negative side of line
  447. return ALL_NEGATIVE;
  448. }
  449. return COINCIDENT;
  450. }
  451. void GetPosPartition(BSPPolygon2 const& polygon, Vertex const& v0,
  452. Vertex const& v1, BSPPolygon2& pos, BSPPolygon2& neg,
  453. BSPPolygon2& coSame, BSPPolygon2& coDiff) const
  454. {
  455. if (mPosChild)
  456. {
  457. mPosChild->GetPartition(polygon, v0, v1, pos, neg, coSame, coDiff);
  458. }
  459. else
  460. {
  461. int i0 = pos.InsertVertex(v0);
  462. int i1 = pos.InsertVertex(v1);
  463. pos.InsertEdge(Edge(i0, i1));
  464. }
  465. }
  466. void GetNegPartition(BSPPolygon2 const& polygon, Vertex const& v0,
  467. Vertex const& v1, BSPPolygon2& pos, BSPPolygon2& neg,
  468. BSPPolygon2& coSame, BSPPolygon2& coDiff) const
  469. {
  470. if (mNegChild)
  471. {
  472. mNegChild->GetPartition(polygon, v0, v1, pos, neg, coSame, coDiff);
  473. }
  474. else
  475. {
  476. int i0 = neg.InsertVertex(v0);
  477. int i1 = neg.InsertVertex(v1);
  478. neg.InsertEdge(Edge(i0, i1));
  479. }
  480. }
  481. class Interval
  482. {
  483. public:
  484. Interval(Real inT0, Real inT1, bool inSameDir, bool inTouching)
  485. :
  486. t0(inT0),
  487. t1(inT1),
  488. sameDir(inSameDir),
  489. touching(inTouching)
  490. {
  491. }
  492. Real t0, t1;
  493. bool sameDir, touching;
  494. };
  495. void GetCoPartition(BSPPolygon2 const& polygon, Vertex const& v0,
  496. Vertex const& v1, BSPPolygon2& pos, BSPPolygon2& neg,
  497. BSPPolygon2& coSame, BSPPolygon2& coDiff) const
  498. {
  499. // Segment the line containing V0 and V1 by the coincident
  500. // intervals that intersect <V0,V1>.
  501. Vertex dir = v1 - v0;
  502. Real tmax = Dot(dir, dir);
  503. Vertex end0, end1;
  504. Real t0, t1;
  505. bool sameDir;
  506. std::list<Interval> intervals;
  507. typename std::list<Interval>::iterator iter;
  508. for (auto const& edge : mCoincident)
  509. {
  510. end0 = polygon.GetVertex(edge.V[0]);
  511. end1 = polygon.GetVertex(edge.V[1]);
  512. t0 = Dot(dir, end0 - v0);
  513. if (std::fabs(t0) <= mEpsilon)
  514. {
  515. t0 = (Real)0;
  516. }
  517. else if (std::fabs(t0 - tmax) <= mEpsilon)
  518. {
  519. t0 = tmax;
  520. }
  521. t1 = Dot(dir, end1 - v0);
  522. if (std::fabs(t1) <= mEpsilon)
  523. {
  524. t1 = (Real)0;
  525. }
  526. else if (std::fabs(t1 - tmax) <= mEpsilon)
  527. {
  528. t1 = tmax;
  529. }
  530. sameDir = (t1 > t0);
  531. if (!sameDir)
  532. {
  533. Real save = t0;
  534. t0 = t1;
  535. t1 = save;
  536. }
  537. if (t1 > (Real)0 && t0 < tmax)
  538. {
  539. if (intervals.empty())
  540. {
  541. intervals.push_front(Interval(t0, t1, sameDir, true));
  542. }
  543. else
  544. {
  545. for (iter = intervals.begin(); iter != intervals.end(); ++iter)
  546. {
  547. if (std::fabs(t1 - iter->t0) <= mEpsilon)
  548. {
  549. t1 = iter->t0;
  550. }
  551. if (t1 <= iter->t0)
  552. {
  553. // [t0,t1] is on the left of [I.t0,I.t1]
  554. intervals.insert(iter, Interval(t0, t1, sameDir, true));
  555. break;
  556. }
  557. // Theoretically, the intervals are disjoint
  558. // or intersect only at an end point. The
  559. // assert makes sure that [t0,t1] is to the
  560. // right of [I.t0,I.t1].
  561. if (std::fabs(t0 - iter->t1) <= mEpsilon)
  562. {
  563. t0 = iter->t1;
  564. }
  565. LogAssert(t0 >= iter->t1, "Invalid ordering in BSPTree2::GetCoPartition.");
  566. auto last = std::prev(intervals.end());
  567. if (iter == last)
  568. {
  569. intervals.push_back(Interval(t0, t1, sameDir, true));
  570. break;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. if (intervals.empty())
  577. {
  578. GetPosPartition(polygon, v0, v1, pos, neg, coSame, coDiff);
  579. GetNegPartition(polygon, v0, v1, pos, neg, coSame, coDiff);
  580. return;
  581. }
  582. // Insert outside intervals between the touching intervals.
  583. // It is possible that two touching intervals are adjacent,
  584. // so this is not just a simple alternation of touching and
  585. // outside intervals.
  586. Interval& front = intervals.front();
  587. if (front.t0 > (Real)0)
  588. {
  589. intervals.push_front(Interval((Real)0, front.t0, front.sameDir, false));
  590. }
  591. else
  592. {
  593. front.t0 = (Real)0;
  594. }
  595. Interval& back = intervals.back();
  596. if (back.t1 < tmax)
  597. {
  598. intervals.push_back(Interval(back.t1, tmax, back.sameDir, false));
  599. }
  600. else
  601. {
  602. back.t1 = tmax;
  603. }
  604. typename std::list<Interval>::iterator iter0 = intervals.begin();
  605. typename std::list<Interval>::iterator iter1 = intervals.begin();
  606. for (++iter1; iter1 != intervals.end(); ++iter0, ++iter1)
  607. {
  608. t0 = iter0->t1;
  609. t1 = iter1->t0;
  610. if (t1 - t0 > mEpsilon)
  611. {
  612. iter0 = intervals.insert(iter1, Interval(t0, t1, true, false));
  613. }
  614. }
  615. // Process the segmentation.
  616. Real invTMax = (Real)1 / tmax;
  617. t0 = intervals.front().t0 * invTMax;
  618. end1 = v0 + (intervals.front().t0 * invTMax) * dir;
  619. for (iter = intervals.begin(); iter != intervals.end(); ++iter)
  620. {
  621. end0 = end1;
  622. t1 = iter->t1 * invTMax;
  623. end1 = v0 + (iter->t1 * invTMax) * dir;
  624. if (iter->touching)
  625. {
  626. Edge edge;
  627. if (iter->sameDir)
  628. {
  629. edge.V[0] = coSame.InsertVertex(end0);
  630. edge.V[1] = coSame.InsertVertex(end1);
  631. if (edge.V[0] != edge.V[1])
  632. {
  633. coSame.InsertEdge(edge);
  634. }
  635. }
  636. else
  637. {
  638. edge.V[0] = coDiff.InsertVertex(end1);
  639. edge.V[1] = coDiff.InsertVertex(end0);
  640. if (edge.V[0] != edge.V[1])
  641. {
  642. coDiff.InsertEdge(edge);
  643. }
  644. }
  645. }
  646. else
  647. {
  648. GetPosPartition(polygon, end0, end1, pos, neg, coSame, coDiff);
  649. GetNegPartition(polygon, end0, end1, pos, neg, coSame, coDiff);
  650. }
  651. }
  652. }
  653. // point-in-polygon support
  654. int Classify(Vertex const& end0, Vertex const& end1, Vertex const& vertex) const
  655. {
  656. Vertex dir = end1 - end0;
  657. Vertex nor = Perp(dir);
  658. Vertex diff = vertex - end0;
  659. Real c = Dot(nor, diff);
  660. if (c > mEpsilon)
  661. {
  662. return ALL_POSITIVE;
  663. }
  664. if (c < -mEpsilon)
  665. {
  666. return ALL_NEGATIVE;
  667. }
  668. return COINCIDENT;
  669. }
  670. int CoPointLocation(BSPPolygon2 const& polygon, Vertex const& vertex) const
  671. {
  672. for (auto const& edge : mCoincident)
  673. {
  674. Vertex end0 = polygon.GetVertex(edge.V[0]);
  675. Vertex end1 = polygon.GetVertex(edge.V[1]);
  676. Vertex dir = end1 - end0;
  677. Vertex diff = vertex - end0;
  678. Real tmax = Dot(dir, dir);
  679. Real t = Dot(dir, diff);
  680. if (-mEpsilon <= t && t <= tmax + mEpsilon)
  681. {
  682. return 0;
  683. }
  684. }
  685. // It does not matter which subtree you use.
  686. if (mPosChild)
  687. {
  688. return mPosChild->PointLocation(polygon, vertex);
  689. }
  690. if (mNegChild)
  691. {
  692. return mNegChild->PointLocation(polygon, vertex);
  693. }
  694. return 0;
  695. }
  696. Real mEpsilon;
  697. EArray mCoincident;
  698. std::shared_ptr<BSPTree2> mPosChild;
  699. std::shared_ptr<BSPTree2> mNegChild;
  700. };
  701. private:
  702. void SplitEdge(int v0, int v1, int vmid)
  703. {
  704. // Find the edge in the map to get the edge-array index.
  705. auto iter = mEMap.find(Edge(v0, v1));
  706. LogAssert(iter != mEMap.end(), "Edge does not exist.");
  707. int eIndex = iter->second;
  708. // Delete edge <V0,V1>.
  709. mEMap.erase(iter);
  710. // Insert edge <V0,VM>.
  711. mEArray[eIndex].V[1] = vmid;
  712. mEMap.insert(std::make_pair(mEArray[eIndex], eIndex));
  713. // Insert edge <VM,V1>.
  714. InsertEdge(Edge(vmid, v1));
  715. }
  716. void GetInsideEdgesFrom(BSPPolygon2 const& polygon, BSPPolygon2& inside) const
  717. {
  718. LogAssert(mTree != nullptr, "Tree must exist.");
  719. BSPPolygon2 ignore(mEpsilon);
  720. const int numEdges = polygon.GetNumEdges();
  721. for (int i = 0; i < numEdges; ++i)
  722. {
  723. int v0 = polygon.mEArray[i].V[0];
  724. int v1 = polygon.mEArray[i].V[1];
  725. Vertex vertex0 = polygon.mVArray[v0];
  726. Vertex vertex1 = polygon.mVArray[v1];
  727. mTree->GetPartition(*this, vertex0, vertex1, ignore, inside, inside, ignore);
  728. }
  729. }
  730. Real mEpsilon;
  731. VMap mVMap;
  732. VArray mVArray;
  733. EMap mEMap;
  734. EArray mEArray;
  735. std::shared_ptr<BSPTree2> mTree;
  736. };
  737. }