Parasol Planning Library (PPL)
SegmentTrees.h
Go to the documentation of this file.
1 #ifndef SEGMENT_TREES_H_
2 #define SEGMENT_TREES_H_
3 
4 #include <memory>
5 #include <map>
6 #include <limits>
7 #include <CGAL/Cartesian.h>
8 #include <CGAL/Segment_tree_k.h>
9 #include <CGAL/Range_segment_tree_traits.h>
10 using namespace std;
11 
12 #include "Vector.h"
13 using namespace mathtool;
14 #include "IOUtils.h"
15 
17 
18 
23 
24 template<typename PropertyType=size_t>
25 class SegmentTrees {
26  public:
29  typedef CGAL::Cartesian<double> CK;
30  typedef CGAL::Segment_tree_map_traits_2<CK, size_t> Traits2;
31  typedef CGAL::Segment_tree_2<Traits2> Segment_tree_2_type;
32  typedef CGAL::Segment_tree_map_traits_3<CK, size_t> Traits3;
33  typedef CGAL::Segment_tree_3<Traits3> Segment_tree_3_type;
34  typedef Traits2::Interval Interval2;
35  typedef Traits3::Interval Interval3;
36  typedef Traits2::Pure_interval Pure_interval2;
37  typedef Traits3::Pure_interval Pure_interval3;
38  typedef Traits2::Key Key2;
39  typedef Traits3::Key Key3;
43 
44  SegmentTrees(size_t _d=3);
45  ~SegmentTrees();
46 
50 
53  void AddBoundary(const Boundary* _b) {
54  m_boundaries.push_back(_b);
55  }
56 
60  void AddBoundary(const Boundary* _b, PropertyType _p);
61 
63  void BuildSegmentTrees();
64 
69  size_t FindEnclosingBoundaries(const Point3d& _p,
70  double _e=0.5*numeric_limits<float>::epsilon());
71 
75 
76  // Get the number of output enclosing ranges
78  return m_output.size();
79  }
80 
81  // Get the input key index of the output enclosing range
82  // @param _i Index to the output enclosing range
83  size_t GetOutputBoundaryIndex(size_t _i) {
84  return m_output[_i];
85  }
86 
87  // Get the property of the output enclosing range
88  // @param _i Index to the output enclosing range
89  PropertyType GetOutputBoundaryProperty(size_t _i) {
90  return m_propertyMap[m_output[_i]];
91  }
92 
93  // Get the output enclosing range
94  // @param _i Index to the output enclosing range
95  const Boundary* GetOutputBoundary(size_t _i) {
96  return m_boundaries[m_output[_i]];
97  }
98 
99  // Dimension of the segment tree
100  size_t GetDimension() { return m_dimension; }
101  void SetDimension(size_t _d = 3) { m_dimension = _d; }
102 
104 
105  private:
106 
109 
110  vector<const Boundary*> m_boundaries;
111  vector<PropertyType> m_propertyMap;
112  vector<size_t> m_output;
113  size_t m_dimension{3};
114  bool m_debug{false};
115  Segment_tree_2_type* m_tree2{nullptr};
116  Segment_tree_3_type* m_tree3{nullptr};
117 
119 };
120 
121 /*------------------------------- Construction ----------------------------*/
122 template<typename PropertyType>
124 SegmentTrees(size_t _d) : m_dimension(_d) {
125 }
126 
127 template<typename PropertyType>
129 ~SegmentTrees() {
130  m_boundaries.clear();
131  m_output.clear();
132  delete m_tree2;
133  delete m_tree3;
134 }
135 
136 
137 /*------------------------- Creation of Segment Trees ---------------------*/
138 
139 template<typename PropertyType>
141 AddBoundary(const Boundary* _b, PropertyType _p) {
142  AddBoundary(_b);
143  m_propertyMap.emplace_back(_p);
144 }
145 
146 
147 template<typename PropertyType>
149  delete m_tree2;
150  delete m_tree3;
151  // Store the intervals of each boundary with the input index as key index
152  if(m_dimension == 2) {
153  vector<Interval2> in;
154  for(size_t i = 0; i < m_boundaries.size(); i++) {
155  auto b = m_boundaries[i];
156  auto x = b->GetRange(0);
157  auto y = b->GetRange(1);
158  in.push_back(Interval2(Pure_interval2(Key2(x.min,y.min),
159  Key2(x.max,y.max)),i));
160  }
161  m_tree2 = new Segment_tree_2_type(in.begin(), in.end());
162  }
163  else {
164  vector<Interval3> in;
165  for(size_t i = 0; i < m_boundaries.size(); i++) {
166  auto b = m_boundaries[i];
167  auto x = b->GetRange(0);
168  auto y = b->GetRange(1);
169  auto z = b->GetRange(2);
170  in.push_back(Interval3(Pure_interval3(Key3(x.min,y.min,z.min),
171  Key3(x.max,y.max,z.max)),i));
172  }
173  m_tree3 = new Segment_tree_3_type(in.begin(), in.end());
174  }
175 }
176 
177 /*------------------------- Find Bounding Volumes -------------------------*/
178 template<typename PropertyType>
179 size_t
181 FindEnclosingBoundaries(const Point3d& _p, double _e) {
182  // Clear previously queried results
183  m_output.clear();
184  // Generate a query range with _p as midpoint, _e as offset and
185  // number of input boundaries as index
186  // Use enclosing_query to find the enclosing range key indices and
187  // store them
188  if(m_dimension == 2) {
189  vector<Interval2> out;
190  auto p = Interval2(Pure_interval2(Key2(_p[0]-_e,_p[1]-_e),
191  Key2(_p[0]+_e,_p[1]+_e)),m_boundaries.size());
192  m_tree2->window_query(p,std::back_inserter(out));
193  for(auto i = out.begin(); i != out.end(); i++)
194  m_output.push_back(i->second);
195  }
196  else {
197  vector<Interval3> out;
198  auto p = Interval3(Pure_interval3(Key3(_p[0]-_e,_p[1]-_e,_p[2]-_e),
199  Key3(_p[0]+_e,_p[1]+_e,_p[2]+_e)),m_boundaries.size());
200  m_tree3->window_query(p,std::back_inserter(out));
201  for(auto i = out.begin(); i != out.end(); i++)
202  m_output.push_back(i->second);
203  }
204 
205  return GetNumberEnclosingBoundaries();
206 }
207 
208 
209 /*-------------------------------------------------------------------------*/
210 
211 #endif
Definition: Boundary.h:30
Definition: SegmentTrees.h:25
Traits3::Key Key3
Definition: SegmentTrees.h:39
CGAL::Cartesian< double > CK
Definition: SegmentTrees.h:29
size_t GetDimension()
Definition: SegmentTrees.h:100
const Boundary * GetOutputBoundary(size_t _i)
Definition: SegmentTrees.h:95
void AddBoundary(const Boundary *_b)
Definition: SegmentTrees.h:53
CGAL::Segment_tree_map_traits_3< CK, size_t > Traits3
Definition: SegmentTrees.h:32
PropertyType GetOutputBoundaryProperty(size_t _i)
Definition: SegmentTrees.h:89
size_t GetOutputBoundaryIndex(size_t _i)
Definition: SegmentTrees.h:83
SegmentTrees(size_t _d=3)
Definition: SegmentTrees.h:124
Traits2::Interval Interval2
Definition: SegmentTrees.h:34
void BuildSegmentTrees()
Build the Segment Trees.
Definition: SegmentTrees.h:148
CGAL::Segment_tree_3< Traits3 > Segment_tree_3_type
Definition: SegmentTrees.h:33
Traits3::Interval Interval3
Definition: SegmentTrees.h:35
Traits3::Pure_interval Pure_interval3
Definition: SegmentTrees.h:37
CGAL::Segment_tree_map_traits_2< CK, size_t > Traits2
Definition: SegmentTrees.h:30
size_t FindEnclosingBoundaries(const Point3d &_p, double _e=0.5 *numeric_limits< float >::epsilon())
Definition: SegmentTrees.h:181
size_t GetNumberEnclosingBoundaries()
Definition: SegmentTrees.h:77
CGAL::Segment_tree_2< Traits2 > Segment_tree_2_type
Definition: SegmentTrees.h:31
void SetDimension(size_t _d=3)
Definition: SegmentTrees.h:101
Traits2::Pure_interval Pure_interval2
Definition: SegmentTrees.h:36
Traits2::Key Key2
Definition: SegmentTrees.h:38
~SegmentTrees()
Definition: SegmentTrees.h:129
Definition: Cfg.h:23