Skip to content

Commit

Permalink
#7 Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-ogre committed Aug 21, 2023
1 parent e4a16a7 commit 23b928b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion CDT/include/CDTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ inline Edge edge_make(VertInd iV1, VertInd iV2)
}

typedef std::vector<Edge> EdgeVec; ///< Vector of edges
typedef std::queue<Edge> EdgeQue; ///< Queue of edges
typedef std::queue<Edge> EdgeQueue; ///< Queue of edges
typedef unordered_set<Edge> EdgeUSet; ///< Hash table of edges
typedef unordered_set<TriInd> TriIndUSet; ///< Hash table of triangles
typedef unordered_map<TriInd, TriInd> TriIndUMap; ///< Triangle hash map
Expand Down
6 changes: 3 additions & 3 deletions CDT/include/Triangulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,15 @@ class CDT_EXPORT Triangulation
/// Search in all fixed edges to find encroached edges, each fixed edge is
/// checked against its opposite vertices
/// Returns queue of encroached edges
EdgeQue detectEncroachedEdges();
EdgeQueue detectEncroachedEdges();
/// Search in all fixed edges to find encroached edges, each fixed edge is
/// checked against its opposite vertices and vertex v
/// Returns queue of encroached edges
EdgeQue detectEncroachedEdges(const V2d<T>& v);
EdgeQueue detectEncroachedEdges(const V2d<T>& v);
/// Recursively split encroached edges
/// @return vector of badly shaped triangles
TriIndVec resolveEncroachedEdges(
EdgeQue encroachedEdges,
EdgeQueue encroachedEdges,
VertInd& newVertBudget,
VertInd steinerVerticesOffset,
const V2d<T>* circumcenterOrNull = NULL,
Expand Down
54 changes: 25 additions & 29 deletions CDT/include/Triangulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,24 +1283,21 @@ bool Triangulation<T, TNearPointLocator>::isRefinementNeeded(
}

template <typename T, typename TNearPointLocator>
EdgeQue Triangulation<T, TNearPointLocator>::detectEncroachedEdges()
EdgeQueue Triangulation<T, TNearPointLocator>::detectEncroachedEdges()
{
// Search in all fixed edges to find encroached edges, each fixed edge is
// checked against its opposite vertices
// Returns queue of encroached edges
EdgeQue encroachedEdges;
for(EdgeUSet::const_iterator cit = fixedEdges.begin();
cit != fixedEdges.end();
++cit)
EdgeQueue encroachedEdges;
typedef EdgeUSet::const_iterator Iter;
for(Iter it = fixedEdges.begin(); it != fixedEdges.end(); ++it)
{
const Edge edge = *cit;
const Edge edge = *it;
TriInd iT, iTopo;
std::tie(iT, iTopo) = edgeTriangles(edge.v1(), edge.v2());
assert(iT != invalidIndex && iTopo != invalidIndex);
const Triangle& t = triangles[iT];
const Triangle& tOpo = triangles[iTopo];
VertInd v1 = opposedVertex(t, iTopo);
VertInd v2 = opposedVertex(tOpo, iT);
const VertInd v1 = opposedVertex(triangles[iT], iTopo);
const VertInd v2 = opposedVertex(triangles[iTopo], iT);
const V2d<T>& edgeStart = vertices[edge.v1()];
const V2d<T>& edgeEnd = vertices[edge.v2()];
if(isEncroachingOnEdge(vertices[v1], edgeStart, edgeEnd) ||
Expand All @@ -1313,18 +1310,17 @@ EdgeQue Triangulation<T, TNearPointLocator>::detectEncroachedEdges()
}

template <typename T, typename TNearPointLocator>
EdgeQue
EdgeQueue
Triangulation<T, TNearPointLocator>::detectEncroachedEdges(const V2d<T>& v)
{
// Search in all fixed edges to find encroached edges, each fixed edge is
// checked against its opposite vertices and vertex v
// Returns queue of encroached edges
EdgeQue encroachedEdges;
for(EdgeUSet::const_iterator cit = fixedEdges.begin();
cit != fixedEdges.end();
++cit)
EdgeQueue encroachedEdges;
typedef EdgeUSet::const_iterator Iter;
for(Iter it = fixedEdges.begin(); it != fixedEdges.end(); ++it)
{
const Edge edge = *cit;
const Edge edge = *it;
if(isEncroachingOnEdge(v, vertices[edge.v1()], vertices[edge.v2()]))
{
encroachedEdges.push(edge);
Expand All @@ -1335,8 +1331,8 @@ Triangulation<T, TNearPointLocator>::detectEncroachedEdges(const V2d<T>& v)

template <typename T, typename TNearPointLocator>
TriIndVec Triangulation<T, TNearPointLocator>::resolveEncroachedEdges(
EdgeQue encroachedEdges,
VertInd& newVertBudget,
EdgeQueue encroachedEdges,
VertInd& remainingVertexBudget,
const VertInd steinerVerticesOffset,
const V2d<T>* const circumcenterOrNull,
const RefinementCriterion::Enum refinementCriterion,
Expand All @@ -1345,9 +1341,9 @@ TriIndVec Triangulation<T, TNearPointLocator>::resolveEncroachedEdges(
IndexSizeType numOfSplits = 0;
std::vector<TriInd> badTriangles;

while(!encroachedEdges.empty() && newVertBudget > 0)
while(!encroachedEdges.empty() && remainingVertexBudget > 0)
{
Edge edge = encroachedEdges.front();
const Edge edge = encroachedEdges.front();
encroachedEdges.pop();
if(!hasEdge(edge.v1(), edge.v2()))
{
Expand All @@ -1358,9 +1354,9 @@ TriIndVec Triangulation<T, TNearPointLocator>::resolveEncroachedEdges(
assert(iT != invalidIndex && iTopo != invalidIndex);
const VertInd i =
splitEncroachedEdge(edge, iT, iTopo, steinerVerticesOffset);
--newVertBudget;
--remainingVertexBudget;

TriInd start = m_vertTris[i];
const TriInd start = m_vertTris[i];
TriInd currTri = start;
do
{
Expand Down Expand Up @@ -1417,7 +1413,7 @@ VertInd Triangulation<T, TNearPointLocator>::splitEncroachedEdge(
// that introduces FP rounding erros, so it's avoided.
const T len = distance(start, end);
const T d = T(0.5) * len;
// Find the splitting distance.
// Find the splitting distance
T nearestPowerOfTwo = T(1);
while(d > nearestPowerOfTwo)
{
Expand All @@ -1432,7 +1428,7 @@ VertInd Triangulation<T, TNearPointLocator>::splitEncroachedEdge(
if(splitEdge.v1() >= steinerVerticesOffset)
split = T(1) - split;
}
V2d<T> mid = V2d<T>::make(
const V2d<T> mid = V2d<T>::make(
detail::lerp(start.x, end.x, split),
detail::lerp(start.y, end.y, split));

Expand Down Expand Up @@ -2279,10 +2275,10 @@ void Triangulation<T, TNearPointLocator>::refineTriangles(
}
tryInitNearestPointLocator();

VertInd newVertBudget = maxVerticesToInsert;
VertInd remainingVertexBudget = maxVerticesToInsert;
const VertInd steinerVerticesOffset = vertices.size();
resolveEncroachedEdges(
detectEncroachedEdges(), newVertBudget, steinerVerticesOffset);
detectEncroachedEdges(), remainingVertexBudget, steinerVerticesOffset);

std::queue<TriInd> badTriangles;
for(TriInd iT(0), n = triangles.size(); iT < n; ++iT)