From 905a765c0f43a20fa63b5aa7fafd491e17b4765b Mon Sep 17 00:00:00 2001 From: Piotr Kania Date: Thu, 29 Apr 2021 11:51:05 +0200 Subject: [PATCH] Additional sanity checks for triangulate operation --- poly2tri/sweep/sweep.cc | 26 ++++++++++++++++++++++---- poly2tri/sweep/sweep_context.cc | 4 ++-- poly2tri/sweep/sweep_context.h | 2 +- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/poly2tri/sweep/sweep.cc b/poly2tri/sweep/sweep.cc index b6e81eb..3267a41 100644 --- a/poly2tri/sweep/sweep.cc +++ b/poly2tri/sweep/sweep.cc @@ -77,7 +77,13 @@ void Sweep::FinalizationPolygon(SweepContext& tcx) Node& Sweep::PointEvent(SweepContext& tcx, Point& point) { - Node& node = tcx.LocateNode(point); + Node* node_ptr = tcx.LocateNode(point); + if (!node_ptr || !node_ptr->point || !node_ptr->next || !node_ptr->next->point) + { + throw std::runtime_error("PointEvent - null node"); + } + + Node& node = *node_ptr; Node& new_node = NewFrontTriangle(tcx, point, node); // Only need to check +epsilon since point never have smaller @@ -775,10 +781,22 @@ void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& if (ot_ptr == nullptr) { throw std::runtime_error("FlipScanEdgeEvent - null neighbor across"); } - Triangle& ot = *ot_ptr; - Point& op = *ot.OppositePoint(t, p); - if (InScanArea(eq, *flip_triangle.PointCCW(eq), *flip_triangle.PointCW(eq), op)) { + Point* op_ptr = ot_ptr->OppositePoint(t, p); + if (op_ptr == nullptr) { + throw std::runtime_error("FlipScanEdgeEvent - null opposing point"); + } + + Point* p1 = flip_triangle.PointCCW(eq); + Point* p2 = flip_triangle.PointCW(eq); + if (p1 == nullptr || p2 == nullptr) { + throw std::runtime_error("FlipScanEdgeEvent - null on either of points"); + } + + Triangle& ot = *ot_ptr; + Point& op = *op_ptr; + + if (InScanArea(eq, *p1, *p2, op)) { // flip with new edge op->eq FlipEdgeEvent(tcx, eq, op, &ot, op); // TODO: Actually I just figured out that it should be possible to diff --git a/poly2tri/sweep/sweep_context.cc b/poly2tri/sweep/sweep_context.cc index a6347de..79bc5e7 100644 --- a/poly2tri/sweep/sweep_context.cc +++ b/poly2tri/sweep/sweep_context.cc @@ -114,10 +114,10 @@ void SweepContext::AddToMap(Triangle* triangle) map_.push_back(triangle); } -Node& SweepContext::LocateNode(const Point& point) +Node* SweepContext::LocateNode(const Point& point) { // TODO implement search tree - return *front_->LocateNode(point.x); + return front_->LocateNode(point.x); } void SweepContext::CreateAdvancingFront() diff --git a/poly2tri/sweep/sweep_context.h b/poly2tri/sweep/sweep_context.h index b6bc1cd..47d970b 100644 --- a/poly2tri/sweep/sweep_context.h +++ b/poly2tri/sweep/sweep_context.h @@ -66,7 +66,7 @@ Point* tail() const; size_t point_count() const; -Node& LocateNode(const Point& point); +Node* LocateNode(const Point& point); void RemoveNode(Node* node);