gKit2 light
Loading...
Searching...
No Matches
BVHT< T > Struct Template Reference

bvh parametre par le type des primitives, cf triangle et instance... More...

Public Member Functions

int build (const std::vector< T > &_primitives)
Hit intersect (const Ray &ray, const float htmax) const
Hit intersect (const Ray &ray) const
int build (const std::vector< T > &_primitives)
Hit intersect (const Ray &ray, const float htmax) const
Hit intersect (const Ray &ray) const
int build (const std::vector< T > &_primitives)
Hit intersect (const Ray &ray, const float htmax) const
Hit intersect (const Ray &ray) const

Protected Member Functions

int build (const int begin, const int end)
BBox primitive_bounds (const int begin, const int end)
BBox centroid_bounds (const int begin, const int end)
void intersect (const int index, const Ray &ray, const Vector &invd, Hit &hit) const
int build (const int begin, const int end)
BBox primitive_bounds (const int begin, const int end)
BBox centroid_bounds (const int begin, const int end)
void intersect (const int index, const Ray &ray, const Vector &invd, Hit &hit) const
int build (const int begin, const int end)
BBox primitive_bounds (const int begin, const int end)
BBox centroid_bounds (const int begin, const int end)
void intersect (const int index, const Ray &ray, const Vector &invd, Hit &hit) const

Protected Attributes

std::vector< Nodenodes
std::vector< T > primitives
int root

Detailed Description

template<typename T>
struct BVHT< T >

bvh parametre par le type des primitives, cf triangle et instance...

Definition at line 127 of file tuto_bvh2.cpp.

Member Function Documentation

◆ build() [1/6]

template<typename T>
int BVHT< T >::build ( const std::vector< T > & _primitives)
inline

Definition at line 130 of file tuto_bvh2.cpp.

131 {
132 primitives= _primitives; // copie les primitives pour les trier
133 nodes.clear(); // efface les noeuds
134 nodes.reserve(primitives.size());
135
136 // construit l'arbre...
137 root= build(0, primitives.size());
138 return root;
139 }
bvh parametre par le type des primitives, cf triangle et instance...

◆ intersect() [1/9]

template<typename T>
Hit BVHT< T >::intersect ( const Ray & ray,
const float htmax ) const
inline

Definition at line 142 of file tuto_bvh2.cpp.

143 {
144 Hit hit;
145 hit.t= htmax;
146 Vector invd= Vector(1 / ray.d.x, 1 / ray.d.y, 1 / ray.d.z);
147 intersect(root, ray, invd, hit);
148 return hit;
149 }

◆ intersect() [2/9]

template<typename T>
Hit BVHT< T >::intersect ( const Ray & ray) const
inline

Definition at line 152 of file tuto_bvh2.cpp.

152{ return intersect(ray, ray.tmax); }

◆ build() [2/6]

template<typename T>
int BVHT< T >::build ( const int begin,
const int end )
inlineprotected

Definition at line 159 of file tuto_bvh2.cpp.

160 {
161 if(end - begin < 2)
162 {
163 // inserer une feuille et renvoyer son indice
164 int index= nodes.size();
165 nodes.push_back( make_leaf( primitive_bounds(begin, end), begin, end ) );
166 return index;
167 }
168
169 // axe le plus etire de l'englobant des centres des englobants des primitives...
170 BBox cbounds= centroid_bounds(begin, end);
171 Vector d= Vector(cbounds.pmin, cbounds.pmax);
172 int axis;
173 if(d.x > d.y && d.x > d.z) // x plus grand que y et z ?
174 axis= 0;
175 else if(d.y > d.z) // y plus grand que z ? (et que x implicitement)
176 axis= 1;
177 else // x et y ne sont pas les plus grands...
178 axis= 2;
179
180 // coupe l'englobant au milieu
181 float cut= cbounds.centroid(axis);
182
183 // repartit les primitives
184 T *pm= std::partition(primitives.data() + begin, primitives.data() + end,
185 [axis, cut]( const T& primitive )
186 {
187 return primitive.bounds().centroid(axis) < cut;
188 }
189 );
190 int m= std::distance(primitives.data(), pm);
191
192 // la repartition peut echouer, et toutes les primitives sont dans la meme moitiee de l'englobant
193 // forcer quand meme un decoupage en 2 ensembles
194 if(m == begin || m == end)
195 m= (begin + end) / 2;
196 assert(m != begin);
197 assert(m != end);
198
199 // construire le fils gauche, les triangles se trouvent dans [begin .. m)
200 int left= build(begin, m);
201
202 // on recommence pour le fils droit, les triangles se trouvent dans [m .. end)
203 int right= build(m, end);
204
205 // construire le noeud et renvoyer son indice
206 int index= nodes.size();
207 nodes.push_back( make_node( BBox(nodes[left].bounds, nodes[right].bounds), left, right ) );
208 return index;
209 }
Node make_leaf(const BBox &bounds, const int begin, const int end)
creation d'une feuille.
Node make_node(const BBox &bounds, const int left, const int right)
creation d'un noeud interne.

◆ primitive_bounds() [1/3]

template<typename T>
BBox BVHT< T >::primitive_bounds ( const int begin,
const int end )
inlineprotected

Definition at line 212 of file tuto_bvh2.cpp.

213 {
214 BBox bbox= primitives[begin].bounds();
215 for(int i= begin +1; i < end; i++)
216 bbox.insert(primitives[i].bounds());
217
218 return bbox;
219 }

◆ centroid_bounds() [1/3]

template<typename T>
BBox BVHT< T >::centroid_bounds ( const int begin,
const int end )
inlineprotected

Definition at line 222 of file tuto_bvh2.cpp.

223 {
224 BBox bbox= primitives[begin].bounds().centroid();
225 for(int i= begin +1; i < end; i++)
226 bbox.insert(primitives[i].bounds().centroid());
227
228 return bbox;
229 }

◆ intersect() [3/9]

template<typename T>
void BVHT< T >::intersect ( const int index,
const Ray & ray,
const Vector & invd,
Hit & hit ) const
inlineprotected

Definition at line 232 of file tuto_bvh2.cpp.

233 {
234 const Node& node= nodes[index];
235 if(node.bounds.intersect(ray, invd, hit.t))
236 {
237 if(node.leaf())
238 {
239 for(int i= node.leaf_begin(); i < node.leaf_end(); i++)
240 if(Hit h= primitives[i].intersect(ray, hit.t))
241 hit= h;
242 }
243 else // if(node.internal())
244 {
245 intersect(node.internal_left(), ray, invd, hit);
246 intersect(node.internal_right(), ray, invd, hit);
247 }
248 }
249 }

◆ build() [3/6]

template<typename T>
int BVHT< T >::build ( const std::vector< T > & _primitives)
inline

Definition at line 135 of file tuto_bvh2_gltf.cpp.

136 {
137 primitives= _primitives; // copie les primitives pour les trier
138 nodes.clear(); // efface les noeuds
139 nodes.reserve(primitives.size());
140
141 // construit l'arbre...
142 root= build(0, primitives.size());
143 return root;
144 }

◆ intersect() [4/9]

template<typename T>
Hit BVHT< T >::intersect ( const Ray & ray,
const float htmax ) const
inline

Definition at line 147 of file tuto_bvh2_gltf.cpp.

148 {
149 Hit hit;
150 hit.t= htmax;
151 Vector invd= Vector(1 / ray.d.x, 1 / ray.d.y, 1 / ray.d.z);
152 intersect(root, ray, invd, hit);
153 return hit;
154 }

◆ intersect() [5/9]

template<typename T>
Hit BVHT< T >::intersect ( const Ray & ray) const
inline

Definition at line 157 of file tuto_bvh2_gltf.cpp.

157{ return intersect(ray, ray.tmax); }

◆ build() [4/6]

template<typename T>
int BVHT< T >::build ( const int begin,
const int end )
inlineprotected

Definition at line 164 of file tuto_bvh2_gltf.cpp.

165 {
166 if(end - begin < 2)
167 {
168 // inserer une feuille et renvoyer son indice
169 int index= nodes.size();
170 nodes.push_back( make_leaf( primitive_bounds(begin, end), begin, end ) );
171 return index;
172 }
173
174 // axe le plus etire de l'englobant des centres des englobants des primitives...
175 BBox cbounds= centroid_bounds(begin, end);
176 Vector d= Vector(cbounds.pmin, cbounds.pmax);
177 int axis;
178 if(d.x > d.y && d.x > d.z) // x plus grand que y et z ?
179 axis= 0;
180 else if(d.y > d.z) // y plus grand que z ? (et que x implicitement)
181 axis= 1;
182 else // x et y ne sont pas les plus grands...
183 axis= 2;
184
185 // coupe l'englobant au milieu
186 float cut= cbounds.centroid(axis);
187
188 // repartit les primitives
189 T *pm= std::partition(primitives.data() + begin, primitives.data() + end,
190 [axis, cut]( const T& primitive )
191 {
192 return primitive.bounds().centroid(axis) < cut;
193 }
194 );
195 int m= std::distance(primitives.data(), pm);
196
197 // la repartition peut echouer, et toutes les primitives sont dans la meme moitiee de l'englobant
198 // forcer quand meme un decoupage en 2 ensembles
199 if(m == begin || m == end)
200 m= (begin + end) / 2;
201 assert(m != begin);
202 assert(m != end);
203
204 // construire le fils gauche, les primtives se trouvent dans [begin .. m)
205 int left= build(begin, m);
206
207 // on recommence pour le fils droit, les primtives se trouvent dans [m .. end)
208 int right= build(m, end);
209
210 // construire le noeud et renvoyer son indice
211 int index= nodes.size();
212 nodes.push_back( make_node( BBox(nodes[left].bounds, nodes[right].bounds), left, right ) );
213 return index;
214 }

◆ primitive_bounds() [2/3]

template<typename T>
BBox BVHT< T >::primitive_bounds ( const int begin,
const int end )
inlineprotected

Definition at line 217 of file tuto_bvh2_gltf.cpp.

218 {
219 BBox bbox= primitives[begin].bounds();
220 for(int i= begin +1; i < end; i++)
221 bbox.insert(primitives[i].bounds());
222
223 return bbox;
224 }

◆ centroid_bounds() [2/3]

template<typename T>
BBox BVHT< T >::centroid_bounds ( const int begin,
const int end )
inlineprotected

Definition at line 227 of file tuto_bvh2_gltf.cpp.

228 {
229 BBox bbox= primitives[begin].bounds().centroid();
230 for(int i= begin +1; i < end; i++)
231 bbox.insert(primitives[i].bounds().centroid());
232
233 return bbox;
234 }

◆ intersect() [6/9]

template<typename T>
void BVHT< T >::intersect ( const int index,
const Ray & ray,
const Vector & invd,
Hit & hit ) const
inlineprotected

Definition at line 237 of file tuto_bvh2_gltf.cpp.

238 {
239 const Node& node= nodes[index];
240 if(node.bounds.intersect(ray, invd, hit.t))
241 {
242 if(node.leaf())
243 {
244 for(int i= node.leaf_begin(); i < node.leaf_end(); i++)
245 if(Hit h= primitives[i].intersect(ray, hit.t))
246 hit= h;
247 }
248 else // if(node.internal())
249 {
250 intersect(node.internal_left(), ray, invd, hit);
251 intersect(node.internal_right(), ray, invd, hit);
252 }
253 }
254 }

◆ build() [5/6]

template<typename T>
int BVHT< T >::build ( const std::vector< T > & _primitives)
inline

Definition at line 135 of file tuto_bvh2_gltf_brdf.cpp.

136 {
137 primitives= _primitives; // copie les primitives pour les trier
138 nodes.clear(); // efface les noeuds
139 nodes.reserve(primitives.size());
140
141 // construit l'arbre...
142 root= build(0, primitives.size());
143 return root;
144 }

◆ intersect() [7/9]

template<typename T>
Hit BVHT< T >::intersect ( const Ray & ray,
const float htmax ) const
inline

Definition at line 147 of file tuto_bvh2_gltf_brdf.cpp.

148 {
149 Hit hit;
150 hit.t= htmax;
151 Vector invd= Vector(1 / ray.d.x, 1 / ray.d.y, 1 / ray.d.z);
152 intersect(root, ray, invd, hit);
153 return hit;
154 }

◆ intersect() [8/9]

template<typename T>
Hit BVHT< T >::intersect ( const Ray & ray) const
inline

Definition at line 157 of file tuto_bvh2_gltf_brdf.cpp.

157{ return intersect(ray, ray.tmax); }

◆ build() [6/6]

template<typename T>
int BVHT< T >::build ( const int begin,
const int end )
inlineprotected

Definition at line 164 of file tuto_bvh2_gltf_brdf.cpp.

165 {
166 if(end - begin < 2)
167 {
168 // inserer une feuille et renvoyer son indice
169 int index= nodes.size();
170 nodes.push_back( make_leaf( primitive_bounds(begin, end), begin, end ) );
171 return index;
172 }
173
174 // axe le plus etire de l'englobant des centres des englobants des primitives...
175 BBox cbounds= centroid_bounds(begin, end);
176 Vector d= Vector(cbounds.pmin, cbounds.pmax);
177 int axis;
178 if(d.x > d.y && d.x > d.z) // x plus grand que y et z ?
179 axis= 0;
180 else if(d.y > d.z) // y plus grand que z ? (et que x implicitement)
181 axis= 1;
182 else // x et y ne sont pas les plus grands...
183 axis= 2;
184
185 // coupe l'englobant au milieu
186 float cut= cbounds.centroid(axis);
187
188 // repartit les primitives
189 T *pm= std::partition(primitives.data() + begin, primitives.data() + end,
190 [axis, cut]( const T& primitive )
191 {
192 return primitive.bounds().centroid(axis) < cut;
193 }
194 );
195 int m= std::distance(primitives.data(), pm);
196
197 // la repartition peut echouer, et toutes les primitives sont dans la meme moitiee de l'englobant
198 // forcer quand meme un decoupage en 2 ensembles
199 if(m == begin || m == end)
200 m= (begin + end) / 2;
201 assert(m != begin);
202 assert(m != end);
203
204 // construire le fils gauche, les primtives se trouvent dans [begin .. m)
205 int left= build(begin, m);
206
207 // on recommence pour le fils droit, les primtives se trouvent dans [m .. end)
208 int right= build(m, end);
209
210 // construire le noeud et renvoyer son indice
211 int index= nodes.size();
212 nodes.push_back( make_node( BBox(nodes[left].bounds, nodes[right].bounds), left, right ) );
213 return index;
214 }

◆ primitive_bounds() [3/3]

template<typename T>
BBox BVHT< T >::primitive_bounds ( const int begin,
const int end )
inlineprotected

Definition at line 217 of file tuto_bvh2_gltf_brdf.cpp.

218 {
219 BBox bbox= primitives[begin].bounds();
220 for(int i= begin +1; i < end; i++)
221 bbox.insert(primitives[i].bounds());
222
223 return bbox;
224 }

◆ centroid_bounds() [3/3]

template<typename T>
BBox BVHT< T >::centroid_bounds ( const int begin,
const int end )
inlineprotected

Definition at line 227 of file tuto_bvh2_gltf_brdf.cpp.

228 {
229 BBox bbox= primitives[begin].bounds().centroid();
230 for(int i= begin +1; i < end; i++)
231 bbox.insert(primitives[i].bounds().centroid());
232
233 return bbox;
234 }

◆ intersect() [9/9]

template<typename T>
void BVHT< T >::intersect ( const int index,
const Ray & ray,
const Vector & invd,
Hit & hit ) const
inlineprotected

Definition at line 237 of file tuto_bvh2_gltf_brdf.cpp.

238 {
239 const Node& node= nodes[index];
240 if(node.bounds.intersect(ray, invd, hit.t))
241 {
242 if(node.leaf())
243 {
244 for(int i= node.leaf_begin(); i < node.leaf_end(); i++)
245 if(Hit h= primitives[i].intersect(ray, hit.t))
246 hit= h;
247 }
248 else // if(node.internal())
249 {
250 intersect(node.internal_left(), ray, invd, hit);
251 intersect(node.internal_right(), ray, invd, hit);
252 }
253 }
254 }

Member Data Documentation

◆ nodes

template<typename T>
std::vector< Node > BVHT< T >::nodes
protected

Definition at line 155 of file tuto_bvh2.cpp.

◆ primitives

template<typename T>
std::vector< T > BVHT< T >::primitives
protected

Definition at line 156 of file tuto_bvh2.cpp.

◆ root

template<typename T>
int BVHT< T >::root
protected

Definition at line 157 of file tuto_bvh2.cpp.


The documentation for this struct was generated from the following files: