Giter Site home page Giter Site logo

sprinlittlewater / convexdecomposition Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mjjq/convexdecomposition

0.0 0.0 0.0 21 KB

Header only C++ library for decomposing and slicing concave polygons into convex polygons

License: MIT License

C++ 100.00%

convexdecomposition's Introduction

ConvexDecomposition

What does this library do?

This is a header only C++ library, made for decomposing and slicing concave polygons into convex polygons. This library also allows for user defined polygon slicing (see usage).

The ConcavePolygon class uses a recursive data structure to either store 0 or 2 sub polygons. When a polygon is sliced, two sub polygons are generated split along a defined line segment.

Credits: The algorithm for decomposing concave polygons to convex can be found here: https://mpen.ca/406/bayazit (Mark Bayazit). Convex decomposition is achieved in O(n*r) time, where n is the number of polygon vertices, r is the number of reflex polygon vertices.

Installation

To install this library, simply copy ConcavePolygon.h into your project and #include "ConcavePolygon.h".

Usage

Example: Creating a concave polygon, decomposing, and acquiring convex subpolygons

#include "ConcavePolygon.h"

#include <vector>

int main()
{
    // Create a vector of vertices
    std::vector<cxd::Vertex > vertices =
    {
        cxd::Vec2({0.0f, 1.0f}),
        cxd::Vec2({-1.0f, 0.0f}),
        cxd::Vec2({0.0f, 0.5f}),
        cxd::Vec2({1.0f, 0.0f})
    };

    // Create polygon from these vertices
    cxd::ConcavePolygon concavePoly(vertices);

    // Perform convex decomposition on polygon
    concavePoly.convexDecomp();

    // Retrieve a decomposed convex subpolygon by index
    // We still use the concave poly type here
    cxd::ConcavePolygon subPolygon = concavePoly.getSubPolygon(0);

    // Retrieve vertices from the subpolygon (also applies to the
    // concave polygon we defined earlier)
    std::vector<cxd::Vertex > subPolyVerts = subPolygon.getVertices();

    // Create a vector and retrieve all convex subpolygons
    // as a single list
    std::vector<cxd::ConcavePolygon > subPolygonList;
    concavePoly.returnLowestLevelPolys(subPolygonList);


    return 0;
}

Example: Creating a polygon and slicing it along a defined line segment

#include "ConcavePolygon.h"

#include <vector>

int main()
{
    // Create a vector of vertices
    std::vector<cxd::Vertex > vertices =
    {
        cxd::Vec2({0.0f, 1.0f}),
        cxd::Vec2({-1.0f, 0.0f}),
        cxd::Vec2({0.0f, -1.0f}),
        cxd::Vec2({1.0f, 0.0f})
    };

    // Create polygon from these vertices
    cxd::ConcavePolygon concavePoly(vertices);

    // Create a valid line segment to slice the polygon.
    // Note: The line segment must pass through two polygon
    // edges in order to slice. Here we define the start
    // and end positions of the line segment.
    cxd::LineSegment line(cxd::Vec2({-2.0f, 0.0f}),
                          cxd::Vec2({ 2.0f, 0.0f}));

    // Slice the polygon along this line segment
    concavePoly.slicePolygon(line);

    // Retrieve a decomposed subpolygons by index
    // This works exactly the same when retrieving subpolygons
    // after convex decomposition
    cxd::ConcavePolygon subPolygon = concavePoly.getSubPolygon(0);

    // Retrieve vertices from the subpolygon (also applies to the
    // concave polygon we defined earlier)
    std::vector<cxd::Vertex > subPolyVerts = subPolygon.getVertices();

    // Create a vector and retrieve all convex subpolygons
    // as a single list
    std::vector<cxd::ConcavePolygon > subPolygonList;
    concavePoly.returnLowestLevelPolys(subPolygonList);

    return 0;
}

convexdecomposition's People

Contributors

mjjq avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.