BVAlgorithms.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009 Ilya Baran <ibaran@mit.edu>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_BVALGORITHMS_H
11 #define EIGEN_BVALGORITHMS_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 #ifndef EIGEN_PARSED_BY_DOXYGEN
20 template<typename BVH, typename Intersector>
21 bool intersect_helper(const BVH &tree, Intersector &intersector, typename BVH::Index root)
22 {
23  typedef typename BVH::Index Index;
24  typedef typename BVH::VolumeIterator VolIter;
25  typedef typename BVH::ObjectIterator ObjIter;
26 
27  VolIter vBegin = VolIter(), vEnd = VolIter();
28  ObjIter oBegin = ObjIter(), oEnd = ObjIter();
29 
30  std::vector<Index> todo(1, root);
31 
32  while(!todo.empty()) {
33  tree.getChildren(todo.back(), vBegin, vEnd, oBegin, oEnd);
34  todo.pop_back();
35 
36  for(; vBegin != vEnd; ++vBegin) //go through child volumes
37  if(intersector.intersectVolume(tree.getVolume(*vBegin)))
38  todo.push_back(*vBegin);
39 
40  for(; oBegin != oEnd; ++oBegin) //go through child objects
41  if(intersector.intersectObject(*oBegin))
42  return true; //intersector said to stop query
43  }
44  return false;
45 }
46 #endif //not EIGEN_PARSED_BY_DOXYGEN
47 
48 template<typename Volume1, typename Object1, typename Object2, typename Intersector>
49 struct intersector_helper1
50 {
51  intersector_helper1(const Object2 &inStored, Intersector &in) : stored(inStored), intersector(in) {}
52  bool intersectVolume(const Volume1 &vol) { return intersector.intersectVolumeObject(vol, stored); }
53  bool intersectObject(const Object1 &obj) { return intersector.intersectObjectObject(obj, stored); }
54  Object2 stored;
55  Intersector &intersector;
56 private:
57  intersector_helper1& operator=(const intersector_helper1&);
58 };
59 
60 template<typename Volume2, typename Object2, typename Object1, typename Intersector>
61 struct intersector_helper2
62 {
63  intersector_helper2(const Object1 &inStored, Intersector &in) : stored(inStored), intersector(in) {}
64  bool intersectVolume(const Volume2 &vol) { return intersector.intersectObjectVolume(stored, vol); }
65  bool intersectObject(const Object2 &obj) { return intersector.intersectObjectObject(stored, obj); }
66  Object1 stored;
67  Intersector &intersector;
68 private:
69  intersector_helper2& operator=(const intersector_helper2&);
70 };
71 
72 } // end namespace internal
73 
80 template<typename BVH, typename Intersector>
81 void BVIntersect(const BVH &tree, Intersector &intersector)
82 {
83  internal::intersect_helper(tree, intersector, tree.getRootIndex());
84 }
85 
94 template<typename BVH1, typename BVH2, typename Intersector>
95 void BVIntersect(const BVH1 &tree1, const BVH2 &tree2, Intersector &intersector) //TODO: tandem descent when it makes sense
96 {
97  typedef typename BVH1::Index Index1;
98  typedef typename BVH2::Index Index2;
99  typedef internal::intersector_helper1<typename BVH1::Volume, typename BVH1::Object, typename BVH2::Object, Intersector> Helper1;
100  typedef internal::intersector_helper2<typename BVH2::Volume, typename BVH2::Object, typename BVH1::Object, Intersector> Helper2;
101  typedef typename BVH1::VolumeIterator VolIter1;
102  typedef typename BVH1::ObjectIterator ObjIter1;
103  typedef typename BVH2::VolumeIterator VolIter2;
104  typedef typename BVH2::ObjectIterator ObjIter2;
105 
106  VolIter1 vBegin1 = VolIter1(), vEnd1 = VolIter1();
107  ObjIter1 oBegin1 = ObjIter1(), oEnd1 = ObjIter1();
108  VolIter2 vBegin2 = VolIter2(), vEnd2 = VolIter2(), vCur2 = VolIter2();
109  ObjIter2 oBegin2 = ObjIter2(), oEnd2 = ObjIter2(), oCur2 = ObjIter2();
110 
111  std::vector<std::pair<Index1, Index2> > todo(1, std::make_pair(tree1.getRootIndex(), tree2.getRootIndex()));
112 
113  while(!todo.empty()) {
114  tree1.getChildren(todo.back().first, vBegin1, vEnd1, oBegin1, oEnd1);
115  tree2.getChildren(todo.back().second, vBegin2, vEnd2, oBegin2, oEnd2);
116  todo.pop_back();
117 
118  for(; vBegin1 != vEnd1; ++vBegin1) { //go through child volumes of first tree
119  const typename BVH1::Volume &vol1 = tree1.getVolume(*vBegin1);
120  for(vCur2 = vBegin2; vCur2 != vEnd2; ++vCur2) { //go through child volumes of second tree
121  if(intersector.intersectVolumeVolume(vol1, tree2.getVolume(*vCur2)))
122  todo.push_back(std::make_pair(*vBegin1, *vCur2));
123  }
124 
125  for(oCur2 = oBegin2; oCur2 != oEnd2; ++oCur2) {//go through child objects of second tree
126  Helper1 helper(*oCur2, intersector);
127  if(internal::intersect_helper(tree1, helper, *vBegin1))
128  return; //intersector said to stop query
129  }
130  }
131 
132  for(; oBegin1 != oEnd1; ++oBegin1) { //go through child objects of first tree
133  for(vCur2 = vBegin2; vCur2 != vEnd2; ++vCur2) { //go through child volumes of second tree
134  Helper2 helper(*oBegin1, intersector);
135  if(internal::intersect_helper(tree2, helper, *vCur2))
136  return; //intersector said to stop query
137  }
138 
139  for(oCur2 = oBegin2; oCur2 != oEnd2; ++oCur2) {//go through child objects of second tree
140  if(intersector.intersectObjectObject(*oBegin1, *oCur2))
141  return; //intersector said to stop query
142  }
143  }
144  }
145 }
146 
147 namespace internal {
148 
149 #ifndef EIGEN_PARSED_BY_DOXYGEN
150 template<typename BVH, typename Minimizer>
151 typename Minimizer::Scalar minimize_helper(const BVH &tree, Minimizer &minimizer, typename BVH::Index root, typename Minimizer::Scalar minimum)
152 {
153  typedef typename Minimizer::Scalar Scalar;
154  typedef typename BVH::Index Index;
155  typedef std::pair<Scalar, Index> QueueElement; //first element is priority
156  typedef typename BVH::VolumeIterator VolIter;
157  typedef typename BVH::ObjectIterator ObjIter;
158 
159  VolIter vBegin = VolIter(), vEnd = VolIter();
160  ObjIter oBegin = ObjIter(), oEnd = ObjIter();
161  std::priority_queue<QueueElement, std::vector<QueueElement>, std::greater<QueueElement> > todo; //smallest is at the top
162 
163  todo.push(std::make_pair(Scalar(), root));
164 
165  while(!todo.empty()) {
166  tree.getChildren(todo.top().second, vBegin, vEnd, oBegin, oEnd);
167  todo.pop();
168 
169  for(; oBegin != oEnd; ++oBegin) //go through child objects
170  minimum = (std::min)(minimum, minimizer.minimumOnObject(*oBegin));
171 
172  for(; vBegin != vEnd; ++vBegin) { //go through child volumes
173  Scalar val = minimizer.minimumOnVolume(tree.getVolume(*vBegin));
174  if(val < minimum)
175  todo.push(std::make_pair(val, *vBegin));
176  }
177  }
178 
179  return minimum;
180 }
181 #endif //not EIGEN_PARSED_BY_DOXYGEN
182 
183 
184 template<typename Volume1, typename Object1, typename Object2, typename Minimizer>
185 struct minimizer_helper1
186 {
187  typedef typename Minimizer::Scalar Scalar;
188  minimizer_helper1(const Object2 &inStored, Minimizer &m) : stored(inStored), minimizer(m) {}
189  Scalar minimumOnVolume(const Volume1 &vol) { return minimizer.minimumOnVolumeObject(vol, stored); }
190  Scalar minimumOnObject(const Object1 &obj) { return minimizer.minimumOnObjectObject(obj, stored); }
191  Object2 stored;
192  Minimizer &minimizer;
193 private:
194  minimizer_helper1& operator=(const minimizer_helper1&);
195 };
196 
197 template<typename Volume2, typename Object2, typename Object1, typename Minimizer>
198 struct minimizer_helper2
199 {
200  typedef typename Minimizer::Scalar Scalar;
201  minimizer_helper2(const Object1 &inStored, Minimizer &m) : stored(inStored), minimizer(m) {}
202  Scalar minimumOnVolume(const Volume2 &vol) { return minimizer.minimumOnObjectVolume(stored, vol); }
203  Scalar minimumOnObject(const Object2 &obj) { return minimizer.minimumOnObjectObject(stored, obj); }
204  Object1 stored;
205  Minimizer &minimizer;
206 private:
207  minimizer_helper2& operator=(const minimizer_helper2&);
208 };
209 
210 } // end namespace internal
211 
220 template<typename BVH, typename Minimizer>
221 typename Minimizer::Scalar BVMinimize(const BVH &tree, Minimizer &minimizer)
222 {
223  return internal::minimize_helper(tree, minimizer, tree.getRootIndex(), (std::numeric_limits<typename Minimizer::Scalar>::max)());
224 }
225 
236 template<typename BVH1, typename BVH2, typename Minimizer>
237 typename Minimizer::Scalar BVMinimize(const BVH1 &tree1, const BVH2 &tree2, Minimizer &minimizer)
238 {
239  typedef typename Minimizer::Scalar Scalar;
240  typedef typename BVH1::Index Index1;
241  typedef typename BVH2::Index Index2;
242  typedef internal::minimizer_helper1<typename BVH1::Volume, typename BVH1::Object, typename BVH2::Object, Minimizer> Helper1;
243  typedef internal::minimizer_helper2<typename BVH2::Volume, typename BVH2::Object, typename BVH1::Object, Minimizer> Helper2;
244  typedef std::pair<Scalar, std::pair<Index1, Index2> > QueueElement; //first element is priority
245  typedef typename BVH1::VolumeIterator VolIter1;
246  typedef typename BVH1::ObjectIterator ObjIter1;
247  typedef typename BVH2::VolumeIterator VolIter2;
248  typedef typename BVH2::ObjectIterator ObjIter2;
249 
250  VolIter1 vBegin1 = VolIter1(), vEnd1 = VolIter1();
251  ObjIter1 oBegin1 = ObjIter1(), oEnd1 = ObjIter1();
252  VolIter2 vBegin2 = VolIter2(), vEnd2 = VolIter2(), vCur2 = VolIter2();
253  ObjIter2 oBegin2 = ObjIter2(), oEnd2 = ObjIter2(), oCur2 = ObjIter2();
254  std::priority_queue<QueueElement, std::vector<QueueElement>, std::greater<QueueElement> > todo; //smallest is at the top
255 
256  Scalar minimum = (std::numeric_limits<Scalar>::max)();
257  todo.push(std::make_pair(Scalar(), std::make_pair(tree1.getRootIndex(), tree2.getRootIndex())));
258 
259  while(!todo.empty()) {
260  tree1.getChildren(todo.top().second.first, vBegin1, vEnd1, oBegin1, oEnd1);
261  tree2.getChildren(todo.top().second.second, vBegin2, vEnd2, oBegin2, oEnd2);
262  todo.pop();
263 
264  for(; oBegin1 != oEnd1; ++oBegin1) { //go through child objects of first tree
265  for(oCur2 = oBegin2; oCur2 != oEnd2; ++oCur2) {//go through child objects of second tree
266  minimum = (std::min)(minimum, minimizer.minimumOnObjectObject(*oBegin1, *oCur2));
267  }
268 
269  for(vCur2 = vBegin2; vCur2 != vEnd2; ++vCur2) { //go through child volumes of second tree
270  Helper2 helper(*oBegin1, minimizer);
271  minimum = (std::min)(minimum, internal::minimize_helper(tree2, helper, *vCur2, minimum));
272  }
273  }
274 
275  for(; vBegin1 != vEnd1; ++vBegin1) { //go through child volumes of first tree
276  const typename BVH1::Volume &vol1 = tree1.getVolume(*vBegin1);
277 
278  for(oCur2 = oBegin2; oCur2 != oEnd2; ++oCur2) {//go through child objects of second tree
279  Helper1 helper(*oCur2, minimizer);
280  minimum = (std::min)(minimum, internal::minimize_helper(tree1, helper, *vBegin1, minimum));
281  }
282 
283  for(vCur2 = vBegin2; vCur2 != vEnd2; ++vCur2) { //go through child volumes of second tree
284  Scalar val = minimizer.minimumOnVolumeVolume(vol1, tree2.getVolume(*vCur2));
285  if(val < minimum)
286  todo.push(std::make_pair(val, std::make_pair(*vBegin1, *vCur2)));
287  }
288  }
289  }
290  return minimum;
291 }
292 
293 } // end namespace Eigen
294 
295 #endif // EIGEN_BVALGORITHMS_H
Matrix3f m
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
void BVIntersect(const BVH &tree, Intersector &intersector)
Definition: BVAlgorithms.h:81
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
CleanedUpDerType< DerType >::type() min(const AutoDiffScalar< DerType > &x, const T &y)
CleanedUpDerType< DerType >::type() max(const AutoDiffScalar< DerType > &x, const T &y)
Minimizer::Scalar BVMinimize(const BVH &tree, Minimizer &minimizer)
Definition: BVAlgorithms.h:221