mod axes;
pub use axes::*;
use sophus_autodiff::linalg::SVec;
use sophus_lie::{
Isometry3,
Isometry3F64,
};
use crate::{
prelude::*,
renderables::{
color::Color,
pixel_renderable::HasToVec2F32,
},
};
#[derive(Clone, Debug)]
pub enum SceneRenderable {
Line(LineSegments3),
Point(PointCloud3),
Mesh3(TriangleMesh3),
}
impl SceneRenderable {
pub fn world_from_entity(&self) -> Isometry3F64 {
match self {
SceneRenderable::Line(lines) => lines.world_from_entity,
SceneRenderable::Point(points) => points.world_from_entity,
SceneRenderable::Mesh3(mesh) => mesh.world_from_entity,
}
}
}
pub fn named_line3_at(
name: impl ToString,
line_segments: Vec<LineSegment3>,
world_from_entity: Isometry3F64,
) -> SceneRenderable {
let lines = LineSegments3 {
name: name.to_string(),
segments: line_segments,
world_from_entity,
};
SceneRenderable::Line(lines)
}
pub fn named_line3(name: impl ToString, line_segments: Vec<LineSegment3>) -> SceneRenderable {
named_line3_at(name, line_segments, Isometry3::identity())
}
pub fn named_point3_at(
name: impl ToString,
points: Vec<Point3>,
world_from_entity: Isometry3F64,
) -> SceneRenderable {
let points = PointCloud3 {
name: name.to_string(),
points,
world_from_entity,
};
SceneRenderable::Point(points)
}
pub fn named_point3(name: impl ToString, points: Vec<Point3>) -> SceneRenderable {
named_point3_at(name, points, Isometry3::identity())
}
pub fn named_mesh3_at(
name: impl ToString,
mesh: TriangleMesh3,
world_from_entity: Isometry3F64,
) -> SceneRenderable {
let mesh = TriangleMesh3 {
name: name.to_string(),
triangles: mesh.triangles,
world_from_entity,
};
SceneRenderable::Mesh3(mesh)
}
pub fn named_mesh3(name: impl ToString, mesh: TriangleMesh3) -> SceneRenderable {
named_mesh3_at(name, mesh, Isometry3::identity())
}
pub fn make_point3_at(
name: impl ToString,
arr: &[impl HasToVec3F32],
color: &Color,
point_size: f32,
world_from_entity: Isometry3F64,
) -> SceneRenderable {
let mut points = PointCloud3 {
name: name.to_string(),
points: vec![],
world_from_entity,
};
for p in arr {
points.points.push(Point3 {
p: p.to_vec3(),
color: *color,
point_size,
});
}
SceneRenderable::Point(points)
}
pub fn make_point3(
name: impl ToString,
arr: &[impl HasToVec3F32],
color: &Color,
point_size: f32,
) -> SceneRenderable {
make_point3_at(name, arr, color, point_size, Isometry3::identity())
}
pub fn make_line3_at(
name: impl ToString,
arr: &[[impl HasToVec3F32; 2]],
color: &Color,
line_width: f32,
world_from_entity: Isometry3F64,
) -> SceneRenderable {
let mut lines = LineSegments3 {
name: name.to_string(),
segments: vec![],
world_from_entity,
};
for tuple in arr {
lines.segments.push(LineSegment3 {
p0: tuple[0].to_vec3(),
p1: tuple[1].to_vec3(),
color: *color,
line_width,
});
}
SceneRenderable::Line(lines)
}
pub fn make_line3(
name: impl ToString,
arr: &[[impl HasToVec3F32; 2]],
color: &Color,
line_width: f32,
) -> SceneRenderable {
make_line3_at(name, arr, color, line_width, Isometry3::identity())
}
pub fn make_mesh3_at(
name: impl ToString,
arr: &[([impl HasToVec3F32; 3], Color)],
world_from_entity: Isometry3F64,
) -> SceneRenderable {
let mut mesh = TriangleMesh3 {
name: name.to_string(),
triangles: vec![],
world_from_entity,
};
for (trig, color) in arr {
mesh.triangles.push(Triangle3 {
p0: trig[0].to_vec3(),
p1: trig[1].to_vec3(),
p2: trig[2].to_vec3(),
color0: *color,
color1: *color,
color2: *color,
});
}
SceneRenderable::Mesh3(mesh)
}
pub fn make_mesh3(name: impl ToString, arr: &[([impl HasToVec3F32; 3], Color)]) -> SceneRenderable {
make_mesh3_at(name, arr, Isometry3::identity())
}
pub fn make_textured_mesh3_at(
name: impl ToString,
arr: &[[(impl HasToVec3F32, impl HasToVec2F32); 3]],
world_from_entity: Isometry3F64,
) -> TexturedTriangleMesh3 {
let mut mesh = TexturedTriangleMesh3 {
name: name.to_string(),
triangles: vec![],
world_from_entity,
};
for trig in arr {
mesh.triangles.push(TexturedTriangle3 {
p0: trig[0].0.to_vec3(),
p1: trig[1].0.to_vec3(),
p2: trig[2].0.to_vec3(),
tex0: trig[0].1.to_vec2(),
tex1: trig[1].1.to_vec2(),
tex2: trig[2].1.to_vec2(),
});
}
mesh
}
pub fn make_textured_mesh3(
name: impl ToString,
arr: &[[(impl HasToVec3F32, impl HasToVec2F32); 3]],
) -> TexturedTriangleMesh3 {
make_textured_mesh3_at(name, arr, Isometry3::identity())
}
#[derive(Clone, Debug)]
pub struct LineSegment3 {
pub p0: SVec<f32, 3>,
pub p1: SVec<f32, 3>,
pub color: Color,
pub line_width: f32,
}
#[derive(Clone, Debug)]
pub struct Point3 {
pub p: SVec<f32, 3>,
pub color: Color,
pub point_size: f32,
}
#[derive(Clone, Debug)]
pub struct Triangle3 {
pub p0: SVec<f32, 3>,
pub p1: SVec<f32, 3>,
pub p2: SVec<f32, 3>,
pub color0: Color,
pub color1: Color,
pub color2: Color,
}
impl Triangle3 {
pub fn new(p0: SVec<f32, 3>, p1: SVec<f32, 3>, p2: SVec<f32, 3>, color: Color) -> Self {
Triangle3 {
p0,
p1,
p2,
color0: color,
color1: color,
color2: color,
}
}
}
#[derive(Clone, Debug)]
pub struct TexturedTriangle3 {
pub p0: SVec<f32, 3>,
pub p1: SVec<f32, 3>,
pub p2: SVec<f32, 3>,
pub tex0: SVec<f32, 2>,
pub tex1: SVec<f32, 2>,
pub tex2: SVec<f32, 2>,
}
pub trait HasToVec3F32 {
fn to_vec3(&self) -> SVec<f32, 3>;
}
impl HasToVec3F32 for [f32; 3] {
fn to_vec3(&self) -> SVec<f32, 3> {
SVec::<f32, 3>::new(self[0], self[1], self[2])
}
}
impl HasToVec3F32 for &[f32; 3] {
fn to_vec3(&self) -> SVec<f32, 3> {
SVec::<f32, 3>::new(self[0], self[1], self[2])
}
}
impl HasToVec3F32 for SVec<f32, 3> {
fn to_vec3(&self) -> SVec<f32, 3> {
*self
}
}
#[derive(Clone, Debug)]
pub struct PointCloud3 {
pub name: String,
pub points: Vec<Point3>,
pub world_from_entity: Isometry3F64,
}
#[derive(Clone, Debug)]
pub struct LineSegments3 {
pub name: String,
pub segments: Vec<LineSegment3>,
pub world_from_entity: Isometry3F64,
}
#[derive(Clone, Debug)]
pub struct TriangleMesh3 {
pub name: String,
pub triangles: Vec<Triangle3>,
pub world_from_entity: Isometry3F64,
}
#[derive(Clone, Debug)]
pub struct TexturedTriangleMesh3 {
pub name: String,
pub triangles: Vec<TexturedTriangle3>,
pub world_from_entity: Isometry3F64,
}