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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/*
* OpenSCAD (www.openscad.org)
* Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
* Marius Kintel <marius@kintel.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* As a special exception, you have permission to link this program
* with the CGAL library and distribute executables, as long as you
* follow the requirements of the GNU GPL in regard to all of the
* software in the executable aside from CGAL.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "csgterm.h"
#include "polyset.h"
CSGTerm::CSGTerm(PolySet *polyset, double m[20], QString label)
{
this->type = TYPE_PRIMITIVE;
this->polyset = polyset;
this->label = label;
this->left = NULL;
this->right = NULL;
for (int i = 0; i < 20; i++)
this->m[i] = m[i];
refcounter = 1;
}
CSGTerm::CSGTerm(type_e type, CSGTerm *left, CSGTerm *right)
{
this->type = type;
this->polyset = NULL;
this->left = left;
this->right = right;
refcounter = 1;
}
CSGTerm *CSGTerm::normalize()
{
// This function implements the CSG normalization
// Reference: Florian Kirsch, Juergen Doeller,
// OpenCSG: A Library for Image-Based CSG Rendering,
// University of Potsdam, Hasso-Plattner-Institute, Germany
// http://www.opencsg.org/data/csg_freenix2005_paper.pdf
if (type == TYPE_PRIMITIVE)
return link();
CSGTerm *t1, *t2, *x, *y;
x = left->normalize();
y = right->normalize();
if (x != left || y != right) {
t1 = new CSGTerm(type, x, y);
} else {
t1 = link();
x->unlink();
y->unlink();
}
while (1) {
t2 = t1->normalize_tail();
t1->unlink();
if (t1 == t2)
break;
t1 = t2;
}
return t1;
}
CSGTerm *CSGTerm::normalize_tail()
{
CSGTerm *x, *y, *z;
// Part A: The 'x . (y . z)' expressions
x = left;
y = right->left;
z = right->right;
// 1. x - (y + z) -> (x - y) - z
if (type == TYPE_DIFFERENCE && right->type == TYPE_UNION)
return new CSGTerm(TYPE_DIFFERENCE, new CSGTerm(TYPE_DIFFERENCE, x->link(), y->link()), z->link());
// 2. x * (y + z) -> (x * y) + (x * z)
if (type == TYPE_INTERSECTION && right->type == TYPE_UNION)
return new CSGTerm(TYPE_UNION, new CSGTerm(TYPE_INTERSECTION, x->link(), y->link()), new CSGTerm(TYPE_INTERSECTION, x->link(), z->link()));
// 3. x - (y * z) -> (x - y) + (x - z)
if (type == TYPE_DIFFERENCE && right->type == TYPE_INTERSECTION)
return new CSGTerm(TYPE_UNION, new CSGTerm(TYPE_DIFFERENCE, x->link(), y->link()), new CSGTerm(TYPE_DIFFERENCE, x->link(), z->link()));
// 4. x * (y * z) -> (x * y) * z
if (type == TYPE_INTERSECTION && right->type == TYPE_INTERSECTION)
return new CSGTerm(TYPE_INTERSECTION, new CSGTerm(TYPE_INTERSECTION, x->link(), y->link()), z->link());
// 5. x - (y - z) -> (x - y) + (x * z)
if (type == TYPE_DIFFERENCE && right->type == TYPE_DIFFERENCE)
return new CSGTerm(TYPE_UNION, new CSGTerm(TYPE_DIFFERENCE, x->link(), y->link()), new CSGTerm(TYPE_INTERSECTION, x->link(), z->link()));
// 6. x * (y - z) -> (x * y) - z
if (type == TYPE_INTERSECTION && right->type == TYPE_DIFFERENCE)
return new CSGTerm(TYPE_DIFFERENCE, new CSGTerm(TYPE_INTERSECTION, x->link(), y->link()), z->link());
// Part B: The '(x . y) . z' expressions
x = left->left;
y = left->right;
z = right;
// 7. (x - y) * z -> (x * z) - y
if (left->type == TYPE_DIFFERENCE && type == TYPE_INTERSECTION)
return new CSGTerm(TYPE_DIFFERENCE, new CSGTerm(TYPE_INTERSECTION, x->link(), z->link()), y->link());
// 8. (x + y) - z -> (x - z) + (y - z)
if (left->type == TYPE_UNION && type == TYPE_DIFFERENCE)
return new CSGTerm(TYPE_UNION, new CSGTerm(TYPE_DIFFERENCE, x->link(), z->link()), new CSGTerm(TYPE_DIFFERENCE, y->link(), z->link()));
// 9. (x + y) * z -> (x * z) + (y * z)
if (left->type == TYPE_UNION && type == TYPE_INTERSECTION)
return new CSGTerm(TYPE_UNION, new CSGTerm(TYPE_INTERSECTION, x->link(), z->link()), new CSGTerm(TYPE_INTERSECTION, y->link(), z->link()));
return link();
}
CSGTerm *CSGTerm::link()
{
refcounter++;
return this;
}
void CSGTerm::unlink()
{
if (--refcounter <= 0) {
if (polyset)
polyset->unlink();
if (left)
left->unlink();
if (right)
right->unlink();
delete this;
}
}
QString CSGTerm::dump()
{
if (type == TYPE_UNION)
return QString("(%1 + %2)").arg(left->dump(), right->dump());
if (type == TYPE_INTERSECTION)
return QString("(%1 * %2)").arg(left->dump(), right->dump());
if (type == TYPE_DIFFERENCE)
return QString("(%1 - %2)").arg(left->dump(), right->dump());
return label;
}
CSGChain::CSGChain()
{
}
void CSGChain::add(PolySet *polyset, double *m, CSGTerm::type_e type, QString label)
{
polysets.append(polyset);
matrices.append(m);
types.append(type);
labels.append(label);
}
void CSGChain::import(CSGTerm *term, CSGTerm::type_e type)
{
if (term->type == CSGTerm::TYPE_PRIMITIVE) {
add(term->polyset, term->m, type, term->label);
} else {
import(term->left, type);
import(term->right, term->type);
}
}
QString CSGChain::dump()
{
QString text;
for (int i = 0; i < types.size(); i++)
{
if (types[i] == CSGTerm::TYPE_UNION) {
if (i != 0)
text += "\n";
text += "+";
}
if (types[i] == CSGTerm::TYPE_DIFFERENCE)
text += " -";
if (types[i] == CSGTerm::TYPE_INTERSECTION)
text += " *";
text += labels[i];
}
text += "\n";
return text;
}
|