2009-06-24 14:59:15 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define INCLUDE_ABSTRACT_NODE_DETAILS
|
|
|
|
|
|
|
|
#include "openscad.h"
|
|
|
|
|
2010-01-07 17:59:10 +03:00
|
|
|
CSGTerm::CSGTerm(PolySet *polyset, double m[20], QString label)
|
2009-06-24 14:59:15 +04:00
|
|
|
{
|
2009-08-04 22:55:25 +04:00
|
|
|
this->type = TYPE_PRIMITIVE;
|
2009-06-24 14:59:15 +04:00
|
|
|
this->polyset = polyset;
|
2009-06-24 19:39:08 +04:00
|
|
|
this->label = label;
|
2009-06-24 14:59:15 +04:00
|
|
|
this->left = NULL;
|
|
|
|
this->right = NULL;
|
2010-01-07 17:59:10 +03:00
|
|
|
for (int i = 0; i < 20; i++)
|
2009-07-28 16:53:52 +04:00
|
|
|
this->m[i] = m[i];
|
2009-06-24 14:59:15 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-06-24 19:39:08 +04:00
|
|
|
CSGTerm *CSGTerm::normalize()
|
2009-06-24 14:59:15 +04:00
|
|
|
{
|
|
|
|
// 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
|
|
|
|
|
2009-08-04 22:55:25 +04:00
|
|
|
if (type == TYPE_PRIMITIVE)
|
2009-06-24 14:59:15 +04:00
|
|
|
return link();
|
|
|
|
|
2009-06-24 19:39:08 +04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2009-06-24 21:40:19 +04:00
|
|
|
while (1) {
|
2009-06-24 19:39:08 +04:00
|
|
|
t2 = t1->normalize_tail();
|
|
|
|
t1->unlink();
|
2009-06-24 21:40:19 +04:00
|
|
|
if (t1 == t2)
|
|
|
|
break;
|
2009-06-24 19:39:08 +04:00
|
|
|
t1 = t2;
|
2009-06-24 21:40:19 +04:00
|
|
|
}
|
2009-06-24 14:59:15 +04:00
|
|
|
|
2009-06-24 19:39:08 +04:00
|
|
|
return t1;
|
|
|
|
}
|
2009-06-24 14:59:15 +04:00
|
|
|
|
2009-06-24 19:39:08 +04:00
|
|
|
CSGTerm *CSGTerm::normalize_tail()
|
|
|
|
{
|
|
|
|
CSGTerm *x, *y, *z;
|
2009-06-24 14:59:15 +04:00
|
|
|
|
|
|
|
// Part A: The 'x . (y . z)' expressions
|
|
|
|
|
|
|
|
x = left;
|
|
|
|
y = right->left;
|
|
|
|
z = right->right;
|
|
|
|
|
|
|
|
// 1. x - (y + z) -> (x - y) - z
|
2009-08-04 22:55:25 +04:00
|
|
|
if (type == TYPE_DIFFERENCE && right->type == TYPE_UNION)
|
|
|
|
return new CSGTerm(TYPE_DIFFERENCE, new CSGTerm(TYPE_DIFFERENCE, x->link(), y->link()), z->link());
|
2009-06-24 14:59:15 +04:00
|
|
|
|
|
|
|
// 2. x * (y + z) -> (x * y) + (x * z)
|
2009-08-04 22:55:25 +04:00
|
|
|
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()));
|
2009-06-24 14:59:15 +04:00
|
|
|
|
|
|
|
// 3. x - (y * z) -> (x - y) + (x - z)
|
2009-08-04 22:55:25 +04:00
|
|
|
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()));
|
2009-06-24 14:59:15 +04:00
|
|
|
|
|
|
|
// 4. x * (y * z) -> (x * y) * z
|
2009-08-04 22:55:25 +04:00
|
|
|
if (type == TYPE_INTERSECTION && right->type == TYPE_INTERSECTION)
|
|
|
|
return new CSGTerm(TYPE_INTERSECTION, new CSGTerm(TYPE_INTERSECTION, x->link(), y->link()), z->link());
|
2009-06-24 14:59:15 +04:00
|
|
|
|
|
|
|
// 5. x - (y - z) -> (x - y) + (x * z)
|
2009-08-04 22:55:25 +04:00
|
|
|
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()));
|
2009-06-24 14:59:15 +04:00
|
|
|
|
|
|
|
// 6. x * (y - z) -> (x * y) - z
|
2009-08-04 22:55:25 +04:00
|
|
|
if (type == TYPE_INTERSECTION && right->type == TYPE_DIFFERENCE)
|
|
|
|
return new CSGTerm(TYPE_DIFFERENCE, new CSGTerm(TYPE_INTERSECTION, x->link(), y->link()), z->link());
|
2009-06-24 14:59:15 +04:00
|
|
|
|
|
|
|
// Part B: The '(x . y) . z' expressions
|
|
|
|
|
|
|
|
x = left->left;
|
|
|
|
y = left->right;
|
|
|
|
z = right;
|
|
|
|
|
|
|
|
// 7. (x - y) * z -> (x * z) - y
|
2009-08-04 22:55:25 +04:00
|
|
|
if (left->type == TYPE_DIFFERENCE && type == TYPE_INTERSECTION)
|
|
|
|
return new CSGTerm(TYPE_DIFFERENCE, new CSGTerm(TYPE_INTERSECTION, x->link(), z->link()), y->link());
|
2009-06-24 14:59:15 +04:00
|
|
|
|
|
|
|
// 8. (x + y) - z -> (x - z) + (y - z)
|
2009-08-04 22:55:25 +04:00
|
|
|
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()));
|
2009-06-24 14:59:15 +04:00
|
|
|
|
|
|
|
// 9. (x + y) * z -> (x * z) + (y * z)
|
2009-08-04 22:55:25 +04:00
|
|
|
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()));
|
2009-06-24 14:59:15 +04:00
|
|
|
|
2009-06-24 19:39:08 +04:00
|
|
|
return link();
|
2009-06-24 14:59:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CSGTerm *CSGTerm::link()
|
|
|
|
{
|
|
|
|
refcounter++;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSGTerm::unlink()
|
|
|
|
{
|
|
|
|
if (--refcounter <= 0) {
|
|
|
|
if (polyset)
|
2009-07-28 16:53:52 +04:00
|
|
|
polyset->unlink();
|
2009-06-24 14:59:15 +04:00
|
|
|
if (left)
|
|
|
|
left->unlink();
|
|
|
|
if (right)
|
|
|
|
right->unlink();
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-24 19:39:08 +04:00
|
|
|
QString CSGTerm::dump()
|
|
|
|
{
|
2009-08-04 22:55:25 +04:00
|
|
|
if (type == TYPE_UNION)
|
2009-06-24 19:39:08 +04:00
|
|
|
return QString("(%1 + %2)").arg(left->dump(), right->dump());
|
2009-08-04 22:55:25 +04:00
|
|
|
if (type == TYPE_INTERSECTION)
|
2009-06-24 19:39:08 +04:00
|
|
|
return QString("(%1 * %2)").arg(left->dump(), right->dump());
|
2009-08-04 22:55:25 +04:00
|
|
|
if (type == TYPE_DIFFERENCE)
|
2009-06-24 19:39:08 +04:00
|
|
|
return QString("(%1 - %2)").arg(left->dump(), right->dump());
|
|
|
|
return label;
|
|
|
|
}
|
2009-06-25 12:12:41 +04:00
|
|
|
|
|
|
|
CSGChain::CSGChain()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-07-28 16:53:52 +04:00
|
|
|
void CSGChain::add(PolySet *polyset, double *m, CSGTerm::type_e type, QString label)
|
2009-06-25 12:12:41 +04:00
|
|
|
{
|
|
|
|
polysets.append(polyset);
|
2009-07-28 16:53:52 +04:00
|
|
|
matrices.append(m);
|
2009-06-25 12:12:41 +04:00
|
|
|
types.append(type);
|
|
|
|
labels.append(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSGChain::import(CSGTerm *term, CSGTerm::type_e type)
|
|
|
|
{
|
2009-08-04 22:55:25 +04:00
|
|
|
if (term->type == CSGTerm::TYPE_PRIMITIVE) {
|
2009-07-28 16:53:52 +04:00
|
|
|
add(term->polyset, term->m, type, term->label);
|
2009-06-25 12:12:41 +04:00
|
|
|
} else {
|
|
|
|
import(term->left, type);
|
|
|
|
import(term->right, term->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CSGChain::dump()
|
|
|
|
{
|
|
|
|
QString text;
|
|
|
|
for (int i = 0; i < types.size(); i++)
|
|
|
|
{
|
2009-08-04 22:55:25 +04:00
|
|
|
if (types[i] == CSGTerm::TYPE_UNION) {
|
2009-06-25 12:12:41 +04:00
|
|
|
if (i != 0)
|
|
|
|
text += "\n";
|
|
|
|
text += "+";
|
|
|
|
}
|
2009-08-04 22:55:25 +04:00
|
|
|
if (types[i] == CSGTerm::TYPE_DIFFERENCE)
|
2009-06-25 12:12:41 +04:00
|
|
|
text += " -";
|
2009-08-04 22:55:25 +04:00
|
|
|
if (types[i] == CSGTerm::TYPE_INTERSECTION)
|
2009-06-25 12:12:41 +04:00
|
|
|
text += " *";
|
|
|
|
text += labels[i];
|
|
|
|
}
|
|
|
|
text += "\n";
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|