Skip to content

Commit

Permalink
chore: clean up unused wasm code
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonflatval-cognite committed Aug 17, 2022
1 parent f053ef7 commit 776541a
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 606 deletions.
34 changes: 0 additions & 34 deletions viewer/wasm/src/bvh.rs

This file was deleted.

128 changes: 0 additions & 128 deletions viewer/wasm/src/bvh/bvh_node.rs

This file was deleted.

91 changes: 9 additions & 82 deletions viewer/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
use wasm_bindgen::prelude::*;
use web_sys::console;
extern crate console_error_panic_hook;

use std::vec::Vec;
use serde::Deserialize;
use std::fmt::Display;

mod point_octree;
mod shapes;
mod linalg;
mod parse_inputs;
mod bvh;

mod point_octree2;
mod point_octree;

use linalg::{Vec3WithIndex,BoundingBox,Vec3,to_bounding_box};
use linalg::to_bounding_box;

#[derive(Deserialize)]
pub struct InputBoundingBox {
Expand Down Expand Up @@ -42,98 +36,31 @@ pub struct InputShape {
oriented_box: Option<Box<InputOrientedBox>>
}

// When the `wee_alloc` feature is enabled, this uses `wee_alloc` as the global
// allocator.
//
// If you don't want to use `wee_alloc`, you can safely delete this.
/* #[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; */

fn init() -> () {
// This provides better error messages in debug mode.
// It's disabled in release mode so it doesn't bloat up the file size.
#[cfg(debug_assertions)]
console_error_panic_hook::set_once();
}


#[wasm_bindgen]
pub fn assign_points(input_shapes: js_sys::Array,
points: js_sys::Float32Array,
input_points: js_sys::Float32Array,
input_bounding_box: js_sys::Object,
input_point_offset: js_sys::Array) -> js_sys::Uint16Array {
init();

let point_offset = Vec3::new(input_point_offset.get(0).as_f64().unwrap(),
input_point_offset.get(1).as_f64().unwrap(),
input_point_offset.get(2).as_f64().unwrap());
let mut point_vec = parse_inputs::parse_points(&points, point_offset);


let mut point_vec = parse_inputs::parse_points(&input_points, input_point_offset);
let bounding_box = to_bounding_box(&input_bounding_box.into_serde::<InputBoundingBox>().unwrap());
let shape_vec = parse_inputs::parse_objects(input_shapes);

let mut input_shape_vec: Vec<InputShape> =
Vec::<InputShape>::with_capacity(input_shapes.length() as usize);

for value in input_shapes.iter() {
assert!(value.is_object());

let input_shape = value.into_serde::<InputShape>().unwrap();
input_shape_vec.push(input_shape);
}

let shape_vec = parse_inputs::parse_objects(input_shape_vec);

// let mut shapes_with_boxes: Vec<(BoundingBox, Box<dyn shapes::shape::Shape>)> = shape_vec.into_iter().map(|shape| (shape.create_bounding_box(), shape)).collect();

// let object_ids = js_sys::Uint16Array::new_with_length(points.length() / 3);
let object_ids = js_sys::Uint16Array::new_with_length(points.length() / 3).fill(0, 0, points.length() / 3);

/* let bounding_volume_hierarchy = bvh::BoundingVolumeHierarchy::new(&mut shapes_with_boxes[..]);
for i in 0..point_vec.len() {
let id = bounding_volume_hierarchy.get_object_id(&point_vec[i].vec);
object_ids.set_index(i as u32, id as u16);
} */
let object_ids = js_sys::Uint16Array::new_with_length(input_points.length() / 3)
.fill(0, 0, input_points.length() / 3);


// console::time();
/* let octree = point_octree::PointOctree::new(point_vec, bounding_box);
for shape in shape_vec.iter() {
let points_in_box = octree.get_points_in_box(&shape.create_bounding_box());
for point in points_in_box {
if shape.contains_point(&point.vec) {
object_ids.set_index(point.index, shape.get_object_id() as u16);
}
}
} */
let octree2 = point_octree2::point_octree2::PointOctree2::new(bounding_box, &mut point_vec);
let octree = point_octree::PointOctree::new(bounding_box, &mut point_vec);
for shape in shape_vec.iter() {
octree2.assign_object_ids(&shape.create_bounding_box(), shape, &object_ids);
octree.assign_object_ids(&shape.create_bounding_box(), shape, &object_ids);
}
/* for shape in shape_vec.iter() {
let points_in_box = octree2.get_points_in_box(&shape.create_bounding_box());
for point in points_in_box {
if shape.contains_point(&point.vec) {
object_ids.set_index(point.index, shape.get_object_id() as u16);
}
}
} */

/* for shape in shape_vec.iter() {
let mut point_callback = |points: &[Vec3WithIndex]| {
for point in points {
if shape.contains_point(&point.vec) {
object_ids.set_index(point.index, shape.get_object_id() as u16);
}
}
};
octree2.traverse_points_in_box(&shape.create_bounding_box(), &mut point_callback);
} */
// console::time_end();


object_ids
}
39 changes: 22 additions & 17 deletions viewer/wasm/src/parse_inputs.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
use nalgebra::Const;
use crate::linalg::{Vec3WithIndex, vec3, Vec3, Mat4};

use std::vec::Vec;

use crate::linalg::{Vec3WithIndex, vec3, Vec3, Mat4};
use crate::shapes;

use crate::{InputShape,InputCylinder,InputOrientedBox};

pub fn parse_points(array: &js_sys::Float32Array,
point_offset: Vec3) -> Vec<Vec3WithIndex> {
pub fn parse_points(input_array: &js_sys::Float32Array,
input_point_offset: js_sys::Array) -> Vec<Vec3WithIndex> {

let point_offset = Vec3::new(input_point_offset.get(0).as_f64().unwrap(),
input_point_offset.get(1).as_f64().unwrap(),
input_point_offset.get(2).as_f64().unwrap());

let num_points = array.length() / 3;
let mut vec = Vec::<Vec3WithIndex>::with_capacity(num_points as usize);
let num_points = input_array.length() / 3;
let mut point_vec = Vec::<Vec3WithIndex>::with_capacity(num_points as usize);

for i in 0..num_points {
vec.push(Vec3WithIndex {
vec: vec3(array.get_index(3 * i + 0) as f64,
array.get_index(3 * i + 1) as f64,
array.get_index(3 * i + 2) as f64) + point_offset,
point_vec.push(Vec3WithIndex {
vec: vec3(input_array.get_index(3 * i + 0) as f64,
input_array.get_index(3 * i + 1) as f64,
input_array.get_index(3 * i + 2) as f64) + point_offset,
index: i
});
}

vec
point_vec
}

fn create_cylinder(input: InputCylinder, id: u32) -> Box<shapes::cylinder::Cylinder> {
Expand Down Expand Up @@ -54,12 +56,15 @@ fn create_shape(obj: InputShape) -> Box<dyn shapes::shape::Shape> {
}
}

pub fn parse_objects(shapes: Vec<InputShape>) -> Vec<Box<dyn shapes::shape::Shape>> {
let mut new_vec = Vec::<Box<dyn shapes::shape::Shape>>::with_capacity(shapes.len());
pub fn parse_objects(input_shapes: js_sys::Array) -> Vec<Box<dyn shapes::shape::Shape>> {
let mut shape_vec = Vec::<Box<dyn shapes::shape::Shape>>::with_capacity(input_shapes.length() as usize);

for value in input_shapes.iter() {
assert!(value.is_object());

for obj in shapes.into_iter() {
new_vec.push(create_shape(obj));
let input_shape = value.into_serde::<InputShape>().unwrap();
shape_vec.push(create_shape(input_shape));
}

new_vec
shape_vec
}
28 changes: 2 additions & 26 deletions viewer/wasm/src/point_octree.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
use crate::Vec3WithIndex;

use std::vec::Vec;

pub mod point_octree;
mod octree_node;
use octree_node::*;

use crate::linalg::{Vec3, BoundingBox};

pub struct PointOctree {
root: OctreeNode
}

impl PointOctree {
pub fn new(points: Vec<Vec3WithIndex>, bounding_box: BoundingBox) -> PointOctree {
PointOctree {
root: OctreeNode::new(points, bounding_box)
}
}

pub fn get_points_in_box(&self, bounding_box: &BoundingBox) -> Vec<Vec3WithIndex> {
let mut v: Vec<Vec3WithIndex> = Vec::new();

self.root.get_points_in_box(bounding_box, &mut v);

v
}
}
pub use point_octree::*;
Loading

0 comments on commit 776541a

Please sign in to comment.