Skip to content

Commit 08ee9ca

Browse files
committed
Support USINGZ/user data in points
1 parent 9a73847 commit 08ee9ca

File tree

11 files changed

+1002
-23
lines changed

11 files changed

+1002
-23
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ categories = ["algorithms", "external-ffi-bindings"]
1515
generate-bindings = ["bindgen"]
1616
update-bindings = ["generate-bindings"]
1717
serde = ["dep:serde"]
18+
usingz = []
1819

1920
[dependencies]
2021
libc = "0.2"

build.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
use std::env;
22

3+
struct BuildOptions {
4+
usingz: bool,
5+
#[allow(unused)]
6+
generated_bindings_name: String,
7+
}
8+
39
fn main() {
10+
build(BuildOptions {
11+
generated_bindings_name: "bindings".to_string(),
12+
usingz: false,
13+
});
14+
build(BuildOptions {
15+
generated_bindings_name: "bindings_usingz".to_string(),
16+
usingz: true,
17+
});
18+
}
19+
20+
fn build(options: BuildOptions) {
421
println!("cargo:rerun-if-changed=clipper2c");
522
if cfg!(feature = "update-bindings") {
623
println!("cargo:rerun-if-changed=generated");
724
}
825

9-
cc::Build::new()
26+
#[allow(unused_mut)]
27+
let mut build = &mut cc::Build::new();
28+
29+
if options.usingz {
30+
build = build.define("USINGZ", None);
31+
}
32+
33+
build
1034
.cpp(true)
1135
.opt_level(3)
1236
.include("clipper2c/vendor/Clipper2/CPP/Clipper2Lib/include/")
@@ -49,7 +73,13 @@ fn main() {
4973

5074
#[cfg(feature = "generate-bindings")]
5175
{
52-
let builder = bindgen::Builder::default()
76+
let mut builder = bindgen::Builder::default();
77+
78+
if options.usingz {
79+
builder = builder.clang_arg("-DUSINGZ");
80+
}
81+
82+
builder = builder
5383
.header("clipper2c/include/clipper2c.h")
5484
.header("clipper2c/include/types.h")
5585
.derive_default(true)
@@ -111,6 +141,7 @@ fn main() {
111141
.allowlist_function("clipper_clipperd_set_reverse_solution")
112142
.allowlist_function("clipper_clipperd_get_reverse_solution")
113143
.allowlist_function("clipper_clipperd_clear")
144+
.allowlist_function("clipper_clipperd_set_z_callback")
114145
.allowlist_function("clipper_delete_clipperd")
115146
// PolyTreeD Methods
116147
.allowlist_function("clipper_polytreed_size")
@@ -153,7 +184,7 @@ fn main() {
153184
.allowlist_function("clipper_paths64_inflate")
154185
.allowlist_function("clipper_paths64_to_pathsd")
155186
.allowlist_function("clipper_paths64_area")
156-
// ClipperD Methods
187+
// Clipper64 Methods
157188
.allowlist_function("clipper_clipper64_size")
158189
.allowlist_function("clipper_clipper64")
159190
.allowlist_function("clipper_clipper64_add_subject")
@@ -166,6 +197,7 @@ fn main() {
166197
.allowlist_function("clipper_clipper64_set_reverse_solution")
167198
.allowlist_function("clipper_clipper64_get_reverse_solution")
168199
.allowlist_function("clipper_clipper64_clear")
200+
.allowlist_function("clipper_clipper64_set_z_callback")
169201
.allowlist_function("clipper_delete_clipper64")
170202
// PolyTreeD Methods
171203
.allowlist_function("clipper_polytree64_size")
@@ -207,7 +239,7 @@ fn main() {
207239
} else {
208240
std::path::PathBuf::from(env::var("OUT_DIR").unwrap())
209241
};
210-
let out_bindings = out_path.join("bindings.rs");
242+
let out_bindings = out_path.join(options.generated_bindings_name + ".rs");
211243

212244
bindings
213245
.write_to_file(&out_bindings)

clipper2c/include/clipper2c.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ int clipper_clipper64_get_preserve_collinear(ClipperClipper64 *c);
364364
int clipper_clipper64_get_reverse_solution(ClipperClipper64 *c);
365365
void clipper_clipper64_clear(ClipperClipper64 *c);
366366

367+
#ifdef USINGZ
368+
void clipper_clipper64_set_z_callback(ClipperClipper64 *c, void* user_data, ClipperZCallback64 cb);
369+
#endif
370+
367371
// ClipperD Setters / Getters
368372
//
369373
void clipper_clipperd_set_preserve_collinear(ClipperClipperD *c, int t);
@@ -372,6 +376,10 @@ int clipper_clipperd_get_preserve_collinear(ClipperClipperD *c);
372376
int clipper_clipperd_get_reverse_solution(ClipperClipperD *c);
373377
void clipper_clipperd_clear(ClipperClipperD *c);
374378

379+
#ifdef USINGZ
380+
void clipper_clipperd_set_z_callback(ClipperClipperD *c, void* user_data, ClipperZCallbackD cb);
381+
#endif
382+
375383
// Clipper64 Methods
376384

377385
void clipper_clipper64_add_subject(ClipperClipper64 *c,
@@ -527,4 +535,4 @@ void clipper_delete_svgreader(ClipperSvgReader *p);
527535

528536
#ifdef __cplusplus
529537
}
530-
#endif
538+
#endif

clipper2c/include/types.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,40 @@ extern "C"
2727
{
2828
double x;
2929
double y;
30+
#ifdef USINGZ
31+
int64_t z;
32+
#endif
3033
} ClipperPointD;
3134

3235
typedef struct ClipperPoint64
3336
{
3437
int64_t x;
3538
int64_t y;
39+
#ifdef USINGZ
40+
int64_t z;
41+
#endif
3642
} ClipperPoint64;
3743

44+
#ifdef USINGZ
45+
typedef void (*ClipperZCallback64)(
46+
void* user_data,
47+
const ClipperPoint64* e1bot,
48+
const ClipperPoint64* e1top,
49+
const ClipperPoint64* e2bot,
50+
const ClipperPoint64* e2top,
51+
ClipperPoint64* pt
52+
);
53+
54+
typedef void (*ClipperZCallbackD)(
55+
void* user_data,
56+
const ClipperPointD* e1bot,
57+
const ClipperPointD* e1top,
58+
const ClipperPointD* e2bot,
59+
const ClipperPointD* e2top,
60+
ClipperPointD* pt
61+
);
62+
#endif
63+
3864
struct ClipperRect64
3965
{
4066
int64_t left;
@@ -102,4 +128,4 @@ extern "C"
102128
}
103129
#endif
104130

105-
#endif
131+
#endif

clipper2c/src/clipper2c.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,13 @@ extern "C"
403403
ClipperPoint64 *pts = reinterpret_cast<ClipperPoint64 *>(mem);
404404
for (size_t i = 0; i < len; ++i)
405405
{
406-
pts[i] = {p[i].x, p[i].y};
406+
pts[i] = {
407+
p[i].x,
408+
p[i].y,
409+
#ifdef USINGZ
410+
p[i].z
411+
#endif
412+
};
407413
}
408414
return pts;
409415
}
@@ -415,7 +421,13 @@ extern "C"
415421
ClipperPointD *pts = reinterpret_cast<ClipperPointD *>(mem);
416422
for (size_t i = 0; i < len; ++i)
417423
{
418-
pts[i] = {p[i].x, p[i].y};
424+
pts[i] = {
425+
p[i].x,
426+
p[i].y,
427+
#ifdef USINGZ
428+
p[i].z
429+
#endif
430+
};
419431
}
420432
return pts;
421433
}
@@ -500,7 +512,13 @@ extern "C"
500512
auto len = ps[i].size();
501513
for (size_t j = 0; j < len; ++j)
502514
{
503-
pts[i][j] = {ps[i][j].x, ps[i][j].y};
515+
pts[i][j] = {
516+
ps[i][j].x,
517+
ps[i][j].y,
518+
#ifdef USINGZ
519+
ps[i][j].z
520+
#endif
521+
};
504522
}
505523
}
506524
return pts;
@@ -516,7 +534,13 @@ extern "C"
516534
auto len = ps[i].size();
517535
for (size_t j = 0; j < len; ++j)
518536
{
519-
pts[i][j] = {ps[i][j].x, ps[i][j].y};
537+
pts[i][j] = {
538+
ps[i][j].x,
539+
ps[i][j].y,
540+
#ifdef USINGZ
541+
ps[i][j].z
542+
#endif
543+
};
520544
}
521545
}
522546
return pts;
@@ -1020,4 +1044,4 @@ extern "C"
10201044

10211045
#ifdef __cplusplus
10221046
}
1023-
#endif
1047+
#endif

clipper2c/src/clipper64.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ int clipper_clipper64_get_reverse_solution(ClipperClipper64 *c) {
3737

3838
void clipper_clipper64_clear(ClipperClipper64 *c) { from_c(c)->Clear(); }
3939

40+
#ifdef USINGZ
41+
void clipper_clipper64_set_z_callback(ClipperClipper64 *c, void* user_data, ClipperZCallback64 cb) {
42+
from_c(c)->SetZCallback([cb, user_data](const Point64 &p1, const Point64 &p2,
43+
const Point64 &p3, const Point64 &p4,
44+
Point64 &p5) {
45+
cb(user_data,
46+
reinterpret_cast<const ClipperPoint64 *>(&p1),
47+
reinterpret_cast<const ClipperPoint64 *>(&p2),
48+
reinterpret_cast<const ClipperPoint64 *>(&p3),
49+
reinterpret_cast<const ClipperPoint64 *>(&p4),
50+
reinterpret_cast<ClipperPoint64 *>(&p5));
51+
});
52+
}
53+
#endif
54+
4055
// Methods
4156

4257
void clipper_clipper64_add_subject(ClipperClipper64 *c,
@@ -77,4 +92,4 @@ int clipper_clipper64_execute_tree_with_open(ClipperClipper64 *c64,
7792

7893
#ifdef __cplusplus
7994
}
80-
#endif
95+
#endif

clipper2c/src/clipperd.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ int clipper_clipperd_get_reverse_solution(ClipperClipperD *c) {
3737

3838
void clipper_clipperd_clear(ClipperClipperD *c) { from_c(c)->Clear(); }
3939

40+
#ifdef USINGZ
41+
void clipper_clipperd_set_z_callback(ClipperClipperD *c, void* user_data, ClipperZCallbackD cb) {
42+
from_c(c)->SetZCallback([cb, user_data](const PointD &p1, const PointD &p2,
43+
const PointD &p3, const PointD &p4,
44+
PointD &p5) {
45+
cb(user_data,
46+
reinterpret_cast<const ClipperPointD *>(&p1),
47+
reinterpret_cast<const ClipperPointD *>(&p2),
48+
reinterpret_cast<const ClipperPointD *>(&p3),
49+
reinterpret_cast<const ClipperPointD *>(&p4),
50+
reinterpret_cast<ClipperPointD *>(&p5));
51+
});
52+
}
53+
#endif
54+
4055
// Methods
4156

4257
void clipper_clipperd_add_subject(ClipperClipperD *c, ClipperPathsD *subjects) {
@@ -75,4 +90,4 @@ int clipper_clipperd_execute_tree_with_open(ClipperClipperD *cD,
7590

7691
#ifdef __cplusplus
7792
}
78-
#endif
93+
#endif

clipper2c/src/conv.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,25 @@
22
#include "clipper2/clipper.core.h"
33
#include "types.h"
44

5-
ClipperPoint64 to_c(Clipper2Lib::Point64 p) { return {p.x, p.y}; }
5+
ClipperPoint64 to_c(Clipper2Lib::Point64 p) {
6+
return {
7+
p.x,
8+
p.y,
9+
#ifdef USINGZ
10+
p.z
11+
#endif
12+
};
13+
}
614

7-
ClipperPointD to_c(Clipper2Lib::PointD p) { return {p.x, p.y}; }
15+
ClipperPointD to_c(Clipper2Lib::PointD p) {
16+
return {
17+
p.x,
18+
p.y,
19+
#ifdef USINGZ
20+
p.z
21+
#endif
22+
};
23+
}
824

925
ClipperPath64 *to_c(Clipper2Lib::Path64 *p)
1026
{
@@ -351,4 +367,4 @@ Clipper2Lib::PointInPolygonResult from_c(ClipperPointInPolygonResult result)
351367
break;
352368
};
353369
return res;
354-
}
370+
}

0 commit comments

Comments
 (0)