mirror of https://github.com/vitalif/openscad
Make CGALRenderer support all Geometries
parent
053fba737a
commit
0e5037dce3
|
@ -41,22 +41,16 @@
|
|||
|
||||
//#include "Preferences.h"
|
||||
|
||||
CGALRenderer::CGALRenderer(shared_ptr<const CGAL_Nef_polyhedron> root) : root(root)
|
||||
CGALRenderer::CGALRenderer(shared_ptr<const class Geometry> geom) : polyhedron(NULL)
|
||||
{
|
||||
if (this->root->isNull()) {
|
||||
this->polyhedron = NULL;
|
||||
this->polyset = NULL;
|
||||
if (shared_ptr<const PolySet> ps = dynamic_pointer_cast<const PolySet>(geom)) {
|
||||
this->polyset = ps;
|
||||
}
|
||||
else if (root->getDimension() == 2) {
|
||||
DxfData *dd = root->convertToDxfData();
|
||||
this->polyhedron = NULL;
|
||||
this->polyset = new PolySet();
|
||||
this->polyset->is2d = true;
|
||||
dxf_tesselate(this->polyset, *dd, 0, Vector2d(1,1), true, false, 0);
|
||||
delete dd;
|
||||
else if (shared_ptr<const Polygon2d> poly = dynamic_pointer_cast<const Polygon2d>(geom)) {
|
||||
this->polyset.reset(poly->tessellate());
|
||||
}
|
||||
else if (root->getDimension() == 3) {
|
||||
this->polyset = NULL;
|
||||
else if (shared_ptr<const CGAL_Nef_polyhedron> new_N = dynamic_pointer_cast<const CGAL_Nef_polyhedron>(geom)) {
|
||||
assert(new_N->getDimension() == 3);
|
||||
this->polyhedron = new Polyhedron();
|
||||
// FIXME: Make independent of Preferences
|
||||
// this->polyhedron->setColor(Polyhedron::CGAL_NEF3_MARKED_FACET_COLOR,
|
||||
|
@ -68,74 +62,54 @@ CGALRenderer::CGALRenderer(shared_ptr<const CGAL_Nef_polyhedron> root) : root(ro
|
|||
// Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).green(),
|
||||
// Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).blue());
|
||||
|
||||
CGAL::OGL::Nef3_Converter<CGAL_Nef_polyhedron3>::convert_to_OGLPolyhedron(*this->root->p3, this->polyhedron);
|
||||
CGAL::OGL::Nef3_Converter<CGAL_Nef_polyhedron3>::convert_to_OGLPolyhedron(*new_N->p3, this->polyhedron);
|
||||
this->polyhedron->init();
|
||||
}
|
||||
else {
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
CGALRenderer::~CGALRenderer()
|
||||
{
|
||||
delete this->polyset;
|
||||
delete this->polyhedron;
|
||||
}
|
||||
|
||||
void CGALRenderer::draw(bool showfaces, bool showedges) const
|
||||
{
|
||||
if (this->root->isNull()) return;
|
||||
if (this->root->getDimension() == 2) {
|
||||
// Draw 2D polygons
|
||||
glDisable(GL_LIGHTING);
|
||||
if (this->polyset) {
|
||||
if (this->polyset->getDimension() == 2) {
|
||||
// Draw 2D polygons
|
||||
glDisable(GL_LIGHTING);
|
||||
// FIXME: const QColor &col = Preferences::inst()->color(Preferences::CGAL_FACE_2D_COLOR);
|
||||
glColor3f(0.0f, 0.75f, 0.60f);
|
||||
|
||||
for (size_t i=0; i < this->polyset->polygons.size(); i++) {
|
||||
glBegin(GL_POLYGON);
|
||||
for (size_t j=0; j < this->polyset->polygons[i].size(); j++) {
|
||||
const Vector3d &p = this->polyset->polygons[i][j];
|
||||
glVertex3d(p[0], p[1], -0.1);
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
|
||||
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 = this->root->p2->explorer();
|
||||
|
||||
// Draw 2D edges
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDisable(GL_LIGHTING);
|
||||
glLineWidth(2);
|
||||
// FIXME: const QColor &col2 = Preferences::inst()->color(Preferences::CGAL_EDGE_2D_COLOR);
|
||||
glColor3f(1.0f, 0.0f, 0.0f);
|
||||
|
||||
// Extract the boundary, including inner boundaries of the polygons
|
||||
for (fci_t fit = E.faces_begin(), facesend = E.faces_end(); fit != facesend; ++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.1);
|
||||
glColor3f(0.0f, 0.75f, 0.60f);
|
||||
|
||||
for (size_t i=0; i < this->polyset->polygons.size(); i++) {
|
||||
glBegin(GL_POLYGON);
|
||||
for (size_t j=0; j < this->polyset->polygons[i].size(); j++) {
|
||||
const Vector3d &p = this->polyset->polygons[i][j];
|
||||
glVertex3d(p[0], p[1], -0.1);
|
||||
}
|
||||
}
|
||||
if (fset) {
|
||||
glVertex3d(fx, fy, -0.1);
|
||||
glEnd();
|
||||
}
|
||||
}
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
// Draw 2D edges
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
glLineWidth(2);
|
||||
// FIXME: const QColor &col2 = Preferences::inst()->color(Preferences::CGAL_EDGE_2D_COLOR);
|
||||
glColor3f(1.0f, 0.0f, 0.0f);
|
||||
this->polyset->render_edges(CSGMODE_NONE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
}
|
||||
else {
|
||||
// Draw 3D polygons
|
||||
const Color4f c(-1,-1,-1,-1);
|
||||
setColor(COLORMODE_MATERIAL, c.data(), NULL);
|
||||
this->polyset->render_surface(CSGMODE_NORMAL, Transform3d::Identity(), NULL);
|
||||
}
|
||||
}
|
||||
else if (this->root->getDimension() == 3) {
|
||||
else if (this->polyhedron) {
|
||||
if (showfaces) this->polyhedron->set_style(SNC_BOUNDARY);
|
||||
else this->polyhedron->set_style(SNC_SKELETON);
|
||||
|
||||
|
|
|
@ -6,14 +6,13 @@
|
|||
class CGALRenderer : public Renderer
|
||||
{
|
||||
public:
|
||||
CGALRenderer(shared_ptr<const class CGAL_Nef_polyhedron> root);
|
||||
CGALRenderer(shared_ptr<const class Geometry> geom);
|
||||
~CGALRenderer();
|
||||
void draw(bool showfaces, bool showedges) const;
|
||||
|
||||
public:
|
||||
const shared_ptr<const CGAL_Nef_polyhedron> root;
|
||||
class Polyhedron *polyhedron;
|
||||
class PolySet *polyset;
|
||||
shared_ptr<const class PolySet> polyset;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
CGAL_Nef_polyhedron::CGAL_Nef_polyhedron(CGAL_Nef_polyhedron2 *p)
|
||||
{
|
||||
assert(false);
|
||||
if (p) {
|
||||
dim = 2;
|
||||
p2.reset(p);
|
||||
|
@ -84,6 +85,7 @@ PolySet *CGAL_Nef_polyhedron::convertToPolyset() const
|
|||
if (this->isNull()) return new PolySet();
|
||||
PolySet *ps = NULL;
|
||||
if (this->dim == 2) {
|
||||
assert(false);
|
||||
DxfData *dd = this->convertToDxfData();
|
||||
Polygon2d *p2d = dd->toPolygon2d();
|
||||
ps = new PolySet(*p2d);
|
||||
|
|
|
@ -122,6 +122,7 @@ std::string CGAL_Nef_polyhedron::dump() const
|
|||
void CGAL_Nef_polyhedron::transform( const Transform3d &matrix )
|
||||
{
|
||||
if (!this->isNull()) {
|
||||
assert(this->dim == 3);
|
||||
if (this->dim == 2) {
|
||||
// Unfortunately CGAL provides no transform method for CGAL_Nef_polyhedron2
|
||||
// objects. So we convert in to our internal 2d data format, transform it,
|
||||
|
|
|
@ -14,16 +14,6 @@
|
|||
#include "CGAL_Nef_polyhedron.h"
|
||||
|
||||
void export_png(const Geometry *root_geom, Camera &cam, std::ostream &output)
|
||||
{
|
||||
const CGAL_Nef_polyhedron *N = dynamic_cast<const CGAL_Nef_polyhedron *>(root_geom);
|
||||
if (!N) {
|
||||
// FIXME: Support rendering non-Nef directly
|
||||
N = createNefPolyhedronFromGeometry(*root_geom);
|
||||
}
|
||||
export_png(N, cam, output);
|
||||
}
|
||||
|
||||
void export_png(const CGAL_Nef_polyhedron *root_N, Camera &cam, std::ostream &output)
|
||||
{
|
||||
OffscreenView *glview;
|
||||
try {
|
||||
|
@ -32,7 +22,7 @@ void export_png(const CGAL_Nef_polyhedron *root_N, Camera &cam, std::ostream &ou
|
|||
fprintf(stderr,"Can't create OpenGL OffscreenView. Code: %i.\n", error);
|
||||
return;
|
||||
}
|
||||
shared_ptr<const CGAL_Nef_polyhedron> ptr(root_N);
|
||||
shared_ptr<const Geometry> ptr(root_geom);
|
||||
CGALRenderer cgalRenderer(ptr);
|
||||
|
||||
BoundingBox bbox;
|
||||
|
|
|
@ -1354,19 +1354,10 @@ void MainWindow::actionRenderDone(shared_ptr<const Geometry> root_geom)
|
|||
PRINT("Rendering finished.");
|
||||
|
||||
this->root_geom = root_geom;
|
||||
if (this->root_geom) {
|
||||
// FIXME: Support rendering PolySets and Polygon2d roots
|
||||
|
||||
shared_ptr<const CGAL_Nef_polyhedron> new_N = dynamic_pointer_cast<const CGAL_Nef_polyhedron>(this->root_geom);
|
||||
if (!new_N) {
|
||||
new_N.reset(createNefPolyhedronFromGeometry(*this->root_geom));
|
||||
}
|
||||
|
||||
this->cgalRenderer = new CGALRenderer(new_N);
|
||||
// Go to CGAL view mode
|
||||
if (viewActionWireframe->isChecked()) viewModeWireframe();
|
||||
else viewModeSurface();
|
||||
}
|
||||
this->cgalRenderer = new CGALRenderer(root_geom);
|
||||
// Go to CGAL view mode
|
||||
if (viewActionWireframe->isChecked()) viewModeWireframe();
|
||||
else viewModeSurface();
|
||||
}
|
||||
else {
|
||||
PRINT("WARNING: No top level geometry to render");
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
#include "polyset.h"
|
||||
#include "linalg.h"
|
||||
#include "printutils.h"
|
||||
#include <Eigen/LU>
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
|
@ -270,30 +271,47 @@ void PolySet::render_surface(Renderer::csgmode_e csgmode, const Transform3d &m,
|
|||
}
|
||||
}
|
||||
|
||||
// This is apparently used only in throwntogether mode
|
||||
/*! This is used in throwntogether and CGAL mode
|
||||
|
||||
csgmode is set to CSGMODE_NONE in CGAL mode. In this mode a pure 2D rendering is performed.
|
||||
|
||||
For some reason, this is not used to render edges in Preview mode
|
||||
*/
|
||||
void PolySet::render_edges(Renderer::csgmode_e csgmode) const
|
||||
{
|
||||
glDisable(GL_LIGHTING);
|
||||
if (this->is2d) {
|
||||
// Render 2D objects 1mm thick, but differences slightly larger
|
||||
double zbase = 1 + (csgmode & CSGMODE_DIFFERENCE_FLAG) * 0.1;
|
||||
|
||||
BOOST_FOREACH(const Outline2d &o, polygon.outlines()) {
|
||||
// Render top+bottom outlines
|
||||
for (double z = -zbase/2; z < zbase; z += zbase) {
|
||||
if (csgmode == Renderer::CSGMODE_NONE) {
|
||||
// Render only outlines
|
||||
BOOST_FOREACH(const Outline2d &o, polygon.outlines()) {
|
||||
glBegin(GL_LINE_LOOP);
|
||||
BOOST_FOREACH(const Vector2d &v, o) {
|
||||
glVertex3d(v[0], v[1], z);
|
||||
glVertex3d(v[0], v[1], -0.1);
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
// Render sides
|
||||
glBegin(GL_LINES);
|
||||
BOOST_FOREACH(const Vector2d &v, o) {
|
||||
glVertex3d(v[0], v[1], -zbase/2);
|
||||
glVertex3d(v[0], v[1], +zbase/2);
|
||||
}
|
||||
else {
|
||||
// Render 2D objects 1mm thick, but differences slightly larger
|
||||
double zbase = 1 + (csgmode & CSGMODE_DIFFERENCE_FLAG) * 0.1;
|
||||
|
||||
BOOST_FOREACH(const Outline2d &o, polygon.outlines()) {
|
||||
// Render top+bottom outlines
|
||||
for (double z = -zbase/2; z < zbase; z += zbase) {
|
||||
glBegin(GL_LINE_LOOP);
|
||||
BOOST_FOREACH(const Vector2d &v, o) {
|
||||
glVertex3d(v[0], v[1], z);
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
// Render sides
|
||||
glBegin(GL_LINES);
|
||||
BOOST_FOREACH(const Vector2d &v, o) {
|
||||
glVertex3d(v[0], v[1], -zbase/2);
|
||||
glVertex3d(v[0], v[1], +zbase/2);
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < polygons.size(); i++) {
|
||||
|
|
Loading…
Reference in New Issue