Skip to content

Commit

Permalink
[FIX] : fix cube overlap issues
Browse files Browse the repository at this point in the history
[ADD] : add a example 3
  • Loading branch information
aiekick committed Aug 11, 2023
1 parent a1ec8ad commit d3187f0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 61 deletions.
11 changes: 6 additions & 5 deletions examples/sample1.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::time::Instant;

fn main() {
let now = Instant::now();
let mut vox = vox_writer::VoxWriter::create_empty();

for i in 0..1000 {
for j in 0..1000 {
let cube_pos =
f64::floor(f64::sin((i * i + j * j) as f64 / 50000.0) * 150.0) + 150.0;
let cube_pos = f64::floor(f64::sin((i * i + j * j) as f64 / 50000.0) * 150.0) + 150.0;
let cube_color = (i + j) % 255 + 1;
vox.add_voxel(i, j, cube_pos as i32, cube_color);
}
}

vox.print_stats();
vox.save_to_file("output_voxwriter.vox".to_string())
.expect("Fail to save vox file");
vox.print_stats();
println!("Elapsed time : {} secs", now.elapsed().as_secs_f32());
}
38 changes: 23 additions & 15 deletions examples/sample2.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use std::time::Instant;

fn main() {
let now = Instant::now();
let mut vox = vox_writer::VoxWriter::create_empty();
const SIZE:i32 = 1000;
const ZOOM_XZ:f64 = 5.0;
const ZOOM_Y:f64 = 5.0;
const ITERATIONS:i32 = 5;
fn mix(x:f64, y:f64, a:f64) -> f64 {
const SIZE: i32 = 1000;
const ZOOM_XZ: f64 = 5.0;
const ZOOM_Y: f64 = 5.0;
const ITERATIONS: i32 = 5;
let mut kk;
let mut hh;
fn mix(x: f64, y: f64, a: f64) -> f64 {
x * (1.0 - a) + y * a
}
for i in 0..SIZE {
Expand All @@ -18,15 +23,18 @@ fn main() {
for j in 0..SIZE {
let mut rev_y = (j as f64 * 2.0 / SIZE as f64 - 1.0) * ZOOM_Y;
let mut rev_x = path;
let mut kk = 1.0;
let mut hh = 1.0;
kk = 1.0;
hh = 1.0;
for _idx in 0..ITERATIONS {
let rev_x_squared = rev_x * rev_x;
let rev_y_squared = rev_y * rev_y;
hh *= 4.0 * kk;
kk = rev_x * rev_x + rev_y * rev_y;
if kk > 4.0 { break; }
let tmp_x = rev_x;
rev_x = rev_x * rev_x - rev_y * rev_y + cx;
rev_y = 2.0 * tmp_x * rev_y + cy;
kk = rev_x_squared + rev_y_squared;
if kk > 4.0 {
break;
}
rev_y = 2.0 * rev_x * rev_y + cy;
rev_x = rev_x_squared - rev_y_squared + cx;
}
let df = f64::sqrt(kk / hh) * f64::log10(kk);
if f64::abs(df) - 0.01 < 0.0 {
Expand All @@ -36,8 +44,8 @@ fn main() {
}
}
}

vox.print_stats();
vox.save_to_file("julia_revolute_voxwriter.vox".to_string())
.expect("Fail to save vox file");
}
vox.print_stats();
println!("Elapsed time : {} secs", now.elapsed().as_secs_f32());
}
17 changes: 17 additions & 0 deletions examples/sample3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::time::Instant;

fn main() {
let now = Instant::now();
let mut vox = vox_writer::VoxWriter::create_empty();
for i in 0..300 {
for j in 0..300 {
for k in 0..300 {
vox.add_voxel(i, j, k, 100);
}
}
}
vox.save_to_file("output_cube_voxwriter.vox".to_string())
.expect("Fail to save vox file");
vox.print_stats();
println!("Elapsed time : {} secs", now.elapsed().as_secs_f32());
}
73 changes: 32 additions & 41 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ mod vox_writer {
/// println!("generate_julia_revolute Elapsed Time : {:.2?}", now.elapsed());
/// }

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct Point3<T> {
pub x: T,
Expand All @@ -115,7 +115,7 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct AABBCC {
lower_bound: Point3<f64>, // the lower left vertex
Expand Down Expand Up @@ -147,7 +147,7 @@ mod vox_writer {
self.upper_bound.z = f64::max(self.upper_bound.z, v_pt.z);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

fn get_id_char(a: char, b: char, c: char, d: char) -> u32 {
return ((a as i32) | ((b as i32) << 8) | ((c as i32) << 16) | ((d as i32) << 24)) as u32;
Expand All @@ -167,13 +167,12 @@ mod vox_writer {
assert_eq!(get_id_u8(86, 79, 88, 32), 542658390);
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct DICTstring {
buffer: CString, // non utf8
}


impl DICTstring {
fn create(v_buffer: CString) -> Self {
Self { buffer: v_buffer }
Expand All @@ -198,7 +197,8 @@ mod vox_writer {
// dont use mem::size_of::<char>() in rust because its unicode so on 4 bytes
// prefer use u8 instead
return mem::size_of::<i32>()
+ mem::size_of::<u8>() * (self.buffer.as_bytes().len() as usize); // prefer use u8 instead
+ mem::size_of::<u8>() * (self.buffer.as_bytes().len() as usize);
// prefer use u8 instead
}
}

Expand All @@ -214,19 +214,19 @@ mod vox_writer {

#[test]
fn test_dictstring_filled_get_size() {
let stru =
DICTstring::create_from_string(CString::new("toto va au zoo et c'est beau").unwrap());
let stru = DICTstring::create_from_string(
CString::new("toto va au zoo et c'est beau").unwrap(),
);
assert_eq!(stru.get_size(), 32);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct DICTitem {
key: DICTstring,
value: DICTstring,
}


#[allow(dead_code)]
impl DICTitem {
fn create_empty() -> Self {
Expand Down Expand Up @@ -254,14 +254,13 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct DICT {
count: i32,
keys: Vec<DICTitem>,
}


impl DICT {
fn create_empty() -> Self {
Self {
Expand Down Expand Up @@ -293,7 +292,7 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct Ntrn {
node_id: i32,
Expand All @@ -305,7 +304,6 @@ mod vox_writer {
frames: Vec<DICT>,
}


impl Ntrn {
fn create(count_frames: i32) -> Self {
let mut _frames: Vec<DICT> = vec![];
Expand Down Expand Up @@ -363,7 +361,6 @@ mod vox_writer {
child_nodes: Vec<i32>,
}


impl Ngrp {
fn create(count: i32) -> Self {
let mut nodes: Vec<i32> = vec![];
Expand Down Expand Up @@ -410,14 +407,13 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct Model {
model_id: i32,
model_attribs: DICT,
}


impl Model {
fn create_empty() -> Self {
Self {
Expand All @@ -437,7 +433,7 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct Nshp {
node_id: i32,
Expand All @@ -446,7 +442,6 @@ mod vox_writer {
models: Vec<Model>,
}


impl Nshp {
fn create(count: i32) -> Self {
let mut _models: Vec<Model> = vec![];
Expand Down Expand Up @@ -490,15 +485,14 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct LAYR {
node_id: i32,
node_attribs: DICT,
reserved_id: i32,
}


#[allow(dead_code)]
impl LAYR {
fn create_empty() -> Self {
Expand Down Expand Up @@ -530,15 +524,14 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct Size {
size_x: i32,
size_y: i32,
size_z: i32,
}


impl Size {
fn create_empty() -> Self {
Self {
Expand Down Expand Up @@ -569,18 +562,15 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct XYZI {
voxels: Vec<u8>,
}


impl XYZI {
fn create_empty() -> Self {
Self {
voxels: vec![],
}
Self { voxels: vec![] }
}

fn write(&mut self, mut fp: &File) -> std::io::Result<()> {
Expand All @@ -607,13 +597,12 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct RGBA {
colors: Vec<i32>,
}


impl RGBA {
fn create_empty() -> Self {
Self {
Expand Down Expand Up @@ -647,7 +636,7 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

struct VoxCube {
cube_id: i32,
Expand Down Expand Up @@ -691,7 +680,7 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

trait Memory<A: Eq + Hash, B: Eq + Hash, C: Eq + Hash> {
fn get(&self, a: &A, b: &B, c: &C) -> Option<&i32>;
Expand Down Expand Up @@ -723,7 +712,7 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

#[doc = "the Vox file format writer"]
pub struct VoxWriter {
Expand Down Expand Up @@ -834,9 +823,9 @@ mod vox_writer {
_cube.tx = v_x;
_cube.ty = v_y;
_cube.tz = v_z;
_cube.size.size_x = self.max_voxel_per_cube_x + 1;
_cube.size.size_y = self.max_voxel_per_cube_y + 1;
_cube.size.size_z = self.max_voxel_per_cube_z + 1;
_cube.size.size_x = self.max_voxel_per_cube_x;
_cube.size.size_y = self.max_voxel_per_cube_y;
_cube.size.size_z = self.max_voxel_per_cube_z;
self.cubes.push(_cube);
}
if cube_id < self.cubes.len() {
Expand Down Expand Up @@ -1009,10 +998,12 @@ mod vox_writer {
println!("---- Stats -----");
let count_cubes = self.cubes.len();
println!("count cubes : {}", count_cubes);
println!("Volume : {} x {} x {}",
self.max_volume.size().x,
self.max_volume.size().y,
self.max_volume.size().z);
println!(
"Volume : {} x {} x {}",
self.max_volume.size().x,
self.max_volume.size().y,
self.max_volume.size().z
);
let mut count_voxels: u64 = 0;
for i in 0..count_cubes {
let c = self.cubes.get(i).unwrap();
Expand All @@ -1023,7 +1014,7 @@ mod vox_writer {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
}

pub use crate::vox_writer::*;

0 comments on commit d3187f0

Please sign in to comment.