template struct mrpt::math::TPoint2D_

Overview

Base template for TPoint2D (double) and TPoint2Df (float).

Forward declarations of all mrpt::math classes related to poses and points.

Represents a point (or free vector) in the 2D Euclidean space R^2. Coordinates are stored as (x, y).

This is a lightweight POD-like type intended for storage and arithmetic. When pose-point composition (i.e. applying an SE(2) rigid transformation to a point) is needed, use mrpt::poses::CPose2D or mrpt::math::TPose2D instead.

TVector2D is a type alias for TPoint2D (both represent elements of R^2; the alias is provided for semantic clarity when the object is a free vector rather than a position).

See also:

mrpt::poses::CPoint2D, TPose2D, TPoint3D

#include <mrpt/math/TPoint2D.h>

template <typename T>
struct TPoint2D_:
    public mrpt::math::TPoint2D_data,
    public mrpt::math::TPoseOrPoint
{
    // fields

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

    // construction

    TPoint2D_();
    TPoint2D_(T xx, T yy);

    template <typename U>
    TPoint2D_(const TPoint2D_data<U>& p);

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

    TPoint2D_(const TPose2D& p);
    TPoint2D_(const TPoint3D_<T>& p);
    TPoint2D_(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>
    TPoint2D_<U> cast() const;

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

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

    template <typename Vector>
    Vector asVector() const;

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

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

    constexpr auto as_tuple() const;
    T sqrNorm() const;
    T norm() const;
    TPoint2D_<T> unitarize() const;

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

    static TPoint2D_ FromString(const std::string& s);
};

Inherited Members

public:
    // fields

    T y;

Fields

T x

X,Y coordinates.

Construction

TPoint2D_()

Default constructor.

Initializes to zeros

TPoint2D_(T xx, T yy)

Constructor from coordinates

template <typename U>
TPoint2D_(const TPoint2D_data<U>& p)

Explicit constructor from coordinates.

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

Constructor from column vector.

TPoint2D_(const TPose2D& p)

Constructor from TPose2D, retaining only the translational part (x,y); phi is discarded.

See also:

TPose2D

TPoint2D_(const TPoint3D_<T>& p)

Constructor from TPoint3D, retaining only (x,y); z is discarded.

See also:

TPoint3D

TPoint2D_(const TPose3D& p)

Constructor from TPose3D, retaining only the translational (x,y) part; z and all angular coordinates are discarded.

See also:

TPose3D

Methods

template <typename U>
TPoint2D_<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). Valid for standard-layout struct with no padding between x and y.

T& operator [] (size_t i)

Coordinate access using operator[].

Order: x,y

constexpr T operator [] (size_t i) const

Coordinate access using operator[].

Order: x,y

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.

void asString(std::string& s) const

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

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]” )

Parameters:

std::exception

On invalid format

See also:

asString

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

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

T sqrNorm() const

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

T norm() const

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

TPoint2D_<T> unitarize() const

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

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

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

Parameters:

Vector

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