Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove getName method on Selection, create getPath on Body/Location #2196

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/celengine/body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -111,13 +112,37 @@ const vector<string>& 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.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/celengine/body.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Body;
class FrameTree;
class ReferenceMark;
class Atmosphere;
class StarDatabase;

class PlanetarySystem
{
Expand Down Expand Up @@ -199,6 +200,7 @@ class Body //NOSONAR
PlanetarySystem* getSystem() const;
const std::vector<std::string>& 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);
Expand Down
24 changes: 8 additions & 16 deletions src/celengine/location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
using std::size_t;
using std::strncmp;


namespace
{

Expand All @@ -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)
{
Expand All @@ -47,70 +44,69 @@ 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
{
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)
{
Expand All @@ -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.
*/
Expand All @@ -148,7 +141,6 @@ Location::getPlanetocentricPosition(double t) const
return q.conjugate() * position.cast<double>();
}


Eigen::Vector3d
Location::getHeliocentricPosition(double t) const
{
Expand Down
4 changes: 3 additions & 1 deletion src/celengine/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
#include <celutil/color.h>

class Body;

class StarDatabase;

class Location
{
public:
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&);

Expand Down
4 changes: 1 addition & 3 deletions src/celengine/parseobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BodyMeanEquatorFrame>(center, obj, freezeEpoch);

Expand Down Expand Up @@ -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();
}

Expand Down
64 changes: 4 additions & 60 deletions src/celengine/selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
{
Expand All @@ -83,7 +58,6 @@ Selection::radius() const
}
}


UniversalCoord
Selection::getPosition(double t) const
{
Expand Down Expand Up @@ -111,7 +85,6 @@ Selection::getPosition(double t) const
}
}


Eigen::Vector3d
Selection::getVelocity(double t) const
{
Expand Down Expand Up @@ -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<const Star*>(obj)->getIndex());

case SelectionType::Body:
return bodyName(static_cast<const Body*>(obj), i18n);

case SelectionType::DeepSky:
return fmt::format("#{}", static_cast<const DeepSkyObject*>(obj)->getIndex());

case SelectionType::Location:
{
auto location = static_cast<const Location*>(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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
11 changes: 5 additions & 6 deletions src/celengine/selection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Star*>(obj) : nullptr;
}

Body* body() const
inline Body* body() const
{
return type == SelectionType::Body ? static_cast<Body*>(obj) : nullptr;
}

DeepSkyObject* deepsky() const
inline DeepSkyObject* deepsky() const
{
return type == SelectionType::DeepSky ? static_cast<DeepSkyObject*>(obj) : nullptr;
}

Location* location() const
inline Location* location() const
{
return type == SelectionType::Location ? static_cast<Location*>(obj) : nullptr;
}

SelectionType getType() const { return type; }
inline SelectionType getType() const { return type; }

private:
SelectionType type { SelectionType::None };
Expand Down
6 changes: 4 additions & 2 deletions src/celengine/stardb.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ class StarDatabase

using CrossIndex = std::vector<CrossIndexEntry>;

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<std::size_t>(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<Star[]> stars; //NOSONAR
Expand Down
Loading
Loading