Skip to content

Commit

Permalink
Switch to new, sound Rust FFI
Browse files Browse the repository at this point in the history
  • Loading branch information
urschrei committed Dec 1, 2021
1 parent ec29912 commit bf895c2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 44 deletions.
21 changes: 13 additions & 8 deletions rdp_p.pxd
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
cdef extern from "header.h":
struct Array:
struct ExternalArray:
void* data
size_t len

cdef Array simplify_rdp_ffi(Array, double epsilon);
cdef Array simplify_rdp_idx_ffi(Array, double epsilon);
cdef Array simplify_visvalingam_ffi(Array, double epsilon);
cdef Array simplify_visvalingam_idx_ffi(Array, double epsilon);
cdef Array simplify_visvalingamp_ffi(Array, double epsilon);
cdef void drop_float_array(Array coords);
cdef void drop_usize_array(Array coords);
cdef extern from "header.h":
struct InternalArray:
void* data
size_t len

cdef InternalArray simplify_rdp_ffi(ExternalArray, double epsilon);
cdef InternalArray simplify_rdp_idx_ffi(ExternalArray, double epsilon);
cdef InternalArray simplify_visvalingam_ffi(ExternalArray, double epsilon);
cdef InternalArray simplify_visvalingam_idx_ffi(ExternalArray, double epsilon);
cdef InternalArray simplify_visvalingamp_ffi(ExternalArray, double epsilon);
cdef void drop_float_array(InternalArray coords);
cdef void drop_usize_array(InternalArray coords);
23 changes: 12 additions & 11 deletions simplification/cutil.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import numpy as np
import numpy
from cython cimport view
from rdp_p cimport (
Array,
ExternalArray,
InternalArray,
simplify_rdp_ffi,
simplify_rdp_idx_ffi,
simplify_visvalingam_ffi,
Expand Down Expand Up @@ -66,10 +67,10 @@ cpdef simplify_coords(coords, double epsilon):
if not arr.flags['C_CONTIGUOUS']:
arr = np.ascontiguousarray(arr)
cdef double[:,::1] ncoords = np.array(arr, dtype=np.float64)
cdef Array coords_ffi
cdef ExternalArray coords_ffi
coords_ffi.data = <void*>&ncoords[0, 0]
coords_ffi.len = ncoords.shape[0]
cdef Array result = simplify_rdp_ffi(coords_ffi, epsilon)
cdef InternalArray result = simplify_rdp_ffi(coords_ffi, epsilon)
cdef double* incoming_ptr = <double*>(result.data)
cdef double[:, ::1] view = <double[:result.len,:2:1]>incoming_ptr
if isinstance(coords, numpy.ndarray):
Expand All @@ -96,10 +97,10 @@ cpdef simplify_coords_idx(coords, double epsilon):
if not len(coords):
return coords
cdef double[:,::1] ncoords = np.array(coords, dtype=np.float64)
cdef Array coords_ffi
cdef ExternalArray coords_ffi
coords_ffi.data = <void*>&ncoords[0, 0]
coords_ffi.len = ncoords.shape[0]
cdef Array result = simplify_rdp_idx_ffi(coords_ffi, epsilon)
cdef InternalArray result = simplify_rdp_idx_ffi(coords_ffi, epsilon)
cdef size_t* incoming_ptr = <size_t*>(result.data)
cdef size_t[::1] view = <size_t[:result.len]>incoming_ptr
if isinstance(coords, numpy.ndarray):
Expand Down Expand Up @@ -128,10 +129,10 @@ cpdef simplify_coords_vw(coords, double epsilon):
if not len(coords):
return coords
cdef double[:,::1] ncoords = np.array(coords, dtype=np.float64)
cdef Array coords_ffi
cdef ExternalArray coords_ffi
coords_ffi.data = <void*>&ncoords[0, 0]
coords_ffi.len = ncoords.shape[0]
cdef Array result = simplify_visvalingam_ffi(coords_ffi, epsilon)
cdef InternalArray result = simplify_visvalingam_ffi(coords_ffi, epsilon)
cdef double* incoming_ptr = <double*>(result.data)
cdef double[:, ::1] view = <double[:result.len,:2:1]>incoming_ptr
if isinstance(coords, numpy.ndarray):
Expand All @@ -158,10 +159,10 @@ cpdef simplify_coords_vw_idx(coords, double epsilon):
if not len(coords):
return coords
cdef double[:,::1] ncoords = np.array(coords, dtype=np.float64)
cdef Array coords_ffi
cdef ExternalArray coords_ffi
coords_ffi.data = <void*>&ncoords[0, 0]
coords_ffi.len = ncoords.shape[0]
cdef Array result = simplify_visvalingam_idx_ffi(coords_ffi, epsilon)
cdef InternalArray result = simplify_visvalingam_idx_ffi(coords_ffi, epsilon)
cdef size_t* incoming_ptr = <size_t*>(result.data)
cdef size_t[::1] view = <size_t[:result.len]>incoming_ptr
if isinstance(coords, numpy.ndarray):
Expand Down Expand Up @@ -191,11 +192,11 @@ cpdef simplify_coords_vwp(coords, double epsilon):
if not len(coords):
return coords
cdef double[:,::1] ncoords = np.array(coords, dtype=np.float64)
cdef Array coords_ffi
cdef ExternalArray coords_ffi
coords_ffi.data = <void*>&ncoords[0, 0]
coords_ffi.len = ncoords.shape[0]

cdef Array result = simplify_visvalingamp_ffi(coords_ffi, epsilon)
cdef InternalArray result = simplify_visvalingamp_ffi(coords_ffi, epsilon)
cdef double* incoming_ptr = <double*>(result.data)
cdef double[:, ::1] view = <double[:result.len,:2:1]>incoming_ptr
if isinstance(coords, numpy.ndarray):
Expand Down
42 changes: 26 additions & 16 deletions simplification/header.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Generated with cbindgen:0.16.0 */
/* Generated with cbindgen:0.20.0 */

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

Expand All @@ -8,12 +8,22 @@
#include <stdlib.h>

/**
* A C-compatible `struct` used for passing arrays across the FFI boundary
* A C-compatible `struct` originating **inside** Rust
* used for passing arrays across the FFI boundary
*/
typedef struct Array {
typedef struct InternalArray {
void *data;
size_t len;
} InternalArray;

/**
* A C-compatible `struct` originating **outside** Rust
* used for passing arrays across the FFI boundary
*/
typedef struct ExternalArray {
const void *data;
size_t len;
} Array;
} ExternalArray;

/**
* FFI wrapper for RDP, returning simplified geometry **coordinates**
Expand All @@ -32,8 +42,8 @@ typedef struct Array {
*
* This function is unsafe because it accesses a raw pointer which could contain arbitrary data
*/
struct Array simplify_rdp_ffi(struct Array coords,
double precision);
struct InternalArray simplify_rdp_ffi(struct ExternalArray coords,
double precision);

/**
* FFI wrapper for RDP, returning simplified geometry **indices**
Expand All @@ -52,8 +62,8 @@ struct Array simplify_rdp_ffi(struct Array coords,
*
* This function is unsafe because it accesses a raw pointer which could contain arbitrary data
*/
struct Array simplify_rdp_idx_ffi(struct Array coords,
double precision);
struct InternalArray simplify_rdp_idx_ffi(struct ExternalArray coords,
double precision);

/**
* FFI wrapper for Visvalingam-Whyatt, returning simplified geometry **coordinates**
Expand All @@ -72,8 +82,8 @@ struct Array simplify_rdp_idx_ffi(struct Array coords,
*
* This function is unsafe because it accesses a raw pointer which could contain arbitrary data
*/
struct Array simplify_visvalingam_ffi(struct Array coords,
double precision);
struct InternalArray simplify_visvalingam_ffi(struct ExternalArray coords,
double precision);

/**
* FFI wrapper for Visvalingam-Whyatt, returning simplified geometry **indices**
Expand All @@ -92,8 +102,8 @@ struct Array simplify_visvalingam_ffi(struct Array coords,
*
* This function is unsafe because it accesses a raw pointer which could contain arbitrary data
*/
struct Array simplify_visvalingam_idx_ffi(struct Array coords,
double precision);
struct InternalArray simplify_visvalingam_idx_ffi(struct ExternalArray coords,
double precision);

/**
* FFI wrapper for topology-preserving Visvalingam-Whyatt, returning simplified geometry **coordinates**.
Expand All @@ -112,8 +122,8 @@ struct Array simplify_visvalingam_idx_ffi(struct Array coords,
*
* This function is unsafe because it accesses a raw pointer which could contain arbitrary data
*/
struct Array simplify_visvalingamp_ffi(struct Array coords,
double precision);
struct InternalArray simplify_visvalingamp_ffi(struct ExternalArray coords,
double precision);

/**
* Free memory which has been allocated across the FFI boundary by:
Expand All @@ -125,7 +135,7 @@ struct Array simplify_visvalingamp_ffi(struct Array coords,
*
* This function is unsafe because it accesses a raw pointer which could contain arbitrary data
*/
void drop_float_array(struct Array arr);
void drop_float_array(struct InternalArray arr);

/**
* Free memory which has been allocated across the FFI boundary by:
Expand All @@ -136,4 +146,4 @@ void drop_float_array(struct Array arr);
*
* This function is unsafe because it accesses a raw pointer which could contain arbitrary data
*/
void drop_usize_array(struct Array arr);
void drop_usize_array(struct InternalArray arr);
21 changes: 13 additions & 8 deletions simplification/rdp_p.pxd
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
cdef extern from "header.h":
struct Array:
struct ExternalArray:
void* data
size_t len

cdef Array simplify_rdp_ffi(Array, double epsilon);
cdef Array simplify_rdp_idx_ffi(Array, double epsilon);
cdef Array simplify_visvalingam_ffi(Array, double epsilon);
cdef Array simplify_visvalingam_idx_ffi(Array, double epsilon);
cdef Array simplify_visvalingamp_ffi(Array, double epsilon);
cdef void drop_float_array(Array coords);
cdef void drop_usize_array(Array coords);
cdef extern from "header.h":
struct InternalArray:
void* data
size_t len

cdef InternalArray simplify_rdp_ffi(ExternalArray, double epsilon);
cdef InternalArray simplify_rdp_idx_ffi(ExternalArray, double epsilon);
cdef InternalArray simplify_visvalingam_ffi(ExternalArray, double epsilon);
cdef InternalArray simplify_visvalingam_idx_ffi(ExternalArray, double epsilon);
cdef InternalArray simplify_visvalingamp_ffi(ExternalArray, double epsilon);
cdef void drop_float_array(InternalArray coords);
cdef void drop_usize_array(InternalArray coords);
2 changes: 1 addition & 1 deletion simplification/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import numpy as np

__author__ = "Stephan Hügel"
__version__ = "0.5.17"
__version__ = "0.5.18"

file_path = os.path.dirname(__file__)

Expand Down

0 comments on commit bf895c2

Please sign in to comment.