2009-06-20 23:00:19 +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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openscad.h"
|
|
|
|
|
2009-06-23 14:31:25 +04:00
|
|
|
Value::Value()
|
|
|
|
{
|
|
|
|
reset_undef();
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(bool v)
|
|
|
|
{
|
|
|
|
reset_undef();
|
|
|
|
type = BOOL;
|
|
|
|
b = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(double v)
|
|
|
|
{
|
|
|
|
reset_undef();
|
|
|
|
type = NUMBER;
|
|
|
|
num = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(double v1, double v2, double v3)
|
|
|
|
{
|
|
|
|
reset_undef();
|
|
|
|
type = VECTOR;
|
|
|
|
x = v1;
|
|
|
|
y = v2;
|
|
|
|
z = v3;
|
|
|
|
}
|
2009-06-21 10:53:46 +04:00
|
|
|
|
2009-06-23 14:31:25 +04:00
|
|
|
Value::Value(double m[16])
|
|
|
|
{
|
|
|
|
reset_undef();
|
|
|
|
type = MATRIX;
|
|
|
|
for (int i=0; i<16; i++)
|
|
|
|
this->m[i] = m[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(const QString &t)
|
|
|
|
{
|
|
|
|
reset_undef();
|
|
|
|
type = STRING;
|
|
|
|
text = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(const Value &v)
|
|
|
|
{
|
|
|
|
reset_undef();
|
|
|
|
type = v.type;
|
|
|
|
b = v.b;
|
|
|
|
num = v.num;
|
|
|
|
x = v.x;
|
|
|
|
y = v.y;
|
|
|
|
z = v.z;
|
|
|
|
for (int i=0; i<16; i++)
|
|
|
|
m[i] = v.m[i];
|
|
|
|
text = v.text;
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator = (const Value &v)
|
|
|
|
{
|
2009-06-23 14:31:25 +04:00
|
|
|
reset_undef();
|
|
|
|
type = v.type;
|
|
|
|
b = v.b;
|
|
|
|
num = v.num;
|
2009-06-20 23:00:19 +04:00
|
|
|
x = v.x;
|
|
|
|
y = v.y;
|
|
|
|
z = v.z;
|
2009-06-23 14:31:25 +04:00
|
|
|
for (int i=0; i<16; i++)
|
|
|
|
m[i] = v.m[i];
|
|
|
|
text = v.text;
|
2009-06-20 23:00:19 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
Value Value::operator + (const Value &v) const
|
|
|
|
{
|
2009-06-23 14:31:25 +04:00
|
|
|
if (type == VECTOR && v.type == VECTOR) {
|
2009-06-20 23:00:19 +04:00
|
|
|
return Value(x + v.x, y + v.y, z + v.z);
|
2009-06-23 14:31:25 +04:00
|
|
|
}
|
|
|
|
if (type == MATRIX && v.type == MATRIX) {
|
|
|
|
double m_[16];
|
|
|
|
for (int i=0; i<16; i++)
|
|
|
|
m_[i] = m[i] + v.m[i];
|
|
|
|
return Value(m);
|
|
|
|
}
|
|
|
|
if (type == NUMBER && v.type == NUMBER) {
|
|
|
|
return Value(num + v.num);
|
|
|
|
}
|
2009-06-20 23:00:19 +04:00
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
Value Value::operator - (const Value &v) const
|
|
|
|
{
|
2009-06-23 14:31:25 +04:00
|
|
|
if (type == VECTOR && v.type == VECTOR) {
|
|
|
|
return Value(x + v.x, y + v.y, z + v.z);
|
|
|
|
}
|
|
|
|
if (type == MATRIX && v.type == MATRIX) {
|
|
|
|
double m_[16];
|
|
|
|
for (int i=0; i<16; i++)
|
|
|
|
m_[i] = m[i] + v.m[i];
|
|
|
|
return Value(m);
|
|
|
|
}
|
|
|
|
if (type == NUMBER && v.type == NUMBER) {
|
|
|
|
return Value(num + v.num);
|
|
|
|
}
|
2009-06-20 23:00:19 +04:00
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
Value Value::operator * (const Value &v) const
|
|
|
|
{
|
2009-06-23 14:31:25 +04:00
|
|
|
if (type == VECTOR && v.type == VECTOR) {
|
2009-06-20 23:00:19 +04:00
|
|
|
double nx = (y-v.y)*(z-v.z) - (z-v.z)*(y-v.y);
|
|
|
|
double ny = (z-v.z)*(x-v.x) - (x-v.x)*(z-v.z);
|
|
|
|
double nz = (x-v.x)*(y-v.y) - (y-v.y)*(x-v.x);
|
|
|
|
return Value(nx, ny, nz);
|
|
|
|
}
|
2009-06-23 14:31:25 +04:00
|
|
|
if (type == VECTOR && v.type == NUMBER) {
|
|
|
|
return Value(x * v.num, y * v.num, z * v.num);
|
|
|
|
}
|
|
|
|
if (type == NUMBER && v.type == VECTOR) {
|
|
|
|
return Value(num * v.x, num * v.y, num * v.z);
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
2009-06-23 14:31:25 +04:00
|
|
|
if (type == NUMBER && v.type == NUMBER) {
|
|
|
|
return Value(num * v.num);
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
2009-06-23 14:31:25 +04:00
|
|
|
return Value();
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
Value Value::operator / (const Value &v) const
|
|
|
|
{
|
2009-06-23 14:31:25 +04:00
|
|
|
if (type == NUMBER && v.type == NUMBER) {
|
|
|
|
return Value(num / v.num);
|
|
|
|
}
|
|
|
|
return Value();
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
Value Value::operator % (const Value &v) const
|
|
|
|
{
|
2009-06-23 14:31:25 +04:00
|
|
|
if (type == NUMBER && v.type == NUMBER) {
|
|
|
|
return Value(fmod(num, v.num));
|
|
|
|
}
|
|
|
|
return Value();
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
Value Value::inv() const
|
|
|
|
{
|
2009-06-23 14:31:25 +04:00
|
|
|
if (type == MATRIX) {
|
|
|
|
double m_[16];
|
|
|
|
for (int i=0; i<16; i++)
|
|
|
|
m_[i] = -m[i];
|
|
|
|
return Value(m);
|
|
|
|
}
|
|
|
|
if (type == VECTOR)
|
2009-06-20 23:00:19 +04:00
|
|
|
return Value(-x, -y, -z);
|
2009-06-23 14:31:25 +04:00
|
|
|
if (type == NUMBER)
|
|
|
|
return Value(-x);
|
|
|
|
return Value();
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
QString Value::dump() const
|
|
|
|
{
|
2009-06-23 14:31:25 +04:00
|
|
|
if (type == STRING) {
|
|
|
|
return QString("\"") + text + QString("\"");
|
|
|
|
}
|
|
|
|
if (type == MATRIX) {
|
|
|
|
QString text = "[";
|
|
|
|
for (int i=0; i<16; i++) {
|
|
|
|
QString t;
|
2009-06-27 03:36:23 +04:00
|
|
|
t.sprintf("%f", m[i == 15 ? 15 : (i*4) % 15]);
|
2009-06-23 14:31:25 +04:00
|
|
|
if (i % 4 == 0 && i > 0)
|
|
|
|
text += ";";
|
|
|
|
if (i > 0)
|
|
|
|
text += " ";
|
|
|
|
text += t;
|
|
|
|
}
|
|
|
|
text += "]";
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
if (type == VECTOR) {
|
2009-06-21 12:59:53 +04:00
|
|
|
QString text;
|
|
|
|
text.sprintf("[%f %f %f]", x, y, z);
|
|
|
|
return text;
|
|
|
|
}
|
2009-06-23 14:31:25 +04:00
|
|
|
if (type == RANGE) {
|
|
|
|
QString text;
|
|
|
|
text.sprintf("[ %f : %f : %f ]", r_begin, r_step, r_end);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
if (type == NUMBER) {
|
|
|
|
QString text;
|
|
|
|
text.sprintf("%f", num);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
if (type == BOOL) {
|
|
|
|
return QString(b ? "true" : "false");
|
|
|
|
}
|
|
|
|
return QString("undef");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Value::reset_undef()
|
|
|
|
{
|
|
|
|
type = UNDEFINED;
|
|
|
|
b = false;
|
|
|
|
num = 0;
|
|
|
|
r_begin = 0;
|
|
|
|
r_step = 0;
|
|
|
|
r_end = 0;
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
z = 0;
|
|
|
|
for (int i=0; i<16; i++)
|
|
|
|
m[i] = 0;
|
|
|
|
text = QString();
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
|