From 4a5d11548c3145c47c54aa09e5390e886405a7a1 Mon Sep 17 00:00:00 2001 From: Tom Hulton-Harrop Date: Sat, 22 Apr 2023 22:04:47 +0100 Subject: [PATCH] add gsl span support --- CMakeLists.txt | 8 ++++++++ poly2tri/sweep/cdt.cc | 4 ++-- poly2tri/sweep/cdt.h | 6 ++++-- poly2tri/sweep/sweep_context.cc | 17 ++++++++++------- poly2tri/sweep/sweep_context.h | 8 +++++--- 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a237b9..b4dd47f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,13 @@ cmake_minimum_required(VERSION 3.12) project(poly2tri LANGUAGES CXX) 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_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) add_library(poly2tri ${SOURCES} ${HEADERS}) target_include_directories(poly2tri INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(poly2tri PUBLIC GSL) get_target_property(poly2tri_target_type poly2tri TYPE) if(poly2tri_target_type STREQUAL SHARED_LIBRARY) diff --git a/poly2tri/sweep/cdt.cc b/poly2tri/sweep/cdt.cc index 4dfe6a6..6b72cb0 100644 --- a/poly2tri/sweep/cdt.cc +++ b/poly2tri/sweep/cdt.cc @@ -32,13 +32,13 @@ namespace p2t { -CDT::CDT(const std::vector& polyline) +CDT::CDT(gsl::span polyline) { sweep_context_ = new SweepContext(polyline); sweep_ = new Sweep; } -void CDT::AddHole(const std::vector& polyline) +void CDT::AddHole(gsl::span polyline) { sweep_context_->AddHole(polyline); } diff --git a/poly2tri/sweep/cdt.h b/poly2tri/sweep/cdt.h index 7c54ea9..2d181da 100644 --- a/poly2tri/sweep/cdt.h +++ b/poly2tri/sweep/cdt.h @@ -35,6 +35,8 @@ #include "sweep_context.h" #include "sweep.h" +#include + #include "../common/dll_symbol.h" /** @@ -54,7 +56,7 @@ public: * * @param polyline */ - CDT(const std::vector& polyline); + explicit CDT(gsl::span polyline); /** * Destructor - clean up memory @@ -66,7 +68,7 @@ public: * * @param polyline */ - void AddHole(const std::vector& polyline); + void AddHole(gsl::span polyline); /** * Add a steiner point diff --git a/poly2tri/sweep/sweep_context.cc b/poly2tri/sweep/sweep_context.cc index 7b9432f..593acc5 100644 --- a/poly2tri/sweep/sweep_context.cc +++ b/poly2tri/sweep/sweep_context.cc @@ -34,7 +34,7 @@ namespace p2t { -SweepContext::SweepContext(std::vector polyline) : points_(std::move(polyline)), +SweepContext::SweepContext(gsl::span polyline) : front_(nullptr), head_(nullptr), tail_(nullptr), @@ -42,14 +42,17 @@ SweepContext::SweepContext(std::vector polyline) : points_(std::move(pol af_middle_(nullptr), af_tail_(nullptr) { - InitEdges(points_); + for (auto& point : polyline) { + points_.push_back(&point); + } + InitEdges(polyline); } -void SweepContext::AddHole(const std::vector& polyline) +void SweepContext::AddHole(gsl::span polyline) { InitEdges(polyline); - for (auto i : polyline) { - points_.push_back(i); + for (auto& point : polyline) { + points_.push_back(&point); } } @@ -95,12 +98,12 @@ void SweepContext::InitTriangulation() } -void SweepContext::InitEdges(const std::vector& polyline) +void SweepContext::InitEdges(gsl::span polyline) { size_t num_points = polyline.size(); for (size_t i = 0; i < num_points; i++) { 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])); } } diff --git a/poly2tri/sweep/sweep_context.h b/poly2tri/sweep/sweep_context.h index 11d5739..cac153b 100644 --- a/poly2tri/sweep/sweep_context.h +++ b/poly2tri/sweep/sweep_context.h @@ -31,6 +31,8 @@ #pragma once +#include + #include #include #include @@ -51,7 +53,7 @@ class SweepContext { public: /// Constructor -explicit SweepContext(std::vector polyline); +explicit SweepContext(gsl::span polyline); /// Destructor ~SweepContext(); @@ -82,7 +84,7 @@ Point* GetPoints(); void RemoveFromMap(Triangle* triangle); -void AddHole(const std::vector& polyline); +void AddHole(gsl::span polyline); void AddPoint(Point* point); @@ -147,7 +149,7 @@ Point* tail_; Node *af_head_, *af_middle_, *af_tail_; void InitTriangulation(); -void InitEdges(const std::vector& polyline); +void InitEdges(gsl::span polyline); };