25 for (
Size i = 0; i < 3; ++i) {
26 invDir[i] = (dir[i] == 0._f) ?
INFTY : 1._f / dir[i];
27 signs[i] = int(invDir[i] < 0._f);
89 if (a > -eps && a < eps) {
92 const Float f = 1._f / a;
95 if (u < 0._f || u > 1._f) {
100 if (v < 0._f || u + v > 1._f) {
103 intersection.
object =
this;
104 intersection.
t = f *
dot(dir2, q);
105 return intersection.
t > 0.f;
117 return v0 + (dir1 + dir2) / 3._f;
142 const Float disc =
sqr(deltaCos) - deltaSqr + rSqr;
147 intersection.
object =
this;
148 intersection.
t = deltaCos -
sqrt(disc);
149 return intersection.
t > 0.f;
172 const bool result =
intersectBox(box, ray, t_min, t_max);
174 intersection.
t = t_min;
175 intersection.
object =
this;
176 return intersection.
t > 0.f;
197 template <
typename TBvhObject>
217 : leafSize(leafSize) {}
233 template <
typename TOutIter>
243 template <
typename TAddIntersection>
244 void getIntersections(
const Ray& ray,
const TAddIntersection& addIntersection)
const;
Generic dynamically allocated resizable storage.
#define SPH_ASSERT(x,...)
Simplified implementation of std::unique_ptr, using only default deleter.
Object representing a three-dimensional axis-aligned box.
bool intersectBox(const Box &box, const Ray &ray, Float &t_min, Float &t_max)
Finds intersections of ray with a box.
uint32_t Size
Integral type used to index arrays (by default).
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Vectorized computations with integral numbers.
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
INLINE T sqrt(const T f)
Return a squared root of a value.
#define INLINE
Macros for conditional compilation based on selected compiler.
#define NAMESPACE_SPH_END
INLINE Float getSqrLength(const Vector &v)
INLINE BasicVector< float > cross(const BasicVector< float > &v1, const BasicVector< float > &v2)
Cross product between two vectors.
BasicVector< Float > Vector
INLINE float dot(const BasicVector< float > &v1, const BasicVector< float > &v2)
Make sure the vector is trivially constructible and destructible, needed for fast initialization of a...
Helper object defining three-dimensional interval (box).
INLINE void extend(const Vector &v)
Enlarges the box to contain the vector.
INLINE Vector center() const
Returns the center of the box.
Trait for finding intersections with a axis-aligned box.
INLINE Box getBBox() const
INLINE bool getIntersection(const Ray &ray, IntersectionInfo &intersection) const
INLINE Vector getCenter() const
Trait for finding intersections with a sphere.
BvhSphere(const Vector ¢er, const Float radius)
INLINE Box getBBox() const
INLINE bool getIntersection(const Ray &ray, IntersectionInfo &intersection) const
INLINE Vector getCenter() const
Trait for finding intersections with a triangle.
INLINE bool getIntersection(const Ray &ray, IntersectionInfo &intersection) const
INLINE Vector getCenter() const
BvhTriangle(const Vector &v0, const Vector &v1, const Vector &v2)
INLINE Box getBBox() const
Simple bounding volume hierarchy.
Bvh(const Size leafSize=4)
void build(Array< TBvhObject > &&objects)
Contructs the BVH from given set of objects.
Box getBoundingBox() const
Returns the bounding box of all objects in BVH.
bool isOccluded(const Ray &ray) const
Returns true if the ray is occluded by some geometry.
Size getAllIntersections(const Ray &ray, TOutIter iter) const
Returns all intersections of the ray.
bool getFirstIntersection(const Ray &ray, IntersectionInfo &intersection) const
Finds the closest intersection of the ray.
Helper object for storing three (possibly four) int or bool values.
const Vector & direction() const
friend bool intersectBox(const Box &box, const Ray &ray, Float &t_min, Float &t_max)
Finds intersections of ray with a box.
const Vector & origin() const
Ray(const Vector &origin, const Vector &dir)
Size userData
Generic user data, can be used to store additional information to the primitives.
Holds intormation about intersection.
Float t
Distance of the hit in units of ray.direction().
INLINE bool operator<(const IntersectionInfo &other) const
Sort by intersection distance.
const BvhPrimitive * object
Object hit by the ray, or nullptr if nothing has been hit.
INLINE Vector hit(const Ray &ray) const
Object with deleted copy constructor and copy operator.