Move basic types to a module

This commit is contained in:
numzero 2024-06-25 13:11:27 +03:00
parent 13da06b294
commit 0cddb8798d
3 changed files with 57 additions and 54 deletions

View File

@ -7,12 +7,13 @@ mod riemann;
mod fns;
mod float_fun;
mod tube;
mod types;
use riemann::{Metric, trace_iter};
use tube::shape::Shape;
use tube::Subspace::{Boundary, Inner, Outer};
use tube::metric::Tube;
use tube::Space;
use types::{Location, Object, Ray};
const DT: f32 = 0.1;
@ -104,35 +105,6 @@ pub fn main() {
});
}
#[derive(Copy, Clone, Debug)]
pub struct Location {
/// Положение в основной СК
pub pos: Vec2,
/// Преобразование вектора из локальной ортонормированной в основную СК
pub rot: Mat2,
}
#[derive(Copy, Clone, Debug)]
pub struct Object {
pub id: i32,
pub loc: Location,
pub r: f32,
}
#[derive(Copy, Clone, Debug)]
pub struct Hit {
pub distance: f32,
pub id: i32,
pub pos: Vec2, // положение в основной СК
pub rel: Ray, // в локальной ортонормированной СК объекта
}
#[derive(Clone, Debug)]
pub struct FlatTraceResult {
pub end: Option<Ray>,
pub objects: Vec<Hit>,
}
fn draw_ray_2(gc: &mut Vec<Draw>, space: &Space, base: Vec2, dir: Vec2) {
let mut hits = Vec::<Draw>::new();
let dir = space.tube.globalize(base, dir);
@ -277,23 +249,3 @@ impl Renderable for Tube {
gc.fill();
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Ray {
pub pos: Vec2,
pub dir: Vec2,
}
impl Ray {
pub fn forward(&self, dist: f32) -> Ray {
Ray { pos: self.pos + self.dir * dist, dir: self.dir }
}
}
impl std::ops::Mul<Ray> for Mat2 {
type Output = Ray;
fn mul(self, rhs: Ray) -> Self::Output {
Ray { pos: self * rhs.pos, dir: self * rhs.dir }
}
}

View File

@ -1,9 +1,10 @@
use glam::{bool, f32, Mat2, Vec2, vec2};
use crate::{FlatTraceResult, Hit, Location, Object, Ray, riemann};
use crate::riemann;
use crate::riemann::Metric;
use Subspace::{Boundary, Inner, Outer};
use metric::Tube;
use shape::Shape;
use crate::types::{FlatTraceResult, Hit, Location, Object, Ray};
pub mod metric;
@ -164,7 +165,7 @@ impl Space {
mod basic_shapes {
use glam::{Vec2, vec2};
use crate::Ray;
use crate::types::Ray;
use super::shape::Shape;
pub struct Rect {
@ -235,9 +236,9 @@ mod basic_shapes {
}
}
pub mod shape {
mod shape {
use glam::Vec2;
use crate::Ray;
use crate::types::Ray;
pub trait Shape {
fn is_inside(&self, pt: Vec2) -> bool;

50
src/bin/flat/types.rs Normal file
View File

@ -0,0 +1,50 @@
use glam::{f32, i32, Mat2, Vec2};
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Ray {
pub pos: Vec2,
pub dir: Vec2,
}
impl Ray {
pub fn forward(&self, dist: f32) -> Ray {
Ray { pos: self.pos + self.dir * dist, dir: self.dir }
}
}
impl std::ops::Mul<Ray> for Mat2 {
type Output = Ray;
fn mul(self, rhs: Ray) -> Self::Output {
Ray { pos: self * rhs.pos, dir: self * rhs.dir }
}
}
#[derive(Copy, Clone, Debug)]
pub struct Location {
/// Положение в основной СК
pub pos: Vec2,
/// Преобразование вектора из локальной ортонормированной в основную СК
pub rot: Mat2,
}
#[derive(Copy, Clone, Debug)]
pub struct Object {
pub id: i32,
pub loc: Location,
pub r: f32,
}
#[derive(Copy, Clone, Debug)]
pub struct Hit {
pub distance: f32,
pub id: i32,
pub pos: Vec2, // положение в основной СК
pub rel: Ray, // в локальной ортонормированной СК объекта
}
#[derive(Clone, Debug)]
pub struct FlatTraceResult {
pub end: Option<Ray>,
pub objects: Vec<Hit>,
}