|
@ -801,6 +801,28 @@ void makeHalfPlane(const HalfPlaneDesc& halfPlaneDesc, VisiblePrimitiveRep& visi |
|
|
buildNearBalancedBlobTree(visiblePrimitive.subBlobTree, 1); |
|
|
buildNearBalancedBlobTree(visiblePrimitive.subBlobTree, 1); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uvector3 getFaceNorm(const std::vector<uvector3>& points, const std::vector<int>& indices, int faceBias, int faceStride) |
|
|
|
|
|
{ |
|
|
|
|
|
// 注意任意三点可能共线!
|
|
|
|
|
|
|
|
|
|
|
|
assert(faceStride >= 3); |
|
|
|
|
|
|
|
|
|
|
|
// 遍历点集,寻找不共线的三个点
|
|
|
|
|
|
for (size_t i = 0; i < faceStride - 2; ++i) { |
|
|
|
|
|
for (size_t j = i + 1; j < faceStride - 1; ++j) { |
|
|
|
|
|
for (size_t k = j + 1; k < faceStride; ++k) { |
|
|
|
|
|
uvector3 v1 = points[indices[faceBias + j]] - points[indices[faceBias + i]]; |
|
|
|
|
|
uvector3 v2 = points[indices[faceBias + k]] - points[indices[faceBias + i]]; |
|
|
|
|
|
uvector3 N = cross(v1, v2); |
|
|
|
|
|
if (!isnan(N) && norm(N) > std::numeric_limits<real>::epsilon()) { |
|
|
|
|
|
return N; // 返回归一化后的法向量
|
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
throw std::runtime_error("所有点都共线,无法计算法向量"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void makeMesh(const MeshDesc& mesh, VisiblePrimitiveRep& visiblePrimitive) |
|
|
void makeMesh(const MeshDesc& mesh, VisiblePrimitiveRep& visiblePrimitive) |
|
|
{ |
|
|
{ |
|
|
assert(visiblePrimitive.tensors.size() == mesh.indexInclusiveScan.size()); |
|
|
assert(visiblePrimitive.tensors.size() == mesh.indexInclusiveScan.size()); |
|
@ -814,7 +836,7 @@ void makeMesh(const MeshDesc& mesh, VisiblePrimitiveRep& visiblePrimitive) |
|
|
auto& indices = mesh.indices; |
|
|
auto& indices = mesh.indices; |
|
|
uvector3 V01 = vertices[indices[indexBeg + 1]] - mesh.vertices[indices[indexBeg]]; |
|
|
uvector3 V01 = vertices[indices[indexBeg + 1]] - mesh.vertices[indices[indexBeg]]; |
|
|
uvector3 V02 = vertices[indices[indexBeg + 2]] - mesh.vertices[indices[indexBeg]]; |
|
|
uvector3 V02 = vertices[indices[indexBeg + 2]] - mesh.vertices[indices[indexBeg]]; |
|
|
uvector3 N = cross(V01, V02); |
|
|
uvector3 N = getFaceNorm(vertices, indices, indexBeg, indexSize); |
|
|
N /= norm(N); |
|
|
N /= norm(N); |
|
|
real d = -dot(N, vertices[indices[indexBeg]]); |
|
|
real d = -dot(N, vertices[indices[indexBeg]]); |
|
|
// 法线所指方向为>0区域
|
|
|
// 法线所指方向为>0区域
|
|
|