LSST Applications g0f08755f38+9c285cab97,g1635faa6d4+13f3999e92,g1653933729+a8ce1bb630,g1a0ca8cf93+bf6eb00ceb,g28da252d5a+0829b12dee,g29321ee8c0+5700dc9eac,g2bbee38e9b+9634bc57db,g2bc492864f+9634bc57db,g2cdde0e794+c2c89b37c4,g3156d2b45e+41e33cbcdc,g347aa1857d+9634bc57db,g35bb328faa+a8ce1bb630,g3a166c0a6a+9634bc57db,g3e281a1b8c+9f2c4e2fc3,g414038480c+077ccc18e7,g41af890bb2+fde0dd39b6,g5fbc88fb19+17cd334064,g781aacb6e4+a8ce1bb630,g80478fca09+55a9465950,g82479be7b0+d730eedb7d,g858d7b2824+9c285cab97,g9125e01d80+a8ce1bb630,g9726552aa6+10f999ec6a,ga5288a1d22+2a84bb7594,gacf8899fa4+c69c5206e8,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+9634bc57db,gcf0d15dbbd+4b7d09cae4,gda3e153d99+9c285cab97,gda6a2b7d83+4b7d09cae4,gdaeeff99f8+1711a396fd,ge2409df99d+5e831397f4,ge79ae78c31+9634bc57db,gf0baf85859+147a0692ba,gf3967379c6+41c94011de,gf3fb38a9a8+8f07a9901b,gfb92a5be7c+9c285cab97,w.2024.46
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 49 of file Vector3d.cc.

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

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

133 {
134 // Use Rodrigues' rotation formula.
135 Vector3d const & v = *this;
136 double s = sin(a);
137 double c = cos(a);
138 return v * c + k.cross(v) * s + k * (k.dot(v) * (1.0 - c));
139}
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: