template struct mrpt::math::TPoint3D_

Overview

Base template for TPoint3D (double) and TPoint3Df (float).

Represents a point (or free vector) in the 3D Euclidean space R^3. Coordinates are stored as (x, y, z) in a 1-byte-packed struct (no padding).

This is a lightweight POD-like type intended for storage, arithmetic, and interoperability with NumPy / Eigen arrays. It supports:

  • Standard vector arithmetic (+, -, scalar *, /).

  • Dot product (dot()) and cross product (cross()).

  • Euclidean norm and distance queries.

  • std::tuple structured-binding (auto [x,y,z] = pt;).

When applying SE(3) rigid-body transformations, use mrpt::poses::CPose3D::composePoint() or mrpt::math::TPose3D::composePoint() rather than adding translation manually.

TVector3D is a type alias for TPoint3D (same storage; use that name when the object is a free vector, e.g. a linear velocity or surface normal).

See also:

mrpt::poses::CPoint3D, TPose3D, TPoint2D

#include <mrpt/math/TPoint3D.h>

template <typename T>
struct TPoint3D_:
    public mrpt::math::TPoint3D_data,
    public mrpt::math::TPoseOrPoint
{
    // fields

    static constexpr std::size_t static_size = 3;
    T x;

    // construction

    TPoint3D_();
    TPoint3D_(T xx, T yy, T zz);

    template <typename U>
    TPoint3D_(const TPoint3D_data<U>& p);

    template <typename U>
    TPoint3D_(const mrpt::math::CMatrixFixed<U, 3, 1>& m);

    TPoint3D_(const TPoint2D_<T>& p);
    TPoint3D_(const TPose2D& p);
    TPoint3D_(const TPose3D& p);

    // methods

    constexpr std::size_t rows() const;
    constexpr std::size_t cols() const;
    constexpr std::size_t size() const;
    void resize(std::size_t n);

    template <typename U>
    TPoint3D_<U> cast() const;

    T* data();
    const T* data() const;
    T& operator [] (size_t i);
    constexpr T operator [] (size_t i) const;

    template <size_t I>
    const T& get() const;

    constexpr auto as_tuple() const;
    T distanceTo(const TPoint3D_<T>& p) const;
    T sqrDistanceTo(const TPoint3D_<T>& p) const;
    T sqrNorm() const;
    T norm() const;
    TPoint3D_<T> unitarize() const;
    TPoint3D_<T>& operator *= (const T f);

    template <typename Vector>
    void asVector(Vector& v) const;

    template <typename Vector>
    Vector asVector() const;

    TPoint3D_<T>& operator += (const TPoint3D_<T>& p);
    TPoint3D_<T>& operator -= (const TPoint3D_<T>& p);
    constexpr TPoint3D_<T> operator + (const TPoint3D_<T>& p) const;
    constexpr TPoint3D_<T> operator - (const TPoint3D_<T>& p) const;
    constexpr T dot(const TPoint3D_<T>& p) const;
    constexpr TPoint3D_<T> cross(const TPoint3D_<T>& p) const;
    constexpr TPoint3D_<T> operator * (T d) const;
    constexpr TPoint3D_<T> operator/ (T d) const;
    bool operator < (const TPoint3D_<T>& p) const;
    void asString(std::string& s) const;
    std::string asString() const;
    void fromString(const std::string& s);

    template <typename Vector>
    static TPoint3D_<T> FromVector(const Vector& v);

    static TPoint3D_<T> FromString(const std::string& s);
};

Inherited Members

public:
    // fields

    T y;
    T z;

Fields

T x

X,Y,Z coordinates.

Construction

TPoint3D_()

Default constructor.

Initializes to zeros.

TPoint3D_(T xx, T yy, T zz)

Constructor from coordinates.

template <typename U>
TPoint3D_(const TPoint3D_data<U>& p)

Constructor from coordinates.

template <typename U>
TPoint3D_(const mrpt::math::CMatrixFixed<U, 3, 1>& m)

Constructor from column vector.

TPoint3D_(const TPoint2D_<T>& p)

Constructor from TPoint2D; the z coordinate is set to zero.

See also:

TPoint2D

TPoint3D_(const TPose2D& p)

Constructor from TPose2D; retains (x,y) from the translational part and sets z=0; the heading angle phi is discarded.

See also:

TPose2D

TPoint3D_(const TPose3D& p)

Constructor from TPose3D; retains only the translational part (x,y,z); all angular coordinates (yaw, pitch, roll) are discarded.

See also:

TPose3D

Methods

template <typename U>
TPoint3D_<U> cast() const

Return a copy of this object using type U for coordinates.

T* data()

Pointer to the first coordinate (x).

Enables branch-free iteration and SIMD-friendly access to (x, y, z). Valid because TPoint3D_data holds only contiguous same-type POD members.

T& operator [] (size_t i)

Coordinate access using operator[].

Order: x,y,z

constexpr T operator [] (size_t i) const

Coordinate access using operator[].

Order: x,y,z

template <size_t I>
const T& get() const

Method so std::tuple, std::tie() works with TPoint3D.

T distanceTo(const TPoint3D_<T>& p) const

Point-to-point distance.

T sqrDistanceTo(const TPoint3D_<T>& p) const

Point-to-point distance, squared.

T sqrNorm() const

Squared norm: |v|^2 = x^2+y^2+z^2

T norm() const

Point norm: |v| = sqrt(x^2+y^2+z^2)

TPoint3D_<T> unitarize() const

Returns this vector with unit length: v/norm(v)

TPoint3D_<T>& operator *= (const T f)

Scale point/vector.

template <typename Vector>
void asVector(Vector& v) const

Gets the pose as a vector of doubles.

Parameters:

Vector

It can be std::vector<double>, Eigen::VectorXd, etc.

template <typename Vector>
Vector asVector() const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

TPoint3D_<T>& operator += (const TPoint3D_<T>& p)

Translation.

TPoint3D_<T>& operator -= (const TPoint3D_<T>& p)

Difference between points.

constexpr TPoint3D_<T> operator + (const TPoint3D_<T>& p) const

Points addition.

constexpr TPoint3D_<T> operator - (const TPoint3D_<T>& p) const

Points substraction.

constexpr T dot(const TPoint3D_<T>& p) const

Scalar product s=dot(this,p)

constexpr TPoint3D_<T> cross(const TPoint3D_<T>& p) const

Cross product res = cross(this, p)

void asString(std::string& s) const

Returns a human-readable textual representation of the object (eg: “[0.02 1.04 -0.8]” )

See also:

fromString

void fromString(const std::string& s)

Set the current object value from a string generated by ‘asString’ (eg: “[0.02 1.04 -0.8]” )

Parameters:

std::exception

On invalid format

See also:

asString

template <typename Vector>
static TPoint3D_<T> FromVector(const Vector& v)

Builds from the first 3 elements of a vector-like object: [x y z].

Parameters:

Vector

It can be std::vector<double>, Eigen::VectorXd, etc.