-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwrapper.go
190 lines (161 loc) · 4.88 KB
/
wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package triangle
/*
#include "triangle.h"
#include <stdlib.h>
*/
// #cgo LDFLAGS: -lm
import "C"
import "unsafe"
type triangulateIO struct {
ct *C.struct_triangulateio
}
func NewTriangulateIO() *triangulateIO {
t := triangulateIO{}
t.ct = (*C.struct_triangulateio)(C.malloc(C.sizeof_struct_triangulateio))
if t.ct == nil {
panic("Unable to allocate memory")
}
t.ct.edgelist = nil
t.ct.edgemarkerlist = nil
t.ct.holelist = nil
t.ct.neighborlist = nil
t.ct.normlist = nil
t.ct.numberofcorners = 0
t.ct.numberofedges = 0
t.ct.numberofholes = 0
t.ct.numberofpointattributes = 0
t.ct.numberofpoints = 0
t.ct.numberofregions = 0
t.ct.numberofsegments = 0
t.ct.numberoftriangleattributes = 0
t.ct.numberoftriangles = 0
t.ct.pointattributelist = nil
t.ct.pointlist = nil
t.ct.pointmarkerlist = nil
t.ct.regionlist = nil
t.ct.segmentlist = nil
t.ct.segmentmarkerlist = nil
t.ct.trianglearealist = nil
t.ct.triangleattributelist = nil
t.ct.trianglelist = nil
return &t
}
func FreeTriangulateIO(t *triangulateIO) {
C.free(unsafe.Pointer(t.ct))
}
func (t *triangulateIO) NumberOfEdges() int {
return int(t.ct.numberofedges)
}
func (t *triangulateIO) NumberOfHoles() int {
return int(t.ct.numberofholes)
}
func (t *triangulateIO) NumberOfPoints() int {
return int(t.ct.numberofpoints)
}
func (t *triangulateIO) NumberOfSegments() int {
return int(t.ct.numberofsegments)
}
func (t *triangulateIO) NumberOfTriangles() int {
return int(t.ct.numberoftriangles)
}
func (t *triangulateIO) Normals() [][2]float64 {
return cArrToFlt64Slice2D(unsafe.Pointer(t.ct.normlist), t.NumberOfEdges())
}
func (t *triangulateIO) Edges() [][2]int32 {
return cArrToIntSlice2D(unsafe.Pointer(t.ct.edgelist), t.NumberOfEdges())
}
func (t *triangulateIO) Points() [][2]float64 {
return cArrToFlt64Slice2D(unsafe.Pointer(t.ct.pointlist), t.NumberOfPoints())
}
func (t *triangulateIO) PointMarkers() []int32 {
return cArrToIntSlice(unsafe.Pointer(t.ct.pointmarkerlist), t.NumberOfPoints())
}
func (t *triangulateIO) Segments() [][2]int32 {
return cArrToIntSlice2D(unsafe.Pointer(t.ct.segmentlist), t.NumberOfSegments())
}
func (t *triangulateIO) SetEdges(edges [][2]int32) {
t.ct.edgelist = (*C.int)(unsafe.Pointer(&edges[0][0]))
t.ct.numberofedges = C.int(len(edges))
}
func (t *triangulateIO) SetPoints(pts [][2]float64) {
t.ct.pointlist = (*C.double)(unsafe.Pointer(&pts[0]))
t.ct.numberofpoints = C.int(len(pts))
}
func (t *triangulateIO) SetPointMarkers(markers []int32) {
t.ct.pointmarkerlist = (*C.int)(unsafe.Pointer(&markers[0]))
}
func (t *triangulateIO) SetSegments(segments [][2]int32) {
t.ct.segmentlist = (*C.int)(unsafe.Pointer(&segments[0][0]))
t.ct.numberofsegments = C.int(len(segments))
}
func (t *triangulateIO) SetSegmentMarkers(markers []int32) {
t.ct.segmentmarkerlist = (*C.int)(unsafe.Pointer(&markers[0]))
}
func (t *triangulateIO) SetTriangles(tri [][3]int32) {
t.ct.trianglelist = (*C.int)(unsafe.Pointer(&tri[0][0]))
}
func (t *triangulateIO) SetTriangleAreas(areas []float64) {
t.ct.trianglearealist = (*C.double)(unsafe.Pointer(&areas[0]))
}
func (t *triangulateIO) SetHoles(holes [][2]float64) {
t.ct.holelist = (*C.double)(unsafe.Pointer(&holes[0][0]))
t.ct.numberofholes = C.int(len(holes))
}
func (t *triangulateIO) Triangles() [][3]int32 {
return cArrToIntSlice3D(unsafe.Pointer(t.ct.trianglelist), t.NumberOfTriangles())
}
func triang(opt string, in, out, vorout *triangulateIO) {
copt := C.CString(opt)
defer C.free(unsafe.Pointer(copt))
if vorout == nil {
C.triangulate(copt, in.ct, out.ct, nil)
} else {
C.triangulate(copt, in.ct, out.ct, vorout.ct)
}
}
func cArrToIntSlice(ptr unsafe.Pointer, length int) []int32 {
slice := (*[1 << 30]C.int)(ptr)[:length:length]
result := make([]int32, length)
for i := 0; i < length; i++ {
result[i] = int32(slice[i])
}
return result
}
func cArrToIntSlice2D(ptr unsafe.Pointer, length int) [][2]int32 {
sz := length * 2
slice := (*[1 << 30]C.int)(ptr)[:sz:sz]
result := make([][2]int32, length)
for i := 0; i < length; i++ {
j := i * 2
result[i] = [2]int32{int32(slice[j]), int32(slice[j+1])}
}
return result
}
func cArrToIntSlice3D(ptr unsafe.Pointer, length int) [][3]int32 {
sz := length * 3
slice := (*[1 << 30]C.int)(ptr)[:sz:sz]
result := make([][3]int32, length)
for i := 0; i < length; i++ {
j := i * 3
result[i] = [3]int32{int32(slice[j]), int32(slice[j+1]), int32(slice[j+2])}
}
return result
}
func cArrToFlt64Slice(ptr unsafe.Pointer, length int) []float64 {
slice := (*[1 << 30]C.double)(ptr)[:length:length]
result := make([]float64, length)
for i := 0; i < length; i++ {
result[i] = float64(slice[i])
}
return result
}
func cArrToFlt64Slice2D(ptr unsafe.Pointer, length int) [][2]float64 {
sz := length * 2
slice := (*[1 << 30]C.double)(ptr)[:sz:sz]
result := make([][2]float64, length)
for i := 0; i < length; i++ {
j := i * 2
result[i] = [2]float64{float64(slice[j]), float64(slice[j+1])}
}
return result
}