Skip to content

Commit

Permalink
2d-operations via Clipper2 (elalish#341)
Browse files Browse the repository at this point in the history
* 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
geoffder authored Mar 4, 2023
1 parent 7c8ce3c commit a97c666
Show file tree
Hide file tree
Showing 23 changed files with 872 additions and 128 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
path = bindings/python/third_party/pybind11
url = https://github.com/pybind/pybind11
branch = stable
[submodule "src/third_party/clipper2"]
path = src/third_party/clipper2
url = https://github.com/AngusJohnson/clipper2
2 changes: 1 addition & 1 deletion bindings/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ endif()

target_link_libraries(
manifoldc
PRIVATE manifold sdf meshIO graphlite
PRIVATE manifold sdf meshIO graphlite cross_section
)

target_include_directories(manifoldc PUBLIC ${PROJECT_SOURCE_DIR}/include)
Expand Down
9 changes: 5 additions & 4 deletions bindings/c/manifoldc.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <conv.h>
#include <cross_section.h>
#include <manifold.h>
#include <manifoldc.h>
#include <meshIO.h>
Expand Down Expand Up @@ -406,19 +407,19 @@ float *manifold_curvature_vert_gaussian(void *mem, ManifoldCurvature *curv) {

// Static Quality Globals
void manifold_set_min_circular_angle(float degrees) {
Manifold::SetMinCircularAngle(degrees);
Quality::SetMinCircularAngle(degrees);
}

void manifold_set_min_circular_edge_length(float length) {
Manifold::SetMinCircularEdgeLength(length);
Quality::SetMinCircularEdgeLength(length);
}

void manifold_set_circular_segments(int number) {
Manifold::SetCircularSegments(number);
Quality::SetCircularSegments(number);
}

int manifold_get_circular_segments(float radius) {
return Manifold::GetCircularSegments(radius);
return Quality::GetCircularSegments(radius);
}

// memory size
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ add_executable(manifoldjs bindings.cpp)
# make sure that we recompile the wasm when bindings.js is being modified
set_source_files_properties(bindings.cpp OBJECT_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/bindings.js)
target_link_libraries(manifoldjs manifold sdf)
target_link_libraries(manifoldjs manifold sdf cross_section)
target_compile_options(manifoldjs PRIVATE ${MANIFOLD_FLAGS} -fexceptions)
target_link_options(manifoldjs PUBLIC --pre-js ${CMAKE_CURRENT_SOURCE_DIR}/bindings.js --bind -sALLOW_TABLE_GROWTH=1
-sEXPORTED_RUNTIME_METHODS=addFunction,removeFunction -sMODULARIZE=1 -sEXPORT_ES6=1)
Expand Down
8 changes: 4 additions & 4 deletions bindings/wasm/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ EMSCRIPTEN_BINDINGS(whatever) {
function("_intersectionN", &IntersectionN);
function("_Compose", &Manifold::Compose);

function("setMinCircularAngle", &Manifold::SetMinCircularAngle);
function("setMinCircularEdgeLength", &Manifold::SetMinCircularEdgeLength);
function("setCircularSegments", &Manifold::SetCircularSegments);
function("getCircularSegments", &Manifold::GetCircularSegments);
function("setMinCircularAngle", &Quality::SetMinCircularAngle);
function("setMinCircularEdgeLength", &Quality::SetMinCircularEdgeLength);
function("setCircularSegments", &Quality::SetCircularSegments);
function("getCircularSegments", &Quality::GetCircularSegments);
function("reserveIDs", &Manifold::ReserveIDs);
}
2 changes: 1 addition & 1 deletion bindings/wasm/manifold.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The Manifold Authors.
// Copyright 2022 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.
Expand Down
2 changes: 1 addition & 1 deletion samples/include/samples.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ Manifold Scallop();

Manifold GyroidModule(float size = 20, int n = 20);
/** @} */ // end of Samples
} // namespace manifold
} // namespace manifold
9 changes: 3 additions & 6 deletions samples/src/bracelet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cross_section.h"
#include "samples.h"

namespace {
Expand All @@ -23,12 +24,8 @@ Manifold Base(float width, float radius, float decorRadius, float twistRadius,
int nCut, int nDivision) {
Manifold base = Manifold::Cylinder(width, radius + twistRadius / 2);

Polygons circle(1);
float dPhiDeg = 180.0f / nDivision;
for (int i = 0; i < 2 * nDivision; ++i) {
circle[0].push_back({decorRadius * cosd(dPhiDeg * i) + twistRadius,
decorRadius * sind(dPhiDeg * i)});
}
CrossSection circle =
CrossSection::Circle(decorRadius, nDivision).Translate({twistRadius, 0});
Manifold decor = Manifold::Extrude(circle, width, nDivision, 180)
.Scale({1.0f, 0.5f, 1.0f})
.Translate({0.0f, radius, 0.0f});
Expand Down
12 changes: 4 additions & 8 deletions samples/src/knot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cross_section.h"
#include "samples.h"

namespace {
Expand All @@ -33,7 +34,7 @@ namespace manifold {
* @param minorRadius Radius of the small cross-section of the imaginary donut.
* @param threadRadius Radius of the small cross-section of the actual object.
* @param circularSegments Number of linear segments making up the threadRadius
* circle. Default is Manifold.GetCircularSegments().
* circle. Default is Quality.GetCircularSegments().
* @param linearSegments Number of segments along the length of the knot.
* Default makes roughly square facets.
*/
Expand All @@ -44,16 +45,11 @@ Manifold TorusKnot(int p, int q, float majorRadius, float minorRadius,
p /= kLoops;
q /= kLoops;
int n = circularSegments > 2 ? circularSegments
: Manifold::GetCircularSegments(threadRadius);
: Quality::GetCircularSegments(threadRadius);
int m =
linearSegments > 2 ? linearSegments : n * q * majorRadius / threadRadius;

Polygons circle(1);
float dPhi = 360.0f / n;
for (int i = 0; i < n; ++i) {
circle[0].push_back({cosd(dPhi * i) + 2.0f, sind(dPhi * i)});
}

CrossSection circle = CrossSection::Circle(1., n).Translate({2, 0});
Manifold knot = Manifold::Revolve(circle, m);

knot =
Expand Down
2 changes: 1 addition & 1 deletion samples/src/rounded_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace manifold {
* @param edgeLength Distance between the corners.
* @param radius Radius of the frame members.
* @param circularSegments Number of segments in the cylinders and spheres.
* Defaults to Manifold.GetCircularSegments().
* Defaults to Quality.GetCircularSegments().
*/
Manifold RoundedFrame(float edgeLength, float radius, int circularSegments) {
Manifold edge = Manifold::Cylinder(edgeLength, radius, -1, circularSegments);
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
add_subdirectory(third_party)
add_subdirectory(utilities)
add_subdirectory(collider)
add_subdirectory(cross_section)
add_subdirectory(polygon)
add_subdirectory(manifold)
add_subdirectory(sdf)
28 changes: 28 additions & 0 deletions src/cross_section/CMakeLists.txt
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)
167 changes: 167 additions & 0 deletions src/cross_section/include/cross_section.h
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
Loading

0 comments on commit a97c666

Please sign in to comment.