mirror of https://github.com/CGAL/cgal
Add an example to build voronoi_2.
This commit is contained in:
parent
488e51e597
commit
f6cd69aa7b
|
|
@ -2036,6 +2036,7 @@ Linear_cell_complex/examples/Linear_cell_complex/linear_cell_complex_3_viewer_vt
|
||||||
Linear_cell_complex/examples/Linear_cell_complex/linear_cell_complex_3_with_colored_vertices.cpp -text
|
Linear_cell_complex/examples/Linear_cell_complex/linear_cell_complex_3_with_colored_vertices.cpp -text
|
||||||
Linear_cell_complex/examples/Linear_cell_complex/linear_cell_complex_4.cpp -text
|
Linear_cell_complex/examples/Linear_cell_complex/linear_cell_complex_4.cpp -text
|
||||||
Linear_cell_complex/examples/Linear_cell_complex/plane_graph_to_lcc_2.cpp -text
|
Linear_cell_complex/examples/Linear_cell_complex/plane_graph_to_lcc_2.cpp -text
|
||||||
|
Linear_cell_complex/examples/Linear_cell_complex/voronoi_2.cpp -text
|
||||||
Linear_cell_complex/examples/Linear_cell_complex/voronoi_3.cpp -text
|
Linear_cell_complex/examples/Linear_cell_complex/voronoi_3.cpp -text
|
||||||
Linear_cell_complex/include/CGAL/Cell_attribute_with_point.h -text
|
Linear_cell_complex/include/CGAL/Cell_attribute_with_point.h -text
|
||||||
Linear_cell_complex/include/CGAL/Linear_cell_complex.h -text
|
Linear_cell_complex/include/CGAL/Linear_cell_complex.h -text
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,12 @@ if ( CGAL_FOUND )
|
||||||
#include("CMakeLCCViewerQt.inc")
|
#include("CMakeLCCViewerQt.inc")
|
||||||
#include("CMakeLCCViewerVtk.inc")
|
#include("CMakeLCCViewerVtk.inc")
|
||||||
|
|
||||||
|
add_executable(voronoi_2 voronoi_2.cpp)
|
||||||
|
target_link_libraries(voronoi_2 ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES})
|
||||||
|
# And if you use a viewer, you have to link with the corresponding lib.
|
||||||
|
target_link_libraries(voronoi_2 ${MAP_VIEWER_LIBRARIES_QT})
|
||||||
|
#target_link_libraries(voronoi_3 ${MAP_VIEWER_LIBRARIES_VTK})
|
||||||
|
|
||||||
add_executable(voronoi_3 voronoi_3.cpp)
|
add_executable(voronoi_3 voronoi_3.cpp)
|
||||||
target_link_libraries(voronoi_3 ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES})
|
target_link_libraries(voronoi_3 ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES})
|
||||||
# And if you use a viewer, you have to link with the corresponding lib.
|
# And if you use a viewer, you have to link with the corresponding lib.
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,114 @@
|
||||||
|
#include <CGAL/Linear_cell_complex.h>
|
||||||
|
#include <CGAL/Linear_cell_complex_constructors.h>
|
||||||
|
#include <CGAL/Linear_cell_complex_operations.h>
|
||||||
|
#include <CGAL/Delaunay_triangulation_2.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
/* If you want to use a viewer, you can use one of the following file
|
||||||
|
* depending if you use vtk or qglviewer. */
|
||||||
|
#ifdef CGAL_LCC_USE_QT
|
||||||
|
//#include "linear_cell_complex_3_viewer_qt.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef CGAL::Linear_cell_complex<2> LCC_2;
|
||||||
|
typedef LCC_2::Dart_handle Dart_handle;
|
||||||
|
typedef LCC_2::Point Point;
|
||||||
|
|
||||||
|
typedef CGAL::Delaunay_triangulation_2<LCC_2::Traits> Triangulation;
|
||||||
|
|
||||||
|
// Function used to display the voronoi diagram.
|
||||||
|
void display_voronoi(LCC_2& alcc, Dart_handle adart)
|
||||||
|
{
|
||||||
|
// We remove all the faces containing one dart of the infinite faces
|
||||||
|
std::stack<Dart_handle> toremove;
|
||||||
|
int mark_toremove=alcc.get_new_mark();
|
||||||
|
|
||||||
|
// We cannot view the infinite face since it does not have
|
||||||
|
// a correct geometry. For that we have to remove the infinite face.
|
||||||
|
toremove.push(adart);
|
||||||
|
CGAL::mark_cell<LCC_2,2>(alcc, adart, mark_toremove);
|
||||||
|
|
||||||
|
// Plus all the faces sharing an edge with it.
|
||||||
|
for (LCC_2::Dart_of_cell_range<2>::iterator
|
||||||
|
it=alcc.darts_of_cell<2>(adart).begin(),
|
||||||
|
itend=alcc.darts_of_cell<2>(adart).end(); it!=itend; ++it)
|
||||||
|
{
|
||||||
|
if ( !alcc.is_marked(it->beta(2), mark_toremove) )
|
||||||
|
{
|
||||||
|
CGAL::mark_cell<LCC_2,2>(alcc, it->beta(2), mark_toremove);
|
||||||
|
toremove.push(it->beta(2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while( !toremove.empty() )
|
||||||
|
{
|
||||||
|
CGAL::remove_cell<LCC_2, 2>(alcc, toremove.top());
|
||||||
|
toremove.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef CGAL_LCC_USE_VIEWER
|
||||||
|
// display_lcc(alcc);
|
||||||
|
#endif // CGAL_LCC_USE_VIEWER
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int narg, char** argv)
|
||||||
|
{
|
||||||
|
if (narg>1 && (!strcmp(argv[1],"-h") || !strcmp(argv[1],"-?")) )
|
||||||
|
{
|
||||||
|
std::cout<<"Usage : voronoi_2 filename"<<std::endl
|
||||||
|
<<" filename being a fine containing 3D points used to "
|
||||||
|
<<" compute the Delaunay_triangulation_3."<<std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string filename;
|
||||||
|
if ( narg==1 )
|
||||||
|
{
|
||||||
|
filename=std::string("data/points");
|
||||||
|
std::cout<<"No filename given: use data/points by default."<<std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
filename=std::string(argv[1]);
|
||||||
|
|
||||||
|
// 1) Compute the Delaunay_triangulation_2.
|
||||||
|
Triangulation T;
|
||||||
|
|
||||||
|
std::ifstream iFile(filename.c_str());
|
||||||
|
if (!iFile)
|
||||||
|
{
|
||||||
|
std::cout << "Problem reading file " << filename << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::istream_iterator<Point> begin(iFile), end;
|
||||||
|
T.insert(begin, end);
|
||||||
|
assert(T.is_valid(false));
|
||||||
|
|
||||||
|
// 2) Convert the triangulation into a 2D lcc.
|
||||||
|
LCC_2 lcc;
|
||||||
|
Dart_handle dh=
|
||||||
|
CGAL::import_from_triangulation_2<LCC_2, Triangulation>(lcc, T);
|
||||||
|
|
||||||
|
std::cout<<"Delaunay triangulation :"<<std::endl<<" ";
|
||||||
|
lcc.display_characteristics(std::cout) << ", valid="
|
||||||
|
<< lcc.is_valid() << std::endl;
|
||||||
|
|
||||||
|
// 3) Compute the dual lcc.
|
||||||
|
LCC_2 dual_lcc;
|
||||||
|
Dart_handle ddh=CGAL::dual<LCC_2>(lcc, dual_lcc, dh);
|
||||||
|
// Here, dual_lcc is the 3D Voronoi diagram.
|
||||||
|
|
||||||
|
// 4) Display the dual_lcc characteristics.
|
||||||
|
std::cout<<"Voronoi subdvision :"<<std::endl<<" ";
|
||||||
|
dual_lcc.display_characteristics(std::cout) << ", valid="
|
||||||
|
<< dual_lcc.is_valid()
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
|
#ifdef CGAL_LCC_USE_VIEWER
|
||||||
|
display_voronoi(dual_lcc, ddh);
|
||||||
|
#endif // CGAL_LCC_USE_VIEWER
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -165,6 +165,107 @@ namespace CGAL {
|
||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Convert a given Triangulation_2 into a 2D linear cell complex.
|
||||||
|
* @param alcc the used linear cell complex.
|
||||||
|
* @param atr the Triangulation_2.
|
||||||
|
* @return A dart incident to the infinite vertex.
|
||||||
|
*/
|
||||||
|
template < class LCC, class Triangulation >
|
||||||
|
typename LCC::Dart_handle import_from_triangulation_2
|
||||||
|
(LCC& alcc, const Triangulation &atr)
|
||||||
|
{
|
||||||
|
CGAL_static_assertion( LCC::dimension>=2 && LCC::ambient_dimension==2 );
|
||||||
|
|
||||||
|
// Case of empty triangulations.
|
||||||
|
if (atr.number_of_vertices() == 0) return NULL;
|
||||||
|
|
||||||
|
// Check the dimension.
|
||||||
|
if (atr.dimension() != 2) return NULL;
|
||||||
|
CGAL_assertion(atr.is_valid());
|
||||||
|
|
||||||
|
typedef typename Triangulation::Vertex_handle TVertex_handle;
|
||||||
|
typedef typename Triangulation::Vertex_iterator TVertex_iterator;
|
||||||
|
typedef typename Triangulation::All_faces_iterator TFace_iterator;
|
||||||
|
typedef typename std::map
|
||||||
|
< TFace_iterator, typename LCC::Dart_handle >::iterator itmap_tcell;
|
||||||
|
|
||||||
|
// Create vertices in the map and associate in a map
|
||||||
|
// TVertex_handle and vertices in the map.
|
||||||
|
std::map< TVertex_handle, typename LCC::Vertex_attribute_handle > TV;
|
||||||
|
for (TVertex_iterator itv = atr.vertices_begin();
|
||||||
|
itv != atr.vertices_end(); ++itv)
|
||||||
|
{
|
||||||
|
// if (it != atr.infinite_vertex())
|
||||||
|
{
|
||||||
|
TV[itv] = alcc.create_vertex_attribute(itv->point());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the triangles and create a map to link Cell_iterator
|
||||||
|
// and triangles.
|
||||||
|
TFace_iterator it;
|
||||||
|
|
||||||
|
std::map< TFace_iterator, typename LCC::Dart_handle > TC;
|
||||||
|
itmap_tcell maptcell_it;
|
||||||
|
|
||||||
|
typename LCC::Dart_handle res=NULL, dart=NULL;
|
||||||
|
typename LCC::Dart_handle cur=NULL, neighbor=NULL;
|
||||||
|
|
||||||
|
for (it = atr.all_faces_begin(); it != atr.all_faces_end(); ++it)
|
||||||
|
{
|
||||||
|
/* if (it->vertex(0) != atr.infinite_vertex() &&
|
||||||
|
it->vertex(1) != atr.infinite_vertex() &&
|
||||||
|
it->vertex(2) != atr.infinite_vertex() &&
|
||||||
|
it->vertex(3) != atr.infinite_vertex())
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
res = alcc.make_triangle(TV[it->vertex(0)],
|
||||||
|
TV[it->vertex(1)],
|
||||||
|
TV[it->vertex(2)]);
|
||||||
|
|
||||||
|
if ( it->vertex(0) == atr.infinite_vertex() && dart==NULL )
|
||||||
|
dart = res;
|
||||||
|
|
||||||
|
for (unsigned int i=0; i<3; ++i)
|
||||||
|
{
|
||||||
|
switch (i)
|
||||||
|
{
|
||||||
|
case 0: cur = res->beta(1); break;
|
||||||
|
case 1: cur = res->beta(0); break;
|
||||||
|
case 2: cur = res; break;
|
||||||
|
}
|
||||||
|
|
||||||
|
maptcell_it = TC.find(it->neighbor(i));
|
||||||
|
if (maptcell_it != TC.end())
|
||||||
|
{
|
||||||
|
switch (it->neighbor(i)->index(it))
|
||||||
|
{
|
||||||
|
case 0: neighbor =
|
||||||
|
maptcell_it->second->beta(1);
|
||||||
|
break;
|
||||||
|
case 1: neighbor =
|
||||||
|
maptcell_it->second->beta(0);
|
||||||
|
break;
|
||||||
|
case 2: neighbor = maptcell_it->second; break;
|
||||||
|
}
|
||||||
|
while (LCC::vertex_attribute(neighbor) !=
|
||||||
|
LCC::vertex_attribute(cur->other_extremity()) )
|
||||||
|
neighbor = neighbor->beta(1);
|
||||||
|
alcc.template topo_sew<2>(cur, neighbor);
|
||||||
|
if (!neighbor->beta(0)->is_free(2) &&
|
||||||
|
!neighbor->beta(1)->is_free(2))
|
||||||
|
TC.erase(maptcell_it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (res->is_free(2) ||
|
||||||
|
res->beta(0)->is_free(3) ||
|
||||||
|
res->beta(1)->is_free(3))
|
||||||
|
TC[it] = res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dart;
|
||||||
|
}
|
||||||
|
|
||||||
/** Convert a given Triangulation_3 into a 3D linear cell complex.
|
/** Convert a given Triangulation_3 into a 3D linear cell complex.
|
||||||
* @param alcc the used linear cell complex.
|
* @param alcc the used linear cell complex.
|
||||||
* @param atr the Triangulation_3.
|
* @param atr the Triangulation_3.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue