From 804dec38533884aae10e7f7f2f39832ed29f4980 Mon Sep 17 00:00:00 2001 From: Geoff deRosenroll Date: Tue, 14 Mar 2023 21:40:13 -0700 Subject: [PATCH 1/4] Add NumVert, NumContours, and Warp --- src/cross_section/include/cross_section.h | 3 +++ src/cross_section/src/cross_section.cpp | 31 +++++++++++++++++++++++ test/cross_section_test.cpp | 14 ++++++++++ 3 files changed, 48 insertions(+) diff --git a/src/cross_section/include/cross_section.h b/src/cross_section/include/cross_section.h index c38991ead..63020cefd 100644 --- a/src/cross_section/include/cross_section.h +++ b/src/cross_section/include/cross_section.h @@ -64,6 +64,8 @@ class CrossSection { // Output Polygons ToPolygons() const; double Area() const; + int NumVert() const; + int NumContours() const; bool IsEmpty() const; Rect Bounds() const; ///@} @@ -76,6 +78,7 @@ class CrossSection { CrossSection Scale(const glm::vec2 s) const; CrossSection Mirror(const glm::vec2 ax) const; CrossSection Transform(const glm::mat3x2& m) const; + CrossSection Warp(std::function warpFunc) const; CrossSection Simplify(double epsilon = 1e-6) const; enum class JoinType { Square, Round, Miter }; CrossSection Offset(double delta, JoinType jt, double miter_limit = 2.0, diff --git a/src/cross_section/src/cross_section.cpp b/src/cross_section/src/cross_section.cpp index 5cea54b39..37d15a2fc 100644 --- a/src/cross_section/src/cross_section.cpp +++ b/src/cross_section/src/cross_section.cpp @@ -305,6 +305,24 @@ CrossSection CrossSection::Transform(const glm::mat3x2& m) const { return transformed; } +CrossSection CrossSection::Warp( + std::function warpFunc) const { + auto paths = GetPaths(); + auto warped = C2::PathsD(); + warped.reserve(paths.size()); + for (auto path : paths) { + auto sz = path.size(); + auto s = C2::PathD(sz); + for (int i = 0; i < sz; ++i) { + auto v = v2_of_pd(path[i]); + warpFunc(v); + s[i] = v2_to_pd(v); + } + warped.push_back(s); + } + return warped; +} + CrossSection CrossSection::Simplify(double epsilon) const { auto ps = SimplifyPaths(GetPaths(), epsilon, false); return CrossSection(ps); @@ -320,10 +338,23 @@ CrossSection CrossSection::Offset(double delta, JoinType jointype, } double CrossSection::Area() const { return C2::Area(GetPaths()); } + +int CrossSection::NumVert() const { + int n = 0; + auto paths = GetPaths(); + for (auto p : paths) { + n += p.size(); + } + return n; +} + +int CrossSection::NumContours() const { return GetPaths().size(); } + Rect CrossSection::Bounds() const { auto r = C2::GetBounds(GetPaths()); return Rect({r.left, r.bottom}, {r.right, r.top}); } + bool CrossSection::IsEmpty() const { return GetPaths().empty(); } Polygons CrossSection::ToPolygons() const { diff --git a/test/cross_section_test.cpp b/test/cross_section_test.cpp index 1cb90d073..0f9967704 100644 --- a/test/cross_section_test.cpp +++ b/test/cross_section_test.cpp @@ -97,3 +97,17 @@ TEST(CrossSection, Transform) { // same transformations are applied in b_copy (giving same result) Identical(ex_b, Manifold::Extrude(b_copy, 1.).GetMesh()); } + +TEST(CrossSection, Warp) { + auto sq = CrossSection::Square({10., 10.}); + auto a = sq.Scale({2, 3}).Translate({4, 5}); + auto b = sq.Warp([](glm::vec2 &v) { + v.x = v.x * 2 + 4; + v.y = v.y * 3 + 5; + }); + + EXPECT_EQ(sq.NumVert(), 4); + EXPECT_EQ(sq.NumContours(), 1); + Identical(Manifold::Extrude(a, 1.).GetMesh(), + Manifold::Extrude(b, 1.).GetMesh()); +} From efde71ce0939eabcadf5a10955d7d1d94729d75d Mon Sep 17 00:00:00 2001 From: Geoff deRosenroll Date: Tue, 14 Mar 2023 21:46:43 -0700 Subject: [PATCH 2/4] Depluralize NumContours (match other info methods) --- src/cross_section/include/cross_section.h | 2 +- src/cross_section/src/cross_section.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cross_section/include/cross_section.h b/src/cross_section/include/cross_section.h index 63020cefd..a724350bb 100644 --- a/src/cross_section/include/cross_section.h +++ b/src/cross_section/include/cross_section.h @@ -65,7 +65,7 @@ class CrossSection { Polygons ToPolygons() const; double Area() const; int NumVert() const; - int NumContours() const; + int NumContour() const; bool IsEmpty() const; Rect Bounds() const; ///@} diff --git a/src/cross_section/src/cross_section.cpp b/src/cross_section/src/cross_section.cpp index 37d15a2fc..09fc125be 100644 --- a/src/cross_section/src/cross_section.cpp +++ b/src/cross_section/src/cross_section.cpp @@ -348,7 +348,7 @@ int CrossSection::NumVert() const { return n; } -int CrossSection::NumContours() const { return GetPaths().size(); } +int CrossSection::NumContour() const { return GetPaths().size(); } Rect CrossSection::Bounds() const { auto r = C2::GetBounds(GetPaths()); From fccf80207e5e9a9874f193b38413cba68fa18ffb Mon Sep 17 00:00:00 2001 From: Geoff deRosenroll Date: Tue, 14 Mar 2023 21:52:06 -0700 Subject: [PATCH 3/4] Fix pluralization on NumContour --- test/cross_section_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cross_section_test.cpp b/test/cross_section_test.cpp index 0f9967704..d7a543ba1 100644 --- a/test/cross_section_test.cpp +++ b/test/cross_section_test.cpp @@ -107,7 +107,7 @@ TEST(CrossSection, Warp) { }); EXPECT_EQ(sq.NumVert(), 4); - EXPECT_EQ(sq.NumContours(), 1); + EXPECT_EQ(sq.NumContour(), 1); Identical(Manifold::Extrude(a, 1.).GetMesh(), Manifold::Extrude(b, 1.).GetMesh()); } From deac43225ec3ae4ee28fa5f3619dcd1bb6a8ab9d Mon Sep 17 00:00:00 2001 From: Geoff deRosenroll Date: Tue, 14 Mar 2023 22:24:09 -0700 Subject: [PATCH 4/4] Union warped paths --- src/cross_section/src/cross_section.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cross_section/src/cross_section.cpp b/src/cross_section/src/cross_section.cpp index 09fc125be..3a6139252 100644 --- a/src/cross_section/src/cross_section.cpp +++ b/src/cross_section/src/cross_section.cpp @@ -320,7 +320,7 @@ CrossSection CrossSection::Warp( } warped.push_back(s); } - return warped; + return CrossSection(C2::Union(warped, C2::FillRule::Positive, precision_)); } CrossSection CrossSection::Simplify(double epsilon) const {