Skip to content

Commit 093881c

Browse files
Implemented Rust-y interface for functions and added unit tests.
1 parent 6855c20 commit 093881c

File tree

1 file changed

+234
-6
lines changed

1 file changed

+234
-6
lines changed

src/lib.rs

Lines changed: 234 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,238 @@
11
mod bindings;
2+
use bindings::*;
23

3-
#[test]
4-
fn test_vx_voxelize_pc() {
5-
unsafe {
6-
let mesh = bindings::vx_mesh_alloc(16, 16);
7-
let _pointcloud = bindings::vx_voxelize_pc(mesh, 1.0, 1.0, 1.0, 1.0);
8-
//I'm not totally sure how to check if this stuff is actually valid, so we're just going to do nothing.
4+
#[derive(std::fmt::Debug)]
5+
pub struct Vertex {
6+
pub v: [f32;3]
7+
}
8+
9+
#[derive(std::fmt::Debug)]
10+
pub struct Vector3 {
11+
pub x: f32,
12+
pub y: f32,
13+
pub z: f32
14+
}
15+
16+
#[derive(std::fmt::Debug)]
17+
pub struct Colour {
18+
pub r: f32,
19+
pub g: f32,
20+
pub b: f32
21+
}
22+
23+
pub struct Mesh {
24+
pub vertices: Vec<Vertex>,
25+
pub colours: Vec<Colour>,
26+
pub normals: Vec<Vector3>,
27+
pub indices: Vec<u32>
28+
}
29+
30+
pub struct PointCloud {
31+
pub vertices: Vec<Vertex>,
32+
pub colours: Vec<Colour>
33+
}
34+
35+
impl Mesh {
36+
pub fn voxelize_pointcloud(&self, voxel_size: Vector3, precision: f32) -> PointCloud {
37+
let mesh = self.allocate();
38+
unsafe {
39+
let pointcloud = vx_voxelize_pc(mesh, voxel_size.x, voxel_size.y, voxel_size.z, precision);
40+
vx_mesh_free(mesh);
41+
PointCloud::from_vx(pointcloud)
42+
}
43+
}
44+
45+
pub fn voxelize(&self, voxel_size: Vector3, precision: f32) -> Mesh {
46+
let mesh = self.allocate();
47+
let mesh = unsafe {
48+
let voxelized = vx_voxelize(mesh, voxel_size.x, voxel_size.y, voxel_size.z, precision);
49+
vx_mesh_free(mesh);
50+
voxelized
51+
};
52+
Mesh::from_vx(mesh)
53+
}
54+
55+
pub fn voxelize_texture(&self, width: u32, height: u32, depth: u32) -> Vec<u32> {
56+
let mesh = self.allocate();
57+
let texture = unsafe {
58+
let voxelized = vx_voxelize_snap_3dgrid(mesh, width, height, depth);
59+
vx_mesh_free(mesh);
60+
voxelized
61+
};
62+
63+
let mut tex: Vec<u32> = Vec::new();
64+
let volume = width * height * depth;
65+
for i in 0..volume {
66+
let t = unsafe { *texture.offset(i as isize) };
67+
tex.push(t);
68+
}
69+
tex
70+
}
71+
72+
pub fn new(vertices: Vec<Vertex>, colours: Vec<Colour>, normals: Vec<Vector3>, indices: Vec<u32>) -> Mesh {
73+
Mesh {
74+
vertices,
75+
colours,
76+
normals,
77+
indices
78+
}
79+
}
80+
81+
fn allocate(&self) -> *mut vx_mesh {
82+
let mesh = unsafe {
83+
match self.colours.len() > 0 {
84+
true => vx_mesh_alloc(self.vertices.len() as i32, self.indices.len() as i32),
85+
false => vx_color_mesh_alloc(self.vertices.len() as i32, self.indices.len() as i32)
86+
}
87+
};
88+
89+
unsafe {
90+
let vertices = (*mesh).vertices;
91+
for i in 0..self.vertices.len() {
92+
let v = vx_vertex { v: self.vertices[i].v };
93+
vertices.offset(i as isize).write(v);
94+
}
95+
96+
let colours = (*mesh).colors;
97+
for i in 0..self.colours.len() {
98+
let colour = &self.colours[i];
99+
let c = vx_color { r: colour.r, g: colour.g, b: colour.b };
100+
colours.offset(i as isize).write(c);
101+
}
102+
103+
let normals = (*mesh).normals;
104+
for i in 0..self.normals.len() {
105+
let normal = &self.normals[i];
106+
let n = vx_vec3 { x: normal.x, y: normal.y, z: normal.z };
107+
normals.offset(i as isize).write(n);
108+
}
109+
110+
let indices = (*mesh).indices;
111+
let normalindices = (*mesh).normalindices;
112+
for i in 0..self.indices.len() {
113+
let index = self.indices[i];
114+
indices.offset(i as isize).write(index);
115+
116+
normalindices.offset(i as isize).write(index);
117+
}
118+
119+
(*mesh).nindices = self.indices.len();
120+
(*mesh).nvertices = self.vertices.len();
121+
(*mesh).nnormals = self.normals.len();
122+
}
123+
124+
mesh
125+
}
126+
127+
fn from_vx(mesh: *mut vx_mesh) -> Mesh {
128+
let mut vertices: Vec<Vertex> = Vec::new();
129+
let mut colours: Vec<Colour> = Vec::new();
130+
let mut normals: Vec<Vector3> = Vec::new();
131+
let mut indices: Vec<u32> = Vec::new();
132+
133+
unsafe {
134+
for i in 0..(*mesh).nvertices {
135+
let v = (*mesh).vertices.offset(i as isize);
136+
vertices.push(Vertex::new((*v).v));
137+
138+
let c = (*mesh).colors.offset(i as isize);
139+
colours.push(Colour::new((*c).r, (*c).g, (*c).b));
140+
}
141+
142+
for i in 0..(*mesh).nnormals {
143+
let n = (*mesh).normals.offset(i as isize);
144+
normals.push(Vector3::new((*n).x, (*n).y, (*n).z));
145+
}
146+
147+
for i in 0..(*mesh).nindices {
148+
let ind = (*mesh).indices.offset(i as isize);
149+
indices.push(*ind);
150+
}
151+
152+
vx_mesh_free(mesh);
153+
}
154+
155+
Mesh::new(vertices, colours, normals, indices)
156+
}
157+
}
158+
159+
impl Vertex {
160+
pub fn new(v: [f32;3]) -> Vertex {
161+
Vertex { v }
9162
}
163+
}
164+
165+
impl Colour {
166+
pub fn new(r: f32, g: f32, b: f32) -> Colour {
167+
Colour { r, g, b }
168+
}
169+
}
170+
171+
impl Vector3 {
172+
pub fn new(x: f32, y: f32, z: f32) -> Vector3 {
173+
Vector3 { x, y, z }
174+
}
175+
}
176+
177+
impl PointCloud {
178+
fn from_vx(pointcloud: *mut vx_point_cloud) -> PointCloud {
179+
let mut vertices: Vec<Vertex> = Vec::new();
180+
let mut colours: Vec<Colour> = Vec::new();
181+
unsafe {
182+
for i in 0..(*pointcloud).nvertices {
183+
let v = (*pointcloud).vertices.offset(i as isize);
184+
vertices.push(Vertex::new((*v).v));
185+
186+
let c = (*pointcloud).colors.offset(i as isize);
187+
colours.push(Colour::new((*c).r, (*c).g, (*c).b));
188+
}
189+
190+
vx_point_cloud_free(pointcloud);
191+
}
192+
193+
PointCloud { vertices, colours }
194+
}
195+
}
196+
197+
#[test]
198+
fn test_voxelize_pointcloud() {
199+
let mesh = Mesh::new(
200+
vec![Vertex::new([-0.5, 0.5, 0.0]), Vertex::new([0.5, 0.5, 0.0]), Vertex::new([0.5, -0.5, 0.0]), Vertex::new([-0.5, 0.5, 0.0])],
201+
Vec::new(),
202+
vec![Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.0, 0.0, 1.0)],
203+
vec![0, 1, 3, 1, 2, 3]
204+
);
205+
206+
let pointcloud = mesh.voxelize_pointcloud(Vector3::new(0.2, 0.2, 0.2), 1.0);
207+
println!("Point Cloud Vertices: {:?}", pointcloud.vertices);
208+
println!("Point Cloud Colours: {:?}", pointcloud.colours);
209+
}
210+
211+
#[test]
212+
fn test_voxelize() {
213+
let mesh = Mesh::new(
214+
vec![Vertex::new([-0.5, 0.5, 0.0]), Vertex::new([0.5, 0.5, 0.0]), Vertex::new([0.5, -0.5, 0.0]), Vertex::new([-0.5, 0.5, 0.0])],
215+
Vec::new(),
216+
vec![Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.0, 0.0, 1.0)],
217+
vec![0, 1, 3, 1, 2, 3]
218+
);
219+
220+
let voxels = mesh.voxelize(Vector3::new(0.2, 0.2, 0.2), 1.0);
221+
println!("Voxel Mesh Vertices: {:?}", voxels.vertices);
222+
println!("Voxel Mesh Colours: {:?}", voxels.colours);
223+
println!("Voxel Mesh Normals: {:?}", voxels.normals);
224+
println!("Voxel Mesh Indices: {:?}", voxels.indices);
225+
}
226+
227+
#[test]
228+
fn test_voxelize_texture() {
229+
let mesh = Mesh::new(
230+
vec![Vertex::new([-0.5, 0.5, 0.0]), Vertex::new([0.5, 0.5, 0.0]), Vertex::new([0.5, -0.5, 0.0]), Vertex::new([-0.5, 0.5, 0.0])],
231+
Vec::new(),
232+
vec![Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.0, 0.0, 1.0)],
233+
vec![0, 1, 3, 1, 2, 3]
234+
);
235+
236+
let voxels = mesh.voxelize_texture(4, 4, 4);
237+
println!("Voxels: {:?}", voxels);
10238
}

0 commit comments

Comments
 (0)