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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
#include "CSGTextRenderer.h"
#include <string>
#include <map>
#include <list>
#include "visitor.h"
#include "state.h"
#include "nodecache.h"
#include "module.h" // FIXME: Temporarily for ModuleInstantiation
#include "csgnode.h"
#include "transformnode.h"
#include <sstream>
#include <iostream>
#include <assert.h>
#include <QRegExp>
string CSGTextRenderer::getCGALMesh() const
{
assert(this->root);
// FIXME: assert that cache contains root
return this->cache[mk_cache_id(*this->root)];
}
// CGAL_Nef_polyhedron CSGTextRenderer::getCGALMesh() const
// {
// assert(this->root);
// // FIXME: assert that cache contains root
// return this->cache[*this->root];
// }
bool CSGTextRenderer::isCached(const AbstractNode &node)
{
return this->cache.contains(mk_cache_id(node));
}
/*!
Modifies target by applying op to target and src:
target = target [op] src
*/
void
CSGTextRenderer::process(string &target, const string &src, CSGTextRenderer::CsgOp op)
{
// if (target.dim != 2 && target.dim != 3) {
// assert(false && "Dimension of Nef polyhedron must be 2 or 3");
// }
switch (op) {
case UNION:
target += "+" + src;
break;
case INTERSECTION:
target += "*" + src;
break;
case DIFFERENCE:
target += "-" + src;
break;
case MINKOWSKI:
target += "M" + src;
break;
}
}
// /*!
// Modifies target by applying op to target and src:
// target = target [op] src
// */
// void process(CGAL_Nef_polyhedron &target, const CGAL_Nef_polyhedron &src, CsgOp op)
// {
// if (target.dim == 2) {
// switch (op) {
// case UNION:
// target.p2 += src.p2;
// break;
// case INTERSECTION:
// target.p2 *= src.p2;
// break;
// case DIFFERENCE:
// target.p2 -= src.p2;
// break;
// case MINKOWSKI:
// target.p2 = minkowski2(target.p2, src.p2);
// break;
// }
// }
// else if (target.dim == 3) {
// switch (op) {
// case UNION:
// target.p3 += src.p3;
// break;
// case INTERSECTION:
// target.p3 *= src.p3;
// break;
// case DIFFERENCE:
// target.p3 -= src.p3;
// break;
// case MINKOWSKI:
// target.p3 = minkowski3(target.p3, src.p3);
// break;
// }
// }
// else {
// assert(false && "Dimention of Nef polyhedron must be 2 or 3");
// }
// }
void CSGTextRenderer::applyToChildren(const AbstractNode &node, CSGTextRenderer::CsgOp op)
{
std::stringstream stream;
stream << typeid(node).name();
stream << "<" << node.index() << ">";
string N = stream.str();
if (this->visitedchildren[node.index()].size() > 0) {
// FIXME: assert that cache contains nodes in code below
bool first = true;
// CGAL_Nef_polyhedron N;
for (ChildList::const_iterator iter = this->visitedchildren[node.index()].begin();
iter != this->visitedchildren[node.index()].end();
iter++) {
const AbstractNode *chnode = iter->first;
const QString &chcacheid = iter->second;
// FIXME: Don't use deep access to modinst members
if (chnode->modinst->tag_background) continue;
if (first) {
N += "(" + this->cache[chcacheid];
// if (N.dim != 0) first = false; // FIXME: when can this happen?
first = false;
} else {
process(N, this->cache[chcacheid], op);
}
chnode->progress_report();
}
N += ")";
}
QString cacheid = mk_cache_id(node);
this->cache.insert(cacheid, N);
}
/*
Typical visitor behavior:
o In prefix: Check if we're cached -> prune
o In postfix: Check if we're cached -> don't apply operator to children
o In postfix: addToParent()
*/
Response CSGTextRenderer::visit(const State &state, const AbstractNode &node)
{
if (state.isPrefix() && isCached(node)) return PruneTraversal;
if (state.isPostfix()) {
if (!isCached(node)) applyToChildren(node, UNION);
addToParent(state, node);
}
return ContinueTraversal;
}
Response CSGTextRenderer::visit(const State &state, const AbstractIntersectionNode &node)
{
if (state.isPrefix() && isCached(node)) return PruneTraversal;
if (state.isPostfix()) {
if (!isCached(node)) applyToChildren(node, INTERSECTION);
addToParent(state, node);
}
return ContinueTraversal;
}
Response CSGTextRenderer::visit(const State &state, const CsgNode &node)
{
if (state.isPrefix() && isCached(node)) return PruneTraversal;
if (state.isPostfix()) {
if (!isCached(node)) {
CsgOp op;
switch (node.type) {
case CSG_TYPE_UNION:
op = UNION;
break;
case CSG_TYPE_DIFFERENCE:
op = DIFFERENCE;
break;
case CSG_TYPE_INTERSECTION:
op = INTERSECTION;
break;
}
applyToChildren(node, op);
}
addToParent(state, node);
}
return ContinueTraversal;
}
Response CSGTextRenderer::visit(const State &state, const TransformNode &node)
{
if (state.isPrefix() && isCached(node)) return PruneTraversal;
if (state.isPostfix()) {
if (!isCached(node)) {
// First union all children
applyToChildren(node, UNION);
// FIXME: Then apply transform
}
addToParent(state, node);
}
return ContinueTraversal;
}
// FIXME: RenderNode: Union over children + some magic
// FIXME: CgaladvNode: Iterate over children. Special operation
// FIXME: Subtypes of AbstractPolyNode:
// ProjectionNode
// DxfLinearExtrudeNode
// DxfRotateExtrudeNode
// (SurfaceNode)
// (PrimitiveNode)
Response CSGTextRenderer::visit(const State &state, const AbstractPolyNode &node)
{
if (state.isPrefix() && isCached(node)) return PruneTraversal;
if (state.isPostfix()) {
if (!isCached(node)) {
// FIXME: Manage caching
// FIXME: Will generate one single Nef polyhedron (no csg ops necessary)
// PolySet *ps = render_polyset(RENDER_CGAL);
// try {
// CGAL_Nef_polyhedron N = ps->renderCSGMesh();
// cgal_nef_cache.insert(cache_id, new cgal_nef_cache_entry(N), N.weight());
// print_messages_pop();
// progress_report();
// ps->unlink();
// return N;
// }
// catch (...) { // Don't leak the PolySet on ProgressCancelException
// ps->unlink();
// throw;
// }
string N = typeid(node).name();
QString cacheid = mk_cache_id(node);
this->cache.insert(cacheid, N);
// std::cout << "Insert: " << N << "\n";
// std::cout << "Node: " << cacheid.toStdString() << "\n\n";
}
addToParent(state, node);
}
return ContinueTraversal;
}
/*!
Adds ourself to out parent's list of traversed children.
Call this for _every_ node which affects output during the postfix traversal.
*/
void CSGTextRenderer::addToParent(const State &state, const AbstractNode &node)
{
assert(state.isPostfix());
QString cacheid = mk_cache_id(node);
this->visitedchildren.erase(node.index());
if (!state.parent()) {
this->root = &node;
}
else {
this->visitedchildren[state.parent()->index()].push_back(std::make_pair(&node, cacheid));
}
}
/*!
Create a cache id of the entire tree under this node. This cache id
is a non-whitespace plaintext of the evaluated scad tree and is used
for lookup in cgal_nef_cache.
*/
QString CSGTextRenderer::mk_cache_id(const AbstractNode &node) const
{
// FIXME: should we keep a cache of cache_id's to avoid recalculating this?
// -> check how often we recalculate it.
// FIXME: Get dump from dump cache
// FIXME: assert that cache contains node
QString cache_id = QString::fromStdString(this->dumpcache[node]);
// Remove all node indices and whitespace
cache_id.remove(QRegExp("[a-zA-Z_][a-zA-Z_0-9]*:"));
cache_id.remove(' ');
cache_id.remove('\t');
cache_id.remove('\n');
return cache_id;
}
|