From 00aeaf792d56116eb035044d9d896d7e1469fba8 Mon Sep 17 00:00:00 2001 From: Andrew Tribick Date: Sun, 2 Jun 2024 21:30:02 +0200 Subject: [PATCH] Remove getName method on Selection, create getPath on Body/Location --- src/celengine/body.cpp | 27 ++++++++++++++- src/celengine/body.h | 2 ++ src/celengine/location.cpp | 24 +++++-------- src/celengine/location.h | 4 ++- src/celengine/parseobject.cpp | 4 +-- src/celengine/selection.cpp | 64 +++-------------------------------- src/celengine/selection.h | 11 +++--- src/celengine/stardb.h | 6 ++-- src/celestia/celestiacore.cpp | 18 +++++++--- src/celestia/url.cpp | 38 ++------------------- 10 files changed, 68 insertions(+), 130 deletions(-) diff --git a/src/celengine/body.cpp b/src/celengine/body.cpp index 816417e8618..dd7656f1037 100644 --- a/src/celengine/body.cpp +++ b/src/celengine/body.cpp @@ -19,6 +19,7 @@ #include "body.h" #include "atmosphere.h" #include "frame.h" +#include "stardb.h" #include "timeline.h" #include "timelinephase.h" #include "frametree.h" @@ -111,13 +112,37 @@ const vector& Body::getNames() const /*! Return the primary name for the body; if i18n, return the * localized name of the body. */ -string Body::getName(bool i18n) const +std::string +Body::getName(bool i18n) const { if (i18n && hasLocalizedName()) return localizedName; return names[0]; } +std::string +Body::getPath(const StarDatabase* starDB, char delimiter) const +{ + std::string name = names[0]; + const PlanetarySystem* planetarySystem = system; + while (planetarySystem != nullptr) + { + if (const Body* parent = planetarySystem->getPrimaryBody(); parent != nullptr) + { + name = parent->getName() + delimiter + name; + planetarySystem = parent->getSystem(); + } + else + { + if (const Star* parentStar = system->getStar(); parentStar != nullptr) + name = starDB->getStarName(*parentStar) + delimiter + name; + break; + } + } + + return name; +} + /*! Get the localized name for the body. If no localized name * has been set, the primary name is returned. */ diff --git a/src/celengine/body.h b/src/celengine/body.h index 67381142589..7a88416b21a 100644 --- a/src/celengine/body.h +++ b/src/celengine/body.h @@ -39,6 +39,7 @@ class Body; class FrameTree; class ReferenceMark; class Atmosphere; +class StarDatabase; class PlanetarySystem { @@ -199,6 +200,7 @@ class Body //NOSONAR PlanetarySystem* getSystem() const; const std::vector& getNames() const; std::string getName(bool i18n = false) const; + std::string getPath(const StarDatabase*, char delimiter = '/') const; std::string getLocalizedName() const; bool hasLocalizedName() const; void addAlias(const std::string& alias); diff --git a/src/celengine/location.cpp b/src/celengine/location.cpp index 31eefb7d000..0dd807fb592 100644 --- a/src/celengine/location.cpp +++ b/src/celengine/location.cpp @@ -21,7 +21,6 @@ using std::size_t; using std::strncmp; - namespace { @@ -30,14 +29,12 @@ namespace } // end unnamed namespace - const std::string& Location::getName(bool i18n) const { return i18n && !i18nName.empty() ? i18nName : name; } - void Location::setName(const std::string& _name) { @@ -47,6 +44,14 @@ Location::setName(const std::string& _name) i18nName = {}; } +std::string +Location::getPath(const StarDatabase* starDB, char delimiter) const +{ + if (parent) + return parent->getPath(starDB, delimiter) + delimiter + name; + else + return name; +} Eigen::Vector3f Location::getPosition() const @@ -54,63 +59,54 @@ Location::getPosition() const return position; } - void Location::setPosition(const Eigen::Vector3f& _position) { position = _position; } - float Location::getSize() const { return size; } - void Location::setSize(float _size) { size = _size; } - float Location::getImportance() const { return importance; } - void Location::setImportance(float _importance) { importance = _importance; } - const std::string& Location::getInfoURL() const { return infoURL; } - Location::FeatureType Location::getFeatureType() const { return featureType; } - void Location::setFeatureType(Location::FeatureType _featureType) // cppcheck-suppress passedByValue { featureType = _featureType; } - Location::FeatureType Location::parseFeatureType(std::string_view s) { @@ -120,21 +116,18 @@ Location::parseFeatureType(std::string_view s) : ptr->featureType; } - Body* Location::getParentBody() const { return parent; } - void Location::setParentBody(Body* _parent) { parent = _parent; } - /*! Get the position of the location relative to its body in * the J2000 ecliptic coordinate system. */ @@ -148,7 +141,6 @@ Location::getPlanetocentricPosition(double t) const return q.conjugate() * position.cast(); } - Eigen::Vector3d Location::getHeliocentricPosition(double t) const { diff --git a/src/celengine/location.h b/src/celengine/location.h index f3dcea166f1..4fddeb2a33e 100644 --- a/src/celengine/location.h +++ b/src/celengine/location.h @@ -18,7 +18,7 @@ #include class Body; - +class StarDatabase; class Location { @@ -26,6 +26,8 @@ class Location const std::string& getName(bool i18n = false) const; void setName(const std::string&); + std::string getPath(const StarDatabase*, char delimiter = '/') const; + Eigen::Vector3f getPosition() const; void setPosition(const Eigen::Vector3f&); diff --git a/src/celengine/parseobject.cpp b/src/celengine/parseobject.cpp index 326ea442c1f..26d5185f1c8 100644 --- a/src/celengine/parseobject.cpp +++ b/src/celengine/parseobject.cpp @@ -871,8 +871,6 @@ CreateMeanEquatorFrame(const Universe& universe, } } - GetLogger()->debug("CreateMeanEquatorFrame {}, {}\n", center.getName(), obj.getName()); - if (double freezeEpoch = 0.0; ParseDate(frameData, "Freeze", freezeEpoch)) return std::make_shared(center, obj, freezeEpoch); @@ -1002,7 +1000,7 @@ getVectorObserver(const Universe& universe, const Hash* vectorData) if (obsObject.empty()) { GetLogger()->warn("Bad two-vector frame: observer object '{}' of vector not found.\n", - obsObject.getName()); + *obsName); return Selection(); } diff --git a/src/celengine/selection.cpp b/src/celengine/selection.cpp index df5431d0022..a45e0e291c3 100644 --- a/src/celengine/selection.cpp +++ b/src/celengine/selection.cpp @@ -27,7 +27,6 @@ namespace // are Julian days. constexpr double VELOCITY_DIFF_DELTA = 1.0 / 1440.0; - UniversalCoord locationPosition(const Location* location, double t) { if (const Body* body = location->getParentBody(); body != nullptr) @@ -38,32 +37,8 @@ UniversalCoord locationPosition(const Location* location, double t) return UniversalCoord::Zero(); } - -std::string bodyName(const Body* body, bool i18n) -{ - std::string name = body->getName(i18n); - const PlanetarySystem* system = body->getSystem(); - while (system != nullptr) - { - if (const Body* parent = system->getPrimaryBody(); parent != nullptr) - { - name = parent->getName(i18n) + '/' + name; - system = parent->getSystem(); - } - else - { - if (const Star* parentStar = system->getStar(); parentStar != nullptr) - name = fmt::format("#{}/{}", parentStar->getIndex(), name); - system = nullptr; - } - } - - return name; -} - } // end unnamed namespace - double Selection::radius() const { @@ -83,7 +58,6 @@ Selection::radius() const } } - UniversalCoord Selection::getPosition(double t) const { @@ -111,7 +85,6 @@ Selection::getPosition(double t) const } } - Eigen::Vector3d Selection::getVelocity(double t) const { @@ -139,37 +112,8 @@ Selection::getVelocity(double t) const } } - -std::string -Selection::getName(bool i18n) const -{ - switch (type) - { - case SelectionType::Star: - return fmt::format("#{}", static_cast(obj)->getIndex()); - - case SelectionType::Body: - return bodyName(static_cast(obj), i18n); - - case SelectionType::DeepSky: - return fmt::format("#{}", static_cast(obj)->getIndex()); - - case SelectionType::Location: - { - auto location = static_cast(obj); - if (auto parentBody = location->getParentBody(); parentBody == nullptr) - return location->getName(i18n); - else - return fmt::format("{}/{}", bodyName(parentBody, i18n), location->getName(i18n)); - } - - default: - return {}; - } -} - - -Selection Selection::parent() const +Selection +Selection::parent() const { switch (type) { @@ -199,9 +143,9 @@ Selection Selection::parent() const } } - /*! Return true if the selection's visibility flag is set. */ -bool Selection::isVisible() const +bool +Selection::isVisible() const { switch (type) { diff --git a/src/celengine/selection.h b/src/celengine/selection.h index 9c3f42c9399..90239bf9241 100644 --- a/src/celengine/selection.h +++ b/src/celengine/selection.h @@ -48,32 +48,31 @@ class Selection double radius() const; UniversalCoord getPosition(double t) const; Eigen::Vector3d getVelocity(double t) const; - std::string getName(bool i18n = false) const; Selection parent() const; bool isVisible() const; - Star* star() const + inline Star* star() const { return type == SelectionType::Star ? static_cast(obj) : nullptr; } - Body* body() const + inline Body* body() const { return type == SelectionType::Body ? static_cast(obj) : nullptr; } - DeepSkyObject* deepsky() const + inline DeepSkyObject* deepsky() const { return type == SelectionType::DeepSky ? static_cast(obj) : nullptr; } - Location* location() const + inline Location* location() const { return type == SelectionType::Location ? static_cast(obj) : nullptr; } - SelectionType getType() const { return type; } + inline SelectionType getType() const { return type; } private: SelectionType type { SelectionType::None }; diff --git a/src/celengine/stardb.h b/src/celengine/stardb.h index ff3bd919577..2d309a466d6 100644 --- a/src/celengine/stardb.h +++ b/src/celengine/stardb.h @@ -92,13 +92,15 @@ class StarDatabase using CrossIndex = std::vector; - AstroCatalog::IndexNumber searchCrossIndexForCatalogNumber(StarCatalog, AstroCatalog::IndexNumber number) const; - Star* searchCrossIndex(StarCatalog, AstroCatalog::IndexNumber number) const; AstroCatalog::IndexNumber crossIndex(StarCatalog, AstroCatalog::IndexNumber number) const; private: static constexpr auto NumCatalogs = static_cast(StarCatalog::_CatalogCount); + AstroCatalog::IndexNumber searchCrossIndexForCatalogNumber(StarCatalog, AstroCatalog::IndexNumber number) const; + Star* searchCrossIndex(StarCatalog, AstroCatalog::IndexNumber number) const; + + std::uint32_t nStars{ 0 }; std::unique_ptr stars; //NOSONAR diff --git a/src/celestia/celestiacore.cpp b/src/celestia/celestiacore.cpp index 47398224260..f09b9772e16 100644 --- a/src/celestia/celestiacore.cpp +++ b/src/celestia/celestiacore.cpp @@ -310,10 +310,16 @@ void CelestiaCore::addFavorite(const string &name, const string &parentFolder, F fav->parentFolder = parentFolder; Selection sel = sim->getSelection(); - if (sel.deepsky() != nullptr) - fav->selectionName = sim->getUniverse()->getDSOCatalog()->getDSOName(sel.deepsky()); + if (const DeepSkyObject* dso = sel.deepsky(); dso != nullptr) + fav->selectionName = sim->getUniverse()->getDSOCatalog()->getDSOName(dso); + else if (const Star* star = sel.star(); star != nullptr) + fav->selectionName = sim->getUniverse()->getStarCatalog()->getStarName(*star); + else if (const Body* body = sel.body(); body != nullptr) + fav->selectionName = body->getPath(sim->getUniverse()->getStarCatalog()); + else if (const Location* location = sel.location(); location != nullptr) + fav->selectionName = location->getPath(sim->getUniverse()->getStarCatalog()); else - fav->selectionName = sel.getName(); + return; fav->coordSys = sim->getFrame()->getCoordinateSystem(); @@ -350,14 +356,16 @@ const DestinationList* CelestiaCore::getDestinations() void showSelectionInfo(const Selection& sel) { Quaternionf orientation; - if (sel.deepsky() != nullptr) + if (const DeepSkyObject* dso = sel.deepsky(); dso != nullptr) orientation = sel.deepsky()->getOrientation(); else if (sel.body() != nullptr) orientation = sel.body()->getGeometryOrientation(); + else + return; AngleAxisf aa(orientation); - GetLogger()->info("{}\nOrientation: [{}, {}, {}], {:.1f}\n", sel.getName(), aa.axis().x(), aa.axis().y(), aa.axis().z(), math::radToDeg(aa.angle())); + GetLogger()->info("Orientation: [{}, {}, {}], {:.1f}\n", aa.axis().x(), aa.axis().y(), aa.axis().z(), math::radToDeg(aa.angle())); } diff --git a/src/celestia/url.cpp b/src/celestia/url.cpp index 687f2664336..cabe3dcb189 100644 --- a/src/celestia/url.cpp +++ b/src/celestia/url.cpp @@ -54,36 +54,6 @@ constexpr unsigned int NEW_FLAG_BIT_1_7 = 27; constexpr std::uint64_t NEW_SHOW_PLANETS_BIT_MASK = (UINT64_C(1) << (NEW_FLAG_BIT_1_7 - 1)); constexpr std::uint64_t RF_MASK = NEW_SHOW_PLANETS_BIT_MASK - 1; - -std::string -getBodyName(const Universe *universe, const Body *body) -{ - std::string name = body->getName(); - const PlanetarySystem* parentSystem = body->getSystem(); - const Body* parentBody = nullptr; - - if (parentSystem != nullptr) - parentBody = parentSystem->getPrimaryBody(); - else - assert(0); - // TODO: Figure out why the line below was added. - //parentBody = body->getOrbitBarycenter(); - - while (parentBody != nullptr) - { - name = parentBody->getName() + ":" + name; - parentSystem = parentBody->getSystem(); - if (parentSystem == nullptr) - break; - parentBody = parentSystem->getPrimaryBody(); - } - - if (auto *star = body->getSystem()->getStar(); star != nullptr) - name = universe->getStarCatalog()->getStarName(*star) + ":" + name; - - return name; -} - std::string_view getCoordSysName(ObserverFrame::CoordinateSystem mode) { @@ -323,12 +293,11 @@ Url::getEncodedObjectName(const Selection& selection, const CelestiaCore* appCor { auto *universe = appCore->getSimulation()->getUniverse(); std::string name; - Body* parentBody = nullptr; switch (selection.getType()) { case SelectionType::Body: - name = getBodyName(universe, selection.body()); + name = selection.body()->getPath(universe->getStarCatalog(), ':'); break; case SelectionType::Star: @@ -340,10 +309,7 @@ Url::getEncodedObjectName(const Selection& selection, const CelestiaCore* appCor break; case SelectionType::Location: - name = selection.location()->getName(); - parentBody = selection.location()->getParentBody(); - if (parentBody != nullptr) - name = getBodyName(universe, parentBody) + ":" + name; + name = selection.location()->getPath(universe->getStarCatalog(), ':'); break; default: