From ab2cf75c6f34b94f0202d7489e155949388b05cc Mon Sep 17 00:00:00 2001 From: yamahigashi Date: Sun, 6 Aug 2023 21:56:39 +0900 Subject: [PATCH] Updated to work embree bvh kernel vs kernel --- src/kernel/EmbreeKernel.cpp | 76 ++++++++++++++++++++----------------- src/kernel/EmbreeKernel.h | 9 +++-- 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/src/kernel/EmbreeKernel.cpp b/src/kernel/EmbreeKernel.cpp index 049d432..d1f0214 100644 --- a/src/kernel/EmbreeKernel.cpp +++ b/src/kernel/EmbreeKernel.cpp @@ -145,14 +145,15 @@ MStatus EmbreeKernel::build(const MObject& meshObject, const MBoundingBox& bbox, arguments.byteSize = sizeof(arguments); // arguments.buildFlags = RTC_BUILD_FLAG_NONE; arguments.buildFlags = RTC_BUILD_FLAG_DYNAMIC; - arguments.buildQuality = RTC_BUILD_QUALITY_MEDIUM; + arguments.buildQuality = RTC_BUILD_QUALITY_LOW; + // arguments.buildQuality = RTC_BUILD_QUALITY_MEDIUM; arguments.maxBranchingFactor = 2; arguments.maxDepth = 1024; arguments.sahBlockSize = 1; arguments.minLeafSize = 1; arguments.maxLeafSize = 8; arguments.traversalCost = 1.0f; - arguments.intersectionCost = 1.0f; + arguments.intersectionCost = 2.0f; arguments.bvh = this->bvh; arguments.primitives = primitives.data(); arguments.primitiveCount = primitives.size(); @@ -176,8 +177,7 @@ MStatus EmbreeKernel::build(const MObject& meshObject, const MBoundingBox& bbox, } - -std::vector EmbreeKernel::queryIntersected(const TriangleData& triangle) const +std::vector EmbreeKernel::queryIntersected(const TriangleData& triangleB) const { std::vector intersectingA; @@ -197,40 +197,47 @@ std::vector EmbreeKernel::queryIntersected(const TriangleData& tri MBoundingBox bboxL = currentNode->branch()->bounds[0]; MBoundingBox bboxR = currentNode->branch()->bounds[1]; - if (intersectBoxBox(bboxL, triangle.bbox)) { + if (intersectBoxBox(bboxL, triangleB.bbox)) { stack.push(currentNode->branch()->children[0]); } - if (intersectBoxBox(bboxR, triangle.bbox)) { + if (intersectBoxBox(bboxR, triangleB.bbox)) { stack.push(currentNode->branch()->children[1]); } } if (isALeaf) { - // FIXME: - // Ideally, we should use box-to-box checks for collisions. However, - // due to the occurrence of extremely small boxes, collisions - // might be missed. Therefore, we're opting to skip the check. - // if (!intersectBoxBox(currentNode->leaf()->bounds, triangle.bbox)) { + // While it's possible to determine intersections between bounding boxes and + // decide if they can be skipped, the subsequent code for triangle-to-triangle + // intersection checks is quite similar. Therefore, it might be more efficient + // to leave this task to the triangle-to-triangle intersection checks + // if (!intersectBoxBox(currentNode->leaf()->bounds, triangleB.bbox)) { + // continue; + // } int index = currentNode->leaf()->id; - TriangleData triangleData = this->triangles[index]; - if (intersectTriangleTriangle(triangle, triangleData)) { - intersectingA.push_back(triangleData); + TriangleData triangleA = this->triangles[index]; + if (intersectTriangleTriangle(triangleB, triangleA)) { + intersectingA.push_back(triangleA); } - if (index > 0) { - triangleData = this->triangles[index-1]; - if (intersectTriangleTriangle(triangle, triangleData)) { - intersectingA.push_back(triangleData); - } - } - if (index < this->triangles.size()-1) { - triangleData = this->triangles[index+1]; - if (intersectTriangleTriangle(triangle, triangleData)) { - intersectingA.push_back(triangleData); - } - } + // Depending on the quality of the BVH, overlapping regions might cause + // adjacent faces to be skipped. It's essential to address this issue. + // If this happens, we can check the adjacent triangles as well. + // + // if (index > 0) { + // triangleA = this->triangles[index-1]; + // if (intersectTriangleTriangle(triangleB, triangleA)) { + // intersectingA.push_back(triangleA); + // } + // } + // + // if (index < this->triangles.size()-1) { + // triangleA = this->triangles[index+1]; + // if (intersectTriangleTriangle(triangleB, triangleA)) { + // intersectingA.push_back(triangleA); + // } + // } } }; @@ -311,7 +318,6 @@ K2KIntersection EmbreeKernel::intersectKernelKernel( EmbreeKernel* other = dynamic_cast(&otherKernel); if (!other) { - // MGlobal::displayError("Failed to cast SpatialDivisionKernel to EmbreeKernel"); return std::make_pair(intersectedTrianglesA, intersectedTrianglesB); } @@ -323,13 +329,15 @@ K2KIntersection EmbreeKernel::intersectKernelKernel( Node* nodeB = pair.second; if (nodeA->isLeaf() && nodeB->isLeaf()) { - for (TriangleData triA : nodeA->triangles) { - for (TriangleData triB : nodeB->triangles) { - if (intersectTriangleTriangle(triA, triB)) { - intersectedTrianglesA.push_back(triA); - intersectedTrianglesB.push_back(triB); - } - } + int nodeAId = nodeA->leaf()->id; + int nodeBId = nodeB->leaf()->id; + + TriangleData triA = this->triangles[nodeAId]; + TriangleData triB = other->triangles[nodeBId]; + + if (intersectTriangleTriangle(triA, triB)) { + intersectedTrianglesA.push_back(triA); + intersectedTrianglesB.push_back(triB); } } } diff --git a/src/kernel/EmbreeKernel.h b/src/kernel/EmbreeKernel.h index f314510..96be155 100644 --- a/src/kernel/EmbreeKernel.h +++ b/src/kernel/EmbreeKernel.h @@ -18,7 +18,6 @@ struct InnerNode; struct LeafNode; -using TriangleList = std::vector; struct Node { virtual InnerNode* branch() { return nullptr; } @@ -26,7 +25,6 @@ struct Node virtual bool isLeaf() { return false; } InnerNode* parent() { return nullptr; } - TriangleList triangles; }; @@ -94,8 +92,13 @@ struct LeafNode : public Node assert(numPrims == 1); void* ptr = rtcThreadLocalAlloc(alloc,sizeof(LeafNode),16); // return (void*) new (ptr) LeafNode(prims->primID,*(MBoundingBox*)prims); + MBoundingBox box( + MPoint(prims->lower_x, prims->lower_y, prims->lower_z), + MPoint(prims->upper_x, prims->upper_y, prims->upper_z) + ); - Node *node = new (ptr) LeafNode(prims->primID, *(MBoundingBox*)prims); + Node *node = new (ptr) LeafNode(prims->primID, box); + // Node *node = new (ptr) LeafNode(prims->primID, *(MBoundingBox*)prims); return (void *)node; }