Refactored the various operation enums to use OpenSCADOperation
This commit is contained in:
parent
97073e54a7
commit
74eaeba2eb
9 changed files with 79 additions and 107 deletions
|
|
@ -47,15 +47,7 @@ void CSGTermEvaluator::applyToChildren(State &state, const AbstractNode &node, C
|
|||
if (t2 && !t1) {
|
||||
t1 = t2;
|
||||
} else if (t2 && t1) {
|
||||
if (op == CSGT_UNION) {
|
||||
t1 = CSGOperation::createCSGNode(CSGOperation::TYPE_UNION, t1, t2);
|
||||
} else if (op == CSGT_DIFFERENCE) {
|
||||
CSGNode::Flag flag = t1->flag;
|
||||
t1 = CSGOperation::createCSGNode(CSGOperation::TYPE_DIFFERENCE, t1, t2);
|
||||
t1->flag = CSGNode::Flag(t1->flag | flag);
|
||||
} else if (op == CSGT_INTERSECTION) {
|
||||
t1 = CSGOperation::createCSGNode(CSGOperation::TYPE_INTERSECTION, t1, t2);
|
||||
}
|
||||
t1 = CSGOperation::createCSGNode(op, t1, t2);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -81,7 +73,7 @@ void CSGTermEvaluator::applyToChildren(State &state, const AbstractNode &node, C
|
|||
Response CSGTermEvaluator::visit(State &state, const AbstractNode &node)
|
||||
{
|
||||
if (state.isPostfix()) {
|
||||
applyToChildren(state, node, CSGT_UNION);
|
||||
applyToChildren(state, node, OPENSCAD_UNION);
|
||||
addToParent(state, node);
|
||||
}
|
||||
return ContinueTraversal;
|
||||
|
|
@ -90,7 +82,7 @@ Response CSGTermEvaluator::visit(State &state, const AbstractNode &node)
|
|||
Response CSGTermEvaluator::visit(State &state, const AbstractIntersectionNode &node)
|
||||
{
|
||||
if (state.isPostfix()) {
|
||||
applyToChildren(state, node, CSGT_INTERSECTION);
|
||||
applyToChildren(state, node, OPENSCAD_INTERSECTION);
|
||||
addToParent(state, node);
|
||||
}
|
||||
return ContinueTraversal;
|
||||
|
|
@ -163,21 +155,7 @@ Response CSGTermEvaluator::visit(State &state, const AbstractPolyNode &node)
|
|||
Response CSGTermEvaluator::visit(State &state, const CsgNode &node)
|
||||
{
|
||||
if (state.isPostfix()) {
|
||||
CsgOp op = CSGT_UNION;
|
||||
switch (node.type) {
|
||||
case OPENSCAD_UNION:
|
||||
op = CSGT_UNION;
|
||||
break;
|
||||
case OPENSCAD_DIFFERENCE:
|
||||
op = CSGT_DIFFERENCE;
|
||||
break;
|
||||
case OPENSCAD_INTERSECTION:
|
||||
op = CSGT_INTERSECTION;
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
applyToChildren(state, node, op);
|
||||
applyToChildren(state, node, node.type);
|
||||
addToParent(state, node);
|
||||
}
|
||||
return ContinueTraversal;
|
||||
|
|
@ -189,7 +167,7 @@ Response CSGTermEvaluator::visit(State &state, const TransformNode &node)
|
|||
state.setMatrix(state.matrix() * node.matrix);
|
||||
}
|
||||
if (state.isPostfix()) {
|
||||
applyToChildren(state, node, CSGT_UNION);
|
||||
applyToChildren(state, node, OPENSCAD_UNION);
|
||||
addToParent(state, node);
|
||||
}
|
||||
return ContinueTraversal;
|
||||
|
|
@ -201,7 +179,7 @@ Response CSGTermEvaluator::visit(State &state, const ColorNode &node)
|
|||
if (!state.color().isValid()) state.setColor(node.color);
|
||||
}
|
||||
if (state.isPostfix()) {
|
||||
applyToChildren(state, node, CSGT_UNION);
|
||||
applyToChildren(state, node, OPENSCAD_UNION);
|
||||
addToParent(state, node);
|
||||
}
|
||||
return ContinueTraversal;
|
||||
|
|
|
|||
|
|
@ -38,9 +38,8 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
enum CsgOp {CSGT_UNION, CSGT_INTERSECTION, CSGT_DIFFERENCE, CSGT_MINKOWSKI};
|
||||
void addToParent(const State &state, const AbstractNode &node);
|
||||
void applyToChildren(State &state, const AbstractNode &node, CSGTermEvaluator::CsgOp op);
|
||||
void applyToChildren(State &state, const AbstractNode &node, OpenSCADOperator op);
|
||||
shared_ptr<CSGNode> evaluateCSGTermFromGeometry(State &state,
|
||||
const shared_ptr<const class Geometry> &geom,
|
||||
const class ModuleInstantiation *modinst,
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ void OpenCSGRenderer::draw(bool /*showfaces*/, bool showedges) const
|
|||
}
|
||||
|
||||
// Primitive for rendering using OpenCSG
|
||||
OpenCSGPrim *OpenCSGRenderer::createCSGPrimitive(const CSGChainObject &csgobj, OpenCSG::Operation operation, bool highlight_mode, bool background_mode, CSGOperation::type_e type) const
|
||||
OpenCSGPrim *OpenCSGRenderer::createCSGPrimitive(const CSGChainObject &csgobj, OpenCSG::Operation operation, bool highlight_mode, bool background_mode, OpenSCADOperator type) const
|
||||
{
|
||||
OpenCSGPrim *prim = new OpenCSGPrim(operation, csgobj.geom->getConvexity());
|
||||
prim->geom = csgobj.geom;
|
||||
|
|
@ -85,7 +85,7 @@ OpenCSGPrim *OpenCSGRenderer::createCSGPrimitive(const CSGChainObject &csgobj, O
|
|||
(highlight_mode ?
|
||||
CSGMODE_HIGHLIGHT :
|
||||
(background_mode ? CSGMODE_BACKGROUND : CSGMODE_NORMAL)) |
|
||||
(type == CSGOperation::TYPE_DIFFERENCE ? CSGMODE_DIFFERENCE : CSGMODE_NONE));
|
||||
(type == OPENSCAD_DIFFERENCE ? CSGMODE_DIFFERENCE : CSGMODE_NONE));
|
||||
return prim;
|
||||
}
|
||||
|
||||
|
|
@ -96,10 +96,10 @@ void OpenCSGRenderer::renderCSGProducts(const CSGProducts &products, GLint *shad
|
|||
BOOST_FOREACH(const CSGProduct &product, products.products) {
|
||||
std::vector<OpenCSG::Primitive*> primitives;
|
||||
BOOST_FOREACH(const CSGChainObject &csgobj, product.intersections) {
|
||||
if (csgobj.geom) primitives.push_back(createCSGPrimitive(csgobj, OpenCSG::Intersection, highlight_mode, background_mode, CSGOperation::TYPE_INTERSECTION));
|
||||
if (csgobj.geom) primitives.push_back(createCSGPrimitive(csgobj, OpenCSG::Intersection, highlight_mode, background_mode, OPENSCAD_INTERSECTION));
|
||||
}
|
||||
BOOST_FOREACH(const CSGChainObject &csgobj, product.subtractions) {
|
||||
if (csgobj.geom) primitives.push_back(createCSGPrimitive(csgobj, OpenCSG::Subtraction, highlight_mode, background_mode, CSGOperation::TYPE_DIFFERENCE));
|
||||
if (csgobj.geom) primitives.push_back(createCSGPrimitive(csgobj, OpenCSG::Subtraction, highlight_mode, background_mode, OPENSCAD_DIFFERENCE));
|
||||
}
|
||||
if (primitives.size() > 1) {
|
||||
OpenCSG::render(primitives);
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ public:
|
|||
virtual BoundingBox getBoundingBox() const;
|
||||
private:
|
||||
#ifdef ENABLE_OPENCSG
|
||||
class OpenCSGPrim *createCSGPrimitive(const class CSGChainObject &csgobj, OpenCSG::Operation operation, bool highlight_mode, bool background_mode, CSGOperation::type_e type) const;
|
||||
class OpenCSGPrim *createCSGPrimitive(const class CSGChainObject &csgobj, OpenCSG::Operation operation, bool highlight_mode, bool background_mode, OpenSCADOperator type) const;
|
||||
#endif
|
||||
void renderCSGProducts(const class CSGProducts &products, GLint *shaderinfo,
|
||||
bool highlight_mode, bool background_mode) const;
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ void ThrownTogetherRenderer::draw(bool /*showfaces*/, bool showedges) const
|
|||
}
|
||||
|
||||
void ThrownTogetherRenderer::renderChainObject(const CSGChainObject &csgobj, bool highlight_mode,
|
||||
bool background_mode, bool showedges, bool fberror, CSGOperation::type_e type) const
|
||||
bool background_mode, bool showedges, bool fberror, OpenSCADOperator type) const
|
||||
{
|
||||
if (this->geomVisitMark[std::make_pair(csgobj.geom.get(), &csgobj.matrix)]++ > 0) return;
|
||||
const Color4f &c = csgobj.color;
|
||||
|
|
@ -66,7 +66,7 @@ void ThrownTogetherRenderer::renderChainObject(const CSGChainObject &csgobj, boo
|
|||
(highlight_mode ?
|
||||
CSGMODE_HIGHLIGHT :
|
||||
(background_mode ? CSGMODE_BACKGROUND : CSGMODE_NORMAL)) |
|
||||
(type == CSGOperation::TYPE_DIFFERENCE ? CSGMODE_DIFFERENCE : CSGMODE_NONE));
|
||||
(type == OPENSCAD_DIFFERENCE ? CSGMODE_DIFFERENCE : CSGMODE_NONE));
|
||||
|
||||
ColorMode colormode = COLORMODE_NONE;
|
||||
ColorMode edge_colormode = COLORMODE_NONE;
|
||||
|
|
@ -83,7 +83,7 @@ void ThrownTogetherRenderer::renderChainObject(const CSGChainObject &csgobj, boo
|
|||
}
|
||||
edge_colormode = COLORMODE_BACKGROUND_EDGES;
|
||||
} else if (fberror) {
|
||||
} else if (type == CSGOperation::TYPE_DIFFERENCE) {
|
||||
} else if (type == OPENSCAD_DIFFERENCE) {
|
||||
if (csgobj.flag & CSGNode::FLAG_HIGHLIGHT) {
|
||||
colormode = COLORMODE_HIGHLIGHT;
|
||||
}
|
||||
|
|
@ -125,10 +125,10 @@ void ThrownTogetherRenderer::renderCSGProducts(CSGProducts *products, bool highl
|
|||
|
||||
BOOST_FOREACH(const CSGProduct &product, products->products) {
|
||||
BOOST_FOREACH(const CSGChainObject &csgobj, product.intersections) {
|
||||
renderChainObject(csgobj, highlight_mode, background_mode, showedges, fberror, CSGOperation::TYPE_INTERSECTION);
|
||||
renderChainObject(csgobj, highlight_mode, background_mode, showedges, fberror, OPENSCAD_INTERSECTION);
|
||||
}
|
||||
BOOST_FOREACH(const CSGChainObject &csgobj, product.subtractions) {
|
||||
renderChainObject(csgobj, highlight_mode, background_mode, showedges, fberror, CSGOperation::TYPE_DIFFERENCE);
|
||||
renderChainObject(csgobj, highlight_mode, background_mode, showedges, fberror, OPENSCAD_DIFFERENCE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ private:
|
|||
void renderCSGProducts(CSGProducts *products, bool highlight_mode, bool background_mode, bool showedges,
|
||||
bool fberror) const;
|
||||
void renderChainObject(const class CSGChainObject &csgobj, bool highlight_mode,
|
||||
bool background_mode, bool showedges, bool fberror, CSGOperation::type_e type) const;
|
||||
bool background_mode, bool showedges, bool fberror, OpenSCADOperator type) const;
|
||||
|
||||
CSGProducts *root_products;
|
||||
CSGProducts *highlight_products;
|
||||
|
|
|
|||
|
|
@ -55,15 +55,15 @@
|
|||
*/
|
||||
|
||||
|
||||
shared_ptr<CSGNode> CSGOperation::createCSGNode(type_e type, shared_ptr<CSGNode> left, shared_ptr<CSGNode> right)
|
||||
shared_ptr<CSGNode> CSGOperation::createCSGNode(OpenSCADOperator type, shared_ptr<CSGNode> left, shared_ptr<CSGNode> right)
|
||||
{
|
||||
// In case we're creating a CSG terms from a pruned tree, left/right can be NULL
|
||||
if (!right) {
|
||||
if (type == TYPE_UNION || type == TYPE_DIFFERENCE) return left;
|
||||
if (type == OPENSCAD_UNION || type == OPENSCAD_DIFFERENCE) return left;
|
||||
else return right;
|
||||
}
|
||||
if (!left) {
|
||||
if (type == TYPE_UNION) return right;
|
||||
if (type == OPENSCAD_UNION) return right;
|
||||
else return left;
|
||||
}
|
||||
|
||||
|
|
@ -72,7 +72,7 @@ shared_ptr<CSGNode> CSGOperation::createCSGNode(type_e type, shared_ptr<CSGNode>
|
|||
const BoundingBox &leftbox = left->getBoundingBox();
|
||||
const BoundingBox &rightbox = right->getBoundingBox();
|
||||
Vector3d newmin, newmax;
|
||||
if (type == TYPE_INTERSECTION) {
|
||||
if (type == OPENSCAD_INTERSECTION) {
|
||||
newmin = leftbox.min().array().cwiseMax( rightbox.min().array() );
|
||||
newmax = leftbox.max().array().cwiseMin( rightbox.max().array() );
|
||||
BoundingBox newbox( newmin, newmax );
|
||||
|
|
@ -80,7 +80,7 @@ shared_ptr<CSGNode> CSGOperation::createCSGNode(type_e type, shared_ptr<CSGNode>
|
|||
return shared_ptr<CSGNode>(); // Prune entire product
|
||||
}
|
||||
}
|
||||
else if (type == TYPE_DIFFERENCE) {
|
||||
else if (type == OPENSCAD_DIFFERENCE) {
|
||||
newmin = leftbox.min().array().cwiseMax( rightbox.min().array() );
|
||||
newmax = leftbox.max().array().cwiseMin( rightbox.max().array() );
|
||||
BoundingBox newbox( newmin, newmax );
|
||||
|
|
@ -99,7 +99,7 @@ CSGLeaf::CSGLeaf(const shared_ptr<const Geometry> &geom, const Transform3d &matr
|
|||
initBoundingBox();
|
||||
}
|
||||
|
||||
CSGOperation::CSGOperation(type_e type, shared_ptr<CSGNode> left, shared_ptr<CSGNode> right)
|
||||
CSGOperation::CSGOperation(OpenSCADOperator type, shared_ptr<CSGNode> left, shared_ptr<CSGNode> right)
|
||||
: type(type)
|
||||
{
|
||||
this->children.push_back(left);
|
||||
|
|
@ -107,7 +107,7 @@ CSGOperation::CSGOperation(type_e type, shared_ptr<CSGNode> left, shared_ptr<CSG
|
|||
initBoundingBox();
|
||||
}
|
||||
|
||||
CSGOperation::CSGOperation(type_e type, CSGNode *left, CSGNode *right)
|
||||
CSGOperation::CSGOperation(OpenSCADOperator type, CSGNode *left, CSGNode *right)
|
||||
: type(type)
|
||||
{
|
||||
this->children.push_back(shared_ptr<CSGNode>(left));
|
||||
|
|
@ -127,17 +127,17 @@ void CSGOperation::initBoundingBox()
|
|||
const BoundingBox &rightbox = this->right()->getBoundingBox();
|
||||
Vector3d newmin, newmax;
|
||||
switch (this->type) {
|
||||
case TYPE_UNION:
|
||||
case OPENSCAD_UNION:
|
||||
newmin = leftbox.min().array().cwiseMin( rightbox.min().array() );
|
||||
newmax = leftbox.max().array().cwiseMax( rightbox.max().array() );
|
||||
this->bbox = BoundingBox( newmin, newmax );
|
||||
break;
|
||||
case TYPE_INTERSECTION:
|
||||
case OPENSCAD_INTERSECTION:
|
||||
newmin = leftbox.min().array().cwiseMax( rightbox.min().array() );
|
||||
newmax = leftbox.max().array().cwiseMin( rightbox.max().array() );
|
||||
this->bbox = BoundingBox( newmin, newmax );
|
||||
break;
|
||||
case TYPE_DIFFERENCE:
|
||||
case OPENSCAD_DIFFERENCE:
|
||||
this->bbox = leftbox;
|
||||
break;
|
||||
default:
|
||||
|
|
@ -154,11 +154,11 @@ std::string CSGOperation::dump()
|
|||
{
|
||||
std::stringstream dump;
|
||||
|
||||
if (type == TYPE_UNION)
|
||||
if (type == OPENSCAD_UNION)
|
||||
dump << "(" << left()->dump() << " + " << right()->dump() << ")";
|
||||
else if (type == TYPE_INTERSECTION)
|
||||
else if (type == OPENSCAD_INTERSECTION)
|
||||
dump << "(" << left()->dump() << " * " << right()->dump() << ")";
|
||||
else if (type == TYPE_DIFFERENCE)
|
||||
else if (type == OPENSCAD_DIFFERENCE)
|
||||
dump << "(" << left()->dump() << " - " << right()->dump() << ")";
|
||||
else
|
||||
assert(false);
|
||||
|
|
@ -166,15 +166,15 @@ std::string CSGOperation::dump()
|
|||
return dump.str();
|
||||
}
|
||||
|
||||
void CSGProducts::import(shared_ptr<CSGNode> term, CSGOperation::type_e type, CSGNode::Flag flag)
|
||||
void CSGProducts::import(shared_ptr<CSGNode> term, OpenSCADOperator type, CSGNode::Flag flag)
|
||||
{
|
||||
CSGNode::Flag newflag = (CSGNode::Flag)(term->flag | flag);
|
||||
|
||||
if (shared_ptr<CSGLeaf> leaf = dynamic_pointer_cast<CSGLeaf>(term)) {
|
||||
if (type == CSGOperation::TYPE_UNION && this->currentproduct->intersections.size() > 0) {
|
||||
if (type == OPENSCAD_UNION && this->currentproduct->intersections.size() > 0) {
|
||||
this->createProduct();
|
||||
}
|
||||
else if (type == CSGOperation::TYPE_DIFFERENCE) {
|
||||
else if (type == OPENSCAD_DIFFERENCE) {
|
||||
this->currentlist = &this->currentproduct->subtractions;
|
||||
}
|
||||
this->currentlist->push_back(CSGChainObject(leaf->geom, leaf->m, leaf->color, leaf->label, newflag));
|
||||
|
|
@ -239,7 +239,7 @@ BoundingBox CSGProducts::getBoundingBox() const
|
|||
}
|
||||
|
||||
/* BOOST_FOREACH(const CSGChainObject &obj, this->objects) {
|
||||
if (obj.type != CSGOperation::TYPE_DIFFERENCE) {
|
||||
if (obj.type != OPENSCAD_DIFFERENCE) {
|
||||
if (obj.geom) {
|
||||
BoundingBox psbox = obj.geom->getBoundingBox();
|
||||
if (!psbox.isNull()) {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include <vector>
|
||||
#include "memory.h"
|
||||
#include "linalg.h"
|
||||
#include "enums.h"
|
||||
|
||||
class CSGNode
|
||||
{
|
||||
|
|
@ -33,12 +34,6 @@ protected:
|
|||
class CSGOperation : public CSGNode
|
||||
{
|
||||
public:
|
||||
enum type_e {
|
||||
TYPE_UNION,
|
||||
TYPE_INTERSECTION,
|
||||
TYPE_DIFFERENCE
|
||||
};
|
||||
|
||||
CSGOperation() {}
|
||||
virtual ~CSGOperation() {}
|
||||
virtual void initBoundingBox();
|
||||
|
|
@ -50,17 +45,17 @@ public:
|
|||
}
|
||||
virtual std::string dump();
|
||||
|
||||
static shared_ptr<CSGNode> createCSGNode(type_e type, shared_ptr<CSGNode> left, shared_ptr<CSGNode> right);
|
||||
static shared_ptr<CSGNode> createCSGNode(type_e type, CSGNode *left, CSGNode *right) {
|
||||
static shared_ptr<CSGNode> createCSGNode(OpenSCADOperator type, shared_ptr<CSGNode> left, shared_ptr<CSGNode> right);
|
||||
static shared_ptr<CSGNode> createCSGNode(OpenSCADOperator type, CSGNode *left, CSGNode *right) {
|
||||
return createCSGNode(type, shared_ptr<CSGNode>(left), shared_ptr<CSGNode>(right));
|
||||
}
|
||||
|
||||
type_e type;
|
||||
OpenSCADOperator type;
|
||||
std::vector<shared_ptr<CSGNode> > children;
|
||||
|
||||
private:
|
||||
CSGOperation(type_e type, shared_ptr<CSGNode> left, shared_ptr<CSGNode> right);
|
||||
CSGOperation(type_e type, CSGNode *left, CSGNode *right);
|
||||
CSGOperation(OpenSCADOperator type, shared_ptr<CSGNode> left, shared_ptr<CSGNode> right);
|
||||
CSGOperation(OpenSCADOperator type, CSGNode *left, CSGNode *right);
|
||||
|
||||
};
|
||||
|
||||
|
|
@ -118,7 +113,7 @@ public:
|
|||
}
|
||||
~CSGProducts() {}
|
||||
|
||||
void import(shared_ptr<CSGNode> term, CSGOperation::type_e type = CSGOperation::TYPE_UNION, CSGNode::Flag flag = CSGNode::FLAG_NONE);
|
||||
void import(shared_ptr<CSGNode> term, OpenSCADOperator type = OPENSCAD_UNION, CSGNode::Flag flag = CSGNode::FLAG_NONE);
|
||||
std::string dump(bool full = false) const;
|
||||
BoundingBox getBoundingBox() const;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
#if 0
|
||||
static bool validate_tree(const shared_ptr<CSGNode> &term)
|
||||
{
|
||||
if (term->type == CSGOperation::TYPE_PRIMITIVE) return true;
|
||||
if (term->type == OPENSCAD_PRIMITIVE) return true;
|
||||
if (!term->left() || !term->right()) return false;
|
||||
if (!validate_tree(term->left())) return false;
|
||||
if (!validate_tree(term->right())) return false;
|
||||
|
|
@ -63,7 +63,7 @@ shared_ptr<CSGNode> CSGTermNormalizer::cleanup_term(shared_ptr<CSGNode> &t)
|
|||
|
||||
static bool isUnion(shared_ptr<CSGNode> term) {
|
||||
shared_ptr<CSGOperation> op = dynamic_pointer_cast<CSGOperation>(term);
|
||||
return op && op->type == CSGOperation::TYPE_UNION;
|
||||
return op && op->type == OPENSCAD_UNION;
|
||||
}
|
||||
|
||||
static bool hasRightLeaf(shared_ptr<CSGNode> term) {
|
||||
|
|
@ -124,11 +124,11 @@ shared_ptr<CSGNode> CSGTermNormalizer::collapse_null_terms(const shared_ptr<CSGN
|
|||
shared_ptr<CSGOperation> op = dynamic_pointer_cast<CSGOperation>(term);
|
||||
if (op) {
|
||||
if (!op->right()) {
|
||||
if (op->type == CSGOperation::TYPE_UNION || op->type == CSGOperation::TYPE_DIFFERENCE) return op->left();
|
||||
if (op->type == OPENSCAD_UNION || op->type == OPENSCAD_DIFFERENCE) return op->left();
|
||||
else return op->right();
|
||||
}
|
||||
if (!op->left()) {
|
||||
if (op->type == CSGOperation::TYPE_UNION) return op->right();
|
||||
if (op->type == OPENSCAD_UNION) return op->right();
|
||||
else return op->left();
|
||||
}
|
||||
}
|
||||
|
|
@ -139,7 +139,7 @@ bool CSGTermNormalizer::match_and_replace(shared_ptr<CSGNode> &term)
|
|||
{
|
||||
shared_ptr<CSGOperation> op = dynamic_pointer_cast<CSGOperation>(term);
|
||||
if (!op) return false;
|
||||
if (op->type == CSGOperation::TYPE_UNION) return false;
|
||||
if (op->type == OPENSCAD_UNION) return false;
|
||||
|
||||
// Part A: The 'x . (y . z)' expressions
|
||||
|
||||
|
|
@ -150,44 +150,44 @@ bool CSGTermNormalizer::match_and_replace(shared_ptr<CSGNode> &term)
|
|||
shared_ptr<CSGNode> z = rightop->right();
|
||||
|
||||
// 1. x - (y + z) -> (x - y) - z
|
||||
if (op->type == CSGOperation::TYPE_DIFFERENCE && rightop->type == CSGOperation::TYPE_UNION) {
|
||||
term = CSGOperation::createCSGNode(CSGOperation::TYPE_DIFFERENCE,
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_DIFFERENCE, x, y),
|
||||
if (op->type == OPENSCAD_DIFFERENCE && rightop->type == OPENSCAD_UNION) {
|
||||
term = CSGOperation::createCSGNode(OPENSCAD_DIFFERENCE,
|
||||
CSGOperation::createCSGNode(OPENSCAD_DIFFERENCE, x, y),
|
||||
z);
|
||||
return true;
|
||||
}
|
||||
// 2. x * (y + z) -> (x * y) + (x * z)
|
||||
else if (op->type == CSGOperation::TYPE_INTERSECTION && rightop->type == CSGOperation::TYPE_UNION) {
|
||||
term = CSGOperation::createCSGNode(CSGOperation::TYPE_UNION,
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_INTERSECTION, x, y),
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_INTERSECTION, x, z));
|
||||
else if (op->type == OPENSCAD_INTERSECTION && rightop->type == OPENSCAD_UNION) {
|
||||
term = CSGOperation::createCSGNode(OPENSCAD_UNION,
|
||||
CSGOperation::createCSGNode(OPENSCAD_INTERSECTION, x, y),
|
||||
CSGOperation::createCSGNode(OPENSCAD_INTERSECTION, x, z));
|
||||
return true;
|
||||
}
|
||||
// 3. x - (y * z) -> (x - y) + (x - z)
|
||||
else if (op->type == CSGOperation::TYPE_DIFFERENCE && rightop->type == CSGOperation::TYPE_INTERSECTION) {
|
||||
term = CSGOperation::createCSGNode(CSGOperation::TYPE_UNION,
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_DIFFERENCE, x, y),
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_DIFFERENCE, x, z));
|
||||
else if (op->type == OPENSCAD_DIFFERENCE && rightop->type == OPENSCAD_INTERSECTION) {
|
||||
term = CSGOperation::createCSGNode(OPENSCAD_UNION,
|
||||
CSGOperation::createCSGNode(OPENSCAD_DIFFERENCE, x, y),
|
||||
CSGOperation::createCSGNode(OPENSCAD_DIFFERENCE, x, z));
|
||||
return true;
|
||||
}
|
||||
// 4. x * (y * z) -> (x * y) * z
|
||||
else if (op->type == CSGOperation::TYPE_INTERSECTION && rightop->type == CSGOperation::TYPE_INTERSECTION) {
|
||||
term = CSGOperation::createCSGNode(CSGOperation::TYPE_INTERSECTION,
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_INTERSECTION, x, y),
|
||||
else if (op->type == OPENSCAD_INTERSECTION && rightop->type == OPENSCAD_INTERSECTION) {
|
||||
term = CSGOperation::createCSGNode(OPENSCAD_INTERSECTION,
|
||||
CSGOperation::createCSGNode(OPENSCAD_INTERSECTION, x, y),
|
||||
z);
|
||||
return true;
|
||||
}
|
||||
// 5. x - (y - z) -> (x - y) + (x * z)
|
||||
else if (op->type == CSGOperation::TYPE_DIFFERENCE && rightop->type == CSGOperation::TYPE_DIFFERENCE) {
|
||||
term = CSGOperation::createCSGNode(CSGOperation::TYPE_UNION,
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_DIFFERENCE, x, y),
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_INTERSECTION, x, z));
|
||||
else if (op->type == OPENSCAD_DIFFERENCE && rightop->type == OPENSCAD_DIFFERENCE) {
|
||||
term = CSGOperation::createCSGNode(OPENSCAD_UNION,
|
||||
CSGOperation::createCSGNode(OPENSCAD_DIFFERENCE, x, y),
|
||||
CSGOperation::createCSGNode(OPENSCAD_INTERSECTION, x, z));
|
||||
return true;
|
||||
}
|
||||
// 6. x * (y - z) -> (x * y) - z
|
||||
else if (op->type == CSGOperation::TYPE_INTERSECTION && rightop->type == CSGOperation::TYPE_DIFFERENCE) {
|
||||
term = CSGOperation::createCSGNode(CSGOperation::TYPE_DIFFERENCE,
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_INTERSECTION, x, y),
|
||||
else if (op->type == OPENSCAD_INTERSECTION && rightop->type == OPENSCAD_DIFFERENCE) {
|
||||
term = CSGOperation::createCSGNode(OPENSCAD_DIFFERENCE,
|
||||
CSGOperation::createCSGNode(OPENSCAD_INTERSECTION, x, y),
|
||||
z);
|
||||
return true;
|
||||
}
|
||||
|
|
@ -201,24 +201,24 @@ bool CSGTermNormalizer::match_and_replace(shared_ptr<CSGNode> &term)
|
|||
shared_ptr<CSGNode> z = op->right();
|
||||
|
||||
// 7. (x - y) * z -> (x * z) - y
|
||||
if (leftop->type == CSGOperation::TYPE_DIFFERENCE && op->type == CSGOperation::TYPE_INTERSECTION) {
|
||||
term = CSGOperation::createCSGNode(CSGOperation::TYPE_DIFFERENCE,
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_INTERSECTION, x, z),
|
||||
if (leftop->type == OPENSCAD_DIFFERENCE && op->type == OPENSCAD_INTERSECTION) {
|
||||
term = CSGOperation::createCSGNode(OPENSCAD_DIFFERENCE,
|
||||
CSGOperation::createCSGNode(OPENSCAD_INTERSECTION, x, z),
|
||||
y);
|
||||
return true;
|
||||
}
|
||||
// 8. (x + y) - z -> (x - z) + (y - z)
|
||||
else if (leftop->type == CSGOperation::TYPE_UNION && op->type == CSGOperation::TYPE_DIFFERENCE) {
|
||||
term = CSGOperation::createCSGNode(CSGOperation::TYPE_UNION,
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_DIFFERENCE, x, z),
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_DIFFERENCE, y, z));
|
||||
else if (leftop->type == OPENSCAD_UNION && op->type == OPENSCAD_DIFFERENCE) {
|
||||
term = CSGOperation::createCSGNode(OPENSCAD_UNION,
|
||||
CSGOperation::createCSGNode(OPENSCAD_DIFFERENCE, x, z),
|
||||
CSGOperation::createCSGNode(OPENSCAD_DIFFERENCE, y, z));
|
||||
return true;
|
||||
}
|
||||
// 9. (x + y) * z -> (x * z) + (y * z)
|
||||
else if (leftop->type == CSGOperation::TYPE_UNION && op->type == CSGOperation::TYPE_INTERSECTION) {
|
||||
term = CSGOperation::createCSGNode(CSGOperation::TYPE_UNION,
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_INTERSECTION, x, z),
|
||||
CSGOperation::createCSGNode(CSGOperation::TYPE_INTERSECTION, y, z));
|
||||
else if (leftop->type == OPENSCAD_UNION && op->type == OPENSCAD_INTERSECTION) {
|
||||
term = CSGOperation::createCSGNode(OPENSCAD_UNION,
|
||||
CSGOperation::createCSGNode(OPENSCAD_INTERSECTION, x, z),
|
||||
CSGOperation::createCSGNode(OPENSCAD_INTERSECTION, y, z));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue