278 lines
6.0 KiB
C++
278 lines
6.0 KiB
C++
#ifndef GPCPOLYGON2_H
|
|
#define GPCPOLYGON2_H
|
|
|
|
#include "../lib/gpc/gpc.cpp.h"
|
|
#include "Polygon2.h"
|
|
#include "Triangle3.h"
|
|
|
|
class TriangleStrip {
|
|
|
|
private:
|
|
std::vector<Point3> pts;
|
|
|
|
public:
|
|
|
|
void add(const Point3 p) {
|
|
pts.push_back(p);
|
|
}
|
|
|
|
void set(const std::vector<Point3>& pts) {
|
|
this->pts = pts;
|
|
}
|
|
|
|
void toTriangles(std::vector<Triangle3>& trias) const {
|
|
|
|
// https://en.wikipedia.org/wiki/Triangle_strip
|
|
// GL_TRIANGLE_STRIP
|
|
// Draws a series of triangles (three-sided polygons) using vertices v0, v1, v2, then v2, v1, v3 (note the order), then v2, v3, v4, and so on. The ordering is to ensure that the triangles are all drawn with the same orientation so that the strip can correctly form part of a surface.
|
|
// For odd n, vertices n, n+1, and n+2 define triangle n. For even n, vertices n+1, n, and n+2 define triangle n. N-2 triangles are drawn.
|
|
|
|
|
|
|
|
for (size_t j = 2; j < pts.size(); ++j) {
|
|
|
|
|
|
if (j % 2 == 0) {
|
|
Triangle3 tria(
|
|
pts[j-2],
|
|
pts[j-1],
|
|
pts[j]
|
|
);
|
|
trias.push_back(tria);
|
|
} else {
|
|
Triangle3 tria(
|
|
pts[j-1],
|
|
pts[j-2],
|
|
pts[j]
|
|
);
|
|
trias.push_back(tria);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
std::vector<Triangle3> toTriangles() const {
|
|
std::vector<Triangle3> trias;
|
|
toTriangles(trias);
|
|
return trias;
|
|
}
|
|
|
|
};
|
|
|
|
class GPCPolygon2 {
|
|
|
|
struct GPCPolygon : gpc_polygon {
|
|
GPCPolygon() {
|
|
num_contours = 0;
|
|
contour = nullptr;
|
|
hole = nullptr;
|
|
}
|
|
|
|
// no copy or move
|
|
GPCPolygon(const GPCPolygon& o) = delete;
|
|
GPCPolygon(GPCPolygon&& o) = delete;
|
|
|
|
~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) = delete;
|
|
};
|
|
|
|
private:
|
|
|
|
GPCPolygon state;
|
|
float z;
|
|
|
|
public:
|
|
|
|
GPCPolygon2() : z(0) {
|
|
;
|
|
}
|
|
|
|
GPCPolygon2(float z) : z(z) {
|
|
;
|
|
}
|
|
|
|
void add(const Polygon2& poly) {
|
|
GPCPolygon cur;
|
|
toGPC(poly, cur);
|
|
gpc_polygon_clip(GPC_UNION, &state, &cur, &state);
|
|
}
|
|
void add(const Floorplan::Polygon2& poly) {
|
|
GPCPolygon cur;
|
|
toGPC(poly, cur);
|
|
gpc_polygon_clip(GPC_UNION, &state, &cur, &state);
|
|
}
|
|
|
|
void remove(const Floorplan::Polygon2& poly) {
|
|
GPCPolygon cur;
|
|
toGPC(poly, cur);
|
|
gpc_polygon_clip(GPC_DIFF, &state, &cur, &state);
|
|
}
|
|
void remove(const Polygon2& poly) {
|
|
GPCPolygon cur;
|
|
toGPC(poly, cur);
|
|
gpc_polygon_clip(GPC_DIFF, &state, &cur, &state);
|
|
}
|
|
|
|
std::vector<std::vector<Point3>> get() {
|
|
|
|
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];
|
|
for (int j = 2; j < lst.num_vertices; ++j) {
|
|
std::vector<Point3> tria;
|
|
gpc_vertex& v1 = lst.vertex[j - 2];
|
|
gpc_vertex& v2 = lst.vertex[j - 1];
|
|
gpc_vertex& v3 = lst.vertex[j];
|
|
tria.push_back(Point3(v1.x, v1.y, z));
|
|
tria.push_back(Point3(v2.x, v2.y, z));
|
|
tria.push_back(Point3(v3.x, v3.y, z));
|
|
trias.push_back(tria);
|
|
}
|
|
|
|
}
|
|
|
|
gpc_free_tristrip(&res);
|
|
|
|
return std::move(trias);
|
|
|
|
}
|
|
|
|
std::vector<Triangle3> getTriangles() {
|
|
|
|
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<Triangle3> trias;
|
|
|
|
for (int i = 0; i < res.num_strips; ++i) {
|
|
gpc_vertex_list lst = res.strip[i];
|
|
|
|
TriangleStrip strip;
|
|
for (int j = 0; j < lst.num_vertices; ++j) {
|
|
gpc_vertex& v = lst.vertex[j];
|
|
strip.add(Point3(v.x, v.y, z));
|
|
}
|
|
strip.toTriangles(trias);
|
|
|
|
// for (int j = 2; j < lst.num_vertices; ++j) {
|
|
|
|
// gpc_vertex& v1 = lst.vertex[j - 2];
|
|
// gpc_vertex& v2 = lst.vertex[j - 1];
|
|
// gpc_vertex& v3 = lst.vertex[j];
|
|
|
|
// // https://en.wikipedia.org/wiki/Triangle_strip
|
|
// // GL_TRIANGLE_STRIP
|
|
// // Draws a series of triangles (three-sided polygons) using vertices v0, v1, v2, then v2, v1, v3 (note the order), then v2, v3, v4, and so on. The ordering is to ensure that the triangles are all drawn with the same orientation so that the strip can correctly form part of a surface.
|
|
// // For odd n, vertices n, n+1, and n+2 define triangle n. For even n, vertices n+1, n, and n+2 define triangle n. N-2 triangles are drawn.
|
|
|
|
// if (j % 2 == 0) {
|
|
// Triangle3 tria(
|
|
// Point3(v1.x, v1.y, z),
|
|
// Point3(v2.x, v2.y, z),
|
|
// Point3(v3.x, v3.y, z)
|
|
// );
|
|
// trias.push_back(tria);
|
|
// } else {
|
|
// Triangle3 tria(
|
|
// Point3(v2.x, v2.y, z),
|
|
// Point3(v1.x, v1.y, z),
|
|
// Point3(v3.x, v3.y, z)
|
|
// );
|
|
// trias.push_back(tria);
|
|
// }
|
|
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
gpc_free_tristrip(&res);
|
|
|
|
return std::move(trias);
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
void toGPC(const Floorplan::Polygon2& poly, GPCPolygon& result) {
|
|
|
|
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);
|
|
}
|
|
|
|
gpc_vertex_list list;
|
|
list.num_vertices = verts.size();
|
|
list.vertex = verts.data();
|
|
gpc_add_contour(&result, &list, 0);
|
|
|
|
}
|
|
|
|
void toGPC(const Polygon2& poly, GPCPolygon& result) {
|
|
|
|
std::vector<gpc_vertex> verts;
|
|
for (Point2 p2 : poly) {
|
|
gpc_vertex vert; vert.x = p2.x; vert.y = p2.y;
|
|
verts.push_back(vert);
|
|
}
|
|
|
|
gpc_vertex_list list;
|
|
list.num_vertices = verts.size();
|
|
list.vertex = verts.data();
|
|
gpc_add_contour(&result, &list, 0);
|
|
|
|
}
|
|
|
|
};
|
|
#endif
|