GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
PolyhedronCore.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Damien Andre IRCER/UNILIM, Limoges France
4 // <damien.andre@unilim.fr>
5 // - Jean-luc Charles Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
6 // <jean-luc.charles@ensam.eu>
7 // - Jeremie Girardot Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UPHF, Valenciennes France
10 // <cedric.hubert@uphf.fr>
11 // - Ivan Iordanoff Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2019 D. Andre, JL. Charles, J. Girardot, C. Hubert, I. Iordanoff
15 //
16 // This program is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 
29 #ifndef PolyhedronCore_hpp
30 #define PolyhedronCore_hpp
31 
32 #include <vector> // std::vector
33 
35 
36 class vtkUnstructuredGrid;
37 
39 
40 public:
41  typedef std::pair<unsigned int, unsigned int> VertexPrimitivePair;
42 
43 public:
51 
59  PolyhedronCore(std::string filePath);
60 
68  PolyhedronCore(std::stringstream &stringStream);
69 
74 
75 public:
82  void read_off_file(std::string filePath);
83 
90  void read_off_file(std::stringstream &stringStream);
91 
99  void to_off_format(std::stringstream &offString, bool basefacet = true) const;
100 
107  void to_vtk_ugrid(vtkUnstructuredGrid &uGrid);
108 
115  void clear();
116 
123  void clear_metric();
124 
132  void build_data_structure();
133 
139  void compute_metric();
140 
147  void build_aabb_tree();
148 
152  inline unsigned int vertex_number() const {
153  return (unsigned int)_vertices.size()/3;
154  };
155 
156 
160  inline std::vector<double>& get_hitPoint() {
161  return _hitPoints;
162  };
163 
164 
168  inline unsigned int basefacet_number() const {
169  return (unsigned int)_basefacetToVertexOffset.size();
170  };
171 
175  inline unsigned int facet_number() const {
176  return (unsigned int)_facetToVertex.size()/3;
177  };
178 
182  inline unsigned int edge_number() const {
183  return (unsigned int)_edgeToFacet.size()/3;
184  };
185 
189  inline bool is_watertight() const {
190  return _isWatertight;
191  };
192 
196  inline const double *min_corner_ptr() const {
197  return _minCorner;
198  };
199 
203  inline const double *max_corner_ptr() const {
204  return _maxCorner;
205  };
206 
210  inline bool is_convex() const {
211  return _isConvex;
212  };
213 
217  inline double volume() const {
218  return _volume;
219  };
220 
224  inline const double *initial_centroid_ptr() const {
225  return _initialCentroid;
226  };
227 
231  inline const double *centroid_ptr() const {
232  return _centroid;
233  };
234 
238  inline void inertia_tensor(double (&tensor)[3][3]) const {
239 
240  for (unsigned int ii = 0; ii < 3; ii++)
241  for (unsigned int jj = 0; jj < 3; jj++)
242  tensor[ii][jj] = _iMatrix[ii][jj];
243  };
244 
248  inline unsigned int vertex_number_of_basefacet(int basefacetIndex) const {
249  return _verticesPerBaseFacet[basefacetIndex];
250  };
251 
258  inline const unsigned int vertex_index_of_basefacet(int basefacetIndex, int vertexIndex) const {
259  return _basefacetToVertex[_basefacetToVertexOffset[basefacetIndex]+vertexIndex];
260  };
261 
268  inline const unsigned int vertex_index_of_facet(int facetIndex, int vertexIndex) const {
269  return _facetToVertex[3*facetIndex+vertexIndex];
270  };
271 
277  inline const double *vertex_ptr_of_facet(unsigned int facetIndex, unsigned int vectorIndex) const {
278  return &(_facetsVectors[6*facetIndex+3*vectorIndex]);
279  };
280 
286  inline const double *normal_ptr_of_facet(int facetIndex) const {
287  return &(_facetsNormals[3*facetIndex]);
288  };
289 
295  inline const double *vertex_ptr(int vertexIndex) const {
296  return &(_vertices[3*vertexIndex]);
297  };
298 
304 // inline double *vertex_ptrRef(int vertexIndex) { return &(_vertices[3*vertexIndex]); };
305 
311  inline const double *normal_ptr_of_vertex(int vertexIndex) const {
312  return &(_verticesNormals[3*vertexIndex]);
313  };
314 
323  inline unsigned int basefacet_index_for_facet(unsigned int facetIndex) const {
324  return _facetToBaseFacet[facetIndex];
325  };
326 
334  double basefacet_area(unsigned int facetIndex) const;
335 
339  double facet_area(unsigned int facetIndex) const;
340 
346  void apply_offset(double offsetX, double offsetY, double offsetZ);
347 
353  void translate_to_centroid();
354 
361  void apply_scale_factor(double factor, double yFactor, double zFactor);
362 
375  bool contain(double aPoint[3]) const;
376 
385  std::vector<unsigned int> facet_hit_by_ray(Ray aRay) const;
386 
395  const double *closest_vertex_ptr(double aPoint[3]) const;
396 
409  VertexPrimitivePair closest_vertex_and_primitive(double aPoint[3]) const;
410 
420  void closest_point(double aPoint[3], double (&closestPoint)[3]) const;
421 
432  void furthest_vertex_along_axis(const double *anAxis, unsigned int &furthestVertex) const;
433 
443  void closest_vertex_callback(unsigned int aFacet, const double *aPoint, double &distance) const;
444 
452  bool raycast_aabb_tree_callback(unsigned int aFacet, Ray aRay) const;
453 
466  void closest_vertex_and_primitive_callback(unsigned int aFacet, const double *aPoint, double &distance) const;
467 
477  void closest_point_callback(unsigned int aFacet, const double *aPoint, double &distance) const;
478 
488  void furthest_vertex_callback(unsigned int aFacet, const double *aPoint, const double *anAxis, double &distance) const;
489 
497  bool is_facet_crossed_by_ray(unsigned int aFacet, Ray aRay, double (&hitPoint)[3]) const;
498 
508  void closest_point_in_facet(unsigned int aFacet, const double *aPoint, double (&closestPoint)[3]) const;
509 
510  const std::vector<unsigned int>& facet_to_vertex() const {return _facetToVertex;}
511  std::vector<unsigned int>& facet_to_vertex() {return _facetToVertex;}
512 
513 private:
521  unsigned int find_local_edge_index_of_facet(unsigned int aFacet, unsigned int anEdge);
522 
523  double closest_point_to_edge(const double *point0, const double *point1, const double *, double (&closestPoint)[3]) const;
524 
525 private:
530 
538  static unsigned char FacetEdgeVertex[3][2];
539 
548  static unsigned char FacetEdgeDirection[3][3];
549 
553  static const unsigned int NotInList = (unsigned int)(-1);
554 
555 private:
559  std::vector<double> _vertices;
560 
566  std::vector<unsigned int> _basefacetToVertex;
567 
571  std::vector<unsigned int> _basefacetToVertexOffset;
572 
576  std::vector<unsigned int> _verticesPerBaseFacet;
577 
581  std::vector<unsigned int> _facetToVertex;
582 
586  std::vector<unsigned int> _basefacetToFacetOffset;
587 
591  std::vector<unsigned int> _facetToBaseFacet;
592 
599  std::vector<unsigned int> _facetToFacet;
600 
606  std::vector<unsigned int> _facetToEdge;
607 
613  std::vector<unsigned int> _edgeToVertex;
614 
623  std::vector<unsigned int> _edgeToFacet;
624 
630  std::vector<unsigned int> _nonManifoldEdges;
631 
637  std::vector<double> _facetsVectors;
638 
644  std::vector<double> _facetsNormals;
645 
651  std::vector<double> _verticesNormals;
652 
660 
664  double _minCorner[3];
665 
669  double _maxCorner[3];
670 
675 
682 
686  bool _isConvex;
687 
691  double _volume;
692 
696  double _initialCentroid[3];
697 
701  double _centroid[3];
702 
706  double _iMatrix[3][3];
707 
708 private:
713  mutable unsigned int _closestVertexIndex;
714 
719  mutable unsigned int _primitivesHitByRayCount;
720 
724  mutable double _closestPoint[3];
725 
729  mutable unsigned int _furthestVertex;
730 
735  mutable std::vector<double> _hitPoints;
736 
741  mutable std::vector<unsigned int> _hitPrimitives;
742 
751 
757  mutable std::vector<unsigned int> _candidateFacets;
758 };
759 
760 #endif /* Polyhedron_hpp */
Definition: PolyhedronCore.hpp:38
void apply_offset(double offsetX, double offsetY, double offsetZ)
Applies an offset to all vertices coordinates of the polyhedron.
Definition: PolyhedronCore.cpp:695
const double * normal_ptr_of_facet(int facetIndex) const
Allows to get the normal of a given facet.
Definition: PolyhedronCore.hpp:286
std::vector< unsigned int > _nonManifoldEdges
The list of the non-manifold edges.
Definition: PolyhedronCore.hpp:630
double closest_point_to_edge(const double *point0, const double *point1, const double *, double(&closestPoint)[3]) const
Definition: PolyhedronCore.cpp:1298
std::vector< unsigned int > _candidateFacets
A vector containing candidate facets during the closest_pointAndPrimitive() procedure....
Definition: PolyhedronCore.hpp:757
std::vector< unsigned int > _hitPrimitives
The list primitives hit by a ray during a raycast on the AABB tree, set by the raycast_aabb_tree_call...
Definition: PolyhedronCore.hpp:741
void build_aabb_tree()
Builds a static AABB tree based on the polyhedron primitives.
Definition: PolyhedronCore.cpp:634
double _initialCentroid[3]
The initial centroid of the polyhedron, before any transformation.
Definition: PolyhedronCore.hpp:696
double volume() const
Definition: PolyhedronCore.hpp:217
static double SquaredDistanceErrorThreshold
Small value used to detect squared distance mismatch between two points.
Definition: PolyhedronCore.hpp:529
std::vector< unsigned int > _facetToVertex
The relationship between triangle facets and vertices.
Definition: PolyhedronCore.hpp:581
void apply_scale_factor(double factor, double yFactor, double zFactor)
Applies a scale factor to all vertices coordinates of the polyhedron.
Definition: PolyhedronCore.cpp:728
std::vector< unsigned int > _facetToEdge
The facet to edges connectivity list.
Definition: PolyhedronCore.hpp:606
void furthest_vertex_along_axis(const double *anAxis, unsigned int &furthestVertex) const
Finds the vertex in the polyhedron which is the furthest in the given direction.
Definition: PolyhedronCore.cpp:882
const double * centroid_ptr() const
Definition: PolyhedronCore.hpp:231
std::vector< double > _verticesNormals
The normals of each vertex in the polyhedron.
Definition: PolyhedronCore.hpp:651
const double * vertex_ptr_of_facet(unsigned int facetIndex, unsigned int vectorIndex) const
Allows to get the in-plane vectors of a given facet.
Definition: PolyhedronCore.hpp:277
bool is_convex() const
Definition: PolyhedronCore.hpp:210
const std::vector< unsigned int > & facet_to_vertex() const
Definition: PolyhedronCore.hpp:510
const unsigned int vertex_index_of_facet(int facetIndex, int vertexIndex) const
Allows to get a the global vertex index for a given facet, with a given local vertex index.
Definition: PolyhedronCore.hpp:268
unsigned int _furthestVertex
contains the furthest vertex found during a call to furthest_vertex_along_axis() method.
Definition: PolyhedronCore.hpp:729
void closest_point_callback(unsigned int aFacet, const double *aPoint, double &distance) const
Finds the point in a polyhedron facets which is the closest to the requested point.
Definition: PolyhedronCore.cpp:952
double facet_area(unsigned int facetIndex) const
Computes the area of a triangle facet with index facetIndex.
Definition: PolyhedronCore.cpp:679
unsigned int find_local_edge_index_of_facet(unsigned int aFacet, unsigned int anEdge)
Finds the local index of an edge of a given global facet.
Definition: PolyhedronCore.cpp:1332
std::pair< unsigned int, unsigned int > VertexPrimitivePair
Definition: PolyhedronCore.hpp:41
void clear_metric()
Clears the all computations performed on the polyhedron.
Definition: PolyhedronCore.cpp:104
std::vector< double > _vertices
The list of vertices coordinates, given as [x0,y0,z0,...,xn,yn,zn].
Definition: PolyhedronCore.hpp:559
double _iMatrix[3][3]
The inertia matrix of the polyhedron.
Definition: PolyhedronCore.hpp:706
PolyhedronCore()
instanciates a new PolyhedronCore object
Definition: PolyhedronCore.cpp:54
std::vector< double > _hitPoints
The list of points coordinates on primitives hit by a ray during raycast on the AABB tree,...
Definition: PolyhedronCore.hpp:735
std::vector< unsigned int > _facetToFacet
The facet to facets connectivity list.
Definition: PolyhedronCore.hpp:599
unsigned int _closestVertexIndex
The index of the closest vertex found during a closest point query on the AABB tree,...
Definition: PolyhedronCore.hpp:713
std::vector< unsigned int > & facet_to_vertex()
Definition: PolyhedronCore.hpp:511
void to_vtk_ugrid(vtkUnstructuredGrid &uGrid)
Creates a VTK unstructured grid with the polyhedron contents.
Definition: PolyhedronCore.cpp:297
std::vector< unsigned int > _verticesPerBaseFacet
The number of vertices that form a base facet.
Definition: PolyhedronCore.hpp:576
std::vector< double > _facetsVectors
The in-plane vectors of each facet in the polyhedron.
Definition: PolyhedronCore.hpp:637
const double * vertex_ptr(int vertexIndex) const
Allows to get a vertex coordinates array from its index in the _vertices vector.
Definition: PolyhedronCore.hpp:295
unsigned int basefacet_number() const
Definition: PolyhedronCore.hpp:168
double _minCorner[3]
The min corner of the polyhedron bounding box.
Definition: PolyhedronCore.hpp:664
void build_data_structure()
Builds the data structure which can be used to perform queries, e.g. adjacency.
Definition: PolyhedronCore.cpp:322
double _closestPoint[3]
contains the closest point found during a call to closest_point() method.
Definition: PolyhedronCore.hpp:724
std::vector< unsigned int > _edgeToVertex
The edge to vertices connectivity list.
Definition: PolyhedronCore.hpp:613
void compute_metric()
Computes geometric parameters related to the polyhedron.
Definition: PolyhedronCore.cpp:415
VertexPrimitivePair _closestVertexAndPrimitiveIndices
The index of the closest primitive associated to the closest vertex found during a closest and primit...
Definition: PolyhedronCore.hpp:750
void furthest_vertex_callback(unsigned int aFacet, const double *aPoint, const double *anAxis, double &distance) const
Finds the vertex in a polyhedron facets which is the furthest, in the requested direction.
Definition: PolyhedronCore.cpp:974
void clear()
Clears the geometric entities of the PolyhedronCore object.
Definition: PolyhedronCore.cpp:88
StaticAABBTree _aabbTree
The Axis Aligned Bounding Box tree built from the facets of the polyhedron.
Definition: PolyhedronCore.hpp:681
const unsigned int vertex_index_of_basefacet(int basefacetIndex, int vertexIndex) const
Allows to get a the global vertex index for a given base facet, with a given local vertex index.
Definition: PolyhedronCore.hpp:258
unsigned int vertex_number() const
Definition: PolyhedronCore.hpp:152
unsigned int basefacet_index_for_facet(unsigned int facetIndex) const
Returns the base facet index for a triangle facet.
Definition: PolyhedronCore.hpp:323
std::vector< unsigned int > _facetToBaseFacet
The relationship between a triangle facet to its base facet.
Definition: PolyhedronCore.hpp:591
const double * closest_vertex_ptr(double aPoint[3]) const
Finds the vertex in the polyhedron which is the closest to the query point.
Definition: PolyhedronCore.cpp:801
bool raycast_aabb_tree_callback(unsigned int aFacet, Ray aRay) const
Checks if a primitive is hit by a ray.
Definition: PolyhedronCore.cpp:1019
const double * initial_centroid_ptr() const
Definition: PolyhedronCore.hpp:224
double _volume
The volume of the tetrahedron.
Definition: PolyhedronCore.hpp:691
void inertia_tensor(double(&tensor)[3][3]) const
update the tensor to the inertia operator of the polyhedron.
Definition: PolyhedronCore.hpp:238
std::vector< double > & get_hitPoint()
Definition: PolyhedronCore.hpp:160
bool _isWatertight
A flag that say if the polyhedron is watertight or not.
Definition: PolyhedronCore.hpp:659
std::vector< unsigned int > _basefacetToVertexOffset
stores the offset required to access the first vertex of a base facet in _basefacetToVertex vector.
Definition: PolyhedronCore.hpp:571
void closest_vertex_and_primitive_callback(unsigned int aFacet, const double *aPoint, double &distance) const
Finds the vertex in a polyhedron facet and the associated facet which realizes the shortest distance ...
Definition: PolyhedronCore.cpp:934
void closest_point(double aPoint[3], double(&closestPoint)[3]) const
Finds the point in the polyhedron which is the closest to the query point.
Definition: PolyhedronCore.cpp:862
static unsigned char FacetEdgeDirection[3][3]
contains flags that allow to know if an edge is oriented towards a facet. or its incident counterpart...
Definition: PolyhedronCore.hpp:548
static const unsigned int NotInList
A flag to determine if an index is in a list.
Definition: PolyhedronCore.hpp:553
std::vector< double > _facetsNormals
The normals of each facet in the polyhedron.
Definition: PolyhedronCore.hpp:644
double _centroid[3]
The centroid of the polyhedron.
Definition: PolyhedronCore.hpp:701
const double * min_corner_ptr() const
Definition: PolyhedronCore.hpp:196
std::vector< unsigned int > _basefacetToFacetOffset
stores the offset required to access the first triangle facet of a base facet in _facetToBaseFacet ve...
Definition: PolyhedronCore.hpp:586
bool _isConvex
The convexity of the polyhedron.
Definition: PolyhedronCore.hpp:686
unsigned int facet_number() const
Definition: PolyhedronCore.hpp:175
unsigned int edge_number() const
Definition: PolyhedronCore.hpp:182
bool contain(double aPoint[3]) const
Checks if the polyhedron contains a 3D point.
Definition: PolyhedronCore.cpp:751
static unsigned char FacetEdgeVertex[3][2]
contains the local connectivity of a facet vertices by edge.
Definition: PolyhedronCore.hpp:538
const double * normal_ptr_of_vertex(int vertexIndex) const
Allows to get a refernce to a vertex coordinates array from its index in the _vertices vector.
Definition: PolyhedronCore.hpp:311
std::vector< unsigned int > _basefacetToVertex
The relationship between base facets (or raw facets, directly from the geometry file that is,...
Definition: PolyhedronCore.hpp:566
void translate_to_centroid()
translates the polyhedron to its center of mass.
Definition: PolyhedronCore.cpp:718
double _maxCorner[3]
The max corner of the polyhedron bounding box.
Definition: PolyhedronCore.hpp:669
~PolyhedronCore()
Default PolyhedronCore destructor.
Definition: PolyhedronCore.cpp:81
bool is_facet_crossed_by_ray(unsigned int aFacet, Ray aRay, double(&hitPoint)[3]) const
Checks if a facet is crossed by a given ray.
Definition: PolyhedronCore.cpp:1053
VertexPrimitivePair closest_vertex_and_primitive(double aPoint[3]) const
Finds the vertex and the primitive it is associated to in the polyhedron which are the closest to the...
Definition: PolyhedronCore.cpp:821
unsigned int _primitivesHitByRayCount
The number of primitives hit by a ray during a intersections count query on the AABB tree,...
Definition: PolyhedronCore.hpp:719
double _greatestSquaredDiagonal
Greatest squared diagonal used to normalize errors.
Definition: PolyhedronCore.hpp:674
bool is_watertight() const
Definition: PolyhedronCore.hpp:189
unsigned int vertex_number_of_basefacet(int basefacetIndex) const
Definition: PolyhedronCore.hpp:248
void closest_point_in_facet(unsigned int aFacet, const double *aPoint, double(&closestPoint)[3]) const
Finds the closest point lying on a facet closest to aPoint.
Definition: PolyhedronCore.cpp:1146
const double * max_corner_ptr() const
Definition: PolyhedronCore.hpp:203
std::vector< unsigned int > facet_hit_by_ray(Ray aRay) const
get_s the list of facets hit by a ray.
Definition: PolyhedronCore.cpp:779
void closest_vertex_callback(unsigned int aFacet, const double *aPoint, double &distance) const
Finds the vertex in a polyhedron facets which is the closest to the requested point.
Definition: PolyhedronCore.cpp:917
std::vector< unsigned int > _edgeToFacet
The edge to facets connectivity list.
Definition: PolyhedronCore.hpp:623
void to_off_format(std::stringstream &offString, bool basefacet=true) const
Creates an std::stringstream with the polyhedron contents, in OFF file format.
Definition: PolyhedronCore.cpp:256
double basefacet_area(unsigned int facetIndex) const
Computes the area of a base facet.
Definition: PolyhedronCore.cpp:663
void read_off_file(std::string filePath)
Reads an OFF file given a file path.
Definition: PolyhedronCore.cpp:121
Definition: StaticAABBTree.hpp:79
Definition: StaticAABBTree.hpp:71