AABB Triangle::getClippedAABB(const Point *positions, const AABB &aabb) const { /* Reserve room for some additional vertices */ Point3d vertices1[MAX_VERTS], vertices2[MAX_VERTS]; int nVertices = 3; /* The kd-tree code will frequently call this function with almost-collapsed AABBs. It's extremely important not to introduce errors in such cases, otherwise the resulting tree will incorrectly remove triangles from the associated nodes. Hence, do the following computation in double precision! */ for (int i=0; i<3; ++i) vertices1[i] = Point3d(positions[idx[i]]); for (int axis=0; axis<3; ++axis) { nVertices = sutherlandHodgman(vertices1, nVertices, vertices2, axis, aabb.min[axis], true); nVertices = sutherlandHodgman(vertices2, nVertices, vertices1, axis, aabb.max[axis], false); } AABB result; for (int i=0; i<nVertices; ++i) { #if defined(SINGLE_PRECISION) for (int j=0; j<3; ++j) { /* Now this is really paranoid! */ double pos_d = vertices1[i][j]; float pos_f = (float) pos_d; float pos_roundedDown, pos_roundedUp; if (pos_f < pos_d) { /* Float value is too small */ pos_roundedDown = pos_f; pos_roundedUp = nextafterf(pos_f, std::numeric_limits<float>::infinity()); } else if (pos_f > pos_d) { /* Float value is too large */ pos_roundedUp = pos_f; pos_roundedDown = nextafterf(pos_f, -std::numeric_limits<float>::infinity()); } else { /* Double value is exactly representable */ pos_roundedDown = pos_roundedUp = pos_f; } result.min[j] = std::min(result.min[j], pos_roundedDown); result.max[j] = std::max(result.max[j], pos_roundedUp); } #else result.expandBy(vertices1[i]); #endif } result.clip(aabb); return result; }
BoundingBox3f Mesh::getClippedBoundingBox(uint32_t index, const BoundingBox3f &bbox) const { /* Reserve room for some additional vertices */ Point3d vertices1[NORI_TRICLIP_MAXVERTS], vertices2[NORI_TRICLIP_MAXVERTS]; int nVertices = 3; /* The kd-tree code will frequently call this function with almost-collapsed bounding boxes. It's extremely important not to introduce errors in such cases, otherwise the resulting tree will incorrectly remove triangles from the associated nodes. Hence, do the following computation in double precision! */ for (int i=0; i<3; ++i) vertices1[i] = m_vertexPositions[m_indices[3*index+i]].cast<double>(); for (int axis=0; axis<3; ++axis) { nVertices = sutherlandHodgman(vertices1, nVertices, vertices2, axis, bbox.min[axis], true); nVertices = sutherlandHodgman(vertices2, nVertices, vertices1, axis, bbox.max[axis], false); } BoundingBox3f result; for (int i=0; i<nVertices; ++i) result.expandBy(vertices1[i].cast<float>()); result.clip(bbox); return result; }