Converting Surface_mesh to Nef_polyhedron_3 - cgal

I'm trying to use CGAL to do some boolean operations on meshes.
How do I convert from Surface_mesh to Nef_polyhedron_3?
EDIT:
I've tried with this code, but I don't know how to continue...
#include <iostream>
#include <CGAL/Nef_polyhedron_3.h>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
using namespace std;
typedef CGAL::Simple_cartesian<double> K;
typedef CGAL::Surface_mesh<K::Point_3> Mesh;
int main()
{
Mesh m;
auto a = m.add_vertex(K::Point_3(0,0,0));
auto b = m.add_vertex(K::Point_3(0,0,0));
auto c = m.add_vertex(K::Point_3(0,0,0));
m.add_face(a,b,c);
Mesh::Halfedge_range range = m.halfedges();
for(Mesh::Halfedge_index hei : range)
{
// ??? <<--
std::cout << hei << std::endl;
}
return 0;
}
Thanks

I think the suggested way to do this is to use the 3d polyhedral surface package instead. The Nef 3 documentation describes the conversion between Polyhedron_3 and Nef_3. The only difference between the 3d polyhedral surface package and the surface mesh package is that, it is pointer based rather than index based.

Related

g++ Static analysis: false positive with -fanalyzer?

Running this very little snippet, to show a problem I have with a much larger code:
// Type your code here, or load an example.
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
int main() {
auto res = make_unique<int>();
auto ptr = res.get();
if (ptr) {
*ptr = 5;
cout << *ptr << endl;
}
return 0;
}
with the -fanalyzer switch, I get a warning
warning: dereference of possibly-NULL 'operator new(4)' [CWE-690] [-Wanalyzer-possible-null-dereference]
But clearly I made all I could do to avoid this warning, but it is buried in the STL, which returns a unique_ptr with no validity control..
I understand the word "possibly" though..
Anyway to correct this on my side?
Update:
I made a mistake in the first go, now corrected
Update 2:
Even that code is refused
// Type your code here, or load an example.
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
int main() {
auto i = new int(3);
if (!i) {
return 1;
}
unique_ptr<int> res(i);
auto ptr = res.get();
if (!ptr) {
return 1;
}
*ptr = 5;
cout << *ptr << endl;
return 0;
}
Please, see here
As for now (gcc-12), the analyzer is not recommended for C++ code although work is underway to support it.
https://developers.redhat.com/articles/2022/04/12/state-static-analysis-gcc-12-compiler#toward_support_for_c__

How to copy a surface mesh in CGAL

I want to copy a mesh with the function copy_face_graph(source, target). But the target mesh is different (it has same number of vertices and faces, but the coordinates and the order are totally different).
The code:
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <iostream>
#include <fstream>
#include <CGAL/boost/graph/copy_face_graph.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Surface_mesh<Kernel::Point_3> Mesh;
namespace PMP = CGAL::Polygon_mesh_processing;
int main(int argc, char* argv[]) {
const char* filename1 = (argc > 1) ? argv[1] : "data/blobby.off";
std::cout << ".off loaded" << std::endl;
std::ifstream input(filename1);
Mesh mesh_orig;
if (!input || !(input >> mesh_orig))
{
std::cerr << "First mesh is not a valid off file." << std::endl;
return 1;
}
input.close();
// ========================================================
Mesh mesh_copy;
CGAL::copy_face_graph(mesh_orig, mesh_copy);
// ========================================================
std::ofstream mesh_cpy("CPY_ANYLYZE/mesh_copy.off");
mesh_cpy << mesh_copy;
mesh_cpy.close();
return 0;
}
Dose anyone knows how to get a complete same mesh from the original mesh? Do I need add the named parameters, or maybe using another function?
Thanks a lot
Except if you intend to write some code working with different data structures, you can use the copy constructor from the Surface_mesh class, Mesh mesh_copy(mesh_orig). copy_face_graph does not do a raw copy because it works also if the input and output are of different types. However the output should be the same up to the order of the simplices.

How to get the remaining triangles after 2D alpha shape using CGAL?

From the CGAL documentation, one can create an alpha_shape_2 from a Delaunay triangulation:
CGAL::Alpha_shape_2< Dt, ExactAlphaComparisonTag >::Alpha_shape_2(Dt& dt, FT alpha = 0, Mode m = GENERAL)
However the operation destroys the triangulation.
In my problem I have a bunch of points which are triangulated. I need to identify the "right" triangles using the alpha shape algorithm. I already computed that myself from the delaunay triangulation (computing the circumcircle radius myself and so on), since I did not find a way to extract the remaining triangles from alpha_shape_2 (I can extract the edges of the alpha shape but not the inner triangles). Is it only possible using CGAL ?
For example in matlab (ouch) one can do:
shp = alphaShape(points.x,points.y);
shp.Alpha = alpha;
tri = alphaTriangulation(shp);
bf = boundaryFacets(shp);
Side question: what is the definition of the alpha value of cgal ? Mine is : r_c/h>alpha, where r_c is the triangle circumcircle radius and h and size parameter?
For the side question see this section. About your initial question I'm not sure to understand precisely what you want to get but you can iterate all the triangles and get their classification using the following code:
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Alpha_shape_2.h>
#include <CGAL/Alpha_shape_vertex_base_2.h>
#include <CGAL/Alpha_shape_face_base_2.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <vector>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::FT FT;
typedef K::Point_2 Point;
typedef CGAL::Alpha_shape_vertex_base_2<K> Vb;
typedef CGAL::Alpha_shape_face_base_2<K> Fb;
typedef CGAL::Triangulation_data_structure_2<Vb,Fb> Tds;
typedef CGAL::Delaunay_triangulation_2<K,Tds> Triangulation_2;
typedef CGAL::Alpha_shape_2<Triangulation_2> Alpha_shape_2;
int main()
{
std::vector<Point> points;
double alpha;
Alpha_shape_2 A(points.begin(), points.end(),
alpha,
Alpha_shape_2::GENERAL);
for (Alpha_shape_2::Finite_faces_iterator fit=A.finite_faces_begin();
fit!=A.finite_faces_end();++fit)
{
switch(A.classify(fit))
{
case Alpha_shape_2::REGULAR:
break;
case Alpha_shape_2::SINGULAR:
break;
case Alpha_shape_2::EXTERIOR:
break;
case Alpha_shape_2::INTERIOR:
break;
}
}
return 0;
}

Using AABB_tree with multiple surface meshes

I'm trying to use CGAL's AABB_tree with multiple Surface_mesh and fail an odd assertion which makes me think it's trying to use the first surface mesh's vertices with the second mesh's indices or something similarly weird.
Before I file a bug, I'd like to validate that I'm not misunderstanding something.
Here's a minimally modified example. I'm using cube.off from: https://github.com/libigl/libigl/blob/master/tutorial/shared/cube.off and the Tetrahedron from CGAL's examples, but it seems to reproduce every time the second surface mesh I add has less vertices than the first mesh no matter what it is.
The assertion I'm failing is /usr/local/include/CGAL/Surface_mesh/Properties.h:178 - CGAL_assertion( idx < data.size() );
Using:
CGAL_VERSION 4.12
CGAL_VERSION_NR 1041201000
CGAL_SVN_REVISION 99999
CGAL_GIT_HASH f7c3c8212b56c0d6dae63787efc99093f4383415
#include <iostream>
#include <fstream>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/AABB_face_graph_triangle_primitive.h>
typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_3 Point;
typedef K::Ray_3 Ray;
typedef CGAL::Surface_mesh<Point> Mesh;
typedef CGAL::AABB_face_graph_triangle_primitive<Mesh> Primitive;
typedef CGAL::AABB_traits<K, Primitive> Traits;
typedef CGAL::AABB_tree<Traits> Tree;
typedef boost::optional<Tree::Intersection_and_primitive_id<Ray>::Type> Ray_intersection;
int main(int argc, char* argv[])
{
const char* filename1 = "cube.off";
const char* filename2 = "tetrahedron.off";
std::ifstream input1(filename1);
Mesh mesh1;
input1 >> mesh1;
std::ifstream input2(filename2);
Mesh mesh2;
input2 >> mesh2;
Tree tree;
tree.insert(faces(mesh1).first, faces(mesh1).second, mesh1);
tree.insert(faces(mesh2).first, faces(mesh2).second, mesh2);
tree.build(); // CGAL_assertion( idx < data.size() ) fails
return 0;
}
I repost my comment as an answer:
From my comment: Actually you can use this primitive but you need to set the template tag OneFaceGraphPerTree to CGAL::Tag_false.
See here

Do constrained refinement with CGAL isotropic_remeshing

I'd like to do refinement of eg a simple cube (from a .off); there are a few ways but the ones suitable for what I want to do next end up with 'wrinkles', ie the object shape gets distorted.
This way below promises to allow the boundaries (shape?) of the object to be preserved, permitting what you'd expect of refinement, to just add more edges and vertices:
http://doc.cgal.org/latest/Polygon_mesh_processing/Polygon_mesh_processing_2isotropic_remeshing_example_8cpp-example.html
I want an edge constraint map (and if that isn't sufficient then I'll want a vertex constraint map as well) but can't figure out the template abstractions well enough. I tried an OpenMesh Constrained_edge_map from a different CGAL example, but that's too different and won't compile. What I'm asking for is an edge map and maybe a vertex map that I can feed to the call:
PMP::isotropic_remeshing(
faces(mesh),
target_edge_length,
mesh,
PMP::parameters::number_of_iterations(nb_iter)
.protect_constraints(true)//i.e. protect border, here
);
I'm using CGAL 4.8.1, the latest at time of writing. Thanks.
Here is a minimal example to remesh a triangulated cube:
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/boost/graph/graph_traits_Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/remesh.h>
#include <CGAL/Mesh_3/dihedral_angle_3.h>
#include <boost/foreach.hpp>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Surface_mesh<K::Point_3> Mesh;
typedef boost::graph_traits<Mesh>::halfedge_descriptor halfedge_descriptor;
typedef boost::graph_traits<Mesh>::edge_descriptor edge_descriptor;
namespace PMP=CGAL::Polygon_mesh_processing;
int main(int, char* argv[])
{
std::ifstream input(argv[1]);
Mesh tmesh;
input >> tmesh;
double target_edge_length = 0.20;
unsigned int nb_iter = 10;
// give each vertex a name, the default is empty
Mesh::Property_map<edge_descriptor,bool> is_constrained =
tmesh.add_property_map<edge_descriptor,bool>("e:is_constrained",false).first;
//detect sharp features
BOOST_FOREACH(edge_descriptor e, edges(tmesh))
{
halfedge_descriptor hd = halfedge(e,tmesh);
if ( !is_border(e,tmesh) ){
double angle = CGAL::Mesh_3::dihedral_angle(tmesh.point(source(hd,tmesh)),
tmesh.point(target(hd,tmesh)),
tmesh.point(target(next(hd,tmesh),tmesh)),
tmesh.point(target(next(opposite(hd,tmesh),tmesh),tmesh)));
if ( CGAL::abs(angle)<100 )
is_constrained[e]=true;
}
}
//remesh
PMP::isotropic_remeshing(
faces(tmesh),
target_edge_length,
tmesh,
PMP::parameters::number_of_iterations(nb_iter)
.edge_is_constrained_map(is_constrained) );
std::ofstream out("out.off");
out << tmesh;
return 0;
}