-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape_ops.go
More file actions
717 lines (625 loc) · 18.1 KB
/
shape_ops.go
File metadata and controls
717 lines (625 loc) · 18.1 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
package topo
/*
#include <stdlib.h>
#include <string.h>
#include "topo_c_api.h"
#cgo CFLAGS: -I ./libs
#cgo linux CXXFLAGS: -I ./libs -std=gnu++14
#cgo darwin,amd64 CXXFLAGS: -I ./libs -std=gnu++14
#cgo darwin,arm64 CXXFLAGS: -I ./libs -std=gnu++14
#cgo windows CXXFLAGS: -I ./libs -std=gnu++14
*/
import "C"
import (
"errors"
"runtime"
"unsafe"
)
func Fuse(shapes []*Shape, tol float64, glue bool) *Shape {
count := len(shapes)
if count == 0 {
return nil
}
cShapes := make([]*C.struct__topo_shape_t, count)
for i, shp := range shapes {
cShapes[i] = shp.inner.val
}
result := C.topo_fuse(&cShapes[0], C.int(count), C.double(tol), C.bool(glue))
if result == nil {
return nil
}
return NewShape(result)
}
func Cut(shp, tool *Shape, tol float64, glue bool) *Shape {
result := C.topo_cut(shp.inner.val, tool.inner.val, C.double(tol), C.bool(glue))
if result == nil {
return nil
}
return NewShape(result)
}
func CutMulti(shp *Shape, toCuts []*Shape, tol float64, glue bool) *Shape {
count := len(toCuts)
if count == 0 {
return nil
}
cToCuts := make([]*C.struct__topo_shape_t, count)
for i, cut := range toCuts {
cToCuts[i] = cut.inner.val
}
result := C.topo_cut_multi(shp.inner.val, &cToCuts[0], C.int(count), C.double(tol), C.bool(glue))
if result == nil {
return nil
}
return NewShape(result)
}
func Intersect(shp, toIntersect *Shape, tol float64, glue bool) *Shape {
result := C.topo_intersect(shp.inner.val, toIntersect.inner.val, C.double(tol), C.bool(glue))
if result == nil {
return nil
}
return NewShape(result)
}
func IntersectMulti(shp *Shape, toIntersects []*Shape, tol float64, glue bool) *Shape {
count := len(toIntersects)
if count == 0 {
return nil
}
cToIntersects := make([]*C.struct__topo_shape_t, count)
for i, intersect := range toIntersects {
cToIntersects[i] = intersect.inner.val
}
result := C.topo_intersect_multi(shp.inner.val, &cToIntersects[0], C.int(count), C.double(tol), C.bool(glue))
if result == nil {
return nil
}
return NewShape(result)
}
func Split(shp *Shape, splitters []*Shape, tol float64) *Shape {
count := len(splitters)
if count == 0 {
return nil
}
cSplitters := make([]*C.struct__topo_shape_t, count)
for i, splitter := range splitters {
cSplitters[i] = splitter.inner.val
}
result := C.topo_split(shp.inner.val, &cSplitters[0], C.int(count), C.double(tol))
if result == nil {
return nil
}
return NewShape(result)
}
func Fill(shp *Shape, constraints []*Shape) *Shape {
count := len(constraints)
if count == 0 {
return nil
}
cConstraints := make([]*C.struct__topo_shape_t, count)
for i, constraint := range constraints {
cConstraints[i] = constraint.inner.val
}
result := C.topo_fill(shp.inner.val, &cConstraints[0], C.int(count))
if result == nil {
return nil
}
return NewShape(result)
}
func Shelling(shp *Shape, faces []*Face, thickness, tolerance float64, joinType int) *Shape {
count := len(faces)
if count == 0 {
return nil
}
cFaces := make([]C.struct__topo_face_t, count)
for i, face := range faces {
cFaces[i] = face.inner.val
}
result := C.topo_shelling(shp.inner.val, &cFaces[0], C.int(count),
C.double(thickness), C.double(tolerance), C.int(joinType))
if result == nil {
return nil
}
return NewShape(result)
}
func Fillet(shp *Shape, edges []*Edge, radius float64) *Shape {
count := len(edges)
if count == 0 {
return nil
}
cEdges := make([]C.struct__topo_edge_t, count)
for i, edge := range edges {
cEdges[i] = edge.inner.val
}
result := C.topo_fillet(shp.inner.val, &cEdges[0], C.int(count), C.double(radius))
if result == nil {
return nil
}
return NewShape(result)
}
func Chamfer(shp *Shape, edges []*Edge, distance1, distance2 float64, hasDistance2 bool) *Shape {
count := len(edges)
if count == 0 {
return nil
}
cEdges := make([]C.struct__topo_edge_t, count)
for i, edge := range edges {
cEdges[i] = edge.inner.val
}
result := C.topo_chamfer(shp.inner.val, &cEdges[0], C.int(count),
C.double(distance1), C.double(distance2), C.bool(hasDistance2))
if result == nil {
return nil
}
return NewShape(result)
}
func Extrude(shape *Shape, direction Vector3) *Shape {
result := C.topo_extrude(shape.inner.val, direction.val)
if result == nil {
return nil
}
return NewShape(result)
}
func ExtrudeLinear(wire *Wire, innerWires []*Wire, normal Vector3, taper float64) *Shape {
count := len(innerWires)
cInnerWires := make([]C.struct__topo_wire_t, count)
for i, innerWire := range innerWires {
cInnerWires[i] = innerWire.inner.val
}
result := C.topo_extrude_linear(wire.inner.val, &cInnerWires[0], C.int(count), normal.val, C.double(taper))
if result == nil {
return nil
}
return NewShape(result)
}
func ExtrudeLinearWithRotation(wire *Wire, innerWires []*Wire, center Point3, normal Vector3, angleDegrees float64) *Shape {
count := len(innerWires)
cInnerWires := make([]C.struct__topo_wire_t, count)
for i, innerWire := range innerWires {
cInnerWires[i] = innerWire.inner.val
}
result := C.topo_extrude_linear_with_rotation(wire.inner.val, &cInnerWires[0], C.int(count), center.val, normal.val, C.double(angleDegrees))
if result == nil {
return nil
}
return NewShape(result)
}
func Revolve(shape *Shape, axisPoint Point3, axisDirection Dir3, angleDegrees float64) *Shape {
result := C.topo_revolve(shape.inner.val, axisPoint.val, axisDirection.val, C.double(angleDegrees))
if result == nil {
return nil
}
return NewShape(result)
}
func RevolveWire(wire *Wire, innerWires []*Wire, angleDegrees float64, axisStart, axisEnd Point3) *Shape {
count := len(innerWires)
cInnerWires := make([]C.struct__topo_wire_t, count)
for i, innerWire := range innerWires {
cInnerWires[i] = innerWire.inner.val
}
result := C.topo_revolve_wire(wire.inner.val, &cInnerWires[0], C.int(count),
C.double(angleDegrees), axisStart.val, axisEnd.val)
if result == nil {
return nil
}
return NewShape(result)
}
type TransitionMode int
const (
TransitionTransformed TransitionMode = C.TRANSITION_TRANSFORMED
TransitionRound TransitionMode = C.TRANSITION_ROUND
TransitionRight TransitionMode = C.TRANSITION_RIGHT
)
func Sweep(wire *Wire, innerWires []*Wire, path *Shape, makeSolid, isFrenet bool,
mode *Shape, transition TransitionMode) *Shape {
count := len(innerWires)
cInnerWires := make([]C.struct__topo_wire_t, count)
for i, innerWire := range innerWires {
cInnerWires[i] = innerWire.inner.val
}
var cMode *C.struct__topo_shape_t
if mode != nil {
cMode = mode.inner.val
}
result := C.topo_sweep(wire.inner.val, &cInnerWires[0], C.int(count),
path.inner.val, C.bool(makeSolid), C.bool(isFrenet), cMode,
C.transition_mode_t(transition))
if result == nil {
return nil
}
return NewShape(result)
}
func SweepMulti(profiles []*Shape, path *Shape, makeSolid, isFrenet bool, mode *Shape) *Shape {
count := len(profiles)
cProfiles := make([]*C.struct__topo_shape_t, count)
for i, profile := range profiles {
cProfiles[i] = profile.inner.val
}
var cMode *C.struct__topo_shape_t
if mode != nil {
cMode = mode.inner.val
}
result := C.topo_sweep_multi(&cProfiles[0], C.int(count), path.inner.val,
C.bool(makeSolid), C.bool(isFrenet), cMode)
if result == nil {
return nil
}
return NewShape(result)
}
func Loft(profiles []*Shape, cap, ruled bool, continuity, parametrization string,
degree int, compat, smoothing bool, weights [3]float64) *Shape {
count := len(profiles)
cProfiles := make([]*C.struct__topo_shape_t, count)
for i, profile := range profiles {
cProfiles[i] = profile.inner.val
}
cContinuity := C.CString(continuity)
cParametrization := C.CString(parametrization)
defer func() {
C.free(unsafe.Pointer(cContinuity))
C.free(unsafe.Pointer(cParametrization))
}()
result := C.topo_loft(&cProfiles[0], C.int(count), C.bool(cap), C.bool(ruled),
cContinuity, cParametrization, C.int(degree), C.bool(compat),
C.bool(smoothing), (*C.double)(&weights[0]))
if result == nil {
return nil
}
return NewShape(result)
}
func Offset(shape *Shape, offset float64, cap, both bool, tol float64) *Shape {
result := C.topo_offset(shape.inner.val, C.double(offset), C.bool(cap),
C.bool(both), C.double(tol))
if result == nil {
return nil
}
return NewShape(result)
}
func Clean(shape *Shape) *Shape {
result := C.topo_clean(shape.inner.val)
if result == nil {
return nil
}
return NewShape(result)
}
func Closest(shape1, shape2 *Shape) (Point3, Point3) {
var p1, p2 C.pnt3d_t
C.topo_closest(shape1.inner.val, shape2.inner.val, &p1, &p2)
return Point3{val: p1}, Point3{val: p2}
}
func CombinedCenter(objects []*Shape) Point3 {
count := len(objects)
if count == 0 {
return Point3{}
}
cObjects := make([]*C.struct__topo_shape_t, count)
for i, obj := range objects {
cObjects[i] = obj.inner.val
}
result := C.topo_combined_center(&cObjects[0], C.int(count))
return Point3{val: result}
}
func CombinedCenterOfBoundBox(objects []*Shape) Point3 {
count := len(objects)
if count == 0 {
return Point3{}
}
cObjects := make([]*C.struct__topo_shape_t, count)
for i, obj := range objects {
cObjects[i] = obj.inner.val
}
result := C.topo_combined_center_of_bound_box(&cObjects[0], C.int(count))
return Point3{val: result}
}
func ReadShapeFromStepFile(f string) *Shape {
fl := C.CString(f)
defer C.free(unsafe.Pointer(fl))
res := C.read_shape_from_step_file(fl)
return NewShape(res)
}
func ReadShapesFromStepFile(filename string) ([]*Shape, []*TopoLocation, error) {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
var count C.int
cShapes := C.read_shapes_from_step_file(cFilename, &count)
if cShapes == nil || count == 0 {
return nil, nil, errors.New("failed to read shapes from STEP file")
}
defer C.free_shapes_from_step(cShapes, count)
shapesSlice := (*[1 << 30]*C.struct__topo_shape_and_location_t)(unsafe.Pointer(cShapes))[:count:count]
locatios := make([]*TopoLocation, int(count))
shapes := make([]*Shape, int(count))
for i := 0; i < int(count); i++ {
shapes[i] = NewShape(shapesSlice[i].shape)
locatios[i] = newTopoLocation(shapesSlice[i].location)
}
return shapes, locatios, nil
}
func WriteShapeToStepBuffer(shape *Shape) ([]byte, error) {
var bufferSize C.int
cBuffer := C.topo_shape_write_to_step_buffer(shape.inner.val, &bufferSize)
if cBuffer == nil {
return nil, errors.New("failed to write shape to STEP buffer")
}
defer C.free(unsafe.Pointer(cBuffer))
buffer := C.GoBytes(unsafe.Pointer(cBuffer), bufferSize)
return buffer, nil
}
type WireSamplePoint struct {
Position Point3
Tangent Vector3
Edge *Edge
}
func SampleWireAtDistances(wire *Wire, distances []float64) []WireSamplePoint {
count := len(distances)
if count == 0 {
return nil
}
var resultCount C.int
cDistances := make([]C.double, count)
for i, d := range distances {
cDistances[i] = C.double(d)
}
cSamples := C.topo_wire_sample_at_distances(
wire.inner.val,
&cDistances[0],
C.int(count),
&resultCount,
)
defer C.topo_wire_sample_list_free(cSamples, resultCount)
if cSamples == nil {
return nil
}
sampleSlice := (*[1 << 30]C.topo_wire_sample_point_t)(unsafe.Pointer(cSamples))[:resultCount:resultCount]
samples := make([]WireSamplePoint, resultCount)
for i := 0; i < int(resultCount); i++ {
e := &Edge{inner: &innerEdge{val: sampleSlice[i].edge}}
runtime.SetFinalizer(e.inner, (*innerEdge).free)
samples[i] = WireSamplePoint{
Position: Point3{val: sampleSlice[i].position},
Tangent: Vector3{val: sampleSlice[i].tangent},
Edge: e,
}
}
return samples
}
func ClipWireBetweenDistances(wire *Wire, startDistance, endDistance float64) *Wire {
result := C.topo_wire_clip_between_distances(
wire.inner.val,
C.double(startDistance),
C.double(endDistance),
)
if result.shp == nil {
return nil
}
w := &Wire{inner: &innerWire{result}}
runtime.SetFinalizer(w.inner, (*innerWire).free)
return w
}
type ProfileProjection struct {
inner C.topo_profile_projection_t
}
func (p *ProfileProjection) GetAxis2() Axis2 {
return Axis2{
val: p.inner.axes,
}
}
func (p *ProfileProjection) GetTrsf() Trsf {
return Trsf{
val: p.inner.trsf,
}
}
func (p *ProfileProjection) GeTangent() Vector3 {
return Vector3{
val: p.inner.tangent,
}
}
func (p *ProfileProjection) GePosition() Point3 {
return Point3{
val: p.inner.position,
}
}
func CalcProfileProjection(wire *Wire, up Dir3, offset *float64) ProfileProjection {
var cOffset *C.double
if offset != nil {
cOffset = (*C.double)(offset)
}
result := C.topo_calc_profile_projection(wire.inner.val, up.val, cOffset)
return ProfileProjection{inner: result}
}
func ProfileProjectPoint(proj *ProfileProjection, point Point3) Point3 {
result := C.topo_profile_project_point(&proj.inner, point.val)
return Point3{val: result}
}
func ProfileProjectPointList(proj *ProfileProjection, points []Point3) []Point3 {
count := len(points)
if count == 0 {
return nil
}
cPoints := make([]C.pnt3d_t, count)
for i, p := range points {
cPoints[i] = p.val
}
cResult := C.topo_profile_project_point_list(&proj.inner, &cPoints[0], C.int(count))
defer C.topo_profile_project_point_list_free(cResult)
if cResult == nil {
return nil
}
result := make([]Point3, count)
resultSlice := (*[1 << 30]C.pnt3d_t)(unsafe.Pointer(cResult))[:count:count]
for i := 0; i < count; i++ {
result[i] = Point3{val: resultSlice[i]}
}
return result
}
func WireLength(wire *Wire) float64 {
if wire == nil || wire.inner == nil {
return 0.0
}
return float64(C.topo_wrie_length(wire.inner.val))
}
func MakeCatenary(p1, p2 Point3, slack, maxSag float64, up Dir3, tessellation float64) []Point3 {
var count C.int
cPoints := C.topo_make_catenary(
p1.val,
p2.val,
C.double(slack),
C.double(maxSag),
up.val,
C.double(tessellation),
&count,
)
defer C.topo_free_catenary_points(cPoints)
if cPoints == nil || count == 0 {
return nil
}
points := make([]Point3, int(count))
pointsSlice := (*[1 << 30]C.pnt3d_t)(unsafe.Pointer(cPoints))[:count:count]
for i := 0; i < int(count); i++ {
points[i] = Point3{val: pointsSlice[i]}
}
return points
}
type ProgressType int
const (
ProgressByRatio ProgressType = 0
ProgressByDistance ProgressType = 1
)
type WorkProgress struct {
Direction *Dir3
Radius *float64
Original *Wire
Points *[]Point3
Type ProgressType
Range [2]float64
}
func ClipWithTopo4D(shape *Shape, progress WorkProgress) *Shape {
if shape == nil || shape.inner == nil {
return nil
}
param := C.malloc(C.size_t(unsafe.Sizeof(C.work_progress_params_t{})))
C.memset(param, 0, C.size_t(unsafe.Sizeof(C.work_progress_params_t{})))
defer C.free(param)
params := (*C.work_progress_params_t)(param)
params._type = C.progress_type_t(progress.Type)
params._range = [2]C.double{C.double(progress.Range[0]), C.double(progress.Range[1])}
if progress.Direction != nil {
params.direction = &progress.Direction.val
}
if progress.Radius != nil {
params.radius = (*C.double)(progress.Radius)
}
if progress.Original != nil && progress.Original.inner != nil {
params.original_path = &progress.Original.inner.val
}
if progress.Points != nil {
points := make([]C.pnt3d_t, len((*progress.Points)))
for i, p := range *progress.Points {
points[i] = p.val
}
params.points = &points[0]
params.point_count = C.int(len(*progress.Points))
}
result := C.topo_clip_with_4d(shape.inner.val, params)
if result == nil {
return nil
}
return NewShape(result)
}
func FitCenterlineFromShape(shape *Shape, numSamples int, smoothingFactor float64) *Wire {
if shape == nil {
return nil
}
result := C.topo_fit_centerline_from_shape(shape.inner.val, C.int(numSamples), C.double(smoothingFactor))
if result.shp == nil {
return nil
}
w := &Wire{inner: &innerWire{result}}
runtime.SetFinalizer(w.inner, (*innerWire).free)
return w
}
func CenterlinePointsToWire(points []Point3) *Wire {
if len(points) == 0 {
return nil
}
cPoints := make([]C.pnt3d_t, len(points))
for i, p := range points {
cPoints[i] = p.val
}
result := C.topo_centerline_points_to_wire(&cPoints[0], C.int(len(points)))
if result.shp == nil {
return nil
}
w := &Wire{inner: &innerWire{result}}
runtime.SetFinalizer(w.inner, (*innerWire).free)
return w
}
func ComputeShapeMaxRadiusFromCenterline(shape *Shape, centerline *Wire) float64 {
if shape == nil || centerline == nil {
return -1.0
}
return float64(C.topo_compute_shape_max_radius_from_centerline(shape.inner.val, centerline.inner.val))
}
func SampleCenterlineWire(centerline *Wire, numSamples int, simplify bool) []Point3 {
if centerline == nil {
return nil
}
var count C.int
cPoints := C.topo_sample_centerline_wire(centerline.inner.val, C.int(numSamples), C.bool(simplify), &count)
defer C.topo_free_points(cPoints)
if cPoints == nil || count == 0 {
return nil
}
points := make([]Point3, int(count))
pointsSlice := (*[1 << 30]C.pnt3d_t)(unsafe.Pointer(cPoints))[:count:count]
for i := 0; i < int(count); i++ {
points[i] = Point3{val: pointsSlice[i]}
}
return points
}
func CreateBoundingCenterlineShape(radius float64, path *Wire) *Shape {
if path == nil {
return nil
}
result := C.topo_create_bounding_centerline_shape(C.double(radius), path.inner.val)
if result == nil {
return nil
}
return NewShape(result)
}
func GetShapeOutline(shape *Shape, numSamples int, simplify bool) [][]Point3 {
if shape == nil {
return nil
}
var (
cOutlines **C.pnt3d_t
cOutlineSizes *C.int
cOutlineCount C.int
)
C.topo_shape_get_outline(
shape.inner.val,
C.int(numSamples),
C.bool(simplify),
&cOutlines,
&cOutlineSizes,
&cOutlineCount,
)
defer C.topo_free_outline_points(cOutlines, cOutlineSizes, cOutlineCount)
if cOutlines == nil || cOutlineSizes == nil || cOutlineCount == 0 {
return nil
}
outlineCount := int(cOutlineCount)
result := make([][]Point3, outlineCount)
outlinesArray := (*[1 << 30]*C.pnt3d_t)(unsafe.Pointer(cOutlines))[:outlineCount:outlineCount]
sizesArray := (*[1 << 30]C.int)(unsafe.Pointer(cOutlineSizes))[:outlineCount:outlineCount]
for i := 0; i < outlineCount; i++ {
pointCount := int(sizesArray[i])
if pointCount > 0 {
pointsArray := (*[1 << 30]C.pnt3d_t)(unsafe.Pointer(outlinesArray[i]))[:pointCount:pointCount]
points := make([]Point3, pointCount)
for j := 0; j < pointCount; j++ {
points[j] = Point3{val: pointsArray[j]}
}
result[i] = points
}
}
return result
}