add gsl span support

This commit is contained in:
Tom Hulton-Harrop 2023-04-22 22:04:47 +01:00
parent 81612cb108
commit 4a5d11548c
5 changed files with 29 additions and 14 deletions

View File

@ -3,6 +3,13 @@ cmake_minimum_required(VERSION 3.12)
project(poly2tri LANGUAGES CXX) project(poly2tri LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
include(FetchContent)
FetchContent_Declare(
gsl
GIT_REPOSITORY https://github.com/microsoft/GSL.git
GIT_TAG 1d036585ccea8a57dc5fdc84406181db3d1f3205)
FetchContent_MakeAvailable(gsl)
option(P2T_BUILD_TESTS "Build tests" OFF) option(P2T_BUILD_TESTS "Build tests" OFF)
option(P2T_BUILD_TESTBED "Build the testbed application" OFF) option(P2T_BUILD_TESTBED "Build the testbed application" OFF)
@ -10,6 +17,7 @@ file(GLOB SOURCES poly2tri/common/*.cc poly2tri/sweep/*.cc)
file(GLOB HEADERS poly2tri/*.h poly2tri/common/*.h poly2tri/sweep/*.h) file(GLOB HEADERS poly2tri/*.h poly2tri/common/*.h poly2tri/sweep/*.h)
add_library(poly2tri ${SOURCES} ${HEADERS}) add_library(poly2tri ${SOURCES} ${HEADERS})
target_include_directories(poly2tri INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(poly2tri INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(poly2tri PUBLIC GSL)
get_target_property(poly2tri_target_type poly2tri TYPE) get_target_property(poly2tri_target_type poly2tri TYPE)
if(poly2tri_target_type STREQUAL SHARED_LIBRARY) if(poly2tri_target_type STREQUAL SHARED_LIBRARY)

View File

@ -32,13 +32,13 @@
namespace p2t { namespace p2t {
CDT::CDT(const std::vector<Point*>& polyline) CDT::CDT(gsl::span<Point> polyline)
{ {
sweep_context_ = new SweepContext(polyline); sweep_context_ = new SweepContext(polyline);
sweep_ = new Sweep; sweep_ = new Sweep;
} }
void CDT::AddHole(const std::vector<Point*>& polyline) void CDT::AddHole(gsl::span<Point> polyline)
{ {
sweep_context_->AddHole(polyline); sweep_context_->AddHole(polyline);
} }

View File

@ -35,6 +35,8 @@
#include "sweep_context.h" #include "sweep_context.h"
#include "sweep.h" #include "sweep.h"
#include <gsl/span>
#include "../common/dll_symbol.h" #include "../common/dll_symbol.h"
/** /**
@ -54,7 +56,7 @@ public:
* *
* @param polyline * @param polyline
*/ */
CDT(const std::vector<Point*>& polyline); explicit CDT(gsl::span<Point> polyline);
/** /**
* Destructor - clean up memory * Destructor - clean up memory
@ -66,7 +68,7 @@ public:
* *
* @param polyline * @param polyline
*/ */
void AddHole(const std::vector<Point*>& polyline); void AddHole(gsl::span<Point> polyline);
/** /**
* Add a steiner point * Add a steiner point

View File

@ -34,7 +34,7 @@
namespace p2t { namespace p2t {
SweepContext::SweepContext(std::vector<Point*> polyline) : points_(std::move(polyline)), SweepContext::SweepContext(gsl::span<Point> polyline) :
front_(nullptr), front_(nullptr),
head_(nullptr), head_(nullptr),
tail_(nullptr), tail_(nullptr),
@ -42,14 +42,17 @@ SweepContext::SweepContext(std::vector<Point*> polyline) : points_(std::move(pol
af_middle_(nullptr), af_middle_(nullptr),
af_tail_(nullptr) af_tail_(nullptr)
{ {
InitEdges(points_); for (auto& point : polyline) {
points_.push_back(&point);
}
InitEdges(polyline);
} }
void SweepContext::AddHole(const std::vector<Point*>& polyline) void SweepContext::AddHole(gsl::span<Point> polyline)
{ {
InitEdges(polyline); InitEdges(polyline);
for (auto i : polyline) { for (auto& point : polyline) {
points_.push_back(i); points_.push_back(&point);
} }
} }
@ -95,12 +98,12 @@ void SweepContext::InitTriangulation()
} }
void SweepContext::InitEdges(const std::vector<Point*>& polyline) void SweepContext::InitEdges(gsl::span<Point> polyline)
{ {
size_t num_points = polyline.size(); size_t num_points = polyline.size();
for (size_t i = 0; i < num_points; i++) { for (size_t i = 0; i < num_points; i++) {
size_t j = i < num_points - 1 ? i + 1 : 0; size_t j = i < num_points - 1 ? i + 1 : 0;
edge_list.push_back(new Edge(*polyline[i], *polyline[j])); edge_list.push_back(new Edge(polyline[i], polyline[j]));
} }
} }

View File

@ -31,6 +31,8 @@
#pragma once #pragma once
#include <gsl/span>
#include <list> #include <list>
#include <vector> #include <vector>
#include <cstddef> #include <cstddef>
@ -51,7 +53,7 @@ class SweepContext {
public: public:
/// Constructor /// Constructor
explicit SweepContext(std::vector<Point*> polyline); explicit SweepContext(gsl::span<Point> polyline);
/// Destructor /// Destructor
~SweepContext(); ~SweepContext();
@ -82,7 +84,7 @@ Point* GetPoints();
void RemoveFromMap(Triangle* triangle); void RemoveFromMap(Triangle* triangle);
void AddHole(const std::vector<Point*>& polyline); void AddHole(gsl::span<Point> polyline);
void AddPoint(Point* point); void AddPoint(Point* point);
@ -147,7 +149,7 @@ Point* tail_;
Node *af_head_, *af_middle_, *af_tail_; Node *af_head_, *af_middle_, *af_tail_;
void InitTriangulation(); void InitTriangulation();
void InitEdges(const std::vector<Point*>& polyline); void InitEdges(gsl::span<Point> polyline);
}; };