Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/manifold/src/csg_tree.h
9903 views
1
// Copyright 2022 The Manifold Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#pragma once
16
#include "manifold/manifold.h"
17
#include "utils.h"
18
19
namespace manifold {
20
21
enum class CsgNodeType { Union, Intersection, Difference, Leaf };
22
23
class CsgLeafNode;
24
25
class CsgNode : public std::enable_shared_from_this<CsgNode> {
26
public:
27
virtual std::shared_ptr<CsgLeafNode> ToLeafNode() const = 0;
28
virtual std::shared_ptr<CsgNode> Transform(const mat3x4& m) const = 0;
29
virtual CsgNodeType GetNodeType() const = 0;
30
31
virtual std::shared_ptr<CsgNode> Boolean(
32
const std::shared_ptr<CsgNode>& second, OpType op);
33
34
std::shared_ptr<CsgNode> Translate(const vec3& t) const;
35
std::shared_ptr<CsgNode> Scale(const vec3& s) const;
36
std::shared_ptr<CsgNode> Rotate(double xDegrees = 0, double yDegrees = 0,
37
double zDegrees = 0) const;
38
};
39
40
class CsgLeafNode final : public CsgNode {
41
public:
42
CsgLeafNode();
43
CsgLeafNode(std::shared_ptr<const Manifold::Impl> pImpl_);
44
CsgLeafNode(std::shared_ptr<const Manifold::Impl> pImpl_, mat3x4 transform_);
45
46
std::shared_ptr<const Manifold::Impl> GetImpl() const;
47
48
std::shared_ptr<CsgLeafNode> ToLeafNode() const override;
49
50
std::shared_ptr<CsgNode> Transform(const mat3x4& m) const override;
51
52
CsgNodeType GetNodeType() const override;
53
54
static std::shared_ptr<CsgLeafNode> Compose(
55
const std::vector<std::shared_ptr<CsgLeafNode>>& nodes);
56
57
private:
58
mutable std::shared_ptr<const Manifold::Impl> pImpl_;
59
mutable mat3x4 transform_ = la::identity;
60
};
61
62
class CsgOpNode final : public CsgNode {
63
public:
64
CsgOpNode();
65
66
CsgOpNode(const std::vector<std::shared_ptr<CsgNode>>& children, OpType op);
67
68
std::shared_ptr<CsgNode> Boolean(const std::shared_ptr<CsgNode>& second,
69
OpType op) override;
70
71
std::shared_ptr<CsgNode> Transform(const mat3x4& m) const override;
72
73
std::shared_ptr<CsgLeafNode> ToLeafNode() const override;
74
75
CsgNodeType GetNodeType() const override;
76
77
~CsgOpNode();
78
79
private:
80
mutable ConcurrentSharedPtr<std::vector<std::shared_ptr<CsgNode>>> impl_ =
81
ConcurrentSharedPtr<std::vector<std::shared_ptr<CsgNode>>>({});
82
OpType op_;
83
mat3x4 transform_ = la::identity;
84
// the following fields are for lazy evaluation, so they are mutable
85
mutable std::shared_ptr<CsgLeafNode> cache_ = nullptr;
86
};
87
88
} // namespace manifold
89
90