LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
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