2009-06-23 02:36:36 +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"
|
|
|
|
|
|
|
|
enum primitive_type_e {
|
|
|
|
CUBE,
|
|
|
|
SPHERE,
|
2009-10-05 13:38:48 +04:00
|
|
|
CYLINDER,
|
2009-10-19 23:50:00 +04:00
|
|
|
POLYHEDRON
|
2009-06-23 02:36:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class PrimitiveModule : public AbstractModule
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
primitive_type_e type;
|
|
|
|
PrimitiveModule(primitive_type_e type) : type(type) { }
|
2009-07-01 13:48:25 +04:00
|
|
|
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstanciation *inst) const;
|
2009-06-23 02:36:36 +04:00
|
|
|
};
|
|
|
|
|
2009-06-24 14:59:15 +04:00
|
|
|
class PrimitiveNode : public AbstractPolyNode
|
2009-06-23 02:36:36 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool center;
|
|
|
|
double x, y, z, h, r1, r2;
|
2009-06-30 22:05:10 +04:00
|
|
|
double fn, fs, fa;
|
2009-06-23 02:36:36 +04:00
|
|
|
primitive_type_e type;
|
2009-10-05 13:38:48 +04:00
|
|
|
Value points, triangles;
|
2009-07-01 13:48:25 +04:00
|
|
|
PrimitiveNode(const ModuleInstanciation *mi, primitive_type_e type) : AbstractPolyNode(mi), type(type) { }
|
2009-06-24 14:59:15 +04:00
|
|
|
virtual PolySet *render_polyset(render_mode_e mode) const;
|
2009-06-23 02:36:36 +04:00
|
|
|
virtual QString dump(QString indent) const;
|
|
|
|
};
|
|
|
|
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *PrimitiveModule::evaluate(const Context *ctx, const ModuleInstanciation *inst) const
|
2009-06-23 02:36:36 +04:00
|
|
|
{
|
2009-07-01 13:48:25 +04:00
|
|
|
PrimitiveNode *node = new PrimitiveNode(inst, type);
|
2009-06-23 02:36:36 +04:00
|
|
|
|
|
|
|
node->center = false;
|
|
|
|
node->x = node->y = node->z = node->h = node->r1 = node->r2 = 1;
|
|
|
|
|
|
|
|
QVector<QString> argnames;
|
|
|
|
QVector<Expression*> argexpr;
|
|
|
|
|
|
|
|
if (type == CUBE) {
|
|
|
|
argnames = QVector<QString>() << "size" << "center";
|
|
|
|
}
|
|
|
|
if (type == SPHERE) {
|
|
|
|
argnames = QVector<QString>() << "r";
|
|
|
|
}
|
|
|
|
if (type == CYLINDER) {
|
|
|
|
argnames = QVector<QString>() << "h" << "r1" << "r2" << "center";
|
|
|
|
}
|
2009-10-19 23:50:00 +04:00
|
|
|
if (type == POLYHEDRON) {
|
2009-10-05 13:38:48 +04:00
|
|
|
argnames = QVector<QString>() << "points" << "triangles";
|
|
|
|
}
|
2009-06-23 02:36:36 +04:00
|
|
|
|
|
|
|
Context c(ctx);
|
2009-07-01 13:48:25 +04:00
|
|
|
c.args(argnames, argexpr, inst->argnames, inst->argvalues);
|
2009-06-23 02:36:36 +04:00
|
|
|
|
2009-06-30 22:05:10 +04:00
|
|
|
node->fn = c.lookup_variable("$fn").num;
|
2009-06-27 02:09:50 +04:00
|
|
|
node->fs = c.lookup_variable("$fs").num;
|
|
|
|
node->fa = c.lookup_variable("$fa").num;
|
2009-06-26 08:32:52 +04:00
|
|
|
|
2009-06-23 02:36:36 +04:00
|
|
|
if (type == CUBE) {
|
|
|
|
Value size = c.lookup_variable("size");
|
|
|
|
Value center = c.lookup_variable("center");
|
2009-06-30 22:05:10 +04:00
|
|
|
size.getnum(node->x);
|
|
|
|
size.getnum(node->y);
|
|
|
|
size.getnum(node->z);
|
|
|
|
size.getv3(node->x, node->y, node->z);
|
2009-06-23 14:31:25 +04:00
|
|
|
if (center.type == Value::BOOL) {
|
|
|
|
node->center = center.b;
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == SPHERE) {
|
|
|
|
Value r = c.lookup_variable("r");
|
2009-06-23 14:31:25 +04:00
|
|
|
if (r.type == Value::NUMBER) {
|
|
|
|
node->r1 = r.num;
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == CYLINDER) {
|
|
|
|
Value h = c.lookup_variable("h");
|
2009-06-30 22:05:10 +04:00
|
|
|
Value r, r1, r2;
|
|
|
|
r1 = c.lookup_variable("r1");
|
|
|
|
r2 = c.lookup_variable("r2");
|
|
|
|
if (r1.type != Value::NUMBER && r1.type != Value::NUMBER)
|
|
|
|
r = c.lookup_variable("r");
|
2009-06-23 02:36:36 +04:00
|
|
|
Value center = c.lookup_variable("center");
|
2009-06-23 14:31:25 +04:00
|
|
|
if (h.type == Value::NUMBER) {
|
|
|
|
node->h = h.num;
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
2009-06-23 14:31:25 +04:00
|
|
|
if (r.type == Value::NUMBER) {
|
|
|
|
node->r1 = r.num;
|
|
|
|
node->r2 = r.num;
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
2009-06-23 14:31:25 +04:00
|
|
|
if (r1.type == Value::NUMBER) {
|
|
|
|
node->r1 = r1.num;
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
2009-06-23 14:31:25 +04:00
|
|
|
if (r2.type == Value::NUMBER) {
|
2009-06-23 15:28:35 +04:00
|
|
|
node->r2 = r2.num;
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
2009-06-23 14:31:25 +04:00
|
|
|
if (center.type == Value::BOOL) {
|
|
|
|
node->center = center.b;
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-19 23:50:00 +04:00
|
|
|
if (type == POLYHEDRON) {
|
2009-10-05 13:38:48 +04:00
|
|
|
node->points = c.lookup_variable("points");
|
|
|
|
node->triangles = c.lookup_variable("triangles");
|
|
|
|
}
|
|
|
|
|
2009-06-23 02:36:36 +04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2009-06-30 00:30:43 +04:00
|
|
|
void register_builtin_primitives()
|
2009-06-23 02:36:36 +04:00
|
|
|
{
|
|
|
|
builtin_modules["cube"] = new PrimitiveModule(CUBE);
|
|
|
|
builtin_modules["sphere"] = new PrimitiveModule(SPHERE);
|
|
|
|
builtin_modules["cylinder"] = new PrimitiveModule(CYLINDER);
|
2009-10-19 23:50:00 +04:00
|
|
|
builtin_modules["polyhedron"] = new PrimitiveModule(POLYHEDRON);
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
|
|
|
|
2009-06-30 22:05:10 +04:00
|
|
|
int get_fragments_from_r(double r, double fn, double fs, double fa)
|
2009-06-25 23:53:12 +04:00
|
|
|
{
|
2009-06-30 22:05:10 +04:00
|
|
|
if (fn > 0.0)
|
2009-08-10 11:18:35 +04:00
|
|
|
return (int)fn;
|
2009-06-26 14:56:50 +04:00
|
|
|
return (int)ceil(fmax(fmin(360.0 / fa, r*M_PI / fs), 5));
|
2009-06-25 23:53:12 +04:00
|
|
|
}
|
2009-06-23 23:56:46 +04:00
|
|
|
|
2009-06-29 15:03:15 +04:00
|
|
|
PolySet *PrimitiveNode::render_polyset(render_mode_e) const
|
2009-06-23 02:36:36 +04:00
|
|
|
{
|
2009-06-24 14:59:15 +04:00
|
|
|
PolySet *p = new PolySet();
|
|
|
|
|
|
|
|
if (type == CUBE && x > 0 && y > 0 && z > 0)
|
|
|
|
{
|
|
|
|
double x1, x2, y1, y2, z1, z2;
|
|
|
|
if (center) {
|
|
|
|
x1 = -x/2;
|
|
|
|
x2 = +x/2;
|
|
|
|
y1 = -y/2;
|
|
|
|
y2 = +y/2;
|
|
|
|
z1 = -z/2;
|
|
|
|
z2 = +z/2;
|
|
|
|
} else {
|
|
|
|
x1 = y1 = z1 = 0;
|
|
|
|
x2 = x;
|
|
|
|
y2 = y;
|
|
|
|
z2 = z;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->append_poly(); // top
|
|
|
|
p->append_vertex(x1, y1, z2);
|
|
|
|
p->append_vertex(x2, y1, z2);
|
|
|
|
p->append_vertex(x2, y2, z2);
|
|
|
|
p->append_vertex(x1, y2, z2);
|
|
|
|
|
|
|
|
p->append_poly(); // bottom
|
|
|
|
p->append_vertex(x1, y2, z1);
|
|
|
|
p->append_vertex(x2, y2, z1);
|
|
|
|
p->append_vertex(x2, y1, z1);
|
|
|
|
p->append_vertex(x1, y1, z1);
|
|
|
|
|
|
|
|
p->append_poly(); // side1
|
|
|
|
p->append_vertex(x1, y1, z1);
|
|
|
|
p->append_vertex(x2, y1, z1);
|
|
|
|
p->append_vertex(x2, y1, z2);
|
|
|
|
p->append_vertex(x1, y1, z2);
|
|
|
|
|
|
|
|
p->append_poly(); // side2
|
|
|
|
p->append_vertex(x2, y1, z1);
|
|
|
|
p->append_vertex(x2, y2, z1);
|
|
|
|
p->append_vertex(x2, y2, z2);
|
|
|
|
p->append_vertex(x2, y1, z2);
|
|
|
|
|
|
|
|
p->append_poly(); // side3
|
|
|
|
p->append_vertex(x2, y2, z1);
|
|
|
|
p->append_vertex(x1, y2, z1);
|
|
|
|
p->append_vertex(x1, y2, z2);
|
|
|
|
p->append_vertex(x2, y2, z2);
|
|
|
|
|
|
|
|
p->append_poly(); // side4
|
|
|
|
p->append_vertex(x1, y2, z1);
|
|
|
|
p->append_vertex(x1, y1, z1);
|
|
|
|
p->append_vertex(x1, y1, z2);
|
|
|
|
p->append_vertex(x1, y2, z2);
|
|
|
|
}
|
2009-06-23 02:36:36 +04:00
|
|
|
|
2009-06-24 14:59:15 +04:00
|
|
|
if (type == SPHERE && r1 > 0)
|
|
|
|
{
|
2009-06-25 23:53:12 +04:00
|
|
|
struct point2d {
|
|
|
|
double x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ring_s {
|
|
|
|
int fragments;
|
|
|
|
point2d *points;
|
|
|
|
double r, z;
|
|
|
|
};
|
|
|
|
|
2009-06-30 22:05:10 +04:00
|
|
|
int rings = get_fragments_from_r(r1, fn, fs, fa);
|
2009-06-25 23:53:12 +04:00
|
|
|
ring_s ring[rings];
|
|
|
|
|
|
|
|
for (int i = 0; i < rings; i++) {
|
|
|
|
double phi = (M_PI * (i + 0.5)) / rings;
|
|
|
|
ring[i].r = r1 * sin(phi);
|
|
|
|
ring[i].z = r1 * cos(phi);
|
2009-06-30 22:05:10 +04:00
|
|
|
ring[i].fragments = get_fragments_from_r(ring[i].r, fn, fs, fa);
|
2009-06-25 23:53:12 +04:00
|
|
|
ring[i].points = new point2d[ring[i].fragments];
|
|
|
|
for (int j = 0; j < ring[i].fragments; j++) {
|
|
|
|
phi = (M_PI*2*j) / ring[i].fragments;
|
|
|
|
ring[i].points[j].x = ring[i].r * cos(phi);
|
|
|
|
ring[i].points[j].y = ring[i].r * sin(phi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p->append_poly();
|
|
|
|
for (int i = 0; i < ring[0].fragments; i++)
|
2009-06-26 14:56:50 +04:00
|
|
|
p->append_vertex(ring[0].points[i].x, ring[0].points[i].y, ring[0].z);
|
2009-06-25 23:53:12 +04:00
|
|
|
|
|
|
|
for (int i = 0; i < rings-1; i++) {
|
|
|
|
ring_s *r1 = &ring[i];
|
|
|
|
ring_s *r2 = &ring[i+1];
|
|
|
|
int r1i = 0, r2i = 0;
|
|
|
|
while (r1i < r1->fragments || r2i < r2->fragments)
|
|
|
|
{
|
|
|
|
if (r1i >= r1->fragments)
|
|
|
|
goto sphere_next_r2;
|
|
|
|
if (r2i >= r2->fragments)
|
|
|
|
goto sphere_next_r1;
|
|
|
|
if ((double)r1i / r1->fragments <
|
|
|
|
(double)r2i / r2->fragments)
|
|
|
|
{
|
|
|
|
sphere_next_r1:
|
|
|
|
p->append_poly();
|
|
|
|
int r1j = (r1i+1) % r1->fragments;
|
2009-06-26 14:56:50 +04:00
|
|
|
p->insert_vertex(r1->points[r1i].x, r1->points[r1i].y, r1->z);
|
|
|
|
p->insert_vertex(r1->points[r1j].x, r1->points[r1j].y, r1->z);
|
|
|
|
p->insert_vertex(r2->points[r2i % r2->fragments].x, r2->points[r2i % r2->fragments].y, r2->z);
|
2009-06-25 23:53:12 +04:00
|
|
|
r1i++;
|
|
|
|
} else {
|
|
|
|
sphere_next_r2:
|
|
|
|
p->append_poly();
|
|
|
|
int r2j = (r2i+1) % r2->fragments;
|
|
|
|
p->append_vertex(r2->points[r2i].x, r2->points[r2i].y, r2->z);
|
|
|
|
p->append_vertex(r2->points[r2j].x, r2->points[r2j].y, r2->z);
|
|
|
|
p->append_vertex(r1->points[r1i % r1->fragments].x, r1->points[r1i % r1->fragments].y, r1->z);
|
|
|
|
r2i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p->append_poly();
|
|
|
|
for (int i = 0; i < ring[rings-1].fragments; i++)
|
2009-06-26 14:56:50 +04:00
|
|
|
p->insert_vertex(ring[rings-1].points[i].x, ring[rings-1].points[i].y, ring[rings-1].z);
|
2009-06-24 14:59:15 +04:00
|
|
|
}
|
2009-06-23 02:36:36 +04:00
|
|
|
|
2009-06-24 14:59:15 +04:00
|
|
|
if (type == CYLINDER && h > 0 && r1 >=0 && r2 >= 0 && (r1 > 0 || r2 > 0))
|
2009-06-23 02:36:36 +04:00
|
|
|
{
|
2009-06-30 22:05:10 +04:00
|
|
|
int fragments = get_fragments_from_r(fmax(r1, r2), fn, fs, fa);
|
2009-06-24 14:59:15 +04:00
|
|
|
|
|
|
|
double z1, z2;
|
|
|
|
if (center) {
|
|
|
|
z1 = -h/2;
|
|
|
|
z2 = +h/2;
|
|
|
|
} else {
|
|
|
|
z1 = 0;
|
|
|
|
z2 = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct point2d {
|
|
|
|
double x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
point2d circle1[fragments];
|
|
|
|
point2d circle2[fragments];
|
|
|
|
|
|
|
|
for (int i=0; i<fragments; i++) {
|
|
|
|
double phi = (M_PI*2*i) / fragments;
|
2009-07-14 09:25:55 +04:00
|
|
|
if (r1 > 0) {
|
|
|
|
circle1[i].x = r1*cos(phi);
|
|
|
|
circle1[i].y = r1*sin(phi);
|
|
|
|
} else {
|
|
|
|
circle1[i].x = 0;
|
|
|
|
circle1[i].y = 0;
|
|
|
|
}
|
|
|
|
if (r2 > 0) {
|
|
|
|
circle2[i].x = r2*cos(phi);
|
|
|
|
circle2[i].y = r2*sin(phi);
|
|
|
|
} else {
|
|
|
|
circle2[i].x = 0;
|
|
|
|
circle2[i].y = 0;
|
|
|
|
}
|
2009-06-24 14:59:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i<fragments; i++) {
|
|
|
|
int j = (i+1) % fragments;
|
2009-06-30 22:19:57 +04:00
|
|
|
if (r1 > 0) {
|
|
|
|
p->append_poly();
|
|
|
|
p->insert_vertex(circle1[i].x, circle1[i].y, z1);
|
|
|
|
p->insert_vertex(circle2[i].x, circle2[i].y, z2);
|
|
|
|
p->insert_vertex(circle1[j].x, circle1[j].y, z1);
|
|
|
|
}
|
|
|
|
if (r2 > 0) {
|
|
|
|
p->append_poly();
|
|
|
|
p->insert_vertex(circle2[i].x, circle2[i].y, z2);
|
|
|
|
p->insert_vertex(circle2[j].x, circle2[j].y, z2);
|
|
|
|
p->insert_vertex(circle1[j].x, circle1[j].y, z1);
|
|
|
|
}
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
|
|
|
|
2009-06-24 14:59:15 +04:00
|
|
|
if (r1 > 0) {
|
|
|
|
p->append_poly();
|
|
|
|
for (int i=0; i<fragments; i++)
|
2009-06-25 13:49:28 +04:00
|
|
|
p->insert_vertex(circle1[i].x, circle1[i].y, z1);
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
|
|
|
|
2009-06-24 14:59:15 +04:00
|
|
|
if (r2 > 0) {
|
|
|
|
p->append_poly();
|
|
|
|
for (int i=0; i<fragments; i++)
|
2009-06-25 13:49:28 +04:00
|
|
|
p->append_vertex(circle2[i].x, circle2[i].y, z2);
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-19 23:50:00 +04:00
|
|
|
if (type == POLYHEDRON)
|
2009-10-05 13:38:48 +04:00
|
|
|
{
|
|
|
|
for (int i=0; i<triangles.vec.size(); i++)
|
|
|
|
{
|
|
|
|
p->append_poly();
|
|
|
|
for (int j=0; j<triangles.vec[i]->vec.size(); j++) {
|
|
|
|
int pt = triangles.vec[i]->vec[j]->num;
|
|
|
|
double px = points.vec[pt]->vec[0]->num;
|
|
|
|
double py = points.vec[pt]->vec[1]->num;
|
|
|
|
double pz = points.vec[pt]->vec[2]->num;
|
|
|
|
p->insert_vertex(px, py, pz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-24 14:59:15 +04:00
|
|
|
return p;
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString PrimitiveNode::dump(QString indent) const
|
|
|
|
{
|
2009-07-03 11:51:22 +04:00
|
|
|
if (dump_cache.isEmpty()) {
|
|
|
|
QString text;
|
|
|
|
if (type == CUBE)
|
|
|
|
text.sprintf("cube(size = [%f %f %f], center = %s);\n", x, y, z, center ? "true" : "false");
|
|
|
|
if (type == SPHERE)
|
|
|
|
text.sprintf("sphere($fn = %f, $fa = %f, $fs = %f, r = %f);\n", fn, fa, fs, r1);
|
|
|
|
if (type == CYLINDER)
|
|
|
|
text.sprintf("cylinder($fn = %f, $fa = %f, $fs = %f, h = %f, r1 = %f, r2 = %f, center = %s);\n", fn, fa, fs, h, r1, r2, center ? "true" : "false");
|
2009-10-19 23:50:00 +04:00
|
|
|
if (type == POLYHEDRON)
|
|
|
|
text.sprintf("polyhedron(points = %s, triangles = %s);\n", points.dump().toAscii().data(), triangles.dump().toAscii().data());
|
2009-07-03 11:51:22 +04:00
|
|
|
((AbstractNode*)this)->dump_cache = indent + QString("n%1: ").arg(idx) + text;
|
|
|
|
}
|
|
|
|
return dump_cache;
|
2009-06-23 02:36:36 +04:00
|
|
|
}
|
|
|
|
|