1 /**
2 * SFCGAL
3 *
4 * Copyright (C) 2012-2013 Oslandia <infos@oslandia.com>
5 * Copyright (C) 2012-2013 IGN (http://www.ign.fr)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 */
20 #include <boost/test/unit_test.hpp>
21
22 #include <SFCGAL/Kernel.h>
23 #include <SFCGAL/Point.h>
24 #include <SFCGAL/LineString.h>
25 #include <SFCGAL/Polygon.h>
26 #include <SFCGAL/Triangle.h>
27 #include <SFCGAL/PolyhedralSurface.h>
28 #include <SFCGAL/TriangulatedSurface.h>
29 #include <SFCGAL/Solid.h>
30 #include <SFCGAL/GeometryCollection.h>
31 #include <SFCGAL/MultiPoint.h>
32 #include <SFCGAL/MultiLineString.h>
33 #include <SFCGAL/MultiPolygon.h>
34 #include <SFCGAL/MultiSolid.h>
35 #include <SFCGAL/io/wkt.h>
36 #include <SFCGAL/algorithm/normal.h>
37
38 using namespace SFCGAL ;
39 using namespace boost::unit_test ;
40
41 BOOST_AUTO_TEST_SUITE( SFCGAL_algorithm_NormalTest )
42
BOOST_AUTO_TEST_CASE(testNormal1)43 BOOST_AUTO_TEST_CASE( testNormal1 )
44 {
45 typedef CGAL::Vector_3< Kernel > Vector_3 ;
46 typedef CGAL::Point_3< Kernel > Point_3 ;
47
48 Point_3 a( 0.0, 0.0, 0.0 );
49 Point_3 b( 1.0, 0.0, 0.0 );
50 Point_3 c( 1.0, 1.0, 0.0 );
51
52 Vector_3 normal = algorithm::normal3D( a, b, c );
53 BOOST_CHECK_EQUAL( normal.x(), 0.0 );
54 BOOST_CHECK_EQUAL( normal.y(), 0.0 );
55 BOOST_CHECK_EQUAL( normal.z(), 1.0 );
56 }
57
BOOST_AUTO_TEST_CASE(testNormal2)58 BOOST_AUTO_TEST_CASE( testNormal2 )
59 {
60 // a square ccw
61 std::unique_ptr<Geometry> gA( io::readWkt( "POLYGON((0 0,1 0,1 1,0 1,0 0))" ) );
62 // a square cw oriented
63 std::unique_ptr<Geometry> gB( io::readWkt( "POLYGON((0 0,0 1,1 1,1 0,0 0))" ) );
64
65 // a pseudo-square ccw oriented, with a concave part
66 std::unique_ptr<Geometry> gC( io::readWkt( "POLYGON((0 0,0.5 0.5,1 0,1 1,0 1,0 0))" ) );
67
68 {
69 CGAL::Vector_3<Kernel> normal = algorithm::normal3D<Kernel>( gA->as<Polygon>() );
70 BOOST_CHECK_EQUAL( normal.x(), 0.0 );
71 BOOST_CHECK_EQUAL( normal.y(), 0.0 );
72 BOOST_CHECK_EQUAL( normal.z(), 2.0 );
73 }
74
75 {
76 CGAL::Vector_3<Kernel> normal = algorithm::normal3D<Kernel>( gB->as<Polygon>() );
77 BOOST_CHECK_EQUAL( normal.x(), 0.0 );
78 BOOST_CHECK_EQUAL( normal.y(), 0.0 );
79 BOOST_CHECK_EQUAL( normal.z(), -2.0 );
80 }
81
82 {
83 CGAL::Vector_3<Kernel> normal = algorithm::normal3D<Kernel>( gC->as<Polygon>() );
84 BOOST_CHECK_EQUAL( normal.x(), 0.0 );
85 BOOST_CHECK_EQUAL( normal.y(), 0.0 );
86 // ok, the normal is pointing up (z > 0)
87 BOOST_CHECK_EQUAL( normal.z(), 1.5 );
88 }
89 }
90
91
BOOST_AUTO_TEST_CASE(testNormal3)92 BOOST_AUTO_TEST_CASE( testNormal3 )
93 {
94 std::unique_ptr<Geometry> gA( io::readWkt( "POLYGON((0 1 0,0 1 1,1 1 1,1 1 0,0 1 0))" ) );
95 // exact
96 {
97 CGAL::Vector_3<Kernel> normal = algorithm::normal3D<Kernel>( gA->as<Polygon>(), true );
98 //std::cout << CGAL::exact(normal) << std::endl;
99 CGAL::Plane_3<Kernel> plane( gA->as<Polygon>().exteriorRing().startPoint().toPoint_3(), normal );
100 //std::cout << CGAL::exact(plane) << std::endl;
101 BOOST_CHECK( ! plane.is_degenerate() );
102 }
103 // round
104 {
105 CGAL::Vector_3<Kernel> normal = algorithm::normal3D<Kernel>( gA->as<Polygon>(), false );
106 //std::cout << CGAL::exact(normal) << std::endl;
107 CGAL::Plane_3<Kernel> plane( gA->as<Polygon>().exteriorRing().startPoint().toPoint_3(), normal );
108 //std::cout << CGAL::exact(plane) << std::endl;
109 BOOST_CHECK( ! plane.is_degenerate() );
110 }
111
112 }
113
114
115
116 BOOST_AUTO_TEST_SUITE_END()
117
118