Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add NumVert, NumContour, and Warp #370

Merged
merged 4 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, either now or in a follow-on PR, would you mind adding docs to these functions? The CrossSection docs are looking a little sparse.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll make that my next PR.

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());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}