cgmath icon indicating copy to clipboard operation
cgmath copied to clipboard

Improve naming of Point::{from_vec, to_vec} to reflect their semantic meaning

Open brendanzab opened this issue 9 years ago • 5 comments

The following properties are true for Point::{from_vec, to_vec}:

  • Point::from_vec(v) - Point::origin() == v
  • Point::origin() + Point::to_vec(p) == p

This suggests that semantically, these are actually all about displacements from the origin. I'm proposing to do the following renames:

  • Point::from_vec to Point::from_displacement
  • Point::to_vec to Point::displacement

Another possibility would be:

  • Point::from_vec to Point::from_translation
  • Point::to_vec to Point::translation

brendanzab avatar Mar 21 '16 07:03 brendanzab

Another option might be to have an Origin marker struct like in CGAL:

struct Origin;

impl<S: BaseNum> Sub<Origin> for Point3<S> {
    type Output = Vector3<S>;

    #[inline]
    fn sub(self, Origin: Origin) -> Vector3<S> {
        unsafe { mem::transmute(self) }
    }
}

impl<S: BaseNum> Add<Vector3<S>> for Origin {
    type Output = Point3<S>;

    #[inline]
    fn sub(self, displacement: Vector3<S>) -> Point3<S> {
        unsafe { mem::transmute(displacement) }
    }
}

let displacement = point - Origin;
let point = Origin + displacement;

brendanzab avatar Mar 25 '16 04:03 brendanzab

Am I confused, or does this have to do with #320?

mhintz avatar Apr 09 '16 16:04 mhintz

This is to do with the naming of the methods, which #320 doesn't touch yet.

brendanzab avatar Apr 10 '16 01:04 brendanzab

Also notice that rotation/transform API uses the full vector suffix, unlike the subject methods:

  • rotate_vector
  • transform_vector
  • between_vectors

So leaving to_vec and from_vec means inconsistency in the API.

kvark avatar Jul 13 '16 02:07 kvark

I'm in favor of the names from_vec and to_vec, because they're quicker to type, they imply that we're talking about a type conversion (whereas it might be uncertain what a "displacement" is, unless one has read the docs), and they seem like easier names for new users of the library to remember. Lastly, because it would be a breaking change to alter them.

mhintz avatar Jul 13 '16 13:07 mhintz