From ab9345670c4412b1ee52a5955344693b9fe5c033 Mon Sep 17 00:00:00 2001 From: vmora Date: Mon, 31 Mar 2014 16:43:19 +0200 Subject: [PATCH] fixes polygon triangulation --- include/SFCGAL/algorithm/plane.h | 9 +++++++-- .../ConstraintDelaunayTriangulation.cpp | 15 +++++---------- src/SFCGAL/triangulate/triangulatePolygon.cpp | 18 ++++++++++-------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/include/SFCGAL/algorithm/plane.h b/include/SFCGAL/algorithm/plane.h index 90e8beb0..ff71e137 100644 --- a/include/SFCGAL/algorithm/plane.h +++ b/include/SFCGAL/algorithm/plane.h @@ -112,8 +112,13 @@ void plane3D( template < typename Kernel > CGAL::Plane_3< Kernel > plane3D( const Polygon& polygon, bool exact = true ) { - CGAL::Vector_3< Kernel > normal = normal3D< Kernel >( polygon, exact ); - return CGAL::Plane_3< Kernel >( polygon.exteriorRing().pointN( 0 ).toPoint_3(), normal ); + CGAL::Vector_3< Kernel > nrml = normal3D< Kernel >( polygon, exact ); + if ( !exact ) + { + const double nrm = std::sqrt( CGAL::to_double( nrml.squared_length() ) ); + nrml = CGAL::Vector_3< Kernel >( nrml.x()/nrm, nrml.y()/nrm, nrml.z()/nrm ); + } + return CGAL::Plane_3< Kernel >( polygon.exteriorRing().pointN( 0 ).toPoint_3(), nrml ); } diff --git a/src/SFCGAL/detail/triangulate/ConstraintDelaunayTriangulation.cpp b/src/SFCGAL/detail/triangulate/ConstraintDelaunayTriangulation.cpp index c0318019..c2ac150b 100644 --- a/src/SFCGAL/detail/triangulate/ConstraintDelaunayTriangulation.cpp +++ b/src/SFCGAL/detail/triangulate/ConstraintDelaunayTriangulation.cpp @@ -50,16 +50,11 @@ ConstraintDelaunayTriangulation::Vertex_handle ConstraintDelaunayTriangulation:: ) ); } - if ( _projectionPlane ) { - Vertex_handle vertex = _cdt.insert( _projectionPlane->to_2d( position.toPoint_3() ) ); - vertex->info().original = position ; - return vertex ; - } - else { - Vertex_handle vertex = _cdt.insert( position.toPoint_2() ); - vertex->info().original = position ; - return vertex ; - } + Vertex_handle vertex = _projectionPlane + ? _cdt.insert( _projectionPlane->to_2d( position.toPoint_3() ) ) + : _cdt.insert( position.toPoint_2() ); + vertex->info().original = position ; + return vertex ; } /// diff --git a/src/SFCGAL/triangulate/triangulatePolygon.cpp b/src/SFCGAL/triangulate/triangulatePolygon.cpp index 5ade8ed2..946f100c 100644 --- a/src/SFCGAL/triangulate/triangulatePolygon.cpp +++ b/src/SFCGAL/triangulate/triangulatePolygon.cpp @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -162,17 +163,18 @@ void triangulatePolygon3D( for ( size_t i = 0; i < polygon.numRings(); i++ ) { const LineString& ring = polygon.ringN( i ); - Vertex_handle v_prev ; - - for ( size_t j = 0; j < ring.numPoints(); j++ ) { + /* + * note, we do not include the last point, since it's equal to the last and that + */ + if (!ring.numPoints()) continue; + Vertex_handle v_prev = cdt.addVertex( ring.pointN( 0 ).coordinate() ); + Vertex_handle v_0 = v_prev; + for ( size_t j = 1; j < ring.numPoints()-1; j++ ) { Vertex_handle vh = cdt.addVertex( ring.pointN( j ).coordinate() ); - - if ( j != 0 ) { - cdt.addConstraint( vh, v_prev ); - } - + cdt.addConstraint( v_prev, vh ); v_prev = vh; } + cdt.addConstraint( v_prev, v_0 ); } /*