Skip to content

Proposal: Add Vector types and remove Point4D #151

@nical

Description

@nical

Currently, euclid and servo use points to describe vectors. It works out but it is a tad dangerous in some cases because we use the same type to express different things and also because transforming a point and transforming a vector does not involve the same computation (translations apply to points but don't apply to vectors, for instance). Euclid currently can't express transforming a vector, you have to transform two points and compute the difference, which is easy to forget when you also use the point types to store vectors).

I Propose adding TypedVector2D, TypedVector3D, types which would implement the usual arithmetic operations. Points would be changed so that:

  • Points + Point is not implemented
  • Point + Vector = Point
  • Point - Vector = Point
  • Point - Point = Vector

Places where we currently use points to express vectors (like Rect::translate) would take vectors instead. points and vectors would implement conversion helpers to_vector/to_point to avoid the burden of writing my_point - Point2D::origin() and Point2D::origin() + my_vector

Matrix transformation would be implemented separately for points (homogeneous coordinate w = 1) and vectors (homogeneous coordinate w = 0).

This would let us remove Point4D which is very awkward because it exposes the homogeneous coordinate, but we always expect it to be 1.
At a glance, Servo only uses Point4D in one place (rust-layers) where Point3D could (should) be used instead, and gecko does not use Point4D at all, so the 4d type is mostly dead weight and a source of confusion since its member w is mostly ignored.

As opposed to my other proposals to euclid, this is actually not implemented in gecko. However it has been a source of confusion for me and having distinct types for points and vectors would have avoided a few mistakes and helped make better self-documented APIs.

I have a prototype of this in my vectors branch if you would like to have a look at what the API would be like.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions