13
#include "simulation.h"
18
#include <celutil/strnatcmp.h>
24
Simulation::Simulation(Universe* _universe) :
27
activeObserver = new Observer();
28
observers.push_back(activeObserver);
32
Simulation::~Simulation()
34
for (const auto observer : observers)
39
static const Star* getSun(const Body* body)
41
const PlanetarySystem* system = body->getSystem();
42
return system ? system->getStar() : nullptr;
46
void Simulation::render(Renderer& renderer)
48
renderer.render(*activeObserver,
55
void Simulation::render(Renderer& renderer, Observer& observer)
57
renderer.render(observer,
64
Universe* Simulation::getUniverse() const
71
double Simulation::getTime() const
73
return activeObserver->getTime();
77
void Simulation::setTime(double jd)
81
for (const auto observer : observers)
83
observer->setTime(jd);
88
activeObserver->setTime(jd);
94
double Simulation::getRealTime() const
100
double Simulation::getArrivalTime() const
102
return activeObserver->getArrivalTime();
107
void Simulation::update(double dt)
111
for (const auto observer : observers)
113
observer->update(dt, timeScale);
117
closestSolarSystem = std::nullopt;
121
Selection Simulation::getSelection() const
127
void Simulation::setSelection(const Selection& sel)
133
Selection Simulation::getTrackedObject() const
135
return activeObserver->getTrackedObject();
139
void Simulation::setTrackedObject(const Selection& sel)
141
activeObserver->setTrackedObject(sel);
145
Selection Simulation::pickObject(const Eigen::Vector3f& pickRay,
146
std::uint64_t renderFlags,
149
return universe->pick(activeObserver->getPosition(),
150
activeObserver->getOrientationf().conjugate() * pickRay,
151
activeObserver->getTime(),
157
void Simulation::reverseObserverOrientation()
159
activeObserver->reverseOrientation();
163
Observer& Simulation::getObserver()
165
return *activeObserver;
169
const Observer& Simulation::getObserver() const
171
return *activeObserver;
175
Observer* Simulation::duplicateActiveObserver()
177
return observers.emplace_back(new Observer(*getActiveObserver()));
181
void Simulation::removeObserver(Observer* o)
183
auto iter = std::find(observers.begin(), observers.end(), o);
184
if (iter != observers.end())
185
observers.erase(iter);
189
Observer* Simulation::getActiveObserver()
191
return activeObserver;
195
const Observer* Simulation::getActiveObserver() const
197
return activeObserver;
201
void Simulation::setActiveObserver(Observer* o)
203
auto iter = std::find(observers.begin(), observers.end(), o);
204
if (iter != observers.end())
209
void Simulation::setObserverPosition(const UniversalCoord& pos)
211
activeObserver->setPosition(pos);
214
void Simulation::setObserverOrientation(const Eigen::Quaternionf& orientation)
216
activeObserver->setOrientation(orientation);
220
Observer::ObserverMode Simulation::getObserverMode() const
222
return activeObserver->getMode();
225
void Simulation::setObserverMode(Observer::ObserverMode mode)
227
activeObserver->setMode(mode);
230
void Simulation::setFrame(ObserverFrame::CoordinateSystem coordSys,
231
const Selection& refObject,
232
const Selection& targetObject)
234
activeObserver->setFrame(coordSys, refObject, targetObject);
237
void Simulation::setFrame(ObserverFrame::CoordinateSystem coordSys,
238
const Selection& refObject)
240
activeObserver->setFrame(coordSys, refObject);
243
const ObserverFrame::SharedConstPtr& Simulation::getFrame() const
245
return activeObserver->getFrame();
249
void Simulation::rotate(const Eigen::Quaternionf& q)
251
activeObserver->rotate(q);
256
void Simulation::orbit(const Eigen::Quaternionf& q)
258
activeObserver->orbit(selection, q);
263
bool Simulation::orbit(const Eigen::Vector3f& from, const Eigen::Vector3f& to)
265
return activeObserver->orbit(selection, from, to);
270
void Simulation::changeOrbitDistance(float d)
272
activeObserver->changeOrbitDistance(selection, d);
275
void Simulation::scaleOrbitDistance(float scale, const std::optional<Eigen::Vector3f>& focus)
277
activeObserver->scaleOrbitDistance(selection, scale, focus);
280
void Simulation::setTargetSpeed(float s)
282
activeObserver->setTargetSpeed(s);
285
float Simulation::getTargetSpeed() const
287
return activeObserver->getTargetSpeed();
290
void Simulation::gotoSelection(double gotoTime,
291
const Eigen::Vector3f& up,
292
ObserverFrame::CoordinateSystem upFrame)
294
if (selection.getType() == SelectionType::Location)
296
activeObserver->gotoSelectionGC(selection,
302
activeObserver->gotoSelection(selection, gotoTime, up, upFrame);
306
void Simulation::gotoSelection(double gotoTime,
308
const Eigen::Vector3f& up,
309
ObserverFrame::CoordinateSystem upCoordSys)
311
activeObserver->gotoSelection(selection, gotoTime, distance, up, upCoordSys);
314
void Simulation::gotoSelectionLongLat(double gotoTime,
318
const Eigen::Vector3f& up)
320
activeObserver->gotoSelectionLongLat(selection, gotoTime, distance,
321
longitude, latitude, up);
325
void Simulation::gotoLocation(const UniversalCoord& position,
326
const Eigen::Quaterniond& orientation,
329
activeObserver->gotoLocation(position, orientation, duration);
333
void Simulation::getSelectionLongLat(double& distance,
337
activeObserver->getSelectionLongLat(selection, distance, longitude, latitude);
341
void Simulation::gotoSurface(double duration)
343
activeObserver->gotoSurface(selection, duration);
347
void Simulation::cancelMotion()
349
activeObserver->cancelMotion();
352
void Simulation::centerSelection(double centerTime)
354
activeObserver->centerSelection(selection, centerTime);
357
void Simulation::centerSelectionCO(double centerTime)
359
activeObserver->centerSelectionCO(selection, centerTime);
362
void Simulation::follow()
364
activeObserver->follow(selection);
367
void Simulation::geosynchronousFollow()
369
activeObserver->geosynchronousFollow(selection);
372
void Simulation::phaseLock()
374
activeObserver->phaseLock(selection);
377
void Simulation::chase()
379
activeObserver->chase(selection);
387
void Simulation::selectPlanet(int index)
391
if (selection.getType() == SelectionType::Body)
393
PlanetarySystem* system = selection.body()->getSystem();
394
if (system != nullptr)
395
setSelection(system->getStar());
400
const Star* star = nullptr;
401
if (selection.getType() == SelectionType::Star)
402
star = selection.star();
403
else if (selection.getType() == SelectionType::Body)
404
star = getSun(selection.body());
406
SolarSystem* solarSystem = nullptr;
408
solarSystem = universe->getSolarSystem(star);
410
solarSystem = getNearestSolarSystem();
412
if (solarSystem != nullptr &&
413
index < solarSystem->getPlanets()->getSystemSize())
415
setSelection(Selection(solarSystem->getPlanets()->getBody(index)));
427
Selection Simulation::findObject(std::string_view s, bool i18n) const
430
std::size_t nPathEntries = 0;
432
if (!selection.empty())
433
path[nPathEntries++] = selection;
435
if (auto nearestSolarSystem = getNearestSolarSystem(); nearestSolarSystem != nullptr)
436
path[nPathEntries++] = Selection(nearestSolarSystem->getStar());
438
return universe->find(s, {path, nPathEntries}, i18n);
445
Selection Simulation::findObjectFromPath(std::string_view s, bool i18n) const
448
std::size_t nPathEntries = 0;
450
if (!selection.empty())
451
path[nPathEntries++] = selection;
453
if (auto nearestSolarSystem = getNearestSolarSystem(); nearestSolarSystem != nullptr)
454
path[nPathEntries++] = Selection(nearestSolarSystem->getStar());
456
return universe->findPath(s, {path, nPathEntries}, i18n);
460
void Simulation::getObjectCompletion(std::vector<std::string>& completion,
462
bool withLocations) const
465
std::size_t nPathEntries = 0;
467
if (!selection.empty())
469
if (selection.getType() == SelectionType::Location)
471
path[nPathEntries++] = Selection(selection.location()->getParentBody());
475
path[nPathEntries++] = selection;
479
if (auto nearestSolarSystem = getNearestSolarSystem();
480
nearestSolarSystem != nullptr && nearestSolarSystem != universe->getSolarSystem(selection))
482
path[nPathEntries++] = Selection(nearestSolarSystem->getStar());
485
universe->getCompletionPath(completion, s, {path, nPathEntries}, withLocations);
487
std::sort(completion.begin(), completion.end(),
488
[](const std::string &s1, const std::string &s2) { return strnatcmp(s1, s2) < 0; });
492
double Simulation::getTimeScale() const
494
return pauseState?storedTimeScale:timeScale;
497
void Simulation::setTimeScale(double _timeScale)
501
storedTimeScale = _timeScale;
505
timeScale = _timeScale;
509
bool Simulation::getSyncTime() const
514
void Simulation::setSyncTime(bool sync)
519
bool Simulation::getPauseState() const
524
void Simulation::setPauseState(bool state)
526
if (pauseState == state) return;
531
storedTimeScale = timeScale;
536
timeScale = storedTimeScale;
541
void Simulation::synchronizeTime()
543
for (const auto observer : observers)
545
observer->setTime(activeObserver->getTime());
550
float Simulation::getFaintestVisible() const
552
return faintestVisible;
556
void Simulation::setFaintestVisible(float magnitude)
558
faintestVisible = magnitude;
562
SolarSystem* Simulation::getNearestSolarSystem() const
564
if (!closestSolarSystem.has_value())
565
closestSolarSystem = universe->getNearestSolarSystem(activeObserver->getPosition());
566
return *closestSolarSystem;