Allocate memory for efficiently.
This should generally improve rendering time.
This commit is contained in:
1 parent
8ba6d56512
commit
d84e78b29a
3 files changed
+41
-66
No files matched your search
+12
-38
@@ -23,14 +23,6 @@ fn new_triangle(p1: &Vertex, p2: &Vertex, p3: &Vertex) -> stl_io::Triangle {
|
||||
}
|
||||
}
|
||||
|
||||
/// N steps around a circle, measured in radians.
|
||||
fn radian_steps(steps: u32) -> Vec<f32> {
|
||||
let counter = 0..steps;
|
||||
let stepsize = std::f64::consts::TAU / steps as f64;
|
||||
|
||||
counter.map(|i| (i as f64 * stepsize) as f32).collect()
|
||||
}
|
||||
|
||||
/// Find a point on the cirle at a given angle (in radians).
|
||||
fn vertex_on_circle(center: &Vertex, radius: f32, angle: f32) -> Vertex {
|
||||
let x = center[0] + (radius * angle.cos());
|
||||
@@ -41,10 +33,14 @@ fn vertex_on_circle(center: &Vertex, radius: f32, angle: f32) -> Vertex {
|
||||
}
|
||||
|
||||
fn vertexes_on_circle(layer: &Layer, steps: u32) -> Vec<Vertex> {
|
||||
radian_steps(steps)
|
||||
.iter()
|
||||
.map(|angle| vertex_on_circle(&layer.center, layer.radius, *angle))
|
||||
.collect()
|
||||
let mut result = Vec::with_capacity(steps as usize);
|
||||
let stepsize = std::f64::consts::TAU / steps as f64;
|
||||
|
||||
for i in 0..steps {
|
||||
let angle = (i as f64 * stepsize) as f32;
|
||||
result.push(vertex_on_circle(&layer.center, layer.radius, angle));
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn tube_base(center: &Vertex, edge: &[Vertex]) -> Vec<Triangle> {
|
||||
@@ -88,13 +84,15 @@ pub fn render(layers: &Layers, resolution: f32) -> Vec<Triangle> {
|
||||
let circle_steps =
|
||||
((layers.max_radius() * std::f32::consts::TAU) / resolution).max(64.0) as u32;
|
||||
|
||||
let mut layer_vertexes: Vec<Vec<Vertex>> = Vec::new();
|
||||
let mut layer_vertexes: Vec<Vec<Vertex>> = Vec::with_capacity(layers.layers.len());
|
||||
|
||||
for layer in &layers.layers {
|
||||
layer_vertexes.push(vertexes_on_circle(layer, circle_steps));
|
||||
}
|
||||
|
||||
let mut result: Vec<Triangle> = Vec::new();
|
||||
// Estimate triangle count: base + sides + top
|
||||
let estimated_triangles = layers.layers.len() * circle_steps as usize * 2;
|
||||
let mut result: Vec<Triangle> = Vec::with_capacity(estimated_triangles as usize);
|
||||
|
||||
result.append(&mut tube_base(&layers.base, &layer_vertexes[0]));
|
||||
for i in 0..layer_vertexes.len() - 1 {
|
||||
@@ -137,30 +135,6 @@ mod tests {
|
||||
assert_eq!(tri_big.normal, Normal::new([0.0, 0.0, 18.0]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn radian_steps_basic() {
|
||||
assert_eq!(radian_steps(0), []);
|
||||
assert_eq!(radian_steps(1), [0.0]);
|
||||
assert_eq!(radian_steps(2), [0.0, std::f32::consts::PI]);
|
||||
assert_eq!(
|
||||
radian_steps(3),
|
||||
[
|
||||
0.0,
|
||||
std::f32::consts::PI * 2.0 / 3.0,
|
||||
std::f32::consts::PI * 4.0 / 3.0
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
radian_steps(4),
|
||||
[
|
||||
0.0,
|
||||
std::f32::consts::PI * 0.5,
|
||||
std::f32::consts::PI,
|
||||
std::f32::consts::PI * 1.5,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
fn validate_vec_vertex_relative_eq(val: &Vec<Vertex>, expected: &Vec<Vertex>) {
|
||||
let mut pass = true;
|
||||
|
||||
|
||||
Reference in new issue
Block a user