forked from elalish/manifold
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2d-operations via Clipper2 (elalish#341)
* WIP Clipper2 wrapper for 2d subsystem * Working example in clippoly_test * Add clean_ property to clippoly * Square center=false by default * Square defaults to center=false * Shorten union example * + simplification and minkowski functions * + Circle and note on Minkowski * + Offset (InflatePaths) * Clippoly -> CrossSection renaming * Switch from NonZero to Positive fill rule * -(minkowski, simplifs, clean_) + pre-alloc vecs Remove minkowski, simplification algorithms other than simplify, clean (union) arbitrary contours on construction, and pre-allocate vectors when length is known. * - index from polygon conversion + missed vec prealloc * Use Polygon shorthand over std::vector * Update copyright date * Add Rotate * Update copyrights to 2023 * Extrude and Revolve accept CrossSection only * Fix incorrect use of vector constructor * Fix remaining incorrect vector construction * Revert "Update copyrights to 2023" This reverts commit af8f6c8. * Re-update copyright notice in cross_section files * Add missing precision_ argument to cleaning Unions * Lift circular resolution globals into Quality * Replace uses of Manifold globals with Quality * + Ellipse, Mirror, Area * Include mirror in Union example and test area * + Rewind (reverse winding of all contours) * + fillrule/IsEmpty - Ellipse/Rewind * Move Quality into public.h * Remove bad definition * Replace manual circles with CrossSection::Circle * Lists of empty contours are empty * + Rect class, RectClip, + const params/methods * Add Transform to CrossSection and Rect
- Loading branch information
Showing
23 changed files
with
872 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright 2023 The Manifold Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
project (cross_section) | ||
|
||
file(GLOB_RECURSE SOURCE_FILES CONFIGURE_DEPENDS *.cpp) | ||
add_library(${PROJECT_NAME} OBJECT ${SOURCE_FILES}) | ||
|
||
target_include_directories(${PROJECT_NAME} | ||
PUBLIC ${PROJECT_SOURCE_DIR}/include | ||
) | ||
target_link_libraries( ${PROJECT_NAME} | ||
PUBLIC utilities Clipper2 | ||
) | ||
|
||
target_compile_options(${PROJECT_NAME} PRIVATE ${MANIFOLD_FLAGS}) | ||
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// Copyright 2023 The Manifold Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <clipper2/clipper.h> | ||
|
||
#include "clipper2/clipper.core.h" | ||
#include "clipper2/clipper.offset.h" | ||
#include "glm/ext/vector_float2.hpp" | ||
#include "public.h" | ||
|
||
namespace C2 = Clipper2Lib; | ||
|
||
namespace manifold { | ||
|
||
class Rect; | ||
|
||
class CrossSection { | ||
public: | ||
CrossSection(); | ||
~CrossSection(); | ||
CrossSection(const CrossSection& other); | ||
CrossSection& operator=(const CrossSection& other); | ||
CrossSection(CrossSection&&) noexcept; | ||
CrossSection& operator=(CrossSection&&) noexcept; | ||
|
||
// Construction from arbitrary contours | ||
enum class FillRule { EvenOdd, NonZero, Positive, Negative }; | ||
CrossSection(const SimplePolygon& contour, | ||
FillRule fillrule = FillRule::Positive); | ||
CrossSection(const Polygons& contours, | ||
FillRule fillrule = FillRule::Positive); | ||
|
||
// Shapes | ||
static CrossSection Square(const glm::vec2 dims, bool center = false); | ||
static CrossSection Circle(float radius, int circularSegments = 0); | ||
|
||
// Booleans | ||
enum class OpType { Add, Subtract, Intersect, Xor }; | ||
CrossSection Boolean(const CrossSection& second, OpType op) const; | ||
static CrossSection BatchBoolean( | ||
const std::vector<CrossSection>& crossSections, OpType op); | ||
CrossSection operator+(const CrossSection&) const; // Add (Union) | ||
CrossSection& operator+=(const CrossSection&); | ||
CrossSection operator-(const CrossSection&) const; // Subtract (Difference) | ||
CrossSection& operator-=(const CrossSection&); | ||
CrossSection operator^(const CrossSection&) const; // Intersect | ||
CrossSection& operator^=(const CrossSection&); | ||
CrossSection RectClip(const Rect& rect) const; | ||
|
||
// Transformations | ||
CrossSection Translate(const glm::vec2 v) const; | ||
CrossSection Rotate(float degrees) const; | ||
CrossSection Scale(const glm::vec2 s) const; | ||
CrossSection Mirror(const glm::vec2 ax) const; | ||
CrossSection Transform(const glm::mat3x2& m) const; | ||
|
||
// Path Simplification | ||
CrossSection Simplify(double epsilon = 1e-6) const; | ||
|
||
// Offsetting | ||
enum class JoinType { Square, Round, Miter }; | ||
CrossSection Offset(double delta, JoinType jt, double miter_limit = 2.0, | ||
double arc_tolerance = 0.0) const; | ||
|
||
// Geometry | ||
double Area() const; | ||
bool IsEmpty() const; | ||
Rect Bounds() const; | ||
|
||
// Output | ||
Polygons ToPolygons() const; | ||
|
||
private: | ||
C2::PathsD paths_; | ||
CrossSection(C2::PathsD paths); | ||
}; | ||
|
||
class Rect { | ||
public: | ||
glm::vec2 min = glm::vec2(0); | ||
glm::vec2 max = glm::vec2(0); | ||
|
||
/** | ||
* Default constructor is an empty rectangle.. | ||
*/ | ||
Rect(); | ||
~Rect(); | ||
Rect(const Rect& other); | ||
Rect& operator=(const Rect& other); | ||
Rect(Rect&&) noexcept; | ||
Rect& operator=(Rect&&) noexcept; | ||
|
||
/** | ||
* Creates a rectangle that contains the two given points. | ||
*/ | ||
Rect(const glm::vec2 a, const glm::vec2 b); | ||
|
||
/** | ||
* Returns the dimensions of the Rect. | ||
*/ | ||
glm::vec2 Size() const; | ||
|
||
/** | ||
* Returns the absolute-largest coordinate value of any contained | ||
* point. | ||
*/ | ||
float Scale() const; | ||
|
||
/** | ||
* Returns the center point of the Box. | ||
*/ | ||
glm::vec2 Center() const; | ||
|
||
bool Contains(const glm::vec2& pt) const; | ||
bool Contains(const Rect& other) const; | ||
bool DoesOverlap(const Rect& other) const; | ||
bool IsEmpty() const; | ||
bool IsFinite() const; | ||
|
||
/** | ||
* Expand this rectangle (in place) to include the given point. | ||
*/ | ||
void Union(const glm::vec2 p); | ||
|
||
/** | ||
* Expand this rectangle to include the given Rect. | ||
*/ | ||
Rect Union(const Rect& other) const; | ||
|
||
/** | ||
* Shift this rectangle by the given vector. | ||
*/ | ||
Rect operator+(const glm::vec2 shift) const; | ||
|
||
/** | ||
* Shift this rectangle in-place by the given vector. | ||
*/ | ||
Rect& operator+=(const glm::vec2 shift); | ||
|
||
/** | ||
* Scale this rectangle by the given vector. | ||
*/ | ||
Rect operator*(const glm::vec2 scale) const; | ||
|
||
/** | ||
* Scale this rectangle in-place by the given vector. | ||
*/ | ||
Rect& operator*=(const glm::vec2 scale); | ||
|
||
Rect Transform(const glm::mat3x2& m) const; | ||
|
||
CrossSection AsCrossSection() const; | ||
}; | ||
} // namespace manifold |
Oops, something went wrong.