From 4cdf0bd6c9ab610c4c4c4652f56b85b9b3e84b90 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Fri, 21 Oct 2022 16:13:01 -0700 Subject: [PATCH] Clipper2: Handle nested poly/hole/poly returns In Clipper1, we had a flat tree structure on returns. Clipper2 nests these, so we need to properly handle the nesting structure when importing the polygons --- libs/kimath/include/geometry/shape_poly_set.h | 3 ++ libs/kimath/src/geometry/shape_poly_set.cpp | 38 ++++++++++++------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/libs/kimath/include/geometry/shape_poly_set.h b/libs/kimath/include/geometry/shape_poly_set.h index 10ae9554bb..cf91a5a544 100644 --- a/libs/kimath/include/geometry/shape_poly_set.h +++ b/libs/kimath/include/geometry/shape_poly_set.h @@ -1394,6 +1394,9 @@ private: void importTree( Clipper2Lib::Paths64& paths, const std::vector& aZValueBuffer, const std::vector& aArcBuffe ); + void importPolyPath( Clipper2Lib::PolyPath64* aPolyPath, + const std::vector& aZValueBuffer, + const std::vector& aArcBuffer ); void inflate1( int aAmount, int aCircleSegCount, CORNER_STRATEGY aCornerStrategy ); void inflate2( int aAmount, int aCircleSegCount, CORNER_STRATEGY aCornerStrategy ); diff --git a/libs/kimath/src/geometry/shape_poly_set.cpp b/libs/kimath/src/geometry/shape_poly_set.cpp index bf4e62c4f2..9a4cbfa491 100644 --- a/libs/kimath/src/geometry/shape_poly_set.cpp +++ b/libs/kimath/src/geometry/shape_poly_set.cpp @@ -1082,6 +1082,29 @@ void SHAPE_POLY_SET::importTree( ClipperLib::PolyTree* tree, } +void SHAPE_POLY_SET::importPolyPath( Clipper2Lib::PolyPath64* aPolyPath, + const std::vector& aZValueBuffer, + const std::vector& aArcBuffer ) +{ + if( !aPolyPath->IsHole() ) + { + POLYGON paths; + paths.reserve( aPolyPath->Count() + 1 ); + paths.emplace_back( aPolyPath->Polygon(), aZValueBuffer, aArcBuffer ); + + for( Clipper2Lib::PolyPath64* child : *aPolyPath ) + { + paths.emplace_back( child->Polygon(), aZValueBuffer, aArcBuffer ); + + for( Clipper2Lib::PolyPath64* grandchild : *child ) + importPolyPath( grandchild, aZValueBuffer, aArcBuffer ); + } + + m_polys.push_back( paths ); + } +} + + void SHAPE_POLY_SET::importTree( Clipper2Lib::PolyTree64& tree, const std::vector& aZValueBuffer, const std::vector& aArcBuffer ) @@ -1089,20 +1112,7 @@ void SHAPE_POLY_SET::importTree( Clipper2Lib::PolyTree64& tree, m_polys.clear(); for( Clipper2Lib::PolyPath64* n : tree ) - { - if( !n->IsHole() ) - { - POLYGON paths; - paths.reserve( n->Count() + 1 ); - - paths.emplace_back( n->Polygon(), aZValueBuffer, aArcBuffer ); - - for( Clipper2Lib::PolyPath64* child : *n) - paths.emplace_back( child->Polygon(), aZValueBuffer, aArcBuffer ); - - m_polys.push_back( paths ); - } - } + importPolyPath( n, aZValueBuffer, aArcBuffer ); }