Skip to content

Commit

Permalink
[ADD] : add samples in examples directorys
Browse files Browse the repository at this point in the history
[ADD] : published the VoxWriter::create function and the module
  • Loading branch information
aiekick committed Aug 10, 2023
1 parent 09f1a8f commit 82438dd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
16 changes: 16 additions & 0 deletions examples/sample1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn main() {
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_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");
}
43 changes: 43 additions & 0 deletions examples/sample2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
fn main() {
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 {
x * (1.0 - a) + y * a
}
for i in 0..SIZE {
let px = (i as f64 * 2.0 / SIZE as f64 - 1.0) * ZOOM_XZ;
for k in 0..SIZE {
let pz = (k as f64 * 2.0 / SIZE as f64 - 1.0) * ZOOM_XZ;
let an = f64::atan2(px, pz);
let cx = mix(0.2, -0.5, f64::sin(an * 2.0));
let cy = mix(0.5, 0.0, f64::sin(an * 3.0));
let path = f64::sqrt(px * px + pz * pz) - 3.0;
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;
for _idx in 0..ITERATIONS {
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;
}
let df = f64::sqrt(kk / hh) * f64::log10(kk);
if f64::abs(df) - 0.01 < 0.0 {
let cube_color = ((f64::sin(rev_x + rev_y) * 0.5 + 0.5) * 6.0) as i32 + 249;
vox.add_voxel(i, k, j, cube_color); // magicavoxel use the z as up axis
}
}
}
}

vox.print_stats();
vox.save_to_file("julia_revolute_voxwriter.vox".to_string())
.expect("Fail to save vox file");
}
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
MIT License
Copyright (c) 2022 Stephane Cuillerdier (aka Aiekick)
Copyright (c) 2022-2023 Stephane Cuillerdier (aka Aiekick)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -747,7 +747,7 @@ mod vox_writer {
#[allow(dead_code)]
impl VoxWriter {
#[doc = "create an empty volume of limitx x limity x limitz voxels"]
fn create(limitx: i32, limity: i32, limitz: i32) -> Self {
pub fn create(limitx: i32, limity: i32, limitz: i32) -> Self {
Self {
mv_version: 150,
id_vox: get_id_char('V', 'O', 'X', ' '),
Expand Down Expand Up @@ -1026,3 +1026,4 @@ mod vox_writer {
///////////////////////////////////////////////////////////////////////////////////////////////////
}

pub use vox_writer::*;

0 comments on commit 82438dd

Please sign in to comment.