FERS 0.1.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
16{
17 /**
18 * @brief Normalizes an angle to the signed principal range.
19 *
20 * Wraps the input angle modulo 2π and returns the equivalent signed angle in
21 * the range (-π, π]. This is useful for angular differences where the shortest
22 * signed rotation is required.
23 *
24 * @param angle The input angle in radians.
25 * @return The equivalent wrapped angle in radians.
26 */
28 {
29 RealType wrapped = std::remainder(angle, 2 * PI);
30
31 // Preserve the previous operator- behavior:
32 // old code mapped -PI to PI, giving the range (-PI, PI].
33 if (wrapped <= -PI)
34 {
35 wrapped += 2 * PI;
36 }
37
38 return wrapped;
39 }
40}
41
42namespace math
43{
45 x(svec.length * std::cos(svec.azimuth) * std::cos(svec.elevation)),
46 y(svec.length * std::sin(svec.azimuth) * std::cos(svec.elevation)), z(svec.length * std::sin(svec.elevation))
47 {
48 }
49
50 Vec3& Vec3::operator+=(const Vec3& b) noexcept
51 {
52 x += b.x;
53 y += b.y;
54 z += b.z;
55 return *this;
56 }
57
58 Vec3& Vec3::operator-=(const Vec3& b) noexcept
59 {
60 x -= b.x;
61 y -= b.y;
62 z -= b.z;
63 return *this;
64 }
65
66 Vec3& Vec3::operator*=(const Vec3& b) noexcept
67 {
68 x *= b.x;
69 y *= b.y;
70 z *= b.z;
71 return *this;
72 }
73
74 Vec3& Vec3::operator*=(const Matrix3& m) noexcept
75 {
76 const RealType* mat = m.getData();
77 const Vec3 v(x, y, z);
78 x = mat[0] * v.x + mat[1] * v.y + mat[2] * v.z;
79 y = mat[3] * v.x + mat[4] * v.y + mat[5] * v.z;
80 z = mat[6] * v.x + mat[7] * v.y + mat[8] * v.z;
81 return *this;
82 }
83
85 {
86 x *= b;
87 y *= b;
88 z *= b;
89 return *this;
90 }
91
93 {
94 x /= b;
95 y /= b;
96 z /= b;
97 return *this;
98 }
99
101 length(vec.length()), azimuth(std::atan2(vec.y, vec.x)), elevation(std::asin(vec.z / length))
102 {
103 }
104
106 {
107 length *= b;
108 return *this;
109 }
110
112 {
113 length /= b;
114 return *this;
115 }
116
117 SVec3 operator+(const SVec3& a, const SVec3& b) noexcept
118 {
119 const RealType new_azimuth = wrapSignedAngle(a.azimuth + b.azimuth);
120 const RealType new_elevation = std::fmod(a.elevation + b.elevation, PI);
121
122 return {a.length + b.length, new_azimuth, new_elevation};
123 }
124
125 SVec3 operator-(const SVec3& a, const SVec3& b) noexcept
126 {
127 const RealType new_azimuth = wrapSignedAngle(a.azimuth - b.azimuth);
128
129 // Elevation difference is typically simpler and doesn't need wrapping
130 // unless 180 degree elevation sweeps are expected.
131 const RealType new_elevation = std::fmod(a.elevation - b.elevation, PI);
132
133 return {a.length - b.length, new_azimuth, new_elevation};
134 }
135}
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 length() const noexcept
Calculates the length (magnitude) of the vector.
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
math::Vec3 max
RealType b
RealType a