123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239 |
- #pragma once
- #include <Mathematics/Logger.h>
- #include <Mathematics/PrimalQuery2.h>
- #include <memory>
- #include <map>
- #include <queue>
- #include <vector>
- namespace WwiseGTE
- {
- template <typename InputType, typename ComputeType>
- class TriangulateEC
- {
- public:
-
-
-
-
-
-
-
- TriangulateEC(int numPoints, Vector2<InputType> const* points)
- :
- mNumPoints(numPoints),
- mPoints(points)
- {
- LogAssert(numPoints >= 3 && points != nullptr, "Invalid input.");
- mComputePoints.resize(mNumPoints);
- mIsConverted.resize(mNumPoints);
- std::fill(mIsConverted.begin(), mIsConverted.end(), false);
- mQuery.Set(mNumPoints, &mComputePoints[0]);
- }
- TriangulateEC(std::vector<Vector2<InputType>> const& points)
- :
- mNumPoints(static_cast<int>(points.size())),
- mPoints(points.data())
- {
- LogAssert(mNumPoints >= 3 && mPoints != nullptr, "Invalid input.");
- mComputePoints.resize(mNumPoints);
- mIsConverted.resize(mNumPoints);
- std::fill(mIsConverted.begin(), mIsConverted.end(), false);
- mQuery.Set(mNumPoints, &mComputePoints[0]);
- }
-
- inline std::vector<std::array<int, 3>> const& GetTriangles() const
- {
- return mTriangles;
- }
-
-
- typedef std::vector<int> Polygon;
-
-
-
- bool operator()()
- {
- mTriangles.clear();
- if (mPoints)
- {
-
- for (int i = 0; i < mNumPoints; ++i)
- {
- if (!mIsConverted[i])
- {
- mIsConverted[i] = true;
- for (int j = 0; j < 2; ++j)
- {
- mComputePoints[i][j] = mPoints[i][j];
- }
- }
- }
-
- InitializeVertices(mNumPoints, nullptr);
- DoEarClipping(mNumPoints, nullptr);
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
- bool operator()(Polygon const& polygon)
- {
- mTriangles.clear();
- if (mPoints)
- {
-
- int const numIndices = static_cast<int>(polygon.size());
- int const* indices = polygon.data();
- for (int i = 0; i < numIndices; ++i)
- {
- int index = indices[i];
- if (!mIsConverted[index])
- {
- mIsConverted[index] = true;
- for (int j = 0; j < 2; ++j)
- {
- mComputePoints[index][j] = mPoints[index][j];
- }
- }
- }
-
- InitializeVertices(numIndices, indices);
- DoEarClipping(numIndices, indices);
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
-
-
- bool operator()(Polygon const& outer, Polygon const& inner)
- {
- mTriangles.clear();
- if (mPoints)
- {
-
-
- int numPointsPlusExtras = mNumPoints + 2;
- if (numPointsPlusExtras > static_cast<int>(mComputePoints.size()))
- {
- mComputePoints.resize(numPointsPlusExtras);
- mIsConverted.resize(numPointsPlusExtras);
- mIsConverted[mNumPoints] = false;
- mIsConverted[mNumPoints + 1] = false;
- mQuery.Set(numPointsPlusExtras, &mComputePoints[0]);
- }
-
-
- int const numOuterIndices = static_cast<int>(outer.size());
- int const* outerIndices = outer.data();
- for (int i = 0; i < numOuterIndices; ++i)
- {
- int index = outerIndices[i];
- if (!mIsConverted[index])
- {
- mIsConverted[index] = true;
- for (int j = 0; j < 2; ++j)
- {
- mComputePoints[index][j] = mPoints[index][j];
- }
- }
- }
- int const numInnerIndices = static_cast<int>(inner.size());
- int const* innerIndices = inner.data();
- for (int i = 0; i < numInnerIndices; ++i)
- {
- int index = innerIndices[i];
- if (!mIsConverted[index])
- {
- mIsConverted[index] = true;
- for (int j = 0; j < 2; ++j)
- {
- mComputePoints[index][j] = mPoints[index][j];
- }
- }
- }
-
-
-
-
- int nextElement = mNumPoints;
- std::map<int, int> indexMap;
- std::vector<int> combined;
- if (!CombinePolygons(nextElement, outer, inner, indexMap, combined))
- {
-
- return false;
- }
-
-
- int numVertices = static_cast<int>(combined.size());
- int* const indices = &combined[0];
- InitializeVertices(numVertices, indices);
- DoEarClipping(numVertices, indices);
-
- RemapIndices(indexMap);
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
-
-
- bool operator()(Polygon const& outer, std::vector<Polygon> const& inners)
- {
- mTriangles.clear();
- if (mPoints)
- {
-
-
-
- int numPointsPlusExtras = mNumPoints + 2 * (int)inners.size();
- if (numPointsPlusExtras > static_cast<int>(mComputePoints.size()))
- {
- mComputePoints.resize(numPointsPlusExtras);
- mIsConverted.resize(numPointsPlusExtras);
- for (int i = mNumPoints; i < numPointsPlusExtras; ++i)
- {
- mIsConverted[i] = false;
- }
- mQuery.Set(numPointsPlusExtras, &mComputePoints[0]);
- }
-
-
- int const numOuterIndices = static_cast<int>(outer.size());
- int const* outerIndices = outer.data();
- for (int i = 0; i < numOuterIndices; ++i)
- {
- int index = outerIndices[i];
- if (!mIsConverted[index])
- {
- mIsConverted[index] = true;
- for (int j = 0; j < 2; ++j)
- {
- mComputePoints[index][j] = mPoints[index][j];
- }
- }
- }
- for (auto const& inner : inners)
- {
- int const numInnerIndices = static_cast<int>(inner.size());
- int const* innerIndices = inner.data();
- for (int i = 0; i < numInnerIndices; ++i)
- {
- int index = innerIndices[i];
- if (!mIsConverted[index])
- {
- mIsConverted[index] = true;
- for (int j = 0; j < 2; ++j)
- {
- mComputePoints[index][j] = mPoints[index][j];
- }
- }
- }
- }
-
-
-
- int nextElement = mNumPoints;
- std::map<int, int> indexMap;
- std::vector<int> combined;
- if (!ProcessOuterAndInners(nextElement, outer, inners, indexMap, combined))
- {
-
- return false;
- }
-
-
- int numVertices = static_cast<int>(combined.size());
- int* const indices = &combined[0];
- InitializeVertices(numVertices, indices);
- DoEarClipping(numVertices, indices);
-
- RemapIndices(indexMap);
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
-
-
-
-
-
- class Tree
- {
- public:
- Polygon polygon;
- std::vector<std::shared_ptr<Tree>> child;
- };
-
-
- bool operator()(std::shared_ptr<Tree> const& tree)
- {
- mTriangles.clear();
- if (mPoints)
- {
-
-
-
- int numPointsPlusExtras = mNumPoints + InitializeFromTree(tree);
- if (numPointsPlusExtras > static_cast<int>(mComputePoints.size()))
- {
- mComputePoints.resize(numPointsPlusExtras);
- mIsConverted.resize(numPointsPlusExtras);
- for (int i = mNumPoints; i < numPointsPlusExtras; ++i)
- {
- mIsConverted[i] = false;
- }
- mQuery.Set(numPointsPlusExtras, &mComputePoints[0]);
- }
- int nextElement = mNumPoints;
- std::map<int, int> indexMap;
- std::queue<std::shared_ptr<Tree>> treeQueue;
- treeQueue.push(tree);
- while (treeQueue.size() > 0)
- {
- std::shared_ptr<Tree> outer = treeQueue.front();
- treeQueue.pop();
- int numChildren = static_cast<int>(outer->child.size());
- int numVertices;
- int const* indices;
- if (numChildren == 0)
- {
-
-
- numVertices = static_cast<int>(outer->polygon.size());
- indices = outer->polygon.data();
- InitializeVertices(numVertices, indices);
- DoEarClipping(numVertices, indices);
- }
- else
- {
-
-
- std::vector<Polygon> inners(numChildren);
- for (int c = 0; c < numChildren; ++c)
- {
- std::shared_ptr<Tree> inner = outer->child[c];
- inners[c] = inner->polygon;
- int numGrandChildren = static_cast<int>(inner->child.size());
- for (int g = 0; g < numGrandChildren; ++g)
- {
- treeQueue.push(inner->child[g]);
- }
- }
-
-
-
- std::vector<int> combined;
- ProcessOuterAndInners(nextElement, outer->polygon, inners, indexMap, combined);
-
-
- numVertices = static_cast<int>(combined.size());
- indices = &combined[0];
- InitializeVertices(numVertices, indices);
- DoEarClipping(numVertices, indices);
- }
- }
-
- RemapIndices(indexMap);
- return true;
- }
- else
- {
- return false;
- }
- }
- private:
-
-
- void InitializeVertices(int numVertices, int const* indices)
- {
- mVertices.clear();
- mVertices.resize(numVertices);
- mCFirst = -1;
- mCLast = -1;
- mRFirst = -1;
- mRLast = -1;
- mEFirst = -1;
- mELast = -1;
-
-
- int numVerticesM1 = numVertices - 1;
- for (int i = 0; i <= numVerticesM1; ++i)
- {
- Vertex& vertex = V(i);
- vertex.index = (indices ? indices[i] : i);
- vertex.vPrev = (i > 0 ? i - 1 : numVerticesM1);
- vertex.vNext = (i < numVerticesM1 ? i + 1 : 0);
- }
-
-
-
-
-
- for (int i = 0; i <= numVerticesM1; ++i)
- {
- if (IsConvex(i))
- {
- InsertAfterC(i);
- }
- else
- {
- InsertAfterR(i);
- }
- }
- }
-
-
-
-
- void DoEarClipping(int numVertices, int const* indices)
- {
-
- if (mRFirst == -1)
- {
- int numVerticesM1 = numVertices - 1;
- if (indices)
- {
- for (int i = 1; i < numVerticesM1; ++i)
- {
- mTriangles.push_back( { indices[0], indices[i], indices[i + 1] } );
- }
- }
- else
- {
- for (int i = 1; i < numVerticesM1; ++i)
- {
- mTriangles.push_back( { 0, i, i + 1 } );
- }
- }
- return;
- }
-
-
-
-
-
-
-
-
-
- for (int i = mCFirst; i != -1; i = V(i).sNext)
- {
- if (IsEar(i))
- {
- InsertEndE(i);
- }
- }
- V(mEFirst).ePrev = mELast;
- V(mELast).eNext = mEFirst;
-
- bool bRemoveAnEar = true;
- while (bRemoveAnEar)
- {
-
-
- int iVPrev = V(mEFirst).vPrev;
- int iVNext = V(mEFirst).vNext;
- mTriangles.push_back( { V(iVPrev).index, V(mEFirst).index, V(iVNext).index } );
-
- RemoveV(mEFirst);
- if (--numVertices == 3)
- {
-
-
- mEFirst = RemoveE(mEFirst);
- iVPrev = V(mEFirst).vPrev;
- iVNext = V(mEFirst).vNext;
- mTriangles.push_back( { V(iVPrev).index, V(mEFirst).index, V(iVNext).index } );
- bRemoveAnEar = false;
- continue;
- }
-
-
- Vertex& vPrev = V(iVPrev);
- if (vPrev.isEar)
- {
- if (!IsEar(iVPrev))
- {
- RemoveE(iVPrev);
- }
- }
- else
- {
- bool wasReflex = !vPrev.isConvex;
- if (IsConvex(iVPrev))
- {
- if (wasReflex)
- {
- RemoveR(iVPrev);
- }
- if (IsEar(iVPrev))
- {
- InsertBeforeE(iVPrev);
- }
- }
- }
- Vertex& vNext = V(iVNext);
- if (vNext.isEar)
- {
- if (!IsEar(iVNext))
- {
- RemoveE(iVNext);
- }
- }
- else
- {
- bool wasReflex = !vNext.isConvex;
- if (IsConvex(iVNext))
- {
- if (wasReflex)
- {
- RemoveR(iVNext);
- }
- if (IsEar(iVNext))
- {
- InsertAfterE(iVNext);
- }
- }
- }
-
- mEFirst = RemoveE(mEFirst);
- }
- }
-
-
-
- bool CombinePolygons(int nextElement, Polygon const& outer,
- Polygon const& inner, std::map<int, int>& indexMap,
- std::vector<int>& combined)
- {
- int const numOuterIndices = static_cast<int>(outer.size());
- int const* outerIndices = outer.data();
- int const numInnerIndices = static_cast<int>(inner.size());
- int const* innerIndices = inner.data();
-
-
- ComputeType xmax = mComputePoints[innerIndices[0]][0];
- int xmaxIndex = 0;
- for (int i = 1; i < numInnerIndices; ++i)
- {
- ComputeType x = mComputePoints[innerIndices[i]][0];
- if (x > xmax)
- {
- xmax = x;
- xmaxIndex = i;
- }
- }
- Vector2<ComputeType> M = mComputePoints[innerIndices[xmaxIndex]];
-
-
-
- ComputeType const cmax = static_cast<ComputeType>(std::numeric_limits<InputType>::max());
- ComputeType const zero = static_cast<ComputeType>(0);
- Vector2<ComputeType> intr{ cmax, M[1] };
- int v0min = -1, v1min = -1, endMin = -1;
- int i0, i1;
- ComputeType s = cmax;
- ComputeType t = cmax;
- for (i0 = numOuterIndices - 1, i1 = 0; i1 < numOuterIndices; i0 = i1++)
- {
-
-
-
- Vector2<ComputeType> diff0 = mComputePoints[outerIndices[i0]] - M;
- if (diff0[1] > zero)
- {
- continue;
- }
- Vector2<ComputeType> diff1 = mComputePoints[outerIndices[i1]] - M;
- if (diff1[1] < zero)
- {
- continue;
- }
-
- int currentEndMin = -1;
- if (diff0[1] < zero)
- {
- if (diff1[1] > zero)
- {
-
-
- s = diff0[1] / (diff0[1] - diff1[1]);
- t = diff0[0] + s * (diff1[0] - diff0[0]);
- }
- else
- {
-
-
- t = diff1[0];
- currentEndMin = i1;
- }
- }
- else
- {
- if (diff1[1] > zero)
- {
-
-
- t = diff0[0];
- currentEndMin = i0;
- }
- else
- {
- if (diff0[0] < diff1[0])
- {
- t = diff0[0];
- currentEndMin = i0;
- }
- else
- {
- t = diff1[0];
- currentEndMin = i1;
- }
- }
- }
- if (zero <= t && t < intr[0])
- {
- intr[0] = t;
- v0min = i0;
- v1min = i1;
- if (currentEndMin == -1)
- {
-
-
- endMin = -1;
- }
- else
- {
-
- endMin = currentEndMin;
- }
- }
- else if (t == intr[0])
- {
-
-
-
- LogAssert(endMin != -1 && currentEndMin != -1, "Unexpected condition.");
-
-
-
- Vector2<ComputeType> shared = mComputePoints[outerIndices[i1]];
-
-
- int other = (endMin == v0min ? v1min : v0min);
-
-
- diff0 = mComputePoints[outerIndices[i0]] - shared;
- diff1 = mComputePoints[outerIndices[other]] - shared;
- ComputeType dotperp = DotPerp(diff0, diff1);
- if (dotperp > zero)
- {
-
- v0min = i0;
- v1min = i1;
- endMin = currentEndMin;
- }
- }
- }
-
-
-
- intr[0] += M[0];
- int maxCosIndex;
- if (endMin == -1)
- {
-
-
- LogAssert(v0min >= 0 && v1min >= 0, "Is this an invalid nested polygon?");
-
-
-
-
- Vector2<ComputeType> sTriangle[3];
- int pIndex;
- if (mComputePoints[outerIndices[v0min]][0] > mComputePoints[outerIndices[v1min]][0])
- {
- sTriangle[0] = mComputePoints[outerIndices[v0min]];
- sTriangle[1] = intr;
- sTriangle[2] = M;
- pIndex = v0min;
- }
- else
- {
- sTriangle[0] = mComputePoints[outerIndices[v1min]];
- sTriangle[1] = M;
- sTriangle[2] = intr;
- pIndex = v1min;
- }
-
-
-
-
-
-
- Vector2<ComputeType> diff = sTriangle[0] - M;
- ComputeType maxSqrLen = Dot(diff, diff);
- ComputeType maxCos = diff[0] * diff[0] / maxSqrLen;
- PrimalQuery2<ComputeType> localQuery(3, sTriangle);
- maxCosIndex = pIndex;
- for (int i = 0; i < numOuterIndices; ++i)
- {
- if (i == pIndex)
- {
- continue;
- }
- int curr = outerIndices[i];
- int prev = outerIndices[(i + numOuterIndices - 1) % numOuterIndices];
- int next = outerIndices[(i + 1) % numOuterIndices];
- if (mQuery.ToLine(curr, prev, next) <= 0
- && localQuery.ToTriangle(mComputePoints[curr], 0, 1, 2) <= 0)
- {
-
-
- diff = mComputePoints[curr] - M;
- ComputeType sqrLen = Dot(diff, diff);
- ComputeType cs = diff[0] * diff[0] / sqrLen;
- if (cs > maxCos)
- {
-
-
-
- maxSqrLen = sqrLen;
- maxCos = cs;
- maxCosIndex = i;
- }
- else if (cs == maxCos && sqrLen < maxSqrLen)
- {
-
-
-
- maxSqrLen = sqrLen;
- maxCosIndex = i;
- }
- }
- }
- }
- else
- {
- maxCosIndex = endMin;
- }
-
-
-
-
-
-
-
- combined.resize(numOuterIndices + numInnerIndices + 2);
- int cIndex = 0;
- for (int i = 0; i <= maxCosIndex; ++i, ++cIndex)
- {
- combined[cIndex] = outerIndices[i];
- }
- for (int i = 0; i < numInnerIndices; ++i, ++cIndex)
- {
- int j = (xmaxIndex + i) % numInnerIndices;
- combined[cIndex] = innerIndices[j];
- }
- int innerIndex = innerIndices[xmaxIndex];
- mComputePoints[nextElement] = mComputePoints[innerIndex];
- combined[cIndex] = nextElement;
- auto iter = indexMap.find(innerIndex);
- if (iter != indexMap.end())
- {
- innerIndex = iter->second;
- }
- indexMap[nextElement] = innerIndex;
- ++cIndex;
- ++nextElement;
- int outerIndex = outerIndices[maxCosIndex];
- mComputePoints[nextElement] = mComputePoints[outerIndex];
- combined[cIndex] = nextElement;
- iter = indexMap.find(outerIndex);
- if (iter != indexMap.end())
- {
- outerIndex = iter->second;
- }
- indexMap[nextElement] = outerIndex;
- ++cIndex;
- ++nextElement;
- for (int i = maxCosIndex + 1; i < numOuterIndices; ++i, ++cIndex)
- {
- combined[cIndex] = outerIndices[i];
- }
- return true;
- }
-
-
-
-
-
- bool ProcessOuterAndInners(int& nextElement, Polygon const& outer,
- std::vector<Polygon> const& inners, std::map<int, int>& indexMap,
- std::vector<int>& combined)
- {
-
- int numInners = static_cast<int>(inners.size());
- std::vector<std::pair<ComputeType, int>> pairs(numInners);
- for (int p = 0; p < numInners; ++p)
- {
- int numIndices = static_cast<int>(inners[p].size());
- int const* indices = inners[p].data();
- ComputeType xmax = mComputePoints[indices[0]][0];
- for (int j = 1; j < numIndices; ++j)
- {
- ComputeType x = mComputePoints[indices[j]][0];
- if (x > xmax)
- {
- xmax = x;
- }
- }
- pairs[p].first = xmax;
- pairs[p].second = p;
- }
- std::sort(pairs.begin(), pairs.end());
-
- Polygon currentPolygon = outer;
- for (int p = numInners - 1; p >= 0; --p)
- {
- Polygon const& polygon = inners[pairs[p].second];
- Polygon currentCombined;
- if (!CombinePolygons(nextElement, currentPolygon, polygon, indexMap, currentCombined))
- {
- return false;
- }
- currentPolygon = std::move(currentCombined);
- nextElement += 2;
- }
- for (auto index : currentPolygon)
- {
- combined.push_back(index);
- }
- return true;
- }
-
-
-
-
- void RemapIndices(std::map<int, int> const& indexMap)
- {
-
-
-
- for (auto& tri : mTriangles)
- {
- for (int i = 0; i < 3; ++i)
- {
- auto iter = indexMap.find(tri[i]);
- if (iter != indexMap.end())
- {
- tri[i] = iter->second;
- }
- }
- }
- }
-
-
-
-
- int InitializeFromTree(std::shared_ptr<Tree> const& tree)
- {
-
-
- int numExtraPoints = 0;
- std::queue<std::shared_ptr<Tree>> treeQueue;
- treeQueue.push(tree);
- while (treeQueue.size() > 0)
- {
-
- std::shared_ptr<Tree> outer = treeQueue.front();
- treeQueue.pop();
-
- int numChildren = static_cast<int>(outer->child.size());
- numExtraPoints += 2 * numChildren;
-
- int const numOuterIndices = static_cast<int>(outer->polygon.size());
- int const* outerIndices = outer->polygon.data();
- for (int i = 0; i < numOuterIndices; ++i)
- {
- int index = outerIndices[i];
- if (!mIsConverted[index])
- {
- mIsConverted[index] = true;
- for (int j = 0; j < 2; ++j)
- {
- mComputePoints[index][j] = mPoints[index][j];
- }
- }
- }
-
-
- for (int c = 0; c < numChildren; ++c)
- {
-
- std::shared_ptr<Tree> inner = outer->child[c];
-
- int const numInnerIndices = static_cast<int>(inner->polygon.size());
- int const* innerIndices = inner->polygon.data();
- for (int i = 0; i < numInnerIndices; ++i)
- {
- int index = innerIndices[i];
- if (!mIsConverted[index])
- {
- mIsConverted[index] = true;
- for (int j = 0; j < 2; ++j)
- {
- mComputePoints[index][j] = mPoints[index][j];
- }
- }
- }
- int numGrandChildren = static_cast<int>(inner->child.size());
- for (int g = 0; g < numGrandChildren; ++g)
- {
- treeQueue.push(inner->child[g]);
- }
- }
- }
- return numExtraPoints;
- }
-
- int mNumPoints;
- Vector2<InputType> const* mPoints;
-
- std::vector<std::array<int, 3>> mTriangles;
-
-
-
-
-
- std::vector<Vector2<ComputeType>> mComputePoints;
- std::vector<bool> mIsConverted;
- PrimalQuery2<ComputeType> mQuery;
-
- class Vertex
- {
- public:
- Vertex()
- :
- index(-1),
- vPrev(-1),
- vNext(-1),
- sPrev(-1),
- sNext(-1),
- ePrev(-1),
- eNext(-1),
- isConvex(false),
- isEar(false)
- {
- }
- int index;
- int vPrev, vNext;
- int sPrev, sNext;
- int ePrev, eNext;
- bool isConvex, isEar;
- };
- inline Vertex& V(int i)
- {
- return mVertices[i];
- }
- bool IsConvex(int i)
- {
- Vertex& vertex = V(i);
- int curr = vertex.index;
- int prev = V(vertex.vPrev).index;
- int next = V(vertex.vNext).index;
- vertex.isConvex = (mQuery.ToLine(curr, prev, next) > 0);
- return vertex.isConvex;
- }
- bool IsEar(int i)
- {
- Vertex& vertex = V(i);
- if (mRFirst == -1)
- {
-
- vertex.isEar = true;
- return true;
- }
-
-
- int prev = V(vertex.vPrev).index;
- int curr = vertex.index;
- int next = V(vertex.vNext).index;
- vertex.isEar = true;
- for (int j = mRFirst; j != -1; j = V(j).sNext)
- {
-
-
- if (j == vertex.vPrev || j == i || j == vertex.vNext)
- {
- continue;
- }
-
-
-
-
- int test = V(j).index;
- if (mComputePoints[test] == mComputePoints[prev]
- || mComputePoints[test] == mComputePoints[curr]
- || mComputePoints[test] == mComputePoints[next])
- {
- continue;
- }
-
-
- if (mQuery.ToTriangle(test, prev, curr, next) <= 0)
- {
- vertex.isEar = false;
- break;
- }
- }
- return vertex.isEar;
- }
-
- void InsertAfterC(int i)
- {
- if (mCFirst == -1)
- {
-
- mCFirst = i;
- }
- else
- {
- V(mCLast).sNext = i;
- V(i).sPrev = mCLast;
- }
- mCLast = i;
- }
-
- void InsertAfterR(int i)
- {
- if (mRFirst == -1)
- {
-
- mRFirst = i;
- }
- else
- {
- V(mRLast).sNext = i;
- V(i).sPrev = mRLast;
- }
- mRLast = i;
- }
-
- void InsertEndE(int i)
- {
- if (mEFirst == -1)
- {
-
- mEFirst = i;
- mELast = i;
- }
- V(mELast).eNext = i;
- V(i).ePrev = mELast;
- mELast = i;
- }
-
- void InsertAfterE(int i)
- {
- Vertex& first = V(mEFirst);
- int currENext = first.eNext;
- Vertex& vertex = V(i);
- vertex.ePrev = mEFirst;
- vertex.eNext = currENext;
- first.eNext = i;
- V(currENext).ePrev = i;
- }
-
- void InsertBeforeE(int i)
- {
- Vertex& first = V(mEFirst);
- int currEPrev = first.ePrev;
- Vertex& vertex = V(i);
- vertex.ePrev = currEPrev;
- vertex.eNext = mEFirst;
- first.ePrev = i;
- V(currEPrev).eNext = i;
- }
-
- void RemoveV(int i)
- {
- int currVPrev = V(i).vPrev;
- int currVNext = V(i).vNext;
- V(currVPrev).vNext = currVNext;
- V(currVNext).vPrev = currVPrev;
- }
-
- int RemoveE(int i)
- {
- int currEPrev = V(i).ePrev;
- int currENext = V(i).eNext;
- V(currEPrev).eNext = currENext;
- V(currENext).ePrev = currEPrev;
- return currENext;
- }
-
- void RemoveR(int i)
- {
- LogAssert(mRFirst != -1 && mRLast != -1, "Reflex vertices must exist.");
- if (i == mRFirst)
- {
- mRFirst = V(i).sNext;
- if (mRFirst != -1)
- {
- V(mRFirst).sPrev = -1;
- }
- V(i).sNext = -1;
- }
- else if (i == mRLast)
- {
- mRLast = V(i).sPrev;
- if (mRLast != -1)
- {
- V(mRLast).sNext = -1;
- }
- V(i).sPrev = -1;
- }
- else
- {
- int currSPrev = V(i).sPrev;
- int currSNext = V(i).sNext;
- V(currSPrev).sNext = currSNext;
- V(currSNext).sPrev = currSPrev;
- V(i).sNext = -1;
- V(i).sPrev = -1;
- }
- }
-
- std::vector<Vertex> mVertices;
- int mCFirst, mCLast;
- int mRFirst, mRLast;
- int mEFirst, mELast;
- };
- }
|