summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mainwin.cc38
-rw-r--r--openscad.h12
-rw-r--r--polyset.cc29
-rw-r--r--primitives.cc44
-rw-r--r--transform.cc4
5 files changed, 116 insertions, 11 deletions
diff --git a/mainwin.cc b/mainwin.cc
index 4fe8724..836901f 100644
--- a/mainwin.cc
+++ b/mainwin.cc
@@ -910,7 +910,9 @@ void MainWindow::actionRenderCGAL()
QApplication::processEvents();
if (root_N->dim == 2) {
- // FIXME
+ PRINTF(" Top level object is a 2D object.");
+ PRINTF(" Empty: %6s", root_N->p2.is_empty() ? "yes" : "no");
+ PRINTF(" Plane: %6s", root_N->p2.is_plane() ? "yes" : "no");
}
if (root_N->dim == 3) {
@@ -1192,7 +1194,39 @@ static void renderGLviaCGAL(void *vp)
delete p;
m->cgal_ogl_p = NULL;
}
- if (m->root_N && m->root_N->dim == 3) {
+ if (m->root_N && m->root_N->dim == 2)
+ {
+ typedef CGAL_Nef_polyhedron2::Explorer Explorer;
+ typedef Explorer::Face_const_iterator fci_t;
+ typedef Explorer::Halfedge_around_face_const_circulator heafcc_t;
+ typedef Explorer::Point Point;
+ Explorer E = m->root_N->p2.explorer();
+
+ for (fci_t fit = E.faces_begin(), fend = E.faces_end(); fit != fend; ++fit)
+ {
+ bool fset = false;
+ double fx = 0.0, fy = 0.0;
+ heafcc_t fcirc(E.halfedge(fit)), fend(fcirc);
+ CGAL_For_all(fcirc, fend) {
+ if(E.is_standard(E.target(fcirc))) {
+ Point p = E.point(E.target(fcirc));
+ double x = to_double(p.x()), y = to_double(p.y());
+ if (!fset) {
+ glBegin(GL_LINE_STRIP);
+ fx = x, fy = y;
+ fset = true;
+ }
+ glVertex3d(x, y, 0.0);
+ }
+ }
+ if (fset) {
+ glVertex3d(fx, fy, 0.0);
+ glEnd();
+ }
+ }
+ }
+ if (m->root_N && m->root_N->dim == 3)
+ {
CGAL::OGL::Polyhedron *p = (CGAL::OGL::Polyhedron*)m->cgal_ogl_p;
if (!p) {
m->cgal_ogl_p = p = new CGAL::OGL::Polyhedron();
diff --git a/openscad.h b/openscad.h
index b765bd2..dfc3912 100644
--- a/openscad.h
+++ b/openscad.h
@@ -468,6 +468,7 @@ public:
typedef CGAL::Extended_cartesian<CGAL::Gmpq> CGAL_Kernel2;
typedef CGAL::Nef_polyhedron_2<CGAL_Kernel2> CGAL_Nef_polyhedron2;
+typedef CGAL_Kernel2::Aff_transformation_2 CGAL_Aff_transformation2;
typedef CGAL::Cartesian<CGAL::Gmpq> CGAL_Kernel3;
typedef CGAL::Polyhedron_3<CGAL_Kernel3> CGAL_Polyhedron;
@@ -501,7 +502,7 @@ struct CGAL_Nef_polyhedron
int weight() {
if (dim == 2)
- return 100;
+ return p2.explorer().number_of_vertices();
if (dim == 3)
return p3.number_of_vertices();
return 0;
@@ -525,6 +526,8 @@ public:
typedef QList<Point> Polygon;
QVector<Polygon> polygons;
Grid3d<void*> grid;
+
+ bool is2d;
int convexity;
PolySet();
@@ -534,6 +537,13 @@ public:
void append_vertex(double x, double y, double z);
void insert_vertex(double x, double y, double z);
+ void append_vertex(double x, double y) {
+ append_vertex(x, y, 0.0);
+ }
+ void insert_vertex(double x, double y) {
+ insert_vertex(x, y, 0.0);
+ }
+
enum colormode_e {
COLORMODE_NONE,
COLORMODE_MATERIAL,
diff --git a/polyset.cc b/polyset.cc
index 467b096..9e0216a 100644
--- a/polyset.cc
+++ b/polyset.cc
@@ -26,6 +26,7 @@ QCache<QString,PolySetPtr> PolySet::ps_cache(100);
PolySet::PolySet()
{
+ is2d = false;
convexity = 1;
refcount = 1;
}
@@ -284,14 +285,30 @@ public:
CGAL_Nef_polyhedron PolySet::render_cgal_nef_polyhedron() const
{
- CGAL_Polyhedron P;
- CGAL_Build_PolySet builder(this);
- P.delegate(builder);
+ if (this->is2d)
+ {
+ int len = this->polygons[0].size();
+ if (len > 0) {
+ CGAL_Nef_polyhedron2::Point points[len];
+ for (int i = 0; i < len; i++)
+ points[i] = CGAL_Nef_polyhedron2::Point(this->polygons[0][i].x,
+ this->polygons[0][i].y);
+ CGAL_Nef_polyhedron2 N(points, points+len);
+ return CGAL_Nef_polyhedron(N);
+ }
+ }
+ else
+ {
+ CGAL_Polyhedron P;
+ CGAL_Build_PolySet builder(this);
+ P.delegate(builder);
#if 0
- std::cout << P;
+ std::cout << P;
#endif
- CGAL_Nef_polyhedron3 N(P);
- return CGAL_Nef_polyhedron(N);
+ CGAL_Nef_polyhedron3 N(P);
+ return CGAL_Nef_polyhedron(N);
+ }
+ return CGAL_Nef_polyhedron();
}
#endif /* ENABLE_CGAL */
diff --git a/primitives.cc b/primitives.cc
index b113c3b..893a0aa 100644
--- a/primitives.cc
+++ b/primitives.cc
@@ -26,7 +26,8 @@ enum primitive_type_e {
CUBE,
SPHERE,
CYLINDER,
- POLYHEDRON
+ POLYHEDRON,
+ SQUARE
};
class PrimitiveModule : public AbstractModule
@@ -72,6 +73,9 @@ AbstractNode *PrimitiveModule::evaluate(const Context *ctx, const ModuleInstanci
if (type == POLYHEDRON) {
argnames = QVector<QString>() << "points" << "triangles";
}
+ if (type == SQUARE) {
+ argnames = QVector<QString>() << "size" << "center";
+ }
Context c(ctx);
c.args(argnames, argexpr, inst->argnames, inst->argvalues);
@@ -130,6 +134,17 @@ AbstractNode *PrimitiveModule::evaluate(const Context *ctx, const ModuleInstanci
node->triangles = c.lookup_variable("triangles");
}
+ if (type == SQUARE) {
+ Value size = c.lookup_variable("size");
+ Value center = c.lookup_variable("center");
+ size.getnum(node->x);
+ size.getnum(node->y);
+ size.getv2(node->x, node->y);
+ if (center.type == Value::BOOL) {
+ node->center = center.b;
+ }
+ }
+
return node;
}
@@ -139,6 +154,7 @@ void register_builtin_primitives()
builtin_modules["sphere"] = new PrimitiveModule(SPHERE);
builtin_modules["cylinder"] = new PrimitiveModule(CYLINDER);
builtin_modules["polyhedron"] = new PrimitiveModule(POLYHEDRON);
+ builtin_modules["square"] = new PrimitiveModule(SQUARE);
}
int get_fragments_from_r(double r, double fn, double fs, double fa)
@@ -357,6 +373,28 @@ sphere_next_r2:
}
}
+ if (type == SQUARE)
+ {
+ double x1, x2, y1, y2;
+ if (center) {
+ x1 = -x/2;
+ x2 = +x/2;
+ y1 = -y/2;
+ y2 = +y/2;
+ } else {
+ x1 = y1 = 0;
+ x2 = x;
+ y2 = y;
+ }
+
+ p->is2d = true;
+ p->append_poly();
+ p->append_vertex(x1, y1);
+ p->append_vertex(x2, y1);
+ p->append_vertex(x2, y2);
+ p->append_vertex(x1, y2);
+ }
+
return p;
}
@@ -365,13 +403,15 @@ QString PrimitiveNode::dump(QString indent) const
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");
+ 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");
if (type == POLYHEDRON)
text.sprintf("polyhedron(points = %s, triangles = %s);\n", points.dump().toAscii().data(), triangles.dump().toAscii().data());
+ if (type == SQUARE)
+ text.sprintf("square(size = [%f, %f], center = %s);\n", x, y, center ? "true" : "false");
((AbstractNode*)this)->dump_cache = indent + QString("n%1: ").arg(idx) + text;
}
return dump_cache;
diff --git a/transform.cc b/transform.cc
index ca9fa20..a30cc9d 100644
--- a/transform.cc
+++ b/transform.cc
@@ -207,7 +207,11 @@ CGAL_Nef_polyhedron TransformNode::render_cgal_nef_polyhedron() const
}
if (N.dim == 2) {
+ CGAL_Aff_transformation2 t(
+ m[0], m[4], m[12],
+ m[1], m[5], m[13], m[15]);
// FIXME
+ // N.p2.transform(t);
}
if (N.dim == 3) {
CGAL_Aff_transformation t(
contact: Jan Huwald // Impressum