20 #if !defined(__MITSUBA_RENDER_SKDTREE_H_)
21 #define __MITSUBA_RENDER_SKDTREE_H_
27 #if defined(MTS_KD_CONSERVE_MEMORY)
28 #if defined(MTS_HAS_COHERENT_RT)
29 #error MTS_KD_CONSERVE_MEMORY & MTS_HAS_COHERENT_RT are incompatible
33 #if defined(SINGLE_PRECISION)
35 #define MTS_KD_INTERSECTION_TEMP 64
37 #define MTS_KD_INTERSECTION_TEMP 128
72 friend class Instance;
73 friend class AnimatedInstance;
74 friend class SingleScatter;
84 void addShape(
const Shape *shape);
87 inline const std::vector<const Shape *> &
getShapes()
const {
return m_shapes; }
94 return m_shapeMap[m_shapeMap.size()-1];
176 bool rayIntersect(
const Ray &ray)
const;
178 #if defined(MTS_HAS_COHERENT_RT)
190 void rayIntersectPacketIncoherent(
const RayPacket4 &packet,
206 std::vector<IndexType>::const_iterator it = std::lower_bound(
207 m_shapeMap.begin(), m_shapeMap.end(), idx + 1) - 1;
209 return (
IndexType) (it - m_shapeMap.begin());
215 const Shape *shape = m_shapes[shapeIdx];
216 if (m_triangleFlag[shapeIdx]) {
227 const Shape *shape = m_shapes[shapeIdx];
228 if (m_triangleFlag[shapeIdx]) {
253 #if defined(MTS_KD_CONSERVE_MEMORY)
255 if (EXPECT_TAKEN(m_triangleFlag[shapeIdx])) {
257 static_cast<const TriMesh *
>(m_shapes[shapeIdx]);
259 Float tempU, tempV, tempT;
261 tempU, tempV, tempT)) {
262 if (tempT < mint || tempT > maxt)
272 const Shape *shape = m_shapes[shapeIdx];
274 reinterpret_cast<uint8_t*>(temp) + 2*
sizeof(
IndexType))) {
281 const TriAccel &ta = m_triAccel[idx];
282 if (EXPECT_TAKEN(m_triAccel[idx].k != KNoTriangleFlag)) {
283 Float tempU, tempV, tempT;
284 if (ta.
rayIntersect(ray, mint, maxt, tempU, tempV, tempT)) {
294 const Shape *shape = m_shapes[shapeIndex];
296 reinterpret_cast<uint8_t*>(temp) + 2*
sizeof(
IndexType))) {
312 #if defined(MTS_KD_CONSERVE_MEMORY)
314 if (EXPECT_TAKEN(m_triangleFlag[shapeIdx])) {
316 static_cast<const TriMesh *
>(m_shapes[shapeIdx]);
318 Float tempU, tempV, tempT;
320 return tempT >= mint && tempT <= maxt;
323 const Shape *shape = m_shapes[shapeIdx];
327 const TriAccel &ta = m_triAccel[idx];
329 const Shape *shape = m_shapes[shapeIndex];
330 if (EXPECT_TAKEN(m_triAccel[idx].k != KNoTriangleFlag)) {
331 Float tempU, tempV, tempT;
332 return ta.
rayIntersect(ray, mint, maxt, tempU, tempV, tempT);
355 const Vector b(1 - cache->
u - cache->
v, cache->
u, cache->
v);
358 const Point &p0 = vertexPositions[idx0];
359 const Point &p1 = vertexPositions[idx1];
360 const Point &p2 = vertexPositions[idx2];
363 its.
p = p0 * b.x + p1 * b.y + p2 * b.z;
367 Vector side1(p1-p0), side2(p2-p0);
371 faceNormal /= length;
373 if (EXPECT_NOT_TAKEN(vertexTangents)) {
382 if (EXPECT_TAKEN(vertexNormals)) {
384 &n0 = vertexNormals[idx0],
385 &n1 = vertexNormals[idx1],
386 &n2 = vertexNormals[idx2];
392 faceNormal = -faceNormal;
398 if (EXPECT_TAKEN(vertexTexcoords)) {
399 const Point2 &t0 = vertexTexcoords[idx0];
400 const Point2 &t1 = vertexTexcoords[idx1];
401 const Point2 &t2 = vertexTexcoords[idx2];
402 its.
uv = t0 * b.x + t1 * b.y + t2 * b.z;
407 if (EXPECT_NOT_TAKEN(vertexColors)) {
408 const Color3 &c0 = vertexColors[idx0],
409 &c1 = vertexColors[idx1],
410 &c2 = vertexColors[idx2];
411 Color3 result(c0 * b.x + c1 * b.y + c2 * b.z);
413 result[2], Spectrum::EReflectance);
423 reinterpret_cast<const uint8_t*>(temp) + 2*
sizeof(
IndexType), its);
432 Float mint, maxt, tempT = std::numeric_limits<Float>::infinity();
433 if (m_aabb.rayIntersect(ray, mint, maxt)) {
434 if (_mint > mint) mint = _mint;
435 if (_maxt < maxt) maxt = _maxt;
437 if (EXPECT_TAKEN(maxt > mint))
438 return rayIntersectHavran<true>(ray, mint, maxt, tempT, NULL);
445 Float mint, maxt, tempT = std::numeric_limits<Float>::infinity();
446 if (m_aabb.rayIntersect(ray, mint, maxt)) {
447 if (_mint > mint) mint = _mint;
448 if (_maxt < maxt) maxt = _maxt;
450 if (EXPECT_TAKEN(maxt > mint)) {
451 if (rayIntersectHavran<false>(ray, mint, maxt, tempT, temp)) {
463 std::vector<const Shape *> m_shapes;
464 std::vector<bool> m_triangleFlag;
465 std::vector<IndexType> m_shapeMap;
466 #if !defined(MTS_KD_CONSERVE_MEMORY)
const Shape * shape
Pointer to the associated shape.
Definition: shape.h:132
AABB getClippedAABB(const Point *positions, const AABB &aabb) const
Returns the axis-aligned bounding box of a triangle after it has clipped to the extends of another gi...
FINLINE __m128 rayIntersectPacket(const TriAccel &tri, const RayPacket4 &packet, __m128 mint, __m128 maxt, __m128 inactive, Intersection4 &its)
Definition: triaccel_sse.h:27
Temporarily holds some intersection information.
Definition: skdtree.h:237
FINLINE void fillIntersectionRecord(const Ray &ray, const void *temp, Intersection &its) const
After having found a unique intersection, fill a proper record using the temporary information collec...
Definition: skdtree.h:343
Three-dimensional normal data structure.
Definition: normal.h:39
static FINLINE bool rayIntersect(const Point &p0, const Point &p1, const Point &p2, const Ray &ray, Float &u, Float &v, Float &t)
Ray-triangle intersection test.
Definition: triangle.h:109
Abstract triangle mesh base class.
Definition: trimesh.h:68
Stores a three-dimensional orthonormal coordinate frame.
Definition: frame.h:37
KDTreeBase< AABB >::SizeType SizeType
Definition: sahkdtree3.h:110
bool rayIntersect(const Ray &ray, Float _mint, Float _maxt) const
Plain shadow ray query (used by the 'instance' plugin)
Definition: skdtree.h:431
Simple triangle class including a collection of routines for analysis and transformation.
Definition: triangle.h:35
Vector dpdu
Position partials wrt. the UV parameterization.
Definition: shape.h:150
uint32_t shapeIndex
Definition: triaccel.h:50
Normal n
Definition: frame.h:39
Vector dpdu
Position partial with respect to the U parameter of the local chart.
Definition: trimesh.h:40
virtual AABB getAABB() const =0
Return a bounding box containing the shape.
uint32_t idx[3]
Indices into a vertex buffer.
Definition: triangle.h:37
SAH KD-tree acceleration data structure for fast ray-triangle intersections.
Definition: skdtree.h:69
MTS_EXPORT_CORE void computeShadingFrame(const Vector &n, const Vector &dpdu, Frame &frame)
Given a smoothly varying shading normal and a tangent of a shape parameterization, compute a smoothly varying orthonormal frame.
Vector dpdv
Definition: shape.h:150
FINLINE AABB getAABB(IndexType idx) const
Return the axis-aligned bounding box of a certain primitive.
Definition: skdtree.h:213
FINLINE bool rayIntersect(const Ray &ray, Float mint, Float maxt, Float &u, Float &v, Float &t) const
Fast ray-triangle intersection test.
Definition: triaccel.h:96
Point2 uv
UV surface coordinates.
Definition: shape.h:147
const Normal * getVertexNormals() const
Return the vertex normals (const version)
Definition: trimesh.h:137
Normal normalize(const Normal &n)
Definition: normal.h:73
virtual bool rayIntersect(const Ray &ray, Float mint, Float maxt, Float &t, void *temp) const
Fast ray intersection test.
virtual void fillIntersectionRecord(const Ray &ray, const void *temp, Intersection &its) const
Given that an intersection has been found, create a detailed intersection record. ...
const Point2 * getVertexTexcoords() const
Return the vertex texture coordinates (const version)
Definition: trimesh.h:151
bool hasUVPartials
Have texture coordinate partials been computed.
Definition: shape.h:165
KDTreeBase< AABB >::IndexType IndexType
Definition: sahkdtree3.h:111
AABB m_aabb
Definition: gkdtree.h:631
Abstract base class of all shapes.
Definition: shape.h:178
const Triangle * getTriangles() const
Return the triangle list (const version)
Definition: trimesh.h:127
RGB color data type.
Definition: spectrum.h:612
TPoint2< Float > Point2
Definition: fwd.h:129
const Shape * instance
Stores a pointer to the parent instance, if applicable.
Definition: shape.h:171
const std::vector< const Shape * > & getShapes() const
Return the list of stored shapes.
Definition: skdtree.h:87
const TangentSpace * getUVTangents() const
Return the per-triangle UV tangents (const version)
Definition: trimesh.h:158
Vector toLocal(const Vector &v) const
Convert a world-space vector into local shading coordinates.
Definition: shape.h:47
Axis-aligned bounding box data structure in three dimensions.
Definition: aabb.h:437
SizeType primIndex
Definition: skdtree.h:239
Vector wi
Incident direction in the local shading frame.
Definition: shape.h:162
Implements the 3D surface area heuristic for use by the GenericKDTree construction algorithm...
Definition: sahkdtree3.h:39
SIMD quad-packed ray for coherent ray tracing.
Definition: ray_sse.h:34
LengthType length() const
Return the 2-norm of this vector.
Definition: vector.h:571
Simple tangent space storage for surfaces.
Definition: trimesh.h:38
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
Optimized KD-tree acceleration data structure for n-dimensional (n<=4) shapes and various queries on ...
Definition: gkdtree.h:706
FINLINE bool intersect(const Ray &ray, IndexType idx, Float mint, Float maxt, Float &t, void *temp) const
Definition: skdtree.h:248
const AABB & getAABB() const
Return an axis-aligned bounding box containing all primitives.
Definition: skdtree.h:98
const Color3 * getVertexColors() const
Return the vertex colors (const version)
Definition: trimesh.h:144
uint32_t primIndex
Primitive index, e.g. the triangle ID (if applicable)
Definition: shape.h:168
virtual AABB getClippedAABB(const AABB &box) const
Returns the minimal axis-aligned bounding box of this shape when clipped to another bounding box...
AABB getAABB(const Point *positions) const
Construct an axis-aligned box, which contains the triangle.
Definition: triangle.h:40
const Shape * ConstShapePtr
Definition: skdtree.h:42
Frame shFrame
Shading frame.
Definition: shape.h:144
uint32_t primIndex
Definition: triaccel.h:51
Float time
Time value associated with the intersection.
Definition: shape.h:156
const Point * getVertexPositions() const
Return the vertex positions (const version)
Definition: trimesh.h:132
Pre-computed triangle representation based on Ingo Wald's TriAccel layout.
Definition: triaccel.h:37
bool rayIntersect(const Ray &ray, Float _mint, Float _maxt, Float &t, void *temp) const
Plain intersection query (used by the 'instance' plugin)
Definition: skdtree.h:444
Spectrum color
Interpolated vertex color.
Definition: shape.h:159
Float t
Distance traveled along the ray.
Definition: shape.h:135
Container for all information related to a surface intersection.
Definition: shape.h:36
SizeType shapeIndex
Definition: skdtree.h:238
Float v
Definition: skdtree.h:240
Vector dpdv
Position partial with respect to the V parameter of the local chart.
Definition: trimesh.h:43
TVector3< T > cross(const TVector3< T > &v1, const TVector3< T > &v2)
Definition: vector.h:617
Frame geoFrame
Geometry frame.
Definition: shape.h:141
T dot(const TQuaternion< T > &q1, const TQuaternion< T > &q2)
Definition: quat.h:348
FINLINE AABB getClippedAABB(IndexType idx, const AABB &aabb) const
Return the AABB of a primitive when clipped to another AABB.
Definition: skdtree.h:225
Specializes GenericKDTree to a three-dimensional tree to be used for ray tracing. ...
Definition: sahkdtree3.h:107
SizeType getPrimitiveCount() const
Return the total number of low-level primitives (triangles and other low-level primitives) ...
Definition: skdtree.h:93
Point p
Definition: shape.h:138
Float u
Definition: skdtree.h:240
FINLINE bool intersect(const Ray &ray, IndexType idx, Float mint, Float maxt) const
Definition: skdtree.h:310
bool isZero() const
Return whether or not this vector is identically zero.
Definition: vector.h:576
void fromLinearRGB(Float r, Float g, Float b, EConversionIntent intent=EReflectance)
Convert linear RGB colors into a plausible spectral power distribution.