How do I iterate through all the faces in a CGAL StraightSkeleton_2 / HalfedgeDS? - cgal

My goal is to take a polygon, find the straight skeleton, then turn each face into its own polygon.
I'm using the CGAL Create_straight_skeleton_2.cpp example as a starting point. I'm able to have it compute the skeleton and can iterate through the faces:
SsPtr iss = CGAL::create_interior_straight_skeleton_2(poly);
for( auto face = iss->faces_begin(); face != iss->faces_end(); ++face ) {
// How do I iterate through the vertexes?
}
But with a HalfedgeDSFace it looks like I can only call halfedge() for a HalfedgeDSHalfedge.
At that point I'm confused how to iterate through the vertexes in the face. Do I just treat it like a circular linked list and follow the next pointer until I get back to face->halfedge()?
Here's my first attempt at treating it like a circular linked list:
SsPtr iss = CGAL::create_interior_straight_skeleton_2(poly);
std::cout << "Faces:" << iss->size_of_faces() << std::endl;
for( auto face = iss->faces_begin(); face != iss->faces_end(); ++face ) {
std::cout << "Faces:" << iss->size_of_faces() << std::endl;
std::cout << "----" << std::endl;
do {
std::cout << edge->vertex()->point() << std::endl;
edge = edge->next();
} while (edge != face->halfedge());
}
But that seems to put an empty vertex in each face:
Faces:4
----
197.401 420.778
0 0
166.95 178.812
----
511.699 374.635
0 0
197.401 420.778
----
428.06 122.923
0 0
511.699 374.635
----
166.95 178.812
0 0
428.06 122.923

So the iteration is much as I'd expected:
// Each face
for( auto face = iss->faces_begin(); face != iss->faces_end(); ++face ) {
Ss::Halfedge_const_handle begin = face->halfedge();
Ss::Halfedge_const_handle edge = begin;
// Each vertex
do {
std::cout << edge->vertex()->point() << std::endl;
edge = edge->next();
} while (edge != begin);
}
The reason it wasn't working was the contour polygon I was using had a clockwise orientation. Once I reversed the order of the points I started getting valid data out of the faces.
For reference here's how you'd iterate over the vertexes in the contour:
// Pick a face and use the opposite edge to get on the contour.
Ss::Halfedge_const_handle begin = iss->faces_begin()->halfedge()->opposite();
Ss::Halfedge_const_handle edge = begin;
do {
std::cout << edge->vertex()->point() << std::endl;
// Iterate in the opposite direction.
edge = edge->prev();
} while (edge != begin);

Related

How can I tell whether a copy-node search failed, or whether my node or graph are invalid?

Consider the CUDA graphs API function cuFindNodeInClone(). The documentation says, that it:
Returns:
CUDA_SUCCESS, CUDA_ERROR_INVALID_VALUE
This seems problematic to me. How can I tell whether the search failed (e.g. because there is no copy of the passed node in the graph), or whether the node or graph are simply invalid (e.g. nullptr)? Does the second error value signify both? Can I get a third error value which is just not mentioned?
When using the runtime API, the returned node is nullptr if the original node does not exist in the cloned graph. For nullptr original node or nullptr cloned graph, the output node is left unmodified.
#include <iostream>
#include <cassert>
int main(){
cudaError_t status;
cudaGraph_t graph;
status = cudaGraphCreate(&graph, 0);
assert(status == cudaSuccess);
cudaGraphNode_t originalNode;
status = cudaGraphAddEmptyNode(&originalNode, graph, nullptr, 0);
assert(status == cudaSuccess);
cudaGraph_t graphclone;
status = cudaGraphClone(&graphclone, graph);
assert(status == cudaSuccess);
cudaGraphNode_t anotherNode;
status = cudaGraphAddEmptyNode(&anotherNode, graph, nullptr, 0);
assert(status == cudaSuccess);
cudaGraphNode_t nodeInClone = (cudaGraphNode_t)7;
status = cudaGraphNodeFindInClone(&nodeInClone, originalNode, graphclone);
std::cout << cudaGetErrorString(status) << " " << (void*)nodeInClone << "\n";
nodeInClone = (cudaGraphNode_t)7;
status = cudaGraphNodeFindInClone(&nodeInClone, nullptr, graphclone);
std::cout << cudaGetErrorString(status) << " " << (void*)nodeInClone << "\n";
nodeInClone = (cudaGraphNode_t)7;
status = cudaGraphNodeFindInClone(&nodeInClone, originalNode, nullptr);
std::cout << cudaGetErrorString(status) << " " << (void*)nodeInClone << "\n";
nodeInClone = (cudaGraphNode_t)7;
status = cudaGraphNodeFindInClone(&nodeInClone, anotherNode, graphclone);
std::cout << cudaGetErrorString(status) << " " << (void*)nodeInClone << "\n";
}
On my machine with CUDA 11.8, this prints
no error 0x555e3cf287c0
invalid argument 0x7
invalid argument 0x7
invalid argument 0

Need help in checking type of edge/curve

I have created a sample example for the intersection of two circles.
In this example I am able to get the bound faces and the source and target points.
I have manually plotted the source and target points. Refer snapshot for the same two intersecting circles:
I want to find out whether the edges between the source and target points is a line segment, arc or a circle.
I tried to find this in the 2D arrangement documentation but couldn't find it.
Below is the code snippet :
#include <CGAL/Cartesian.h>
#include <CGAL/Exact_rational.h>
#include <CGAL/Arr_circle_segment_traits_2.h>
#include <CGAL/Arrangement_2.h>
typedef CGAL::Cartesian<CGAL::Exact_rational> Kernel;
typedef Kernel::Circle_2 Circle_2;
typedef CGAL::Arr_circle_segment_traits_2<Kernel> Traits_2;
typedef Traits_2::CoordNT CoordNT;
typedef Traits_2::Point_2 Point_2;
typedef Traits_2::Curve_2 Curve_2;
typedef CGAL::Arrangement_2<Traits_2> Arrangement_2;
int main()
{
// Create a circle centered at (0,0) with radius 8.
Kernel::Point_2 c1 = Kernel::Point_2(0, 0);
CGAL::Exact_rational sqr_r1 = CGAL::Exact_rational(64); // = 8*^2
Circle_2 circ1 = Circle_2(c1, sqr_r1, CGAL::CLOCKWISE);
Curve_2 cv1 = Curve_2(circ1);
// Create a circle centered at (10,0) with radius 8.
Kernel::Point_2 c2 = Kernel::Point_2(10, 0);
CGAL::Exact_rational sqr_r2 = CGAL::Exact_rational(64); // = 8*^2
Circle_2 circ2 = Circle_2(c2, sqr_r2, CGAL::CLOCKWISE);
Curve_2 cv2 = Curve_2(circ2);
Arrangement_2 arr;
insert(arr, cv1);
insert(arr, cv2);
for (auto fit = arr.faces_begin(); fit != arr.faces_end(); ++fit)
{
if (fit->is_unbounded())
std::cout << "Unbounded face.\n";
else {
Arrangement_2::Ccb_halfedge_circulator curr, start;
start = curr = fit->outer_ccb();
do {
std::cout << " source --> " << curr->source()->point() << "\n";
std::cout << " target --> " << curr->target()->point() << "\n";
++curr;
} while (curr != start);
std::cout << std::endl;
}
}
return 0;
}

CGAL example cannot read input files?

this is my first stackoverflow question, so I hope the following text meets the question requirements. If not, please tell me what needs to be changed so I can adapt the question.
I'm new to CGAL and C++ in general. I would like to use CGAL 5.0.2 on a Macbook Pro early 2015 with macOS Catalina Version 10.15.4.
So to begin with, I followed the instruction steps given by the CGAL documentation using the package manager Homebrew. Since CGAL is a header-only library I configured it using CMake, as is recommended by the documentation.
It all worked out fine, so I went on trying the recommended examples given in the file CGAL-5.0.2.tar.xz, which is provided here. I'm particularly interested in the example Voronoi_Diagram_2.
Using the Terminal I executed the command -DCGAL_DIR=$HOME/CGAL-5.0.2 -DCMAKE_BUILD_TYPE=Release . in the example folder called Voronoi_Diagram_2. Then I executed the command make. All went well, no error messages were prompted. But executing the resulting exec file didn't produce any results.
After some research I managed to modify the code in a way that it prints the values of some variables. Problem seems to be that the input file which contains the line segments for which the voronoi diagramm shall be calculated is not correctly read.
The while loop which I highlighted in the code below by inserting //// signs seems not to be entered. That's why I assume that the variable ifs is empty, even though the input file "data1.svd.cin", which can be found in the folder "data" of the example, wasn't.
Does anyone have an idea for the reasons of this behaviour? Any help is appreciated.
This is the vd_2_point_location_sdg_linf.cpp file included in the example, which I modified:
// standard includes
#include <iostream>
#include <fstream>
#include <cassert>
// includes for defining the Voronoi diagram adaptor
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Segment_Delaunay_graph_Linf_filtered_traits_2.h>
#include <CGAL/Segment_Delaunay_graph_Linf_2.h>
#include <CGAL/Voronoi_diagram_2.h>
#include <CGAL/Segment_Delaunay_graph_adaptation_traits_2.h>
#include <CGAL/Segment_Delaunay_graph_adaptation_policies_2.h>
// typedefs for defining the adaptor
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Segment_Delaunay_graph_Linf_filtered_traits_2<K> Gt;
typedef CGAL::Segment_Delaunay_graph_Linf_2<Gt> DT;
typedef CGAL::Segment_Delaunay_graph_adaptation_traits_2<DT> AT;
typedef CGAL::Segment_Delaunay_graph_degeneracy_removal_policy_2<DT> AP;
typedef CGAL::Voronoi_diagram_2<DT,AT,AP> VD;
// typedef for the result type of the point location
typedef AT::Site_2 Site_2;
typedef AT::Point_2 Point_2;
typedef VD::Locate_result Locate_result;
typedef VD::Vertex_handle Vertex_handle;
typedef VD::Face_handle Face_handle;
typedef VD::Halfedge_handle Halfedge_handle;
typedef VD::Ccb_halfedge_circulator Ccb_halfedge_circulator;
void print_endpoint(Halfedge_handle e, bool is_src) {
std::cout << "\t";
if ( is_src ) {
if ( e->has_source() ) std::cout << e->source()->point() << std::endl;
else std::cout << "point at infinity" << std::endl;
} else {
if ( e->has_target() ) std::cout << e->target()->point() << std::endl;
else std::cout << "point at infinity" << std::endl;
}
}
int main()
{
std::ifstream ifs("data/data1.svd.cin");
assert( ifs );
VD vd;
Site_2 t;
// /////////// Inserted Comment ////////////////////////////////
std::cout << "In the following the insertion from ifs should take place" << std::flush;
// ///////////////// while loop which doesn't seem to be active //////////////////
while ( ifs >> t ) {
// Existing Code to insert the points in the voronoi structure
vd.insert(t);
// Inserted Code to check if while loop is entered
std::cout << "Entered while loop" << std::flush;
}
// ///////////////////////////////////////////////////////////////////////////////
ifs.close();
assert( vd.is_valid() );
std::ifstream ifq("data/queries1.svd.cin");
assert( ifq );
Point_2 p;
while ( ifq >> p ) {
std::cout << "Query point (" << p.x() << "," << p.y()
<< ") lies on a Voronoi " << std::flush;
Locate_result lr = vd.locate(p);
if ( Vertex_handle* v = boost::get<Vertex_handle>(&lr) ) {
std::cout << "vertex." << std::endl;
std::cout << "The Voronoi vertex is:" << std::endl;
std::cout << "\t" << (*v)->point() << std::endl;
} else if ( Halfedge_handle* e = boost::get<Halfedge_handle>(&lr) ) {
std::cout << "edge." << std::endl;
std::cout << "The source and target vertices "
<< "of the Voronoi edge are:" << std::endl;
print_endpoint(*e, true);
print_endpoint(*e, false);
} else if ( Face_handle* f = boost::get<Face_handle>(&lr) ) {
std::cout << "face." << std::endl;
std::cout << "The vertices of the Voronoi face are"
<< " (in counterclockwise order):" << std::endl;
Ccb_halfedge_circulator ec_start = (*f)->ccb();
Ccb_halfedge_circulator ec = ec_start;
do {
print_endpoint(ec, false);
} while ( ++ec != ec_start );
}
std::cout << std::endl;
}
ifq.close();
return 0;
}

CGAL hole filling with color

I need to implement a 3D hole filling using CGAL library that support color.
is there any possibility to do it without CGAL library modification? I need to fill the hole with an average color of the hole's edge.
Regards, Ali
....
int main(int argc, char* argv[])
{
const char* filename = (argc > 1) ? argv[1] : "data/mech-holes-shark.off";
Mesh mesh;
OpenMesh::IO::read_mesh(mesh, filename);
// Incrementally fill the holes
unsigned int nb_holes = 0;
BOOST_FOREACH(halfedge_descriptor h, halfedges(mesh))
{
if(CGAL::is_border(h,mesh))
{
std::vector<face_descriptor> patch_facets;
std::vector<vertex_descriptor> patch_vertices;
bool success = CGAL::cpp11::get<0>(
CGAL::Polygon_mesh_processing::triangulate_refine_and_fair_hole(
mesh,
h,
std::back_inserter(patch_facets),
std::back_inserter(patch_vertices),
CGAL::Polygon_mesh_processing::parameters::vertex_point_map(get(CGAL::vertex_point, mesh)).
geom_traits(Kernel())) );
CGAL_assertion(CGAL::is_valid_polygon_mesh(mesh));
std::cout << "* FILL HOLE NUMBER " << ++nb_holes << std::endl;
std::cout << " Number of facets in constructed patch: " << patch_facets.size() << std::endl;
std::cout << " Number of vertices in constructed patch: " << patch_vertices.size() << std::endl;
std::cout << " Is fairing successful: " << success << std::endl;
}
}
CGAL_assertion(CGAL::is_valid_polygon_mesh(mesh));
OpenMesh::IO::write_mesh(mesh, "filled_OM.off");
return 0;
}
If you use CGAL::Surface_mesh as Mesh, you can use dynamic property maps to define attributes for your simplices, which allows for example to define colors per face. The "standard" syntax for this is
mesh.add_property_map<face_descriptor, CGAL::Color >("f:color")
I think. There are examples in the documentation of Surface_mesh.

OpenNI2+Nite2 isNew() isLost() methods in Kinect SDK

I recently switched from OpenNI2+Nite2 confuguration to official Kinect SDK for a project. In nite my code goes like this:
const nite::Array<nite::UserData>& users = frame.getUsers();
for (int i=0; i<users.getSize(); i++){
const nite::UserData& user = users[i];
if(user.isNew()){/* do this */}
if(user.isLost()){/* do that */}
else {/* update*/}
However, I couldn't find a method that does the same thing as isNew & isLost in Kinect SDK. I implemented my own method for isNew, but I failed in isLost.
// handle user exit
map<string,Group3D*> g3D_copy = g3D;
for(map<string,Group3D*>::iterator mit = g3D_copy.begin();mit != g3D_copy.end();mit++){
if(mit->second->getType() == "KINECT_SKELETON")
{
string groupID = mit->first;
int countExistance2 = 0;
for (int i = 0; i < NUI_SKELETON_COUNT; i++){
int userID = (int)SkeletonFrame.SkeletonData[i].dwTrackingID;
char buffer [33];
sprintf(buffer,"%lu",SkeletonFrame.SkeletonData[i].dwTrackingID);
string myID2 = buffer;
cout << "groupID " << groupID << endl;
cout << "myID2 " << myID2 << endl;
if(myID2 == groupID){ countExistance2++;}
}
// user lost
if(countExistance2 == 0){
delete g3D[groupID];
g3D.erase(groupID);
cout << "*************deleted*******" << endl;
}
}
}
Basicly, I am trying to erase the dedicated slot to a skeleton in a map called g3D in every update of the skeleton frame if the skeleton is lost.
Any ideas or sharp eyes are appreciated.
Finally I solved the problem by counting the frames which do not have a tracked skeleton. Skeleton data has 6 slots and in some frames its tracking id is not set to NUI_SKELETON_TRACKED.
Thus, If the number of empty skeleton slots exceeds 20 (which means appx 3-4 successive empty frames), I assume the user is lost.
// handle user exit
// The skeleton is not tracked in every successive frame, so use g3DCounts to count the number of frames
map<string,Group3D*> g3D_copy = g3D;
for(map<string,Group3D*>::iterator mit = g3D_copy.begin();mit != g3D_copy.end();mit++){
if(mit->second->getType() == "KINECT_SKELETON")
{
string groupID = mit->first;
for (int i = 0; i < NUI_SKELETON_COUNT; i++){
char buffer [33];
sprintf(buffer,"%lu",SkeletonFrame.SkeletonData[i].dwTrackingID);
string myID2 = buffer;
if(myID2 == groupID){ g3DCounts[groupID] = 0;}
else{ g3DCounts[groupID] += 1;}
}
if(g3DCounts[groupID] > 20){
delete g3D[groupID];
g3D.erase(groupID);
cout << "*************deleted successfully*******" << endl;
}
}
}
Hope it helps others