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.
octree.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: clabra
11 //
12 
13 #pragma once
14 
15 // System includes
16 #include <cstddef>
17 
18 // External includes
19 
20 // Project includes
21 #include "tree.h"
22 
23 namespace Kratos
24 {
27 
31 
35 
39 
43 
44 template< std::size_t Dimension, class PointType, class IteratorType, class CoordinateType >
46 {
47 public:
48  void operator()( PointType& Position, PointType const& MinPoint, PointType const& MaxPoint, IteratorType const& PointsBegin, IteratorType const& PointsEnd )
49  {
50  for(std::size_t i = 0 ; i < Dimension ; i++)
51  Position[i] = 0.0;
52  for(IteratorType i_point = PointsBegin ; i_point < PointsEnd ; i_point++)
53  for(std::size_t i = 0 ; i < Dimension ; i++)
54  Position[i] += (**i_point)[i];
55  for(std::size_t i = 0 ; i < Dimension ; i++)
56  Position[i] /= static_cast<CoordinateType>(SearchUtils::PointerDistance(PointsBegin,PointsEnd));
57  }
58 };
59 
60 
61 template< std::size_t Dimension, class PointType, class IteratorType, class CoordinateType >
63 {
64 public:
65  void operator()( PointType& Position, PointType const& MinPoint, PointType const& MaxPoint, IteratorType const& PointsBegin, IteratorType const& PointsEnd )
66  {
67  // calculating the partition postion as midpoint of box
68  for(std::size_t i = 0 ; i < Dimension ; i++)
69  Position[i] = (MaxPoint[i] + MinPoint[i]) * 0.500;
70  }
71 };
72 
73 
74 
76 
78 template< class TLeafType >
79 class OCTreePartition : public TreeNode< TLeafType::Dimension,
80  typename TLeafType::PointType,
81  typename TLeafType::PointerType,
82  typename TLeafType::IteratorType,
83  typename TLeafType::DistanceIteratorType >
84 {
85 public:
88 
91 
92  typedef TLeafType LeafType;
93 
94  typedef typename LeafType::PointType PointType;
95 
97 
99 
100  typedef typename LeafType::DistanceIteratorType DistanceIteratorType;
101 
102  typedef typename LeafType::PointerType PointerType;
103 
104  typedef typename LeafType::DistanceFunction DistanceFunction;
105 
106  enum { Dimension = LeafType::Dimension };
107 
109 
111 
113 
115 
118 
119  typedef typename LeafType::SearchStructureType SearchStructureType;
120 
121  static const SizeType number_of_childs = 1 << Dimension;
122 
123 
127 
128 
130  OCTreePartition(IteratorType PointsBegin, IteratorType PointsEnd,
131  PointType const& MinPoint, PointType const& MaxPoint, SizeType BucketSize = 1)
132  {
133  /* const SizeType number_of_childs = 8; */
134  PointType mid_cell_lenght;
135 
136  SizeType TempSize = SearchUtils::PointerDistance(PointsBegin,PointsEnd);
137  PointerType* Temp = new PointerType[ TempSize ];
138 
139  // Template definition of SplitMode
140  // SplitMode()(mPostion,MinPoint,MaxPoint,mPointBegin,mPointEnd);
141  AverageSplit()(mPosition,MinPoint,MaxPoint,PointsBegin,PointsEnd);
142 
143  SizeType cell_sizes[number_of_childs];
144 
145  IteratorType cell_position[number_of_childs];
146 
147  for(IndexType i = 0 ; i < number_of_childs ; i++)
148  cell_sizes[i] = 0;
149 
150  PointerType* i_temp = Temp;
151  for(IteratorType i_point = PointsBegin ; i_point < PointsEnd ; i_point++, i_temp++)
152  {
153  *i_temp = *i_point;
154  IndexType child_index = GetChildIndex(**i_point);
155  cell_sizes[child_index]++;
156  }
157 
158  cell_position[0] = PointsBegin;
159  for(IndexType i = 1 ; i < number_of_childs ; i++)
160  {
161  cell_position[i] = cell_position[i-1];
162  std::advance(cell_position[i], cell_sizes[i-1]);
163  }
164 
165  // for(IteratorType i_point = PointsBegin ; i_point < PointsEnd ; i_point++)
166  // {
167  // IndexType child_index = GetChildIndex(**i_point);
168  // *(cell_position[child_index]++) = *i_point;
169  // //swap(*i_point, *(cell_position[child_index]++));
170  // }
171  // The same in Bin_Static ( use temporal array for reorder the list )
172  for(PointerType* i_point = Temp ; i_point < Temp+TempSize ; i_point++)
173  {
174  IndexType child_index = GetChildIndex(**i_point);
175  *(cell_position[child_index]++) = *i_point;
176  }
177 
178  // Creatig the first child
179  if(cell_sizes[0] > BucketSize)
180  mpChilds[0]= new OCTreePartition(PointsBegin, cell_position[0], MinPoint, mPosition, BucketSize);
181  else
182  mpChilds[0]= new LeafType(PointsBegin, cell_position[0]);
183 
184  PointType new_min_point;
185  PointType new_max_point;
186 
187  // Creating the rest of the childs
188  for(IndexType i = 1 ; i < number_of_childs ; i++)
189  {
190  if(cell_sizes[i] > BucketSize)
191  {
192  IndexType factor = 1;
193  for(IndexType j = 0 ; j < Dimension ; j++)
194  {
195  if(i & factor)
196  {
197  new_min_point[j] = mPosition[j];
198  new_max_point[j] = MaxPoint[j];
199  }
200  else
201  {
202  new_min_point[j] = MinPoint[j];
203  new_max_point[j] = mPosition[j];
204  }
205  factor <<= 1;
206  }
207 
208  mpChilds[i]= new OCTreePartition(cell_position[i-1], cell_position[i],
209  new_min_point , new_max_point, BucketSize);
210  }
211  else
212  mpChilds[i]= new LeafType(cell_position[i-1], cell_position[i]);
213 
214  }
215 
216  }
217 
218  void PrintData(std::ostream& rOStream, std::string const& Perfix = std::string()) const override
219  {
220  rOStream << Perfix << "Partition at point (" << mPosition[0];
221  for(IndexType j = 0 ; j < Dimension - 1 ; j++)
222  rOStream << "," << mPosition[j];
223  rOStream << std::endl;
224 
225  for(SizeType j = 0 ; j < number_of_childs ; j++)
226  mpChilds[j]->PrintData(rOStream, Perfix + " ");
227 
228  }
229 
232  {
233  /* SizeType number_of_childs = 8; */
234  for(SizeType i = 0 ; i < number_of_childs ; i++)
235  delete mpChilds[i];
236  }
237 
241 
242  void SearchNearestPoint(PointType const& ThisPoint, PointerType& Result, CoordinateType& rResultDistance) override
243  {
244  CoordinateType distances_to_partitions[number_of_childs];
245 
246  SizeType child_index = GetChildIndex(ThisPoint);
247 
248  mpChilds[child_index]->SearchNearestPoint(ThisPoint, Result, rResultDistance);
249 
250  DistanceToPartitions(child_index, ThisPoint, distances_to_partitions);
251 
252  for(SizeType i = 0 ; i < number_of_childs ; i++)
253  if((i != child_index) && (distances_to_partitions[i] < rResultDistance))
254  mpChilds[i]->SearchNearestPoint(ThisPoint, Result, rResultDistance);
255 
256  }
257 
258  void SearchInRadius(PointType const& ThisPoint, CoordinateType const& Radius, CoordinateType const& Radius2, IteratorType& Results,
259  DistanceIteratorType& ResultsDistances, SizeType& NumberOfResults, SizeType const& MaxNumberOfResults) override
260  {
261  SizeType child_index = GetChildIndex(ThisPoint);
262 
263  // n is number of points found
264  mpChilds[child_index]->SearchInRadius(ThisPoint, Radius, Radius2, Results, ResultsDistances, NumberOfResults, MaxNumberOfResults);
265 
266  CoordinateType distances_to_partitions[number_of_childs];
267 
268  DistanceToPartitions(child_index, ThisPoint, distances_to_partitions);
269 
270  for(IndexType i = 0 ; i < number_of_childs ; i++)
271  if((i != child_index) && (distances_to_partitions[i] < Radius2))
272  mpChilds[i]->SearchInRadius(ThisPoint, Radius, Radius2, Results, ResultsDistances, NumberOfResults, MaxNumberOfResults);
273  }
274 
275  void SearchInRadius(PointType const& ThisPoint, CoordinateType const& Radius, CoordinateType const& Radius2, IteratorType& Results,
276  SizeType& NumberOfResults, SizeType const& MaxNumberOfResults) override
277  {
278  SizeType child_index = GetChildIndex(ThisPoint);
279 
280  // n is number of points found
281  mpChilds[child_index]->SearchInRadius(ThisPoint, Radius, Radius2, Results, NumberOfResults, MaxNumberOfResults);
282 
283  CoordinateType distances_to_partitions[number_of_childs];
284 
285  DistanceToPartitions(child_index, ThisPoint, distances_to_partitions);
286 
287  for(IndexType i = 0 ; i < number_of_childs ; i++)
288  if((i != child_index) && (distances_to_partitions[i] < Radius2))
289  mpChilds[i]->SearchInRadius(ThisPoint, Radius, Radius2, Results, NumberOfResults, MaxNumberOfResults);
290  }
291 
292 
293 private:
294 
295  IndexType GetChildIndex(PointType const& rThisPoint) const
296  {
297  // Calculating the cell index
298  IndexType child_index = 0;
299  const IndexType dim_mask[] = { 1, 2, 4 };
300 
301  for( IndexType i = 0 ; i < Dimension ; i++)
302  if( rThisPoint[i] >= mPosition[i] ) child_index += dim_mask[i];
303 
304  return child_index;
305  }
306 
307  void DistanceToPartitions(IndexType ContainingChildIndex, PointType const& rThisPoint, CoordinateType rDistances[]) const
308  {
309  const IndexType coordinate_mask[] = { 1, 2, 4 };
310 
311  CoordinateType offset_from_postition[Dimension];
312 
313  for(IndexType j = 0 ; j < Dimension ; j++)
314  {
315  CoordinateType temp = rThisPoint[j] - mPosition[j];
316  offset_from_postition[j] = temp*temp;
317  }
318 
319  for(IndexType i = 0 ; i < number_of_childs ; i++)
320  {
321  rDistances[i] = 0.00; //ResidualDistance;
322 
323  IndexType partitions = ContainingChildIndex^i;
324 
325  for(IndexType j = 0 ; j < Dimension ; j++)
326  //rDistances[i] += ((coordinate_mask[j] & partitions) >> j) * offset_from_postition[j];
327  if(coordinate_mask[j] & partitions) rDistances[i] += offset_from_postition[j];
328  }
329 
330  }
331 
332 private:
333 
334  IndexType mCutingDimension;
335  PointType mPosition; // Position of partition
336 
337  TreeNodeType* mpChilds[8]; // 8 is number of childs
338 
339 
340 
341 public:
342  static TreeNodeType* Construct(IteratorType PointsBegin,
343  IteratorType PointsEnd,
344  PointType HighPoint,
345  PointType LowPoint,
346  SizeType BucketSize)
347  {
348  SizeType number_of_points = SearchUtils::PointerDistance(PointsBegin,PointsEnd);
349  if (number_of_points == 0)
350  return NULL;
351  else if (number_of_points <= BucketSize)
352  {
353  return new LeafType(PointsBegin, PointsEnd);
354  }
355  else
356  {
357  return new OCTreePartition(PointsBegin, PointsEnd, LowPoint, HighPoint, BucketSize);
358  }
359  }
360 
361 };
362 
363 
364 } // namespace Kratos.
365 
366 
Short class definition.
Definition: octree.h:84
LeafType::SearchStructureType SearchStructureType
Definition: octree.h:119
OcTreeMidPointSplit< Dimension, PointType, IteratorType, CoordinateType > MidPointSplit
Definition: octree.h:117
TLeafType LeafType
Definition: octree.h:92
void SearchNearestPoint(PointType const &ThisPoint, PointerType &Result, CoordinateType &rResultDistance) override
Definition: octree.h:242
TreeNodeType::SizeType SizeType
Definition: octree.h:112
LeafType::PointerType PointerType
Definition: octree.h:102
LeafType::DistanceFunction DistanceFunction
Definition: octree.h:104
static const SizeType number_of_childs
Definition: octree.h:121
static TreeNodeType * Construct(IteratorType PointsBegin, IteratorType PointsEnd, PointType HighPoint, PointType LowPoint, SizeType BucketSize)
Definition: octree.h:342
LeafType::PointType PointType
Definition: octree.h:94
OCTreePartition(IteratorType PointsBegin, IteratorType PointsEnd, PointType const &MinPoint, PointType const &MaxPoint, SizeType BucketSize=1)
Partition constructor.
Definition: octree.h:130
void PrintData(std::ostream &rOStream, std::string const &Perfix=std::string()) const override
Definition: octree.h:218
LeafType::ContainerType ContainerType
Definition: octree.h:96
void SearchInRadius(PointType const &ThisPoint, CoordinateType const &Radius, CoordinateType const &Radius2, IteratorType &Results, SizeType &NumberOfResults, SizeType const &MaxNumberOfResults) override
Definition: octree.h:275
LeafType::IteratorType IteratorType
Definition: octree.h:98
@ Dimension
Definition: octree.h:106
TreeNodeType::IndexType IndexType
Definition: octree.h:114
virtual ~OCTreePartition()
Destructor.
Definition: octree.h:231
void SearchInRadius(PointType const &ThisPoint, CoordinateType const &Radius, CoordinateType const &Radius2, IteratorType &Results, DistanceIteratorType &ResultsDistances, SizeType &NumberOfResults, SizeType const &MaxNumberOfResults) override
Definition: octree.h:258
LeafType::DistanceIteratorType DistanceIteratorType
Definition: octree.h:100
KRATOS_CLASS_POINTER_DEFINITION(OCTreePartition)
Pointer definition of KDTree.
OcTreeAverageSplit< Dimension, PointType, IteratorType, CoordinateType > AverageSplit
Definition: octree.h:116
TreeNodeType::CoordinateType CoordinateType
Definition: octree.h:110
TreeNode< Dimension, PointType, PointerType, IteratorType, DistanceIteratorType > TreeNodeType
Definition: octree.h:108
Definition: octree.h:46
void operator()(PointType &Position, PointType const &MinPoint, PointType const &MaxPoint, IteratorType const &PointsBegin, IteratorType const &PointsEnd)
Definition: octree.h:48
Definition: octree.h:63
void operator()(PointType &Position, PointType const &MinPoint, PointType const &MaxPoint, IteratorType const &PointsBegin, IteratorType const &PointsEnd)
Definition: octree.h:65
Point class.
Definition: point.h:59
Short class definition.
Definition: tree.h:61
virtual void SearchNearestPoint(PointType const &ThisPoint, PointerType &rResult, CoordinateType &rResultDistance)
Definition: tree.h:105
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
double CoordinateType
Define CoordinateType as double.
Definition: tree.h:76
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
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
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
Temp
Definition: PecletTest.py:105
int j
Definition: quadrature.py:648
float temp
Definition: rotating_cone.py:85
integer i
Definition: TensorModule.f:17
float factor
for node in (self.combined_model_part).Nodes: pold = node.GetSolutionStepValue(PRESSURE,...
Definition: ulf_PGLASS.py:254
Configure::IteratorType IteratorType
Definition: transfer_utility.h:249
Configure::PointType PointType
Definition: transfer_utility.h:245
Configure::ContainerType ContainerType
Definition: transfer_utility.h:247