Code_TYMPAN  4.4.0
Industrial site acoustic simulation
cgal_bridge.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 #include "cgal_bridge.h"
17 
18 #include <boost/foreach.hpp>
19 
23 
24 namespace tympan
25 {
26 
28 {
29 public:
30  TYPolygonTriangulator(const TYPolygon& poly_);
31 
32  virtual void exportMesh(std::deque<OPoint3D>& points, std::deque<OTriangle>& triangles) const;
33 
34  const TYPolygon& poly;
35 }; // class TYPolygonTriangulator
36 
38 
39 void TYPolygonTriangulator::exportMesh(std::deque<OPoint3D>& points, std::deque<OTriangle>& triangles) const
40 {
41 
42  assert(points.size() == 0 && "Output arguments 'points' is expected to be initially empty");
43  assert(triangles.size() == 0 && "Output arguments 'triangles' is expected to be initially empty");
44 
45  TYPolygon checked_poly(poly);
46  checked_poly.updateNormal(); // This actually updates the associated plane
47  if (!checked_poly.checkCoplanar())
48  {
49  std::deque<LPTYElement> elements;
50  LPTYElement element(new TYPolygon(poly));
51  elements.push_back(element);
52  throw tympan::invalid_data("Polygon is not planar.")
54  }
55  // NB The triangulation happen in the local r/ frame
56  // We build a polygon in the plane (aka 2D) so as to be able to triangulate it
57  CGAL_Plane plane(to_cgal(checked_poly.getPlan()));
58  CGAL_Polygon poly2d;
59  BOOST_FOREACH (const OPoint3D& op, checked_poly.getPoints())
60  {
61  // Projection of `op` onto `plane`
62  const CGAL_Point3 pp3d = plane.projection(to_cgal(op));
63  // Conversion into a 2D point
64  const CGAL_Point2 pp2d = plane.to_2d(pp3d);
65  poly2d.push_back(pp2d);
66  points.push_back(op);
67  }
68 
69  if (!poly2d.is_simple())
70  {
71  std::deque<LPTYElement> elements;
72  LPTYElement element(new TYPolygon(poly));
73  elements.push_back(element);
74  throw tympan::invalid_data("Face can not be triangulated (invalid plane?).")
76  }
77  // Actual triangulation done by CGAL
78  PolygonTriangulator triangulator(poly2d);
79  // Use information from triangulator.vertice_handles and the triangulation itself.
80  // to level-up from 2D CGAL representation to our 3D OPoints world
81  std::deque<PolygonTriangulator::Tri_indices> tri_indices;
82  triangulator.exportTrianglesIndices(tri_indices);
83  BOOST_FOREACH (const auto& tri_idx, tri_indices)
84  {
85  OTriangle tri(tri_idx[0], tri_idx[1], tri_idx[2]);
86  for (unsigned i = 0; i < 3; ++i)
87  {
88  const OPoint3D& p = points[tri_idx[i]];
89  tri.vertex(i) = p;
90  }
91  triangles.push_back(tri);
92  assert(triangles.back().checkConsistencyWrtPointsTab(points));
93  }
94 } // void TYPolygonTriangulator::exportMesh(...)
95 
96 std::unique_ptr<ITYPolygonTriangulator> make_polygon_triangulator(const TYPolygon& poly)
97 {
98  return std::unique_ptr<ITYPolygonTriangulator>(new TYPolygonTriangulator(poly));
99 } // ITYPolygonTriangulator* make_polygon_triangulator()
100 } // namespace tympan
Bridges TY* types with CGAL functionalities exposed by.
Utilities to ease (and wrap) use of CGAL.
The 3D point class.
Definition: 3d.h:487
Triangle class.
Definition: triangle.h:28
OPoint3D & vertex(unsigned i)
Get the OPoint3D from the specific index.
Definition: triangle.cpp:26
const TYTabPoint & getPoints() const
Definition: TYPolygon.h:123
bool checkCoplanar() const
Definition: TYPolygon.cpp:399
void updateNormal()
Definition: TYPolygon.cpp:523
virtual const OPlan & getPlan() const
Definition: TYPolygon.h:205
This class provides triangulating simple polygons without holes.
Definition: cgal_tools.h:119
void exportTrianglesIndices(std::deque< Tri_indices > &triangles) const
Exports the triangles inside the polygon.
Definition: cgal_tools.cpp:168
virtual void exportMesh(std::deque< OPoint3D > &points, std::deque< OTriangle > &triangles) const
Export the surface as a triangular mesh.
Definition: cgal_bridge.cpp:39
TYPolygonTriangulator(const TYPolygon &poly_)
Definition: cgal_bridge.cpp:37
#define tympan_source_loc
This macro build a source_loc object to be attached to a tympan::Exception.
Definition: exceptions.h:76
Define some exception related utilities relying on DataManagerMetier classes.
boost::error_info< struct tag_elements_implied, std::deque< LPTYElement > > elements_implied_errinfo
Definition: exceptions.h:15
CGAL_Plane to_cgal(const OPlan &oplan)
Convert a OPlan to CGAL_Plane.
Definition: cgal_tools.cpp:19
CGAL::Point_3< CGAL_Gt > CGAL_Point3
Definition: cgal_tools.h:45
CGAL::Point_2< CGAL_Gt > CGAL_Point2
Definition: cgal_tools.h:44
CGAL::Plane_3< CGAL_Gt > CGAL_Plane
Definition: cgal_tools.h:49
CGAL::Polygon_2< CGAL_Gt > CGAL_Polygon
Definition: cgal_tools.h:48
std::unique_ptr< ITYPolygonTriangulator > make_polygon_triangulator(const TYPolygon &poly)
Definition: cgal_bridge.cpp:96
The base exception class for errors due to invalid data.
Definition: exceptions.h:60