LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Public Member Functions | List of all members
lsst::sphgeom::Vector3d Class Reference

Vector3d is a vector in ℝ³ with components stored in double precision. More...

#include <Vector3d.h>

Public Member Functions

 Vector3d ()
 The default constructor creates a zero vector. More...
 
 Vector3d (double x, double y, double z)
 This constructor creates a vector with the given components. More...
 
bool operator== (Vector3d const &v) const
 
bool operator!= (Vector3d const &v) const
 
double const * getData () const
 data returns a pointer to the 3 components of this vector. More...
 
double operator() (int i) const
 The function call operator returns the i-th component of this vector. More...
 
double x () const
 
double y () const
 
double z () const
 
double dot (Vector3d const &v) const
 dot returns the inner product of this vector and v. More...
 
double getSquaredNorm () const
 getSquaredNorm returns the inner product of this vector with itself. More...
 
double getNorm () const
 getNorm returns the L2 norm of this vector. More...
 
bool isZero () const
 isZero returns true if all the components of this vector are zero. More...
 
double normalize ()
 normalize scales this vector to have unit norm and returns its norm prior to scaling. More...
 
bool isNormalized () const
 isNormalized returns true if this vectors norm is very close to 1. More...
 
Vector3d cross (Vector3d const &v) const
 cross returns the cross product of this vector and v. More...
 
Vector3d operator- () const
 The unary minus operator negates every component of this vector. More...
 
Vector3d operator* (double s) const
 The multiplication operator returns the component-wise product of this vector with scalar s. More...
 
Vector3d operator/ (double s) const
 The division operator returns the component-wise quotient of this vector with scalar s. More...
 
Vector3d operator+ (Vector3d const &v) const
 The addition operator returns the sum of this vector and v. More...
 
Vector3d operator- (Vector3d const &v) const
 The subtraction operator returns the difference between this vector and v. More...
 
Vector3doperator*= (double s)
 
Vector3doperator/= (double s)
 
Vector3doperator+= (Vector3d const &v)
 
Vector3doperator-= (Vector3d const &v)
 
Vector3d cwiseProduct (Vector3d const &v) const
 cwiseProduct returns the component-wise product of this vector and v. More...
 
Vector3d rotatedAround (UnitVector3d const &k, Angle a) const
 rotatedAround returns a copy of this vector, rotated around the unit vector k by angle a according to the right hand rule. More...
 

Detailed Description

Vector3d is a vector in ℝ³ with components stored in double precision.

Definition at line 44 of file Vector3d.h.

Constructor & Destructor Documentation

◆ Vector3d() [1/2]

lsst::sphgeom::Vector3d::Vector3d ( )
inline

The default constructor creates a zero vector.

Definition at line 47 of file Vector3d.h.

47 { _v[0] = 0.0; _v[1] = 0.0; _v[2] = 0.0; }

◆ Vector3d() [2/2]

lsst::sphgeom::Vector3d::Vector3d ( double  x,
double  y,
double  z 
)
inline

This constructor creates a vector with the given components.

Definition at line 50 of file Vector3d.h.

50 { _v[0] = x; _v[1] = y; _v[2] = z; }
double x() const
Definition: Vector3d.h:66
double y() const
Definition: Vector3d.h:68
double z() const
Definition: Vector3d.h:70

Member Function Documentation

◆ cross()

Vector3d lsst::sphgeom::Vector3d::cross ( Vector3d const &  v) const
inline

cross returns the cross product of this vector and v.

Definition at line 101 of file Vector3d.h.

101  {
102  return Vector3d(_v[1] * v._v[2] - _v[2] * v._v[1],
103  _v[2] * v._v[0] - _v[0] * v._v[2],
104  _v[0] * v._v[1] - _v[1] * v._v[0]);
105  }
Vector3d()
The default constructor creates a zero vector.
Definition: Vector3d.h:47

◆ cwiseProduct()

Vector3d lsst::sphgeom::Vector3d::cwiseProduct ( Vector3d const &  v) const
inline

cwiseProduct returns the component-wise product of this vector and v.

Definition at line 150 of file Vector3d.h.

150  {
151  return Vector3d(_v[0] * v._v[0],
152  _v[1] * v._v[1],
153  _v[2] * v._v[2]);
154  }

◆ dot()

double lsst::sphgeom::Vector3d::dot ( Vector3d const &  v) const
inline

dot returns the inner product of this vector and v.

Definition at line 73 of file Vector3d.h.

73  {
74  return _v[0] * v._v[0] + _v[1] * v._v[1] + _v[2] * v._v[2];
75  }

◆ getData()

double const* lsst::sphgeom::Vector3d::getData ( ) const
inline

data returns a pointer to the 3 components of this vector.

Definition at line 61 of file Vector3d.h.

61 { return _v; }

◆ getNorm()

double lsst::sphgeom::Vector3d::getNorm ( ) const
inline

getNorm returns the L2 norm of this vector.

Definition at line 81 of file Vector3d.h.

81  {
82  return std::sqrt(getSquaredNorm());
83  }
double getSquaredNorm() const
getSquaredNorm returns the inner product of this vector with itself.
Definition: Vector3d.h:78
T sqrt(T... args)

◆ getSquaredNorm()

double lsst::sphgeom::Vector3d::getSquaredNorm ( ) const
inline

getSquaredNorm returns the inner product of this vector with itself.

Definition at line 78 of file Vector3d.h.

78 { return dot(*this); }
double dot(Vector3d const &v) const
dot returns the inner product of this vector and v.
Definition: Vector3d.h:73

◆ isNormalized()

bool lsst::sphgeom::Vector3d::isNormalized ( ) const
inline

isNormalized returns true if this vectors norm is very close to 1.

Definition at line 96 of file Vector3d.h.

96  {
97  return std::fabs(1.0 - getSquaredNorm()) <= 1e-15;
98  }
T fabs(T... args)

◆ isZero()

bool lsst::sphgeom::Vector3d::isZero ( ) const
inline

isZero returns true if all the components of this vector are zero.

Definition at line 86 of file Vector3d.h.

86 { return *this == Vector3d(); }

◆ normalize()

double lsst::sphgeom::Vector3d::normalize ( )

normalize scales this vector to have unit norm and returns its norm prior to scaling.

It will accurately normalize any vector with finite components except for (0, 0, 0), including those with norms that overflow. Trying to normalize (0, 0, 0) will cause a std::runtime_error to be thrown.

Definition at line 41 of file Vector3d.cc.

41  {
42  static constexpr uint8_t UNUSED = 255;
43  // Given a 3 component vector (x, y, z), this LUT provides the indexes
44  // of the components in order of smallest absolute value to largest.
45  // The index into the LUT must be computed as:
46  //
47  // ((|x| > |z|) << 2) +
48  // ((|x| > |y|) << 1) +
49  // (|y| > |z|)
50  static uint8_t const COMPONENT[8][4] = {
51  {0, 1, 2, UNUSED},
52  {0, 2, 1, UNUSED},
53  {1, 0, 2, UNUSED},
54  {UNUSED, UNUSED, UNUSED, UNUSED},
55  {UNUSED, UNUSED, UNUSED, UNUSED},
56  {2, 0, 1, UNUSED},
57  {1, 2, 0, UNUSED},
58  {2, 1, 0, UNUSED}
59  };
60 #if defined(NO_SIMD) || !defined(__x86_64__)
61  double ax = std::fabs(_v[0]);
62  double ay = std::fabs(_v[1]);
63  double az = std::fabs(_v[2]);
64  int index = ((ax > az) << 2) +
65  ((ax > ay) << 1) +
66  (ay > az);
67  double w = _v[COMPONENT[index][2]];
68  if (w == 0.0) {
69  throw std::runtime_error("Cannot normalize zero vector");
70  }
71  // Divide components by the absolute value of the largest
72  // component to avoid overflow/underflow.
73  double maxabs = std::fabs(w);
74  double u = _v[COMPONENT[index][0]] / maxabs;
75  double v = _v[COMPONENT[index][1]] / maxabs;
76  w = std::copysign(1.0, w);
77  double d = u * u + v * v;
78  double norm = std::sqrt(1.0 + d);
79  _v[COMPONENT[index][0]] = u / norm;
80  _v[COMPONENT[index][1]] = v / norm;
81  _v[COMPONENT[index][2]] = w / norm;
82  return norm * maxabs;
83 #else
84  static __m128d const m0m0 = _mm_set_pd(-0.0, -0.0);
85  __m128d ayaz = _mm_andnot_pd(m0m0, _mm_loadu_pd(_v + 1));
86  __m128d axax = _mm_andnot_pd(m0m0, _mm_set1_pd(_v[0]));
87  __m128d az = _mm_unpackhi_pd(ayaz, _mm_setzero_pd());
88  int index = (_mm_movemask_pd(_mm_cmpgt_pd(axax, ayaz)) << 1) |
89  _mm_movemask_pd(_mm_cmplt_sd(az, ayaz));
90  // The lower double in uv contains the vector component
91  // with the lowest absolute value. The higher double contains
92  // the component with absolute value betweem the lowest and
93  // highest absolute values.
94  __m128d uv = _mm_set_pd(_v[COMPONENT[index][1]],
95  _v[COMPONENT[index][0]]);
96  // ww contains two copies of the vector component with the
97  // highest absolute value.
98  __m128d ww = _mm_set1_pd(_v[COMPONENT[index][2]]);
99  __m128d maxabs = _mm_andnot_pd(m0m0, ww);
100  if (_mm_ucomieq_sd(ww, _mm_setzero_pd())) {
101  throw std::runtime_error("Cannot normalize zero vector");
102  }
103  // Divide components by the absolute value of the largest
104  // component to avoid overflow/underflow.
105  uv = _mm_div_pd(uv, maxabs);
106  ww = _mm_or_pd(_mm_and_pd(m0m0, ww), _mm_set1_pd(1.0));
107  __m128d norm = _mm_mul_pd(uv, uv);
108  norm = _mm_sqrt_sd(
109  _mm_setzero_pd(),
110  _mm_add_sd(
111  _mm_set_sd(1.0),
112  _mm_add_sd(norm, _mm_unpackhi_pd(norm, _mm_setzero_pd()))
113  )
114  );
115  // Normalize components and store the results.
116  ww = _mm_div_sd(ww, norm);
117  uv = _mm_div_pd(uv, _mm_shuffle_pd(norm, norm, 0));
118  _mm_store_sd(&_v[COMPONENT[index][0]], uv);
119  _mm_storeh_pd(&_v[COMPONENT[index][1]], uv);
120  _mm_store_sd(&_v[COMPONENT[index][2]], ww);
121  return _mm_cvtsd_f64(_mm_mul_sd(norm, maxabs));
122 #endif
123 }
T copysign(T... args)
T norm(const T &x)
Definition: Integrate.h:160
double w
Definition: CoaddPsf.cc:69

◆ operator!=()

bool lsst::sphgeom::Vector3d::operator!= ( Vector3d const &  v) const
inline

Definition at line 56 of file Vector3d.h.

56  {
57  return _v[0] != v._v[0] || _v[1] != v._v[1] || _v[2] != v._v[2];
58  }

◆ operator()()

double lsst::sphgeom::Vector3d::operator() ( int  i) const
inline

The function call operator returns the i-th component of this vector.

Definition at line 64 of file Vector3d.h.

64 { return _v[i]; }

◆ operator*()

Vector3d lsst::sphgeom::Vector3d::operator* ( double  s) const
inline

The multiplication operator returns the component-wise product of this vector with scalar s.

Definition at line 116 of file Vector3d.h.

116  {
117  return Vector3d(_v[0] * s,
118  _v[1] * s,
119  _v[2] * s);
120  }

◆ operator*=()

Vector3d& lsst::sphgeom::Vector3d::operator*= ( double  s)
inline

Definition at line 144 of file Vector3d.h.

144 { *this = *this * s; return *this; }

◆ operator+()

Vector3d lsst::sphgeom::Vector3d::operator+ ( Vector3d const &  v) const
inline

The addition operator returns the sum of this vector and v.

Definition at line 131 of file Vector3d.h.

131  {
132  return Vector3d(_v[0] + v._v[0],
133  _v[1] + v._v[1],
134  _v[2] + v._v[2]);
135  }

◆ operator+=()

Vector3d& lsst::sphgeom::Vector3d::operator+= ( Vector3d const &  v)
inline

Definition at line 146 of file Vector3d.h.

146 { *this = *this + v; return *this; }

◆ operator-() [1/2]

Vector3d lsst::sphgeom::Vector3d::operator- ( ) const
inline

The unary minus operator negates every component of this vector.

Definition at line 108 of file Vector3d.h.

108  {
109  return Vector3d(-_v[0],
110  -_v[1],
111  -_v[2]);
112  }

◆ operator-() [2/2]

Vector3d lsst::sphgeom::Vector3d::operator- ( Vector3d const &  v) const
inline

The subtraction operator returns the difference between this vector and v.

Definition at line 138 of file Vector3d.h.

138  {
139  return Vector3d(_v[0] - v._v[0],
140  _v[1] - v._v[1],
141  _v[2] - v._v[2]);
142  }

◆ operator-=()

Vector3d& lsst::sphgeom::Vector3d::operator-= ( Vector3d const &  v)
inline

Definition at line 147 of file Vector3d.h.

147 { *this = *this - v; return *this; }

◆ operator/()

Vector3d lsst::sphgeom::Vector3d::operator/ ( double  s) const
inline

The division operator returns the component-wise quotient of this vector with scalar s.

Definition at line 124 of file Vector3d.h.

124  {
125  return Vector3d(_v[0] / s,
126  _v[1] / s,
127  _v[2] / s);
128  }

◆ operator/=()

Vector3d& lsst::sphgeom::Vector3d::operator/= ( double  s)
inline

Definition at line 145 of file Vector3d.h.

145 { *this = *this / s; return *this; }

◆ operator==()

bool lsst::sphgeom::Vector3d::operator== ( Vector3d const &  v) const
inline

Definition at line 52 of file Vector3d.h.

52  {
53  return _v[0] == v._v[0] && _v[1] == v._v[1] && _v[2] == v._v[2];
54  }

◆ rotatedAround()

Vector3d lsst::sphgeom::Vector3d::rotatedAround ( UnitVector3d const &  k,
Angle  a 
) const

rotatedAround returns a copy of this vector, rotated around the unit vector k by angle a according to the right hand rule.

Definition at line 125 of file Vector3d.cc.

125  {
126  // Use Rodrigues' rotation formula.
127  Vector3d const & v = *this;
128  double s = sin(a);
129  double c = cos(a);
130  return v * c + k.cross(v) * s + k * (k.dot(v) * (1.0 - c));
131 }
table::Key< int > a
double sin(Angle const &a)
Definition: Angle.h:102
double cos(Angle const &a)
Definition: Angle.h:103

◆ x()

double lsst::sphgeom::Vector3d::x ( ) const
inline

Definition at line 66 of file Vector3d.h.

66 { return _v[0]; }

◆ y()

double lsst::sphgeom::Vector3d::y ( ) const
inline

Definition at line 68 of file Vector3d.h.

68 { return _v[1]; }

◆ z()

double lsst::sphgeom::Vector3d::z ( ) const
inline

Definition at line 70 of file Vector3d.h.

70 { return _v[2]; }

The documentation for this class was generated from the following files: