Imath
<navlinks>
<page>

# Line3

    #include <Imath/ImathLine.h>

The `Line3` class template represents a line in 3D space, with predefined typedefs for lines of type `float` and `double`.

There are also various utility functions that operate on `Line3` objects defined in `ImathLineAlgo.h` and described in Line Functions.

Example:

    #include <Imath/ImathLine.h>
    #include <cassert>

    void
    line3_example()
    {
        Imath::V3f   a (0.0f, 0.0f, 0.0f);
        Imath::V3f   b (1.0f, 1.0f, 1.0f);

        Imath::Line3f line (a, b);

        assert (line.pos == a);
        assert (line.dir == (b-a).normalized());

        Imath::V3f   c (0.5f, 0.5f, 0.5f);

        float f = line.distanceTo (c);
        assert (Imath::equalWithAbsError (f, 0.0f, 0.0001f));

        Imath::V3f p = line (0.5f); // midpoint, i.e. 0.5 units from a along (b-a)

        assert (p.equalWithAbsError (Imath::V3f (0.288675f, 0.288675f, 0.288675f), 0.0001f));
    }

typedef Line3<float> Imath::Line3f

  Line of type float.

typedef Line3<double> Imath::Line3d

  Line of type double.

template<class T>
class Line3

  The `Line3` class represents a 3D line, defined by a point and a direction vector.

  Direct access to member fields

  Vec3<T> pos

    A point on the line.

  Vec3<T> dir

    The direction of the line.

  Constructors

  inline constexpr Line3() noexcept

    Uninitialized by default.

  inline constexpr Line3(const Vec3<T> &point1, const Vec3<T> &point2) noexcept

    Initialize with two points.

    The direction is the difference between the points.

  Manipulation

  inline void set(const Vec3<T> &point1, const Vec3<T> &point2) noexcept

    Set the line defined by two points.

    The direction is the difference between the points.

  Utility Methods

  inline Vec3<T> operator()(T parameter) const noexcept

    Return the point on the line at the given parameter value, e.g.

    L(t)

  inline T distanceTo(const Vec3<T> &point) const noexcept

    Return the distance to the given point.

  inline T distanceTo(const Line3<T> &line) const noexcept

    Return the distance to the given line.

  inline Vec3<T> closestPointTo(const Vec3<T> &point) const noexcept

    Return the point on the line closest to the given point.

  inline Vec3<T> closestPointTo(const Line3<T> &line) const noexcept

    Return the point on the line closest to the given line.

template<class T>
std::ostream &Imath::operator<<(std::ostream &o, const Line3<T> &line)

  Stream output, as “(pos dir)”.

</page>