From e0fbd6f8a4d33dd2c880e2a63783cb40b68a1f9b Mon Sep 17 00:00:00 2001 From: pca006132 Date: Tue, 3 Dec 2024 14:45:36 +0800 Subject: [PATCH] some simplification --- src/csg_tree.cpp | 96 +++++++++++++++--------------------------------- src/csg_tree.h | 7 ---- 2 files changed, 30 insertions(+), 73 deletions(-) diff --git a/src/csg_tree.cpp b/src/csg_tree.cpp index 6e4b11f81..38aeecb11 100644 --- a/src/csg_tree.cpp +++ b/src/csg_tree.cpp @@ -19,7 +19,6 @@ #endif #include -#include #include "./boolean3.h" #include "./csg_tree.h" @@ -31,51 +30,6 @@ constexpr int kParallelThreshold = 4096; namespace { using namespace manifold; -struct Transform4x3 { - mat3x4 transform; - - vec3 operator()(vec3 position) const { - return transform * vec4(position, 1.0); - } -}; - -struct UpdateHalfedge { - const int nextVert; - const int nextEdge; - const int nextFace; - - Halfedge operator()(Halfedge edge) { - edge.startVert += nextVert; - edge.endVert += nextVert; - edge.pairedHalfedge += nextEdge; - return edge; - } -}; - -struct UpdateTriProp { - const int nextProp; - - ivec3 operator()(ivec3 tri) { - tri += nextProp; - return tri; - } -}; - -struct UpdateMeshIDs { - const int offset; - - TriRef operator()(TriRef ref) { - ref.meshID += offset; - return ref; - } -}; - -struct CheckOverlap { - VecView boxes; - const size_t i; - bool operator()(size_t j) { return boxes[i].DoesOverlap(boxes[j]); } -}; - struct MeshCompare { bool operator()(const std::shared_ptr &a, const std::shared_ptr &b) { @@ -144,8 +98,6 @@ std::shared_ptr CsgLeafNode::GetImpl() const { return pImpl_; } -mat3x4 CsgLeafNode::GetTransform() const { return transform_; } - std::shared_ptr CsgLeafNode::ToLeafNode() const { return std::make_shared(*this); } @@ -236,18 +188,30 @@ std::shared_ptr CsgLeafNode::Compose( copy(node->pImpl_->halfedgeTangent_.begin(), node->pImpl_->halfedgeTangent_.end(), combined.halfedgeTangent_.begin() + edgeIndices[i]); - transform( - node->pImpl_->halfedge_.begin(), node->pImpl_->halfedge_.end(), - combined.halfedge_.begin() + edgeIndices[i], - UpdateHalfedge({vertIndices[i], edgeIndices[i], triIndices[i]})); + const int nextVert = vertIndices[i]; + const int nextEdge = edgeIndices[i]; + const int nextFace = triIndices[i]; + transform(node->pImpl_->halfedge_.begin(), + node->pImpl_->halfedge_.end(), + combined.halfedge_.begin() + edgeIndices[i], + [nextVert, nextEdge, nextFace](Halfedge edge) { + edge.startVert += nextVert; + edge.endVert += nextVert; + edge.pairedHalfedge += nextEdge; + return edge; + }); if (numPropOut > 0) { auto start = combined.meshRelation_.triProperties.begin() + triIndices[i]; if (node->pImpl_->NumProp() > 0) { auto &triProp = node->pImpl_->meshRelation_.triProperties; + const int nextProp = propVertIndices[i]; transform(triProp.begin(), triProp.end(), start, - UpdateTriProp({propVertIndices[i]})); + [nextProp](ivec3 tri) { + tri += nextProp; + return tri; + }); const int numProp = node->pImpl_->NumProp(); auto &oldProp = node->pImpl_->meshRelation_.properties; @@ -276,8 +240,11 @@ std::shared_ptr CsgLeafNode::Compose( } else { // no need to apply the transform to the node, just copy the vertices // and face normals and apply transform on the fly + const mat3x4 transform = node->transform_; auto vertPosBegin = TransformIterator( - node->pImpl_->vertPos_.begin(), Transform4x3({node->transform_})); + node->pImpl_->vertPos_.begin(), [&transform](vec3 position) { + return transform * vec4(position, 1.0); + }); mat3 normalTransform = la::inverse(la::transpose(mat3(node->transform_))); auto faceNormalBegin = @@ -305,7 +272,10 @@ std::shared_ptr CsgLeafNode::Compose( transform(node->pImpl_->meshRelation_.triRef.begin(), node->pImpl_->meshRelation_.triRef.end(), combined.meshRelation_.triRef.begin() + triIndices[i], - UpdateMeshIDs({offset})); + [offset](TriRef ref) { + ref.meshID += offset; + return ref; + }); }); for (size_t i = 0; i < nodes.size(); i++) { @@ -420,8 +390,9 @@ void BatchUnion(std::vector> &children) { std::vector> disjointSets; for (size_t i = 0; i < boxes.size(); i++) { auto lambda = [&boxes, i](const Vec &set) { - return std::find_if(set.begin(), set.end(), CheckOverlap({boxes, i})) == - set.end(); + return std::find_if(set.begin(), set.end(), [&boxes, i](size_t j) { + return boxes[i].DoesOverlap(boxes[j]); + }) == set.end(); }; auto it = std::find_if(disjointSets.begin(), disjointSets.end(), lambda); if (it == disjointSets.end()) { @@ -461,13 +432,6 @@ CsgOpNode::CsgOpNode(const std::vector> &children, impl->children_ = children; } -CsgOpNode::CsgOpNode(std::vector> &&children, - OpType op) - : impl_(Impl{}), op_(op) { - auto impl = impl_.GetGuard(); - impl->children_ = children; -} - std::shared_ptr CsgOpNode::Boolean( const std::shared_ptr &second, OpType op) { std::vector> children; @@ -570,6 +534,8 @@ std::shared_ptr CsgOpNode::ToLeafNode() const { // the `children_` set only contains one element, and `BatchUnion`, // `BatchBoolean` or `Subtract` on this is already a no-op... if (frame->op_node->cache_) { + // destination can only be nullptr for the outermost frame, and in that + // case the cache_ cannot be non-empty. frame->destination->push_back(std::static_pointer_cast( frame->op_node->cache_->Transform(frame->transform))); stack.pop_back(); @@ -666,6 +632,4 @@ CsgNodeType CsgOpNode::GetNodeType() const { return CsgNodeType::Leaf; } -mat3x4 CsgOpNode::GetTransform() const { return transform_; } - } // namespace manifold diff --git a/src/csg_tree.h b/src/csg_tree.h index f1d2b6591..5c24dc5a2 100644 --- a/src/csg_tree.h +++ b/src/csg_tree.h @@ -27,7 +27,6 @@ class CsgNode : public std::enable_shared_from_this { virtual std::shared_ptr ToLeafNode() const = 0; virtual std::shared_ptr Transform(const mat3x4 &m) const = 0; virtual CsgNodeType GetNodeType() const = 0; - virtual mat3x4 GetTransform() const = 0; virtual std::shared_ptr Boolean( const std::shared_ptr &second, OpType op); @@ -52,8 +51,6 @@ class CsgLeafNode final : public CsgNode { CsgNodeType GetNodeType() const override; - mat3x4 GetTransform() const override; - static std::shared_ptr Compose( const std::vector> &nodes); @@ -68,8 +65,6 @@ class CsgOpNode final : public CsgNode { CsgOpNode(const std::vector> &children, OpType op); - CsgOpNode(std::vector> &&children, OpType op); - std::shared_ptr Boolean(const std::shared_ptr &second, OpType op) override; @@ -79,8 +74,6 @@ class CsgOpNode final : public CsgNode { CsgNodeType GetNodeType() const override; - mat3x4 GetTransform() const override; - private: struct Impl { std::vector> children_;