Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/luizzak/libtessswift
Libtess2 tesselation/triangulation library wrapper for Swift
https://github.com/luizzak/libtessswift
cocoapod swift tesselation triangulation
Last synced: 9 days ago
JSON representation
Libtess2 tesselation/triangulation library wrapper for Swift
- Host: GitHub
- URL: https://github.com/luizzak/libtessswift
- Owner: LuizZak
- License: mit
- Created: 2017-02-28T15:20:42.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-24T10:43:37.000Z (over 4 years ago)
- Last Synced: 2024-03-15T07:03:33.490Z (8 months ago)
- Topics: cocoapod, swift, tesselation, triangulation
- Language: C
- Homepage:
- Size: 685 KB
- Stars: 15
- Watchers: 5
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LibTessSwift
[![Build Status](https://dev.azure.com/luiz-fs/LibTessSwift/_apis/build/status/LuizZak.LibTessSwift?branchName=master)](https://dev.azure.com/luiz-fs/LibTessSwift/_build/latest?definitionId=1&branchName=master)
A Swift wrapper on top of [Libtess2](https://github.com/memononen/Libtess2) for polygon triangulation.
Tests where derived from [LibTessDotNet](https://github.com/speps/LibTessDotNet) library, which is also a port of the library above.
Also, a fix for an issue of 0-area polygons from libtess2 that was fixed by LibTessDotNet is also merged in.
Supports self-intersecting polygons and polygons with holes.
## Sample Usage
This is a sample wrapper over TessC to process polygons and return resultin vertices/indices pairs.
```swift
func process(polygon: [MyVector]) throws -> (vertices: [MyVector], indices: [Int])? {
let polygon: [MyVector] = ... // This should be an array of vertices - must have at least an `x` and `y` coordinate pairs!guard let tess = TessC() else {
print("Something went wrong while initializing TessC! :c")
return nil
}// Size of polygon (number of points per face)
let polySize = 3// Map from MyVector to CVector3 (LibTessSwift's vector representation)
let contour = polygon.map {
CVector3(x: TESSreal($0.x), y: TESSreal($0.y), z: 0.0)
}// Add the contour to LibTess
tess.addContour(contour)// Tesselate - if no errors are thrown, we're good!
try tess.tessellate(windingRule: .evenOdd, elementType: .polygons, polySize: polySize)// Collect resulting vector
var result: [MyVector] = []
var indices: [Int] = []
// Extract vertices
for vertex in tess.vertices! {
result.append(MyVector(x: vertex.x, y: vertex.y))
}
// Extract each index for each polygon triangle found
for i in 0..