Skip to content

Commit

Permalink
flatten the tree on the fly if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
pca006132 committed Jun 5, 2022
1 parent 2b601ea commit ecfdcd6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
16 changes: 12 additions & 4 deletions manifold/src/csg_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,16 @@ CsgOpNode::CsgOpNode(const std::vector<std::shared_ptr<CsgNode>> &children,
Manifold::OpType op)
: children_(children) {
SetOp(op);
// opportunisticly flatten the tree without costly evaluation
GetChildren(false);
}

CsgOpNode::CsgOpNode(std::vector<std::shared_ptr<CsgNode>> &&children,
Manifold::OpType op)
: children_(children) {
SetOp(op);
// opportunisticly flatten the tree without costly evaluation
GetChildren(false);
}

std::shared_ptr<CsgNode> CsgOpNode::Transform(const glm::mat4x3 &m) const {
Expand All @@ -230,6 +234,7 @@ std::shared_ptr<CsgNode> CsgOpNode::Transform(const glm::mat4x3 &m) const {
node->op_ = op_;
node->transform_ = m * glm::mat4(transform_);
node->simplified = simplified;
node->flattened = flattened;
return node;
}

Expand Down Expand Up @@ -382,25 +387,28 @@ void CsgOpNode::BatchUnion() const {

/**
* Flatten the children to a list of leaf nodes and return them.
* If finalize is true, the list will be guaranteed to be a list of leaf nodes
* (i.e. no ops). Otherwise, the list may contain ops.
* Note that this function will not apply the transform to children, as they may
* be shared with other nodes.
*/
std::vector<std::shared_ptr<CsgNode>> &CsgOpNode::GetChildren() const {
if (children_.empty() || simplified) return children_;
std::vector<std::shared_ptr<CsgNode>> &CsgOpNode::GetChildren(bool finalize) const {
if (children_.empty() || (simplified && !finalize) || flattened) return children_;
simplified = true;
flattened = finalize;
std::vector<std::shared_ptr<CsgNode>> newChildren;

CsgNodeType op = op_;
for (auto &child : children_) {
if (child->GetNodeType() == op) {
auto grandchildren =
std::dynamic_pointer_cast<CsgOpNode>(child)->GetChildren();
std::dynamic_pointer_cast<CsgOpNode>(child)->GetChildren(finalize);
int start = children_.size();
for (auto &grandchild : grandchildren) {
newChildren.push_back(grandchild->Transform(child->GetTransform()));
}
} else {
if (child->GetNodeType() == CsgNodeType::LEAF) {
if (!finalize || child->GetNodeType() == CsgNodeType::LEAF) {
newChildren.push_back(child);
} else {
newChildren.push_back(child->ToLeafNode());
Expand Down
3 changes: 2 additions & 1 deletion manifold/src/csg_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class CsgOpNode final : public CsgNode {
mutable std::vector<std::shared_ptr<CsgNode>> children_;
mutable std::shared_ptr<CsgLeafNode> cache_ = nullptr;
mutable bool simplified = false;
mutable bool flattened = false;

void SetOp(Manifold::OpType);

Expand All @@ -81,7 +82,7 @@ class CsgOpNode final : public CsgNode {

void BatchUnion() const;

std::vector<std::shared_ptr<CsgNode>> &GetChildren() const;
std::vector<std::shared_ptr<CsgNode>> &GetChildren(bool finalize = true) const;
};

} // namespace manifold

0 comments on commit ecfdcd6

Please sign in to comment.