GOAT (Geometrical optics application tool) 0.1
Loading...
Searching...
No Matches
octree.h
Go to the documentation of this file.
1#pragma once
2#include "triangle.h"
3#include "box.h"
4#include "vector.h"
5#include <iostream>
6#include <memory>
7
8namespace GOAT
9{
10 namespace raytracing
11 {
26
27 template <class T> class Octree
28 {
29 public:
30 bool isLeaf;
32 {
33 /*parent = 0;
34 child = 0;*/
35 nChilds = 0;
36 // Element = 0;
37 nElements = 0;
38 isLeaf = false;
40 }
41
42 Octree(const Octree& O);
43
45 {
47 }
48 void delElements();
49 void trimOctree();
50 void trimOctree(int rek);
51 void createTree(int maxRecursions = 1, int rek = 0);
52 void createChilds();
53 void delChild(int i);
54 void delAllChilds();
62 void setRecursiondepth(int max_recursions);
64
65 std::vector<std::unique_ptr<Octree<T>>> child;
66 std::vector<T> Element;
70 };
71
72 template <class T> Octree<T>::Octree(const Octree& O)
73 {
74 if (O.parent)
75 parent = O.parent;
76 else
77 parent = nullptr;
78 nChilds = O.nChilds;
79 for (const auto& ptr : O.child)
80 child.push_back(std::make_unique<Octree<T>>(*ptr));
81
82 Element = O.Element;
83 nElements = Element.size();
84
86 BBox = O.BBox;
87 isLeaf = O.isLeaf;
88 }
89
90 template <class T> void Octree<T>::delElements()
91 {
92 if (nElements > 0)
93 Element.clear();
94 nElements = 0;
95 }
96
97
98 template <class T> void Octree<T>::trimOctree()
99 {
100 int rek = 0;
101 this->trimOctree(rek);
102 }
103
104 template <class T> void Octree<T>::trimOctree(int rek)
105 {
106 int hnChilds = nChilds;
107 bool valid[8] = { true,true,true,true,true,true, true,true };
108 for (size_t i = 0; i < child.size(); i++)
109 {
110
111 if (child[i]->isLeaf && child[i]->Element.empty())
112 {
113
114 child[i].reset();
115 valid[i] = false;
116 nChilds--;
117 }
118
119 else
120 child[i]->trimOctree(rek + 1);
121 }
122
123 if (hnChilds < nChilds) // Kinder wurden gel�scht ==> umsortieren notwendig
124 {
125 std::vector<std::unique_ptr<Octree<T> > > hChild;
126 int i = 0;
127 for (int j = 0; j < hnChilds; j++)
128 {
129 if (valid[j])
130 {
131 hChild.push_back(std::move(child[j]));
132 i++;
133 }
134 }
135
136 child = std::move(hChild);
137 nChilds = child.size();
138 }
139 }
140
141
142 template <class T> void Octree<T>::delAllChilds()
143 {
144 child.clear();
145 nChilds = 0;
146 }
147
148 template <class T> void Octree<T>::delChild(int i)
149 {
150 child[i]->delAllChilds();
151 }
152
153 template <class T> void Octree<T>::createChilds()
154 {
155 int sx, sy, sz;
157 child.clear();
158 for (int i = 0; i < 8; i++)
159 {
160
161 auto newChild = std::make_unique<Octree<T>>();
162 newChild->parent = this;
163
164 switch (i)
165 {
166 case 0: sx = -1; sy = -1; sz = -1; break;
167 case 1: sx = +1; sy = -1; sz = -1; break;
168 case 2: sx = -1; sy = +1; sz = -1; break;
169 case 3: sx = +1; sy = +1; sz = -1; break;
170 case 4: sx = -1; sy = -1; sz = +1; break;
171 case 5: sx = +1; sy = -1; sz = +1; break;
172 case 6: sx = -1; sy = +1; sz = +1; break;
173 case 7: sx = +1; sy = +1; sz = +1; break;
174 }
175 P = BBox.P + maths::Vector<double>(sx * BBox.d[0] / 4.0, sy * BBox.d[1] / 4.0, sz * BBox.d[2] / 4.0);
176 d = BBox.d / 2.0;
177 newChild->BBox = Box(P, d, BBox.n);
178
179 child.push_back(std::move(newChild));
180
181 }
182 nChilds = 8;
183 }
184
185
186
187 template <class T> void Octree<T>::createTree(int maxRecursions, int rek)
188 {
189 MAX_RECURSIONS = maxRecursions;
190 if (rek < MAX_RECURSIONS)
191 {
192 createChilds();
193 for (int i = 0; i < 8; i++) child[i]->createTree(maxRecursions, rek + 1);
194 }
195 else
196 {
197 nChilds = 0;
198 isLeaf = true;
199 }
200 }
201
203 {
204 BBox = Box(MP, d, 1.0);
205 }
206
207 template <class T> void Octree<T>:: setRecursiondepth(int max_recursions)
208 {
209 // delete the whole tree...
210 delAllChilds();
211 delElements();
212
213 // ... and recreate it with the new number of
214 // createTree(max_recursions);
215 }
216
217
218
220
221
226 std::ostream& operator<< (std::ostream& os, Octree<triangle>&);
227 }
228}
Template class for threedimensional vectors.
Definition vector.h:57
class which represents a box (cuboid). It is derived by class ObjectShape This class is mainly used f...
Definition box.h:18
This template class is used for internal purposes and represents an octree.
Definition octree.h:28
void delElements()
Deletes all elements.
Definition octree.h:90
std::vector< std::unique_ptr< Octree< T > > > child
Definition octree.h:65
void delAllChilds()
Delete all children of the Octree.
Definition octree.h:142
std::vector< T > Element
Definition octree.h:66
void setRecursiondepth(int max_recursions)
Definition octree.h:207
void setBoundingBox(maths::Vector< double > MP, maths::Vector< double > d)
Set the bounding box of the Octree The bounding box is a circumscribing cuboid with center MP and the...
Definition octree.h:202
void trimOctree()
Trims the tree, i.e. removing all empty childs.
Definition octree.h:98
bool isLeaf
Sign wether the Octree is a leaf or not.
Definition octree.h:30
void createChilds()
Creates the childs of the Octree and calculates the bounding boxes.
Definition octree.h:153
Octree< T > * parent
Definition octree.h:63
void delChild(int i)
Delete the i-th child of the Octree.
Definition octree.h:148
void createTree(int maxRecursions=1, int rek=0)
Prepare the tree with recursion depth maxRecursions.
Definition octree.h:187
This class describes a triangle, represented by its corner points. It is intented for internal purpos...
Definition triangle.h:13
Raytracer used for ultrashort pulse calculation with raytracing only.
Definition asphericLens.h:6
double rayOctreeIntersection(Octree< triangle > &T, maths::Vector< double > P, maths::Vector< double > k, triangle &D)
bool checkTriangleBoxIntersection(Box B, triangle D)
void writeTriangleOctree(char *Fname, Octree< triangle > &)
std::ostream & operator<<(std::ostream &os, Box B)
output operator for the Box class
void addTriangleToTriangle(Octree< triangle > *O, triangle D, int rek)
This class is used for the iray class. This class is intended for internal use only....
Definition fresnel.h:7
This file contains the Vector template class and some useful functions around this class.