Helios++
Helios software for LiDAR simulations
SAHKDTreeFactory.h
1 #pragma once
2 
3 #include <SimpleKDTreeFactory.h>
4 #include <KDTreePrimitiveComparator.h>
5 
7 class SAHKDTreeGeoemtricStrategy;
8 
127  // *** FRIENDS *** //
128  // ***************** //
129  friend class MultiThreadSAHKDTreeFactory;
130  friend class SAHKDTreeGeometricStrategy;
131 
132 private:
133  // *** SERIALIZATION *** //
134  // *********************** //
135  friend class boost::serialization::access;
144  template <typename Archive>
145  void serialize(Archive &ar, unsigned int const version){
146  boost::serialization::void_cast_register<
149  >();
150  ar &boost::serialization::base_object<SimpleKDTreeFactory>(*this);
151  ar &ci &cl &co;
152  ar &lossNodes;
153  ar &cacheI &cacheL &cacheO &cacheT;
154  ar &cacheRoot;
155  }
156 
157 protected:
158  // *** ATTRIBUTES *** //
159  // ******************** //
166  size_t lossNodes;
171  double ci;
176  double cl;
181  double co;
182 
183  // *** CACHE ATTRIBUTES *** //
184  // ************************** //
188  KDTreeNode *cacheRoot = nullptr;
198  double cacheI;
208  double cacheL;
218  double cacheO;
233  double cacheT;
241  std::function<void(void)> _lockILOT;
248  std::function<void(void)> _unlockILOT;
249 
250 public:
251  // *** CONSTRUCTION / DESTRUCTION *** //
252  // ************************************ //
260  size_t const lossNodes=21,
261  double const ci=1,
262  double const cl=1,
263  double const co=1
264  );
265  virtual ~SAHKDTreeFactory() = default;
266 
267  // *** CLONE *** //
268  // *************** //
272  KDTreeFactory * clone() const override;
276  void _clone(KDTreeFactory *kdtf) const override;
277 
278  // *** BUILDING METHODS *** //
279  // ************************** //
324  void defineSplit(
325  KDTreeNode *node,
326  KDTreeNode *parent,
327  vector<Primitive *> &primitives,
328  int const depth
329  ) const override;
330 
341  void computeKDTreeStats(KDTreeNodeRoot *root) const override;
342 
403  void buildChildrenNodes(
404  KDTreeNode *node,
405  KDTreeNode *parent,
406  vector<Primitive *> const &primitives,
407  int const depth,
408  int const index,
409  vector<Primitive *> &leftPrimitives,
410  vector<Primitive *> &rightPrimitives
411  ) override;
412 
427  virtual void buildChildrenNodesRecipe(
428  KDTreeNode *node,
429  KDTreeNode *parent,
430  vector<Primitive *> const &primitives,
431  int const depth,
432  int const index,
433  vector<Primitive *> &leftPrimitives,
434  vector<Primitive *> &rightPrimitives,
435  std::function<void(
436  KDTreeNode *node,
437  int const depth,
438  int const index,
439  vector<Primitive *> &leftPrimitives,
440  vector<Primitive *> &rightPrimitives
441  )>f_buildChildrenNodes
442  );
443 
444 protected:
445  // *** BUILDING UTILS *** //
446  // ************************ //
457  bool checkNodeMustSplit(
458  vector<Primitive *> const &primitives,
459  vector<Primitive *> const &leftPrimitives,
460  vector<Primitive *> const &rightPrimitives
461  ) const override;
462 
463  // *** SAH UTILS *** //
464  // ******************* //
493  virtual double splitLoss(
494  vector<Primitive *> const &primitives,
495  int const splitAxis,
496  double const splitPos,
497  double const r
498  ) const ;
499 
514  virtual void computeBestSplit(
515  vector<Primitive *> &primitives,
516  size_t const lossNodes,
517  double const start,
518  double const step,
519  int const splitAxis,
520  double const minBound,
521  double const boundLength,
522  double &loss,
523  double &splitPos
524  ) const;
525 
533  virtual double findSplitPositionBySAH(
534  KDTreeNode *node,
535  vector<Primitive *> &primitives
536  ) const;
537 
556  virtual double findSplitPositionBySAHRecipe(
557  KDTreeNode *node,
558  vector<Primitive *> &primitives,
559  std::function<void(
560  vector<Primitive *>::iterator begin,
561  vector<Primitive *>::iterator end,
562  KDTreePrimitiveComparator comparator
563  )> f_sortPrimitives,
564  std::function<void(
565  vector<Primitive *> &primitives,
566  size_t const lossNodes,
567  double const start,
568  double const step,
569  int const splitAxis,
570  double const minBound,
571  double const boundLength,
572  double &loss,
573  double &splitPos
574  )> f_computeLossNodes
575  ) const;
576 
594  virtual double heuristicILOT(
595  double &hi,
596  double &hl,
597  double &ho,
598  double &ht,
599  double const surfaceAreaRoot,
600  double const surfaceAreaInterior,
601  double const surfaceAreaLeaf,
602  vector<Primitive *> const &primitives
603  ) const;
604 
623  virtual double cumulativeILOT(
624  double &hi,
625  double &hl,
626  double &ho,
627  double &ht,
628  double const _hi,
629  double const _hl,
630  double const _ho,
631  double const saRoot
632  ) const ;
645  virtual void internalizeILOT(
646  double &hi,
647  double &hl,
648  double &ho,
649  double &ht,
650  KDTreeNode *node,
651  vector<Primitive *> const &primitives,
652  vector<Primitive *> const &leftPrimitives,
653  vector<Primitive *> const &rightPrimitives
654  );
655 
656  // *** CACHE UTILS *** //
657  // ********************* //
666  virtual inline void toILOTCache(
667  double const I,
668  double const L,
669  double const O,
670  double const T
671  ){
672  cacheI = I;
673  cacheL = L;
674  cacheO = O;
675  cacheT = T;
676  }
686  virtual inline void fromILOTCache(
687  double &I,
688  double &L,
689  double &O,
690  double &T
691  ) const {
692  I = cacheI;
693  L = cacheL;
694  O = cacheO;
695  T = cacheT;
696  }
704  virtual inline void fromILOCache(
705  double &I,
706  double &L,
707  double &O
708  ) const {
709  I = cacheI;
710  L = cacheL;
711  O = cacheO;
712  }
717  virtual inline double getCacheT() const {return cacheT;}
723  virtual void initILOT(
724  KDTreeNode *root,
725  vector<Primitive *> const &primitives
726  );
732  virtual inline void setCacheRoot(KDTreeNode *root) {cacheRoot = root;}
733 
734 public:
735  // *** GETTERs and SETTERs *** //
736  // ***************************** //
742  virtual inline size_t getLossNodes() const {return lossNodes;}
750  virtual inline void setLossNodes(size_t const lossNodes)
751  {this->lossNodes = lossNodes;}
757  virtual inline double getInteriorCost() const {return ci;}
763  virtual inline void setInteriorCost(double const ci) {this->ci = ci;}
769  virtual inline double getLeafCost() const {return cl;}
775  virtual inline void setLeafCost(double const cl) {this->cl = cl;}
781  virtual inline double getObjectCost() const {return co;}
787  virtual inline void setObjectCost(double const co) {this->co = co;}
788 };
Class that must be extended by any class which provides factory methods for k-dimensional trees....
Definition: KDTreeFactory.h:22
Class representing the root node of a KDTree.
Definition: KDTreeNodeRoot.h:12
Class representing a KDTree node.
Definition: KDTreeNode.h:9
Decorator for any SAH KDTree which provides support for multi thread KDTree building.
Definition: MultiThreadSAHKDTreeFactory.h:15
Class providing building methods for k-dimensional trees with surface area heuristic (SAH)
Definition: SAHKDTreeFactory.h:126
KDTreeFactory * clone() const override
Definition: SAHKDTreeFactory.cpp:41
void _clone(KDTreeFactory *kdtf) const override
Assign attributes from SAHKDTreeFactory to its clone.
Definition: SAHKDTreeFactory.cpp:49
virtual void computeBestSplit(vector< Primitive * > &primitives, size_t const lossNodes, double const start, double const step, int const splitAxis, double const minBound, double const boundLength, double &loss, double &splitPos) const
Iteratively compute the best split position, it is the one with smaller loss.
Definition: SAHKDTreeFactory.cpp:221
virtual double getInteriorCost() const
Obtain the cost-weight of interior nodes.
Definition: SAHKDTreeFactory.h:757
double co
Cost-weight for testing an object for intersection.
Definition: SAHKDTreeFactory.h:181
bool checkNodeMustSplit(vector< Primitive * > const &primitives, vector< Primitive * > const &leftPrimitives, vector< Primitive * > const &rightPrimitives) const override
Check wheter the node must be splitted (true) or not (false) depending on its total primitives.
Definition: SAHKDTreeFactory.cpp:193
virtual void internalizeILOT(double &hi, double &hl, double &ho, double &ht, KDTreeNode *node, vector< Primitive * > const &primitives, vector< Primitive * > const &leftPrimitives, vector< Primitive * > const &rightPrimitives)
Compute ILOT corresponding to internalization (make interior) of given node and its corresponding lef...
Definition: SAHKDTreeFactory.cpp:382
SAHKDTreeFactory(size_t const lossNodes=21, double const ci=1, double const cl=1, double const co=1)
Surface area heuristic KDTree factory default constructor.
Definition: SAHKDTreeFactory.cpp:6
virtual void buildChildrenNodesRecipe(KDTreeNode *node, KDTreeNode *parent, vector< Primitive * > const &primitives, int const depth, int const index, vector< Primitive * > &leftPrimitives, vector< Primitive * > &rightPrimitives, std::function< void(KDTreeNode *node, int const depth, int const index, vector< Primitive * > &leftPrimitives, vector< Primitive * > &rightPrimitives)>f_buildChildrenNodes)
The recipe for building of children nodes by SAH algorithm. It is meant to be used by the SAHKDTreeFa...
Definition: SAHKDTreeFactory.cpp:138
virtual size_t getLossNodes() const
Obtain the loss nodes used to compute the Surface Area Heuristic.
Definition: SAHKDTreeFactory.h:742
virtual double findSplitPositionBySAHRecipe(KDTreeNode *node, vector< Primitive * > &primitives, std::function< void(vector< Primitive * >::iterator begin, vector< Primitive * >::iterator end, KDTreePrimitiveComparator comparator)> f_sortPrimitives, std::function< void(vector< Primitive * > &primitives, size_t const lossNodes, double const start, double const step, int const splitAxis, double const minBound, double const boundLength, double &loss, double &splitPos)> f_computeLossNodes) const
The recipe for finding split position by SAH algorithm. It is meant to be used by the SAHKDTreeFactor...
Definition: SAHKDTreeFactory.cpp:288
virtual double getCacheT() const
Obtain the T component of ILOT cache.
Definition: SAHKDTreeFactory.h:717
virtual double splitLoss(vector< Primitive * > const &primitives, int const splitAxis, double const splitPos, double const r) const
Compute the loss function for the splitting hyperplane.
Definition: SAHKDTreeFactory.cpp:203
virtual double getObjectCost() const
Obtain the cost-weight of testing an object for intersection.
Definition: SAHKDTreeFactory.h:781
void buildChildrenNodes(KDTreeNode *node, KDTreeNode *parent, vector< Primitive * > const &primitives, int const depth, int const index, vector< Primitive * > &leftPrimitives, vector< Primitive * > &rightPrimitives) override
Build children nodes using heuristic to handle KDTree in-depth partitioning.
Definition: SAHKDTreeFactory.cpp:99
virtual void fromILOTCache(double &I, double &L, double &O, double &T) const
Set references from ILOT cache.
Definition: SAHKDTreeFactory.h:686
virtual double heuristicILOT(double &hi, double &hl, double &ho, double &ht, double const surfaceAreaRoot, double const surfaceAreaInterior, double const surfaceAreaLeaf, vector< Primitive * > const &primitives) const
Compute the heuristic preserving partials result of interest.
Definition: SAHKDTreeFactory.cpp:341
double ci
Cost-weight for traversing interior nodes.
Definition: SAHKDTreeFactory.h:171
std::function< void(void)> _unlockILOT
Function to unlock the ILOT cache. It is the counterpart of the _lockILOT function.
Definition: SAHKDTreeFactory.h:248
virtual void setLeafCost(double const cl)
Set the cost-weight for leaf nodes.
Definition: SAHKDTreeFactory.h:775
void serialize(Archive &ar, unsigned int const version)
Serialize a surface area heuristic KDTree factory to a stream of bytes.
Definition: SAHKDTreeFactory.h:145
KDTreeNode * cacheRoot
Cache pointer to root node of current KDTree being built.
Definition: SAHKDTreeFactory.h:188
virtual double getLeafCost() const
Obtain the cost-weight for leaf nodes.
Definition: SAHKDTreeFactory.h:769
double cacheO
Cache last valid object cost.
Definition: SAHKDTreeFactory.h:218
virtual void fromILOCache(double &I, double &L, double &O) const
Set references from ILOT cache but only for ILO components.
Definition: SAHKDTreeFactory.h:704
size_t lossNodes
How many loss nodes must be computed when optimizing the loss function to determine the best split p...
Definition: SAHKDTreeFactory.h:166
virtual void setObjectCost(double const co)
Set the cost-weight of testing an object for intersection.
Definition: SAHKDTreeFactory.h:787
virtual void initILOT(KDTreeNode *root, vector< Primitive * > const &primitives)
Initialize the ILOT cache from given root node.
Definition: SAHKDTreeFactory.cpp:419
double cacheT
Cache last valid tree cost.
Definition: SAHKDTreeFactory.h:233
double cacheI
Cache last valid interior cost.
Definition: SAHKDTreeFactory.h:198
virtual void toILOTCache(double const I, double const L, double const O, double const T)
Set ILOT cache from given values.
Definition: SAHKDTreeFactory.h:666
virtual void setLossNodes(size_t const lossNodes)
Set the number loss nodes used to compute the Surface Area Heuristic.
Definition: SAHKDTreeFactory.h:750
virtual void setInteriorCost(double const ci)
Set the cost-weight of interior nodes.
Definition: SAHKDTreeFactory.h:763
void computeKDTreeStats(KDTreeNodeRoot *root) const override
Compute the simple stats of the KDTree but also its cost based on surface area heuristic defined in d...
Definition: SAHKDTreeFactory.cpp:73
double cl
Cost-weight for traversing leaf nodes.
Definition: SAHKDTreeFactory.h:176
virtual double cumulativeILOT(double &hi, double &hl, double &ho, double &ht, double const _hi, double const _hl, double const _ho, double const saRoot) const
Compute the cumulative of heuristic ILOT.
Definition: SAHKDTreeFactory.cpp:365
void defineSplit(KDTreeNode *node, KDTreeNode *parent, vector< Primitive * > &primitives, int const depth) const override
Define the split axis and position for current node.
Definition: SAHKDTreeFactory.cpp:62
double cacheL
Cache last valid leaves cost.
Definition: SAHKDTreeFactory.h:208
std::function< void(void)> _lockILOT
Function to lock the ILOT cache on a unique way (no thread but locker one must be able to use it)....
Definition: SAHKDTreeFactory.h:241
virtual double findSplitPositionBySAH(KDTreeNode *node, vector< Primitive * > &primitives) const
Find the best split position using Surface Area Heuristic (SAH) as described in SAHKDTreeFactory::def...
Definition: SAHKDTreeFactory.cpp:247
virtual void setCacheRoot(KDTreeNode *root)
Set the cached root node.
Definition: SAHKDTreeFactory.h:732
Class providing a strategy for geometry-level parallelization of SAH KDTree building....
Definition: SAHKDTreeGeometricStrategy.h:17
Class providing building methods for simple k-dimensional trees.
Definition: SimpleKDTreeFactory.h:18
KDTree primitive comparator.
Definition: KDTreePrimitiveComparator.h:8