FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
geometry_ops.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright (c) 2006-2008 Marc Brooker and Michael Inggs
4// Copyright (c) 2008-present FERS Contributors (see AUTHORS.md).
5//
6// See the GNU GPLv2 LICENSE file in the FERS project root for more information.
7
8/**
9 * @file geometry_ops.cpp
10 * @brief Implementation of geometry classes.
11 */
12
13#include "geometry_ops.h"
14
15namespace math
16{
17 Vec3::Vec3(const SVec3& svec) noexcept :
18 x(svec.length * std::cos(svec.azimuth) * std::cos(svec.elevation)),
19 y(svec.length * std::sin(svec.azimuth) * std::cos(svec.elevation)), z(svec.length * std::sin(svec.elevation))
20 {
21 }
22
23 Vec3& Vec3::operator+=(const Vec3& b) noexcept
24 {
25 x += b.x;
26 y += b.y;
27 z += b.z;
28 return *this;
29 }
30
31 Vec3& Vec3::operator-=(const Vec3& b) noexcept
32 {
33 x -= b.x;
34 y -= b.y;
35 z -= b.z;
36 return *this;
37 }
38
39 Vec3& Vec3::operator*=(const Vec3& b) noexcept
40 {
41 x *= b.x;
42 y *= b.y;
43 z *= b.z;
44 return *this;
45 }
46
47 Vec3& Vec3::operator*=(const Matrix3& m) noexcept
48 {
49 const RealType* mat = m.getData();
50 const Vec3 v(x, y, z);
51 x = mat[0] * v.x + mat[1] * v.y + mat[2] * v.z;
52 y = mat[3] * v.x + mat[4] * v.y + mat[5] * v.z;
53 z = mat[6] * v.x + mat[7] * v.y + mat[8] * v.z;
54 return *this;
55 }
56
57 Vec3& Vec3::operator*=(const RealType b) noexcept
58 {
59 x *= b;
60 y *= b;
61 z *= b;
62 return *this;
63 }
64
65 Vec3& Vec3::operator/=(const RealType b) noexcept
66 {
67 x /= b;
68 y /= b;
69 z /= b;
70 return *this;
71 }
72
73 SVec3::SVec3(const Vec3& vec) noexcept : length(vec.length())
74 {
75 elevation = std::asin(vec.z / length);
76 azimuth = std::atan2(vec.y, vec.x);
77 }
78
79 SVec3& SVec3::operator*=(const RealType b) noexcept
80 {
81 length *= b;
82 return *this;
83 }
84
85 SVec3& SVec3::operator/=(const RealType b) noexcept
86 {
87 length /= b;
88 return *this;
89 }
90
91 SVec3 operator+(const SVec3& a, const SVec3& b) noexcept
92 {
93 RealType new_azimuth = fmod(a.azimuth + b.azimuth, 2 * PI);
94 if (new_azimuth < 0)
95 {
96 new_azimuth += 2 * PI;
97 }
98 RealType new_elevation = fmod(a.elevation + b.elevation, PI);
99 return {a.length + b.length, new_azimuth, new_elevation};
100 }
101
102 SVec3 operator-(const SVec3& a, const SVec3& b) noexcept
103 {
104 RealType new_azimuth = a.azimuth - b.azimuth;
105
106 // Wrap the azimuth to the range [-PI, PI] to find the shortest angle
107 // TODO: Has to be a better way to do this...
108 while (new_azimuth <= -PI)
109 new_azimuth += 2 * PI;
110 while (new_azimuth > PI)
111 new_azimuth -= 2 * PI;
112
113 // Elevation difference is typically simpler and doesn't need wrapping
114 // unless 180 degree elevation sweeps are expected.
115 RealType new_elevation = fmod(a.elevation - b.elevation, PI);
116 return {a.length - b.length, new_azimuth, new_elevation};
117 }
118}
A class representing a 3x3 matrix.
A class representing a vector in spherical coordinates.
SVec3() noexcept=default
SVec3 & operator/=(RealType b) noexcept
Scalar division assignment for SVec3.
SVec3 & operator*=(RealType b) noexcept
Scalar multiplication assignment for SVec3.
A class representing a vector in rectangular coordinates.
RealType x
The x component of the vector.
RealType z
The z component of the vector.
Vec3 & operator-=(const Vec3 &b) noexcept
Subtraction assignment operator for Vec3.
RealType y
The y component of the vector.
Vec3 & operator+=(const Vec3 &b) noexcept
Addition assignment operator for Vec3.
Vec3 & operator/=(RealType b) noexcept
Scalar division assignment for Vec3.
Vec3() noexcept=default
Vec3 & operator*=(const Vec3 &b) noexcept
Multiplication assignment operator for Vec3.
double RealType
Type for real numbers.
Definition config.h:27
constexpr RealType PI
Mathematical constant π (pi).
Definition config.h:43
Classes and operations for 3D geometry.
Definition coord.h:18
Coord operator+(const Coord &a, const Coord &b) noexcept
Adds two Coord objects' positions and copies the time.
Definition coord.h:53
Coord operator-(const Coord &a, const Coord &b) noexcept
Subtracts two Coord objects' positions and copies the time.
Definition coord.h:55