Skip to content

Commit

Permalink
rayon
Browse files Browse the repository at this point in the history
  • Loading branch information
yjh0502 committed Mar 12, 2023
1 parent 2933f6f commit d77d946
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 36 deletions.
97 changes: 97 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ nalgebra = "0.31.4"
nom-gcode = "0.1.1"
ordslice = "0.3.0"
rangemap = "1.2.0"
rayon = "1.7.0"
stopwatch = "0.0.7"
12 changes: 11 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ impl Model {
self.add_face(coord, [0, -1, -1].into());
}

fn merge(&mut self, other: Self) {
for [i0, i1, i2, i3] in other.faces {
let i0 = self.add_vert(other.vertices[i0]);
let i1 = self.add_vert(other.vertices[i1]);
let i2 = self.add_vert(other.vertices[i2]);
let i3 = self.add_vert(other.vertices[i3]);
self.faces.push([i0, i1, i2, i3]);
}
}

fn serialize(&self, path: &str, offset: [f32; 3], scale: f32) -> Result<()> {
use std::io::Write;

Expand Down Expand Up @@ -511,7 +521,7 @@ fn generate_gcode<V: Voxel + Default>(
let out_filename = format!("{}/gcode_{:03}.obj", out_filename, layer_idx);
model.serialize(&out_filename, [-90f32, -90f32, 0f32], UNIT)?;
info!(
"Model::Serialize: took={}ms, filename={}",
"Model::serialize: took={}ms, filename={}",
sw.elapsed_ms(),
out_filename
);
Expand Down
79 changes: 44 additions & 35 deletions src/monotonicvoxel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{BoundingBox, Model, Voxel, VoxelIdx};
use rayon::prelude::*;
use std::collections::BTreeMap;
use std::ops::Range;

Expand Down Expand Up @@ -83,46 +84,54 @@ impl Voxel for MonotonicVoxel {
}

fn to_model(&self) -> Model {
let mut model = Model::default();

for (coord, ranges) in self.ranges.iter() {
for range in ranges {
if Range::is_empty(range) {
continue;
}

let x = coord[0];
let y = coord[1];
self.ranges
.par_iter()
.map(|(coord, ranges)| {
let mut model = Model::default();

for range in ranges {
if Range::is_empty(range) {
continue;
}

/*
if !self.occupied([x, y, range.start - 1].into()) {
model.add_face([x, y, range.start].into(), up);
}
*/
if !self.occupied([x, y, range.end].into()) {
model.add_face([x + 1, y + 1, range.end].into(), [-1, -1, 0].into());
}
let x = coord[0];
let y = coord[1];

let faces = [
([1, 0], [1, 1, 1], [0, -1, -1]),
// ([-1, 0], [0, 0, 0], [0, 1, 1]),
([0, 1], [1, 1, 1], [-1, 0, -1]),
// ([0, -1], [0, 0, 0], [1, 0, 1]),
];
/*
if !self.occupied([x, y, range.start - 1].into()) {
model.add_face([x, y, range.start].into(), up);
}
*/
if !self.occupied([x, y, range.end].into()) {
model.add_face([x + 1, y + 1, range.end].into(), [-1, -1, 0].into());
}

for ([dx, dy], offset, dir) in faces {
for z in range.clone() {
if !self.occupied([x + dx, y + dy, z].into()) {
model.add_face(
[x + offset[0], y + offset[1], z + offset[2]].into(),
dir.into(),
);
let faces = [
([1, 0], [1, 1, 1], [0, -1, -1]),
// ([-1, 0], [0, 0, 0], [0, 1, 1]),
([0, 1], [1, 1, 1], [-1, 0, -1]),
// ([0, -1], [0, 0, 0], [1, 0, 1]),
];

for ([dx, dy], offset, dir) in faces {
for z in range.clone() {
if !self.occupied([x + dx, y + dy, z].into()) {
model.add_face(
[x + offset[0], y + offset[1], z + offset[2]].into(),
dir.into(),
);
}
}
}
}
}
}

model
model
})
.reduce(
|| Model::default(),
|mut a, b| {
a.merge(b);
a
},
)
}
}

0 comments on commit d77d946

Please sign in to comment.