Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fix/rohan/clippy-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
ObiWanRohan committed Feb 21, 2022
2 parents def6d29 + 4ae6e66 commit 9ab0d78
Show file tree
Hide file tree
Showing 17 changed files with 237 additions and 127 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ cargo run -- -m spacer --export spacer.3mf
Some models have parameters that can be overridden. For example, to override the inner and outer radii of the spacer model:

``` sh
cargo run -- -m spacer --arguments outer=8.0 inner=5.0
cargo run -- -m spacer --parameters outer=8.0 --parameters inner=5.0
```


Expand Down
28 changes: 12 additions & 16 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,15 @@ impl Camera {
//
// To do that, first compute the model's highest point, as well as
// the furthest point from the origin, in x and y.
let highest_point = aabb.max.z();
let furthest_point = [
aabb.min.x().abs(),
aabb.max.x(),
aabb.min.y().abs(),
aabb.max.y(),
]
.into_iter()
.reduce(Scalar::max)
// `reduce` can only return `None`, if there are no items in
// the iterator. And since we're creating an array full of
// items above, we know this can't panic.
.unwrap();
let highest_point = aabb.max.z;
let furthest_point =
[aabb.min.x.abs(), aabb.max.x, aabb.min.y.abs(), aabb.max.y]
.into_iter()
.reduce(Scalar::max)
// `reduce` can only return `None`, if there are no items in
// the iterator. And since we're creating an array full of
// items above, we know this can't panic.
.unwrap();

// The actual furthest point is not far enough. We don't want the
// model to fill the whole screen.
Expand All @@ -74,7 +70,7 @@ impl Camera {

let initial_offset = {
let mut offset = aabb.center();
*offset.z_mut() = Scalar::ZERO;
offset.z = Scalar::ZERO;
-offset
};

Expand All @@ -84,8 +80,8 @@ impl Camera {

rotation: Transform::identity(),
translation: Translation::from([
initial_offset.x().into_f64(),
initial_offset.y().into_f64(),
initial_offset.x.into_f64(),
initial_offset.y.into_f64(),
-initial_distance.into_f64(),
]),
}
Expand Down
16 changes: 16 additions & 0 deletions src/graphics/config_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use wgpu_glyph::{
GlyphBrush, GlyphBrushBuilder, Section, Text,
};

use crate::math::Aabb;

use super::{draw_config::DrawConfig, COLOR_FORMAT};

#[derive(Debug)]
Expand Down Expand Up @@ -45,6 +47,7 @@ impl ConfigUi {
encoder: &mut wgpu::CommandEncoder,
view: &wgpu::TextureView,
surface_config: &wgpu::SurfaceConfiguration,
aabb: &Aabb<3>,
draw_config: &DrawConfig,
) -> Result<(), String> {
let mut section = Section::new().with_screen_position((50.0, 50.0));
Expand All @@ -62,6 +65,19 @@ impl ConfigUi {
section = section.add_text(text);
}

/* Render size of model bounding box */
let bbsize = aabb.size().components();
let info = format!(
"Model bounding box size: {:0.1} {:0.1} {:0.1}",
bbsize[0].into_f32(),
bbsize[1].into_f32(),
bbsize[2].into_f32()
);
let text = Text::new(&info)
.with_color([0.0, 0.0, 0.0, 1.0])
.with_scale(50.0);
section = section.add_text(text);

self.glyph_brush.queue(section);
self.glyph_brush.draw_queued(
device,
Expand Down
5 changes: 4 additions & 1 deletion src/graphics/geometries.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::math::Aabb;
use std::convert::TryInto;

use wgpu::util::DeviceExt;
Expand All @@ -8,19 +9,21 @@ use super::vertices::{Vertex, Vertices};
pub struct Geometries {
pub mesh: Geometry,
pub lines: Geometry,
pub aabb: Aabb<3>,
}

impl Geometries {
pub fn new(
device: &wgpu::Device,
mesh: &Vertices,
debug_info: &Vertices,
aabb: Aabb<3>,
) -> Self {
let mesh = Geometry::new(device, mesh.vertices(), mesh.indices());
let lines =
Geometry::new(device, debug_info.vertices(), debug_info.indices());

Self { mesh, lines }
Self { mesh, lines, aabb }
}
}

Expand Down
23 changes: 18 additions & 5 deletions src/graphics/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use wgpu::util::DeviceExt as _;
use wgpu_glyph::ab_glyph::InvalidFont;
use winit::dpi::PhysicalSize;

use crate::{camera::Camera, window::Window};
use crate::{camera::Camera, math::Aabb, math::Point, window::Window};

use super::{
config_ui::ConfigUi, draw_config::DrawConfig, drawables::Drawables,
Expand Down Expand Up @@ -114,8 +114,15 @@ impl Renderer {
label: None,
});

let geometries =
Geometries::new(&device, &Vertices::empty(), &Vertices::empty());
let geometries = Geometries::new(
&device,
&Vertices::empty(),
&Vertices::empty(),
Aabb {
min: Point::from([0.0, 0.0, 0.0]),
max: Point::from([0.0, 0.0, 0.0]),
},
);
let pipelines = Pipelines::new(&device, &bind_group_layout);

let config_ui = ConfigUi::new(&device)?;
Expand All @@ -138,8 +145,13 @@ impl Renderer {
})
}

pub fn update_geometry(&mut self, mesh: Vertices, lines: Vertices) {
self.geometries = Geometries::new(&self.device, &mesh, &lines);
pub fn update_geometry(
&mut self,
mesh: Vertices,
lines: Vertices,
aabb: Aabb<3>,
) {
self.geometries = Geometries::new(&self.device, &mesh, &lines, aabb);
}

pub fn handle_resize(&mut self, size: PhysicalSize<u32>) {
Expand Down Expand Up @@ -215,6 +227,7 @@ impl Renderer {
&mut encoder,
&color_view,
&self.surface_config,
&self.geometries.aabb,
config,
)
.map_err(DrawError::Text)?;
Expand Down
12 changes: 6 additions & 6 deletions src/kernel/approximation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ impl Approximation {
// As this is a cycle, neighboring edges are going to share vertices.
// Let's remove all those duplicates.
points.sort_by(|a, b| {
if a.x() < b.x() {
if a.x < b.x {
return Ordering::Less;
}
if a.x() > b.x() {
if a.x > b.x {
return Ordering::Greater;
}
if a.y() < b.y() {
if a.y < b.y {
return Ordering::Less;
}
if a.y() > b.y() {
if a.y > b.y {
return Ordering::Greater;
}
if a.z() < b.z() {
if a.z < b.z {
return Ordering::Less;
}
if a.z() > b.z() {
if a.z > b.z {
return Ordering::Greater;
}

Expand Down
8 changes: 4 additions & 4 deletions src/kernel/geometry/curves/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Circle {
/// error.
pub fn point_model_to_curve(&self, point: &Point<3>) -> Point<1> {
let v = point - self.center;
let atan = Scalar::atan2(v.y(), v.x());
let atan = Scalar::atan2(v.y, v.x);
let coord = if atan >= Scalar::ZERO {
atan
} else {
Expand All @@ -59,13 +59,13 @@ impl Circle {

/// Convert a point on the curve into model coordinates
pub fn point_curve_to_model(&self, point: &Point<1>) -> Point<3> {
self.center + self.vector_curve_to_model(&point.coords())
self.center + self.vector_curve_to_model(&point.coords)
}

/// Convert a vector on the curve into model coordinates
pub fn vector_curve_to_model(&self, point: &Vector<1>) -> Vector<3> {
pub fn vector_curve_to_model(&self, vector: &Vector<1>) -> Vector<3> {
let radius = self.radius.magnitude();
let angle = point.t();
let angle = vector.t;

let (sin, cos) = angle.sin_cos();

Expand Down
12 changes: 5 additions & 7 deletions src/kernel/geometry/curves/line.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use approx::AbsDiffEq;

use crate::math::{Point, Transform, Vector};

/// A line, defined by a point and a vector
Expand Down Expand Up @@ -50,17 +48,17 @@ impl Line {

/// Convert a point on the curve into model coordinates
pub fn point_curve_to_model(&self, point: &Point<1>) -> Point<3> {
self.origin + self.vector_curve_to_model(&point.coords())
self.origin + self.vector_curve_to_model(&point.coords)
}

/// Convert a vector on the curve into model coordinates
pub fn vector_curve_to_model(&self, point: &Vector<1>) -> Vector<3> {
self.direction * point.t()
pub fn vector_curve_to_model(&self, vector: &Vector<1>) -> Vector<3> {
self.direction * vector.t
}
}

impl AbsDiffEq for Line {
type Epsilon = <f64 as AbsDiffEq>::Epsilon;
impl approx::AbsDiffEq for Line {
type Epsilon = <f64 as approx::AbsDiffEq>::Epsilon;

fn default_epsilon() -> Self::Epsilon {
f64::default_epsilon()
Expand Down
7 changes: 3 additions & 4 deletions src/kernel/geometry/surfaces/swept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Swept {

/// Convert a point in model coordinates to surface coordinates
pub fn point_model_to_surface(&self, point: &Point<3>) -> Point<2> {
let u = self.curve.point_model_to_curve(point).t();
let u = self.curve.point_model_to_curve(point).t;
let v = (point - self.curve.origin()).dot(&self.path.normalize())
/ self.path.magnitude();

Expand All @@ -33,13 +33,12 @@ impl Swept {

/// Convert a point in surface coordinates to model coordinates
pub fn point_surface_to_model(&self, point: &Point<2>) -> Point<3> {
self.curve.point_curve_to_model(&point.to_t()) + self.path * point.v()
self.curve.point_curve_to_model(&point.to_t()) + self.path * point.v
}

/// Convert a vector in surface coordinates to model coordinates
pub fn vector_surface_to_model(&self, vector: &Vector<2>) -> Vector<3> {
self.curve.vector_curve_to_model(&vector.to_t())
+ self.path * vector.v()
self.curve.vector_curve_to_model(&vector.to_t()) + self.path * vector.v
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/kernel/shapes/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
impl Shape for fj::Sweep {
fn bounding_volume(&self) -> Aabb<3> {
let mut aabb = self.shape.bounding_volume();
*aabb.max.z_mut() = self.length.into();
aabb.max.z = self.length.into();
aabb
}

Expand Down
4 changes: 2 additions & 2 deletions src/kernel/triangulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl HasPosition for SurfacePoint {

fn position(&self) -> spade::Point2<Self::Scalar> {
spade::Point2 {
x: self.value.u(),
y: self.value.v(),
x: self.value.u,
y: self.value.v,
}
}
}
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ fn main() -> anyhow::Result<()> {
let mut input_handler = input::Handler::new(previous_time);
let mut renderer = block_on(Renderer::new(&window))?;

renderer.update_geometry((&triangles).into(), (&debug_info).into());
renderer.update_geometry((&triangles).into(), (&debug_info).into(), aabb);

let mut draw_config = DrawConfig::default();
let mut camera = Camera::new(&aabb);
Expand All @@ -218,8 +218,11 @@ fn main() -> anyhow::Result<()> {
aabb = shape.bounding_volume();
faces.triangles(tolerance, &mut triangles, &mut debug_info);

renderer
.update_geometry((&triangles).into(), (&debug_info).into());
renderer.update_geometry(
(&triangles).into(),
(&debug_info).into(),
aabb,
);
}
Err(mpsc::TryRecvError::Empty) => {
// Nothing to receive from the channel. We don't care.
Expand Down
22 changes: 22 additions & 0 deletions src/math/coordinates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use super::Scalar;

/// 1-dimensional curve coordinates
#[repr(C)]
pub struct T {
pub t: Scalar,
}

/// 2-dimensional surface coordinates
#[repr(C)]
pub struct Uv {
pub u: Scalar,
pub v: Scalar,
}

/// 3-dimensional model coordinates
#[repr(C)]
pub struct Xyz {
pub x: Scalar,
pub y: Scalar,
pub z: Scalar,
}
1 change: 1 addition & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod aabb;
pub mod coordinates;
pub mod point;
pub mod scalar;
pub mod segment;
Expand Down
Loading

0 comments on commit 9ab0d78

Please sign in to comment.