Manifold 3.0
Robust geometry
 
Loading...
Searching...
No Matches
cross_section.h
1// Copyright 2023 The Manifold Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <functional>
18#include <memory>
19#include <vector>
20
21#include "manifold/common.h"
22#include "manifold/vec_view.h"
23
24namespace manifold {
25
32struct PathImpl;
33
42 public:
49
50 CrossSection(const CrossSection& other);
51 CrossSection& operator=(const CrossSection& other);
52 CrossSection(CrossSection&&) noexcept;
53 CrossSection& operator=(CrossSection&&) noexcept;
55
56 // Adapted from Clipper2 docs:
57 // http://www.angusj.com/clipper2/Docs/Units/Clipper/Types/FillRule.htm
58 // (Copyright © 2010-2023 Angus Johnson)
66 enum class FillRule {
67 EvenOdd,
68 NonZero,
69 Positive,
71 };
72
73 // Adapted from Clipper2 docs:
74 // http://www.angusj.com/clipper2/Docs/Units/Clipper/Types/JoinType.htm
75 // (Copyright © 2010-2023 Angus Johnson)
82 enum class JoinType {
83 Square,
86 Round,
89 Miter
94 };
95
99 CrossSection(const SimplePolygon& contour,
100 FillRule fillrule = FillRule::Positive);
101 CrossSection(const Polygons& contours,
102 FillRule fillrule = FillRule::Positive);
103 CrossSection(const Rect& rect);
104 Polygons ToPolygons() const;
106
111 std::vector<CrossSection> Decompose() const;
112 static CrossSection Compose(std::vector<CrossSection>&);
113 static CrossSection Square(const vec2 dims, bool center = false);
114 static CrossSection Circle(double radius, int circularSegments = 0);
116
121 bool IsEmpty() const;
122 size_t NumVert() const;
123 size_t NumContour() const;
124 Rect Bounds() const;
125 double Area() const;
127
131 CrossSection Translate(const vec2 v) const;
132 CrossSection Rotate(double degrees) const;
133 CrossSection Scale(const vec2 s) const;
134 CrossSection Mirror(const vec2 ax) const;
135 CrossSection Transform(const mat2x3& m) const;
136 CrossSection Warp(std::function<void(vec2&)> warpFunc) const;
137 CrossSection WarpBatch(std::function<void(VecView<vec2>)> warpFunc) const;
138 CrossSection Simplify(double epsilon = 1e-6) const;
139 CrossSection Offset(double delta, JoinType jt, double miter_limit = 2.0,
140 int circularSegments = 0) const;
142
147 CrossSection Boolean(const CrossSection& second, OpType op) const;
149 const std::vector<CrossSection>& crossSections, OpType op);
150 CrossSection operator+(const CrossSection&) const;
152 CrossSection operator-(const CrossSection&) const;
154 CrossSection operator^(const CrossSection&) const;
157
161 CrossSection Hull() const;
162 static CrossSection Hull(const std::vector<CrossSection>& crossSections);
163 static CrossSection Hull(const SimplePolygon pts);
164 static CrossSection Hull(const Polygons polys);
166
167 private:
168 mutable std::shared_ptr<const PathImpl> paths_;
169 mutable mat2x3 transform_ = la::identity;
170 CrossSection(std::shared_ptr<const PathImpl> paths);
171 std::shared_ptr<const PathImpl> GetPaths() const;
172};
174} // namespace manifold
Two-dimensional cross sections guaranteed to be without self-intersections, or overlaps between polyg...
Definition cross_section.h:41
CrossSection Boolean(const CrossSection &second, OpType op) const
Definition cross_section.cpp:359
CrossSection & operator+=(const CrossSection &)
Definition cross_section.cpp:407
Polygons ToPolygons() const
Definition cross_section.cpp:775
CrossSection WarpBatch(std::function< void(VecView< vec2 >)> warpFunc) const
Definition cross_section.cpp:574
CrossSection operator+(const CrossSection &) const
Definition cross_section.cpp:399
CrossSection operator^(const CrossSection &) const
Definition cross_section.cpp:432
CrossSection Translate(const vec2 v) const
Definition cross_section.cpp:485
CrossSection Mirror(const vec2 ax) const
Definition cross_section.cpp:528
CrossSection & operator-=(const CrossSection &)
Definition cross_section.cpp:424
std::vector< CrossSection > Decompose() const
Definition cross_section.cpp:458
CrossSection Transform(const mat2x3 &m) const
Definition cross_section.cpp:544
static CrossSection Compose(std::vector< CrossSection > &)
Definition cross_section.cpp:449
CrossSection Rotate(double degrees) const
Definition cross_section.cpp:498
static CrossSection BatchBoolean(const std::vector< CrossSection > &crossSections, OpType op)
Definition cross_section.cpp:371
size_t NumContour() const
Definition cross_section.cpp:756
bool IsEmpty() const
Definition cross_section.cpp:761
CrossSection Offset(double delta, JoinType jt, double miter_limit=2.0, int circularSegments=0) const
Definition cross_section.cpp:656
FillRule
Definition cross_section.h:66
@ Positive
Only sub-regions with winding counts > 0 are filled.
@ NonZero
Only non-zero sub-regions are filled.
@ EvenOdd
Only odd numbered sub-regions are filled.
@ Negative
Only sub-regions with winding counts < 0 are filled.
CrossSection & operator^=(const CrossSection &)
Definition cross_section.cpp:440
CrossSection Simplify(double epsilon=1e-6) const
Definition cross_section.cpp:610
CrossSection Hull() const
Definition cross_section.cpp:702
CrossSection Warp(std::function< void(vec2 &)> warpFunc) const
Definition cross_section.cpp:559
double Area() const
Definition cross_section.cpp:738
size_t NumVert() const
Definition cross_section.cpp:743
CrossSection()
Definition cross_section.cpp:212
CrossSection Scale(const vec2 s) const
Definition cross_section.cpp:513
Rect Bounds() const
Definition cross_section.cpp:767
JoinType
Definition cross_section.h:82
CrossSection operator-(const CrossSection &) const
Definition cross_section.cpp:416
static CrossSection Circle(double radius, int circularSegments=0)
Definition cross_section.cpp:342
Definition vec_view.h:32
constexpr double degrees(double a)
Definition common.h:75
std::vector< SimplePolygon > Polygons
Set of polygons with holes. Order of contours is arbitrary. Can contain any depth of nested holes and...
Definition common.h:139
OpType
Boolean operation type: Add (Union), Subtract (Difference), and Intersect.
Definition common.h:473
std::vector< vec2 > SimplePolygon
Single polygon contour, wound CCW. First and last point are implicitly connected. Should ensure all i...
Definition common.h:131
Axis-aligned 2D box, primarily for bounding.
Definition common.h:307