-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsg.cxx
181 lines (170 loc) · 4.98 KB
/
csg.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "implicit_group.h"
template <typename T>
class difference_node : public implicit_group<T>
{
public:
/// main templated superclass which we want to inherit stuff from
typedef implicit_group<T> base;
using typename base::vec_type;
using typename base::pnt_type;
using base::gui_color;
using base::get_nr_children;
using base::get_implicit_child;
using base::evaluate_gradient;
difference_node() { gui_color = 0xffff00; }
T eval_and_get_index(const pnt_type& p, unsigned int& selected_i) const {
T value = get_implicit_child(0)->evaluate(p);
selected_i = 0;
for (unsigned int i=1; i<get_nr_children(); ++i) {
T new_value = -get_implicit_child(i)->evaluate(p);
if (new_value > value) {
selected_i = i;
value = new_value;
}
}
return value;
}
T evaluate(const pnt_type& p) const {
if (get_nr_children() == 0)
return 1;
unsigned int i;
return eval_and_get_index(p, i);
}
vec_type evaluate_gradient(const pnt_type& p) const {
if (get_nr_children() == 0)
return vec_type(0,0,0);
unsigned int i;
eval_and_get_index(p, i);
return i == 0 ? get_implicit_child(i)->evaluate_gradient(p) : -get_implicit_child(i)->evaluate_gradient(p);
}
std::string get_type_name() const {
return "difference_node";
}
};
template <typename T>
class union_node : public implicit_group<T>
{
public:
/// main templated superclass which we want to inherit stuff from
typedef implicit_group<T> base;
using typename base::vec_type;
using typename base::pnt_type;
using base::gui_color;
using base::get_nr_children;
using base::get_implicit_child;
using base::evaluate_gradient;
union_node() { gui_color = 0xffff00; }
T eval_and_get_index(const pnt_type& p, unsigned int& selected_i) const {
T value = get_implicit_child(0)->evaluate(p);
selected_i = 0;
for (unsigned int i=1; i<get_nr_children(); ++i) {
T new_value = get_implicit_child(i)->evaluate(p);
if (new_value < value) {
selected_i = i;
value = new_value;
}
}
return value;
}
T evaluate(const pnt_type& p) const {
if (get_nr_children() == 0)
return 1;
unsigned int i;
return eval_and_get_index(p, i);
}
vec_type evaluate_gradient(const pnt_type& p) const {
if (get_nr_children() == 0)
return vec_type(0,0,0);
unsigned int i;
eval_and_get_index(p, i);
return get_implicit_child(i)->evaluate_gradient(p);
}
std::string get_type_name() const {
return "union_node";
}
};
template <typename T>
class intersection_node : public implicit_group<T>
{
public:
/// main templated superclass which we want to inherit stuff from
typedef implicit_group<T> base;
using typename base::vec_type;
using typename base::pnt_type;
using base::gui_color;
using base::get_nr_children;
using base::get_implicit_child;
using base::evaluate_gradient;
intersection_node() { gui_color = 0xffff00; }
T eval_and_get_index(const pnt_type& p, unsigned int& selected_i) const {
T value = get_implicit_child(0)->evaluate(p);
selected_i = 0;
for (unsigned int i=1; i<get_nr_children(); ++i) {
T new_value = get_implicit_child(i)->evaluate(p);
if (new_value > value) {
selected_i = i;
value = new_value;
}
}
return value;
}
T evaluate(const pnt_type& p) const {
if (get_nr_children() == 0)
return 1;
unsigned int i;
return eval_and_get_index(p, i);
}
vec_type evaluate_gradient(const pnt_type& p) const {
if (get_nr_children() == 0)
return vec_type(0,0,0);
unsigned int i;
eval_and_get_index(p, i);
return get_implicit_child(i)->evaluate_gradient(p);
}
std::string get_type_name() const {
return "intersection_node";
}
};
template <typename T>
class complement_node : public implicit_group<T>
{
public:
/// main templated superclass which we want to inherit stuff from
typedef implicit_group<T> base;
using typename base::vec_type;
using typename base::pnt_type;
using base::gui_color;
using base::get_nr_children;
using base::get_implicit_child;
using base::evaluate_gradient;
complement_node() { gui_color = 0xffff00; }
T eval_and_get_index(const pnt_type& p, unsigned int& selected_i) const {
T value = get_implicit_child(0)->evaluate(p);
selected_i = 0;
for (unsigned int i=1; i<get_nr_children(); ++i) {
T new_value = get_implicit_child(i)->evaluate(p);
if (new_value > value) {
selected_i = i;
value = new_value;
}
}
return value;
}
T evaluate(const pnt_type& p) const {
if (get_nr_children() == 0)
return 1;
return -get_implicit_child(0)->evaluate(p);
}
vec_type evaluate_gradient(const pnt_type& p) const {
if (get_nr_children() == 0)
return vec_type(0,0,0);
return -get_implicit_child(0)->evaluate_gradient(p);
}
std::string get_type_name() const {
return "complement_node";
}
};
scene_factory_registration<difference_node<double> >sfr_difference("-;difference");
scene_factory_registration<union_node<double> >sfr_union("+;union");
scene_factory_registration<intersection_node<double> >sfr_intersect("*;intersection");
scene_factory_registration<complement_node<double> >sfr_complement("!;complement");