Remove single-implementation traits
This commit is contained in:
parent
0cddb8798d
commit
4caa260a34
|
|
@ -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,23 +162,16 @@ 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,
|
||||
}
|
||||
}
|
||||
|
||||
impl Rect {
|
||||
impl Rect {
|
||||
/// Отражает луч, чтобы все координаты направления были положительны (допустимо благодаря симметрии Rect).
|
||||
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()
|
||||
}
|
||||
|
|
@ -206,10 +198,10 @@ mod basic_shapes {
|
|||
fn visualise(&self) -> Vec<Vec2> {
|
||||
vec![vec2(-self.size.x, -self.size.y), vec2(self.size.x, -self.size.y), vec2(self.size.x, self.size.y), vec2(-self.size.x, self.size.y)]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rect() {
|
||||
#[test]
|
||||
fn test_rect() {
|
||||
assert_eq!(Rect::flip_ray(Ray { pos: vec2(2.0, 3.0), dir: vec2(4.0, 5.0) }), Ray { pos: vec2(2.0, 3.0), dir: vec2(4.0, 5.0) });
|
||||
assert_eq!(Rect::flip_ray(Ray { pos: vec2(2.0, 3.0), dir: vec2(-4.0, 5.0) }), Ray { pos: vec2(-2.0, 3.0), dir: vec2(4.0, 5.0) });
|
||||
assert_eq!(Rect::flip_ray(Ray { pos: vec2(2.0, 3.0), dir: vec2(4.0, -5.0) }), Ray { pos: vec2(2.0, -3.0), dir: vec2(4.0, 5.0) });
|
||||
|
|
@ -233,37 +225,18 @@ mod basic_shapes {
|
|||
assert_eq!(r.trace_out_of(Ray { pos: vec2(0.0, 1.0), dir: vec2(0.0, -1.0) }), Some(4.0));
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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>;
|
||||
}
|
||||
#[derive(Debug)]
|
||||
struct TubeInside {
|
||||
tube: Tube,
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user