2009-06-21 15:41:11 +04:00
|
|
|
/*
|
2011-01-21 04:21:09 +03:00
|
|
|
* OpenSCAD (www.openscad.org)
|
|
|
|
* Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
|
|
|
|
* Marius Kintel <marius@kintel.net>
|
2009-06-21 15:41:11 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2010-02-01 12:34:18 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2009-06-21 15:41:11 +04:00
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-03-28 19:57:39 +04:00
|
|
|
#include "transformnode.h"
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "module.h"
|
|
|
|
#include "context.h"
|
|
|
|
#include "dxfdata.h"
|
|
|
|
#include "csgterm.h"
|
|
|
|
#include "polyset.h"
|
|
|
|
#include "dxftess.h"
|
|
|
|
#include "builtin.h"
|
2010-01-06 13:48:51 +03:00
|
|
|
#include "printutils.h"
|
2010-03-02 21:22:31 +03:00
|
|
|
#include "visitor.h"
|
|
|
|
#include <sstream>
|
2011-07-11 05:54:20 +04:00
|
|
|
#include <vector>
|
2010-08-28 23:21:44 +04:00
|
|
|
#include <assert.h>
|
2009-06-21 15:41:11 +04:00
|
|
|
|
2009-06-27 03:36:23 +04:00
|
|
|
enum transform_type_e {
|
|
|
|
SCALE,
|
|
|
|
ROTATE,
|
2010-01-09 20:15:56 +03:00
|
|
|
MIRROR,
|
2009-06-27 03:36:23 +04:00
|
|
|
TRANSLATE,
|
2011-09-03 01:35:10 +04:00
|
|
|
MULTMATRIX
|
2009-06-27 03:36:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class TransformModule : public AbstractModule
|
2009-06-21 15:41:11 +04:00
|
|
|
{
|
|
|
|
public:
|
2009-06-27 03:36:23 +04:00
|
|
|
transform_type_e type;
|
|
|
|
TransformModule(transform_type_e type) : type(type) { }
|
2010-01-06 22:38:45 +03:00
|
|
|
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstantiation *inst) const;
|
2009-06-21 15:41:11 +04:00
|
|
|
};
|
|
|
|
|
2011-07-11 05:54:20 +04:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
static vector<string> split(const string &str, const string &delim)
|
2009-06-21 15:41:11 +04:00
|
|
|
{
|
2011-07-11 05:54:20 +04:00
|
|
|
assert(delim.size() > 0);
|
|
|
|
|
|
|
|
vector<string> strvec;
|
|
|
|
size_t start = 0, end = 0;
|
|
|
|
while (end != string::npos) {
|
|
|
|
end = str.find(delim, start);
|
|
|
|
// If at end, use length=maxLength. Else use length=end-start.
|
|
|
|
strvec.push_back(str.substr(start, (end == string::npos) ? string::npos : end - start));
|
|
|
|
// If at end, use start=maxSize. Else use start=end+delimiter.
|
|
|
|
start = ((end > (string::npos - delim.size())) ? string::npos : end + delim.size());
|
|
|
|
}
|
|
|
|
return strvec;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T> static bool from_string(T &t, const string &s)
|
|
|
|
{
|
|
|
|
std::istringstream iss(s);
|
|
|
|
return !(iss >> t).fail();
|
|
|
|
}
|
2009-06-21 15:41:11 +04:00
|
|
|
|
2010-01-06 22:38:45 +03:00
|
|
|
AbstractNode *TransformModule::evaluate(const Context *ctx, const ModuleInstantiation *inst) const
|
2009-06-21 15:41:11 +04:00
|
|
|
{
|
2009-07-01 13:48:25 +04:00
|
|
|
TransformNode *node = new TransformNode(inst);
|
2009-06-27 03:36:23 +04:00
|
|
|
|
2010-01-07 17:59:10 +03:00
|
|
|
for (int i = 0; i < 16; i++)
|
2010-08-28 19:34:22 +04:00
|
|
|
node->matrix[i] = i % 5 == 0 ? 1.0 : 0.0;
|
2009-06-27 03:36:23 +04:00
|
|
|
|
|
|
|
QVector<QString> argnames;
|
|
|
|
QVector<Expression*> argexpr;
|
|
|
|
|
2010-08-28 23:21:44 +04:00
|
|
|
switch (this->type) {
|
|
|
|
case SCALE:
|
2009-06-27 03:36:23 +04:00
|
|
|
argnames = QVector<QString>() << "v";
|
2010-08-28 23:21:44 +04:00
|
|
|
break;
|
|
|
|
case ROTATE:
|
2009-06-27 03:36:23 +04:00
|
|
|
argnames = QVector<QString>() << "a" << "v";
|
2010-08-28 23:21:44 +04:00
|
|
|
break;
|
|
|
|
case MIRROR:
|
2010-01-09 20:15:56 +03:00
|
|
|
argnames = QVector<QString>() << "v";
|
2010-08-28 23:21:44 +04:00
|
|
|
break;
|
|
|
|
case TRANSLATE:
|
2009-06-27 03:36:23 +04:00
|
|
|
argnames = QVector<QString>() << "v";
|
2010-08-28 23:21:44 +04:00
|
|
|
break;
|
|
|
|
case MULTMATRIX:
|
2009-06-27 03:36:23 +04:00
|
|
|
argnames = QVector<QString>() << "m";
|
2010-08-28 23:21:44 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
2010-01-07 17:59:10 +03:00
|
|
|
}
|
2009-06-27 03:36:23 +04:00
|
|
|
|
2009-12-09 17:22:04 +03:00
|
|
|
Context c(ctx);
|
|
|
|
c.args(argnames, argexpr, inst->argnames, inst->argvalues);
|
2009-06-27 03:36:23 +04:00
|
|
|
|
2010-08-28 23:21:44 +04:00
|
|
|
if (this->type == SCALE)
|
2009-06-27 03:36:23 +04:00
|
|
|
{
|
|
|
|
Value v = c.lookup_variable("v");
|
2010-08-28 19:34:22 +04:00
|
|
|
v.getnum(node->matrix[0]);
|
|
|
|
v.getnum(node->matrix[5]);
|
|
|
|
v.getnum(node->matrix[10]);
|
|
|
|
v.getv3(node->matrix[0], node->matrix[5], node->matrix[10]);
|
|
|
|
if (node->matrix[10] <= 0)
|
|
|
|
node->matrix[10] = 1;
|
2009-06-27 03:36:23 +04:00
|
|
|
}
|
2010-08-28 23:21:44 +04:00
|
|
|
else if (this->type == ROTATE)
|
2009-06-27 03:36:23 +04:00
|
|
|
{
|
|
|
|
Value val_a = c.lookup_variable("a");
|
2009-10-14 11:59:44 +04:00
|
|
|
if (val_a.type == Value::VECTOR)
|
|
|
|
{
|
2010-01-13 10:51:42 +03:00
|
|
|
for (int i = 0; i < 3 && i < val_a.vec.size(); i++) {
|
2009-10-14 11:59:44 +04:00
|
|
|
double a;
|
|
|
|
val_a.vec[i]->getnum(a);
|
|
|
|
double c = cos(a*M_PI/180.0);
|
|
|
|
double s = sin(a*M_PI/180.0);
|
|
|
|
double x = i == 0, y = i == 1, z = i == 2;
|
|
|
|
double mr[16] = {
|
|
|
|
x*x*(1-c)+c,
|
|
|
|
y*x*(1-c)+z*s,
|
|
|
|
z*x*(1-c)-y*s,
|
|
|
|
0,
|
|
|
|
x*y*(1-c)-z*s,
|
|
|
|
y*y*(1-c)+c,
|
|
|
|
z*y*(1-c)+x*s,
|
|
|
|
0,
|
|
|
|
x*z*(1-c)+y*s,
|
|
|
|
y*z*(1-c)-x*s,
|
|
|
|
z*z*(1-c)+c,
|
|
|
|
0,
|
|
|
|
0, 0, 0, 1
|
|
|
|
};
|
|
|
|
double m[16];
|
|
|
|
for (int x = 0; x < 4; x++)
|
|
|
|
for (int y = 0; y < 4; y++)
|
|
|
|
{
|
|
|
|
m[x+y*4] = 0;
|
|
|
|
for (int i = 0; i < 4; i++)
|
2010-08-28 19:34:22 +04:00
|
|
|
m[x+y*4] += node->matrix[i+y*4] * mr[x+i*4];
|
2009-10-14 11:59:44 +04:00
|
|
|
}
|
|
|
|
for (int i = 0; i < 16; i++)
|
2010-08-28 19:34:22 +04:00
|
|
|
node->matrix[i] = m[i];
|
2009-06-27 11:31:40 +04:00
|
|
|
}
|
2009-06-27 03:36:23 +04:00
|
|
|
}
|
2009-10-14 11:59:44 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Value val_v = c.lookup_variable("v");
|
|
|
|
double a = 0, x = 0, y = 0, z = 1;
|
|
|
|
|
|
|
|
val_a.getnum(a);
|
|
|
|
|
|
|
|
if (val_v.getv3(x, y, z)) {
|
|
|
|
if (x != 0.0 || y != 0.0 || z != 0.0) {
|
|
|
|
double sn = 1.0 / sqrt(x*x + y*y + z*z);
|
|
|
|
x *= sn, y *= sn, z *= sn;
|
|
|
|
}
|
|
|
|
}
|
2009-06-27 03:36:23 +04:00
|
|
|
|
2010-01-09 20:15:56 +03:00
|
|
|
if (x != 0.0 || y != 0.0 || z != 0.0)
|
|
|
|
{
|
|
|
|
double c = cos(a*M_PI/180.0);
|
|
|
|
double s = sin(a*M_PI/180.0);
|
|
|
|
|
2010-08-28 19:34:22 +04:00
|
|
|
node->matrix[ 0] = x*x*(1-c)+c;
|
|
|
|
node->matrix[ 1] = y*x*(1-c)+z*s;
|
|
|
|
node->matrix[ 2] = z*x*(1-c)-y*s;
|
2010-01-09 20:15:56 +03:00
|
|
|
|
2010-08-28 19:34:22 +04:00
|
|
|
node->matrix[ 4] = x*y*(1-c)-z*s;
|
|
|
|
node->matrix[ 5] = y*y*(1-c)+c;
|
|
|
|
node->matrix[ 6] = z*y*(1-c)+x*s;
|
2010-01-09 20:15:56 +03:00
|
|
|
|
2010-08-28 19:34:22 +04:00
|
|
|
node->matrix[ 8] = x*z*(1-c)+y*s;
|
|
|
|
node->matrix[ 9] = y*z*(1-c)-x*s;
|
|
|
|
node->matrix[10] = z*z*(1-c)+c;
|
2010-01-09 20:15:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-28 23:21:44 +04:00
|
|
|
else if (this->type == MIRROR)
|
2010-01-09 20:15:56 +03:00
|
|
|
{
|
|
|
|
Value val_v = c.lookup_variable("v");
|
|
|
|
double x = 1, y = 0, z = 0;
|
|
|
|
|
|
|
|
if (val_v.getv3(x, y, z)) {
|
|
|
|
if (x != 0.0 || y != 0.0 || z != 0.0) {
|
|
|
|
double sn = 1.0 / sqrt(x*x + y*y + z*z);
|
|
|
|
x *= sn, y *= sn, z *= sn;
|
|
|
|
}
|
|
|
|
}
|
2009-06-27 03:36:23 +04:00
|
|
|
|
2010-01-09 20:15:56 +03:00
|
|
|
if (x != 0.0 || y != 0.0 || z != 0.0)
|
|
|
|
{
|
2010-08-28 19:34:22 +04:00
|
|
|
node->matrix[ 0] = 1-2*x*x;
|
|
|
|
node->matrix[ 1] = -2*y*x;
|
|
|
|
node->matrix[ 2] = -2*z*x;
|
2009-06-27 03:36:23 +04:00
|
|
|
|
2010-08-28 19:34:22 +04:00
|
|
|
node->matrix[ 4] = -2*x*y;
|
|
|
|
node->matrix[ 5] = 1-2*y*y;
|
|
|
|
node->matrix[ 6] = -2*z*y;
|
2009-06-27 03:36:23 +04:00
|
|
|
|
2010-08-28 19:34:22 +04:00
|
|
|
node->matrix[ 8] = -2*x*z;
|
|
|
|
node->matrix[ 9] = -2*y*z;
|
|
|
|
node->matrix[10] = 1-2*z*z;
|
2009-10-14 11:59:44 +04:00
|
|
|
}
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
2010-08-28 23:21:44 +04:00
|
|
|
else if (this->type == TRANSLATE)
|
2009-06-27 03:36:23 +04:00
|
|
|
{
|
|
|
|
Value v = c.lookup_variable("v");
|
2010-08-28 19:34:22 +04:00
|
|
|
v.getv3(node->matrix[12], node->matrix[13], node->matrix[14]);
|
2009-06-27 03:36:23 +04:00
|
|
|
}
|
2010-08-28 23:21:44 +04:00
|
|
|
else if (this->type == MULTMATRIX)
|
2009-06-27 03:36:23 +04:00
|
|
|
{
|
|
|
|
Value v = c.lookup_variable("m");
|
2009-06-30 22:05:10 +04:00
|
|
|
if (v.type == Value::VECTOR) {
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
int x = i / 4, y = i % 4;
|
|
|
|
if (y < v.vec.size() && v.vec[y]->type == Value::VECTOR && x < v.vec[y]->vec.size())
|
2010-08-28 19:34:22 +04:00
|
|
|
v.vec[y]->vec[x]->getnum(node->matrix[i]);
|
2009-06-30 22:05:10 +04:00
|
|
|
}
|
2009-06-27 03:36:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-06 22:38:45 +03:00
|
|
|
foreach (ModuleInstantiation *v, inst->children) {
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *n = v->evaluate(inst->ctx);
|
2009-06-30 00:30:43 +04:00
|
|
|
if (n != NULL)
|
2011-08-04 06:58:17 +04:00
|
|
|
node->children.push_back(n);
|
2009-06-30 00:30:43 +04:00
|
|
|
}
|
2009-06-27 03:36:23 +04:00
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2011-07-11 05:54:20 +04:00
|
|
|
string TransformNode::toString() const
|
2010-03-02 21:22:31 +03:00
|
|
|
{
|
|
|
|
std::stringstream stream;
|
|
|
|
|
2011-09-03 01:35:10 +04:00
|
|
|
stream << "multmatrix([";
|
|
|
|
for (int j=0;j<4;j++) {
|
|
|
|
stream << "[";
|
|
|
|
for (int i=0;i<4;i++) {
|
|
|
|
// FIXME: The 0 test is to avoid a leading minus before a single 0 (cosmetics)
|
|
|
|
stream << ((this->matrix[i*4+j]==0)?0:this->matrix[i*4+j]);
|
|
|
|
if (i != 3) stream << ", ";
|
2010-03-02 21:22:31 +03:00
|
|
|
}
|
2011-09-03 01:35:10 +04:00
|
|
|
stream << "]";
|
|
|
|
if (j != 3) stream << ", ";
|
2010-03-02 21:22:31 +03:00
|
|
|
}
|
2011-09-03 01:35:10 +04:00
|
|
|
stream << "])";
|
2010-03-02 21:22:31 +03:00
|
|
|
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
2011-07-11 05:54:20 +04:00
|
|
|
string TransformNode::name() const
|
2010-08-28 23:21:44 +04:00
|
|
|
{
|
|
|
|
return "transform";
|
|
|
|
}
|
|
|
|
|
2009-06-30 00:30:43 +04:00
|
|
|
void register_builtin_transform()
|
2009-06-21 15:41:11 +04:00
|
|
|
{
|
2009-06-27 03:36:23 +04:00
|
|
|
builtin_modules["scale"] = new TransformModule(SCALE);
|
|
|
|
builtin_modules["rotate"] = new TransformModule(ROTATE);
|
2010-01-09 20:15:56 +03:00
|
|
|
builtin_modules["mirror"] = new TransformModule(MIRROR);
|
2009-06-27 03:36:23 +04:00
|
|
|
builtin_modules["translate"] = new TransformModule(TRANSLATE);
|
|
|
|
builtin_modules["multmatrix"] = new TransformModule(MULTMATRIX);
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|