Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback functionality for vertex insertation, triangle add/change #179

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CDT/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ option(CDT_USE_STRONG_TYPING
"If enabled uses strong typing for types: useful for development and debugging"
OFF)

option(CDT_CALLBACK_DISABLED
"If enabled a callbeack function can be registered if a triabngle is created or changed (>=C++11 necessary)"
Copy link

@chemodansama chemodansama Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo in triabngle

OFF)

option(CDT_DEVELOPER_BUILD
"Enables all warnings."
OFF)
Expand All @@ -52,20 +56,28 @@ if(cxx_std_11 IN_LIST CMAKE_CXX_COMPILE_FEATURES)
message(STATUS "AppleClang work-around: set CMAKE_CXX_STANDARD to 11")
set(CMAKE_CXX_STANDARD 11)
endif()

message(STATUS "Compiler supports C++11")
else()
message(STATUS "Compiler does not support C++11: falling back to Boost")
set(CDT_USE_BOOST ON)
set(CDT_CALLBACK_DISABLED ON)
endif()

if(CDT_USE_STRONG_TYPING)
set(CDT_USE_BOOST ON)
endif()

if (CDT_DISABLE_CALLBACK)
add_definitions(-DCDT_CALLBACK_DISABLED)
endif()

message(STATUS "CDT_USE_AS_COMPILED_LIBRARY is ${CDT_USE_AS_COMPILED_LIBRARY}")
message(STATUS "CDT_USE_64_BIT_INDEX_TYPE is ${CDT_USE_64_BIT_INDEX_TYPE}")
message(STATUS "CDT_ENABLE_TESTING is ${CDT_ENABLE_TESTING}")
message(STATUS "CDT_USE_STRONG_TYPING is ${CDT_USE_STRONG_TYPING}")
message(STATUS "CDT_DEVELOPER_BUILD is ${CDT_DEVELOPER_BUILD}")
message(STATUS "CDT_CALLBACK_DISABLED is ${CDT_CALLBACK_DISABLED}")

# Use boost for c++98 versions of c++11 containers or for Boost::rtree
if(CDT_USE_BOOST)
Expand Down
44 changes: 44 additions & 0 deletions CDT/include/Triangulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,26 @@
#include <string>
#include <utility>
#include <vector>
#ifndef CDT_CALLBACK_DISABLED
#include <functional>
#endif

/// Namespace containing triangulation functionality
namespace CDT
{

#ifndef CDT_CALLBACK_DISABLED
/**
* Types of callbacks to indicate what happened
*/
enum CallbackType
{
Unknown,
Added,
Changed,
};
#endif

/// @addtogroup API
/// @{

Expand Down Expand Up @@ -451,7 +466,30 @@ class CDT_EXPORT Triangulation
* @return true if triangulation is finalized, false otherwise
*/
bool isFinalized() const;

#ifndef CDT_CALLBACK_DISABLED
/**
* Register a callback function for triangles has been created
* currently one callback function is supported
* Example using lambda function:
* CDT::Triangulation<double> cdt = CDT::Triangulation<double> {};
* auto onTriCreated = [](CDT::TriInd ind, CDT::CallbackType type) -> void
* {
* std::cout << "Triangle created/changed with index " << ind << " and type";
* if (type == CDT::CallbackType::Added)
* std::cout << "ADDED" << std::endl;
* else if (type == CDT::CallbackType::Changed)
* std::cout << "CHANGED" << std::endl;
* };
* cdt.registerTriangleCallback(onTriCreated);
*/
void registerTriangleCallback(std::function<void(TriInd, CallbackType)> fnc);

/**
* Register a callback function for vertexes has been inserted
*/
void registerVertexCallback(std::function<void(VertInd, CallbackType)> fnc);
#endif
/**
* Calculate depth of each triangle in constraint triangulation. Supports
* overlapping boundaries.
Expand Down Expand Up @@ -743,6 +781,12 @@ class CDT_EXPORT Triangulation
IntersectingConstraintEdges::Enum m_intersectingEdgesStrategy;
T m_minDistToConstraintEdge;
TriIndVec m_vertTris; /// one triangle adjacent to each vertex

// C++11 required for std::fucntion callbacks
#ifndef CDT_CALLBACK_DISABLED
std::function<void(TriInd, CallbackType)> callback_tri; // callback function for triangle creation
std::function<void(VertInd, CallbackType)> callback_vertex; // callback function for vertex insertation
Copy link

@chemodansama chemodansama Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems Triangulation class members follow m_memberName naming style.
Naming style of callback_tri and callback_vertex makes members names inconsistent.

#endif
};

/// @}
Expand Down
54 changes: 53 additions & 1 deletion CDT/include/Triangulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ void Triangulation<T, TNearPointLocator>::addSuperTriangle(const Box2d<T>& box)
}

const T R = T(2) * r; // excircle radius
const T cos_30_deg = 0.8660254037844386; // note: (std::sqrt(3.0) / 2.0)
const T cos_30_deg = T(0.8660254037844386); // note: (std::sqrt(3.0) / 2.0)
const T shiftX = R * cos_30_deg;
const V2d<T> posV1 = {center.x - shiftX, center.y - r};
const V2d<T> posV2 = {center.x + shiftX, center.y - r};
Expand Down Expand Up @@ -1084,6 +1084,12 @@ void Triangulation<T, TNearPointLocator>::insertVertex(
? insertVertexInsideTriangle(iVert, trisAt[0])
: insertVertexOnEdge(iVert, trisAt[0], trisAt[1]);
ensureDelaunayByEdgeFlips(iVert, triStack);
#ifndef CDT_CALLBACK_DISABLED
if(callback_vertex)
{
callback_vertex(iVert, CallbackType::Added);
}
#endif
}

template <typename T, typename TNearPointLocator>
Expand All @@ -1093,6 +1099,12 @@ void Triangulation<T, TNearPointLocator>::insertVertex(const VertInd iVert)
const VertInd walkStart = m_nearPtLocator.nearPoint(v, vertices);
insertVertex(iVert, walkStart);
tryAddVertexToLocator(iVert);
#ifndef CDT_CALLBACK_DISABLED
if(callback_vertex)
{
callback_vertex(iVert, CallbackType::Added);
}
#endif
}

template <typename T, typename TNearPointLocator>
Expand Down Expand Up @@ -1317,6 +1329,13 @@ void Triangulation<T, TNearPointLocator>::flipEdge(
setAdjacentTriangle(v4, iT);
setAdjacentTriangle(v2, iTopo);
}
#ifndef CDT_CALLBACK_DISABLED
if(callback_tri)
{
callback_tri(iT, CallbackType::Changed); // reused so changed
callback_tri(iTopo, CallbackType::Changed); // reused so changed
}
#endif
}

/* Insert point into triangle: split into 3 triangles:
Expand Down Expand Up @@ -1366,6 +1385,14 @@ Triangulation<T, TNearPointLocator>::insertVertexInsideTriangle(
newTriangles.push(iT);
newTriangles.push(iNewT1);
newTriangles.push(iNewT2);
#ifndef CDT_CALLBACK_DISABLED
if(callback_tri)
{
callback_tri(iT, CallbackType::Changed); // reused so changed
callback_tri(iNewT1, CallbackType::Added); // new one
callback_tri(iNewT2, CallbackType::Added); // new one
}
#endif
return newTriangles;
}

Expand Down Expand Up @@ -1421,6 +1448,15 @@ std::stack<TriInd> Triangulation<T, TNearPointLocator>::insertVertexOnEdge(
newTriangles.push(iTnew2);
newTriangles.push(iT2);
newTriangles.push(iTnew1);
#ifndef CDT_CALLBACK_DISABLED
if(callback_tri)
{
callback_tri(iT1, CallbackType::Changed); // reused so changed
callback_tri(iT2, CallbackType::Changed); // reused so changed
callback_tri(iTnew1, CallbackType::Added); // new one
callback_tri(iTnew2, CallbackType::Added); // new one
}
#endif
return newTriangles;
}

Expand Down Expand Up @@ -1730,6 +1766,22 @@ bool Triangulation<T, TNearPointLocator>::isFinalized() const
return m_vertTris.empty() && !vertices.empty();
}

#ifndef CDT_CALLBACK_DISABLED
template <typename T, typename TNearPointLocator>
void Triangulation<T, TNearPointLocator>::registerTriangleCallback(
std::function<void(TriInd, CallbackType)> fnc)
{
callback_tri = fnc;
}

template <typename T, typename TNearPointLocator>
void Triangulation<T, TNearPointLocator>::registerVertexCallback(
std::function<void(VertInd, CallbackType)> fnc)
{
callback_vertex = fnc;
}
#endif

template <typename T, typename TNearPointLocator>
unordered_map<TriInd, LayerDepth>
Triangulation<T, TNearPointLocator>::peelLayer(
Expand Down