23#ifndef VCL_SPACE_CORE_POINT_H
24#define VCL_SPACE_CORE_POINT_H
26#include <vclib/base.h>
29#include <Eigen/Geometry>
53template<
typename Scalar, u
int N>
54class Point :
public Eigen::Matrix<Scalar, N, 1>
56 using Base = Eigen::Matrix<Scalar, N, 1>;
59 using BaseMatrixType = Base;
62 using Base::operator+;
63 using Base::operator-;
64 using Base::operator*;
65 using Base::operator/;
66 using Base::operator+=;
67 using Base::operator-=;
68 using Base::operator*=;
69 using Base::operator/=;
79 static const uint
DIM = N;
95 template<
typename OtherDerived>
209 const ScalarType& at(uint i)
const {
return Base::operator()(i); }
225 if constexpr (std::is_same_v<Scalar, S>) {
246 for (
size_t i = 0;
i <
DIM; ++
i)
270 Scalar
epsilon = std::numeric_limits<Scalar>::epsilon())
const
273 for (
size_t i = 0;
i <
DIM; ++
i)
293 Scalar
w = Base::norm() * p1.norm();
296 Scalar t = Base::dot(p1) /
w;
301 return (Scalar)
acos(t);
316 Scalar
dist(
const Point& p1)
const {
return (*
this - p1).norm(); }
332 return (*
this - p1).squaredNorm();
350 for (
size_t i = 0;
i <
DIM; ++
i)
374 for (
size_t i = 0;
i <
DIM; ++
i) {
376 throw std::runtime_error(
377 "Math error: Attempted to divide by Zero\n");
391 constexpr uint
size()
const {
return N; }
410 Scalar
args[N] = {
static_cast<Scalar
>(
scalars)...};
411 for (uint
i = 0;
i < N;
i++)
429 Eigen::Matrix<ScalarType, DIM, DIM>
res;
430 for (uint
i = 0;
i <
DIM;
i++) {
431 for (uint
j = 0;
j <
DIM;
j++) {
465 double len = u.norm();
467 if (std::abs(at(0)) < std::abs(at(1))) {
468 if (std::abs(at(0)) < std::abs(at(2)))
474 if (std::abs(at(1)) < std::abs(at(2)))
490 vcl::serializeN(
os, Base::data(), N);
499 vcl::deserializeN(is, Base::data(), N);
513 for (
size_t i = 0;
i <
DIM; ++
i)
523 template<
typename OtherDerived>
526 this->Base::operator=(
other);
547 while (
i <
DIM && at(
i) == p1[
i]) {
550 return i ==
DIM ? std::strong_ordering::equal : at(
i) <=> p1[
i];
610 Eigen::Matrix<Scalar, 1, N> s;
612 s(0) =
m(0, 0) * at(0) +
m(0, 1) * at(1) +
m(0, 2) * at(2) +
m(0, 3);
613 s(1) =
m(1, 0) * at(0) +
m(1, 1) * at(1) +
m(1, 2) * at(2) +
m(1, 3);
614 s(2) =
m(2, 0) * at(0) +
m(2, 1) * at(1) +
m(2, 2) * at(2) +
m(2, 3);
617 at(0) *
m(3, 0) + at(1) *
m(3, 1) + at(2) *
m(3, 2) +
m(3, 3);
694template<
typename Scalar>
743template<
typename Scalar>
792template<
typename Scalar>
844 std::remove_cvref_t<T>,
845 Point<typename RemoveRef<T>::ScalarType, RemoveRef<T>::DIM>>;
964template<
typename Scalar, u
int N>
966 const Point<Scalar, N>& p1,
967 const Point<Scalar, N>& p2,
968 const Scalar& epsilon = std::numeric_limits<Scalar>::epsilon())
970 return p1.epsilonEquals(p2, epsilon);
983template<
typename Scalar, u
int N>
989 for (
size_t i = 0;
i <
p.
DIM;
i++) {
990 p[
i] = std::min(p1[
i],
p2[
i]);
1005template<
typename Scalar, u
int N>
1006constexpr Point<Scalar, N>
max(
1007 const Point<Scalar, N>& p1,
1008 const Point<Scalar, N>& p2)
1011 for (
size_t i = 0; i < p.DIM; i++) {
1012 p[i] = std::max(p1[i], p2[i]);
1031template<
typename Scalar, u
int N>
1032std::ostream& operator<<(std::ostream& os,
const Point<Scalar, N>& p)
1037 static const Eigen::IOFormat rowFormat(
1038 Eigen::StreamPrecision,
1039 Eigen::DontAlignCols,
1048 return os << static_cast<const typename Point<Scalar, N>::BaseMatrixType&>(
1055template<
typename S,
typename... Scalars>
1056Point(S, Scalars... scalars) -> Point<S,
sizeof...(Scalars) + 1>;
1063template<
typename Scalar,
unsigned int N>
1064struct hash<vcl::Point<Scalar, N>>
A class representing a box in N-dimensional space.
Definition box.h:46
static const uint DIM
The dimensionality of the box.
Definition box.h:59
The Point class represents an N-dimensional point containing N scalar values.
Definition point.h:55
auto outerProduct(const Point &p1) const
Returns the outer product between this point p and p1, which is p * p1^T.
Definition point.h:427
Point & operator*=(const Eigen::Matrix< Scalar, N+1, N+1 > &m)
Applies a TRS 4x4 matrix transformation to this point.
Definition point.h:668
Point & operator+=(const Scalar &s)
Adds a scalar value to this point.
Definition point.h:633
Point operator*(const Eigen::Matrix< Scalar, N+1, N+1 > &m) const
Returns a new 3D point/vector on which has been applied a TRS 4x4 matrix.
Definition point.h:607
Point(const Eigen::MatrixBase< OtherDerived > &other)
Constructs a Point object from an Eigen matrix.
Definition point.h:96
Scalar operator*(const Point &p1) const
Computes the dot product of this point with another point.
Definition point.h:590
Point operator-(const Scalar &s) const
Subtracts a scalar value from each coordinate of the point.
Definition point.h:577
Scalar squaredDist(const Point &p1) const
Computes the squared Euclidean distance between two Point objects.
Definition point.h:330
Point(Scalars... scalars)
Constructs a Point object from a set of scalar values.
Definition point.h:114
auto cast() const
Casts the Point object to a different scalar type.
Definition point.h:223
const ScalarType & y() const
Returns a const reference to the y-component of the Point object.
Definition point.h:161
Scalar ScalarType
The Scalar type of the Point.
Definition point.h:74
void orthoBase(Point &u, Point &v) const
Computes an Orthonormal Basis starting from this point n.
Definition point.h:458
constexpr uint size() const
Returns the size of the Point object.
Definition point.h:391
ScalarType & z()
Returns a reference to the z-component of the Point object.
Definition point.h:172
Scalar angle(const Point &p1) const
Computes the angle between two Point objects.
Definition point.h:291
Scalar dist(const Point &p1) const
Computes the Euclidean distance between two Point objects.
Definition point.h:316
Point operator+(const Scalar &s) const
Adds a scalar value to each coordinate of the point.
Definition point.h:564
Point()
Constructs a Point object with all components set to zero.
Definition point.h:84
Point div(const Point &p1) const
Divides the components of two Point objects.
Definition point.h:371
ScalarType & x()
Returns a reference to the x-component of the Point object.
Definition point.h:128
Point & operator=(const Eigen::MatrixBase< OtherDerived > &other)
Assigns the point to the given Eigen matrix.
Definition point.h:524
ScalarType & w()
Returns a reference to the w-component of the Point object.
Definition point.h:194
Point & operator-=(const Scalar &s)
Subtracts a scalar value from this point.
Definition point.h:649
void deserialize(std::istream &is)
Deserializes the point from the given input stream.
Definition point.h:497
auto operator<=>(const Point &p1) const
Compares the point with another point using the spaceship operator.
Definition point.h:544
void set(Scalars... scalars)
Sets all the components of the Point object from a set of scalar values.
Definition point.h:408
const ScalarType & z() const
Returns a const reference to the z-component of the Point object.
Definition point.h:183
void serialize(std::ostream &os) const
Serializes the point to the given output stream.
Definition point.h:488
std::size_t hash() const
Computes the hash value of the point.
Definition point.h:510
static const uint DIM
DIM: the number of dimensions of the Point.
Definition point.h:79
Point mul(const Point &p1) const
Multiplies the components of two Point objects.
Definition point.h:347
ScalarType & y()
Returns a reference to the y-component of the Point object.
Definition point.h:150
const ScalarType & w() const
Returns a const reference to the w-component of the Point object.
Definition point.h:205
const ScalarType & x() const
Returns a const reference to the x-component of the Point object.
Definition point.h:139
bool isDegenerate() const
Returns true if at least one of its components is NaN or inf.
Definition point.h:244
bool epsilonEquals(const Point &p1, Scalar epsilon=std::numeric_limits< Scalar >::epsilon()) const
Checks for the equality of two Point objects within a given epsilon tolerance.
Definition point.h:268
The IteratorConcept is satisfied if T is an input or output iterator.
Definition iterators.h:37
A concept representing a 2D Point.
Definition point.h:858
A concept representing an iterator that iterates over 2D Points (specifically, a class that satisfies...
Definition point.h:914
A concept representing a 3D Point.
Definition point.h:871
A concept representing an iterator that iterates over 3D Points (specifically, a class that satisfies...
Definition point.h:930
A concept representing a 4D Point.
Definition point.h:884
A concept representing an iterator that iterates over 4D Points (specifically, a class that satisfies...
Definition point.h:946
A concept representing a Point.
Definition point.h:843
A concept representing an iterator that iterates over Points (specifically, a class that satisfies th...
Definition point.h:898
bool epsilonEquals(Scalar n1, Scalar n2, Scalar epsilon=std::numeric_limits< Scalar >::epsilon())
Checks if two floating point numbers are equal within an epsilon value.
Definition math.h:64
constexpr auto max(const T &p1, const T &p2)
Returns the maximum between the two parameters.
Definition min_max.h:81
bool isDegenerate(Scalar number)
Checks if a floating point number is degenerate.
Definition math.h:43
constexpr auto min(const T &p1, const T &p2)
Returns the minimum between the two parameters.
Definition min_max.h:40
void hashCombine(std::size_t &seed, const T &v, const Rest &... rest)
Starting from a seed, computes the hash of a series of objects.
Definition hash.h:42