LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
LSST Data Management Base Package
Loading...
Searching...
No Matches
Vector3d.cc
Go to the documentation of this file.
1/*
2 * This file is part of sphgeom.
3 *
4 * Developed for the LSST Data Management System.
5 * This product includes software developed by the LSST Project
6 * (http://www.lsst.org).
7 * See the COPYRIGHT file at the top-level directory of this distribution
8 * for details of code ownership.
9 *
10 * This software is dual licensed under the GNU General Public License and also
11 * under a 3-clause BSD license. Recipients may choose which of these licenses
12 * to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
13 * respectively. If you choose the GPL option then the following text applies
14 * (but note that there is still no warranty even if you opt for BSD instead):
15 *
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29
32
34
35#if !defined(NO_SIMD) && defined(__x86_64__)
36 #include <x86intrin.h>
37#endif
38#include <cstdio>
39#include <ostream>
40#include <cstdint>
41
42#include "lsst/sphgeom/Angle.h"
44
45
46namespace lsst {
47namespace sphgeom {
48
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}
132
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}
140
142 char buf[128];
143 std::snprintf(buf, sizeof(buf), "[%.17g, %.17g, %.17g]",
144 v.x(), v.y(), v.z());
145 return os << buf;
146}
147
148}} // namespace lsst::sphgeom
This file declares a class for representing unit vectors in ℝ³.
This file declares a class for representing vectors in ℝ³.
Angle represents an angle in radians.
Definition Angle.h:50
UnitVector3d is a unit vector in ℝ³ with components stored in double precision.
double dot(Vector3d const &v) const
dot returns the inner product of this unit vector and v.
Vector3d cross(Vector3d const &v) const
cross returns the cross product of this unit vector and v.
Vector3d is a vector in ℝ³ with components stored in double precision.
Definition Vector3d.h:51
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...
Definition Vector3d.cc:133
double x() const
Definition Vector3d.h:73
double y() const
Definition Vector3d.h:75
double normalize()
normalize scales this vector to have unit norm and returns its norm prior to scaling.
Definition Vector3d.cc:49
double z() const
Definition Vector3d.h:77
T copysign(T... args)
T fabs(T... args)
T snprintf(T... args)
std::ostream & operator<<(std::ostream &, Angle const &)
Definition Angle.cc:41
double sin(Angle const &a)
Definition Angle.h:109
double cos(Angle const &a)
Definition Angle.h:110
This file declares a class for representing angles.
T sqrt(T... args)
double w
Definition CoaddPsf.cc:70