Skip to content

Commit

Permalink
Return alternate surface names, locations and reference marks as views
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtribick committed Oct 5, 2023
1 parent cc49f68 commit c1cd1fe
Show file tree
Hide file tree
Showing 12 changed files with 542 additions and 156 deletions.
33 changes: 3 additions & 30 deletions src/celengine/body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,20 +944,6 @@ void Body::addAlternateSurface(const string& name, std::unique_ptr<Surface>&& al
}


vector<string>* Body::getAlternateSurfaceNames() const
{
vector<string>* names = new vector<string>();
if (altSurfaces)
{
std::transform(altSurfaces->begin(), altSurfaces->end(),
std::back_inserter(*names),
[](const auto& s) { return s.first; });
}

return names;
}


void Body::addLocation(std::unique_ptr<Location>&& loc)
{
if (!loc)
Expand All @@ -970,12 +956,6 @@ void Body::addLocation(std::unique_ptr<Location>&& loc)
}


const std::vector<std::unique_ptr<Location>>* Body::getLocations() const
{
return locations.get();
}


Location* Body::findLocation(std::string_view name, bool i18n) const
{
if (!locations)
Expand Down Expand Up @@ -1004,6 +984,9 @@ void Body::computeLocations()

locationsComputed = true;

if (locations == nullptr)
return;

// No work to do if there's no mesh, or if the mesh cannot be loaded
if (geometry == InvalidResource)
return;
Expand Down Expand Up @@ -1087,16 +1070,6 @@ Body::findReferenceMark(const string& tag) const
}


/*! Get the list of reference marks associated with this body. May return
* nullptr if there are no reference marks.
*/
const std::list<std::unique_ptr<ReferenceMark>>*
Body::getReferenceMarks() const
{
return referenceMarks.get();
}


/*! Sets whether or not the object is visible.
*/
void Body::setVisible(bool _visible)
Expand Down
37 changes: 34 additions & 3 deletions src/celengine/body.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
#include <celengine/timeline.h>
#include <celephem/rotation.h>
#include <celephem/orbit.h>
#include <celutil/ranges.h>
#include <celutil/utf8.h>
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
Expand Down Expand Up @@ -305,9 +307,32 @@ class Body

Surface* getAlternateSurface(const std::string&) const;
void addAlternateSurface(const std::string&, std::unique_ptr<Surface>&&);
std::vector<std::string>* getAlternateSurfaceNames() const;
auto getAlternateSurfaceNames() const
{
using range_type = decltype(celestia::util::keysView(*altSurfaces));
return altSurfaces
? std::make_optional(celestia::util::keysView(*altSurfaces))
: std::optional<range_type>();
}

bool hasLocations() const { return locations != nullptr && !locations->empty(); }

auto getLocations()
{
using range_type = decltype(celestia::util::pointerView(*locations));
return locations
? std::make_optional(celestia::util::pointerView(*locations))
: std::optional<range_type>();
}

auto getLocations() const
{
using range_type = decltype(celestia::util::constPointerView(*locations));
return locations
? std::make_optional(celestia::util::constPointerView(*locations))
: std::optional<range_type>();
}

const std::vector<std::unique_ptr<Location>>* getLocations() const;
void addLocation(std::unique_ptr<Location>&&);
Location* findLocation(std::string_view, bool i18n = false) const;
void computeLocations();
Expand Down Expand Up @@ -348,7 +373,13 @@ class Body
void addReferenceMark(std::unique_ptr<ReferenceMark>&& refMark);
void removeReferenceMark(const std::string& tag);
const ReferenceMark* findReferenceMark(const std::string& tag) const;
const std::list<std::unique_ptr<ReferenceMark>>* getReferenceMarks() const;
auto getReferenceMarks() const
{
using range_type = decltype(celestia::util::constPointerView(*referenceMarks));
return referenceMarks
? std::make_optional(celestia::util::constPointerView(*referenceMarks))
: std::optional<range_type>();
}

void markChanged();
void markUpdated();
Expand Down
36 changes: 18 additions & 18 deletions src/celengine/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1907,10 +1907,8 @@ Renderer::locationsToAnnotations(const Body& body,
const Vector3d& bodyPosition,
const Quaterniond& bodyOrientation)
{
const std::vector<std::unique_ptr<Location>>* locations = body.getLocations();

if (locations == nullptr)
return;
assert(body.hasLocations());
auto locations = body.getLocations();

Vector3f semiAxes = body.getSemiAxes();

Expand All @@ -1928,15 +1926,14 @@ Renderer::locationsToAnnotations(const Body& body,

Matrix3d bodyMatrix = bodyOrientation.conjugate().toRotationMatrix();

for (const auto& location : *locations)
for (const auto location : *locations)
{
const Location* loc = location.get();
auto featureType = loc->getFeatureType();
auto featureType = location->getFeatureType();
if ((featureType & locationFilter) == 0)
continue;

// Get the position of the location with respect to the planet center
Vector3f ppos = loc->getPosition();
Vector3f ppos = location->getPosition();

// Compute the bodycentric position of the location
Vector3d locPos = ppos.cast<double>();
Expand All @@ -1948,9 +1945,9 @@ Renderer::locationsToAnnotations(const Body& body,
// Get the camera space label position
Vector3d labelPos = bodyCenter + bodyMatrix * locPos;

float effSize = loc->getImportance();
float effSize = location->getImportance();
if (effSize < 0.0f)
effSize = loc->getSize();
effSize = location->getSize();

if (float pixSize = effSize / (float) (labelPos.norm() * pixelSize);
pixSize <= minFeatureSize || labelPos.dot(viewNormal) <= 0.0)
Expand Down Expand Up @@ -2003,9 +2000,12 @@ Renderer::locationsToAnnotations(const Body& body,
else if (featureType & (Location::EruptiveCenter))
locationMarker = &genericLocationRep;

Color labelColor = loc->isLabelColorOverridden() ? loc->getLabelColor() : LocationLabelColor;
Color labelColor = location->isLabelColorOverridden()
? location->getLabelColor()
: LocationLabelColor;

addObjectAnnotation(locationMarker,
loc->getName(true),
location->getName(true),
labelColor,
labelPos.cast<float>(),
LabelHorizontalAlignment::Start,
Expand Down Expand Up @@ -2782,7 +2782,7 @@ void Renderer::renderPlanet(Body& body,

rp.orientation = body.getGeometryOrientation() * q.cast<float>();

if (body.getLocations() != nullptr && (labelMode & LocationLabels) != 0)
if ((labelMode & LocationLabels) != 0)
body.computeLocations();

Vector3f scaleFactors;
Expand Down Expand Up @@ -2978,7 +2978,7 @@ void Renderer::renderPlanet(Body& body,
nearPlaneDistance, farPlaneDistance,
rp, lights, m);

if (body.getLocations() != nullptr && (labelMode & LocationLabels) != 0)
if (body.hasLocations() && (labelMode & LocationLabels) != 0)
{
// Set up location markers for this body
using namespace celestia;
Expand Down Expand Up @@ -3286,14 +3286,14 @@ void Renderer::addRenderListEntries(RenderListEntry& rle,
}
}

const std::list<std::unique_ptr<ReferenceMark>>* refMarks = body.getReferenceMarks();
if (refMarks == nullptr)
auto refMarks = body.getReferenceMarks();
if (!refMarks.has_value())
return;

for (const auto& rm : *refMarks)
for (const auto rm : *refMarks)
{
rle.renderableType = RenderListEntry::RenderableReferenceMark;
rle.refMark = rm.get();
rle.refMark = rm;
rle.isOpaque = rm->isOpaque();
rle.radius = rm->boundingSphereRadius();
renderList.push_back(rle);
Expand Down
28 changes: 14 additions & 14 deletions src/celengine/universe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,22 +542,22 @@ void
getLocationsCompletion(std::vector<std::string>& completion,
std::string_view s,
bool i18n,
const std::vector<std::unique_ptr<Location>>* locations)
const Body& body)
{
if (locations == nullptr)
auto locations = body.getLocations();
if (!locations.has_value() || locations->empty())
return;

for (const auto& location : *locations)
for (const auto location : *locations)
{
const Location* loc = location.get();
const std::string& name = loc->getName(false);
const std::string& name = location->getName(false);
if (UTF8StartsWith(name, s))
{
completion.push_back(name);
}
else if (i18n)
{
const std::string& lname = loc->getName(true);
const std::string& lname = location->getName(true);
if (lname != name && UTF8StartsWith(lname, s))
completion.push_back(lname);
}
Expand All @@ -569,20 +569,20 @@ void
getLocationsCompletionPath(std::vector<std::string>& completion,
std::string_view search,
bool i18n,
const std::vector<std::unique_ptr<Location>>* locations)
const Body& body)
{
if (locations == nullptr)
auto locations = body.getLocations();
if (!locations.has_value() || locations->empty())
return;

for (const auto& location : *locations)
for (const auto location : *locations)
{
const Location* loc = location.get();
const std::string& name = loc->getName(false);
const std::string& name = location->getName(false);
if (UTF8StartsWith(name, search))
completion.push_back(name);
else if (i18n)
{
const std::string& lname = loc->getName(true);
const std::string& lname = location->getName(true);
if (lname != name && UTF8StartsWith(lname, search))
completion.push_back(lname);
}
Expand Down Expand Up @@ -1171,7 +1171,7 @@ Universe::getCompletion(std::vector<std::string>& completion,
if (withLocations && context.getType() == SelectionType::Body)
{
getLocationsCompletion(completion, s, i18n,
context.body()->getLocations());
*context.body());
}

const SolarSystem* sys = getSolarSystem(context);
Expand Down Expand Up @@ -1242,7 +1242,7 @@ Universe::getCompletionPath(std::vector<std::string>& completion,
getLocationsCompletionPath(completion,
s.substr(pos + 1),
i18n,
sel.body()->getLocations());
*sel.body());
}
}

Expand Down
58 changes: 33 additions & 25 deletions src/celestia/gtk/menu-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/

#include <algorithm>
#include <iterator>

#include <gtk/gtk.h>

#include <celengine/simulation.h>
Expand All @@ -35,7 +37,8 @@ static void handleContextSurface(gpointer data);
/* Definitions: Helpers */
static GtkMenuItem* AppendMenu(GtkWidget* parent, GCallback callback, const gchar* name, gpointer extra);
static GtkMenu* CreatePlanetarySystemMenu(string parentName, const PlanetarySystem* psys);
static GtkMenu* CreateAlternateSurfaceMenu(const vector<string>& surfaces);
template<typename T>
static GtkMenu* CreateAlternateSurfaceMenu(const T& surfaces);


/* There is no way to pass the AppData struct to the menu at this time. This
Expand Down Expand Up @@ -76,22 +79,18 @@ void GTKContextMenuHandler::requestContextMenu(float, float, Selection sel)
AppendMenu(popup, G_CALLBACK(handleContextPrimary), "Select _Primary Body", NULL);
}

const PlanetarySystem* satellites = sel.body()->getSatellites();
if (satellites != NULL && satellites->getSystemSize() != 0)
if (const PlanetarySystem* satellites = sel.body()->getSatellites();
satellites != nullptr && satellites->getSystemSize() != 0)
{
GtkMenu* satMenu = CreatePlanetarySystemMenu(name, satellites);
gtk_menu_item_set_submenu(AppendMenu(popup, NULL, "_Satellites", 0), GTK_WIDGET(satMenu));
}

vector<string>* altSurfaces = sel.body()->getAlternateSurfaceNames();
if (altSurfaces != NULL)
if (auto altSurfaces = sel.body()->getAlternateSurfaceNames();
altSurfaces.has_value() && !altSurfaces->empty())
{
if (altSurfaces->size() > 0)
{
GtkMenu* surfMenu = CreateAlternateSurfaceMenu(*altSurfaces);
gtk_menu_item_set_submenu(AppendMenu(popup, NULL, "_Alternate Surfaces", 0), GTK_WIDGET(surfMenu));
delete altSurfaces;
}
GtkMenu* surfMenu = CreateAlternateSurfaceMenu(*altSurfaces);
gtk_menu_item_set_submenu(AppendMenu(popup, NULL, "_Alternate Surfaces", 0), GTK_WIDGET(surfMenu));
}
}
break;
Expand Down Expand Up @@ -230,18 +229,24 @@ static void handleContextSurface(gpointer data)

/* Handle the alternate surface submenu */
Selection sel = app->simulation->getSelection();
if (sel.body() != NULL)
const Body* body = sel.body();
if (body == nullptr)
return;

guint index = value - 1;
auto surfNames = body->getAlternateSurfaceNames();
if (!surfNames.has_value())
return;

if (index < surfNames->size())
{
guint index = value - 1;
vector<string>* surfNames = sel.body()->getAlternateSurfaceNames();
if (surfNames != NULL)
{
string surfName;
if (index < surfNames->size())
surfName = surfNames->at(index);
app->simulation->getActiveObserver()->setDisplayedSurface(surfName);
delete surfNames;
}
auto it = surfNames->begin();
std::advance(it, index);
app->simulation->getActiveObserver()->setDisplayedSurface(*it);
}
else
{
app->simulation->getActiveObserver()->setDisplayedSurface({});
}
}

Expand Down Expand Up @@ -434,14 +439,17 @@ static GtkMenu* CreatePlanetarySystemMenu(string parentName, const PlanetarySyst


/* HELPER: Create surface submenu for context menu, return menu pointer. */
static GtkMenu* CreateAlternateSurfaceMenu(const vector<string>& surfaces)
template<typename T>
static GtkMenu* CreateAlternateSurfaceMenu(const T& surfaces)
{
GtkWidget* menu = gtk_menu_new();

AppendMenu(menu, G_CALLBACK(handleContextSurface), "Normal", 0);
for (guint i = 0; i < surfaces.size(); i++)
guint i = 0;
for (const auto& surface : surfaces)
{
AppendMenu(menu, G_CALLBACK(handleContextSurface), surfaces[i].c_str(), GINT_TO_POINTER(i+1));
++i;
AppendMenu(menu, G_CALLBACK(handleContextSurface), surface.c_str(), GINT_TO_POINTER(i));
}

return GTK_MENU(menu);
Expand Down
Loading

0 comments on commit c1cd1fe

Please sign in to comment.