Bayeux  3.4.1
Core Foundation library for SuperNEMO
tessellation.h
Go to the documentation of this file.
1 /* Author(s): Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date: 2010-06-04
4  * Last modified: 2015-03-19
5  *
6  * License:
7  *
8  * Description:
9  *
10  * History:
11  *
12  */
13 
14 #ifndef GEOMTOOLS_TESSELLATION_H
15 #define GEOMTOOLS_TESSELLATION_H 1
16 
17 // Standard library:
18 #include <iostream>
19 #include <string>
20 #include <map>
21 #include <vector>
22 
23 // Third party:
24 // - Boost:
25 #include <boost/cstdint.hpp>
26 // - Bayeux/mygsl:
27 #include <mygsl/min_max.h>
28 
29 // This project:
30 #include <geomtools/i_shape_2d.h>
31 #include <geomtools/i_shape_3d.h>
32 
33 namespace geomtools {
34 
35  // Forward declarations:
36  class facet34;
37  class box;
38  class triangle;
39  class quadrangle;
40 
42  struct facet_vertex
43  {
44 
45  public:
46 
48  bool is_valid() const;
49 
51  facet_vertex();
52 
54  facet_vertex(double x_, double y_, double z_);
55 
57  void print_xyz(std::ostream & out_, int color_ = 0) const;
58 
60  void print(std::ostream & out_) const;
61 
63  void add_ref_facet(int facet_index_, int facet_node_);
64 
66  const vector_3d & get_position() const;
67 
69  void set_position(const vector_3d &);
70 
73 
75  void reset();
76 
78  friend std::ostream & operator<<(std::ostream &, const facet_vertex &);
79 
80  public:
81 
83  std::map<int32_t, int32_t> ref_facets;
84 
90  };
91 
93  class facet34
94  {
95 
96  public:
97 
99  static const unsigned int MAX_CATEGORY = 30;
100  static const int32_t INVALID_CATEGORY = -1;
101 
102  bool has_category() const;
103 
104  void set_category(unsigned int c_);
105 
106  void unset_category();
107 
108  uint32_t get_number_of_vertices() const;
109 
110  const facet_vertex & get_vertex(int i_) const;
111 
112  int32_t get_vertex_key(int i_) const;
113 
114  bool has_normal() const;
115 
116  bool has_surface() const;
117 
118  bool is_valid() const;
119 
120  const vector_3d & get_normal() const;
121 
122  double get_surface() const;
123 
124  double get_internal_angle(int i_) const;
125 
126  void print(std::ostream & out_) const;
127 
128  static bool check_triangle(const geomtools::vector_3d & v0_,
129  const geomtools::vector_3d & v1_,
130  const geomtools::vector_3d & v2_,
131  double tolerance_ = 0.0);
132 
133  static bool check_quadrangle(const geomtools::vector_3d & v0_,
134  const geomtools::vector_3d & v1_,
135  const geomtools::vector_3d & v2_,
136  const geomtools::vector_3d & v3_,
137  double tolerance_ = 0.0);
138 
139  bool is_triangle() const;
140 
141  bool is_quadrangle() const;
142 
143  void set_triangle(const facet_vertex & v0_,
144  const facet_vertex & v1_,
145  const facet_vertex & v2_,
146  int iv0_,
147  int iv1_,
148  int iv2_);
149 
150  void set_quadrangle(const facet_vertex & v0_,
151  const facet_vertex & v1_,
152  const facet_vertex & v2_,
153  const facet_vertex & v3_,
154  int iv0_,
155  int iv1_,
156  int iv2_,
157  int iv3_);
158 
160  facet34();
161 
163  facet34(const facet_vertex & v0_,
164  const facet_vertex & v1_,
165  const facet_vertex & v2_,
166  int, int, int);
167 
169  facet34(const facet_vertex & v0_,
170  const facet_vertex & v1_,
171  const facet_vertex & v2_,
172  const facet_vertex & v3_,
173  int, int, int, int);
174 
176  bool has_face() const;
177 
179  bool has_tface() const;
180 
182  bool has_qface() const;
183 
185  const triangle & get_tface() const;
186 
188  const quadrangle & get_qface() const;
189 
190  const face_handle_type & get_tface_handle() const;
191 
193 
194  const face_handle_type & get_qface_handle() const;
195 
197 
198  protected:
199 
201  void _set_defaults();
202 
203  void _compute_normal();
204 
205  void _compute_surface();
206 
207  void _compute_internal_angles(); // Not implemented yet
208 
209  void _compute_face();
210 
211  void _compute_internals();
212 
213  private:
214 
215  // Primary data:
216  uint32_t _number_of_vertices_;
217  const facet_vertex * _vertices_[4];
218  int32_t _vertices_keys_[4];
219 
220  // Working data:
221  geomtools::vector_3d _normal_;
222  double _surface_;
223  double _internal_angles_[4];
224  int32_t _category_;
225  face_handle_type _tface_;
226  face_handle_type _qface_;
227 
228  };
229 
232  {
233  public :
234 
240  };
241 
243  bool is_valid() const;
244 
246  bool is_shown() const;
247 
249  void set_shown(bool);
250 
252  facet_segment();
253 
255  void set_vertex_keys(int, int);
256 
258  void set_vertexes(const facet_vertex & vtx0_, const facet_vertex & vtx1_);
259 
261  bool has_vertex(vertex_id_type) const;
262 
265 
267  void set_facet_keys(int, int);
268 
269  void set(int, int, int, int);
270 
271  friend bool operator==(const facet_segment & fs1_, const facet_segment & fs2_);
272 
273  friend bool operator<(const facet_segment & fs1_, const facet_segment & fs2_);
274 
276  void dump(std::ostream & out_) const;
277 
278  public :
279 
284 
285  private:
286 
287  bool _shown_;
288  const facet_vertex * _vertex0_;
289  const facet_vertex * _vertex1_;
290 
291  };
292 
295  public i_stackable
296  {
297  public:
298 
299  static const int INVALID_VERTEX = -1;
300  static const int MAX_VERTEX = 0x0FFFFFFF;
301  static const unsigned int INVALID_FACET_INDEX = 0xFFFFFFFF;
302 
304  static const std::string & tessellated_label();
305 
306  typedef std::map<unsigned int, facet_vertex> vertices_col_type;
307  typedef std::map<unsigned int, facet34> facets_col_type;
308  typedef std::map<unsigned int, facet_segment> facet_segments_col_type;
309 
311  virtual std::string get_shape_name() const;
312 
313  static bool validate_index(int);
314 
316  bool is_consistent() const;
317 
319  bool using_face_id_bits() const;
320 
322  bool using_face_id_index() const;
323 
326 
328  virtual ~tessellated_solid();
329 
331  const vertices_col_type & vertices() const;
332 
334  const facets_col_type & facets() const;
335 
337  const facet_segments_col_type & facet_segments() const;
338 
340 
342 
344 
345  int add_vertex(unsigned int vtx_key_, const facet_vertex & vtx_);
346 
347  int add_vertex(unsigned int vtx_key_,
348  double x_, double y_, double z_);
349 
350  void add_facet(unsigned int facet_key_,
351  int ivtx0_, int ivtx1_, int ivtx2_, int ivtx3_ = INVALID_VERTEX);
352 
353  void add_facet3(unsigned int facet_key_,
354  int ivtx0_, int ivtx1_, int ivtx2_);
355 
356  void add_facet4(unsigned int facet_key_,
357  int ivtx0_, int ivtx1_, int ivtx2_, int ivtx3_);
358 
359  void remove_facet(unsigned int facet_key_);
360 
361  bool has_facet(unsigned int facet_key_) const;
362 
363  const facet34 & get_facet(unsigned int facet_key_) const;
364 
365  void compute_bounding_box();
366 
367  const mygsl::min_max & get_bounding_box_x() const;
368 
369  const mygsl::min_max & get_bounding_box_y() const;
370 
371  const mygsl::min_max & get_bounding_box_z() const;
372 
374  double get_xmin() const;
375 
377  double get_xmax() const;
378 
380  double get_ymin() const;
381 
383  double get_ymax() const;
384 
386  double get_zmin() const;
387 
389  double get_zmax() const;
390 
392  void print_xyz(std::ostream & out_) const;
393 
395  void dump(std::ostream & out_ = std::clog) const;
396 
398  virtual bool is_inside (const vector_3d &,
399  double skin_ = GEOMTOOLS_PROPER_TOLERANCE) const;
400 
402  virtual bool is_outside (const vector_3d &,
403  double skin_ = GEOMTOOLS_PROPER_TOLERANCE) const;
404 
406  virtual face_identifier on_surface(const vector_3d &,
407  const face_identifier & a_surface_mask = face_identifier::FACE_INDEX_ANY,
408  double a_skin = GEOMTOOLS_PROPER_TOLERANCE) const;
409 
411  virtual vector_3d get_normal_on_surface (const vector_3d & position_,
412  const face_identifier & ) const;
413 
415  virtual bool find_intercept(const vector_3d & from_,
416  const vector_3d & direction_,
417  face_intercept_info & intercept_,
418  double skin_ = GEOMTOOLS_PROPER_TOLERANCE) const;
419 
421  virtual unsigned int compute_faces(face_info_collection_type &) const;
422 
428  )
429  };
430 
432  virtual void generate_wires_self(wires_type & wires_,
433  uint32_t options_ = 0) const;
434 
435 
437  virtual void initialize(const datatools::properties &, const handle_dict_type * = 0);
438 
440  virtual void reset();
441 
443  void initialize_from_stl(const std::string & filename_,
444  double length_unit_);
445 
447  bool is_valid() const;
448 
450  virtual void tree_dump(std::ostream & a_out = std::clog,
451  const std::string & a_title = "",
452  const std::string & a_indent = "",
453  bool a_inherit = false) const;
454 
455  protected:
456 
458  virtual void _at_lock();
459 
461  virtual void _at_unlock();
462 
464  virtual void _build_bounding_data();
465 
468 
470  bool _on_facet(const vector_3d & position_, unsigned int & facet_index_, double skin_) const;
471 
473  bool _find_intercept_exclude(const vector_3d & from_,
474  const vector_3d & direction_,
475  face_intercept_info & intercept_,
476  double skin_,
477  const std::set<unsigned int> & excluded_facet_indexes_) const;
478 
480  unsigned int _find_intercepts_exclude(const vector_3d & from_,
481  const vector_3d & direction_,
482  std::vector<face_intercept_info> & intercepts_,
483  double skin_,
484  const std::set<unsigned int> & excluded_facet_indexes_) const;
485 
486  private:
487 
489  bool _check_();
490 
491  private:
492 
493  bool _consistent_;
494  vertices_col_type _vertices_;
495  facets_col_type _facets_;
496  mygsl::min_max _xrange_;
497  mygsl::min_max _yrange_;
498  mygsl::min_max _zrange_;
499  facet_segments_col_type _facet_segments_;
500 
501  // Registration interface :
503 
504  };
505 
506 } // end of namespace geomtools
507 
508 #endif // GEOMTOOLS_TESSELLATION_H
509 
510 /*
511 ** Local Variables: --
512 ** mode: c++ --
513 ** c-file-style: "gnu" --
514 ** tab-width: 2 --
515 ** End: --
516 */
static const unsigned int MAX_CATEGORY
The maximum index of the category the facet belongs to.
Definition: tessellation.h:99
const face_handle_type & get_qface_handle() const
virtual unsigned int compute_faces(face_info_collection_type &) const
Compute informations about the faces of this solid shape.
virtual face_identifier on_surface(const vector_3d &, const face_identifier &a_surface_mask=face_identifier::FACE_INDEX_ANY, double a_skin=GEOMTOOLS_PROPER_TOLERANCE) const
Return the surface bit a point belongs to.
void print_xyz(std::ostream &out_) const
Print.
void set_facet_keys(int, int)
Set facet keys.
virtual void initialize(const datatools::properties &, const handle_dict_type *=0)
Initialize from properties.
const mygsl::min_max & get_bounding_box_x() const
facet_segment()
Default constructor.
virtual void _build_bounding_data()
Build the bounding data.
int facet0_key
Key of the first facet.
Definition: tessellation.h:282
int32_t get_vertex_key(int i_) const
vector_3d & grab_position()
Return the position of the vertex.
Mother abstract class for all 3D solid shapes.
Definition: i_shape_3d.h:46
void initialize_from_stl(const std::string &filename_, double length_unit_)
Initialize from STL file.
static bool check_triangle(const geomtools::vector_3d &v0_, const geomtools::vector_3d &v1_, const geomtools::vector_3d &v2_, double tolerance_=0.0)
void add_facet3(unsigned int facet_key_, int ivtx0_, int ivtx1_, int ivtx2_)
std::map< unsigned int, facet_segment > facet_segments_col_type
Definition: tessellation.h:308
facets_col_type & grab_facets()
void _compute_facet_segments()
Compute the collection of facet segments.
virtual void reset()
Reset.
virtual bool find_intercept(const vector_3d &from_, const vector_3d &direction_, face_intercept_info &intercept_, double skin_=GEOMTOOLS_PROPER_TOLERANCE) const
Find the intercept point with a face of the frustrum.
friend std::ostream & operator<<(std::ostream &, const facet_vertex &)
Print.
double get_zmin() const
Return the min Z coordinates (bounding box)
void dump(std::ostream &out_) const
Print.
virtual std::string get_shape_name() const
Return the identifier/name of the shape.
Definition: tessellation.h:237
Identifier of a face attached to a solid shape.
Definition: face_identifier.h:32
bool using_face_id_index() const
Check if the face identification scheme is based on face index.
Tessellated solid made of facets.
Definition: tessellation.h:294
void add_facet4(unsigned int facet_key_, int ivtx0_, int ivtx1_, int ivtx2_, int ivtx3_)
const mygsl::min_max & get_bounding_box_y() const
virtual void _at_unlock()
Executed at unlock stage.
Render all segments.
Definition: tessellation.h:425
const facets_col_type & facets() const
Return the collection of facets.
static bool validate_index(int)
double get_internal_angle(int i_) const
tessellated_solid()
Default constructor.
bool has_normal() const
virtual bool is_outside(const vector_3d &, double skin_=GEOMTOOLS_PROPER_TOLERANCE) const
Check if a point is outside the frustrum.
bool is_valid() const
Check validity.
A vertex (corner) of a triangle or quadrangle facet in a tessellated solid.
Definition: tessellation.h:42
Record the min and max values from a set of values.
Definition: min_max.h:24
double get_surface() const
bool _find_intercept_exclude(const vector_3d &from_, const vector_3d &direction_, face_intercept_info &intercept_, double skin_, const std::set< unsigned int > &excluded_facet_indexes_) const
Find the intercept point with a face of the solid.
vertex_id_type
The Id of the vertex.
Definition: tessellation.h:236
std::map< unsigned int, facet34 > facets_col_type
Definition: tessellation.h:307
double get_zmax() const
Return the max Z coordinates (bounding box)
bool is_valid() const
void print_xyz(std::ostream &out_, int color_=0) const
Print in 'x,y,z[,color]' format (ASCII)
void dump(std::ostream &out_=std::clog) const
Print.
The face_intercept_info class hosts the parameters of.
Definition: face_intercept_info.h:31
facet_vertex()
Default constructor.
void set_shown(bool)
Set the shown flag.
#define GEOMTOOLS_PROPER_TOLERANCE
Definition: geomtools_config.h:26
bool has_facet(unsigned int facet_key_) const
static const std::string & tessellated_label()
Return the identifier label for the class.
tessella_wires_rendering_option_type
3D rendering options
Definition: tessellation.h:424
friend bool operator==(const facet_segment &fs1_, const facet_segment &fs2_)
const facet_segments_col_type & facet_segments() const
Return the collection of facet segments.
Abstract base stackable object.
Definition: i_stackable.h:32
Last defined bit.
Definition: tessellation.h:426
const mygsl::min_max & get_bounding_box_z() const
void print(std::ostream &out_) const
void set_triangle(const facet_vertex &v0_, const facet_vertex &v1_, const facet_vertex &v2_, int iv0_, int iv1_, int iv2_)
int vertex1_key
Key of the second vertex.
Definition: tessellation.h:281
Templatized handle class that wraps a Boost shared pointer and behaves like a reference.
Definition: handle.h:114
void remove_facet(unsigned int facet_key_)
const vertices_col_type & vertices() const
Return the collection of vertexes.
virtual ~tessellated_solid()
Destructor.
Definition: tessellation.h:239
bool is_triangle() const
facet_segments_col_type & grab_facet_segments()
bool using_face_id_bits() const
Check if the face identification scheme is based on face bits.
A segment associated to a facet.
Definition: tessellation.h:231
virtual void generate_wires_self(wires_type &wires_, uint32_t options_=0) const
Generate a list of polylines representing the contour of the shape (for display clients)
bool has_surface() const
std::map< std::string, object_entry > handle_dict_type
Dictionary of handle of 3D object entries.
Definition: i_object_3d.h:124
const quadrangle & get_qface() const
Return the associated quadrangle face.
A triangle.
Definition: triangle.h:29
bool _on_facet(const vector_3d &position_, unsigned int &facet_index_, double skin_) const
Check if a position is on a facet.
static const uint32_t FACE_INDEX_ANY
Special value for any face index (wildcard)
Definition: face_identifier.h:52
void set_quadrangle(const facet_vertex &v0_, const facet_vertex &v1_, const facet_vertex &v2_, const facet_vertex &v3_, int iv0_, int iv1_, int iv2_, int iv3_)
const face_handle_type & get_tface_handle() const
bool has_vertex(vertex_id_type) const
Check if the vertex at given rank is defined.
facet34()
Default constructor.
#define GEOMTOOLS_OBJECT_3D_REGISTRATION_INTERFACE(ModelClassName)
Definition: i_object_3d.h:260
void _set_defaults()
Set default attributes values.
void add_facet(unsigned int facet_key_, int ivtx0_, int ivtx1_, int ivtx2_, int ivtx3_=INVALID_VERTEX)
bool is_consistent() const
Check the consistent flag.
bool has_face() const
Check the availability of face (triangle or quadrangle)
int add_vertex(unsigned int vtx_key_, const facet_vertex &vtx_)
static bool check_quadrangle(const geomtools::vector_3d &v0_, const geomtools::vector_3d &v1_, const geomtools::vector_3d &v2_, const geomtools::vector_3d &v3_, double tolerance_=0.0)
unsigned int _find_intercepts_exclude(const vector_3d &from_, const vector_3d &direction_, std::vector< face_intercept_info > &intercepts_, double skin_, const std::set< unsigned int > &excluded_facet_indexes_) const
Find the intercept points with several faces of the solid.
CLHEP::Hep3Vector vector_3d
Alias for the CLHEP 3D-vector class.
Definition: clhep.h:63
const facet_vertex & get_vertex(int i_) const
bool is_valid() const
Check validity.
face_handle_type & grab_tface_handle()
void set(int, int, int, int)
void print(std::ostream &out_) const
Simple print.
const facet34 & get_facet(unsigned int facet_key_) const
std::map< int32_t, int32_t > ref_facets
Definition: tessellation.h:83
double get_ymax() const
Return the max Y coordinates (bounding box)
static const int32_t INVALID_CATEGORY
Definition: tessellation.h:100
Last defined bit.
Definition: i_wires_3d_rendering.h:62
friend bool operator<(const facet_segment &fs1_, const facet_segment &fs2_)
bool has_tface() const
Check the availability of triangle face.
virtual vector_3d get_normal_on_surface(const vector_3d &position_, const face_identifier &) const
Compute the normal to the surface of the furstrum.
double get_ymin() const
Return the min Y coordinates (bounding box)
bool is_valid() const
Check the validity.
vertices_col_type & grab_vertices()
const vector_3d & get_vertex_position(vertex_id_type) const
Return the vertex at given rank (first/second)
std::list< polyline_type > wires_type
Alias for a list of 3D-polylines.
Definition: utils.h:61
void set_category(unsigned int c_)
bool is_shown() const
Check if the segment is shown.
double get_xmax() const
Return the max X coordinates (bounding box)
vector_3d position
Position of the vertex.
Definition: tessellation.h:82
bool has_qface() const
Check the availability of quadrangle face.
void _compute_internal_angles()
face_handle_type & grab_qface_handle()
virtual void tree_dump(std::ostream &a_out=std::clog, const std::string &a_title="", const std::string &a_indent="", bool a_inherit=false) const
Smart print.
int facet1_key
Key of the second facet.
Definition: tessellation.h:283
bool is_quadrangle() const
int vertex0_key
Key of the first vertex.
Definition: tessellation.h:280
const vector_3d & get_position() const
Return the position of the vertex.
static const int MAX_VERTEX
Definition: tessellation.h:300
double get_xmin() const
Return the min X coordinates (bounding box)
const vector_3d & get_normal() const
virtual void _at_lock()
Executed at lock stage.
virtual bool is_inside(const vector_3d &, double skin_=GEOMTOOLS_PROPER_TOLERANCE) const
Check if a point is inside the frustrum.
A triangle or convex quadrangle facet of a tessellated solid.
Definition: tessellation.h:93
static const unsigned int INVALID_FACET_INDEX
Definition: tessellation.h:301
static const int INVALID_VERTEX
Definition: tessellation.h:299
Definition: tessellation.h:238
A quadrangle.
Definition: quadrangle.h:36
void add_ref_facet(int facet_index_, int facet_node_)
Add the reference of a facet the vertex is associated to.
bool has_category() const
void set_vertex_keys(int, int)
Set vertex keys.
std::vector< face_info > face_info_collection_type
Type alias for a collection of face information objects.
Definition: face_info.h:168
void set_vertexes(const facet_vertex &vtx0_, const facet_vertex &vtx1_)
Set vertexes (associated to vertex keys)
uint32_t get_number_of_vertices() const
const triangle & get_tface() const
Return the associated triangle face.
Top-level namespace of the Bayeux/geomtools module library.
Definition: electromagnetic_field_manager.h:39
void set_position(const vector_3d &)
Set the position of the vertex.
A dictionary of arbitrary properties.
Definition: properties.h:125
std::map< unsigned int, facet_vertex > vertices_col_type
Definition: tessellation.h:306