many changes :P
This commit is contained in:
25
mapview/3D/MV3DElement.h
Normal file
25
mapview/3D/MV3DElement.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef MV3DELEMENT_H
|
||||
#define MV3DELEMENT_H
|
||||
|
||||
#include "MapView3D.h"
|
||||
|
||||
/**
|
||||
* represents one drawable
|
||||
* element shown within the MapView3D
|
||||
*/
|
||||
class MV3DElement {
|
||||
|
||||
public:
|
||||
|
||||
/** dtor */
|
||||
virtual ~MV3DElement() {;}
|
||||
|
||||
public:
|
||||
|
||||
/** repaint me */
|
||||
virtual void paintGL() = 0;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // MV3DELEMENT_H
|
||||
34
mapview/3D/MV3DElementAccessPoint.h
Normal file
34
mapview/3D/MV3DElementAccessPoint.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef MV3DELEMENTACCESSPOINT_H
|
||||
#define MV3DELEMENTACCESSPOINT_H
|
||||
|
||||
#include <Indoor/floorplan/v2/Floorplan.h>
|
||||
|
||||
#include "misc/Cube.h"
|
||||
#include "MV3DElement.h"
|
||||
|
||||
class MV3DElementAccessPoint : public MV3DElement {
|
||||
|
||||
Floorplan::Floor* f;
|
||||
Floorplan::AccessPoint* ap;
|
||||
|
||||
public:
|
||||
|
||||
/** ctor */
|
||||
MV3DElementAccessPoint(Floorplan::Floor* f, Floorplan::AccessPoint* ap) : f(f), ap(ap) {
|
||||
;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
/** repaint me */
|
||||
void paintGL() override {
|
||||
|
||||
Cube cube(ap->getPos(f), 0.5);
|
||||
cube.paintGL();
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // MV3DELEMENTACCESSPOINT_H
|
||||
4
mapview/3D/MV3DElementFloorObstaclePillar.h
Normal file
4
mapview/3D/MV3DElementFloorObstaclePillar.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#ifndef MV3DELEMENTFLOOROBSTACLEPILLAR_H
|
||||
#define MV3DELEMENTFLOOROBSTACLEPILLAR_H
|
||||
|
||||
#endif // MV3DELEMENTFLOOROBSTACLEPILLAR_H
|
||||
86
mapview/3D/MV3DElementFloorObstacleWall.h
Normal file
86
mapview/3D/MV3DElementFloorObstacleWall.h
Normal file
@@ -0,0 +1,86 @@
|
||||
#ifndef MV3DELEMENTFLOOROBSTACLEWALL_H
|
||||
#define MV3DELEMENTFLOOROBSTACLEWALL_H
|
||||
|
||||
#include <Indoor/floorplan/v2/Floorplan.h>
|
||||
|
||||
#include "MV3DElement.h"
|
||||
|
||||
class MV3DElementFloorObstacleWall : public MV3DElement {
|
||||
|
||||
Floorplan::Floor* f;
|
||||
Floorplan::FloorObstacleLine* fo;
|
||||
|
||||
public:
|
||||
|
||||
/** ctor */
|
||||
MV3DElementFloorObstacleWall(Floorplan::Floor* f, Floorplan::FloorObstacleLine* fo) : f(f), fo(fo) {
|
||||
;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
Point3 cross(Point3 u, Point3 v) {
|
||||
float x = u.y*v.z - u.z*v.y;
|
||||
float y = u.z*v.x - u.x*v.z;
|
||||
float z = u.x*v.y - u.y*v.x;
|
||||
return Point3(x,y,z);
|
||||
}
|
||||
|
||||
/** repaint me */
|
||||
void paintGL() override {
|
||||
|
||||
float y1 = f->atHeight;
|
||||
float y2 = y1+f->height;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Point3 p1 = Point3(fo->from.x, y1, fo->from.y);
|
||||
Point3 p2 = Point3(fo->to.x, y1, fo->to.y);
|
||||
Point3 p3 = Point3(fo->to.x, y2, fo->to.y);
|
||||
Point3 p4 = Point3(fo->from.x, y2, fo->from.y);
|
||||
|
||||
Point3 v1 = p2-p1;
|
||||
Point3 v2 = p3-p1;
|
||||
Point3 n = cross(v1, v2);
|
||||
n/=n.length();
|
||||
|
||||
// align normals to virtual viewport
|
||||
Point3 view(99,99,99);
|
||||
if ((view-n).length() > (view+n).length()) {n = -n;}
|
||||
|
||||
if (fo->type == Floorplan::ObstacleType::WALL) {
|
||||
|
||||
// fill the wall
|
||||
glColor3f(0.75, 0.75, 0.75);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glBegin(GL_QUADS);
|
||||
glNormal3f(n.x, n.y, n.z);
|
||||
glVertex3f(p1.x, p1.y, p1.z);
|
||||
glVertex3f(p2.x, p2.y, p2.z);
|
||||
glVertex3f(p3.x, p3.y, p3.z);
|
||||
glVertex3f(p4.x, p4.y, p4.z);
|
||||
glEnd();
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
}
|
||||
|
||||
glColor3f(0,0,0);
|
||||
|
||||
// glDisable(GL_LIGHTING);
|
||||
// glBegin(GL_LINE_STRIP);
|
||||
// glVertex3f(p1.x, p1.y, p1.z);
|
||||
// glVertex3f(p2.x, p2.y, p2.z);
|
||||
// glVertex3f(p3.x, p3.y, p3.z);
|
||||
// glVertex3f(p4.x, p4.y, p4.z);
|
||||
// glVertex3f(p1.x, p1.y, p1.z);
|
||||
// glEnd();
|
||||
// glEnable(GL_LIGHTING);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // MV3DELEMENTFLOOROBSTACLEWALL_H
|
||||
71
mapview/3D/MV3DElementFloorOutline.h
Normal file
71
mapview/3D/MV3DElementFloorOutline.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#ifndef MV3DELEMENTFLOOROUTLINE_H
|
||||
#define MV3DELEMENTFLOOROUTLINE_H
|
||||
|
||||
#include <Indoor/floorplan/v2/Floorplan.h>
|
||||
|
||||
#include "misc/Cube.h"
|
||||
#include "MV3DElement.h"
|
||||
|
||||
#include "../../lib/gpc/gpc.h"
|
||||
#include "misc/Polygon.h"
|
||||
|
||||
|
||||
class MV3DElementFloorOutline : public MV3DElement {
|
||||
|
||||
Floorplan::Floor* f;
|
||||
Floorplan::FloorOutline* out;
|
||||
|
||||
public:
|
||||
|
||||
/** ctor */
|
||||
MV3DElementFloorOutline(Floorplan::Floor* f, Floorplan::FloorOutline* out) : f(f), out(out) {
|
||||
;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/** repaint me */
|
||||
void paintGL() override {
|
||||
|
||||
std::vector<gpc_polygon> add;
|
||||
std::vector<gpc_polygon> rem;
|
||||
|
||||
std::vector<Floorplan::FloorOutlinePolygon*> polys = *out;
|
||||
|
||||
Polygon pol;
|
||||
|
||||
for (Floorplan::FloorOutlinePolygon* poly : polys) {
|
||||
switch (poly->method) {
|
||||
case Floorplan::OutlineMethod::ADD: pol.add(poly->poly); break;
|
||||
case Floorplan::OutlineMethod::REMOVE: pol.remove(poly->poly); break;
|
||||
default: throw 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<Point3>> trias = pol.get(f->atHeight);
|
||||
|
||||
glColor3f(0.2, 0.2, 0.2);
|
||||
glDisable(GL_CULL_FACE);
|
||||
for (const std::vector<Point3>& tria : trias) {
|
||||
glBegin(GL_TRIANGLE_STRIP);
|
||||
for (const Point3& p3 : tria) {
|
||||
glVertex3f(p3.x, p3.z, p3.y);
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // MV3DELEMENTFLOOROUTLINE_H
|
||||
53
mapview/3D/MV3DElementFloorOutlinePolygon.h
Normal file
53
mapview/3D/MV3DElementFloorOutlinePolygon.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef MV3DELEMENTFLOOROUTLINEPOLYGON_H
|
||||
#define MV3DELEMENTFLOOROUTLINEPOLYGON_H
|
||||
|
||||
#include <Indoor/floorplan/v2/Floorplan.h>
|
||||
|
||||
#include "misc/Cube.h"
|
||||
#include "MV3DElement.h"
|
||||
|
||||
class MV3DElementFloorOutlinePolygon : public MV3DElement {
|
||||
|
||||
Floorplan::Floor* f;
|
||||
Floorplan::FloorOutlinePolygon* poly;
|
||||
|
||||
public:
|
||||
|
||||
/** ctor */
|
||||
MV3DElementFloorOutlinePolygon(Floorplan::Floor* f, Floorplan::FloorOutlinePolygon* poly) : f(f), poly(poly) {
|
||||
;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
/** repaint me */
|
||||
void paintGL() override {
|
||||
|
||||
glDisable(GL_CULL_FACE);
|
||||
|
||||
switch (poly->method) {
|
||||
case Floorplan::OutlineMethod::ADD:
|
||||
glColor3f(1,1,1);
|
||||
break;
|
||||
case Floorplan::OutlineMethod::REMOVE:
|
||||
glColor3f(0.2, 0.2, 0.2);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
glBegin(GL_POLYGON);
|
||||
glNormal3f(0,1,0);
|
||||
for (Point2 p2 : poly->poly.points) {
|
||||
Point3 p3(p2.x, p2.y, f->atHeight);
|
||||
glVertex3f(p3.x, p3.z, p3.y);
|
||||
}
|
||||
glEnd();
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // MV3DELEMENTFLOOROUTLINEPOLYGON_H
|
||||
194
mapview/3D/MapView3D.cpp
Normal file
194
mapview/3D/MapView3D.cpp
Normal file
@@ -0,0 +1,194 @@
|
||||
#include "MapView3D.h"
|
||||
|
||||
|
||||
#include "../model/MapModelElement.h"
|
||||
#include "../model/MapModel.h"
|
||||
|
||||
#include "../3DGrid/GridModel.h"
|
||||
#include "../3DGrid/GridRenderer.h"
|
||||
|
||||
MapView3D::MapView3D(QWidget *parent) : QGLWidget(parent) {
|
||||
|
||||
rot.x = 45;
|
||||
rot.y = 0;
|
||||
rot.z = 45;
|
||||
|
||||
center.x = 0;
|
||||
center.y = 0;
|
||||
center.z = 0;
|
||||
|
||||
scale.x = 0.05f;
|
||||
scale.y = 0.05f;
|
||||
scale.z = 0.05f;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void MapView3D::initializeGL() {
|
||||
|
||||
//setFormat(QGLFormat(QGL::SampleBuffers));
|
||||
|
||||
QGLWidget::initializeGL();
|
||||
|
||||
// culling, lighting, depth-test, ...
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_CULL_FACE);
|
||||
glShadeModel(GL_SMOOTH);
|
||||
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
// glEnable(GL_LINE_SMOOTH);
|
||||
|
||||
glEnable(GL_LIGHTING);
|
||||
glEnable(GL_LIGHT0);
|
||||
glEnable(GL_LIGHT1);
|
||||
|
||||
GLfloat light0_position [] = {+5, 5, +5, 1};
|
||||
GLfloat light1_position [] = {-5, 5, -5, 1};
|
||||
|
||||
glLightfv ( GL_LIGHT0, GL_POSITION, light0_position );
|
||||
glLightfv ( GL_LIGHT1, GL_POSITION, light1_position );
|
||||
|
||||
GLfloat light_diffuse []={ 0.7, 0.7, 0.7, 1.0 };
|
||||
glLightfv ( GL_LIGHT0, GL_DIFFUSE, light_diffuse );
|
||||
glLightfv ( GL_LIGHT1, GL_DIFFUSE, light_diffuse );
|
||||
|
||||
|
||||
// otherwise scaling the scene kills lighting normals!
|
||||
glEnable(GL_NORMALIZE);
|
||||
|
||||
// allow using glColor3(r,g,b)
|
||||
glEnable(GL_COLOR_MATERIAL);
|
||||
|
||||
// GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
|
||||
// GLfloat mat_shininess[] = { 50.0 };
|
||||
// glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
|
||||
// glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
|
||||
|
||||
// background color
|
||||
qglClearColor(Qt::white);
|
||||
|
||||
}
|
||||
|
||||
void MapView3D::paintGL() {
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glLoadIdentity();
|
||||
|
||||
|
||||
|
||||
// 3) scale
|
||||
glScalef(scale.x, scale.y, scale.z);
|
||||
|
||||
// 2) rotate around center
|
||||
glRotatef(rot.x, 1.0, 0.0, 0.0);
|
||||
glRotatef(rot.z, 0.0, 1.0, 0.0);
|
||||
glRotatef(rot.y, 0.0, 0.0, 1.0);
|
||||
|
||||
// 1) post translate (mouse moving)
|
||||
glTranslatef(center.x, center.z, center.y);
|
||||
|
||||
// 0) swap the y axis
|
||||
//glScalef(+1, -1, +1);
|
||||
|
||||
|
||||
|
||||
// // 1) translate into center
|
||||
// glTranslatef(tra.x, tra.y, tra.z);
|
||||
|
||||
draw();
|
||||
|
||||
|
||||
}
|
||||
|
||||
void MapView3D::resizeGL(int width, int height) {
|
||||
|
||||
//int side = qMin(width, height);
|
||||
//glViewport((width - side) / 2, (height - side) / 2, side, side);
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
#ifdef QT_OPENGL_ES_1
|
||||
//glOrthof(-2, +2, -2, +2, 1.0, 15.0);
|
||||
#else
|
||||
//glOrtho(-2, +2, -2, +2, 1.0, 25.0);
|
||||
//glFrustum(-1,1, -1,1, 0.1,20);
|
||||
viewport.size.x = 2.0f;
|
||||
viewport.size.y = 2.0f * height / width;
|
||||
const float w = viewport.size.x;
|
||||
const float h = viewport.size.y;
|
||||
glOrtho(-w, +w, -h, +h, -20, +20);
|
||||
//glFrustum(+w, -w, -h, +h, -20, +20);
|
||||
#endif
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
}
|
||||
|
||||
|
||||
void MapView3D::mousePressEvent(QMouseEvent* e) {
|
||||
mouse.btn = e->button();
|
||||
mouse.x = e->x();
|
||||
mouse.y = e->y();
|
||||
update();
|
||||
}
|
||||
|
||||
void MapView3D::mouseMoveEvent(QMouseEvent* e) {
|
||||
float dx = mouse.x - e->x();
|
||||
float dy = mouse.y - e->y();
|
||||
if (mouse.btn == 1) {
|
||||
rot.z -= dx/2.0f; // upward
|
||||
rot.x -= dy/2.0f;
|
||||
} else if (mouse.btn == 4) {
|
||||
Point3 vec(-dx / width() * 2 * viewport.size.x, 0, dy / height() * 2 * viewport.size.y);
|
||||
vec = vec.rot(rot.x/180*M_PI, rot.y/180*M_PI, rot.z/180*M_PI);
|
||||
vec /= scale;
|
||||
center += vec;
|
||||
}
|
||||
mouse.x = e->x();
|
||||
mouse.y = e->y();
|
||||
update();
|
||||
}
|
||||
|
||||
void MapView3D::mouseReleaseEvent(QMouseEvent* e) {
|
||||
update();
|
||||
}
|
||||
|
||||
void MapView3D::wheelEvent(QWheelEvent* e) {
|
||||
float f = e->delta() / 120.0f;
|
||||
scale *= (f > 0) ? (2) : (0.5);
|
||||
update();
|
||||
}
|
||||
|
||||
|
||||
void MapView3D::showFloorplan() {
|
||||
if (gridModel) {delete gridModel; gridModel = nullptr;}
|
||||
update();
|
||||
}
|
||||
|
||||
void MapView3D::showGrid() {
|
||||
if (gridModel) {delete gridModel; gridModel = nullptr;}
|
||||
GridModel* gm = new GridModel();
|
||||
Floorplan::IndoorMap* im = getModel()->getMap();
|
||||
gm->rebuild(im);
|
||||
this->gridModel = gm;
|
||||
update();
|
||||
}
|
||||
|
||||
void MapView3D::draw() {
|
||||
|
||||
if (gridModel) {
|
||||
|
||||
// show grid
|
||||
GridRenderer renderer(gridModel->getGrid());
|
||||
renderer.paintGL();
|
||||
|
||||
} else {
|
||||
|
||||
// show floorplan
|
||||
for (MapModelElement* el : getModel()->getVisibleElements()) {
|
||||
if (el->getMV3D()) {el->getMV3D()->paintGL();}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
72
mapview/3D/MapView3D.h
Normal file
72
mapview/3D/MapView3D.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef MAPVIEW3D_H
|
||||
#define MAPVIEW3D_H
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtOpenGL>
|
||||
|
||||
#include <Indoor/geo/Point3.h>
|
||||
|
||||
class MapModel;
|
||||
class GridModel;
|
||||
|
||||
class MapView3D : public QGLWidget {
|
||||
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
MapView3D(QWidget* parent = 0);
|
||||
|
||||
/** set the underlying data-model */
|
||||
void setModel(MapModel* mdl) {
|
||||
this->model = mdl;
|
||||
update();
|
||||
}
|
||||
|
||||
/** get the underlying data-model */
|
||||
MapModel* getModel() {return model;}
|
||||
|
||||
/** show 3D rendered floorplan */
|
||||
void showFloorplan();
|
||||
|
||||
/** show 3D rendered grid derived from the floorplan */
|
||||
void showGrid();
|
||||
|
||||
private:
|
||||
|
||||
/** the underlying data-model */
|
||||
MapModel* model = nullptr;
|
||||
GridModel* gridModel = nullptr;
|
||||
|
||||
Point3 rot;
|
||||
Point3 center;
|
||||
Point3 scale;
|
||||
|
||||
struct Mouse {
|
||||
int btn;
|
||||
float x;
|
||||
float y;
|
||||
} mouse;
|
||||
|
||||
struct Viewport {
|
||||
Point2 size;
|
||||
} viewport;
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
void initializeGL();
|
||||
void paintGL();
|
||||
void resizeGL(int width, int height);
|
||||
|
||||
void mousePressEvent(QMouseEvent* e);
|
||||
void mouseMoveEvent(QMouseEvent* e);
|
||||
void mouseReleaseEvent(QMouseEvent* e);
|
||||
void wheelEvent(QWheelEvent* e);
|
||||
|
||||
private:
|
||||
void draw();
|
||||
|
||||
};
|
||||
|
||||
#endif // MAPVIEW3D_H
|
||||
82
mapview/3D/misc/Cube.h
Normal file
82
mapview/3D/misc/Cube.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#ifndef CUBE_H
|
||||
#define CUBE_H
|
||||
|
||||
#include <Indoor/geo/Point3.h>
|
||||
#include <QtOpenGL>
|
||||
|
||||
class Cube {
|
||||
|
||||
private:
|
||||
|
||||
Point3 pos;
|
||||
float size;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
Cube(Point3 pos, float size) : pos(pos), size(size) {
|
||||
|
||||
}
|
||||
|
||||
void paintGL() {
|
||||
|
||||
float s = size;
|
||||
|
||||
glPushMatrix();
|
||||
glTranslatef(pos.x, pos.z, pos.y);
|
||||
|
||||
glColor3f(0,0,1);
|
||||
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
// bottom
|
||||
glNormal3f(0,-1,0);
|
||||
glVertex3f(+s, -s, -s);
|
||||
glVertex3f(+s, -s, +s);
|
||||
glVertex3f(-s, -s, +s);
|
||||
glVertex3f(-s, -s, -s);
|
||||
|
||||
// top
|
||||
glNormal3f(0,+1,0);
|
||||
glVertex3f(-s, +s, -s);
|
||||
glVertex3f(-s, +s, +s);
|
||||
glVertex3f(+s, +s, +s);
|
||||
glVertex3f(+s, +s, -s);
|
||||
|
||||
// left
|
||||
glNormal3f(-1,0,0);
|
||||
glVertex3f(-s, -s, -s);
|
||||
glVertex3f(-s, -s, +s);
|
||||
glVertex3f(-s, +s, +s);
|
||||
glVertex3f(-s, +s, -s);
|
||||
|
||||
// right
|
||||
glNormal3f(+1,0,0);
|
||||
glVertex3f(+s, +s, -s);
|
||||
glVertex3f(+s, +s, +s);
|
||||
glVertex3f(+s, -s, +s);
|
||||
glVertex3f(+s, -s, -s);
|
||||
|
||||
// front
|
||||
glNormal3f(0,0,+1);
|
||||
glVertex3f(+s, +s, +s);
|
||||
glVertex3f(-s, +s, +s);
|
||||
glVertex3f(-s, -s, +s);
|
||||
glVertex3f(+s, -s, +s);
|
||||
|
||||
// rear
|
||||
glNormal3f(0,0,-1);
|
||||
glVertex3f(+s, -s, -s);
|
||||
glVertex3f(-s, -s, -s);
|
||||
glVertex3f(-s, +s, -s);
|
||||
glVertex3f(+s, +s, -s);
|
||||
|
||||
glEnd();
|
||||
|
||||
glPopMatrix();
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // CUBE_H
|
||||
109
mapview/3D/misc/Polygon.h
Normal file
109
mapview/3D/misc/Polygon.h
Normal file
@@ -0,0 +1,109 @@
|
||||
#ifndef POLYGON_H
|
||||
#define POLYGON_H
|
||||
|
||||
#include <Indoor/floorplan/v2/Floorplan.h>
|
||||
#include "../../../lib/gpc/gpc.h"
|
||||
|
||||
class Polygon {
|
||||
|
||||
struct GPCPolygon : gpc_polygon {
|
||||
GPCPolygon() {
|
||||
// contour = (gpc_vertex_list*) calloc(0, 1024);
|
||||
// contour->num_vertices = 0;
|
||||
// contour->vertex = (gpc_vertex*) calloc(0, 1024);
|
||||
// hole = (int*) calloc(0, 1024);
|
||||
num_contours = 0;
|
||||
contour = nullptr;
|
||||
hole = nullptr;
|
||||
}
|
||||
~GPCPolygon() {
|
||||
if (contour) {
|
||||
gpc_free_polygon(this);
|
||||
//free(contour->vertex); contour->vertex = nullptr;
|
||||
}
|
||||
free(contour); contour = nullptr;
|
||||
free(hole); hole = nullptr;
|
||||
|
||||
}
|
||||
GPCPolygon& operator = (const GPCPolygon& o) = delete;
|
||||
GPCPolygon& operator = (GPCPolygon& o) {
|
||||
this->contour = o.contour;
|
||||
this->hole = o.hole;
|
||||
this->num_contours = o.num_contours;
|
||||
o.contour = nullptr;
|
||||
o.hole = nullptr;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
GPCPolygon state;
|
||||
|
||||
public:
|
||||
|
||||
void add(const Floorplan::Polygon2& poly) {
|
||||
GPCPolygon cur = toGPC(poly);
|
||||
//GPCPolygon out;
|
||||
gpc_polygon_clip(GPC_UNION, &state, &cur, &state);
|
||||
//state = out;
|
||||
}
|
||||
|
||||
void remove(const Floorplan::Polygon2& poly) {
|
||||
GPCPolygon cur = toGPC(poly);
|
||||
//GPCPolygon out;
|
||||
gpc_polygon_clip(GPC_DIFF, &state, &cur, &state);
|
||||
//state = out;
|
||||
}
|
||||
|
||||
std::vector<std::vector<Point3>> get(float z) {
|
||||
|
||||
gpc_tristrip res;
|
||||
res.num_strips = 0;
|
||||
res.strip = nullptr;
|
||||
|
||||
//res.strip = (gpc_vertex_list*) malloc(1024);
|
||||
gpc_polygon_to_tristrip(&state, &res);
|
||||
|
||||
std::vector<std::vector<Point3>> trias;
|
||||
|
||||
for (int i = 0; i < res.num_strips; ++i) {
|
||||
gpc_vertex_list lst = res.strip[i];
|
||||
std::vector<Point3> tria;
|
||||
for (int j = 0; j < lst.num_vertices; ++j) {
|
||||
gpc_vertex& vert = lst.vertex[j];
|
||||
Point3 p3(vert.x, vert.y, z);
|
||||
tria.push_back(p3);
|
||||
}
|
||||
trias.push_back(tria);
|
||||
}
|
||||
|
||||
gpc_free_tristrip(&res);
|
||||
|
||||
return std::move(trias);
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
GPCPolygon toGPC(Floorplan::Polygon2 poly) {
|
||||
|
||||
std::vector<gpc_vertex> verts;
|
||||
for (Point2 p2 : poly.points) {
|
||||
gpc_vertex vert; vert.x = p2.x; vert.y = p2.y;
|
||||
verts.push_back(vert);
|
||||
}
|
||||
|
||||
GPCPolygon gpol;
|
||||
gpc_vertex_list list;
|
||||
list.num_vertices = verts.size();
|
||||
list.vertex = verts.data();
|
||||
gpc_add_contour(&gpol, &list, 0);
|
||||
|
||||
return gpol;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // POLYGON_H
|
||||
Reference in New Issue
Block a user