LSST Applications g0265f82a02+093ff98f47,g02d81e74bb+10352d6f39,g1f3e9fa97e+40b0fc677d,g2079a07aa2+14824f138e,g2bbee38e9b+093ff98f47,g337abbeb29+093ff98f47,g3ddfee87b4+064c02c7ee,g487adcacf7+7e842ebf4b,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g568d43a26c+1d7ac31cb0,g591dd9f2cf+fb1f69e2ea,g858d7b2824+10352d6f39,g8a8a8dda67+a6fc98d2e7,g8cdfe0ae6a+66d966b544,g99cad8db69+7ce8a7c20a,g9ddcbc5298+d4bad12328,ga1e77700b3+246acaaf9c,ga2e4dd1c03+064c02c7ee,ga8c6da7877+04f6ba86dc,gae46bcf261+093ff98f47,gb0e22166c9+3863383f4c,gba4ed39666+9664299f35,gbb8dafda3b+db40f59a7d,gbeb006f7da+e6a448e96d,gbf5cecdb8a+10352d6f39,gc0f3af6251+10a3fd39cd,gc120e1dc64+5839e50a77,gc28159a63d+093ff98f47,gcf0d15dbbd+064c02c7ee,gd2a12a3803+0c2c227a2b,gdaeeff99f8+a38ce5ea23,ge79ae78c31+093ff98f47,gee10cc3b42+a6fc98d2e7,gf1cff7945b+10352d6f39,w.2024.15
LSST Data Management Base Package
Loading...
Searching...
No Matches
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.
 
 Vector3d (double x, double y, double z)
 This constructor creates a vector with the given components.
 
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.
 
double operator() (int i) const
 The function call operator returns the i-th component of this vector.
 
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.
 
double getSquaredNorm () const
 getSquaredNorm returns the inner product of this vector with itself.
 
double getNorm () const
 getNorm returns the L2 norm of this vector.
 
bool isZero () const
 isZero returns true if all the components of this vector are zero.
 
double normalize ()
 normalize scales this vector to have unit norm and returns its norm prior to scaling.
 
bool isNormalized () const
 isNormalized returns true if this vectors norm is very close to 1.
 
Vector3d cross (Vector3d const &v) const
 cross returns the cross product of this vector and v.
 
Vector3d operator- () const
 The unary minus operator negates every component of this vector.
 
Vector3d operator* (double s) const
 The multiplication operator returns the component-wise product of this vector with scalar s.
 
Vector3d operator/ (double s) const
 The division operator returns the component-wise quotient of this vector with scalar s.
 
Vector3d operator+ (Vector3d const &v) const
 The addition operator returns the sum of this vector and v.
 
Vector3d operator- (Vector3d const &v) const
 The subtraction operator returns the difference between this vector and v.
 
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.
 
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.
 

Detailed Description

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

Definition at line 51 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 54 of file Vector3d.h.

54{ _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 57 of file Vector3d.h.

57{ _v[0] = x; _v[1] = y; _v[2] = z; }
double x() const
Definition Vector3d.h:73
double y() const
Definition Vector3d.h:75
double z() const
Definition Vector3d.h:77

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 108 of file Vector3d.h.

108 {
109 return Vector3d(_v[1] * v._v[2] - _v[2] * v._v[1],
110 _v[2] * v._v[0] - _v[0] * v._v[2],
111 _v[0] * v._v[1] - _v[1] * v._v[0]);
112 }
Vector3d()
The default constructor creates a zero vector.
Definition Vector3d.h:54

◆ 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 157 of file Vector3d.h.

157 {
158 return Vector3d(_v[0] * v._v[0],
159 _v[1] * v._v[1],
160 _v[2] * v._v[2]);
161 }

◆ dot()

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

dot returns the inner product of this vector and v.

Definition at line 80 of file Vector3d.h.

80 {
81 return _v[0] * v._v[0] + _v[1] * v._v[1] + _v[2] * v._v[2];
82 }

◆ getData()

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

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

Definition at line 68 of file Vector3d.h.

68{ return _v; }

◆ getNorm()

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

getNorm returns the L2 norm of this vector.

Definition at line 88 of file Vector3d.h.

88 {
89 return std::sqrt(getSquaredNorm());
90 }
double getSquaredNorm() const
getSquaredNorm returns the inner product of this vector with itself.
Definition Vector3d.h:85
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 85 of file Vector3d.h.

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

◆ isNormalized()

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

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

Definition at line 103 of file Vector3d.h.

103 {
104 return std::fabs(1.0 - getSquaredNorm()) <= 1e-15;
105 }
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 93 of file Vector3d.h.

93{ 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 48 of file Vector3d.cc.

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

◆ operator!=()

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

Definition at line 63 of file Vector3d.h.

63 {
64 return _v[0] != v._v[0] || _v[1] != v._v[1] || _v[2] != v._v[2];
65 }

◆ 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 71 of file Vector3d.h.

71{ 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 123 of file Vector3d.h.

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

◆ operator*=()

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

Definition at line 151 of file Vector3d.h.

151{ *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 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 153 of file Vector3d.h.

153{ *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 115 of file Vector3d.h.

115 {
116 return Vector3d(-_v[0],
117 -_v[1],
118 -_v[2]);
119 }

◆ 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 145 of file Vector3d.h.

145 {
146 return Vector3d(_v[0] - v._v[0],
147 _v[1] - v._v[1],
148 _v[2] - v._v[2]);
149 }

◆ operator-=()

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

Definition at line 154 of file Vector3d.h.

154{ *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 131 of file Vector3d.h.

131 {
132 return Vector3d(_v[0] / s,
133 _v[1] / s,
134 _v[2] / s);
135 }

◆ operator/=()

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

Definition at line 152 of file Vector3d.h.

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

◆ operator==()

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

Definition at line 59 of file Vector3d.h.

59 {
60 return _v[0] == v._v[0] && _v[1] == v._v[1] && _v[2] == v._v[2];
61 }

◆ 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 132 of file Vector3d.cc.

132 {
133 // Use Rodrigues' rotation formula.
134 Vector3d const & v = *this;
135 double s = sin(a);
136 double c = cos(a);
137 return v * c + k.cross(v) * s + k * (k.dot(v) * (1.0 - c));
138}
double sin(Angle const &a)
Definition Angle.h:109
double cos(Angle const &a)
Definition Angle.h:110

◆ x()

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

Definition at line 73 of file Vector3d.h.

73{ return _v[0]; }

◆ y()

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

Definition at line 75 of file Vector3d.h.

75{ return _v[1]; }

◆ z()

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

Definition at line 77 of file Vector3d.h.

77{ return _v[2]; }

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