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
|
#include "CSGTextRenderer.h"
#include <string>
#include <map>
#include <list>
#include "visitor.h"
#include "state.h"
#include "module.h" // FIXME: Temporarily for ModuleInstantiation
#include "csgnode.h"
#include "transformnode.h"
#include <sstream>
#include <iostream>
#include <assert.h>
bool CSGTextRenderer::isCached(const AbstractNode &node)
{
return this->cache.contains(node);
}
/*!
Modifies target by applying op to target and src:
target = target [op] src
*/
void
CSGTextRenderer::process(string &target, const string &src, OpenSCADOperator op)
{
// if (target.dim != 2 && target.dim != 3) {
// assert(false && "Dimension of Nef polyhedron must be 2 or 3");
// }
switch (op) {
case OPENSCAD_UNION:
target += "+" + src;
break;
case OPENSCAD_INTERSECTION:
target += "*" + src;
break;
case OPENSCAD_DIFFERENCE:
target += "-" + src;
break;
case OPENSCAD_MINKOWSKI:
target += "M" + src;
break;
default:
assert(false);
}
}
void CSGTextRenderer::applyToChildren(const AbstractNode &node, OpenSCADOperator op)
{
std::stringstream stream;
stream << node.name() << 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;
for (ChildList::const_iterator iter = this->visitedchildren[node.index()].begin();
iter != this->visitedchildren[node.index()].end();
iter++) {
const AbstractNode *chnode = *iter;
assert(this->cache.contains(*chnode));
// FIXME: Don't use deep access to modinst members
if (chnode->modinst->tag_background) continue;
if (first) {
N += "(" + this->cache[*chnode];
// if (N.dim != 0) first = false; // FIXME: when can this happen?
first = false;
} else {
process(N, this->cache[*chnode], op);
}
chnode->progress_report();
}
N += ")";
}
this->cache.insert(node, 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(State &state, const AbstractNode &node)
{
if (state.isPrefix() && isCached(node)) return PruneTraversal;
if (state.isPostfix()) {
if (!isCached(node)) applyToChildren(node, OPENSCAD_UNION);
addToParent(state, node);
}
return ContinueTraversal;
}
Response CSGTextRenderer::visit(State &state, const AbstractIntersectionNode &node)
{
if (state.isPrefix() && isCached(node)) return PruneTraversal;
if (state.isPostfix()) {
if (!isCached(node)) applyToChildren(node, OPENSCAD_INTERSECTION);
addToParent(state, node);
}
return ContinueTraversal;
}
Response CSGTextRenderer::visit(State &state, const CsgNode &node)
{
if (state.isPrefix() && isCached(node)) return PruneTraversal;
if (state.isPostfix()) {
if (!isCached(node)) {
applyToChildren(node, node.type);
}
addToParent(state, node);
}
return ContinueTraversal;
}
Response CSGTextRenderer::visit(State &state, const TransformNode &node)
{
if (state.isPrefix() && isCached(node)) return PruneTraversal;
if (state.isPostfix()) {
if (!isCached(node)) {
// First union all children
applyToChildren(node, OPENSCAD_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(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)
string N = node.name();
this->cache.insert(node, 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());
this->visitedchildren.erase(node.index());
if (state.parent()) {
this->visitedchildren[state.parent()->index()].push_back(&node);
}
}
|