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 & r & g & b;
22  ar & bbox;
23  ar & halfSize;
24  ar & normal;
25  }
26 
27 public:
28  // *** ATTRIBUTES *** //
29  // ******************** //
38  int numPoints = 0;
42  double r = 0;
46  double g = 0;
50  double b = 0;
54  glm::dvec3 normal = glm::dvec3(0, 0, 0);
58  AABB* bbox = nullptr;
67  double halfSize;
68 
69  // *** CONSTRUCTION *** //
70  // ********************** //
74  Voxel() = default;
80  Voxel(glm::dvec3 center, double voxelSize);
88  Voxel(double x, double y, double z, double halfVoxelSize);
89  ~Voxel() override{if(bbox!=nullptr) delete bbox;}
93  Primitive* clone() override;
97  void _clone(Primitive *p) override;
98 
99  // *** COPY / MOVE SEMANTICS *** //
100  // ****************************** //
105  void swap(Voxel &voxel){ // Swap function
106  std::swap(this->v, voxel.v);
107  std::swap(this->numPoints, voxel.numPoints);
108  std::swap(this->r, voxel.r);
109  std::swap(this->g, voxel.g);
110  std::swap(this->b, voxel.b);
111  std::swap(this->bbox, voxel.bbox);
112  std::swap(this->color, voxel.color);
113  std::swap(this->halfSize, voxel.halfSize);
114  }
115  Voxel(Voxel const &voxel) : // Copy constructor
116  v(voxel.v),
117  numPoints(voxel.numPoints),
118  r(voxel.r),
119  g(voxel.g),
120  b(voxel.b),
121  bbox(new AABB(*voxel.bbox)),
122  color(voxel.color),
123  halfSize(voxel.halfSize)
124  {}
125  Voxel & operator= (Voxel const &voxel){ // Copy assignment
126  Voxel newVoxel = Voxel(voxel);
127  swap(newVoxel);
128  return *this;
129  }
130  Voxel(Voxel &&voxel){ // Move constructor
131  swap(voxel);
132  }
133  Voxel & operator= (Voxel &&voxel){ // Move assignment
134  swap(voxel);
135  return *this;
136  }
137 
138 
139  // *** M E T H O D S *** //
140  // *********************** //
144  AABB* getAABB() override;
148  glm::dvec3 getCentroid() override;
152  double getIncidenceAngle_rad(
153  const glm::dvec3& rayOrigin,
154  const glm::dvec3& rayDir,
155  const glm::dvec3& intersectionPoint
156  ) override;
167  const glm::dvec3& rayOrigin,
168  const glm::dvec3& rayDir,
169  const glm::dvec3& intersectionPoint
170  );
174  std::vector<double> getRayIntersection(
175  const glm::dvec3& rayOrigin,
176  const glm::dvec3& rayDir
177  ) override;
182  const glm::dvec3& rayOrigin,
183  const glm::dvec3& rayDir
184  ) override;
188  inline virtual size_t getNumVertices() override {return 1;}
192  Vertex* getVertices() override;
196  inline virtual size_t getNumFullVertices() override {return 2;}
200  Vertex* getFullVertices() override;
204  inline double getGroundZOffset() override {return halfSize*2.0;}
212  bool hasNormal()
213  {return normal[0]!=0.0 || normal[1]!=0.0 || normal[2]!=0.0;}
214 
215 
216  // *** TRANSFORMATIONS *** //
217  // ************************* //
224  void rotate(Rotation &r) override{
226  update();
227  }
231  void scale(double const factor) override{
232  halfSize *= factor;
233  update();
234  }
238  void translate(glm::dvec3 const &shift) override{
239  Primitive::translate(shift);
240  update();
241  }
242 
246  void update() override;
247 };
248 
249 // *** BOOST SERIALIZATION *** //
250 // ***************************** //
251 /*
252  * Below code is commented because it is not necessary and it might cause
253  * memory leaks when called from external sources (i.e. from python)
254  */
255 /*namespace boost {
256  namespace serialization {
257 
258  template<class Archive>
259  inline void save_construct_data(
260  Archive & ar, const Voxel * t, const unsigned int file_version)
261  {
262  // save data required to construct instance
263  ar << t->v.pos;
264  ar << t->halfSize * 2;
265  }
266 
267  template<class Archive>
268  inline void load_construct_data(
269  Archive & ar, Voxel * t, const unsigned int file_version)
270  {
271  // retrieve data from archive required to construct new instance
272  glm::dvec3 center;
273  double voxelSize;
274  ar >> center;
275  ar >> voxelSize;
276 
277  // invoke inplace constructor to initialize instance of my_class
278  ::new(t)Voxel(center, voxelSize);
279  }
280  }
281 }*/
glm::dvec3 normal
Voxel normal vecctor.
Definition: Voxel.h:54
void _clone(Primitive *p) override
Definition: Voxel.cpp:22
glm::dvec3 getCentroid() override
Definition: Voxel.cpp:41
Voxel()=default
Default voxel constructor.
AABB * bbox
Axis aligned bounding box containing the voxel.
Definition: Voxel.h:58
Vertex * getVertices() override
Definition: Voxel.cpp:120
Definition: Rotation.h:80
Vertex v
Vertex representing the voxel center.
Definition: Voxel.h:33
Primitive * clone() override
Definition: Voxel.cpp:17
double getRayIntersectionDistance(const glm::dvec3 &rayOrigin, const glm::dvec3 &rayDir) override
Definition: Voxel.cpp:113
virtual size_t getNumVertices() override
Definition: Voxel.h:188
void translate(glm::dvec3 const &shift) override
Definition: Voxel.h:238
void update() override
Definition: Voxel.cpp:128
Class representing a color with 4 float components: RGBA.
Definition: Color4f.h:6
double b
Aggregated blue component from points inside voxel.
Definition: Voxel.h:50
double getGroundZOffset() override
Definition: Voxel.h:204
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 rotate(Rotation &r) override
Voxel cannot be rotated
Definition: Voxel.h:224
Color4f color
Voxel color. This attribute is not used at the moment and might be removed in the future...
Definition: Voxel.h:63
virtual void translate(glm::dvec3 const &shift)
Translate primitive by given shift.
Definition: Primitive.cpp:59
AABB * getAABB() override
Definition: Voxel.cpp:37
double getIncidenceAngle_rad(const glm::dvec3 &rayOrigin, const glm::dvec3 &rayDir, const glm::dvec3 &intersectionPoint) override
Definition: Voxel.cpp:45
Class representing an Axis Aligned Bounding Box (AABB)
Definition: AABB.h:10
Class representing a voxel primitive.
Definition: Voxel.h:11
void scale(double const factor) override
Definition: Voxel.h:231
std::vector< double > getRayIntersection(const glm::dvec3 &rayOrigin, const glm::dvec3 &rayDir) override
Definition: Voxel.cpp:107
double halfSize
Half of the voxel sizxe.
Definition: Voxel.h:67
void swap(Voxel &voxel)
Swap semantic implementation for voxel.
Definition: Voxel.h:105
double r
Aggregated red component from points inside voxel.
Definition: Voxel.h:42
virtual void rotate(Rotation &r)
Performs rotation over primitive.
Definition: Primitive.cpp:44
Abstract class defining the common behavior for all primitives.
Definition: Primitive.h:20
double g
Aggregated green component from points inside voxel.
Definition: Voxel.h:46
virtual size_t getNumFullVertices() override
Definition: Voxel.h:196
bool hasNormal()
Check if voxel has a valid normal.
Definition: Voxel.h:212
Vertex * getFullVertices() override
Definition: Voxel.cpp:124
int numPoints
Number of points inside the voxel. Useful when the voxel has been built from a point cloud...
Definition: Voxel.h:38
Class representing a vertex.
Definition: Vertex.h:14