Helios++
Helios software for LiDAR simulations
Voxel.h
1 #pragma once
2 
3 #include "Primitive.h"
4 #include "Vertex.h"
5 #include "AABB.h"
6 
7 
11 class Voxel : public Primitive {
12 private:
13  // *** BOOST SERIALIZATION *** //
14  // ***************************** //
15  friend class boost::serialization::access;
16  template <typename Archive>
17  void serialize(Archive& ar, const unsigned int version) {
18  boost::serialization::void_cast_register<Voxel, Primitive>();
19  ar & boost::serialization::base_object<Primitive>(*this);
20  ar & v;
21  ar & numPoints;
22  ar & r & g & b;
23  ar & bbox;
24  ar & color;
25  ar & halfSize;
26  }
27 
28 public:
29  // *** ATTRIBUTES *** //
30  // ******************** //
39  int numPoints = 0;
43  double r = 0;
47  double g = 0;
51  double b = 0;
55  AABB* bbox = nullptr;
64  double halfSize;
65 
66  // *** CONSTRUCTION *** //
67  // ********************** //
71  Voxel() : Primitive() {};
77  Voxel(glm::dvec3 center, double voxelSize);
85  Voxel(double x, double y, double z, double halfVoxelSize);
86  ~Voxel() override{if(bbox!=nullptr) delete bbox;}
90  Primitive* clone() override;
94  void _clone(Primitive *p) override;
95 
96  // *** COPY / MOVE SEMANTICS *** //
97  // ****************************** //
102  void swap(Voxel &voxel){ // Swap function
103  std::swap(this->v, voxel.v);
104  std::swap(this->numPoints, voxel.numPoints);
105  std::swap(this->r, voxel.r);
106  std::swap(this->g, voxel.g);
107  std::swap(this->b, voxel.b);
108  std::swap(this->bbox, voxel.bbox);
109  std::swap(this->color, voxel.color);
110  std::swap(this->halfSize, voxel.halfSize);
111  }
112  Voxel(Voxel const &voxel) : // Copy constructor
113  v(voxel.v),
114  numPoints(voxel.numPoints),
115  r(voxel.r),
116  g(voxel.g),
117  b(voxel.b),
118  bbox(new AABB(*voxel.bbox)),
119  color(voxel.color),
120  halfSize(voxel.halfSize)
121  {}
122  Voxel & operator= (Voxel const &voxel){ // Copy assignment
123  Voxel newVoxel = Voxel(voxel);
124  swap(newVoxel);
125  return *this;
126  }
127  Voxel(Voxel &&voxel){ // Move constructor
128  swap(voxel);
129  }
130  Voxel & operator= (Voxel &&voxel){ // Move assignment
131  swap(voxel);
132  return *this;
133  }
134 
135 
136  // *** M E T H O D S *** //
137  // *********************** //
141  AABB* getAABB() override;
145  glm::dvec3 getCentroid() override;
149  double getIncidenceAngle_rad(
150  const glm::dvec3& rayOrigin,
151  const glm::dvec3& rayDir,
152  const glm::dvec3& intersectionPoint
153  ) override;
164  const glm::dvec3& rayOrigin,
165  const glm::dvec3& rayDir,
166  const glm::dvec3& intersectionPoint
167  );
171  std::vector<double> getRayIntersection(
172  const glm::dvec3& rayOrigin,
173  const glm::dvec3& rayDir
174  ) override;
179  const glm::dvec3& rayOrigin,
180  const glm::dvec3& rayDir
181  ) override;
185  inline virtual size_t getNumVertices() override {return 1;}
189  Vertex* getVertices() override;
193  inline virtual size_t getNumFullVertices() override {return 2;}
197  Vertex* getFullVertices() override;
201  inline double getGroundZOffset() override {return halfSize*2.0;}
209  bool inline hasNormal() const
210  {return v.normal[0]!=0.0 || v.normal[1]!=0.0 || v.normal[2]!=0.0;}
211 
212 
213  // *** TRANSFORMATIONS *** //
214  // ************************* //
221  void rotate(Rotation &r) override{
223  update();
224  }
228  void scale(double const factor) override{
229  halfSize *= factor;
230  update();
231  }
235  void translate(glm::dvec3 const &shift) override{
236  Primitive::translate(shift);
237  update();
238  }
239 
243  void update() override;
244 };
245 
246 // *** BOOST SERIALIZATION *** //
247 // ***************************** //
248 /*
249  * Below code is commented because it is not necessary and it might cause
250  * memory leaks when called from external sources (i.e. from python)
251  */
252 /*namespace boost {
253  namespace serialization {
254 
255  template<class Archive>
256  inline void save_construct_data(
257  Archive & ar, const Voxel * t, const unsigned int file_version)
258  {
259  // save data required to construct instance
260  ar << t->v.pos;
261  ar << t->halfSize * 2;
262  }
263 
264  template<class Archive>
265  inline void load_construct_data(
266  Archive & ar, Voxel * t, const unsigned int file_version)
267  {
268  // retrieve data from archive required to construct new instance
269  glm::dvec3 center;
270  double voxelSize;
271  ar >> center;
272  ar >> voxelSize;
273 
274  // invoke inplace constructor to initialize instance of my_class
275  ::new(t)Voxel(center, voxelSize);
276  }
277  }
278 }*/
Class representing an Axis Aligned Bounding Box (AABB)
Definition: AABB.h:10
Class representing a color with 4 float components: RGBA.
Definition: Color4f.h:10
Abstract class defining the common behavior for all primitives.
Definition: Primitive.h:24
virtual void translate(glm::dvec3 const &shift)
Translate primitive by given shift.
Definition: Primitive.cpp:59
virtual void rotate(Rotation &r)
Performs rotation over primitive.
Definition: Primitive.cpp:44
Definition: Rotation.h:80
Class representing a vertex.
Definition: Vertex.h:14
glm::dvec3 normal
Vertex normal vector.
Definition: Vertex.h:43
Class representing a voxel primitive.
Definition: Voxel.h:11
double getRayIntersectionDistance(const glm::dvec3 &rayOrigin, const glm::dvec3 &rayDir) override
Definition: Voxel.cpp:114
double getIncidenceAngleClosestFace_rad(const glm::dvec3 &rayOrigin, const glm::dvec3 &rayDir, const glm::dvec3 &intersectionPoint)
Obtain the incidence angle with respect to closest face for given intersection point.
Definition: Voxel.cpp:63
void _clone(Primitive *p) override
Definition: Voxel.cpp:22
Voxel()
Default voxel constructor.
Definition: Voxel.h:71
void update() override
Definition: Voxel.cpp:129
virtual size_t getNumVertices() override
Definition: Voxel.h:185
Primitive * clone() override
Definition: Voxel.cpp:17
AABB * getAABB() override
Definition: Voxel.cpp:37
Vertex * getFullVertices() override
Definition: Voxel.cpp:125
void rotate(Rotation &r) override
Voxel cannot be rotated
Definition: Voxel.h:221
glm::dvec3 getCentroid() override
Definition: Voxel.cpp:41
void scale(double const factor) override
Definition: Voxel.h:228
Vertex * getVertices() override
Definition: Voxel.cpp:121
Vertex v
Vertex representing the voxel center.
Definition: Voxel.h:34
double getIncidenceAngle_rad(const glm::dvec3 &rayOrigin, const glm::dvec3 &rayDir, const glm::dvec3 &intersectionPoint) override
Definition: Voxel.cpp:45
double halfSize
Half of the voxel sizxe.
Definition: Voxel.h:64
void translate(glm::dvec3 const &shift) override
Definition: Voxel.h:235
AABB * bbox
Axis aligned bounding box containing the voxel.
Definition: Voxel.h:55
double b
Aggregated blue component from points inside voxel.
Definition: Voxel.h:51
int numPoints
Number of points inside the voxel. Useful when the voxel has been built from a point cloud.
Definition: Voxel.h:39
double getGroundZOffset() override
Definition: Voxel.h:201
double g
Aggregated green component from points inside voxel.
Definition: Voxel.h:47
bool hasNormal() const
Check if voxel has a valid normal.
Definition: Voxel.h:209
double r
Aggregated red component from points inside voxel.
Definition: Voxel.h:43
virtual size_t getNumFullVertices() override
Definition: Voxel.h:193
std::vector< double > getRayIntersection(const glm::dvec3 &rayOrigin, const glm::dvec3 &rayDir) override
Definition: Voxel.cpp:108
void swap(Voxel &voxel)
Swap semantic implementation for voxel.
Definition: Voxel.h:102
Color4f color
Voxel color. This attribute is not used at the moment and might be removed in the future.
Definition: Voxel.h:60