Skip to content

Commit

Permalink
Remove FLATTENING_TOLERANCE const and add tol parameter down from p…
Browse files Browse the repository at this point in the history
…arse()

- update the ffi wrapper
- tests are using the old const value

Closes #3
  • Loading branch information
naufraghi committed Nov 2, 2020
1 parent dd43ac2 commit 608a229
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 59 deletions.
4 changes: 2 additions & 2 deletions svg2polylines-ffi/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ typedef struct Polyline {
size_t len;
} Polyline;

uint8_t svg_str_to_polylines(char* svg, Polyline** polylines, size_t* polylines_len);
uint8_t svg_str_to_polylines(char* svg, double tol, Polyline** polylines, size_t* polylines_len);
void free_polylines(Polyline* polylines, size_t polylines_len);


Expand Down Expand Up @@ -53,7 +53,7 @@ int main() {
size_t polylines_len = 0;

// Process data
uint8_t err = svg_str_to_polylines(input, &polylines, &polylines_len);
uint8_t err = svg_str_to_polylines(input, 0.15, &polylines, &polylines_len);

// Error handling
if (err > 0) {
Expand Down
4 changes: 2 additions & 2 deletions svg2polylines-ffi/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
size_t len;
} Polyline;
uint8_t svg_str_to_polylines(char* svg, Polyline** polylines, size_t* polylines_len);
uint8_t svg_str_to_polylines(char* svg, double tol, Polyline** polylines, size_t* polylines_len);
void free_polylines(Polyline* polylines, size_t polylines_len);
''')

Expand Down Expand Up @@ -47,7 +47,7 @@ def print_polyline(p):

polylines = ffi.new('Polyline**')
polylines_len = ffi.new('size_t*')
lib.svg_str_to_polylines(svg_input, polylines, polylines_len)
lib.svg_str_to_polylines(svg_input, 0.15, polylines, polylines_len)


print('Found %d polylines!' % polylines_len[0])
Expand Down
47 changes: 27 additions & 20 deletions svg2polylines-ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![crate_type = "dylib"]

use std::mem;
use std::ffi::CStr;
use std::mem;

use libc::{c_char, size_t};
use svg2polylines::{CoordinatePair, parse};
use libc::{c_char, c_double, size_t};
use svg2polylines::{parse, CoordinatePair};

/// Structure that contains a pointer to the coordinate pairs as well as the
/// number of coordinate pairs. It is only used for C interop.
Expand All @@ -16,12 +16,12 @@ pub struct Polyline {
}

#[no_mangle]
pub extern fn svg_str_to_polylines(
pub extern "C" fn svg_str_to_polylines(
svg: *const c_char,
tol: c_double,
polylines: *mut *mut Polyline,
polylines_len: *mut size_t,
) -> u8 {

// Convert C string to Rust string
let c_str = unsafe {
assert!(!svg.is_null());
Expand All @@ -30,38 +30,45 @@ pub extern fn svg_str_to_polylines(
let r_str = c_str.to_str().unwrap();

// Process
match parse(r_str) {
match parse(r_str, tol) {
Ok(vec) => {
// Convert `Vec<Vec<CoordinatePair>>` to `Vec<Polyline>`
let mut tmp_vec: Vec<Polyline> = vec.into_iter().map(|mut v| {
v.shrink_to_fit();
let p = Polyline {
ptr: v.as_mut_ptr(),
len: v.len(),
};
mem::forget(v);
p
}).collect();
let mut tmp_vec: Vec<Polyline> = vec
.into_iter()
.map(|mut v| {
v.shrink_to_fit();
let p = Polyline {
ptr: v.as_mut_ptr(),
len: v.len(),
};
mem::forget(v);
p
})
.collect();
tmp_vec.shrink_to_fit();
assert!(tmp_vec.len() == tmp_vec.capacity());

// Return number of polylines
unsafe { *polylines_len = tmp_vec.len() as size_t; }
unsafe {
*polylines_len = tmp_vec.len() as size_t;
}

// Return pointer to data
unsafe { *polylines = tmp_vec.as_mut_ptr(); }
unsafe {
*polylines = tmp_vec.as_mut_ptr();
}

// Prevent memory from being deallocated
mem::forget(tmp_vec);

0
},
Err(_) => 1
}
Err(_) => 1,
}
}

#[no_mangle]
pub extern fn free_polylines(polylines: *mut Polyline, polylines_len: size_t) {
pub extern "C" fn free_polylines(polylines: *mut Polyline, polylines_len: size_t) {
unsafe {
for p in Vec::from_raw_parts(polylines, polylines_len as usize, polylines_len as usize) {
Vec::from_raw_parts(p.ptr, p.len as usize, p.len as usize);
Expand Down
6 changes: 3 additions & 3 deletions svg2polylines/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ fn main() {
// Argument parsing
let args: Vec<_> = env::args().collect();
match args.len() {
2 => {},
2 => {}
_ => {
println!("Usage: {} <path/to/file.svg>", args[0]);
exit(1);
},
}
};

// Load file
Expand All @@ -26,7 +26,7 @@ fn main() {
file.read_to_string(&mut s).unwrap();

// Parse data
let polylines: Vec<Polyline> = svg2polylines::parse(&s).unwrap_or_else(|e| {
let polylines: Vec<Polyline> = svg2polylines::parse(&s, 0.15).unwrap_or_else(|e| {
println!("Error: {}", e);
exit(2);
});
Expand Down
33 changes: 18 additions & 15 deletions svg2polylines/examples/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::fs;
use std::io::Read;
use std::process::exit;

use drag_controller::{DragController, Drag};
use drag_controller::{Drag, DragController};
use env_logger;
use piston_window::{PistonWindow, WindowSettings, OpenGL, Transformed, clear, line};
use piston_window::math::Matrix2d;
use piston_window::{clear, line, OpenGL, PistonWindow, Transformed, WindowSettings};
use svg2polylines::{self, Polyline};

fn main() {
Expand All @@ -18,11 +18,11 @@ fn main() {
// Argument parsing
let args: Vec<_> = env::args().collect();
match args.len() {
2 => {},
2 => {}
_ => {
println!("Usage: {} <path/to/file.svg>", args[0]);
exit(1);
},
}
};

// Load file
Expand All @@ -31,7 +31,7 @@ fn main() {
file.read_to_string(&mut s).unwrap();

// Parse data
let polylines: Vec<Polyline> = svg2polylines::parse(&s).unwrap_or_else(|e| {
let polylines: Vec<Polyline> = svg2polylines::parse(&s, 0.15).unwrap_or_else(|e| {
println!("Error: {}", e);
exit(2);
});
Expand All @@ -51,8 +51,7 @@ fn main() {
let black = [0.0, 0.0, 0.0, 1.0];
let radius = 1.0;
let mut drag = DragController::new();
let mut translate: Matrix2d = [[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0]];
let mut translate: Matrix2d = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
let mut translate_tmp: Matrix2d = translate.clone();
let mut translate_start = None;
while let Some(e) = window.next() {
Expand All @@ -61,18 +60,18 @@ fn main() {
Drag::Start(x, y) => {
translate_start = Some((x, y));
true
},
}
Drag::Move(x, y) => {
let start_x = translate_start.unwrap().0;
let start_y = translate_start.unwrap().1;
translate_tmp = translate.trans(x - start_x, y - start_y);
true
},
}
Drag::End(..) => {
translate_start = None;
translate = translate_tmp;
false
},
}
// Continue dragging when receiving focus.
Drag::Interrupt => true,
}
Expand All @@ -81,11 +80,15 @@ fn main() {
clear([1.0; 4], g);
for polyline in &polylines {
for pair in polyline.windows(2) {
line(black,
radius,
[pair[0].x, pair[0].y, pair[1].x, pair[1].y],
c.transform.append_transform(translate_tmp).scale(fscale, fscale),
g);
line(
black,
radius,
[pair[0].x, pair[0].y, pair[1].x, pair[1].y],
c.transform
.append_transform(translate_tmp)
.scale(fscale, fscale),
g,
);
}
}
});
Expand Down
Loading

0 comments on commit 608a229

Please sign in to comment.