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
|
/*
* OpenSCAD (www.openscad.at)
* Copyright (C) 2009 Clifford Wolf <clifford@clifford.at>
*
* 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 "module.h"
#include "node.h"
#include "csgterm.h"
#include "builtin.h"
#include "printutils.h"
#ifdef ENABLE_CGAL
# include "cgal.h"
#endif
enum csg_type_e {
CSG_TYPE_UNION,
CSG_TYPE_DIFFERENCE,
CSG_TYPE_INTERSECTION
};
class CsgModule : public AbstractModule
{
public:
csg_type_e type;
CsgModule(csg_type_e type) : type(type) { }
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstantiation *inst) const;
};
class CsgNode : public AbstractNode
{
public:
csg_type_e type;
CsgNode(const ModuleInstantiation *mi, csg_type_e type) : AbstractNode(mi), type(type) { }
#ifdef ENABLE_CGAL
virtual CGAL_Nef_polyhedron render_cgal_nef_polyhedron() const;
#endif
CSGTerm *render_csg_term(double m[20], QVector<CSGTerm*> *highlights, QVector<CSGTerm*> *background) const;
virtual QString dump(QString indent) const;
};
AbstractNode *CsgModule::evaluate(const Context*, const ModuleInstantiation *inst) const
{
CsgNode *node = new CsgNode(inst, type);
foreach (ModuleInstantiation *v, inst->children) {
AbstractNode *n = v->evaluate(inst->ctx);
if (n != NULL)
node->children.append(n);
}
return node;
}
#ifdef ENABLE_CGAL
CGAL_Nef_polyhedron CsgNode::render_cgal_nef_polyhedron() const
{
QString cache_id = mk_cache_id();
if (cgal_nef_cache.contains(cache_id)) {
progress_report();
PRINT(cgal_nef_cache[cache_id]->msg);
return cgal_nef_cache[cache_id]->N;
}
print_messages_push();
bool first = true;
CGAL_Nef_polyhedron N;
foreach (AbstractNode *v, children) {
if (v->modinst->tag_background)
continue;
if (first) {
N = v->render_cgal_nef_polyhedron();
if (N.dim != 0)
first = false;
} else if (N.dim == 2) {
if (type == CSG_TYPE_UNION) {
N.p2 += v->render_cgal_nef_polyhedron().p2;
} else if (type == CSG_TYPE_DIFFERENCE) {
N.p2 -= v->render_cgal_nef_polyhedron().p2;
} else if (type == CSG_TYPE_INTERSECTION) {
N.p2 *= v->render_cgal_nef_polyhedron().p2;
}
} else if (N.dim == 3) {
if (type == CSG_TYPE_UNION) {
N.p3 += v->render_cgal_nef_polyhedron().p3;
} else if (type == CSG_TYPE_DIFFERENCE) {
N.p3 -= v->render_cgal_nef_polyhedron().p3;
} else if (type == CSG_TYPE_INTERSECTION) {
N.p3 *= v->render_cgal_nef_polyhedron().p3;
}
}
}
cgal_nef_cache.insert(cache_id, new cgal_nef_cache_entry(N), N.weight());
print_messages_pop();
progress_report();
return N;
}
#endif /* ENABLE_CGAL */
CSGTerm *CsgNode::render_csg_term(double m[20], QVector<CSGTerm*> *highlights, QVector<CSGTerm*> *background) const
{
CSGTerm *t1 = NULL;
foreach (AbstractNode *v, children) {
CSGTerm *t2 = v->render_csg_term(m, highlights, background);
if (t2 && !t1) {
t1 = t2;
} else if (t2 && t1) {
if (type == CSG_TYPE_UNION) {
t1 = new CSGTerm(CSGTerm::TYPE_UNION, t1, t2);
} else if (type == CSG_TYPE_DIFFERENCE) {
t1 = new CSGTerm(CSGTerm::TYPE_DIFFERENCE, t1, t2);
} else if (type == CSG_TYPE_INTERSECTION) {
t1 = new CSGTerm(CSGTerm::TYPE_INTERSECTION, t1, t2);
}
}
}
if (t1 && modinst->tag_highlight && highlights)
highlights->append(t1->link());
if (t1 && modinst->tag_background && background) {
background->append(t1);
return NULL;
}
return t1;
}
QString CsgNode::dump(QString indent) const
{
if (dump_cache.isEmpty()) {
QString text = indent + QString("n%1: ").arg(idx);
if (type == CSG_TYPE_UNION)
text += "union() {\n";
if (type == CSG_TYPE_DIFFERENCE)
text += "difference() {\n";
if (type == CSG_TYPE_INTERSECTION)
text += "intersection() {\n";
foreach (AbstractNode *v, children)
text += v->dump(indent + QString("\t"));
((AbstractNode*)this)->dump_cache = text + indent + "}\n";
}
return dump_cache;
}
void register_builtin_csgops()
{
builtin_modules["union"] = new CsgModule(CSG_TYPE_UNION);
builtin_modules["difference"] = new CsgModule(CSG_TYPE_DIFFERENCE);
builtin_modules["intersection"] = new CsgModule(CSG_TYPE_INTERSECTION);
}
|