FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
kml_generator_utils.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright (c) 2026-present FERS Contributors (see AUTHORS.md).
4//
5// See the GNU GPLv2 LICENSE file in the FERS project root for more information.
6
7/**
8 * @file kml_generator_utils.h
9 * @brief Utility definitions and functions for generating KML files from simulation scenarios.
10 *
11 * This file provides the internal mechanisms required to export FERS scenarios
12 * to KML format for visualization in Google Earth or similar tools. It defines
13 * functions for formatting coordinates, rendering styling, creating models,
14 * and assembling the top-level document structure.
15 */
16
17#pragma once
18
19#include <functional>
20#include <optional>
21#include <ostream>
22#include <string>
23#include <vector>
24
25#include "core/parameters.h"
26#include "math/geometry_ops.h"
27
28namespace core
29{
30 class World;
31}
32
33namespace radar
34{
35 class Platform;
36 class Radar;
37 class Object;
38}
39
40namespace antenna
41{
42 class Gaussian;
43 class Parabolic;
44 class SquareHorn;
45}
46
48{
49 /**
50 * @brief Callback signature used for converting math coordinates to lat/lon/altitude.
51 */
52 using ConverterFunc = std::function<void(const math::Vec3&, double&, double&, double&)>;
53
54 /**
55 * @struct KmlContext
56 * @brief Context data required during KML generation.
57 */
59 {
60 params::Parameters parameters; ///< A copy of the global simulation parameters.
61 ConverterFunc converter; ///< Function used to translate simulation Cartesian space into geographic coords.
62 };
63
64 /**
65 * @brief Calculates a normalized sinc-based antenna gain mapping.
66 * @param theta The angle to evaluate.
67 * @param alpha Primary dimensional scale factor.
68 * @param beta Secondary dimensional scale factor.
69 * @param gamma Falloff rate.
70 * @return The calculated gain.
71 */
72 double sincAntennaGain(double theta, double alpha, double beta, double gamma);
73
74 /**
75 * @brief Numerically determines the 3dB drop angle for a parameterized generic antenna.
76 * @param alpha Primary dimensional scale factor.
77 * @param beta Secondary dimensional scale factor.
78 * @param gamma Falloff rate.
79 * @return The calculated 3dB drop angle in radians.
80 */
81 double find3DbDropAngle(double alpha, double beta, double gamma);
82
83 /**
84 * @brief Calculates the 3dB drop angle for a Gaussian antenna.
85 * @param gaussianAnt Pointer to the Gaussian antenna.
86 * @return The 3dB drop angle in degrees.
87 */
88 double findGaussian3DbDropAngle(const antenna::Gaussian* gaussianAnt);
89
90 /**
91 * @brief Calculates the 3dB drop angle for a Parabolic antenna.
92 * @param parabolicAnt Pointer to the Parabolic antenna.
93 * @param wavelength The operating wavelength.
94 * @return The 3dB drop angle in degrees.
95 */
96 double findParabolic3DbDropAngle(const antenna::Parabolic* parabolicAnt, double wavelength);
97
98 /**
99 * @brief Calculates the 3dB drop angle for a Square Horn antenna.
100 * @param squarehornAnt Pointer to the Square Horn antenna.
101 * @param wavelength The operating wavelength.
102 * @return The 3dB drop angle in degrees.
103 */
104 double findSquareHorn3DbDropAngle(const antenna::SquareHorn* squarehornAnt, double wavelength);
105
106 /**
107 * @brief Formats coordinates into a comma-separated string suitable for KML `<coordinates>`.
108 * @param lon Longitude in degrees.
109 * @param lat Latitude in degrees.
110 * @param alt Altitude in meters.
111 * @return Formatted coordinate string containing "lon,lat,alt".
112 */
113 std::string formatCoordinates(double lon, double lat, double alt);
114
115 /**
116 * @brief Calculates a destination coordinate given a starting position, bearing, and distance.
117 * @param startLatitude Starting latitude in degrees.
118 * @param startLongitude Starting longitude in degrees.
119 * @param angle Bearing angle in degrees.
120 * @param distance Distance to travel in meters.
121 * @param destLatitude Output destination latitude in degrees.
122 * @param destLongitude Output destination longitude in degrees.
123 */
124 void calculateDestinationCoordinate(double startLatitude, double startLongitude, double angle, double distance,
125 double& destLatitude, double& destLongitude);
126
127 /**
128 * @brief Generates a collection of points tracing a circle around a center coordinate.
129 * @param lat Center latitude in degrees.
130 * @param lon Center longitude in degrees.
131 * @param radius_km Circle radius in kilometers.
132 * @return A vector of latitude-longitude pairs defining the circle.
133 */
134 std::vector<std::pair<double, double>> generateCircleCoordinates(double lat, double lon, double radius_km);
135
136 /**
137 * @brief Writes the standard KML preamble and style definitions to the output stream.
138 * @param out The stream to write to.
139 * @param ctx The current KML generation context.
140 */
141 void writeKmlHeaderAndStyles(std::ostream& out, const KmlContext& ctx);
142
143 /**
144 * @brief Writes a KML `<Point>` placemark to the output stream.
145 * @param out The stream to write to.
146 * @param indent The indentation string.
147 * @param name The name of the point.
148 * @param styleUrl The ID of the style to apply.
149 * @param coordinates The pre-formatted coordinate string.
150 * @param objectAltitude The absolute altitude of the point.
151 * @param referenceAltitude The reference altitude (e.g. ground level) for relative extrusions.
152 */
153 void writePoint(std::ostream& out, const std::string& indent, const std::string& name, const std::string& styleUrl,
154 const std::string& coordinates, double objectAltitude, double referenceAltitude);
155
156 /**
157 * @brief Writes a visual cone/beam line representing the antenna look direction.
158 * @param out The stream to write to.
159 * @param indent The indentation string.
160 * @param name The name of the beam projection line.
161 * @param style The ID of the line style to apply.
162 * @param startCoords The pre-formatted origin coordinate string.
163 * @param endCoords The pre-formatted destination coordinate string.
164 */
165 void writeAntennaBeamLine(std::ostream& out, const std::string& indent, const std::string& name,
166 const std::string& style, const std::string& startCoords, const std::string& endCoords);
167
168 /**
169 * @brief Determines the proper KML style definition ID to use based on the platform's attached objects.
170 * @param objects A collection of objects attached to the platform.
171 * @return The style URL string (e.g., "#RadarPlatformStyle").
172 */
173 std::string getPlacemarkStyleForPlatform(const std::vector<const radar::Object*>& objects);
174
175 /**
176 * @brief Identifies the primary radar object within a platform for styling and direction tracking.
177 * @param objects A collection of objects attached to the platform.
178 * @return Pointer to the primary radar, or nullptr if none exists.
179 */
180 const radar::Radar* getPrimaryRadar(const std::vector<const radar::Object*>& objects);
181
182 /**
183 * @brief Renders the visual representation for an isotropic antenna into the output stream.
184 * @param out The stream to write to.
185 * @param position The position of the antenna in simulation space.
186 * @param ctx The current KML context.
187 * @param indent The indentation string.
188 */
189 void generateIsotropicAntennaKml(std::ostream& out, const math::Vec3& position, const KmlContext& ctx,
190 const std::string& indent);
191
192 /**
193 * @brief Renders the visual pointing representation for a directional (beam) antenna.
194 * @param out The stream to write to.
195 * @param platform The platform the antenna is mounted on.
196 * @param ctx The current KML context.
197 * @param angle3DbDropDeg An optional computed angular drop-off to dictate the beam width rendering.
198 * @param indent The indentation string.
199 */
200 void generateDirectionalAntennaKml(std::ostream& out, const radar::Platform* platform, const KmlContext& ctx,
201 const std::optional<double>& angle3DbDropDeg, const std::string& indent);
202
203 /**
204 * @brief Dispatch function that selects and generates the appropriate KML for a given radar's antenna.
205 * @param out The stream to write to.
206 * @param platform The platform containing the radar.
207 * @param radar The radar to visualize.
208 * @param ctx The current KML context.
209 * @param indent The indentation string.
210 */
211 void generateAntennaKml(std::ostream& out, const radar::Platform* platform, const radar::Radar* radar,
212 const KmlContext& ctx, const std::string& indent);
213
214 /**
215 * @brief Generates KML for a continuously moving dynamic platform path.
216 * @param out The stream to write to.
217 * @param platform The platform undergoing dynamic motion.
218 * @param styleUrl The style ID for the path line.
219 * @param refAlt The baseline reference altitude.
220 * @param ctx The current KML context.
221 * @param indent The indentation string.
222 */
223 void generateDynamicPathKml(std::ostream& out, const radar::Platform* platform, const std::string& styleUrl,
224 double refAlt, const KmlContext& ctx, const std::string& indent);
225
226 /**
227 * @brief Generates KML rendering start and end pushpins for a moving platform's track.
228 * @param out The stream to write to.
229 * @param platform The platform containing the path boundaries.
230 * @param refAlt The baseline reference altitude.
231 * @param ctx The current KML context.
232 * @param indent The indentation string.
233 */
234 void generateTrackEndpointsKml(std::ostream& out, const radar::Platform* platform, double refAlt,
235 const KmlContext& ctx, const std::string& indent);
236
237 /**
238 * @brief Generates a simple static placemark KML for a non-moving platform.
239 * @param out The stream to write to.
240 * @param platform The stationary platform.
241 * @param styleUrl The style ID to apply to the placemark.
242 * @param refAlt The baseline reference altitude.
243 * @param ctx The current KML context.
244 * @param indent The indentation string.
245 */
246 void generateStaticPlacemarkKml(std::ostream& out, const radar::Platform* platform, const std::string& styleUrl,
247 double refAlt, const KmlContext& ctx, const std::string& indent);
248
249 /**
250 * @brief Dispatches the generation of a platform's path representation (static vs dynamic).
251 * @param out The stream to write to.
252 * @param platform The platform whose path needs to be rendered.
253 * @param style The path tracing styling.
254 * @param refAlt The baseline reference altitude.
255 * @param ctx The current KML context.
256 * @param indent The indentation string.
257 */
258 void generatePlatformPathKml(std::ostream& out, const radar::Platform* platform, const std::string& style,
259 double refAlt, const KmlContext& ctx, const std::string& indent);
260
261 /**
262 * @brief Orchestrates full processing and rendering of an individual platform into the KML stream.
263 * @param out The stream to write to.
264 * @param platform The platform to process.
265 * @param objects Attached radar or target objects corresponding to the platform.
266 * @param ctx The current KML context.
267 * @param referenceAltitude Baseline reference altitude for the entire platform.
268 * @param indent The indentation string.
269 */
270 void processPlatform(std::ostream& out, const radar::Platform* platform,
271 const std::vector<const radar::Object*>& objects, const KmlContext& ctx,
272 double referenceAltitude, const std::string& indent);
273
274 /**
275 * @brief Master entry point designed to convert the comprehensive simulation world state into a valid KML document.
276 * @param out The output stream where the resulting KML markup goes.
277 * @param world The aggregated global simulation state.
278 * @param ctx The configured KML context containing simulation parameters and a coordinate converter.
279 */
280 void generateKmlToStream(std::ostream& out, const core::World& world, const KmlContext& ctx);
281}
Represents a Gaussian-shaped antenna gain pattern.
Represents a parabolic reflector antenna.
Represents a square horn antenna.
The World class manages the simulator environment.
Definition world.h:39
A class representing a vector in rectangular coordinates.
Represents a simulation platform with motion and rotation paths.
Definition platform.h:32
Represents a radar system on a platform.
Definition radar_obj.h:47
Classes and operations for 3D geometry.
void generateTrackEndpointsKml(std::ostream &out, const radar::Platform *platform, const double refAlt, const KmlContext &ctx, const std::string &indent)
Generates KML rendering start and end pushpins for a moving platform's track.
void writePoint(std::ostream &out, const std::string &indent, const std::string &name, const std::string &styleUrl, const std::string &coordinates, const double objectAltitude, const double referenceAltitude)
Writes a KML <Point> placemark to the output stream.
std::string formatCoordinates(const double lon, const double lat, const double alt)
Formats coordinates into a comma-separated string suitable for KML <coordinates>.
std::vector< std::pair< double, double > > generateCircleCoordinates(const double lat, const double lon, const double radius_km)
Generates a collection of points tracing a circle around a center coordinate.
double findSquareHorn3DbDropAngle(const antenna::SquareHorn *squarehornAnt, const double wavelength)
Calculates the 3dB drop angle for a Square Horn antenna.
void generateDirectionalAntennaKml(std::ostream &out, const radar::Platform *platform, const KmlContext &ctx, const std::optional< double > &angle3DbDropDeg, const std::string &indent)
Renders the visual pointing representation for a directional (beam) antenna.
double findParabolic3DbDropAngle(const antenna::Parabolic *parabolicAnt, const double wavelength)
Calculates the 3dB drop angle for a Parabolic antenna.
std::string getPlacemarkStyleForPlatform(const std::vector< const radar::Object * > &objects)
Determines the proper KML style definition ID to use based on the platform's attached objects.
double sincAntennaGain(const double theta, const double alpha, const double beta, const double gamma)
Calculates a normalized sinc-based antenna gain mapping.
double findGaussian3DbDropAngle(const antenna::Gaussian *gaussianAnt)
Calculates the 3dB drop angle for a Gaussian antenna.
void generateKmlToStream(std::ostream &out, const core::World &world, const KmlContext &ctx)
Master entry point designed to convert the comprehensive simulation world state into a valid KML docu...
double find3DbDropAngle(const double alpha, const double beta, const double gamma)
Numerically determines the 3dB drop angle for a parameterized generic antenna.
void generateDynamicPathKml(std::ostream &out, const radar::Platform *platform, const std::string &styleUrl, const double refAlt, const KmlContext &ctx, const std::string &indent)
Generates KML for a continuously moving dynamic platform path.
void generateAntennaKml(std::ostream &out, const radar::Platform *platform, const radar::Radar *radar, const KmlContext &ctx, const std::string &indent)
Dispatch function that selects and generates the appropriate KML for a given radar's antenna.
const radar::Radar * getPrimaryRadar(const std::vector< const radar::Object * > &objects)
Identifies the primary radar object within a platform for styling and direction tracking.
std::function< void(const math::Vec3 &, double &, double &, double &)> ConverterFunc
Callback signature used for converting math coordinates to lat/lon/altitude.
void processPlatform(std::ostream &out, const radar::Platform *platform, const std::vector< const radar::Object * > &objects, const KmlContext &ctx, const double referenceAltitude, const std::string &indent)
Orchestrates full processing and rendering of an individual platform into the KML stream.
void calculateDestinationCoordinate(const double startLatitude, const double startLongitude, const double angle, const double distance, double &destLatitude, double &destLongitude)
Calculates a destination coordinate given a starting position, bearing, and distance.
void writeKmlHeaderAndStyles(std::ostream &out, const KmlContext &ctx)
Writes the standard KML preamble and style definitions to the output stream.
void generateStaticPlacemarkKml(std::ostream &out, const radar::Platform *platform, const std::string &styleUrl, const double refAlt, const KmlContext &ctx, const std::string &indent)
Generates a simple static placemark KML for a non-moving platform.
void generatePlatformPathKml(std::ostream &out, const radar::Platform *platform, const std::string &style, const double refAlt, const KmlContext &ctx, const std::string &indent)
Dispatches the generation of a platform's path representation (static vs dynamic).
void writeAntennaBeamLine(std::ostream &out, const std::string &indent, const std::string &name, const std::string &style, const std::string &startCoords, const std::string &endCoords)
Writes a visual cone/beam line representing the antenna look direction.
void generateIsotropicAntennaKml(std::ostream &out, const math::Vec3 &position, const KmlContext &ctx, const std::string &indent)
Renders the visual representation for an isotropic antenna into the output stream.
Defines the Parameters struct and provides methods for managing simulation parameters.
Struct to hold simulation parameters.
Definition parameters.h:51
Context data required during KML generation.
params::Parameters parameters
A copy of the global simulation parameters.
ConverterFunc converter
Function used to translate simulation Cartesian space into geographic coords.