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.
geometrical_objects_bins.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: Pooyan Dadvand
11 //
12 
13 #pragma once
14 
15 // System includes
16 #include <unordered_set>
17 
18 // External includes
19 
20 // Project includes
22 #include "geometries/point.h"
24 
25 namespace Kratos
26 {
29 
32 
33 class GeometricalObject; // forward declaration, to be included in the cpp. This is needed to reduce the compilation time. Can be done as we consider the GeometricalObject as a pointer
34 
45 class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins
46 {
47 public:
50 
53 
55  using PointType = Point;
56 
59 
61  using CellType = std::vector<GeometricalObject*>;
63 
67 
74  template<typename TIteratorType>
76  TIteratorType GeometricalObjectsBegin,
77  TIteratorType GeometricalObjectsEnd,
78  const double Tolerance = 1e-12
79  )
80  {
81  mTolerance = Tolerance;
82  const std::size_t number_of_objects = std::distance(GeometricalObjectsBegin, GeometricalObjectsEnd);
83  if (number_of_objects > 0){
84  mBoundingBox.Set(GeometricalObjectsBegin->GetGeometry().begin(), GeometricalObjectsBegin->GetGeometry().end());
85  for (TIteratorType i_object = GeometricalObjectsBegin ; i_object != GeometricalObjectsEnd ; i_object++){
86  mBoundingBox.Extend(i_object->GetGeometry().begin() , i_object->GetGeometry().end());
87  }
88  mBoundingBox.Extend(Tolerance);
89  }
90  CalculateCellSize(number_of_objects);
91  mCells.resize(GetTotalNumberOfCells());
92  AddObjectsToCells(GeometricalObjectsBegin, GeometricalObjectsEnd);
93  }
94 
100  template<typename TContainer>
102  TContainer& rGeometricalObjectsVector,
103  const double Tolerance = 1e-12)
104  : GeometricalObjectsBins(rGeometricalObjectsVector.begin(), rGeometricalObjectsVector.end(), Tolerance)
105  {
106  }
107 
110 
114 
115 
119 
127  CellType& GetCell(
128  const std::size_t I,
129  const std::size_t J,
130  const std::size_t K
131  );
132 
140  BoundingBox<PointType> GetCellBoundingBox(
141  const std::size_t I,
142  const std::size_t J,
143  const std::size_t K
144  );
145 
153  void SearchInRadius(
154  const PointType& rPoint,
155  const double Radius,
156  std::vector<ResultType>& rResults
157  );
158 
168  template<typename TPointIteratorType>
170  TPointIteratorType itPointBegin,
171  TPointIteratorType itPointEnd,
172  const double Radius,
173  std::vector<std::vector<ResultType>>& rResults
174  )
175  {
176  const std::size_t number_of_points = std::distance(itPointBegin, itPointEnd);
177  rResults.resize(number_of_points);
178  for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){
179  SearchInRadius(*it_point, Radius, rResults[it_point - itPointBegin]);
180  }
181  }
182 
192  ResultType SearchNearestInRadius(
193  const PointType& rPoint,
194  const double Radius
195  );
196 
208  template<typename TPointIteratorType>
209  std::vector<ResultType> SearchNearestInRadius(
210  TPointIteratorType itPointBegin,
211  TPointIteratorType itPointEnd,
212  const double Radius
213  )
214  {
215  // Doing a vector of results
216  std::vector<ResultType> results;
217  const std::size_t number_of_points = std::distance(itPointBegin, itPointEnd);
218  results.resize(number_of_points);
219  for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){
220  results[it_point - itPointBegin] = SearchNearestInRadius(*it_point, Radius);
221  }
222  return results;
223  }
224 
232  ResultType SearchNearest(const PointType& rPoint);
233 
243  template<typename TPointIteratorType>
244  std::vector<ResultType> SearchNearest(
245  TPointIteratorType itPointBegin,
246  TPointIteratorType itPointEnd
247  )
248  {
249  // Doing a vector of results
250  std::vector<ResultType> results;
251  const std::size_t number_of_points = std::distance(itPointBegin, itPointEnd);
252  results.resize(number_of_points);
253  for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){
254  results[it_point - itPointBegin] = SearchNearest(*it_point);
255  }
256  return results;
257  }
258 
268  ResultType SearchIsInside(const PointType& rPoint);
269 
281  template<typename TPointIteratorType>
282  std::vector<ResultType> SearchIsInside(
283  TPointIteratorType itPointBegin,
284  TPointIteratorType itPointEnd
285  )
286  {
287  // Doing a vector of results
288  std::vector<ResultType> results;
289  const std::size_t number_of_points = std::distance(itPointBegin, itPointEnd);
290  results.resize(number_of_points);
291  for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){
292  results[it_point - itPointBegin] = SearchIsInside(*it_point);
293  }
294  return results;
295  }
296 
300 
306  return mBoundingBox;
307  }
308 
314  return mCellSizes;
315  }
316 
322  return mNumberOfCells;
323  }
324 
329  std::size_t GetTotalNumberOfCells(){
330  return mNumberOfCells[0] * mNumberOfCells[1] * mNumberOfCells[2];
331  }
332 
336 
337 
341 
343  virtual std::string Info() const
344  {
345  std::stringstream buffer;
346  buffer << "GeometricalObjectsBins" ;
347  return buffer.str();
348  }
349 
351  virtual void PrintInfo(std::ostream& rOStream) const {rOStream << "GeometricalObjectsBins";}
352 
354  virtual void PrintData(std::ostream& rOStream) const {}
355 
359 
361 protected:
364 
367 
371 
372  static constexpr unsigned int Dimension = 3;
373 
377 
382  std::vector<CellType> mCells;
383  double mTolerance;
384 
388 
394  bool PointIsInsideBoundingBox(const array_1d<double, 3>& rCoords);
395 
402  bool PointIsInsideBoundingBoxWithTolerance(
403  const array_1d<double, 3>& rCoords,
404  const double Tolerance
405  );
406 
412  void CalculateCellSize(const std::size_t NumberOfCells);
413 
421  template<typename TIteratorType>
423  TIteratorType GeometricalObjectsBegin,
424  TIteratorType GeometricalObjectsEnd
425  )
426  {
427  for(auto i_geometrical_object = GeometricalObjectsBegin ; i_geometrical_object != GeometricalObjectsEnd ; i_geometrical_object++){
428  array_1d<std::size_t, 3> min_position(3,0);
429  array_1d<std::size_t, 3> max_position(3,0);
430  CalculateMinMaxPositions(i_geometrical_object->GetGeometry(), min_position, max_position);
431  for(std::size_t k = min_position[2] ; k < max_position[2] ; k++){
432  for(std::size_t j = min_position[1] ; j < max_position[1] ; j++){
433  for(std::size_t i = min_position[0] ; i < max_position[0] ; i++){
434  auto cell_bounding_box = GetCellBoundingBox(i,j,k);
435  if(IsIntersected(i_geometrical_object->GetGeometry(), cell_bounding_box, mTolerance)){
436  GetCell(i,j,k).push_back(&(*i_geometrical_object));
437  }
438  }
439  }
440  }
441  }
442  }
443 
445 private:
448 
452 
462  template<typename TGeometryType>
463  void CalculateMinMaxPositions(
464  const TGeometryType& rGeometry,
465  array_1d<std::size_t, 3>& rMinPosition,
466  array_1d<std::size_t, 3>& rMaxPosition
467  )
468  {
469  if(rGeometry.empty())
470  return;
471 
472  BoundingBox<PointType> bounding_box(rGeometry.begin(), rGeometry.end());
473 
474  for(unsigned int i = 0; i < 3; i++ ) {
475  rMinPosition[i] = CalculatePosition( bounding_box.GetMinPoint()[i], i );
476  rMaxPosition[i] = CalculatePosition( bounding_box.GetMaxPoint()[i], i ) + 1;
477  }
478  }
479 
486  std::size_t CalculatePosition(
487  const double Coordinate,
488  const int ThisDimension
489  ) const;
490 
500  template<typename TGeometryType>
501  static inline bool IsIntersected(
502  TGeometryType& rGeometry,
503  const BoundingBox<PointType>& rBox,
504  const double ThisTolerance
505  )
506  {
507  PointType low_point_tolerance;
508  PointType high_point_tolerance;
509 
510  for(unsigned int i = 0; i<3; i++) {
511  low_point_tolerance[i] = rBox.GetMinPoint()[i] - ThisTolerance;
512  high_point_tolerance[i] = rBox.GetMaxPoint()[i] + ThisTolerance;
513  }
514 
515  return rGeometry.HasIntersection(low_point_tolerance,high_point_tolerance);
516  }
517 
526  void SearchInRadiusInCell(
527  const CellType& rCell,
528  const PointType& rPoint,
529  const double Radius,
530  std::unordered_map<GeometricalObject*, double>& rResults
531  );
532 
541  void SearchNearestInCell(
542  const CellType& rCell,
543  const PointType& rPoint,
544  ResultType& rResult,
545  const double MaxRadius
546  );
547 
555  void SearchIsInsideInCell(
556  const CellType& rCell,
557  const PointType& rPoint,
558  ResultType& rResult
559  );
560 
564 
565 
569 
570 
574 
576  GeometricalObjectsBins& operator=(GeometricalObjectsBins const& rOther) = delete;
577 
579  GeometricalObjectsBins(GeometricalObjectsBins const& rOther) = delete;
580 
582 
583 }; // Class GeometricalObjectsBins
584 
586 
589 
590 
594 
595 // /// input stream function
596 // inline std::istream& operator >> (std::istream& rIStream,
597 // GeometricalObjectsBins& rThis){
598 // return rIStream;
599 // }
600 
601 // /// output stream function
602 // inline std::ostream& operator << (std::ostream& rOStream,
603 // const GeometricalObjectsBins& rThis)
604 // {
605 // rThis.PrintInfo(rOStream);
606 // rOStream << std::endl;
607 // rThis.PrintData(rOStream);
608 
609 // return rOStream;
610 // }
612 
614 
615 } // namespace Kratos.
PeriodicInterfaceProcess & operator=(const PeriodicInterfaceProcess &)=delete
This defines the geometrical object, base definition of the element and condition entities.
Definition: geometrical_object.h:58
A bins container for 3 dimensional GeometricalObject entities.
Definition: geometrical_objects_bins.h:46
array_1d< double, 3 > mInverseOfCellSize
The size of each cell in each direction.
Definition: geometrical_objects_bins.h:381
virtual std::string Info() const
Turn back information as a string.
Definition: geometrical_objects_bins.h:343
std::vector< ResultType > SearchNearest(TPointIteratorType itPointBegin, TPointIteratorType itPointEnd)
This method takes a point and finds the nearest object to it (iterative version).
Definition: geometrical_objects_bins.h:244
KRATOS_CLASS_POINTER_DEFINITION(GeometricalObjectsBins)
Pointer definition of GeometricalObjectsBins.
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: geometrical_objects_bins.h:351
const BoundingBox< PointType > & GetBoundingBox() const
Getting the bins bounding box.
Definition: geometrical_objects_bins.h:305
virtual ~GeometricalObjectsBins()
Destructor.
Definition: geometrical_objects_bins.h:109
std::size_t GetTotalNumberOfCells()
The total number of cells in the container.
Definition: geometrical_objects_bins.h:329
const array_1d< std::size_t, 3 > & GetNumberOfCells()
returns a 3D array having the number of cells in direction x, y and z
Definition: geometrical_objects_bins.h:321
void AddObjectsToCells(TIteratorType GeometricalObjectsBegin, TIteratorType GeometricalObjectsEnd)
Adding objects to the cells that intersecting with it.
Definition: geometrical_objects_bins.h:422
GeometricalObjectsBins(TIteratorType GeometricalObjectsBegin, TIteratorType GeometricalObjectsEnd, const double Tolerance=1e-12)
The constructor with all geometries to be stored. Please note that all of them should be available at...
Definition: geometrical_objects_bins.h:75
BoundingBox< PointType > mBoundingBox
Definition: geometrical_objects_bins.h:378
array_1d< std::size_t, Dimension > mNumberOfCells
The bounding box of the domain.
Definition: geometrical_objects_bins.h:379
array_1d< double, 3 > mCellSizes
The number of cells in each direction.
Definition: geometrical_objects_bins.h:380
GeometricalObjectsBins(TContainer &rGeometricalObjectsVector, const double Tolerance=1e-12)
The constructor with all geometries to be stored. Please note that all of them should be available at...
Definition: geometrical_objects_bins.h:101
std::vector< ResultType > SearchIsInside(TPointIteratorType itPointBegin, TPointIteratorType itPointEnd)
This method takes a point and search if it's inside an geometrical object of the domain (iterative ve...
Definition: geometrical_objects_bins.h:282
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: geometrical_objects_bins.h:354
std::vector< CellType > mCells
The inverse of the size of each cell in each direction.
Definition: geometrical_objects_bins.h:382
void SearchInRadius(TPointIteratorType itPointBegin, TPointIteratorType itPointEnd, const double Radius, std::vector< std::vector< ResultType >> &rResults)
This method takes a point and finds all of the objects in the given radius to it (iterative version).
Definition: geometrical_objects_bins.h:169
double mTolerance
The cells of the domain.
Definition: geometrical_objects_bins.h:383
std::vector< GeometricalObject * > CellType
The type of geometrical object to be stored in the bins.
Definition: geometrical_objects_bins.h:61
std::vector< ResultType > SearchNearestInRadius(TPointIteratorType itPointBegin, TPointIteratorType itPointEnd, const double Radius)
This method takes a point and finds the nearest object to it in a given radius (iterative version).
Definition: geometrical_objects_bins.h:209
GeometricalObjectsBins()=default
Default constructor protected.
const array_1d< double, 3 > & GetCellSizes()
return an array with the x,y and z size of the cube
Definition: geometrical_objects_bins.h:313
Point class.
Definition: point.h:59
This class is the result of the spatial searches.
Definition: spatial_search_result.h:40
end
Definition: DEM_benchmarks.py:180
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
bool CalculatePosition(Geometry< Node > &geom, const double xc, const double yc, const double zc, array_1d< double, 3 > &N)
Definition: transfer_utility.h:343
int k
Definition: quadrature.py:595
int j
Definition: quadrature.py:648
J
Definition: sensitivityMatrix.py:58
K
Definition: sensitivityMatrix.py:73
integer i
Definition: TensorModule.f:17
e
Definition: run_cpp_mpi_tests.py:31
Configure::PointType PointType
Definition: transfer_utility.h:245