Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ endif()
message("#cuBQL: compiling with CMAKE_CUDA_ARCHITECTURES=${CMAKE_CUDA_ARCHITECTURES}")
add_subdirectory(cuBQL)

option(CUBQL_ENABLE_TESTING "Enable Testing?" OFF)
if (NOT CUBQL_IS_SUBPROJECT)
add_subdirectory(samples)
if (CUBQL_ENABLE_TESTING)
add_subdirectory(testing)
endif()
endif()
6 changes: 3 additions & 3 deletions cuBQL/math/Ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace cuBQL {

using ray3f = ray_t<float>;
using ray3d = ray_t<double>;
using Ray = ray_t<float>;
using Ray = ray_t<float>;

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

// =============================================================================
// ========================================================================
// *** IMPLEMENTATION ***
// =============================================================================
// ========================================================================

template<typename T>
inline __cubql_both ray_t<T>::ray_t(typename ray_t<T>::vec3 org,
Expand Down
2 changes: 1 addition & 1 deletion cuBQL/math/affine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace cuBQL {
p(ZeroTy())
{}

inline __cubql_both AffineSpaceT(const AffineSpaceT &other) = default;
inline AffineSpaceT(const AffineSpaceT &other) = default;

inline __cubql_both AffineSpaceT(const L &other)
{
Expand Down
84 changes: 54 additions & 30 deletions cuBQL/queries/triangleData/Triangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,41 @@

namespace cuBQL {

// =============================================================================
// =========================================================================
// *** INTERFACE ***
// =============================================================================
// =========================================================================

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

vec3f a, b, c;
// vec3f a, b, c;
// };

template<typename T>
struct triangle_t
{
using vec3 = vec_t<T,3>;
using box3 = box_t<T,3>;

inline __cubql_both box3 bounds() const;
inline __cubql_both vec3 sample(float u, float v) const;
inline __cubql_both vec3 normal() const;

vec3 a;
vec3 b;
vec3 c;
};

using Triangle = triangle_t<float>;

/*! a typical triangle mesh, with array of vertices and
indices. This class will NOT do any allocation/deallocation, not
use smart pointers - it's just a 'view' on what whoever else
Expand All @@ -51,18 +68,10 @@ namespace cuBQL {
int numIndices;
};

template<typename T>
struct triangle_t
{
using vec3 = vec_t<T,3>;
vec3 a;
vec3 b;
vec3 c;
};

// =============================================================================
// ========================================================================
// *** IMPLEMENTATION ***
// =============================================================================
// ========================================================================

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

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

inline __cubql_both box3f Triangle::bounds() const
{ return box3f().including(a).including(b).including(c); }
template<typename T>
inline __cubql_both box_t<T,3> triangle_t<T>::bounds() const
{ return box_t<T,3>().including(a).including(b).including(c); }

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

inline __cubql_both vec3f Triangle::sample(float u, float v) const
template<typename T>
inline __cubql_both vec_t<T,3>
triangle_t<T>::sample(float u, float v) const
{
if (u+v >= 1.f) { u = 1.f-u; v = 1.f-v; }
return (1.f-u-v)*a + u * b + v * c;
}
// inline __cubql_both vec3f Triangle::sample(float u, float v) const
// {
// if (u+v >= 1.f) { u = 1.f-u; v = 1.f-v; }
// return (1.f-u-v)*a + u * b + v * c;
// }

inline __cubql_both dbgout operator<<(dbgout o, const Triangle &triangle)
{ o << "{" << triangle.a << "," << triangle.b << "," << triangle.c << "}"; return o; }
template<typename T>
inline __cubql_both
dbgout operator<<(dbgout o, const triangle_t<T> &triangle)
{
o << "{" << triangle.a << "," << triangle.b << "," << triangle.c << "}";
return o;
}


} // ::cuBQL
Expand Down
114 changes: 54 additions & 60 deletions cuBQL/queries/triangleData/math/rayTriangleIntersections.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,49 @@

namespace cuBQL {

// =============================================================================
// ========================================================================
// *** INTERFACE ***
// =============================================================================
// ========================================================================

struct RayTriangleIntersection {
vec3f N;
float t,u,v;
// struct RayTriangleIntersection {
// vec3f N;
// float t,u,v;

inline __cubql_both bool compute(Ray ray, Triangle tri);
};
// inline __cubql_both bool compute(Ray ray, Triangle tri);
// };

template<typename T>
struct RayTriangleIntersection_t {
using vec3 = vec_t<T,3>;
T t=0,u=0,v=0;
vec3 N;

inline __cubql_both bool compute(const ray_t<T> &ray,
const triangle_t<T> &tri);
const triangle_t<T> &tri,
bool dbg=false);
};

using RayTriangleIntersection = RayTriangleIntersection_t<float>;

// =============================================================================
// ========================================================================
// *** IMPLEMENTATION ***
// =============================================================================
// ========================================================================

template<typename T>
inline __cubql_both
bool RayTriangleIntersection_t<T>::compute(const ray_t<T> &ray,
const triangle_t<T> &tri)
const triangle_t<T> &tri,
bool dbg)
{
using vec3 = vec_t<T,3>;
const vec3 v0(tri.a);
const vec3 v1(tri.b);
const vec3 v2(tri.c);

const vec3 e1 = v1-v0;
const vec3 e2 = v2-v0;

vec3 N = cross(e1,e2);
N = cross(e1,e2);
if (N == vec3(T(0)))
return false;

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

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

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

#if 1
#if 0
T den = det(e1u,e2u,e1v,e2v);
T sign = den < T(0) ? T(-1):T(1);
den *= sign;
Expand All @@ -103,60 +107,50 @@ namespace cuBQL {
return true;
}

inline __cubql_both
bool RayTriangleIntersection::compute(Ray ray, Triangle tri)
{
const vec3f v0 = tri.a;
const vec3f v1 = tri.b;
const vec3f v2 = tri.c;
// inline __cubql_both
// bool RayTriangleIntersection::compute(Ray ray, Triangle tri)
// {
// const vec3f v0 = tri.a;
// const vec3f v1 = tri.b;
// const vec3f v2 = tri.c;

const vec3f e1 = v1-v0;
const vec3f e2 = v2-v0;

N = cross(e1,e2);
if (N == vec3f(0.f)) return false;
// const vec3f e1 = v1-v0;
// const vec3f e2 = v2-v0;

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

// P = o+td
// dot(P-v0,N) = 0
// dot(o+td-v0,N) = 0
// dot(td,N)+dot(o-v0,N)=0
// t*dot(d,N) = -dot(o-v0,N)
// t = -dot(o-v0,N)/dot(d,N)
t = -dot(ray.origin-v0,N)/dot(ray.direction,N);
// t = -dot(ray.origin-v0,N)/dot(ray.direction,N);

if (t < ray.tMin || t > ray.tMax) return false;
// if (t <= 0.f || t >= ray.tMax) return false;

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

float e1u,e2u,Pu;
float e1v,e2v,Pv;
if (fabsf(N.x) >= max(fabsf(N.y),fabsf(N.z))) {
e1u = e1.y; e2u = e2.y; Pu = P.y;
e1v = e1.z; e2v = e2.z; Pv = P.z;
} else if (fabsf(N.y) > fabsf(N.z)) {
e1u = e1.x; e2u = e2.x; Pu = P.x;
e1v = e1.z; e2v = e2.z; Pv = P.z;
} else {
e1u = e1.x; e2u = e2.x; Pu = P.x;
e1v = e1.y; e2v = e2.y; Pv = P.y;
}
auto det = [](float a, float b, float c, float d) -> float
{ return a*d - c*b; };
// float e1u,e2u,Pu;
// float e1v,e2v,Pv;
// if (fabsf(N.x) >= max(fabsf(N.y),fabsf(N.z))) {
// e1u = e1.y; e2u = e2.y; Pu = P.y;
// e1v = e1.z; e2v = e2.z; Pv = P.z;
// } else if (fabsf(N.y) > fabsf(N.z)) {
// e1u = e1.x; e2u = e2.x; Pu = P.x;
// e1v = e1.z; e2v = e2.z; Pv = P.z;
// } else {
// e1u = e1.x; e2u = e2.x; Pu = P.x;
// e1v = e1.y; e2v = e2.y; Pv = P.y;
// }
// auto det = [](float a, float b, float c, float d) -> float
// { return a*d - c*b; };

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

u = det(Pu,e2u,Pv,e2v)/det(e1u,e2u,e1v,e2v);
v = det(e1u,Pu,e1v,Pv)/det(e1u,e2u,e1v,e2v);

if ((u < 0.f) || (v < 0.f) || ((u+v) > 1.f)) return false;
// u = det(Pu,e2u,Pv,e2v)/det(e1u,e2u,e1v,e2v);
// v = det(e1u,Pu,e1v,Pv)/det(e1u,e2u,e1v,e2v);
// if ((u < 0.f) || (v < 0.f) || ((u+v) >= 1.f)) return false;

return true;
}
// return true;
// }



Expand Down
Loading