forked from mthh/sfcgal-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.rs
2357 lines (1789 loc) · 79.3 KB
/
geometry.rs
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::{
ffi::{c_void, CString},
mem::MaybeUninit,
os::raw::c_char,
ptr::NonNull,
};
use num_traits::FromPrimitive;
use sfcgal_sys::{
initialize, sfcgal_alloc_handler_t, sfcgal_error_handler_t, sfcgal_free_handler_t,
sfcgal_geometry_alpha_shapes, sfcgal_geometry_approximate_medial_axis, sfcgal_geometry_area,
sfcgal_geometry_area_3d, sfcgal_geometry_as_text, sfcgal_geometry_as_text_decim,
sfcgal_geometry_clone, sfcgal_geometry_collection_add_geometry,
sfcgal_geometry_collection_create, sfcgal_geometry_collection_geometry_n,
sfcgal_geometry_collection_num_geometries, sfcgal_geometry_convexhull,
sfcgal_geometry_convexhull_3d, sfcgal_geometry_covers, sfcgal_geometry_covers_3d,
sfcgal_geometry_delete, sfcgal_geometry_difference, sfcgal_geometry_difference_3d,
sfcgal_geometry_distance, sfcgal_geometry_distance_3d, sfcgal_geometry_extrude,
sfcgal_geometry_extrude_polygon_straight_skeleton, sfcgal_geometry_extrude_straight_skeleton,
sfcgal_geometry_intersection, sfcgal_geometry_intersection_3d, sfcgal_geometry_intersects,
sfcgal_geometry_intersects_3d, sfcgal_geometry_is_3d, sfcgal_geometry_is_empty,
sfcgal_geometry_is_measured, sfcgal_geometry_is_planar, sfcgal_geometry_is_valid,
sfcgal_geometry_is_valid_detail, sfcgal_geometry_line_sub_string,
sfcgal_geometry_minkowski_sum, sfcgal_geometry_offset_polygon,
sfcgal_geometry_optimal_alpha_shapes, sfcgal_geometry_orientation,
sfcgal_geometry_straight_skeleton, sfcgal_geometry_straight_skeleton_distance_in_m,
sfcgal_geometry_t, sfcgal_geometry_tesselate, sfcgal_geometry_triangulate_2dz,
sfcgal_geometry_type_id, sfcgal_geometry_union, sfcgal_geometry_union_3d,
sfcgal_geometry_volume, sfcgal_io_read_wkt, sfcgal_multi_linestring_create,
sfcgal_multi_point_create, sfcgal_multi_polygon_create, sfcgal_prepared_geometry_t, srid_t,
BufferType,
};
use sfcgal_sys::{
/* sfcgal_solid_set_exterior_shell, */
sfcgal_approx_convex_partition_2, sfcgal_full_version, sfcgal_geometry_as_hexwkb,
sfcgal_geometry_as_obj, sfcgal_geometry_as_obj_file, sfcgal_geometry_as_vtk,
sfcgal_geometry_as_vtk_file, sfcgal_geometry_as_wkb, sfcgal_geometry_buffer3d,
sfcgal_geometry_force_lhr, sfcgal_geometry_force_rhr, sfcgal_geometry_force_valid,
sfcgal_geometry_has_validity_flag, sfcgal_geometry_make_solid, sfcgal_geometry_rotate,
sfcgal_geometry_rotate_2d, sfcgal_geometry_rotate_3d, sfcgal_geometry_rotate_3d_around_center,
sfcgal_geometry_rotate_x, sfcgal_geometry_rotate_y, sfcgal_geometry_rotate_z,
sfcgal_geometry_round, sfcgal_geometry_scale, sfcgal_geometry_scale_3d,
sfcgal_geometry_scale_3d_around_center, sfcgal_geometry_straight_skeleton_partition,
sfcgal_geometry_translate_2d, sfcgal_geometry_translate_3d, sfcgal_geometry_visibility_point,
sfcgal_geometry_visibility_segment, sfcgal_greene_approx_convex_partition_2, sfcgal_init,
sfcgal_io_read_binary_prepared, sfcgal_io_read_ewkt, sfcgal_io_read_wkb,
sfcgal_io_write_binary_prepared, sfcgal_linestring_add_point, sfcgal_linestring_create,
sfcgal_linestring_num_points, sfcgal_linestring_point_n, sfcgal_multi_solid_create,
sfcgal_optimal_convex_partition_2, sfcgal_point_create, sfcgal_point_create_from_xy,
sfcgal_point_create_from_xym, sfcgal_point_create_from_xyz, sfcgal_point_create_from_xyzm,
sfcgal_point_m, sfcgal_point_x, sfcgal_point_y, sfcgal_point_z,
sfcgal_polygon_add_interior_ring, sfcgal_polygon_create,
sfcgal_polygon_create_from_exterior_ring, sfcgal_polygon_exterior_ring,
sfcgal_polygon_interior_ring_n, sfcgal_polygon_num_interior_rings,
sfcgal_polyhedral_surface_add_polygon, sfcgal_polyhedral_surface_create,
sfcgal_polyhedral_surface_num_polygons, sfcgal_polyhedral_surface_polygon_n,
sfcgal_prepared_geometry_as_ewkt, sfcgal_prepared_geometry_create,
sfcgal_prepared_geometry_create_from_geometry, sfcgal_prepared_geometry_delete,
sfcgal_prepared_geometry_geometry, sfcgal_prepared_geometry_set_geometry,
sfcgal_prepared_geometry_set_srid, sfcgal_prepared_geometry_srid, sfcgal_set_alloc_handlers,
sfcgal_set_error_handlers, sfcgal_set_geometry_validation, sfcgal_solid_add_interior_shell,
sfcgal_solid_create, sfcgal_solid_create_from_exterior_shell, sfcgal_solid_num_shells,
sfcgal_solid_shell_n, sfcgal_triangle_create, sfcgal_triangle_create_from_points,
sfcgal_triangle_set_vertex, sfcgal_triangle_set_vertex_from_xy,
sfcgal_triangle_set_vertex_from_xyz, sfcgal_triangle_vertex,
sfcgal_triangulated_surface_add_triangle, sfcgal_triangulated_surface_create,
sfcgal_triangulated_surface_num_triangles, sfcgal_triangulated_surface_triangle_n,
sfcgal_version, sfcgal_y_monotone_partition_2,
};
use crate::{
conversion::{CoordSeq, CoordType, ToSFCGALGeom},
errors::get_last_error,
utils::{
_c_string_with_size, _string, check_computed_value, check_nan_value,
check_null_prepared_geom, check_predicate,
},
Result, ToSFCGAL,
};
/// SFCGAL Geometry types.
///
/// Indicates the type of shape represented by a `SFCGeometry`.
/// ([C API reference](https://oslandia.github.io/SFCGAL/doxygen/group__capi.html#ga1afcf1fad6c2daeca001481b125b84c6))
#[repr(C)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Primitive)]
pub enum GeomType {
Point = 1,
Linestring = 2,
Polygon = 3,
Multipoint = 4,
Multilinestring = 5,
Multipolygon = 6,
Geometrycollection = 7,
Polyhedralsurface = 15,
Triangulatedsurface = 16,
Triangle = 17,
Solid = 101,
Multisolid = 102,
}
impl GeomType {
fn is_collection_type(&self) -> bool {
matches!(
&self,
GeomType::Multipoint
| GeomType::Multilinestring
| GeomType::Multipolygon
| GeomType::Multisolid
| GeomType::Geometrycollection
)
}
}
/// Represents the orientation of a `SFCGeometry`.
#[derive(PartialEq, Eq, Debug, Primitive)]
pub enum Orientation {
CounterClockWise = -1isize,
ClockWise = 1isize,
Undetermined = 0isize,
}
/// Object representing a SFCGAL Geometry.
///
/// Most of the operations allowed by SFCGAL C API are wrapped,
/// except those modifying the geometry in-place (such as adding a new
/// point to a linestring for example) and those retrieving a specific part
/// of a geometry (such as getting the 2nd interior ring of some polygon as a
/// linestring). However, this can easily be done by yourself by converting them
/// from/to coordinates with the `new_from_coordinates` and `to_coordinates`
/// methods.
///
/// ([C API reference](https://oslandia.github.io/SFCGAL/doxygen/group__capi.html#gadd6d3ea5a71a957581248791624fad58))
#[repr(C)]
pub struct SFCGeometry {
pub(crate) c_geom: NonNull<sfcgal_geometry_t>,
pub(crate) owned: bool,
}
impl Drop for SFCGeometry {
fn drop(&mut self) {
if self.owned {
unsafe { sfcgal_geometry_delete(self.c_geom.as_mut()) }
}
}
}
impl Clone for SFCGeometry {
fn clone(&self) -> SFCGeometry {
SFCGeometry {
c_geom: NonNull::new(unsafe { sfcgal_geometry_clone(self.c_geom.as_ref()) }).unwrap(),
owned: true,
}
}
}
impl std::fmt::Debug for SFCGeometry {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.to_wkt_decim(8).unwrap())
}
}
impl SFCGeometry {
/// Create a geometry by parsing a [WKT](https://en.wikipedia.org/wiki/Well-known_text) string.
pub fn new(wkt: &str) -> Result<SFCGeometry> {
initialize();
let c_str = CString::new(wkt)?;
let obj = unsafe { sfcgal_io_read_wkt(c_str.as_ptr(), wkt.len()) };
unsafe { SFCGeometry::new_from_raw(obj, true) }
}
pub(crate) unsafe fn new_from_raw(
g: *mut sfcgal_geometry_t,
owned: bool,
) -> Result<SFCGeometry> {
Ok(SFCGeometry {
owned,
c_geom: NonNull::new(g).ok_or_else(|| {
format_err!(
"Obtained null pointer when creating geometry: {}",
get_last_error()
)
})?,
})
}
pub fn new_from_coordinates<T>(coords: &CoordSeq<T>) -> Result<SFCGeometry>
where
T: ToSFCGALGeom + CoordType,
{
coords.to_sfcgal()
}
/// Returns a WKT representation of the given `SFCGeometry` using CGAL
/// exact integer fractions as coordinate values. ([C API reference](https://sfcgal.gitlab.io/SFCGAL/doxygen/group__capi.html#ga3bc1954e3c034b60f0faff5e8227c398))
pub fn to_wkt(&self) -> Result<String> {
let mut ptr = MaybeUninit::<*mut c_char>::uninit();
let mut length: usize = 0;
unsafe {
sfcgal_geometry_as_text(self.c_geom.as_ref(), ptr.as_mut_ptr(), &mut length);
Ok(_c_string_with_size(ptr.assume_init(), length))
}
}
/// Returns a WKT representation of the given `SFCGeometry` using
/// floating point coordinate values with the desired number of
/// decimals. ([C API reference](https://sfcgal.gitlab.io/SFCGAL/doxygen/group__capi.html#gaaf23f2c95fd48810beb37d07a9652253))
pub fn to_wkt_decim(&self, nb_decim: i32) -> Result<String> {
let mut ptr = MaybeUninit::<*mut c_char>::uninit();
let mut length: usize = 0;
unsafe {
sfcgal_geometry_as_text_decim(
self.c_geom.as_ref(),
nb_decim,
ptr.as_mut_ptr(),
&mut length,
);
Ok(_c_string_with_size(ptr.assume_init(), length))
}
}
/// Test if the given `SFCGeometry` is empty or not.
pub fn is_empty(&self) -> Result<bool> {
let rv = unsafe { sfcgal_geometry_is_empty(self.c_geom.as_ptr()) };
check_predicate(rv)
}
/// Test if the given `SFCGeometry` is valid or not.
pub fn is_valid(&self) -> Result<bool> {
let rv = unsafe { sfcgal_geometry_is_valid(self.c_geom.as_ptr()) };
check_predicate(rv)
}
/// Test if the given `SFCGeometry` is measured (has an 'm' coordinates)
pub fn is_measured(&self) -> Result<bool> {
let rv = unsafe { sfcgal_geometry_is_measured(self.c_geom.as_ptr()) };
check_predicate(rv)
}
/// Test if the given `SFCGeometry` is planar or not.
pub fn is_planar(&self) -> Result<bool> {
let rv = unsafe { sfcgal_geometry_is_planar(self.c_geom.as_ptr()) };
check_predicate(rv)
}
/// Test if the given `SFCGeometry` is a 3d geometry or not.
pub fn is_3d(&self) -> Result<bool> {
let rv = unsafe { sfcgal_geometry_is_3d(self.c_geom.as_ptr()) };
check_predicate(rv)
}
/// Returns reason for the invalidity or None in case of validity.
pub fn validity_detail(&self) -> Result<Option<String>> {
let mut ptr = MaybeUninit::<*mut c_char>::uninit();
unsafe {
let rv = sfcgal_geometry_is_valid_detail(
self.c_geom.as_ptr(),
ptr.as_mut_ptr(),
std::ptr::null::<sfcgal_geometry_t>() as *mut *mut sfcgal_geometry_t,
);
match rv {
1 => Ok(None),
0 => Ok(Some(_string(ptr.assume_init()))),
_ => Err(format_err!("SFCGAL error: {}", get_last_error())),
}
}
}
/// Returns the SFCGAL type of the given `SFCGeometry`.
pub fn _type(&self) -> Result<GeomType> {
let type_geom = unsafe { sfcgal_geometry_type_id(self.c_geom.as_ptr()) };
GeomType::from_u32(type_geom)
.ok_or_else(|| format_err!("Unknown geometry type (val={})", type_geom))
}
/// Computes the distance to an other `SFCGeometry`.
pub fn distance(&self, other: &SFCGeometry) -> Result<f64> {
let distance =
unsafe { sfcgal_geometry_distance(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
check_computed_value(distance)
}
/// Computes the 3d distance to an other `SFCGeometry`.
pub fn distance_3d(&self, other: &SFCGeometry) -> Result<f64> {
let distance =
unsafe { sfcgal_geometry_distance_3d(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
check_computed_value(distance)
}
/// Computes the area of the given `SFCGeometry`.
pub fn area(&self) -> Result<f64> {
let area = unsafe { sfcgal_geometry_area(self.c_geom.as_ptr()) };
check_computed_value(area)
}
/// Computes the 3d area of the given `SFCGeometry`.
pub fn area_3d(&self) -> Result<f64> {
let area = unsafe { sfcgal_geometry_area_3d(self.c_geom.as_ptr()) };
check_computed_value(area)
}
/// Computes the volume of the given `SFCGeometry` (must be a volume).
pub fn volume(&self) -> Result<f64> {
let volume = unsafe { sfcgal_geometry_volume(self.c_geom.as_ptr()) };
check_computed_value(volume)
}
/// Computes the orientation of the given `SFCGeometry` (must be a
/// Polygon)
pub fn orientation(&self) -> Result<Orientation> {
let orientation = unsafe { sfcgal_geometry_orientation(self.c_geom.as_ptr()) };
Orientation::from_i32(orientation)
.ok_or_else(|| format_err!("Error while retrieving orientation (val={})", orientation))
}
/// Test the intersection with an other `SFCGeometry`.
pub fn intersects(&self, other: &SFCGeometry) -> Result<bool> {
let rv = unsafe { sfcgal_geometry_intersects(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
check_predicate(rv)
}
/// Test the 3d intersection with an other `SFCGeometry`.
pub fn intersects_3d(&self, other: &SFCGeometry) -> Result<bool> {
let rv =
unsafe { sfcgal_geometry_intersects_3d(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
check_predicate(rv)
}
/// Returns the intersection of the given `SFCGeometry` to an other
/// `SFCGeometry`.
pub fn intersection(&self, other: &SFCGeometry) -> Result<SFCGeometry> {
let result =
unsafe { sfcgal_geometry_intersection(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the 3d intersection of the given `SFCGeometry` to an other
/// `SFCGeometry`.
pub fn intersection_3d(&self, other: &SFCGeometry) -> Result<SFCGeometry> {
let result =
unsafe { sfcgal_geometry_intersection_3d(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Tests the coverage of geom1 and geom2
pub fn covers(&self, other: &SFCGeometry) -> Result<bool> {
let rv = unsafe { sfcgal_geometry_covers(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
check_predicate(rv)
}
/// Tests the 3D coverage of geom1 and geom2
pub fn covers_3d(&self, other: &SFCGeometry) -> Result<bool> {
let rv = unsafe { sfcgal_geometry_covers_3d(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
check_predicate(rv)
}
/// Returns the difference of the given `SFCGeometry` to an other
/// `SFCGeometry`.
pub fn difference(&self, other: &SFCGeometry) -> Result<SFCGeometry> {
let result =
unsafe { sfcgal_geometry_difference(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the 3d difference of the given `SFCGeometry` to an other
/// `SFCGeometry`.
pub fn difference_3d(&self, other: &SFCGeometry) -> Result<SFCGeometry> {
let result =
unsafe { sfcgal_geometry_difference_3d(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the union of the given `SFCGeometry` to an other
/// `SFCGeometry`.
pub fn union(&self, other: &SFCGeometry) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_union(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the 3d union of the given `SFCGeometry` to an other
/// `SFCGeometry`.
pub fn union_3d(&self, other: &SFCGeometry) -> Result<SFCGeometry> {
let result =
unsafe { sfcgal_geometry_union_3d(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the minkowski sum of the given `SFCGeometry` and an other
/// `SFCGEOMETRY`. ([C API reference](https://oslandia.github.io/SFCGAL/doxygen/group__capi.html#ga02d35888dac40eee2eb2a2b133979c8d))
pub fn minkowski_sum(&self, other: &SFCGeometry) -> Result<SFCGeometry> {
let result =
unsafe { sfcgal_geometry_minkowski_sum(self.c_geom.as_ptr(), other.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the straight skeleton of the given `SFCGeometry`.
/// ([C API reference](https://sfcgal.gitlab.io/SFCGAL/doxygen/group__capi.html#gaefaa76b61d66e2ad11d902e6b5a13635))
pub fn straight_skeleton(&self) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_straight_skeleton(self.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the straight skeleton of the given `SFCGeometry` with the
/// distance to the border as M coordinate. ([C API reference](https://sfcgal.gitlab.io/SFCGAL/doxygen/group__capi.html#ga972ea9e378eb2dc99c00b6ad57d05e88))
pub fn straight_skeleton_distance_in_m(&self) -> Result<SFCGeometry> {
let result =
unsafe { sfcgal_geometry_straight_skeleton_distance_in_m(self.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the extrude straight skeleton of the given Polygon.
/// ([C API reference](https://sfcgal.gitlab.io/SFCGAL/doxygen/group__capi.html#ga5389fd88daf80a8221a3ca619813a2be))
pub fn extrude_straight_skeleton(&self, height: f64) -> Result<SFCGeometry> {
let result =
unsafe { sfcgal_geometry_extrude_straight_skeleton(self.c_geom.as_ptr(), height) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the union of the polygon z-extrusion (with respect to
/// building_height) and the extrude straight skeleton (with
/// respect to roof_height) of the given Polygon. ([C API reference](https://sfcgal.gitlab.io/SFCGAL/doxygen/group__capi.html#ga5389fd88daf80a8221a3ca619813a2be))
pub fn extrude_polygon_straight_skeleton(
&self,
building_height: f64,
roof_height: f64,
) -> Result<SFCGeometry> {
let result = unsafe {
sfcgal_geometry_extrude_polygon_straight_skeleton(
self.c_geom.as_ptr(),
building_height,
roof_height,
)
};
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the approximate medial axis for the given `SFCGeometry`
/// Polygon. ([C API reference](https://sfcgal.gitlab.io/SFCGAL/doxygen/group__capi.html#ga16a9b4b1211843f8444284b1fefebc46))
pub fn approximate_medial_axis(&self) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_approximate_medial_axis(self.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the offset polygon of the given `SFCGeometry`.
/// ([C API reference](https://sfcgal.gitlab.io/SFCGAL/doxygen/group__capi.html#ga9766f54ebede43a9b71fccf1524a1054))
pub fn offset_polygon(&self, radius: f64) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_offset_polygon(self.c_geom.as_ptr(), radius) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the extrusion of the given `SFCGeometry` (not supported on
/// Solid and Multisolid). ([C API reference](https://oslandia.github.io/SFCGAL/doxygen/group__capi.html#ga277d01bd9978e13644baa1755f1cd3e0)
pub fn extrude(&self, ex: f64, ey: f64, ez: f64) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_extrude(self.c_geom.as_ptr(), ex, ey, ez) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns a tesselation of the given `SFCGeometry`.
/// ([C API reference](https://sfcgal.gitlab.io/SFCGAL/doxygen/group__capi.html#ga570ce6214f305ed35ebbec62d366b588))
pub fn tesselate(&self) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_tesselate(self.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns a triangulation of the given `SFCGeometry`.
/// ([C API reference](https://oslandia.github.io/SFCGAL/doxygen/group__capi.html#gae382792f387654a9adb2e2c38735e08d))
pub fn triangulate_2dz(&self) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_triangulate_2dz(self.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the convex hull of the given `SFCGeometry`.
/// ([C API reference](https://oslandia.github.io/SFCGAL/doxygen/group__capi.html#ga9027b5654cbacf6c2106d70b129d3a23))
pub fn convexhull(&self) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_convexhull(self.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the 3d convex hull of the given `SFCGeometry`.
/// ([C API reference](https://oslandia.github.io/SFCGAL/doxygen/group__capi.html#gacf01a9097f2059afaad871658b4b5a6f))
pub fn convexhull_3d(&self) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_convexhull_3d(self.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the substring of the given `SFCGeometry` LineString between
/// fractional distances. ([C API reference](https://oslandia.gitlab.io/SFCGAL/doxygen/group__capi.html#ga9184685ade86d02191ffaf0337ed3c1d))
pub fn line_substring(&self, start: f64, end: f64) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_line_sub_string(self.c_geom.as_ptr(), start, end) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the alpha shape of the given `SFCGeometry` Point set.
/// ([C API reference](https://oslandia.gitlab.io/SFCGAL/doxygen/group__capi.html#gadb33896047f57656dec64dff1984fba5))
pub fn alpha_shapes(&self, alpha: f64, allow_holes: bool) -> Result<SFCGeometry> {
if !self.is_valid().unwrap() {
return Err(format_err!(
"Error: alpha shapes can only be computed on valid geometries"
));
}
if alpha < 0.0 || !alpha.is_finite() {
return Err(format_err!(
"Error: alpha parameter must be positive or equal to 0.0, got {}",
alpha,
));
}
let result =
unsafe { sfcgal_geometry_alpha_shapes(self.c_geom.as_ptr(), alpha, allow_holes) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Return the optimal alpha shape of the given `SFCGeometry` Point set.
pub fn optimal_alpha_shapes(
&self,
allow_holes: bool,
nb_components: usize,
) -> Result<SFCGeometry> {
let result = unsafe {
sfcgal_geometry_optimal_alpha_shapes(self.c_geom.as_ptr(), allow_holes, nb_components)
};
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Create a SFCGeometry collection type (MultiPoint, MultiLineString,
/// MultiPolygon, MultiSolid or GeometryCollection) given a
/// mutable slice of `SFCGeometry`'s (this is a destructive
/// operation) ``` rust
/// use sfcgal::SFCGeometry;
/// let a = SFCGeometry::new("POINT(1.0 1.0)").unwrap();
/// let b = SFCGeometry::new("POINT(2.0 2.0)").unwrap();
/// let g = SFCGeometry::create_collection(&mut[a, b]).unwrap();
/// assert_eq!(
/// g.to_wkt_decim(1).unwrap(),
/// "MULTIPOINT((1.0 1.0),(2.0 2.0))",
/// );
/// ```
pub fn create_collection(geoms: &mut [SFCGeometry]) -> Result<SFCGeometry> {
if geoms.is_empty() {
let res_geom = unsafe { sfcgal_geometry_collection_create() };
return unsafe { SFCGeometry::new_from_raw(res_geom, true) };
}
let types = geoms
.iter()
.map(|g| g._type().unwrap())
.collect::<Vec<GeomType>>();
let multis = types
.iter()
.map(|gt| gt.is_collection_type())
.collect::<Vec<bool>>();
if !is_all_same(&types) || multis.iter().any(|&x| x) {
let res_geom = unsafe { sfcgal_geometry_collection_create() };
make_multi_geom(res_geom, geoms)
} else if types[0] == GeomType::Point {
let res_geom = unsafe { sfcgal_multi_point_create() };
make_multi_geom(res_geom, geoms)
} else if types[0] == GeomType::Linestring {
let res_geom = unsafe { sfcgal_multi_linestring_create() };
make_multi_geom(res_geom, geoms)
} else if types[0] == GeomType::Polygon {
let res_geom = unsafe { sfcgal_multi_polygon_create() };
make_multi_geom(res_geom, geoms)
} else if types[0] == GeomType::Solid {
let mut res_geom = SFCGeometry::new("MULTISOLID EMPTY")?;
res_geom.owned = false;
make_multi_geom(res_geom.c_geom.as_ptr(), geoms)
} else {
unreachable!();
}
}
/// Get the members of a SFCGeometry.
/// Returns Err if the SFCGeometry if not a collection (i.e. if it's
/// type is not in { MultiPoint, MultiLineString, MultiPolygon,
/// MultiSolid, GeometryCollection }). The original geometry
/// stay untouched. ``` rust
/// use sfcgal::SFCGeometry;
/// let g = SFCGeometry::new("MULTIPOINT((1.0 1.0),(2.0
/// 2.0))").unwrap(); let members =
/// g.get_collection_members().unwrap(); assert_eq!(
/// members[0].to_wkt_decim(1).unwrap(),
/// "POINT(1.0 1.0)",
/// );
/// assert_eq!(
/// members[1].to_wkt_decim(1).unwrap(),
/// "POINT(2.0 2.0)",
/// );
/// ```
pub fn get_collection_members(self) -> Result<Vec<SFCGeometry>> {
let _type = self._type()?;
if !_type.is_collection_type() {
return Err(format_err!(
"Error: the given geometry doesn't have any member ({:?} is not a collection type)",
_type,
));
}
unsafe {
let ptr = self.c_geom.as_ptr();
let n_geom = sfcgal_geometry_collection_num_geometries(ptr);
let mut result = Vec::new();
for n in 0..n_geom {
let _original_c_geom = sfcgal_geometry_collection_geometry_n(ptr, n);
let clone_c_geom = sfcgal_geometry_clone(_original_c_geom);
result.push(SFCGeometry::new_from_raw(clone_c_geom, true)?);
}
Ok(result)
}
}
/// Creates an empty point
pub fn point_create() -> Result<SFCGeometry> {
let result = unsafe { sfcgal_point_create() };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Creates a point from two X and Y coordinates
pub fn point_create_from_xy(x: f64, y: f64) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_point_create_from_xy(x, y) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Creates a point from three X, Y and M coordinates
pub fn point_create_from_xym(x: f64, y: f64, m: f64) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_point_create_from_xym(x, y, m) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Creates a point from three X, Y and Z coordinates
pub fn point_create_from_xyz(x: f64, y: f64, z: f64) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_point_create_from_xyz(x, y, z) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Create a point from x, y, z, m components.
pub fn point_create_from_xyzm(&self, x: f64, y: f64, z: f64, m: f64) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_point_create_from_xyzm(x, y, z, m) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the X coordinate of the given Point
pub fn point_x(&self) -> Result<f64> {
match self._type()? {
GeomType::Point => unsafe { Ok(sfcgal_point_x(self.c_geom.as_ptr())) },
_ => bail!("Geometry is not a Point"),
}
}
/// Returns the Y coordinate of the given Point
pub fn point_y(&self) -> Result<f64> {
match self._type()? {
GeomType::Point => unsafe { Ok(sfcgal_point_y(self.c_geom.as_ptr())) },
_ => bail!("Geometry is not a Point"),
}
}
/// Returns the Z coordinate of the given Point
pub fn point_z(&self) -> Result<f64> {
match self._type()? {
GeomType::Point => unsafe { Ok(sfcgal_point_z(self.c_geom.as_ptr())) },
_ => bail!("Geometry is not a Point"),
}
}
/// Returns the M coordinate of the given Point
pub fn point_m(&self) -> Result<f64> {
let result = unsafe { sfcgal_point_m(self.c_geom.as_ptr()) };
check_nan_value(result)
}
/// Sets one vertex of a Triangle
pub fn triangle_set_vertex(&self, index: i32, vertex: &SFCGeometry) -> Result<()> {
match self._type()? {
GeomType::Triangle => {
if !(0..=2).contains(&index) {
bail!("Bad index for a Triangle: it must be a value in the range (0..=2)");
}
unsafe {
let explicit_converted_int: ::std::os::raw::c_int = index;
sfcgal_triangle_set_vertex(
self.c_geom.as_ptr(),
explicit_converted_int,
vertex.c_geom.as_ptr(),
);
Ok(())
}
}
_ => bail!("Geometry is not a Triangle"),
}
}
/// Sets one vertex of a Triangle from two coordinates
pub fn triangle_set_vertex_from_xy(&self, index: i32, x: f64, y: f64) -> Result<()> {
match self._type()? {
GeomType::Triangle => {
if !(0..=2).contains(&index) {
bail!("Bad index for a Triangle: it must be a value in the range (0..=2)");
}
unsafe {
let explicit_converted_int: ::std::os::raw::c_int = index;
sfcgal_triangle_set_vertex_from_xy(
self.c_geom.as_ptr(),
explicit_converted_int,
x,
y,
);
Ok(())
}
}
_ => bail!("Geometry is not a Triangle"),
}
}
/// Sets one vertex of a Triangle from three coordinates
pub fn triangle_set_vertex_from_xyz(&self, index: i32, x: f64, y: f64, z: f64) -> Result<()> {
match self._type()? {
GeomType::Triangle => {
if !(0..=2).contains(&index) {
bail!("Bad index for a Triangle: it must be a value in the range (0..=2)");
}
unsafe {
let explicit_converted_int: ::std::os::raw::c_int = index;
sfcgal_triangle_set_vertex_from_xyz(
self.c_geom.as_ptr(),
explicit_converted_int,
x,
y,
z,
);
Ok(())
}
}
_ => bail!("Geometry is not a Triangle"),
}
}
/// Returns the straight skeleton partition for the given Polygon
pub fn geometry_straight_skeleton_partition(
&self,
auto_orientation: bool,
) -> Result<SFCGeometry> {
let result = unsafe {
sfcgal_geometry_straight_skeleton_partition(self.c_geom.as_ptr(), auto_orientation)
};
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Returns the visibility polygon of a Point inside a Polygon
pub fn geometry_visibility_point(&self, point: &SFCGeometry) -> Result<SFCGeometry> {
let result = unsafe {
sfcgal_geometry_visibility_point(self.c_geom.as_ptr(), point.c_geom.as_ptr())
};
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Rotates a geometry around the origin (0,0,0) by a given angle
pub fn geometry_rotate(&self, angle: f64) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_rotate(self.c_geom.as_ptr(), angle) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Rotates a geometry around the X axis by a given angle
pub fn geometry_rotate_x(&self, angle: f64) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_rotate_x(self.c_geom.as_ptr(), angle) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Rotates a geometry around the Y axis by a given angle
pub fn geometry_rotate_y(&self, angle: f64) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_rotate_y(self.c_geom.as_ptr(), angle) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Rotates a geometry around the Z axis by a given angle
pub fn geometry_rotate_z(&self, angle: f64) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_rotate_z(self.c_geom.as_ptr(), angle) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Rotates a geometry around a specified point by a given angle
pub fn geometry_rotate_2d(
&self,
angle: f64,
origin_x: f64,
origin_y: f64,
) -> Result<SFCGeometry> {
let result =
unsafe { sfcgal_geometry_rotate_2d(self.c_geom.as_ptr(), angle, origin_x, origin_y) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Rotates a 3D geometry around a specified axis by a given angle
pub fn geometry_rotate_3d(
&self,
angle: f64,
axis_x_angle: f64,
axis_y_angle: f64,
axis_z_angle: f64,
) -> Result<SFCGeometry> {
let result = unsafe {
sfcgal_geometry_rotate_3d(
self.c_geom.as_ptr(),
angle,
axis_x_angle,
axis_y_angle,
axis_z_angle,
)
};
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Rotates a 3D geometry around a specified axis and center point by a given
pub fn geometry_rotate_3d_around_center(
&self,
angle: f64,
axis_x_angle: f64,
axis_y_angle: f64,
axis_z_angle: f64,
center_x: f64,
center_y: f64,
center_z: f64,
) -> Result<SFCGeometry> {
let result = unsafe {
sfcgal_geometry_rotate_3d_around_center(
self.c_geom.as_ptr(),
angle,
axis_x_angle,
axis_y_angle,
axis_z_angle,
center_x,
center_y,
center_z,
)
};
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Force a Right Handed Rule on the given Geometry
pub fn geometry_force_rhr(&self) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_force_rhr(self.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Force a Left Handed Rule on the given Geometry
pub fn geometry_force_lhr(&self) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_force_lhr(self.c_geom.as_ptr()) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Scale a geometry by a given factor
pub fn geometry_scale(&self, scale: f64) -> Result<SFCGeometry> {
let result = unsafe { sfcgal_geometry_scale(self.c_geom.as_ptr(), scale) };
unsafe { SFCGeometry::new_from_raw(result, true) }
}
/// Scale a geometry by different factors for each dimension
pub fn geometry_scale_3d(
&self,
scale_x: f64,
scale_y: f64,
scale_z: f64,
) -> Result<SFCGeometry> {
let result =
unsafe { sfcgal_geometry_scale_3d(self.c_geom.as_ptr(), scale_x, scale_y, scale_z) };
unsafe { SFCGeometry::new_from_raw(result, true) }