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
//! This module contains the C foreign function interface for cavalier_contours.
#![allow(non_camel_case_types)]
mod error_handling;
use cavalier_contours::{PlineVertex, Polyline, Vector2};
use core::slice;
use error_handling::{set_last_error, LAST_ERROR};
use std::{ffi::CStr, os::raw::c_char, panic};

/// Helper macro to catch unwind and return -1 if panic was caught otherwise returns whatever the expression returned.
macro_rules! ffi_catch_unwind {
    ($body: expr) => {
        match panic::catch_unwind(move || $body) {
            Ok(r) => r,
            Err(_) => {
                set_last_error("Internal algorithm/library error occurred.", "");
                -1
            }
        }
    };
}
/// Represents a polyline vertex holding x, y, and bulge.
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct cavc_vertex {
    pub data: [f64; 3],
}

impl cavc_vertex {
    pub fn new(x: f64, y: f64, bulge: f64) -> Self {
        cavc_vertex {
            data: [x, y, bulge],
        }
    }

    pub fn x(&self) -> f64 {
        self.data[0]
    }

    pub fn y(&self) -> f64 {
        self.data[1]
    }

    pub fn bulge(&self) -> f64 {
        self.data[2]
    }

    fn from_internal(v: PlineVertex<f64>) -> Self {
        cavc_vertex::new(v.x, v.y, v.bulge)
    }
}

/// Represents a basic 2D point holding x and y.
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct cavc_point {
    pub data: [f64; 2],
}

/// Polyline object type.
#[derive(Debug, Clone)]
pub struct cavc_pline(Polyline<f64>);

/// Gets the last error message set as a null terminated c string.
///
/// The c string returned is in thread local storage and is only valid until another function that can error is called!
pub unsafe extern "C" fn cavc_last_error_msg() -> *const c_char {
    let mut result = CStr::from_bytes_with_nul_unchecked(b"0").as_ptr();
    LAST_ERROR.with(|last_error| {
        if let Some(error_data) = last_error.borrow().as_ref() {
            result = CStr::from_bytes_with_nul_unchecked(&error_data.error_msg.as_bytes_with_nul())
                .as_ptr();
        }
    });

    result
}

/// Gets the last error report data set as a null terminated c string.
///
/// The c string returned is in thread local storage and is only valid until another function that can error is called!
pub unsafe extern "C" fn cavc_last_error_report() -> *const c_char {
    let mut result = CStr::from_bytes_with_nul_unchecked(b"0").as_ptr();
    LAST_ERROR.with(|last_error| {
        if let Some(error_data) = last_error.borrow().as_ref() {
            result = CStr::from_bytes_with_nul_unchecked(
                &error_data.error_report_data.as_bytes_with_nul(),
            )
            .as_ptr();
        }
    });

    result
}

/// Create a new polyline object.
///
/// `vertexes` is an array of [cavc_vertex] to create the polyline with (may be null if `n_vertexes` is 0).
/// `n_vertexes` contains the number of vertexes in the array.
/// `is_closed` sets the polyline to be closed if non-zero.
/// `pline` is an out parameter to hold the created polyline.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_create(
    vertexes: *const cavc_vertex,
    n_vertexes: u32,
    is_closed: u8,
    pline: *mut *mut cavc_pline,
) -> i32 {
    ffi_catch_unwind!({
        let mut result = Polyline::new();
        if is_closed != 0 {
            result.set_is_closed(true);
        }

        if !vertexes.is_null() && n_vertexes != 0 {
            let data = slice::from_raw_parts(vertexes, n_vertexes as usize);
            for v in data {
                result.add_from_array(v.data);
            }
        }

        *pline = Box::into_raw(Box::new(cavc_pline(result)));
        0
    })
}

/// Free an existing polyline object.
///
/// Nothing happens if `pline` is null.
#[no_mangle]
pub unsafe extern "C" fn cavc_pline_f(pline: *mut cavc_pline) {
    if !pline.is_null() {
        drop(Box::from_raw(pline))
    }
}

/// Get whether the polyline is closed or not.
///
/// `is_closed` is used as an out parameter to hold the whether `pline` is closed (non-zero) or not (0).
///
/// ## Specific Error Codes
/// * 1 = `pline` is null.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_get_is_closed(
    pline: *const cavc_pline,
    is_closed: *mut u8,
) -> i32 {
    ffi_catch_unwind!({
        if pline.is_null() {
            return 1;
        }
        *is_closed = if (*pline).0.is_closed() { 1 } else { 0 };
        0
    })
}

/// Set whether the polyline is closed or not.
///
/// If `is_closed` is non-zero then `pline` is set to be closed, otherwise it is set to be open.
///
/// ## Specific Error Codes
/// * 1 = `pline` is null.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_set_is_closed(pline: *mut cavc_pline, is_closed: u8) -> i32 {
    ffi_catch_unwind!({
        if pline.is_null() {
            return 1;
        }
        (*pline)
            .0
            .set_is_closed(if is_closed == 0 { false } else { true });
        0
    })
}

/// Get the vertex count of a polyline.
///
/// `count` used as out parameter to hold the vertex count.
///
/// ## Specific Error Codes
/// * 1 = `pline` is null.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_get_vertex_count(
    pline: *const cavc_pline,
    count: *mut u32,
) -> i32 {
    ffi_catch_unwind!({
        if pline.is_null() {
            return 1;
        }
        *count = (*pline).0.len() as u32;
        0
    })
}

/// Fills the buffer given with the vertex data of a polyline.
///
/// You must use [cavc_pline_get_vertex_count] to ensure the buffer given has adequate length
/// to be filled with all vertexes!
///
/// `vertex_data` should point to a buffer that can be filled with all `pline` vertexes.
///
/// ## Specific Error Codes
/// * 1 = `pline` is null.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_get_vertex_data(
    pline: *const cavc_pline,
    vertex_data: *mut cavc_vertex,
) -> i32 {
    ffi_catch_unwind!({
        if pline.is_null() {
            return 1;
        }

        let buffer = slice::from_raw_parts_mut(vertex_data, (*pline).0.len());
        for (i, v) in (*pline).0.iter().enumerate() {
            buffer[i] = cavc_vertex::new(v.x, v.y, v.bulge);
        }
        0
    })
}

/// Sets all of the vertexes of a polyline.
///
/// `vertex_data` is an array of vertexes to use for the polyline.
/// `n_vertexes` must specify the number of vertexes to be read from the
/// `vertex_data` array.
///
/// ## Specific Error Codes
/// * 1 = `pline` is null.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_set_vertex_data(
    pline: *mut cavc_pline,
    vertex_data: *const cavc_vertex,
    n_vertexes: u32,
) -> i32 {
    ffi_catch_unwind!({
        if pline.is_null() {
            return 1;
        }

        (*pline).0.clear();
        let buffer = slice::from_raw_parts(vertex_data, n_vertexes as usize);
        (*pline).0.reserve(buffer.len());
        for v in buffer {
            (*pline).0.add(v.x(), v.y(), v.bulge());
        }
        0
    })
}

/// Clears all of the vertexes of a polyline.
///
/// ## Specific Error Codes
/// * 1 = `pline` is null.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_clear(pline: *mut cavc_pline) -> i32 {
    ffi_catch_unwind!({
        if pline.is_null() {
            return 1;
        }

        (*pline).0.clear();
        0
    })
}

/// Add a vertex to a polyline `pline` with `x`, `y`, and `bulge`.
///
/// ## Specific Error Codes
/// * 1 = `pline` is null.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_add(pline: *mut cavc_pline, x: f64, y: f64, bulge: f64) -> i32 {
    ffi_catch_unwind!({
        if pline.is_null() {
            return 1;
        }

        (*pline).0.add(x, y, bulge);
        0
    })
}

/// Get a polyline vertex at a given index position.
///
/// `position` is is the index to get the vertex at.
/// `vertex` used as out parameter to hold the vertex retrieved.
///
/// ## Specific Error Codes
/// * 1 = `pline` is null.
/// * 2 = `position` is out of bounds for the `pline` given.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_get_vertex(
    pline: *const cavc_pline,
    position: u32,
    vertex: *mut cavc_vertex,
) -> i32 {
    ffi_catch_unwind!({
        if pline.is_null() {
            return 1;
        }

        if position >= (*pline).0.len() as u32 {
            return 2;
        }

        let v = (*pline).0[position as usize];
        *vertex = cavc_vertex::from_internal(v);
        0
    })
}

/// Remove a vertex from a polyline at an index position.
///
/// `position` is the index of the vertex to be removed from the polyline.
///
/// ## Specific Error Codes
/// * 1 = `pline` is null.
/// * 2 = `position` is out of bounds for the `pline` given.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_remove(pline: *mut cavc_pline, position: u32) -> i32 {
    ffi_catch_unwind!({
        if pline.is_null() {
            return 1;
        }

        if position as usize >= (*pline).0.len() {
            return 2;
        }

        (*pline).0.remove(position as usize);
        0
    })
}

/// Compute the path length of a polyline.
///
/// `path_length` is used as the out parameter to hold the computed path length.
///
/// ## Specific Error Codes
/// * 1 = `pline` is null.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_eval_path_length(
    pline: *const cavc_pline,
    path_length: *mut f64,
) -> i32 {
    ffi_catch_unwind!({
        if pline.is_null() {
            return 1;
        }
        *path_length = (*pline).0.path_length();
        0
    })
}

/// Compute the signed area of a polyline.
///
/// If `pline` is an open polyline then the computed area is always 0.
/// If `pline` direction is counter clockwise then result is positive otherwise it is negative.
/// `area` is used as the out parameter to hold the computed area.
///
/// ## Specific Error Codes
/// * 1 = `pline` is null.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_eval_area(pline: *const cavc_pline, area: *mut f64) -> i32 {
    ffi_catch_unwind!({
        if pline.is_null() {
            return 1;
        }
        *area = (*pline).0.area();
        0
    })
}

/// Compute the winding number for a closed polyline relative to a point.
///
/// If `pline` is an open polyline then 0 is always returned.
/// The winding number has a magnitude equal to the net number of times the `pline` winds around
/// the `point` given and its sign is positive if the windings are net counter clockwise or negative if
/// they are net clockwise.
///
/// `winding_number` is used as the out parameter to hold the computed winding number.
///
/// ## Specific Error Codes
/// * 1 = `pline` is null.
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn cavc_pline_eval_wn(
    pline: *const cavc_pline,
    point: cavc_point,
    winding_number: *mut i32,
) -> i32 {
    ffi_catch_unwind!({
        if pline.is_null() {
            return 1;
        }
        *winding_number = (*pline)
            .0
            .winding_number(Vector2::new(point.data[0], point.data[1]));
        0
    })
}