Skip to content

Commit

Permalink
Add NumVert, NumContour, and Warp to CrossSection (elalish#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffder authored Mar 15, 2023
1 parent 5a7c3b1 commit 7c2767a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/cross_section/include/cross_section.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class CrossSection {
// Output
Polygons ToPolygons() const;
double Area() const;
int NumVert() const;
int NumContour() const;
bool IsEmpty() const;
Rect Bounds() const;
///@}
Expand All @@ -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<void(glm::vec2&)> 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,
Expand Down
31 changes: 31 additions & 0 deletions src/cross_section/src/cross_section.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,24 @@ CrossSection CrossSection::Transform(const glm::mat3x2& m) const {
return transformed;
}

CrossSection CrossSection::Warp(
std::function<void(glm::vec2&)> 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 CrossSection(C2::Union(warped, C2::FillRule::Positive, precision_));
}

CrossSection CrossSection::Simplify(double epsilon) const {
auto ps = SimplifyPaths(GetPaths(), epsilon, false);
return CrossSection(ps);
Expand All @@ -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::NumContour() 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 {
Expand Down
14 changes: 14 additions & 0 deletions test/cross_section_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.NumContour(), 1);
Identical(Manifold::Extrude(a, 1.).GetMesh(),
Manifold::Extrude(b, 1.).GetMesh());
}

0 comments on commit 7c2767a

Please sign in to comment.