23#ifndef VCL_SPACE_CORE_POINT_H
24#define VCL_SPACE_CORE_POINT_H
26#include <vclib/concepts/space/point.h>
27#include <vclib/io/serialization.h>
28#include <vclib/math/base.h>
29#include <vclib/misc/hash.h>
32#include <Eigen/Geometry>
56template<
typename Scalar, u
int N>
57class Point :
public Eigen::Matrix<Scalar, N, 1>
59 using Base = Eigen::Matrix<Scalar, N, 1>;
62 using BaseMatrixType = Base;
65 using Base::operator+;
66 using Base::operator-;
67 using Base::operator*;
68 using Base::operator/;
69 using Base::operator+=;
70 using Base::operator-=;
71 using Base::operator*=;
72 using Base::operator/=;
82 static const uint
DIM = N;
98 template<
typename OtherDerived>
212 const ScalarType& at(uint i)
const {
return Base::operator()(i); }
228 if constexpr (std::is_same_v<Scalar, S>) {
249 for (
size_t i = 0;
i <
DIM; ++
i)
273 Scalar
epsilon = std::numeric_limits<Scalar>::epsilon())
const
276 for (
size_t i = 0;
i <
DIM; ++
i)
296 Scalar
w = Base::norm() * p1.norm();
299 Scalar t = Base::dot(p1) /
w;
304 return (Scalar)
acos(t);
319 Scalar
dist(
const Point& p1)
const {
return (*
this - p1).norm(); }
335 return (*
this - p1).squaredNorm();
353 for (
size_t i = 0;
i <
DIM; ++
i)
377 for (
size_t i = 0;
i <
DIM; ++
i) {
379 throw std::runtime_error(
380 "Math error: Attempted to divide by Zero\n");
394 constexpr uint
size()
const {
return N; }
413 Scalar
args[N] = {
static_cast<Scalar
>(
scalars)...};
414 for (uint
i = 0;
i < N;
i++)
432 Eigen::Matrix<ScalarType, DIM, DIM>
res;
433 for (uint
i = 0;
i <
DIM;
i++) {
434 for (uint
j = 0;
j <
DIM;
j++) {
468 double len = u.norm();
470 if (std::abs(at(0)) < std::abs(at(1))) {
471 if (std::abs(at(0)) < std::abs(at(2)))
477 if (std::abs(at(1)) < std::abs(at(2)))
493 vcl::serializeN(
os, Base::data(), N);
502 vcl::deserializeN(is, Base::data(), N);
516 for (
size_t i = 0;
i <
DIM; ++
i)
517 hashCombine(
h, at(
i));
526 template<
typename OtherDerived>
529 this->Base::operator=(
other);
550 while (
i <
DIM && at(
i) == p1[
i]) {
553 return i ==
DIM ? std::strong_ordering::equal : at(
i) <=> p1[
i];
613 Eigen::Matrix<Scalar, 1, N> s;
615 s(0) =
m(0, 0) * at(0) +
m(0, 1) * at(1) +
m(0, 2) * at(2) +
m(0, 3);
616 s(1) =
m(1, 0) * at(0) +
m(1, 1) * at(1) +
m(1, 2) * at(2) +
m(1, 3);
617 s(2) =
m(2, 0) * at(0) +
m(2, 1) * at(1) +
m(2, 2) * at(2) +
m(2, 3);
620 at(0) *
m(3, 0) + at(1) *
m(3, 1) + at(2) *
m(3, 2) +
m(3, 3);
697template<
typename Scalar, u
int N>
699 const Point<Scalar, N>& p1,
700 const Point<Scalar, N>& p2,
701 const Scalar& epsilon = std::numeric_limits<Scalar>::epsilon())
703 return p1.epsilonEquals(p2, epsilon);
719template<
typename Scalar>
768template<
typename Scalar>
817template<
typename Scalar>
857template<
typename S,
typename...
Scalars>
865template<
typename Scalar,
unsigned int N>
866struct hash<vcl::Point<Scalar, N>>
The Point class represents an N-dimensional point containing N scalar values.
Definition point.h:58
auto outerProduct(const Point &p1) const
Returns the outer product between this point p and p1, which is p * p1^T.
Definition point.h:430
Point & operator*=(const Eigen::Matrix< Scalar, N+1, N+1 > &m)
Applies a TRS 4x4 matrix transformation to this point.
Definition point.h:671
Point & operator+=(const Scalar &s)
Adds a scalar value to this point.
Definition point.h:636
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:610
Point(const Eigen::MatrixBase< OtherDerived > &other)
Constructs a Point object from an Eigen matrix.
Definition point.h:99
Scalar operator*(const Point &p1) const
Computes the dot product of this point with another point.
Definition point.h:593
Point operator-(const Scalar &s) const
Subtracts a scalar value from each coordinate of the point.
Definition point.h:580
Scalar squaredDist(const Point &p1) const
Computes the squared Euclidean distance between two Point objects.
Definition point.h:333
Point(Scalars... scalars)
Constructs a Point object from a set of scalar values.
Definition point.h:117
auto cast() const
Casts the Point object to a different scalar type.
Definition point.h:226
const ScalarType & y() const
Returns a const reference to the y-component of the Point object.
Definition point.h:164
Scalar ScalarType
The Scalar type of the Point.
Definition point.h:77
void orthoBase(Point &u, Point &v) const
Computes an Orthonormal Basis starting from this point n.
Definition point.h:461
constexpr uint size() const
Returns the size of the Point object.
Definition point.h:394
ScalarType & z()
Returns a reference to the z-component of the Point object.
Definition point.h:175
Scalar angle(const Point &p1) const
Computes the angle between two Point objects.
Definition point.h:294
Scalar dist(const Point &p1) const
Computes the Euclidean distance between two Point objects.
Definition point.h:319
Point operator+(const Scalar &s) const
Adds a scalar value to each coordinate of the point.
Definition point.h:567
Point()
Constructs a Point object with all components set to zero.
Definition point.h:87
Point div(const Point &p1) const
Divides the components of two Point objects.
Definition point.h:374
ScalarType & x()
Returns a reference to the x-component of the Point object.
Definition point.h:131
Point & operator=(const Eigen::MatrixBase< OtherDerived > &other)
Assigns the point to the given Eigen matrix.
Definition point.h:527
ScalarType & w()
Returns a reference to the w-component of the Point object.
Definition point.h:197
Point & operator-=(const Scalar &s)
Subtracts a scalar value from this point.
Definition point.h:652
void deserialize(std::istream &is)
Deserializes the point from the given input stream.
Definition point.h:500
auto operator<=>(const Point &p1) const
Compares the point with another point using the spaceship operator.
Definition point.h:547
void set(Scalars... scalars)
Sets all the components of the Point object from a set of scalar values.
Definition point.h:411
const ScalarType & z() const
Returns a const reference to the z-component of the Point object.
Definition point.h:186
void serialize(std::ostream &os) const
Serializes the point to the given output stream.
Definition point.h:491
std::size_t hash() const
Computes the hash value of the point.
Definition point.h:513
static const uint DIM
DIM: the number of dimensions of the Point.
Definition point.h:82
Point mul(const Point &p1) const
Multiplies the components of two Point objects.
Definition point.h:350
ScalarType & y()
Returns a reference to the y-component of the Point object.
Definition point.h:153
const ScalarType & w() const
Returns a const reference to the w-component of the Point object.
Definition point.h:208
const ScalarType & x() const
Returns a const reference to the x-component of the Point object.
Definition point.h:142
bool isDegenerate() const
Returns true if at least one of its components is NaN or inf.
Definition point.h:247
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:271
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
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 base.h:66
bool isDegenerate(Scalar number)
Checks if a floating point number is degenerate.
Definition base.h:45