Helios++
Helios software for LiDAR simulations
Scene.h
1 #pragma once
2 
3 #include <boost/archive/text_iarchive.hpp>
4 #include <boost/archive/text_oarchive.hpp>
5 #include <boost/serialization/base_object.hpp>
6 #include <boost/serialization/split_member.hpp>
7 
8 #include <glm/glm.hpp>
9 #include <serial.h>
10 
11 #include <AABB.h>
12 #include <Asset.h>
13 #include <DetailedVoxel.h>
14 #include <Triangle.h>
15 #include <Vertex.h>
16 #include <Voxel.h>
17 
18 #include <KDTreeNodeRoot.h>
19 #include <KDGroveFactory.h>
20 #include <KDGrove.h>
21 #include <KDGroveRaycaster.h>
22 
23 #include <RaySceneIntersection.h>
24 
28 class Scene : public Asset {
29 
30 private:
31  // *** SERIALIZATION *** //
32  // *********************** //
33  friend class boost::serialization::access;
41  template <class Archive>
42  void save (
43  Archive &ar,
44  unsigned int const version
45  ) const {
46  // Register primitive derivates
47  ar.template register_type<Vertex>();
48  ar.template register_type<AABB>();
49  ar.template register_type<Triangle>();
50  ar.template register_type<Voxel>();
51  ar.template register_type<DetailedVoxel>();
52 
53  // Save the scene itself
54  boost::serialization::void_cast_register<Scene, Asset>();
55  ar &boost::serialization::base_object<Asset>(*this);
56  ar &kdgf;
57  //ar &kdgrove; // KDGrove not saved because trees might be too deep
58  ar &bbox;
59  ar &bbox_crs;
60  ar &primitives;
61  ar &parts;
62  }
70  template <class Archive>
71  void load(
72  Archive &ar,
73  unsigned int const fileVersion
74  ){
75  // Register primitive derivates
76  ar.template register_type<Vertex>();
77  ar.template register_type<AABB>();
78  ar.template register_type<Triangle>();
79  ar.template register_type<Voxel>();
80  ar.template register_type<DetailedVoxel>();
81 
82  // Load the scene itself
83  boost::serialization::void_cast_register<Scene, Asset>();
84  ar &boost::serialization::base_object<Asset>(*this);
85  ar &kdgf;
86  //ar &kdgrove; // KDTree not loaded because it might be too deep
87  ar &bbox;
88  ar &bbox_crs;
89  ar &primitives;
90  ar &parts;
91 
92  // Build KDTree from primitives
93  if(kdgf != nullptr) buildKDGroveWithLog(true);
94  }
95  BOOST_SERIALIZATION_SPLIT_MEMBER();
96 
97 protected:
98  // *** ATTRIBUTES *** //
99  // ******************** //
105  std::shared_ptr<KDGroveFactory> kdgf;
110  std::shared_ptr<KDGrove> kdgrove;
114  std::shared_ptr<AABB> bbox;
119  std::shared_ptr<AABB> bbox_crs;
126  std::shared_ptr<KDGroveRaycaster> raycaster;
127 
128 public:
132  std::vector<Primitive *> primitives;
141  std::vector<std::shared_ptr<ScenePart>> parts;
142 
143  // *** CONSTRUCTION / DESTRUCTION *** //
144  // ************************************ //
148  Scene() :
149  kdgf(make_shared<KDGroveFactory>(make_shared<SimpleKDTreeFactory>()))
150  {}
151  ~Scene() override {
152  for (Primitive *p : primitives) delete p;
153  }
154  Scene(Scene &s);
155 
156  // *** M E T H O D S *** //
157  // ************************* //
172  bool finalizeLoading(bool const safe=false);
178  void registerParts();
183  std::shared_ptr<AABB> getAABB();
190  glm::dvec3 getGroundPointAt(glm::dvec3 point);
202  std::shared_ptr<RaySceneIntersection> getIntersection(
203  glm::dvec3 const &rayOrigin,
204  glm::dvec3 const &rayDir,
205  bool const groundOnly
206  ) const;
215  std::shared_ptr<RaySceneIntersection> getIntersection(
216  vector<double> const &tMinMax,
217  glm::dvec3 const &rayOrigin,
218  glm::dvec3 const &rayDir,
219  bool const groundOnly
220  ) const ;
233  std::map<double, Primitive *> getIntersections(
234  glm::dvec3 &rayOrigin,
235  glm::dvec3 &rayDir,
236  bool const groundOnly
237  );
238 
246  glm::dvec3 getShift();
252  inline glm::dvec3 const& getShiftRef() const {return bbox_crs->getMin();}
253 
258  std::vector<Vertex *> getAllVertices();
259 
334  void doForceOnGround();
361  glm::dvec3 findForceOnGroundQ(
362  int const searchDepth,
363  glm::dvec3 const minzv,
364  vector<Vertex *> &vertices,
365  vector<double> const &o,
366  vector<double> const &v
367  );
368 
375  void buildKDGrove(bool const safe=false);
381  void buildKDGroveWithLog(bool const safe=false);
382 
383  // *** GETTERs and SETTERs *** //
384  // ***************************** //
385  virtual inline std::shared_ptr<KDGroveRaycaster> const &getRaycaster()
386  const {return raycaster;}
392  virtual inline std::shared_ptr<KDGroveFactory> getKDGroveFactory() const
393  {return kdgf;}
399  virtual inline void setKDGroveFactory(
400  std::shared_ptr<KDGroveFactory> const kdgf
401  )
402  {this->kdgf = kdgf;}
408  virtual inline std::shared_ptr<KDGrove> getKDGrove() const
409  {return kdgrove;}
415  virtual inline void setKDGrove(std::shared_ptr<KDGrove> const kdgrove)
416  {this->kdgrove = kdgrove;}
417 
423  virtual inline std::shared_ptr<AABB> getBBox() const {return bbox;}
429  virtual inline void setBBox(std::shared_ptr<AABB> const bbox)
430  {this->bbox = bbox;}
436  virtual inline std::shared_ptr<AABB> getBBoxCRS() const {return bbox_crs;}
442  virtual inline void setBBoxCRS(std::shared_ptr<AABB> const bbox)
443  {this->bbox_crs = bbox;}
444 
450  virtual inline bool hasMovingObjects() const{
451  for(std::shared_ptr<ScenePart> part : parts){
452  if(part->getType()==ScenePart::ObjectType::DYN_MOVING_OBJECT)
453  return true;
454  }
455  return false;
456  }
457 
458  // *** READ/WRITE *** //
459  // ********************* //
464  virtual void writeObject(std::string path);
470  static Scene *readObject(std::string path);
471 
472  // *** SIMULATION STEP *** //
473  // ************************* //
484  virtual bool doSimStep() {return false;}
485 };
Base class for all assets.
Definition: Asset.h:10
Definition: KDGroveFactory.h:18
Abstract class defining the common behavior for all primitives.
Definition: Primitive.h:24
Class representing a scene asset.
Definition: Scene.h:28
std::shared_ptr< AABB > bbox
Axis aligned bounding box defining scene boundaries.
Definition: Scene.h:114
virtual std::shared_ptr< AABB > getBBox() const
Obtain the scene's bounding box.
Definition: Scene.h:423
virtual std::shared_ptr< KDGrove > getKDGrove() const
Obtain the KDGrove used by the scene.
Definition: Scene.h:408
virtual void setKDGroveFactory(std::shared_ptr< KDGroveFactory > const kdgf)
Set the KDGrove factory to be used by the scene.
Definition: Scene.h:399
static Scene * readObject(std::string path)
Read serialized scene from given file.
Definition: Scene.cpp:392
glm::dvec3 getShift()
Obtain the minimum boundaries of the original axis aligned bounding box containing the scene,...
Definition: Scene.cpp:210
std::shared_ptr< RaySceneIntersection > getIntersection(glm::dvec3 const &rayOrigin, glm::dvec3 const &rayDir, bool const groundOnly) const
Obtain the intersection between the ray and the scene, if any.
Definition: Scene.cpp:168
std::shared_ptr< KDGroveFactory > kdgf
The KDGrove factory used to build the scene KDGrove.
Definition: Scene.h:105
void doForceOnGround()
For each scene part which is flagged as forceOnGround, it will be vertically translated to closest gr...
Definition: Scene.cpp:222
std::vector< std::shared_ptr< ScenePart > > parts
Parts composing the scene with no repeats.
Definition: Scene.h:141
glm::dvec3 getGroundPointAt(glm::dvec3 point)
Obtain the ground point at specified XY coordinates.
Definition: Scene.cpp:144
void buildKDGrove(bool const safe=false)
Build the KDGrove for the scene, overwriting previous one if any.
Definition: Scene.cpp:359
std::map< double, Primitive * > getIntersections(glm::dvec3 &rayOrigin, glm::dvec3 &rayDir, bool const groundOnly)
Obtain all intersections between the ray and the scene, if any.
Definition: Scene.cpp:193
void save(Archive &ar, unsigned int const version) const
Handle scene save operation.
Definition: Scene.h:42
glm::dvec3 const & getShiftRef() const
Like Scene::getShift but returning a const reference instead of a copy.
Definition: Scene.h:252
std::shared_ptr< AABB > bbox_crs
Original axis aligned bounding box defining scene boundaries before centering it.
Definition: Scene.h:119
glm::dvec3 findForceOnGroundQ(int const searchDepth, glm::dvec3 const minzv, vector< Vertex * > &vertices, vector< double > const &o, vector< double > const &v)
Assist doForceOnGround function to find an adequate for step 4.
Definition: Scene.cpp:328
std::vector< Primitive * > primitives
Vector of primitives composing the scene.
Definition: Scene.h:132
virtual void writeObject(std::string path)
Serialize the scene and write it to given output file.
Definition: Scene.cpp:385
std::shared_ptr< KDGrove > kdgrove
KDGrove containing a KDTree for each scene part to speed-up ray-primitive intersection check computat...
Definition: Scene.h:110
void registerParts()
Register all scene parts composing the scene in the parts vector with no repetitions.
Definition: Scene.cpp:129
virtual std::shared_ptr< KDGroveFactory > getKDGroveFactory() const
Obtain the KDGrove factory used by the scene.
Definition: Scene.h:392
virtual void setKDGrove(std::shared_ptr< KDGrove > const kdgrove)
Set the KDGrove to be used by the scene.
Definition: Scene.h:415
std::shared_ptr< AABB > getAABB()
Obtain the axis aligned bounding box defining scene boundaries.
Definition: Scene.cpp:142
std::shared_ptr< KDGroveRaycaster > raycaster
The raycaster based on KDGrove.
Definition: Scene.h:126
void load(Archive &ar, unsigned int const fileVersion)
Handle scene load operation.
Definition: Scene.h:71
virtual void setBBox(std::shared_ptr< AABB > const bbox)
Set the scene's bounding box.
Definition: Scene.h:429
Scene()
Scene default constructor.
Definition: Scene.h:148
void buildKDGroveWithLog(bool const safe=false)
Call buildKDGrove exporting building information through logging system.
Definition: Scene.cpp:372
bool finalizeLoading(bool const safe=false)
Handle scene loading finish process.
Definition: Scene.cpp:69
virtual bool doSimStep()
Support simulation step handling from scene side.
Definition: Scene.h:484
std::vector< Vertex * > getAllVertices()
Obtain all vertices (without repetitions) composing the scene.
Definition: Scene.cpp:212
virtual void setBBoxCRS(std::shared_ptr< AABB > const bbox)
Set the scene's coordinate reference system bounding box.
Definition: Scene.h:442
virtual bool hasMovingObjects() const
Checke whether the scene contains at least one moving object (true) or not (false)
Definition: Scene.h:450
virtual std::shared_ptr< AABB > getBBoxCRS() const
Obtain the scene's coordinate reference system bounding box.
Definition: Scene.h:436
Class providing building methods for simple k-dimensional trees.
Definition: SimpleKDTreeFactory.h:18