a little wild with the trait bounds

This commit is contained in:
Quaternions 2025-02-06 13:34:02 -08:00
parent 9b7101cbea
commit 915d6c67cd

@ -1,7 +1,7 @@
use std::collections::BTreeMap;
use crate::aabb::Aabb;
use crate::integer::{Planar64, Planar64Vec3};
use crate::integer::{Ratio,Planar64,Planar64Vec3};
use crate::instruction::{InstructionCollector,TimedInstruction};
//da algaritum
@ -19,8 +19,20 @@ pub struct Ray{
pub direction:Planar64Vec3,
}
impl Ray{
pub fn intersect_aabb(&self,aabb:&Aabb)->Ratio<Planar64,Planar64>{
pub fn intersect_aabb(&self,aabb:&Aabb)->Option<Ratio<Planar64,Planar64>>{
// n.(o+d*t)==n.p
// n.o + n.d * t == n.p
// t == (n.p - n.o)/n.d
// divide by 0 might be bad
// this is all wrong because it just does the planes
[
(aabb.min().x-self.origin.x)/self.direction.x,
(aabb.min().y-self.origin.y)/self.direction.y,
(aabb.min().z-self.origin.z)/self.direction.z,
(aabb.max().x-self.origin.x)/self.direction.x,
(aabb.max().y-self.origin.y)/self.direction.y,
(aabb.max().z-self.origin.z)/self.direction.z,
].into_iter().min()
}
}
@ -58,23 +70,28 @@ impl<L> BvhNode<L>{
},
}
}
fn populate_nodes<T:Ord+Copy,F:Fn(&L,&Ray)->Option<T>>(
&self,
collector:&mut InstructionCollector<&L,T>,
nodes:&mut BTreeMap<T,&BvhNode<L>>,
fn populate_nodes<'a,T,F>(
&'a self,
collector:&mut InstructionCollector<&'a L,Ratio<Planar64,Planar64>>,
nodes:&mut BTreeMap<Ratio<Planar64,Planar64>,&'a BvhNode<L>>,
ray:&Ray,
start_time:T,
start_time:Ratio<Planar64,Planar64>,
f:&F,
){
)
where
T:Ord+Copy,
Ratio<Planar64,Planar64>:From<T>,
F:Fn(&L,&Ray)->Option<T>,
{
match &self.content{
RecursiveContent::Leaf(leaf)=>collector.collect(f(leaf,ray).map(|time|TimedInstruction{time,instruction:leaf})),
RecursiveContent::Leaf(leaf)=>collector.collect(f(leaf,ray).map(|time|TimedInstruction{time:time.into(),instruction:leaf})),
RecursiveContent::Branch(children)=>for child in children{
if child.aabb.contains(ray.origin){
child.populate_nodes(collector,nodes,ray,start_time,f);
}else{
// Am I an upcoming superstar?
if let Some(t)=ray.intersect_aabb(&child.aabb){
if start_time<t&&t<collector.time(){
if start_time.lt_ratio(t)&&t.lt_ratio(collector.time()){
nodes.insert(t,child);
}
}
@ -82,8 +99,23 @@ impl<L> BvhNode<L>{
},
}
}
pub fn sample_ray<T:Ord+Copy,F:Fn(&L,&Ray)->Option<T>>(&self,ray:&Ray,start_time:T,time_limit:T,f:&F)->Option<(T,&L)>{
pub fn sample_ray<T,F>(
&self,
ray:&Ray,
start_time:T,
time_limit:T,
f:&F,
)->Option<(T,&L)>
where
T:Ord+Copy,
T:From<Ratio<Planar64,Planar64>>,
Ratio<Planar64,Planar64>:From<T>,
F:Fn(&L,&Ray)->Option<T>,
{
let mut nodes=BTreeMap::new();
let start_time=start_time.into();
let time_limit=time_limit.into();
let mut collector=InstructionCollector::new(time_limit);
// break open all nodes that contain ray.origin and populate nodes with future intersection times
self.populate_nodes(&mut collector,&mut nodes,ray,start_time,f);
@ -94,7 +126,7 @@ impl<L> BvhNode<L>{
break;
}
match &node.content{
RecursiveContent::Leaf(leaf)=>collector.collect(f(leaf,ray).map(|time|TimedInstruction{time,instruction:leaf})),
RecursiveContent::Leaf(leaf)=>collector.collect(f(leaf,ray).map(|time|TimedInstruction{time:time.into(),instruction:leaf})),
// break open the node and predict collisions with the child nodes
RecursiveContent::Branch(children)=>for child in children{
// Am I an upcoming superstar?
@ -107,7 +139,7 @@ impl<L> BvhNode<L>{
}
}
collector.take().map(|TimedInstruction{time,instruction:leaf}|(time,leaf))
collector.take().map(|TimedInstruction{time,instruction:leaf}|(time.into(),leaf))
}
pub fn into_inner(self)->(RecursiveContent<BvhNode<L>,L>,Aabb){
(self.content,self.aabb)