TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
Octree_Double.h
1/****************************************************************************
2* Copyright (c) 2024, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#ifndef Octree_Double_included
17#define Octree_Double_included
18
19#include <Octree_Int.h>
20#include <TRUSTTab.h>
21
22/*! @brief : An octree allowing to search in space for elements or points described by real-valued coordinates.
23 *
24 * This object is based on Octree_Int.
25 * Tips:
26 * To search for points up to epsilon tolerance one can do:
27 * 1) build_nodes(coord, include_virt, 0.)
28 * followed by
29 * search_elements_box(center, epsilon, elements);
30 *
31 * 2) build_nodes(coord, include_virt, epsilon)
32 * followed by
33 * search_elements(x,y,z,...)
34 * The first solution takes more time to build the octree but the search is faster.
35 * The second is the reverse... and one can choose epsilon for each point.
36 *
37 */
38template <typename _SIZE_>
40{
41public:
42 using int_t = _SIZE_;
43 using ArrOfInt_t = ArrOfInt_T<_SIZE_>;
44 using IntTab_t = IntTab_T<_SIZE_>;
45 using ArrOfDouble_t = ArrOfDouble_T<_SIZE_>;
46 using DoubleTab_t = DoubleTab_T<_SIZE_>;
47
48 void reset();
49
50 template<class _TAB_TYPE_>
51 void build_elements(const _TAB_TYPE_& coords, const IntTab_t& elements, const double epsilon, const bool include_virtual);
52
53 void build_nodes(const DoubleTab_t& coords, const bool include_virtual, const double epsilon = 0.);
54 int_t search_elements(double x, double y, double z, int_t& index) const;
55 int_t search_elements_box(double xmin, double ymin, double zmin, double xmax, double ymax, double zmax, ArrOfInt_t& elements) const;
56 static int_t search_nodes_close_to(double x, double y, double z, const DoubleTab_t& coords, ArrOfInt_t& node_list, double epsilon);
57 int_t search_elements_box(const ArrOfDouble& center, const double radius, ArrOfInt_t& elements) const;
58 static int_t search_nodes_close_to(const ArrOfDouble& point, const DoubleTab_t& coords, ArrOfInt_t& node_list, double epsilon);
59 int dimension() const
60 {
61 assert(dim_ > 0);
62 return dim_;
63 }
64 inline const ArrOfInt_t& floor_elements() const { return octree_int_.floor_elements(); }
65
66protected:
67 inline bool integer_position(double x, int direction, int& ix) const;
68 inline bool integer_position_clip(double xmin, double xmax, int& x0, int& x1, int direction) const;
69
70 template<class _TAB_TYPE_>
71 void compute_origin_factors(const _TAB_TYPE_& coords, const double epsilon, const int include_virtual);
72
74 // These two arrays are always of size 3 for convenience
75 ArrOfDouble origin_, factor_;
76 int dim_ = 0;
77};
78
79//////////////////// INLINE METHODS /////////////////////////////
80
81/*! @brief Converts a real coordinate to an integer coordinate for the octree_int.
82 *
83 * Return value: 1 if ok, 0 if coordinate is outside the octree
84 */
85template <typename _SIZE_>
86inline bool Octree_Double_32_64<_SIZE_>::integer_position(double x, int direction, int& ix) const
87{
88 const double coord_max = (double) Octree_Int_32_64<_SIZE_>::coord_max_;
89 double rnd_x = (x - origin_[direction]) * factor_[direction];
90 // 0.49 allows accepting a coordinate x equal to xmin or xmax of the octree,
91 // otherwise for an octree built from nodes, there is a risk
92 // of not finding the coordinates of points placed at the edge of the octree.
93 if (rnd_x >= -0.49 && rnd_x <= coord_max + 0.49)
94 {
95 ix = (int) floor(rnd_x + 0.5);
96 return true;
97 }
98 return false;
99}
100
101// Return value: 1 if there is a non-empty intersection with the octree, 0 otherwise
102template <typename _SIZE_>
103inline bool Octree_Double_32_64<_SIZE_>::integer_position_clip(double xmin, double xmax,
104 int& x0, int& x1,
105 int direction) const
106{
107 const double coord_max = (double) Octree_Int_32_64<_SIZE_>::coord_max_;
108 xmin = (xmin - origin_[direction]) * factor_[direction];
109 xmax = (xmax - origin_[direction]) * factor_[direction];
110 // no margin here since we search with a box, the epsilon is already
111 // included in the dimension of the box.
112 if (xmin > coord_max || xmax < 0.)
113 return false;
114 if (xmin < .0)
115 x0 = 0;
116 else
117 x0 = (int) (floor(xmin+0.5));
118 if (xmax > coord_max)
120 else
121 x1 = (int) (floor(xmax+0.5));
122 return true;
123}
124
125
126//////////////////// TEMPLATE METHOD IMPL ////////////////////////
127
128#include <Octree_Double.tpp>
129
130using Octree_Double = Octree_Double_32_64<int>;
131using Octree_Double_64 = Octree_Double_32_64<trustIdType>;
132
133#endif
: An octree allowing to search in space for elements or points described by real-valued coordinates.
const ArrOfInt_t & floor_elements() const
bool integer_position(double x, int direction, int &ix) const
Converts a real coordinate to an integer coordinate for the octree_int.
DoubleTab_T< _SIZE_ > DoubleTab_t
void compute_origin_factors(const _TAB_TYPE_ &coords, const double epsilon, const int include_virtual)
Helper method for build_nodes and build_elements: computes the conversion factors from real to intege...
static int_t search_nodes_close_to(const ArrOfDouble &point, const DoubleTab_t &coords, ArrOfInt_t &node_list, double epsilon)
Same as search_nodes_close_to(double x, double y, double z, ...).
int dimension() const
static int_t search_nodes_close_to(double x, double y, double z, const DoubleTab_t &coords, ArrOfInt_t &node_list, double epsilon)
Non-member method. Searches among the vertices in node_list for those within a distance.
ArrOfInt_T< _SIZE_ > ArrOfInt_t
bool integer_position_clip(double xmin, double xmax, int &x0, int &x1, int direction) const
int_t search_elements_box(const ArrOfDouble &center, const double radius, ArrOfInt_t &elements) const
Searches for all elements or points potentially having a non-empty intersection with the given box (c...
void build_nodes(const DoubleTab_t &coords, const bool include_virtual, const double epsilon=0.)
builds an octree containing the points with coordinates coords.
Octree_Int_32_64< int > octree_int_
ArrOfDouble_T< _SIZE_ > ArrOfDouble_t
int_t search_elements(double x, double y, double z, int_t &index) const
searches for the elements or points contained in the octree_floor that contains the point (x,...
int_t search_elements_box(double xmin, double ymin, double zmin, double xmax, double ymax, double zmax, ArrOfInt_t &elements) const
searches for all elements or points potentially having a non-empty intersection with the given box.
IntTab_T< _SIZE_ > IntTab_t
void build_elements(const _TAB_TYPE_ &coords, const IntTab_t &elements, const double epsilon, const bool include_virtual)
Builds an octree from volumetric elements described by sets of vertices.
: An octree allowing retrieval of point-like or parallelepiped-shaped objects in 1D,...
Definition Octree_Int.h:28
static const int coord_max_
Definition Octree_Int.h:48