Skip to content

Commit 52294ac

Browse files
authored
Merge pull request #9 from ingowald/main
added new traversal template for two-level BVH traversal
2 parents 0f0340c + a6eebd5 commit 52294ac

6 files changed

Lines changed: 416 additions & 147 deletions

File tree

CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,6 @@ endif()
133133
message("#cuBQL: compiling with CMAKE_CUDA_ARCHITECTURES=${CMAKE_CUDA_ARCHITECTURES}")
134134
add_subdirectory(cuBQL)
135135

136-
option(CUBQL_ENABLE_TESTING "Enable Testing?" OFF)
137136
if (NOT CUBQL_IS_SUBPROJECT)
138137
add_subdirectory(samples)
139-
if (CUBQL_ENABLE_TESTING)
140-
add_subdirectory(testing)
141-
endif()
142138
endif()

cuBQL/math/Ray.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace cuBQL {
2626

2727
using ray3f = ray_t<float>;
2828
using ray3d = ray_t<double>;
29-
using Ray = ray_t<float>;
29+
using Ray = ray_t<float>;
3030

3131
template<int /*! 0, 1, or 2 */axis, int /* +1 or -1 */sign>
3232
struct AxisAlignedRay {
@@ -44,9 +44,9 @@ namespace cuBQL {
4444
inline __cubql_both
4545
bool rayIntersectsBox(ray_t<T> ray, box_t<T,3> box);
4646

47-
// =============================================================================
47+
// ========================================================================
4848
// *** IMPLEMENTATION ***
49-
// =============================================================================
49+
// ========================================================================
5050

5151
template<typename T>
5252
inline __cubql_both ray_t<T>::ray_t(typename ray_t<T>::vec3 org,

cuBQL/math/affine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace cuBQL {
3030
p(ZeroTy())
3131
{}
3232

33-
inline __cubql_both AffineSpaceT(const AffineSpaceT &other) = default;
33+
inline AffineSpaceT(const AffineSpaceT &other) = default;
3434

3535
inline __cubql_both AffineSpaceT(const L &other)
3636
{

cuBQL/queries/triangleData/Triangle.h

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,41 @@
1111

1212
namespace cuBQL {
1313

14-
// =============================================================================
14+
// =========================================================================
1515
// *** INTERFACE ***
16-
// =============================================================================
16+
// =========================================================================
1717

18-
/*! a simple triangle consisting of three vertices. In order to not
19-
overload this class with too many functions the actual
20-
operations on triangles - such as intersectin with a ray,
21-
computing distance to a point, etc - will be defined in the
22-
respective queries */
23-
struct Triangle {
24-
/*! returns an axis aligned bounding box enclosing this triangle */
25-
inline __cubql_both box3f bounds() const;
26-
inline __cubql_both vec3f sample(float u, float v) const;
27-
inline __cubql_both vec3f normal() const;
18+
// /*! a simple triangle consisting of three vertices. In order to not
19+
// overload this class with too many functions the actual
20+
// operations on triangles - such as intersectin with a ray,
21+
// computing distance to a point, etc - will be defined in the
22+
// respective queries */
23+
// struct Triangle {
24+
// /*! returns an axis aligned bounding box enclosing this triangle */
25+
// inline __cubql_both box3f bounds() const;
26+
// inline __cubql_both vec3f sample(float u, float v) const;
27+
// inline __cubql_both vec3f normal() const;
2828

29-
vec3f a, b, c;
29+
// vec3f a, b, c;
30+
// };
31+
32+
template<typename T>
33+
struct triangle_t
34+
{
35+
using vec3 = vec_t<T,3>;
36+
using box3 = box_t<T,3>;
37+
38+
inline __cubql_both box3 bounds() const;
39+
inline __cubql_both vec3 sample(float u, float v) const;
40+
inline __cubql_both vec3 normal() const;
41+
42+
vec3 a;
43+
vec3 b;
44+
vec3 c;
3045
};
3146

47+
using Triangle = triangle_t<float>;
48+
3249
/*! a typical triangle mesh, with array of vertices and
3350
indices. This class will NOT do any allocation/deallocation, not
3451
use smart pointers - it's just a 'view' on what whoever else
@@ -51,18 +68,10 @@ namespace cuBQL {
5168
int numIndices;
5269
};
5370

54-
template<typename T>
55-
struct triangle_t
56-
{
57-
using vec3 = vec_t<T,3>;
58-
vec3 a;
59-
vec3 b;
60-
vec3 c;
61-
};
6271

63-
// =============================================================================
72+
// ========================================================================
6473
// *** IMPLEMENTATION ***
65-
// =============================================================================
74+
// ========================================================================
6675

6776
// ---------------------- TriangleMesh ----------------------
6877
inline __cubql_both Triangle TriangleMesh::getTriangle(int i) const
@@ -72,23 +81,38 @@ namespace cuBQL {
7281
}
7382

7483
// ---------------------- Triangle ----------------------
75-
inline __cubql_both vec3f Triangle::normal() const
84+
template<typename T>
85+
inline __cubql_both vec_t<T,3> triangle_t<T>::normal() const
7686
{ return cross(b-a,c-a); }
7787

78-
inline __cubql_both box3f Triangle::bounds() const
79-
{ return box3f().including(a).including(b).including(c); }
88+
template<typename T>
89+
inline __cubql_both box_t<T,3> triangle_t<T>::bounds() const
90+
{ return box_t<T,3>().including(a).including(b).including(c); }
8091

81-
inline __cubql_both float area(Triangle tri)
92+
template<typename T>
93+
inline __cubql_both float area(triangle_t<T> tri)
8294
{ return length(cross(tri.b-tri.a,tri.c-tri.a)); }
8395

84-
inline __cubql_both vec3f Triangle::sample(float u, float v) const
96+
template<typename T>
97+
inline __cubql_both vec_t<T,3>
98+
triangle_t<T>::sample(float u, float v) const
8599
{
86100
if (u+v >= 1.f) { u = 1.f-u; v = 1.f-v; }
87101
return (1.f-u-v)*a + u * b + v * c;
88102
}
103+
// inline __cubql_both vec3f Triangle::sample(float u, float v) const
104+
// {
105+
// if (u+v >= 1.f) { u = 1.f-u; v = 1.f-v; }
106+
// return (1.f-u-v)*a + u * b + v * c;
107+
// }
89108

90-
inline __cubql_both dbgout operator<<(dbgout o, const Triangle &triangle)
91-
{ o << "{" << triangle.a << "," << triangle.b << "," << triangle.c << "}"; return o; }
109+
template<typename T>
110+
inline __cubql_both
111+
dbgout operator<<(dbgout o, const triangle_t<T> &triangle)
112+
{
113+
o << "{" << triangle.a << "," << triangle.b << "," << triangle.c << "}";
114+
return o;
115+
}
92116

93117

94118
} // ::cuBQL

cuBQL/queries/triangleData/math/rayTriangleIntersections.h

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,49 @@
88

99
namespace cuBQL {
1010

11-
// =============================================================================
11+
// ========================================================================
1212
// *** INTERFACE ***
13-
// =============================================================================
13+
// ========================================================================
1414

15-
struct RayTriangleIntersection {
16-
vec3f N;
17-
float t,u,v;
15+
// struct RayTriangleIntersection {
16+
// vec3f N;
17+
// float t,u,v;
1818

19-
inline __cubql_both bool compute(Ray ray, Triangle tri);
20-
};
19+
// inline __cubql_both bool compute(Ray ray, Triangle tri);
20+
// };
2121

2222
template<typename T>
2323
struct RayTriangleIntersection_t {
2424
using vec3 = vec_t<T,3>;
2525
T t=0,u=0,v=0;
26+
vec3 N;
2627

2728
inline __cubql_both bool compute(const ray_t<T> &ray,
28-
const triangle_t<T> &tri);
29+
const triangle_t<T> &tri,
30+
bool dbg=false);
2931
};
3032

33+
using RayTriangleIntersection = RayTriangleIntersection_t<float>;
3134

32-
// =============================================================================
35+
// ========================================================================
3336
// *** IMPLEMENTATION ***
34-
// =============================================================================
37+
// ========================================================================
3538

3639
template<typename T>
3740
inline __cubql_both
3841
bool RayTriangleIntersection_t<T>::compute(const ray_t<T> &ray,
39-
const triangle_t<T> &tri)
42+
const triangle_t<T> &tri,
43+
bool dbg)
4044
{
4145
using vec3 = vec_t<T,3>;
4246
const vec3 v0(tri.a);
4347
const vec3 v1(tri.b);
4448
const vec3 v2(tri.c);
45-
49+
4650
const vec3 e1 = v1-v0;
4751
const vec3 e2 = v2-v0;
4852

49-
vec3 N = cross(e1,e2);
53+
N = cross(e1,e2);
5054
if (N == vec3(T(0)))
5155
return false;
5256

@@ -59,7 +63,7 @@ namespace cuBQL {
5963
// t*dot(d,N) = -dot(o-v0,N)
6064
// t = -dot(o-v0,N)/dot(d,N)
6165
t = -dot(ray.origin-v0,N)/dot(ray.direction,N);
62-
if (t < ray.tMin || t > ray.tMax) return false;
66+
if (t <= ray.tMin || t >= ray.tMax) return false;
6367

6468
vec3 P = (ray.origin - v0) + t*ray.direction;
6569

@@ -82,7 +86,7 @@ namespace cuBQL {
8286
// (P-v0) = [e1,e2]*(u,v,h)
8387
if (det(e1u,e1v,e2u,e2v) == T(0)) return false;
8488

85-
#if 1
89+
#if 0
8690
T den = det(e1u,e2u,e1v,e2v);
8791
T sign = den < T(0) ? T(-1):T(1);
8892
den *= sign;
@@ -103,60 +107,50 @@ namespace cuBQL {
103107
return true;
104108
}
105109

106-
inline __cubql_both
107-
bool RayTriangleIntersection::compute(Ray ray, Triangle tri)
108-
{
109-
const vec3f v0 = tri.a;
110-
const vec3f v1 = tri.b;
111-
const vec3f v2 = tri.c;
110+
// inline __cubql_both
111+
// bool RayTriangleIntersection::compute(Ray ray, Triangle tri)
112+
// {
113+
// const vec3f v0 = tri.a;
114+
// const vec3f v1 = tri.b;
115+
// const vec3f v2 = tri.c;
112116

113-
const vec3f e1 = v1-v0;
114-
const vec3f e2 = v2-v0;
115-
116-
N = cross(e1,e2);
117-
if (N == vec3f(0.f)) return false;
117+
// const vec3f e1 = v1-v0;
118+
// const vec3f e2 = v2-v0;
118119

119-
// N = normalize(N);
120-
if (fabsf(dot(ray.direction,N)) < 1e-12f) return false;
120+
// vec3f N = cross(e1,e2);
121+
// if (fabsf(dot(ray.direction,N)) < 1e-12f) return false;
121122

122-
// P = o+td
123-
// dot(P-v0,N) = 0
124-
// dot(o+td-v0,N) = 0
125-
// dot(td,N)+dot(o-v0,N)=0
126-
// t*dot(d,N) = -dot(o-v0,N)
127-
// t = -dot(o-v0,N)/dot(d,N)
128-
t = -dot(ray.origin-v0,N)/dot(ray.direction,N);
123+
// t = -dot(ray.origin-v0,N)/dot(ray.direction,N);
129124

130-
if (t < ray.tMin || t > ray.tMax) return false;
125+
// if (t <= 0.f || t >= ray.tMax) return false;
131126

132-
vec3f P = (ray.origin - v0) + t*ray.direction;
127+
// vec3f P = ray.origin - v0 + t*ray.direction;
133128

134-
float e1u,e2u,Pu;
135-
float e1v,e2v,Pv;
136-
if (fabsf(N.x) >= max(fabsf(N.y),fabsf(N.z))) {
137-
e1u = e1.y; e2u = e2.y; Pu = P.y;
138-
e1v = e1.z; e2v = e2.z; Pv = P.z;
139-
} else if (fabsf(N.y) > fabsf(N.z)) {
140-
e1u = e1.x; e2u = e2.x; Pu = P.x;
141-
e1v = e1.z; e2v = e2.z; Pv = P.z;
142-
} else {
143-
e1u = e1.x; e2u = e2.x; Pu = P.x;
144-
e1v = e1.y; e2v = e2.y; Pv = P.y;
145-
}
146-
auto det = [](float a, float b, float c, float d) -> float
147-
{ return a*d - c*b; };
129+
// float e1u,e2u,Pu;
130+
// float e1v,e2v,Pv;
131+
// if (fabsf(N.x) >= max(fabsf(N.y),fabsf(N.z))) {
132+
// e1u = e1.y; e2u = e2.y; Pu = P.y;
133+
// e1v = e1.z; e2v = e2.z; Pv = P.z;
134+
// } else if (fabsf(N.y) > fabsf(N.z)) {
135+
// e1u = e1.x; e2u = e2.x; Pu = P.x;
136+
// e1v = e1.z; e2v = e2.z; Pv = P.z;
137+
// } else {
138+
// e1u = e1.x; e2u = e2.x; Pu = P.x;
139+
// e1v = e1.y; e2v = e2.y; Pv = P.y;
140+
// }
141+
// auto det = [](float a, float b, float c, float d) -> float
142+
// { return a*d - c*b; };
148143

149-
// P = v0 + u * e1 + v * e2 + h * N
150-
// (P-v0) = [e1,e2]*(u,v,h)
151-
if (det(e1u,e1v,e2u,e2v) == 0.f) return false;
144+
// // P = v0 + u * e1 + v * e2 + h * N
145+
// // (P-v0) = [e1,e2]*(u,v,h)
146+
// if (det(e1u,e1v,e2u,e2v) == 0.f) return false;
152147

153-
u = det(Pu,e2u,Pv,e2v)/det(e1u,e2u,e1v,e2v);
154-
v = det(e1u,Pu,e1v,Pv)/det(e1u,e2u,e1v,e2v);
155-
156-
if ((u < 0.f) || (v < 0.f) || ((u+v) > 1.f)) return false;
148+
// u = det(Pu,e2u,Pv,e2v)/det(e1u,e2u,e1v,e2v);
149+
// v = det(e1u,Pu,e1v,Pv)/det(e1u,e2u,e1v,e2v);
150+
// if ((u < 0.f) || (v < 0.f) || ((u+v) >= 1.f)) return false;
157151

158-
return true;
159-
}
152+
// return true;
153+
// }
160154

161155

162156

0 commit comments

Comments
 (0)