KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
tree.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: klabra
11 //
12 
13 #pragma once
14 
15 // System includes
16 #include <string>
17 #include <iostream>
18 #include <cmath>
19 #include <type_traits>
20 
21 // External includes
22 
23 // Project includes
24 #include "search_structure.h"
27 
28 namespace Kratos
29 {
32 
36 
40 
44 
48 
50 
52 template<
53 std::size_t TDimension,
54  class TPointType,
55  class TPointerType,
56  class TIteratorType,
57  class TDistanceIteratorType,
58  class TIteratorIteratorType = typename std::vector<TIteratorType>::iterator
59  >
60 class TreeNode
61 {
62 public:
63 
65 
68 
70  using SizeType = std::size_t;
71 
73  using IndexType = std::size_t;
74 
77 
79  using PointType = TPointType;
80 
82  using PointerType = TPointerType;
83 
85  using IteratorType = TIteratorType;
86 
88  using DistanceIteratorType = TDistanceIteratorType;
89 
92 
94  using IteratorIteratorType = typename std::vector<IteratorType>::iterator;
95 
98 
99  virtual void PrintData(std::ostream& rOStream, std::string const& Perfix = std::string()) const {}
100 
101  TreeNode() {}
102 
103  virtual ~TreeNode() {}
104 
105  virtual void SearchNearestPoint(PointType const& ThisPoint, PointerType& rResult, CoordinateType& rResultDistance) {}
106 
107  virtual void SearchNearestPoint(PointType const& ThisPoint, PointerType& rResult, CoordinateType& rResultDistance,
108  SearchStructureType& Auxiliar) {}
109 
110  virtual void SearchInRadius(PointType const& ThisPoint, CoordinateType const& Radius, CoordinateType const& Radius2, IteratorType& Results,
111  DistanceIteratorType& ResultsDistances, SizeType& NumberOfResults, SizeType const& MaxNumberOfResults)
112  {
113  // must be implemented in derived classes.
114  return;
115  }
116 
117  virtual void SearchInRadius(PointType const& ThisPoint, CoordinateType const& Radius, CoordinateType const& Radius2, IteratorType& Results,
118  DistanceIteratorType& ResultsDistances, SizeType& NumberOfResults, SizeType const& MaxNumberOfResults, SearchStructureType& Auxiliar)
119  {
120  // must be implemented in derived classes.
121  return;
122  }
123 
124  virtual void SearchInRadius(PointType const& ThisPoint, CoordinateType const& Radius, CoordinateType const& Radius2, IteratorType& Results,
125  SizeType& NumberOfResults, SizeType const& MaxNumberOfResults)
126  {
127  // must be implemented in derived classes.
128  return;
129  }
130 
131  virtual void SearchInRadius(PointType const& ThisPoint, CoordinateType const& Radius, CoordinateType const& Radius2, IteratorType& Results,
132  SizeType& NumberOfResults, SizeType const& MaxNumberOfResults, SearchStructureType& Auxiliar)
133  {
134  // must be implemented in derived classes.
135  return;
136  }
137 
138  virtual void SearchInBox(PointType const& SearchMinPoint, PointType const& SearchMaxPoint, IteratorType& Results, SizeType& NumberOfResults,
139  SizeType const& MaxNumberOfResults ) // This corresponds with a AABB bounding-box
140  {
141  // must be implemented in derived classes.
142  return;
143  }
144 
145  // Note: Add OBB bounding-box and K-DOP bounding-box
146 
148  {
149  return msNull;
150  }
151 
153  {
154  return msNullPointer;
155  }
156 
157  static TreeNode& NullLeaf()
158  {
159  return msNullLeaf;
160  }
161 
162 private:
163  static IteratorType msNull;
164  static PointerType msNullPointer;
165  static TreeNode msNullLeaf;
166 };
167 
168 template<std::size_t TDimension, class TPointType, class TPointerType, class TIteratorType, class TDistanceIteratorType, class TIteratorIteratorType>
170 TreeNode<TDimension, TPointType, TPointerType, TIteratorType, TDistanceIteratorType, TIteratorIteratorType>::msNull;
171 
172 template<std::size_t TDimension, class TPointType, class TPointerType, class TIteratorType, class TDistanceIteratorType, class TIteratorIteratorType>
174 TreeNode<TDimension, TPointType, TPointerType, TIteratorType, TDistanceIteratorType, TIteratorIteratorType>::msNullPointer;
175 
176 template<std::size_t TDimension, class TPointType, class TPointerType, class TIteratorType, class TDistanceIteratorType, class TIteratorIteratorType>
177 TreeNode<TDimension, TPointType, TPointerType, TIteratorType, TDistanceIteratorType, TIteratorIteratorType>
178 TreeNode<TDimension, TPointType, TPointerType, TIteratorType, TDistanceIteratorType, TIteratorIteratorType>::msNullLeaf;
179 
188 template< class TPartitionType >
189 class Tree
190 {
191 public:
194 
195  // Helper template to extract ObjectType or default to void
196  template <typename T, typename = void>
197  struct GetObjectType {
198  using type = void;
199  };
200 
201  template <typename T>
202  struct GetObjectType<T, std::void_t<typename T::ObjectType>> {
203  using type = typename T::ObjectType;
204  };
205 
211  {
212  public:
217  explicit Partitions( const std::size_t NumPartitions ) : mNumPartitions(NumPartitions) {}
218 
221 
223  std::size_t mNumPartitions;
224  };
225 
228 
230  using PartitionType = TPartitionType;
231 
233  using LeafType = typename PartitionType::LeafType;
234 
237 
240 
243 
245  using DistanceIteratorType = typename PartitionType::DistanceIteratorType;
246 
248  using PointerType = typename PartitionType::PointerType;
249 
251  using DistanceFunction = typename PartitionType::DistanceFunction;
252 
254  static constexpr std::size_t Dimension = PartitionType::Dimension;
255 
258 
261 
263  using SizeType = typename NodeType::SizeType;
264 
266  using IndexType = typename NodeType::IndexType;
267 
269  using SearchStructureType = typename PartitionType::SearchStructureType;
270 
274 
282  IteratorType itPointsBegin,
283  IteratorType itPointsEnd,
284  SizeType BucketSize = 1
285  ) : mBucketSize(BucketSize),
286  mitPointsBegin(itPointsBegin),
287  mitPointsEnd(itPointsEnd)
288  {
289  if(mitPointsBegin == mitPointsEnd)
290  return;
291 
292  // The BB points
293  auto& r_high_point = BoundingBoxHighPoint();
294  auto& r_low_point = BoundingBoxLowPoint();
295 
296  for(SizeType i = 0 ; i < Dimension ; i++) {
297  r_high_point[i] = (**mitPointsBegin)[i];
298  r_low_point[i] = (**mitPointsBegin)[i];
299  }
300 
301  for(IteratorType point_iterator = mitPointsBegin ; point_iterator != mitPointsEnd ; point_iterator++) {
302  for(SizeType i = 0 ; i < Dimension ; i++) {
303  if((**point_iterator)[i] > r_high_point[i]) {
304  r_high_point[i] = (**point_iterator)[i];
305  } else if((**point_iterator)[i] < r_low_point[i]) {
306  r_low_point[i] = (**point_iterator)[i];
307  }
308  }
309  }
310 
311  mRoot = TPartitionType::Construct(mitPointsBegin, mitPointsEnd, r_high_point, r_low_point, mBucketSize);
312  }
313 
321  IteratorType itPointsBegin,
322  IteratorType itPointsEnd,
323  Partitions Parts
324  ) : mitPointsBegin(itPointsBegin),
325  mitPointsEnd(itPointsEnd)
326  {
327  if(mitPointsBegin == mitPointsEnd)
328  return;
329 
330  // The BB points
331  auto& r_high_point = BoundingBoxHighPoint();
332  auto& r_low_point = BoundingBoxLowPoint();
333 
334  const SizeType NumPoints = SearchUtils::PointerDistance(mitPointsBegin,mitPointsEnd);
335  mBucketSize = static_cast<std::size_t>( (double) NumPoints / (double) Parts.mNumPartitions ) + 1;
336 
337  r_high_point = **mitPointsBegin;
338  r_low_point = **mitPointsBegin;
339  for(IteratorType point_iterator = mitPointsBegin ; point_iterator != mitPointsEnd ; point_iterator++) {
340  for(SizeType i = 0 ; i < Dimension ; i++) {
341  if((**point_iterator)[i] > r_high_point[i]) {
342  r_high_point[i] = (**point_iterator)[i];
343  } else if((**point_iterator)[i] < r_low_point[i]) {
344  r_low_point[i] = (**point_iterator)[i];
345  }
346  }
347  }
348 
349  mRoot = TPartitionType::Construct(mitPointsBegin, mitPointsEnd, r_high_point, r_low_point, mBucketSize);
350  }
351 
353  virtual ~Tree()
354  {
355  delete mRoot;
356  }
357 
361 
365 
373  PointerType const& ThisPoint,
374  CoordinateType const Tolerance = static_cast<CoordinateType>(10.0*DBL_EPSILON)
375  )
376  {
377  PointerType Result = *mitPointsBegin;
378  CoordinateType ResultDistance = static_cast<CoordinateType>(DBL_MAX);
379  // Searching the tree
380  mRoot->SearchNearestPoint(*ThisPoint, Result, ResultDistance);
381  if (ResultDistance<Tolerance*Tolerance)
382  return Result;
383  return NodeType::NullPointer();
384  }
385 
392  PointType const& ThisPoint,
393  CoordinateType& rResultDistance
394  )
395  {
396  PointerType Result = *mitPointsBegin;
397  rResultDistance = static_cast<CoordinateType>(DBL_MAX); // DistanceFunction()(ThisPoint,**mitPointsBegin);
398 
399  // Searching the tree
400  mRoot->SearchNearestPoint(ThisPoint, Result, rResultDistance);
401 
402  return Result;
403  }
404 
415  {
416  PointerType Result = *mitPointsBegin; // NULL ??
417  CoordinateType rResultDistance = static_cast<CoordinateType>(DBL_MAX); // DistanceFunction()(ThisPoint,**mitPointsBegin);
418 
419  // Searching the tree
420  mRoot->SearchNearestPoint(ThisPoint, Result, rResultDistance);
421 
422  return Result;
423  }
424 
435  PointType const& ThisPoint,
436  CoordinateType Radius,
437  IteratorType Results,
438  DistanceIteratorType ResultsDistances,
439  SizeType MaxNumberOfResults
440  )
441  {
442  // Using the square of radius for avoiding square root calculation during search
443  const CoordinateType Radius2 = Radius * Radius;
444 
445  // Searching the tree
446  SizeType NumberOfResults = 0;
447  mRoot->SearchInRadius(ThisPoint, Radius, Radius2, Results, ResultsDistances, NumberOfResults, MaxNumberOfResults);
448 
449  return NumberOfResults;
450  }
451 
461  PointType const& ThisPoint,
462  CoordinateType Radius,
463  IteratorType Results,
464  SizeType MaxNumberOfResults
465  )
466  {
467  // Using the square of radius for avoiding square root calculation during search
468  const CoordinateType Radius2 = Radius * Radius;
469 
470  // Searching the tree
471  SizeType NumberOfResults = 0;
472  mRoot->SearchInRadius(ThisPoint, Radius, Radius2, Results, NumberOfResults, MaxNumberOfResults);
473  return NumberOfResults;
474  }
475 
485  PointType const& MinPointBox,
486  PointType const& MaxPointBox,
487  IteratorType Results,
488  SizeType MaxNumberOfResults
489  )
490  {
491  SizeType NumberOfResults = 0;
492  mRoot->SearchInBox(MinPointBox,MaxPointBox,Results,NumberOfResults,MaxNumberOfResults);
493  return NumberOfResults;
494  }
495 
499 
505  {
506  return mBoundingBox.GetMinPoint();
507  }
508 
514  {
515  return mBoundingBox.GetMaxPoint();
516  }
517 
524  {
525  return mBoundingBox;
526  }
527 
531 
535 
537  virtual std::string Info() const
538  {
539  return "Tree";
540  }
541 
543  virtual void PrintInfo(std::ostream& rOStream) const
544  {
545  rOStream << "Tree";
546  }
547 
549  virtual void PrintData(std::ostream& rOStream, std::string const& Perfix = std::string()) const
550  {
551  mRoot->PrintData(rOStream, " ");
552  }
553 
557 
559 private:
562 
563  static LeafType msEmptyLeaf;
564 
568 
569  SizeType mBucketSize;
570 
571  BoundingBox<PointType> mBoundingBox;
572 
573  IteratorType mitPointsBegin;
574  IteratorType mitPointsEnd;
575 
576  NodeType* mRoot;
577 
581 
585 
589 
593 
597 
599  Tree& operator=(Tree const& rOther);
600 
602  Tree(Tree const& rOther);
603 
605 }; // Class Tree
606 
607 template< class TPartitionType >
608 typename Tree<TPartitionType>::LeafType Tree<TPartitionType>::msEmptyLeaf;
609 
613 
617 
619 template<class TPartitionType>
620 inline std::istream& operator >> (std::istream& rIStream, Tree<TPartitionType>& rThis);
621 
623 template<class TPartitionType>
624 inline std::ostream& operator << (std::ostream& rOStream, const Tree<TPartitionType>& rThis)
625 {
626  rThis.PrintInfo(rOStream);
627  rOStream << std::endl;
628  rThis.PrintData(rOStream);
629 
630  return rOStream;
631 }
633 
634 } // namespace Kratos.
TPointType & GetMinPoint()
Gets a reference to the minimum point.
Definition: bounding_box.h:179
TPointType & GetMaxPoint()
Gets a reference to the maximum point.
Definition: bounding_box.h:199
This class defines the node.
Definition: node.h:65
Definition: search_structure.h:309
Class to represent partitions for the tree.
Definition: tree.h:211
Partitions(const std::size_t NumPartitions)
Constructor to initialize the number of partitions.
Definition: tree.h:217
std::size_t mNumPartitions
The number of partitions.
Definition: tree.h:220
~Partitions()
Destructor.
Definition: tree.h:220
A generic tree data structure for spatial partitioning.
Definition: tree.h:190
virtual void PrintData(std::ostream &rOStream, std::string const &Perfix=std::string()) const
Print object's data.
Definition: tree.h:549
SizeType SearchInBox(PointType const &MinPointBox, PointType const &MaxPointBox, IteratorType Results, SizeType MaxNumberOfResults)
Search for points within a given axis-aligned box.
Definition: tree.h:484
BoundingBox< PointType > & GetBoundingBox()
Get the bounding box.
Definition: tree.h:523
typename PartitionType::SearchStructureType SearchStructureType
The search structure type definition.
Definition: tree.h:269
typename NodeType::SizeType SizeType
The size type definition.
Definition: tree.h:263
typename NodeType::IndexType IndexType
The index type definition.
Definition: tree.h:266
typename PartitionType::PointType PointType
The point type definition.
Definition: tree.h:236
static constexpr std::size_t Dimension
Dimension definition.
Definition: tree.h:254
typename PartitionType::DistanceFunction DistanceFunction
The distance function type definition.
Definition: tree.h:251
Tree(IteratorType itPointsBegin, IteratorType itPointsEnd, SizeType BucketSize=1)
Construct a new Tree object.
Definition: tree.h:281
PointerType SearchNearestPoint(PointType const &ThisPoint, CoordinateType &rResultDistance)
Search for the nearest point to a given point.
Definition: tree.h:391
typename PartitionType::LeafType LeafType
The leaf type definition.
Definition: tree.h:233
PointType & BoundingBoxHighPoint()
Get a reference to the high point of the bounding box.
Definition: tree.h:513
virtual std::string Info() const
Turn back information as a string.
Definition: tree.h:537
typename PartitionType::IteratorType IteratorType
The iterator type definition.
Definition: tree.h:239
typename PartitionType::PointerType PointerType
The pointer type definition.
Definition: tree.h:248
PointType & BoundingBoxLowPoint()
Get a reference to the low point of the bounding box.
Definition: tree.h:504
SizeType SearchInRadius(PointType const &ThisPoint, CoordinateType Radius, IteratorType Results, DistanceIteratorType ResultsDistances, SizeType MaxNumberOfResults)
Search for points within a given radius of a point.
Definition: tree.h:434
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: tree.h:543
KRATOS_CLASS_POINTER_DEFINITION(Tree)
Pointer definition of Tree.
typename NodeType::CoordinateType CoordinateType
The coordinate type definition.
Definition: tree.h:260
SizeType SearchInRadius(PointType const &ThisPoint, CoordinateType Radius, IteratorType Results, SizeType MaxNumberOfResults)
Search for points within a given radius of a point.
Definition: tree.h:460
virtual ~Tree()
Destructor.
Definition: tree.h:353
Tree(IteratorType itPointsBegin, IteratorType itPointsEnd, Partitions Parts)
Construct a new Tree object.
Definition: tree.h:320
TPartitionType PartitionType
The partition type definition.
Definition: tree.h:230
PointerType SearchNearestPoint(PointType const &ThisPoint)
Search for points within a given radius of a point.
Definition: tree.h:414
PointerType ExistPoint(PointerType const &ThisPoint, CoordinateType const Tolerance=static_cast< CoordinateType >(10.0 *DBL_EPSILON))
Check if a point exists in the tree within a given tolerance.
Definition: tree.h:372
typename PartitionType::DistanceIteratorType DistanceIteratorType
The distance iterator type definition.
Definition: tree.h:245
typename GetObjectType< PointType >::type ObjectType
The object type.
Definition: tree.h:242
Short class definition.
Definition: tree.h:61
KRATOS_CLASS_POINTER_DEFINITION(TreeNode)
Pointer definition of TreeNode.
TPointerType PointerType
Define PointerType as TPointerType.
Definition: tree.h:82
virtual void SearchInBox(PointType const &SearchMinPoint, PointType const &SearchMaxPoint, IteratorType &Results, SizeType &NumberOfResults, SizeType const &MaxNumberOfResults)
Definition: tree.h:138
static TreeNode & NullLeaf()
Definition: tree.h:157
TDistanceIteratorType DistanceIteratorType
Define DistanceIteratorType as TDistanceIteratorType.
Definition: tree.h:88
virtual void SearchInRadius(PointType const &ThisPoint, CoordinateType const &Radius, CoordinateType const &Radius2, IteratorType &Results, SizeType &NumberOfResults, SizeType const &MaxNumberOfResults)
Definition: tree.h:124
static IteratorType & NullIterator()
Definition: tree.h:147
virtual void SearchNearestPoint(PointType const &ThisPoint, PointerType &rResult, CoordinateType &rResultDistance, SearchStructureType &Auxiliar)
Definition: tree.h:107
virtual void SearchNearestPoint(PointType const &ThisPoint, PointerType &rResult, CoordinateType &rResultDistance)
Definition: tree.h:105
TreeNode()
Definition: tree.h:101
typename std::vector< IteratorType >::iterator IteratorIteratorType
Define IteratorIteratorType as an iterator type for a vector of IteratorType.
Definition: tree.h:94
TIteratorType IteratorType
Define IteratorType as TIteratorType.
Definition: tree.h:85
virtual void SearchInRadius(PointType const &ThisPoint, CoordinateType const &Radius, CoordinateType const &Radius2, IteratorType &Results, DistanceIteratorType &ResultsDistances, SizeType &NumberOfResults, SizeType const &MaxNumberOfResults)
Definition: tree.h:110
virtual ~TreeNode()
Definition: tree.h:103
TPointType PointType
Define PointType as TPointType.
Definition: tree.h:79
double CoordinateType
Define CoordinateType as double.
Definition: tree.h:76
virtual void SearchInRadius(PointType const &ThisPoint, CoordinateType const &Radius, CoordinateType const &Radius2, IteratorType &Results, SizeType &NumberOfResults, SizeType const &MaxNumberOfResults, SearchStructureType &Auxiliar)
Definition: tree.h:131
virtual void PrintData(std::ostream &rOStream, std::string const &Perfix=std::string()) const
Definition: tree.h:99
std::size_t IndexType
Define IndexType as std::size_t.
Definition: tree.h:73
std::size_t SizeType
Define SizeType as std::size_t.
Definition: tree.h:70
static PointerType & NullPointer()
Definition: tree.h:152
virtual void SearchInRadius(PointType const &ThisPoint, CoordinateType const &Radius, CoordinateType const &Radius2, IteratorType &Results, DistanceIteratorType &ResultsDistances, SizeType &NumberOfResults, SizeType const &MaxNumberOfResults, SearchStructureType &Auxiliar)
Definition: tree.h:117
std::size_t PointerDistance(TPointerType const &PointerBegin, TPointerType const &PointerEnd)
Definition: search_structure.h:91
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::size_t SizeType
The definition of the size type.
Definition: mortar_classes.h:43
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
TABLE_NUMBER_ANGULAR_VELOCITY TABLE_NUMBER_MOMENT I33 BEAM_INERTIA_ROT_UNIT_LENGHT_Y KRATOS_DEFINE_APPLICATION_VARIABLE(DEM_APPLICATION, double, BEAM_INERTIA_ROT_UNIT_LENGHT_Z) typedef std double
Definition: DEM_application_variables.h:182
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
namespace
Definition: array_1d.h:793
integer i
Definition: TensorModule.f:17
#define DBL_MAX
Definition: search_structure.h:23
Definition: tree.h:197
void type
Definition: tree.h:198
Configure::IteratorType IteratorType
Definition: transfer_utility.h:249
Configure::PointType PointType
Definition: transfer_utility.h:245