Skip to content

Commit

Permalink
some simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
pca006132 committed Dec 3, 2024
1 parent 89d65b2 commit e0fbd6f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 73 deletions.
96 changes: 30 additions & 66 deletions src/csg_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#endif

#include <algorithm>
#include <variant>

#include "./boolean3.h"
#include "./csg_tree.h"
Expand All @@ -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<const Box> boxes;
const size_t i;
bool operator()(size_t j) { return boxes[i].DoesOverlap(boxes[j]); }
};

struct MeshCompare {
bool operator()(const std::shared_ptr<CsgLeafNode> &a,
const std::shared_ptr<CsgLeafNode> &b) {
Expand Down Expand Up @@ -144,8 +98,6 @@ std::shared_ptr<const Manifold::Impl> CsgLeafNode::GetImpl() const {
return pImpl_;
}

mat3x4 CsgLeafNode::GetTransform() const { return transform_; }

std::shared_ptr<CsgLeafNode> CsgLeafNode::ToLeafNode() const {
return std::make_shared<CsgLeafNode>(*this);
}
Expand Down Expand Up @@ -236,18 +188,30 @@ std::shared_ptr<CsgLeafNode> 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) {

Check warning on line 197 in src/csg_tree.cpp

View check run for this annotation

Codecov / codecov/patch

src/csg_tree.cpp#L197

Added line #L197 was not covered by tests
edge.startVert += nextVert;
edge.endVert += nextVert;
edge.pairedHalfedge += nextEdge;
return edge;

Check warning on line 201 in src/csg_tree.cpp

View check run for this annotation

Codecov / codecov/patch

src/csg_tree.cpp#L201

Added line #L201 was not covered by tests
});

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) {

Check warning on line 211 in src/csg_tree.cpp

View check run for this annotation

Codecov / codecov/patch

src/csg_tree.cpp#L211

Added line #L211 was not covered by tests
tri += nextProp;
return tri;

Check warning on line 213 in src/csg_tree.cpp

View check run for this annotation

Codecov / codecov/patch

src/csg_tree.cpp#L213

Added line #L213 was not covered by tests
});

const int numProp = node->pImpl_->NumProp();
auto &oldProp = node->pImpl_->meshRelation_.properties;
Expand Down Expand Up @@ -276,8 +240,11 @@ std::shared_ptr<CsgLeafNode> 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) {

Check warning on line 245 in src/csg_tree.cpp

View check run for this annotation

Codecov / codecov/patch

src/csg_tree.cpp#L245

Added line #L245 was not covered by tests
return transform * vec4(position, 1.0);
});
mat3 normalTransform =
la::inverse(la::transpose(mat3(node->transform_)));
auto faceNormalBegin =
Expand Down Expand Up @@ -305,7 +272,10 @@ std::shared_ptr<CsgLeafNode> CsgLeafNode::Compose(
transform(node->pImpl_->meshRelation_.triRef.begin(),
node->pImpl_->meshRelation_.triRef.end(),
combined.meshRelation_.triRef.begin() + triIndices[i],
UpdateMeshIDs({offset}));
[offset](TriRef ref) {

Check warning on line 275 in src/csg_tree.cpp

View check run for this annotation

Codecov / codecov/patch

src/csg_tree.cpp#L275

Added line #L275 was not covered by tests
ref.meshID += offset;
return ref;

Check warning on line 277 in src/csg_tree.cpp

View check run for this annotation

Codecov / codecov/patch

src/csg_tree.cpp#L277

Added line #L277 was not covered by tests
});
});

for (size_t i = 0; i < nodes.size(); i++) {
Expand Down Expand Up @@ -420,8 +390,9 @@ void BatchUnion(std::vector<std::shared_ptr<CsgLeafNode>> &children) {
std::vector<Vec<size_t>> disjointSets;
for (size_t i = 0; i < boxes.size(); i++) {
auto lambda = [&boxes, i](const Vec<size_t> &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()) {
Expand Down Expand Up @@ -461,13 +432,6 @@ CsgOpNode::CsgOpNode(const std::vector<std::shared_ptr<CsgNode>> &children,
impl->children_ = children;
}

CsgOpNode::CsgOpNode(std::vector<std::shared_ptr<CsgNode>> &&children,
OpType op)
: impl_(Impl{}), op_(op) {
auto impl = impl_.GetGuard();
impl->children_ = children;
}

std::shared_ptr<CsgNode> CsgOpNode::Boolean(
const std::shared_ptr<CsgNode> &second, OpType op) {
std::vector<std::shared_ptr<CsgNode>> children;
Expand Down Expand Up @@ -570,6 +534,8 @@ std::shared_ptr<CsgLeafNode> 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<CsgLeafNode>(
frame->op_node->cache_->Transform(frame->transform)));

Check warning on line 540 in src/csg_tree.cpp

View check run for this annotation

Codecov / codecov/patch

src/csg_tree.cpp#L539-L540

Added lines #L539 - L540 were not covered by tests
stack.pop_back();
Expand Down Expand Up @@ -666,6 +632,4 @@ CsgNodeType CsgOpNode::GetNodeType() const {
return CsgNodeType::Leaf;
}

mat3x4 CsgOpNode::GetTransform() const { return transform_; }

} // namespace manifold
7 changes: 0 additions & 7 deletions src/csg_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class CsgNode : public std::enable_shared_from_this<CsgNode> {
virtual std::shared_ptr<CsgLeafNode> ToLeafNode() const = 0;
virtual std::shared_ptr<CsgNode> Transform(const mat3x4 &m) const = 0;
virtual CsgNodeType GetNodeType() const = 0;
virtual mat3x4 GetTransform() const = 0;

virtual std::shared_ptr<CsgNode> Boolean(
const std::shared_ptr<CsgNode> &second, OpType op);
Expand All @@ -52,8 +51,6 @@ class CsgLeafNode final : public CsgNode {

CsgNodeType GetNodeType() const override;

mat3x4 GetTransform() const override;

static std::shared_ptr<CsgLeafNode> Compose(
const std::vector<std::shared_ptr<CsgLeafNode>> &nodes);

Expand All @@ -68,8 +65,6 @@ class CsgOpNode final : public CsgNode {

CsgOpNode(const std::vector<std::shared_ptr<CsgNode>> &children, OpType op);

CsgOpNode(std::vector<std::shared_ptr<CsgNode>> &&children, OpType op);

std::shared_ptr<CsgNode> Boolean(const std::shared_ptr<CsgNode> &second,
OpType op) override;

Expand All @@ -79,8 +74,6 @@ class CsgOpNode final : public CsgNode {

CsgNodeType GetNodeType() const override;

mat3x4 GetTransform() const override;

private:
struct Impl {
std::vector<std::shared_ptr<CsgNode>> children_;
Expand Down

0 comments on commit e0fbd6f

Please sign in to comment.