|
|
|
@ -4,20 +4,22 @@
|
|
|
|
|
extern crate obj;
|
|
|
|
|
extern crate noise;
|
|
|
|
|
extern crate cgmath;
|
|
|
|
|
extern crate rayon;
|
|
|
|
|
|
|
|
|
|
use std::io::Result;
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use std::f32::consts::PI;
|
|
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
use std::io::{BufWriter, Write};
|
|
|
|
|
use obj::{Obj, SimplePolygon, IndexTuple};
|
|
|
|
|
use noise::Fbm;
|
|
|
|
|
//use noise::Seedable;
|
|
|
|
|
//use noise::MultiFractal;
|
|
|
|
|
use noise::MultiFractal;
|
|
|
|
|
use noise::NoiseModule;
|
|
|
|
|
use cgmath::Vector3;
|
|
|
|
|
use cgmath::ElementWise;
|
|
|
|
|
|
|
|
|
|
use rayon::prelude::*;
|
|
|
|
|
|
|
|
|
|
// A function called test that takes in 1 32-bit integer
|
|
|
|
|
// and returns a 32-bit integer.
|
|
|
|
|
/*
|
|
|
|
@ -45,12 +47,11 @@ fn distance_a_to_b(ax: f32, ay: f32, bx: f32, by: f32) -> f32 {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn return_at(x: f32, y: f32, fbmnoise: &Fbm<f32>) -> f32 {
|
|
|
|
|
let n = 20.0 * fbmnoise.get([x, y]);
|
|
|
|
|
let n = n - n.floor();
|
|
|
|
|
let zero_to_one_noise = (1.0 + fbmnoise.get([x, y])) * 0.5;
|
|
|
|
|
let z = 0.0f32;
|
|
|
|
|
let m = z.max(1.0 - 1.0 * distance_a_to_b(x, y, 0.5, 0.5));
|
|
|
|
|
|
|
|
|
|
let m = distance_a_to_b(x, y, 0.5, 0.5);
|
|
|
|
|
|
|
|
|
|
return (m * 0.15) + (n * 0.85);
|
|
|
|
|
return (zero_to_one_noise * 0.90) + (m * 0.10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn find_l_w(obj: &Obj<SimplePolygon>) -> (f32, f32) {
|
|
|
|
@ -85,16 +86,8 @@ fn find_l_w(obj: &Obj<SimplePolygon>) -> (f32, f32) {
|
|
|
|
|
* to the initial input obj's position.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
fn calculate_angle(current_duplicate: i32, current_layer: i32) -> f32 {
|
|
|
|
|
(current_duplicate / (2 * current_layer)) as f32 * (0.5 * PI)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn calculate_translation(length: f32, width: f32, angle: f32) -> Vector3<f32> {
|
|
|
|
|
Vector3::new(length * angle.cos(), width * angle.sin(), 0.0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn duplicate(
|
|
|
|
|
positions: Vec<Vector3<f32>>,
|
|
|
|
|
positions: &[Vector3<f32>],
|
|
|
|
|
translation: Vector3<f32>,
|
|
|
|
|
height_scalar: f32,
|
|
|
|
|
) -> Vec<Vector3<f32>> {
|
|
|
|
@ -104,14 +97,14 @@ fn duplicate(
|
|
|
|
|
Vector3::new(
|
|
|
|
|
point.x + translation.x,
|
|
|
|
|
point.y + translation.y,
|
|
|
|
|
point.z * height_scalar,
|
|
|
|
|
point.z * height_scalar * 2.0,
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn generate_city(
|
|
|
|
|
positions: Vec<Vector3<f32>>,
|
|
|
|
|
positions: &[Vector3<f32>],
|
|
|
|
|
layers: i32,
|
|
|
|
|
spacing: f32,
|
|
|
|
|
length: f32,
|
|
|
|
@ -120,47 +113,77 @@ fn generate_city(
|
|
|
|
|
let length = length + spacing;
|
|
|
|
|
let width = width + spacing;
|
|
|
|
|
|
|
|
|
|
let mut temp = Vector3::new(0.0, 0.0, 0.0);
|
|
|
|
|
let fbm: Fbm<f32> = Fbm::new()
|
|
|
|
|
.set_octaves(1)
|
|
|
|
|
.set_frequency(6.0)
|
|
|
|
|
.set_persistence(3.0)
|
|
|
|
|
.set_lacunarity(30.0);
|
|
|
|
|
|
|
|
|
|
let mut coord = Vector3::new(0.5, 0.5, 0.0);
|
|
|
|
|
let mut output_positions = duplicate(
|
|
|
|
|
positions,
|
|
|
|
|
Vector3::new(0.0, 0.0, 0.0),
|
|
|
|
|
return_at(0.5, 0.5, &fbm),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let fbm: Fbm<f32> = Fbm::new();
|
|
|
|
|
let rest_vec: Vec<_> = (1..layers)
|
|
|
|
|
.into_par_iter()
|
|
|
|
|
.flat_map(|current_layer| {
|
|
|
|
|
(0..(current_layer * 8))
|
|
|
|
|
.into_par_iter()
|
|
|
|
|
.flat_map(|current_duplicate| {
|
|
|
|
|
let current_ratio = current_duplicate as f32 / (current_layer as f32 * 8.0);
|
|
|
|
|
|
|
|
|
|
(1..layers).fold(positions.clone(), |acc_positions, current_layer| {
|
|
|
|
|
temp.x = -length * (current_layer as f32);
|
|
|
|
|
temp.y = -width * (current_layer as f32);
|
|
|
|
|
let unit_translation = if current_ratio <= 1.0 / 4.0 {
|
|
|
|
|
Vector3::new(1.0, -1.0 + (current_ratio * 8.0), 0.0)
|
|
|
|
|
} else if current_ratio <= 2.0 / 4.0 {
|
|
|
|
|
Vector3::new(1.0 - ((current_ratio) - 1.0 / 4.0) * 8.0, 1.0, 0.0)
|
|
|
|
|
} else if current_ratio <= 3.0 / 4.0 {
|
|
|
|
|
Vector3::new(-1.0, 1.0 - ((current_ratio) - 2.0 / 4.0) * 8.0, 0.0)
|
|
|
|
|
} else {
|
|
|
|
|
Vector3::new(-1.0 + ((current_ratio) - 3.0 / 4.0) * 8.0, -1.0, 0.0)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
(0..(current_layer * 8)).fold(acc_positions, |mut acc_positions, current_duplicate| {
|
|
|
|
|
let translation = current_layer as f32 *
|
|
|
|
|
Vector3::new(length * unit_translation.x, width * unit_translation.y, 0.0);
|
|
|
|
|
|
|
|
|
|
let angle = calculate_angle(current_duplicate, current_layer);
|
|
|
|
|
// gets into range -1 to +1
|
|
|
|
|
let coord = 1.0 / 5.0 *
|
|
|
|
|
translation.mul_element_wise(
|
|
|
|
|
Vector3::new(1.0 / length as f32, 1.0 / width as f32, 0.0),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let translation = calculate_translation(length, width, angle);
|
|
|
|
|
temp += translation;
|
|
|
|
|
// gets into range -0.4 to +0.4
|
|
|
|
|
let coord = 0.4 * coord;
|
|
|
|
|
|
|
|
|
|
coord += translation.mul_element_wise(Vector3::new(
|
|
|
|
|
(2.0 / (layers as f32 * 2.0 - 1.0)),
|
|
|
|
|
(2.0 / (layers as f32 * 2.0 - 1.0)),
|
|
|
|
|
0.0,
|
|
|
|
|
));
|
|
|
|
|
// gets into range 0.1 to 0.9
|
|
|
|
|
let coord = coord + Vector3::new(0.5, 0.5, 0.0);
|
|
|
|
|
|
|
|
|
|
let height_scalar = return_at(coord.x, coord.y, &fbm);
|
|
|
|
|
|
|
|
|
|
acc_positions.extend(duplicate(positions.clone(), temp, height_scalar));
|
|
|
|
|
duplicate(&positions, translation, height_scalar)
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
acc_positions
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
output_positions.extend(rest_vec);
|
|
|
|
|
|
|
|
|
|
output_positions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn copy_faces(
|
|
|
|
|
faces: Vec<Vec<IndexTuple>>,
|
|
|
|
|
faces: &[Vec<IndexTuple>],
|
|
|
|
|
n_positions: usize,
|
|
|
|
|
layers: usize,
|
|
|
|
|
) -> Vec<Vec<IndexTuple>> {
|
|
|
|
|
(0..(2 * layers - 1).pow(2)).fold(Vec::new(), |mut acc_faces, current_value| {
|
|
|
|
|
(0..(2 * layers - 1).pow(2))
|
|
|
|
|
.into_par_iter()
|
|
|
|
|
.flat_map(|current_value| {
|
|
|
|
|
let offset = n_positions * current_value + 1;
|
|
|
|
|
|
|
|
|
|
acc_faces.extend(faces.iter().map(|current_face| {
|
|
|
|
|
faces
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|current_face| {
|
|
|
|
|
current_face
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|index_tuple| {
|
|
|
|
@ -171,14 +194,14 @@ fn copy_faces(
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
acc_faces
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn save(filename: &Path, positions: Vec<Vector3<f32>>, faces: Vec<Vec<IndexTuple>>) {
|
|
|
|
|
let mut file = File::create(filename).unwrap();
|
|
|
|
|
let mut file = BufWriter::new(File::create(filename).unwrap());
|
|
|
|
|
|
|
|
|
|
for pos in positions {
|
|
|
|
|
write!(file, "v {} {} {}\n", pos[0], pos[1], pos[2]).unwrap();
|
|
|
|
@ -202,34 +225,32 @@ fn save(filename: &Path, positions: Vec<Vector3<f32>>, faces: Vec<Vec<IndexTuple
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
|
|
|
|
|
let path = Path::new("data/test.obj");
|
|
|
|
|
let path = Path::new("data/teapot.obj");
|
|
|
|
|
let maybe_obj: Result<Obj<SimplePolygon>> = Obj::load(&path);
|
|
|
|
|
|
|
|
|
|
if let Ok(obj) = maybe_obj {
|
|
|
|
|
println!("Position: {:?}", obj.position);
|
|
|
|
|
|
|
|
|
|
let layers = 10;
|
|
|
|
|
let spacing = 1.0;
|
|
|
|
|
|
|
|
|
|
let (length, width) = find_l_w(&obj);
|
|
|
|
|
|
|
|
|
|
println!("Length: {} Width: {}", length, width);
|
|
|
|
|
|
|
|
|
|
let input_positions = obj.position
|
|
|
|
|
let input_positions: Vec<_> = obj.position
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|point| Vector3::new(point[0], point[1], point[2]))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let output_positions = generate_city(input_positions, layers, spacing, length, width);
|
|
|
|
|
|
|
|
|
|
println!("Objects: {:?}", obj.objects[0].groups[0].polys[0]);
|
|
|
|
|
let output_positions = generate_city(&input_positions, layers, spacing, length, width);
|
|
|
|
|
|
|
|
|
|
let output_faces = copy_faces(
|
|
|
|
|
obj.objects[0].groups[0].polys.clone(),
|
|
|
|
|
&obj.objects[0].groups[0].polys,
|
|
|
|
|
obj.position.len(),
|
|
|
|
|
layers as usize,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
save(Path::new("build/noice.obj"), output_positions, output_faces);
|
|
|
|
|
save(
|
|
|
|
|
Path::new("target/noice.obj"),
|
|
|
|
|
output_positions,
|
|
|
|
|
output_faces,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|