Skip to content

Commit

Permalink
l: add facet object so that we can reder triangles, thus meshes!
Browse files Browse the repository at this point in the history
  • Loading branch information
danieledapo committed Feb 1, 2023
1 parent 018d301 commit 3f498eb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
Binary file added data/t-rex-skull.stl
Binary file not shown.
33 changes: 33 additions & 0 deletions l/examples/trex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::{path::Path, sync::Arc};

use geo::{mesh::load_mesh, util::opener, Vec3};

use l::*;

pub fn main() -> opener::Result<()> {
let mesh = load_mesh(
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("data")
.join("t-rex-skull.stl"),
)
.expect("cannot load t-rex-skull.stl");

let scene = Scene::new(
mesh.triangles()
.map(|f| Arc::new(Facet::new(f)) as Arc<dyn Object>)
.collect::<Vec<_>>(),
);

let camera = Camera::look_at(
Vec3::new(-30.0, -10.0, 10.0),
Vec3::zero(),
Vec3::new(0.0, 0.0, -1.0),
)
.with_perspective_projection(60.0, 1.0, 0.01, 10000.0);

let paths = render(camera, &scene, &Settings { eps: 0.001 });
dump_svg("trex.svg", &paths, (1024.0, 1024.0)).expect("cannot save trex.svg");

opener::open("trex.svg")
}
38 changes: 38 additions & 0 deletions l/src/object/facet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use geo::{spatial_index::Shape, triangle, Vec3};

use crate::Object;

#[derive(Debug)]
pub struct Facet {
a: Vec3,
b: Vec3,
c: Vec3,
}

impl Facet {
pub fn new(triangle: [Vec3; 3]) -> Self {
Self {
a: triangle[0],
b: triangle[1],
c: triangle[2],
}
}
}

impl Shape for Facet {
type Intersection = f64;

fn intersection(&self, ray: &geo::ray::Ray) -> Option<Self::Intersection> {
triangle::ray_intersection((self.a, self.b, self.c), ray)
}

fn bbox(&self) -> geo::Aabb {
triangle::bbox(self.a, self.b, self.c)
}
}

impl Object for Facet {
fn paths(&self) -> Vec<crate::Polyline> {
vec![vec![self.a, self.b, self.c]]
}
}
2 changes: 2 additions & 0 deletions l/src/object/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod cube;
mod facet;

pub use cube::Cube;
pub use facet::Facet;

0 comments on commit 3f498eb

Please sign in to comment.