-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
120 lines (110 loc) · 4.04 KB
/
lib.rs
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
use hsl::HSL;
use scroll::Pread;
use wasm_bindgen::prelude::*;
// usize here represents 32bits for the pointer space, since wasm target is 32bit
const MEMORY_WIDTH: usize = 1_000_000 * 32;
#[wasm_bindgen]
pub fn get_memory() -> JsValue {
wasm_bindgen::memory()
}
#[wasm_bindgen]
pub fn get_memory_width() -> usize {
MEMORY_WIDTH
}
#[wasm_bindgen]
pub struct PCLDecoder {
copy_memory_buffer: [u8; MEMORY_WIDTH],
position_memory_buffer: [f32; MEMORY_WIDTH / 4],
color_memory_buffer: [f32; MEMORY_WIDTH / 4],
}
#[wasm_bindgen]
impl PCLDecoder {
#[wasm_bindgen(constructor)]
pub fn new() -> PCLDecoder {
console_error_panic_hook::set_once();
PCLDecoder {
copy_memory_buffer: [0; MEMORY_WIDTH],
position_memory_buffer: [0.0; MEMORY_WIDTH / 4],
color_memory_buffer: [0.0; MEMORY_WIDTH / 4],
}
}
pub fn get_copy_memory_ptr(&self) -> *const u8 {
self.copy_memory_buffer.as_ptr()
}
pub fn get_position_memory_ptr(&self) -> *const f32 {
self.position_memory_buffer.as_ptr()
}
pub fn get_color_memory_ptr(&self) -> *const f32 {
self.color_memory_buffer.as_ptr()
}
pub fn compute(
&mut self,
num_points: usize,
point_step: usize,
offset_x: usize,
offset_y: usize,
offset_z: usize,
offset_rgb: usize,
offset_intensity: usize,
use_intensity_channel: bool,
use_rainbow: bool,
) {
for i in 0..num_points {
let stride: usize = i * point_step;
self.position_memory_buffer[3 * i] = self
.copy_memory_buffer
.pread_with::<f32>(stride + offset_x, scroll::LE)
.unwrap();
self.position_memory_buffer[3 * i + 1] = self
.copy_memory_buffer
.pread_with::<f32>(stride + offset_y, scroll::LE)
.unwrap();
self.position_memory_buffer[3 * i + 2] = self
.copy_memory_buffer
.pread_with::<f32>(stride + offset_z, scroll::LE)
.unwrap();
if offset_rgb != 0 {
self.color_memory_buffer[3 * i] = (self
.copy_memory_buffer
.pread_with::<u8>(stride + offset_rgb + 2, scroll::LE)
.unwrap() as f32
) / 255.0;
self.color_memory_buffer[3 * i + 1] = (self
.copy_memory_buffer
.pread_with::<u8>(stride + offset_rgb + 1, scroll::LE)
.unwrap() as f32
) / 255.0;
self.color_memory_buffer[3 * i + 2] = (self
.copy_memory_buffer
.pread_with::<u8>(stride + offset_rgb, scroll::LE)
.unwrap() as f32
) / 255.0;
}
if offset_intensity != 0 && use_intensity_channel {
let intensity: f32 = self
.copy_memory_buffer
.pread_with::<f32>(stride + offset_intensity, scroll::LE)
.unwrap();
let normalized_intensity = if intensity < 360.0 {
intensity
} else {
360.0
};
if use_rainbow {
let color: (u8, u8, u8) = HSL {
h: normalized_intensity as f64,
s: 1.0,
l: 0.5,
}.to_rgb();
self.color_memory_buffer[3 * i] = (color.0 as f32) / 255.0;
self.color_memory_buffer[3 * i + 1] = (color.1 as f32) / 255.0;
self.color_memory_buffer[3 * i + 2] = (color.2 as f32) / 255.0;
} else {
self.color_memory_buffer[3 * i] = normalized_intensity / 360.0;
self.color_memory_buffer[3 * i + 1] = normalized_intensity / 360.0;
self.color_memory_buffer[3 * i + 2] = normalized_intensity / 360.0;
}
}
}
}
}