Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dobkeratops/e6f4c007399c2b12e05f0f7b56d181ef to your computer and use it in GitHub Desktop.
Save dobkeratops/e6f4c007399c2b12e05f0f7b56d181ef to your computer and use it in GitHub Desktop.
-- some operations should be supported by bigger 'objects' e.g.
-- vertices , valid to say 'whats the maximum of each component of 2 vertices', 'blend 2 vertices..'
-- but vertices are not necaserily vectors?.. the data within is in different spaces (position, color, normal..)
class VertexOps v where
vlerp::Num s=>v->s->v
vmin::v->v->v
vmax::v->v->v
-- TODO some of this might be monad malarky? (fmap? liftM2? )
class VectorOps x where
vbinop::(a->b->c) -> x a -> x b -> x c -- for binary operation between two vectors
vreduce::(a->a->a) -> x a -> a -- for componentwise 'reduction' e.g. for sum-elements
vscalarop::(a->b->c) -> x a -> b -> x c -- for binary operations between a vector and a scalar
????
// Rust..
// trait VectorOps e.g. VecXYZ
// trait AllOps : VectorOps+VertexOps
// trait VertexOps e.g. PointColorNormal
// 'T' = the scalar type inside..
trait VectorOps<T> {
fn vbinop<F>(&self,b:&Self, f:F)->Self // use to roll add,sub,mul, ...
where F:Fn(T,T)->T;
..
}
trait VertexOps<T> {
fn vlerp(&self,b:&Self,f:T)->Self
fn vmin(&self,:b:&Self)->Self
fn vmax(&self,:b:&Self)->Self
}
// 'for any 'V' that supports Vector Ops, implement VertexOps trivially.
impl<T:Num,V:VectorOps<T>> VertexOps<T> for V {
fn vlerp(&self,b:&V,f:T)->V { a + (b-a)*f }
}
trait AllOps<T> : VectorOps<T>,VertexOps<T>{} // just combines them
impl<V:VectorOps<T>+VertexOps> AllOps for V{} // required to actually instantiate
// now we can bound types with 'AllOps' for everthing, or just 'VertexOps' for 'vlerp,vmin,vmax'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment