0
pub type ESign = i8;
const CONCAVE: i8 = -1;
const TANGENTIAL: i8 = 0;
const CONVEX: i8 = 1;

fn signum_enum(a: f64) -> ESign {
    if a > 0.0 { CONVEX } else if a < 0.0 { CONCAVE } else { TANGENTIAL }
}

pub fn area_tri_signed_v2_alt_2x(v1: &[f64; 2], v2: &[f64; 2], v3: &[f64; 2]) -> f64 {
    ((v1[0] * (v2[1] - v3[1])) + (v2[0] * (v3[1] - v1[1])) + (v3[0] * (v1[1] - v2[1])))
}

pub fn span_tri_v2_sign(v1: &[f64; 2], v2: &[f64; 2], v3: &[f64; 2]) -> ESign {
    return signum_enum(area_tri_signed_v2_alt_2x(v3, v2, v1));
}

I managed to make these into generic functions, however I ended up having to duplicate type boundaries.

use std::ops::{Mul, Sub, Add};

fn signum_enum<T: Default + PartialOrd>(a: T) -> ESign {
    let zero = T::default();
    if a > zero { CONVEX } else if a < zero { CONCAVE } else { TANGENTIAL }
}

pub fn area_tri_signed_v2_alt_2x<T: Default + PartialOrd + Copy + Mul<Output=T> + Sub<Output=T> + Add<Output=T>>(
    v1: &[T; 2],
    v2: &[T; 2],
    v3: &[T; 2]) -> T
{
    ((v1[0] * (v2[1] - v3[1])) + (v2[0] * (v3[1] - v1[1])) + (v3[0] * (v1[1] - v2[1])))
}

pub fn span_tri_v2_sign<T: Default + PartialOrd + Copy + Mul<Output=T> + Sub<Output=T> + Add<Output=T>>(
    v1: &[T; 2],
    v2: &[T; 2],
    v3: &[T; 2]) -> ESign
{
    return signum_enum(area_tri_signed_v2_alt_2x(v3, v2, v1));
}

Is there a way to define these capabilities in one place and reuse them in multiple generic functions?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ideasman42
  • 42,413
  • 44
  • 197
  • 320

1 Answers1

2

The easiest way is to define a new trait which both requires all the bounds you are dealing with:

pub trait Floatlike: Default + PartialOrd + Copy +
    Mul<Output=Self> + Sub<Output=Self> + Add<Output=Self> {}

and is implemented by all types that satisfy these bounds:

impl<T> Floatlike for T where T: Default + PartialOrd + Copy + 
    Mul<Output=T> + Sub<Output=T> + Add<Output=T> {}

and then bound on that trait:

pub fn area_tri_signed_v2_alt_2x<T: Floatlike>(...)
pub fn span_tri_v2_sign<T: Floatlike>(...)
SpaceManiac
  • 525
  • 4
  • 7