Remove single-implementation traits

This commit is contained in:
numzero 2024-06-25 13:20:52 +03:00
parent 0cddb8798d
commit 4caa260a34

View File

@ -3,7 +3,6 @@ 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;
@ -76,7 +75,7 @@ impl Space {
pub fn trace_outer(&self, ray: Ray) -> FlatTraceResult {
assert_eq!(self.which_subspace(ray.pos), Outer);
let cell = basic_shapes::Rect { size: vec2(self.tube.outer_radius, self.tube.external_halflength) };
let cell = Rect { size: vec2(self.tube.outer_radius, self.tube.external_halflength) };
let objs = self.list_objects_outer();
let lim = cell.trace_into(ray);
let dist = lim.unwrap_or(f32::INFINITY);
@ -163,12 +162,7 @@ impl Space {
}
}
mod basic_shapes {
use glam::{Vec2, vec2};
use crate::types::Ray;
use super::shape::Shape;
pub struct Rect {
struct Rect {
pub size: Vec2,
}
@ -177,9 +171,7 @@ mod basic_shapes {
fn flip_ray(ray: Ray) -> Ray {
Ray { pos: ray.pos * ray.dir.signum(), dir: ray.dir.abs() }
}
}
impl Shape for Rect {
fn is_inside(&self, pt: Vec2) -> bool {
pt.abs().cmplt(self.size).all()
}
@ -234,36 +226,17 @@ mod basic_shapes {
assert_eq!(r.trace_out_of(Ray { pos: vec2(1.0, 1.0), dir: vec2(0.0, -1.0) }), Some(4.0));
assert_eq!(r.trace_out_of(Ray { pos: vec2(2.0, 3.0), dir: vec2(1.0, 1.0) }), Some(0.0));
}
#[derive(Debug)]
struct TubeInside {
tube: Tube,
}
mod shape {
use glam::Vec2;
use crate::types::Ray;
pub trait Shape {
fn is_inside(&self, pt: Vec2) -> bool;
/// Ищет ближайшее пересечение луча с границей в направлении внутрь контура. Возвращает расстояние (в ray.dir).
fn trace_into(&self, ray: Ray) -> Option<f32>;
/// Ищет ближайшее пересечение луча с границей в направлении вовне контура. Возвращает расстояние (в ray.dir).
fn trace_out_of(&self, ray: Ray) -> Option<f32>;
/// Возвращает визуальное представление контура, для отладки.
fn visualise(&self) -> Vec<Vec2>;
}
}
trait FlatCell: std::fmt::Debug {
fn pos_to_global(&self, pos: Vec2) -> Vec2;
fn pos_to_local(&self, pos: Vec2) -> Vec2;
fn ray_to_global(&self, ray: Ray) -> Ray;
fn ray_to_local(&self, ray: Ray) -> Ray;
impl TubeInside {
fn is_inside(&self, pos: Vec2) -> bool {
let bnd = self.local_bounds();
pos.cmpge(bnd.0).all() && pos.cmple(bnd.1).all()
}
fn local_bounds(&self) -> (Vec2, Vec2);
fn to_boundary(&self, ray: Ray) -> Option<f32> {
assert!(self.is_inside(ray.pos));
@ -288,14 +261,7 @@ trait FlatCell: std::fmt::Debug {
None
}
}
}
#[derive(Debug)]
struct TubeInside {
tube: Tube,
}
impl FlatCell for TubeInside {
fn pos_to_global(&self, pos: Vec2) -> Vec2 {
vec2(pos.x, self.tube.y(pos.y))
}