Giter VIP home page Giter VIP logo

ishape-swift / ishapetriangulation Goto Github PK

View Code? Open in Web Editor NEW
28.0 2.0 5.0 4.13 MB

Complex polygon triangulation. A fast O(n*log(n)) algorithm based on "Triangulation of monotone polygons". The result can be represented as a Delaunay triangulation.

License: MIT License

Ruby 0.25% Swift 99.41% Objective-C 0.08% Metal 0.26%
delaunay polygon triangulation mesh convex-polygons tesselation swift delaunay-triangulation monotone-polygons triangle-vertices

ishapetriangulation's Introduction

iShapeTriangulation

Complex polygon triangulation, tessellation and split into convex polygons. A fast O(n*log(n)) algorithm based on "Triangulation of monotone polygons". The result can be represented as a Delaunay triangulation.

Delaunay triangulation

Breaking into convex polygons

Triangulation with extra points

Tessellation

Centroid net

Important Update: Check Out My New Library!

I'm excited to introduce iTriangle, the latest evolution of this project. Not only does it retain most of the fantastic features of iShapeTriangulation, but it also brings significant improvements, including better support for self-intersection cases. Additionally, it offers more precise adherence to the Delaunay condition. I'll be dedicating my future efforts to this new library, so I highly recommend trying it out for the most up-to-date features and enhancements. Your feedback is always appreciated!

Features

๐Ÿ’ก Fast O(n*log(n)) algorithm based on "Triangulation of monotone polygons"

๐Ÿ’ก All code is written to suit "Data Oriented Design". No reference type like class, just structs.

๐Ÿ’ก Supports polygons with holes

๐Ÿ’ก Supports plain and Delaunay triangulation

๐Ÿ’ก Supports tesselation

๐Ÿ’ก Supports breaking into convex polygons

๐Ÿ’ก Supports building centroid net

๐Ÿ’ก Same points is not restricted

๐Ÿ’ก Polygon must not have self intersections

๐Ÿ’ก Use integer geometry for calculations

๐Ÿ’ก More then 100 tests


Basic Usage

Add import:

import iGeometry
import iShapeTriangulation

After that you need represent your polygon as an array of vertices listed in a clockwise direction. Let's have a look for an example of a cheese polygon.

    let path = [
        // vertices listed in clockwise direction
        Point(x: 0, y: 20),       // 0
        Point(x: 8, y: 10),       // 1
        Point(x: 7, y: 6),        // 2
        Point(x: 9, y: 1),        // 3
        Point(x: 13, y: -1),      // 4
        Point(x: 17, y: 1),       // 5
        Point(x: 26, y: -7),      // 6
        Point(x: 14, y: -15),     // 7
        Point(x: 0, y: -18),      // 8
        Point(x: -14, y: -15),    // 9
        Point(x: -25, y: -7),     // 10
        Point(x: -18, y: 0),      // 11
        Point(x: -16, y: -3),     // 12
        Point(x: -13, y: -4),     // 13
        Point(x: -8, y: -2),      // 14
        Point(x: -6, y: 2),       // 15
        Point(x: -7, y: 6),       // 16
        Point(x: -10, y: 8)       // 17
    ]

Then get an instance of a Triangulator class and triangulate your polygon. As the result you will get an array of indices on your vertices array. Where each triple are represent an indices of a triangle vertices.

    let triangulator = Triangulator()
    if let triangles = try? triangulator.triangulateDelaunay(points: path) {
        for i in 0..<triangles.count / 3 {
            let ai = triangles[3 * i]
            let bi = triangles[3 * i + 1]
            let ci = triangles[3 * i + 2]
            print("triangle \(i): (\(ai), \(bi), \(ci))")
        }
    }

The triple are always list vertices in a clock wise direction.

Lets look another example for a polygon with a hole. Now you need represent a hole as an array of vertices listed in counterclockwise direction

    let hole = [
        // vertices listed in counterclockwise direction
        Point(x: 2, y: 0),    // 18
        Point(x: -2, y: -2),  // 19
        Point(x: -4, y: -5),  // 20
        Point(x: -2, y: -9),  // 21
        Point(x: 2, y: -11),  // 22
        Point(x: 5, y: -9),   // 23
        Point(x: 7, y: -5),   // 24
        Point(x: 5, y: -2)    // 25
    ]
    
    let points = path + hole
    if let triangles = try? triangulator.triangulateDelaunay(points: points, hull: points[0..<path.count], holes: [points[path.count..<points.count]], extraPoints: nil) {
        for i in 0..<triangles.count / 3 {
            let ai = triangles[3 * i]
            let bi = triangles[3 * i + 1]
            let ci = triangles[3 * i + 2]
            print("triangle \(i): (\(ai), \(bi), \(ci))")
        }
    }

---

Installation

add imports:

import iGeometry
import iShapeTriangulation

Add the following to your Podfile:

pod 'iShapeTriangulation'

Add the following to your Cartfile:

github "iShape-Swift/iShapeTriangulation"

Add the following to your Package.swift:

let package = Package(
    name: "[your name]",
    products: [
        dependencies: [
            .package(url: "https://github.com/iShape-Swift/iShapeTriangulation", from: "1.0.2")
        ],
        targets: [
            .target(
                name: "[your target]",
                dependencies: ["iShapeTriangulation"])
        ]
    ]
)

Or add it directly throw .xcodeproj

ishapetriangulation's People

Contributors

nailxsharipov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ishapetriangulation's Issues

Declaration errors

I try to implement within a project using cocoapods but it throws errors at me. I don't know if I'm implementing it wrong or what happens but I have the following errors.
Import iShapeTriangulation & iGeometry into my project to be safe and nothing

image

Ignore unused points?

I'm getting this exception thrown from PlainShape.split(). Seems to happen when a polygon has two very nearby vertices.

                throw SplitError.unusedPoint

Is it safe to ignore the unused point by replacing the throw with the following code?

                i += 1
                continue nextNode

Any advice much appreciated!

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.