Skip to content

Commit aaf22f1

Browse files
committed
Add quaternion constants in celmath, use sincos in rotation matrix functions
1 parent 9ac8339 commit aaf22f1

File tree

11 files changed

+91
-39
lines changed

11 files changed

+91
-39
lines changed

src/celengine/axisarrow.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <algorithm>
1212
#include <vector>
1313
#include <celcompat/numbers.h>
14+
#include <celmath/geomutil.h>
1415
#include <celmath/mathlib.h>
1516
#include <celmath/vecgl.h>
1617
#include <celrender/linerenderer.h>
@@ -308,19 +309,19 @@ AxesReferenceMark::render(Renderer* renderer,
308309
Eigen::Matrix4f labelTransformMatrix = labelTransform.matrix();
309310

310311
// x-axis
311-
Eigen::Matrix4f xModelView = modelView * celmath::rotate(Eigen::AngleAxisf(90.0_deg, Eigen::Vector3f::UnitY()));
312+
Eigen::Matrix4f xModelView = modelView * celmath::YRot90Matrix<float>;
312313
glVertexAttrib4f(CelestiaGLProgram::ColorAttributeIndex, 1.0f, 0.0f, 0.0f, opacity);
313314
prog->setMVPMatrices(projection, xModelView);
314315
GetArrowVAO().draw();
315316

316317
// y-axis
317-
Eigen::Matrix4f yModelView = modelView * celmath::rotate(Eigen::AngleAxisf(180.0_deg, Eigen::Vector3f::UnitY()));
318+
Eigen::Matrix4f yModelView = modelView * celmath::YRot180Matrix<float>;
318319
glVertexAttrib4f(CelestiaGLProgram::ColorAttributeIndex, 0.0f, 1.0f, 0.0f, opacity);
319320
prog->setMVPMatrices(projection, yModelView);
320321
GetArrowVAO().draw();
321322

322323
// z-axis
323-
Eigen::Matrix4f zModelView = modelView * celmath::rotate(Eigen::AngleAxisf(-90.0_deg, Eigen::Vector3f::UnitX()));
324+
Eigen::Matrix4f zModelView = modelView * celmath::XRot270Matrix<float>;
324325
glVertexAttrib4f(CelestiaGLProgram::ColorAttributeIndex, 0.0f, 0.0f, 1.0f, opacity);
325326
prog->setMVPMatrices(projection, zModelView);
326327
GetArrowVAO().draw();
@@ -455,7 +456,7 @@ BodyAxisArrows::BodyAxisArrows(const Body& _body) :
455456
Eigen::Quaterniond
456457
BodyAxisArrows::getOrientation(double tdb) const
457458
{
458-
return (Eigen::Quaterniond(Eigen::AngleAxis<double>(celestia::numbers::pi, Eigen::Vector3d::UnitY())) * body.getEclipticToBodyFixed(tdb)).conjugate();
459+
return (celmath::YRot180<double> * body.getEclipticToBodyFixed(tdb)).conjugate();
459460
}
460461

461462

src/celengine/observer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ void Observer::setLocationFilter(uint64_t _locationFilter)
538538

539539
void Observer::reverseOrientation()
540540
{
541-
setOrientation(getOrientation() * Quaterniond(AngleAxisd(celestia::numbers::pi, Vector3d::UnitY())));
541+
setOrientation(getOrientation() * celmath::YRot180<double>);
542542
reverseFlag = !reverseFlag;
543543
}
544544

src/celengine/planetgrid.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <Eigen/Geometry>
1515
#include <fmt/format.h>
1616
#include <celcompat/numbers.h>
17+
#include <celmath/geomutil.h>
1718
#include <celmath/intersect.h>
1819
#include <celmath/vecgl.h>
1920
#include <celrender/linerenderer.h>
@@ -99,8 +100,7 @@ PlanetographicGrid::render(Renderer* renderer,
99100
InitializeGeometry(*renderer);
100101

101102
// Compatibility
102-
Eigen::Quaterniond q(Eigen::AngleAxis(celestia::numbers::pi, Eigen::Vector3d::UnitY()));
103-
q *= body.getEclipticToBodyFixed(tdb);
103+
Eigen::Quaterniond q = celmath::YRot180<double> * body.getEclipticToBodyFixed(tdb);
104104
Eigen::Quaternionf qf = q.cast<float>();
105105

106106
// The grid can't be rendered exactly on the planet sphere, or

src/celengine/skygrid.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,6 @@ SkyGrid::render(Renderer& renderer,
335335
int windowWidth,
336336
int windowHeight)
337337
{
338-
// 90 degree rotation about the x-axis used to transform coordinates
339-
// to Celestia's system.
340-
Quaterniond xrot90 = XRotation(-celestia::numbers::pi / 2.0);
341-
342338
auto vfov = static_cast<double>(renderer.getProjectionMode()->getFOV(observer.getZoom()));
343339
double viewAspectRatio = static_cast<double>(windowWidth) / static_cast<double>(windowHeight);
344340

@@ -369,7 +365,13 @@ SkyGrid::render(Renderer& renderer,
369365
Vector3d c3( w, h, -1.0);
370366

371367
Quaterniond cameraOrientation = renderer.getCameraOrientation();
372-
Matrix3d r = (cameraOrientation * xrot90 * m_orientation.conjugate() * xrot90.conjugate()).toRotationMatrix().transpose();
368+
369+
// 90 degree rotation about the x-axis used to transform coordinates
370+
// to Celestia's system.
371+
Matrix3d r = (cameraOrientation *
372+
celmath::XRot90Conjugate<double> *
373+
m_orientation.conjugate() *
374+
celmath::XRot90<double>).toRotationMatrix().transpose();
373375

374376
// Transform the frustum corners by the camera and grid
375377
// rotations.
@@ -481,13 +483,15 @@ SkyGrid::render(Renderer& renderer,
481483
int endDec = (int) std::floor(DEG_MIN_SEC_TOTAL * (maxDec * celestia::numbers::inv_pi) / (double) decIncrement) * decIncrement;
482484

483485
// Get the orientation at single precision
484-
Quaterniond q = xrot90 * m_orientation * xrot90.conjugate();
486+
Quaterniond q = celmath::XRot90Conjugate<double> * m_orientation * celmath::XRot90<double>;
485487
Quaternionf orientationf = q.cast<float>();
486488

487489
// Radius of sphere is arbitrary, with the constraint that it shouldn't
488490
// intersect the near or far plane of the view frustum.
489491
Matrix4f m = renderer.getModelViewMatrix() *
490-
celmath::rotate((xrot90 * m_orientation.conjugate() * xrot90.conjugate()).cast<float>()) *
492+
celmath::rotate((celmath::XRot90Conjugate<double> *
493+
m_orientation.conjugate() *
494+
celmath::XRot90<double>).cast<float>()) *
491495
celmath::scale(1000.0f);
492496
Matrices matrices = {&renderer.getProjectionMatrix(), &m};
493497

src/celephem/customorbit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,8 +1366,8 @@ class PhobosOrbit : public CachingOrbit
13661366

13671367
// Orientation of the orbital plane with respect to the Laplacian plane
13681368
Eigen::Matrix3d Rorbit = (celmath::YRotation(node) *
1369-
celmath::XRotation(celmath::degToRad(i)) *
1370-
celmath::YRotation(w)).toRotationMatrix();
1369+
celmath::XRotation(celmath::degToRad(i)) *
1370+
celmath::YRotation(w)).toRotationMatrix();
13711371

13721372
// Rotate to the Earth's equatorial plane
13731373
constexpr double N = celmath::degToRad(refplane_RA);

src/celephem/customrotation.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class IAURotationModel : public CachingRotationModel
9090
double inclination = 90.0 - poleDec;
9191

9292
if (flipped)
93-
return celmath::XRotation(celestia::numbers::pi) *
93+
return celmath::XRot180<double> *
9494
celmath::XRotation(celmath::degToRad(-inclination)) *
9595
celmath::YRotation(celmath::degToRad(-node));
9696
else
@@ -176,9 +176,7 @@ class EarthRotationModel : public CachingRotationModel
176176
Eigen::Quaterniond q = celmath::XRotation(obliquity) * celmath::ZRotation(-precession) * eclRotation.conjugate();
177177

178178
// convert to Celestia's coordinate system
179-
return celmath::XRotation(celestia::numbers::pi / 2.0) *
180-
q *
181-
celmath::XRotation(-celestia::numbers::pi / 2.0);
179+
return celmath::XRot90<double> * q * celmath::XRot90Conjugate<double>;
182180
}
183181

184182
double getPeriod() const override

src/celephem/samporient.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,12 @@ class SampledOrientation : public RotationModel
111111
void
112112
SampledOrientation::addSample(double t, const Eigen::Quaternionf& q)
113113
{
114-
// 90 degree rotation about x-axis to convert orientation to Celestia's
115-
// coordinate system.
116-
static const Eigen::Quaternionf coordSysCorrection = celmath::XRotation((float) (celestia::numbers::pi / 2.0));
117-
118114
// TODO: add a check for out of sequence samples
119115
OrientationSample& samp = samples.emplace_back();
120116
samp.t = t;
121-
samp.q = q * coordSysCorrection;
117+
// 90 degree rotation about x-axis to convert orientation to Celestia's
118+
// coordinate system.
119+
samp.q = q * celmath::XRot90<float>;
122120
}
123121

124122

src/celephem/spicerotation.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,10 @@ SpiceRotation::computeSpin(double jd) const
182182
Eigen::Quaterniond q = Eigen::Quaterniond(Eigen::Map<Eigen::Matrix3d>(matrixData)).conjugate();
183183

184184
// Transform into Celestia's coordinate system
185-
static const Eigen::Quaterniond Rx90 = celmath::XRotation(celestia::numbers::pi / 2.0);
186-
static const Eigen::Quaterniond Ry180 = celmath::YRotation(celestia::numbers::pi);
187-
return Ry180 * Rx90.conjugate() * q.conjugate() * Rx90;
185+
return celmath::YRot180<double> *
186+
celmath::XRot90Conjugate<double> *
187+
q.conjugate() *
188+
celmath::XRot90<double>;
188189
}
189190
}
190191

src/celestia/gtk/dialog-eclipse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ static gint eclipseGoto(GtkButton*, EclipseData* ed)
332332

333333
double distance = target.radius() * 4.0;
334334
sim->gotoLocation(UniversalCoord::Zero().offsetKm(Vector3d::UnitX() * distance),
335-
(YRotation(-celestia::numbers::pi / 2) * XRotation(-celestia::numbers::pi / 2)), 2.5);
335+
(celmath::YRot90Conjugate<double> * celmath::XRot90Conjugate<double>), 2.5);
336336

337337
return TRUE;
338338
}

src/celestia/win32/wineclipses.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ BOOL APIENTRY EclipseFinderProc(HWND hDlg,
390390

391391
double distance = target.radius() * 4.0;
392392
sim->gotoLocation(UniversalCoord::Zero().offsetKm(Vector3d::UnitX() * distance),
393-
YRotation(-celestia::numbers::pi / 2) * XRotation(-celestia::numbers::pi / 2),
393+
celmath::YRot90Conjugate<double> * celmath::XRot90Conjugate<double>,
394394
2.5);
395395
}
396396
break;

0 commit comments

Comments
 (0)