From 3b76c0998637f551b4073720c7574f9958177c67 Mon Sep 17 00:00:00 2001 From: Markus Bullmann Date: Wed, 4 Apr 2018 09:37:05 +0200 Subject: [PATCH] merged duplicated code regarding GPCPolygon --- geo/GPCPolygon2.h | 137 ++++++++++++++++++++++++ navMesh/NavMeshFactory.h | 115 +------------------- sensors/offline/FileReader.h | 2 +- wifi/estimate/ray3/ModelFactory.h | 5 +- wifi/estimate/ray3/ModelFactoryHelper.h | 109 ------------------- 5 files changed, 144 insertions(+), 224 deletions(-) create mode 100644 geo/GPCPolygon2.h delete mode 100644 wifi/estimate/ray3/ModelFactoryHelper.h diff --git a/geo/GPCPolygon2.h b/geo/GPCPolygon2.h new file mode 100644 index 0000000..9111458 --- /dev/null +++ b/geo/GPCPolygon2.h @@ -0,0 +1,137 @@ +#ifndef GPCPOLYGON2_H +#define GPCPOLYGON2_H + +#include "../lib/gpc/gpc.cpp.h" + +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 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); + } + + std::vector> 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> 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 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> 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> trias; + + for (int i = 0; i < res.num_strips; ++i) { + gpc_vertex_list lst = res.strip[i]; + std::vector 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(Floorplan::Polygon2 poly, GPCPolygon& result) { + + std::vector 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); + + } + +}; +#endif diff --git a/navMesh/NavMeshFactory.h b/navMesh/NavMeshFactory.h index d563402..4672788 100644 --- a/navMesh/NavMeshFactory.h +++ b/navMesh/NavMeshFactory.h @@ -7,6 +7,7 @@ #include "../floorplan/v2/FloorplanHelper.h" #include "../geo/ConvexHull2.h" +#include "../geo/GPCPolygon2.h" #include "../wifi/estimate/ray3/OBJPool.h" #include "NavMesh.h" @@ -15,121 +16,11 @@ #include "NavMeshType.h" #include "NavMeshSettings.h" -#include "../lib/gpc/gpc.cpp.h" #include "../lib/Recast/Recast.h" #include // memset namespace NM { - - class NavMeshPoly { - - struct GPCPolygon : gpc_polygon { - GPCPolygon() { - 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; - float z; - - public: - - NavMeshPoly(float z) : z(z) { - ; - } - - void add(const Floorplan::Polygon2& poly) { - GPCPolygon cur = toGPC(poly); - gpc_polygon_clip(GPC_UNION, &state, &cur, &state); - } - - void remove(const Floorplan::Polygon2& poly) { - GPCPolygon cur = toGPC(poly); - gpc_polygon_clip(GPC_DIFF, &state, &cur, &state); - } - - std::vector> 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> 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 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); - - } - - private: - - GPCPolygon toGPC(Floorplan::Polygon2 poly) { - - std::vector 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; - - } - - }; - - - - - - struct TriangleIn { Point3 p1; Point3 p2; @@ -233,7 +124,7 @@ namespace NM { // if this is a to-be-added polygon, add it if (poly->method == Floorplan::OutlineMethod::ADD) { - NavMeshPoly nmPoly(floor->atHeight); + GPCPolygon2 nmPoly(floor->atHeight); nmPoly.add(poly->poly); // get all other polygons of this floor, that are tagged as "remove" and remove them (many will be outside of the added polygon) @@ -302,7 +193,7 @@ namespace NM { { // add (overlay) all doors for tagging them within the plan - NavMeshPoly nmDoors(floor->atHeight); + GPCPolygon2 nmDoors(floor->atHeight); for (Floorplan::FloorObstacle* obs : floor->obstacles) { Floorplan::FloorObstacleDoor* door = dynamic_cast(obs); if (door != nullptr) { diff --git a/sensors/offline/FileReader.h b/sensors/offline/FileReader.h index 03f7665..569c5e7 100644 --- a/sensors/offline/FileReader.h +++ b/sensors/offline/FileReader.h @@ -28,7 +28,7 @@ #include "Sensors.h" #include "Listener.h" -#warning "adjust to to use the new splitter for all parsers [gps, compass, etc. already do!]" +#pragma message "adjust to to use the new splitter for all parsers [gps, compass, etc. already do!]" namespace Offline { diff --git a/wifi/estimate/ray3/ModelFactory.h b/wifi/estimate/ray3/ModelFactory.h index acce398..c9d3814 100644 --- a/wifi/estimate/ray3/ModelFactory.h +++ b/wifi/estimate/ray3/ModelFactory.h @@ -3,7 +3,8 @@ #include "../../../floorplan/v2/Floorplan.h" #include "../../../geo/Triangle3.h" -#include "ModelFactoryHelper.h" +#include "../../../geo/GPCPolygon2.h" + #include "Obstacle3.h" #include "Cube.h" #include "Tube.h" @@ -129,7 +130,7 @@ namespace Ray3D { // process all "add" regions by type // [this allows for overlaps of the same type] - std::unordered_map types; + std::unordered_map types; for (Floorplan::FloorOutlinePolygon* fop : f->outline) { if (fop->method == Floorplan::OutlineMethod::ADD) { if (fop->outdoor) { diff --git a/wifi/estimate/ray3/ModelFactoryHelper.h b/wifi/estimate/ray3/ModelFactoryHelper.h deleted file mode 100644 index 3fa8847..0000000 --- a/wifi/estimate/ray3/ModelFactoryHelper.h +++ /dev/null @@ -1,109 +0,0 @@ -#ifndef MODELFACTORYHELPER_H -#define MODELFACTORYHELPER_H - -#include -#include "../../../lib/gpc/gpc.h" - -namespace Ray3D { - - class Polygon { - - struct GPCPolygon : gpc_polygon { - GPCPolygon() { - 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> 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> trias; - - for (int i = 0; i < res.num_strips; ++i) { - gpc_vertex_list lst = res.strip[i]; - std::vector 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 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 // MODELFACTORYHELPER_H