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

Use optional instead of infinity #1840

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/celengine/body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ bool Body::extant(double t) const
}


void Body::getLifespan(double& begin, double& end) const
void Body::getLifespan(std::optional<double>& begin, std::optional<double>& end) const
{
begin = timeline->startTime();
end = timeline->endTime();
Expand Down
3 changes: 1 addition & 2 deletions src/celengine/body.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,7 @@ class Body
Eigen::Vector3d eclipticToPlanetocentric(const Eigen::Vector3d& ecl, double tdb) const;

bool extant(double) const;
void setLifespan(double, double);
void getLifespan(double&, double&) const;
void getLifespan(std::optional<double>&, std::optional<double>&) const;

Surface* getAlternateSurface(const std::string&) const;
void addAlternateSurface(const std::string&, Surface*);
Expand Down
2 changes: 1 addition & 1 deletion src/celengine/fisheyeprojectionmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ float FisheyeProjectionMode::getFieldCorrection(float /*zoom*/) const
}

celmath::Frustum
FisheyeProjectionMode::getFrustum(float nearZ, float farZ, float zoom) const
FisheyeProjectionMode::getFrustum(float nearZ, std::optional<float> farZ, float zoom) const
{
return celmath::Frustum(getFOV(zoom), width / height, nearZ, farZ);
}
Expand Down
2 changes: 1 addition & 1 deletion src/celengine/fisheyeprojectionmode.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FisheyeProjectionMode : public ProjectionMode
float getZoom(float fov) const override;
float getPixelSize(float zoom) const override;
float getFieldCorrection(float zoom) const override;
celmath::Frustum getFrustum(float nearZ, float farZ, float zoom) const override;
celmath::Frustum getFrustum(float nearZ, std::optional<float> farZ, float zoom) const override;
double getViewConeAngleMax(float zoom) const override;

float getNormalizedDeviceZ(float nearZ, float farZ, float z) const override;
Expand Down
22 changes: 13 additions & 9 deletions src/celengine/parseobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,31 @@ GetDefaultUnits(bool usePlanetUnits, double& distanceScale)
bool
ParseDate(const Hash* hash, const string& name, double& jd)
{
// Check first for a number value representing a Julian date
if (auto jdVal = hash->getNumber<double>(name); jdVal.has_value())
if (auto value = ParseDate(hash, name); value.has_value())
{
jd = *jdVal;
jd = value.value();
return true;
}
return false;
}

std::optional<double>
ParseDate(const Hash* hash, const string& name)
{
// Check first for a number value representing a Julian date
if (auto jdVal = hash->getNumber<double>(name); jdVal.has_value())
return *jdVal;

if (const std::string* dateString = hash->getString(name); dateString != nullptr)
{
astro::Date date(1, 1, 1);
if (astro::parseDate(*dateString, date))
{
jd = (double) date;
return true;
}
return static_cast<double>(date);
}

return false;
return std::nullopt;
}


/*!
* Create a new Keplerian orbit from an ssc property table:
*
Expand Down
2 changes: 2 additions & 0 deletions src/celengine/parseobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <string>
#include <memory>
#include <optional>
#include <celephem/orbit.h>
#include <celephem/rotation.h>
#include <celcompat/filesystem.h>
Expand All @@ -34,6 +35,7 @@ enum class DataDisposition


bool ParseDate(const Hash* hash, const std::string& name, double& jd);
std::optional<double> ParseDate(const Hash* hash, const std::string& name);

celestia::ephem::Orbit* CreateOrbit(const Selection& centralObject,
const Hash* planetData,
Expand Down
2 changes: 1 addition & 1 deletion src/celengine/perspectiveprojectionmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ float PerspectiveProjectionMode::getFieldCorrection(float zoom) const
}

celmath::Frustum
PerspectiveProjectionMode::getFrustum(float nearZ, float farZ, float zoom) const
PerspectiveProjectionMode::getFrustum(float nearZ, std::optional<float> farZ, float zoom) const
{
return celmath::Frustum(getFOV(zoom), width / height, nearZ, farZ);
}
Expand Down
2 changes: 1 addition & 1 deletion src/celengine/perspectiveprojectionmode.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PerspectiveProjectionMode : public ProjectionMode
float getZoom(float fov) const override;
float getPixelSize(float zoom) const override;
float getFieldCorrection(float zoom) const override;
celmath::Frustum getFrustum(float nearZ, float farZ, float zoom) const override;
celmath::Frustum getFrustum(float nearZ, std::optional<float> farZ, float zoom) const override;
double getViewConeAngleMax(float zoom) const override;

float getNormalizedDeviceZ(float nearZ, float farZ, float z) const override;
Expand Down
3 changes: 2 additions & 1 deletion src/celengine/projectionmode.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include <Eigen/Core>
#include <optional>

class ShaderManager;

Expand All @@ -36,7 +37,7 @@ class ProjectionMode
virtual float getZoom(float fov) const = 0;
virtual float getPixelSize(float zoom) const = 0;
virtual float getFieldCorrection(float zoom) const = 0;
virtual celmath::Frustum getFrustum(float nearZ, float farZ, float zoom) const = 0;
virtual celmath::Frustum getFrustum(float nearZ, std::optional<float> farZ, float zoom) const = 0;

// Calculate the cosine of half the maximum field of view. We'll use this for
// fast testing of object visibility.
Expand Down
4 changes: 2 additions & 2 deletions src/celengine/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,7 @@ void Renderer::draw(const Observer& observer,
m_cameraOrientation = Quaterniond(m_cameraTransform) * observer.getOrientation();

// Get the view frustum used for culling in camera space.
auto frustum = projectionMode->getFrustum(MinNearPlaneDistance, std::numeric_limits<float>::infinity(), zoom);
auto frustum = projectionMode->getFrustum(MinNearPlaneDistance, std::nullopt, zoom);

// Get the transformed frustum, used for culling in the astrocentric coordinate
// system.
Expand Down Expand Up @@ -3939,7 +3939,7 @@ void Renderer::renderDeepSkyObjects(const Universe& universe,
dsoRenderer.renderFlags = renderFlags;
dsoRenderer.labelMode = labelMode;

dsoRenderer.frustum = projectionMode->getFrustum(MinNearPlaneDistance, std::numeric_limits<float>::infinity(), observer.getZoom());
dsoRenderer.frustum = projectionMode->getFrustum(MinNearPlaneDistance, std::nullopt, observer.getZoom());
// Use pixelSize * screenDpi instead of FoV, to eliminate windowHeight dependence.
// = 1.0 at startup
float effDistanceToScreen = mmToInches((float) REF_DISTANCE_TO_SCREEN) * pixelSize * getScreenDpi();
Expand Down
42 changes: 27 additions & 15 deletions src/celengine/solarsys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,24 +272,30 @@ TimelinePhase::SharedConstPtr CreateTimelinePhase(Body* body,
const ReferenceFrame::SharedConstPtr& defaultBodyFrame,
bool isFirstPhase,
bool isLastPhase,
double previousPhaseEnd)
std::optional<double> previousPhaseEnd)
{
double beginning = previousPhaseEnd;
double ending = std::numeric_limits<double>::infinity();
std::optional<double> beginning = previousPhaseEnd;
std::optional<double> ending = std::nullopt;

// Beginning is optional for the first phase of a timeline, and not
// allowed for the other phases, where beginning is always the ending
// of the previous phase.
bool hasBeginning = ParseDate(phaseData, "Beginning", beginning);
if (!isFirstPhase && hasBeginning)
if (auto newBeginning = ParseDate(phaseData, "Beginning"); newBeginning.has_value())
{
GetLogger()->error("Error: Beginning can only be specified for initial phase of timeline.\n");
return nullptr;
beginning = newBeginning.value();
if (!isFirstPhase)
{
GetLogger()->error("Error: Beginning can only be specified for initial phase of timeline.\n");
return nullptr;
}
}

// Ending is required for all phases except for the final one.
bool hasEnding = ParseDate(phaseData, "Ending", ending);
if (!isLastPhase && !hasEnding)
if (auto newEnding = ParseDate(phaseData, "Ending"); newEnding.has_value())
{
ending = newEnding.value();
}
else if (!isLastPhase)
{
GetLogger()->error("Error: Ending is required for all timeline phases other than the final one.\n");
return nullptr;
Expand Down Expand Up @@ -375,7 +381,7 @@ Timeline* CreateTimelineFromArray(Body* body,
const ReferenceFrame::SharedConstPtr& defaultBodyFrame)
{
auto timeline = std::make_unique<Timeline>();
double previousEnding = -std::numeric_limits<double>::infinity();
std::optional<double> previousEnding = std::nullopt;

if (timelineArray->empty())
{
Expand Down Expand Up @@ -487,8 +493,8 @@ bool CreateTimeline(Body* body,
ReferenceFrame::SharedConstPtr bodyFrame;
celestia::ephem::Orbit* orbit = nullptr;
celestia::ephem::RotationModel* rotationModel = nullptr;
double beginning = -std::numeric_limits<double>::infinity();
double ending = std::numeric_limits<double>::infinity();
std::optional<double> beginning = std::nullopt;
std::optional<double> ending = std::nullopt;

// If any new timeline values are specified, we need to overrideOldTimeline will
// be set to true.
Expand Down Expand Up @@ -601,18 +607,24 @@ bool CreateTimeline(Body* body,
rotationModel = CreateDefaultRotationModel(syncRotationPeriod);
}

if (ParseDate(planetData, "Beginning", beginning))
if (auto newBeginning = ParseDate(planetData, "Beginning"); newBeginning.has_value())
{
beginning = newBeginning.value();
overrideOldTimeline = true;
if (ParseDate(planetData, "Ending", ending))
}
if (auto newEnding = ParseDate(planetData, "Ending"); newEnding.has_value())
{
ending = newEnding.value();
overrideOldTimeline = true;
}

// Something went wrong if the disposition isn't modify and no timeline
// is to be created.
assert(disposition == DataDisposition::Modify || overrideOldTimeline);

if (overrideOldTimeline)
{
if (beginning >= ending)
if (beginning.has_value() && ending.has_value() && beginning.value() >= ending.value())
{
GetLogger()->error("Beginning time must be before Ending time.\n");
delete rotationModel;
Expand Down
16 changes: 11 additions & 5 deletions src/celengine/timeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ Timeline::appendPhase(TimelinePhase::SharedConstPtr &phase)
// no gaps and no overlaps.
if (!phases.empty())
{
if (phase->startTime() != phases.back()->endTime())
auto startTime = phase->startTime();
if (!startTime.has_value() || startTime.value() != phases.back()->endTime())
return false;
}

Expand All @@ -64,7 +65,8 @@ Timeline::findPhase(double t) const
{
for (const auto& phase : phases)
{
if (t < phase->endTime())
auto endTime = phase->endTime();
if (!endTime.has_value() || t < endTime.value())
return phase;
}

Expand Down Expand Up @@ -92,14 +94,14 @@ Timeline::phaseCount() const
}


double
std::optional<double>
Timeline::startTime() const
{
return phases.front()->startTime();
}


double
std::optional<double>
Timeline::endTime() const
{
return phases.back()->endTime();
Expand All @@ -114,7 +116,11 @@ Timeline::endTime() const
bool
Timeline::includes(double t) const
{
return phases.front()->startTime() <= t && t <= phases.back()->endTime();
if (auto startTime = phases.front()->startTime(); startTime.has_value() && t < startTime.value())
return false;
if (auto endTime = phases.back()->endTime(); endTime.has_value() && t > endTime.value())
return false;
return true;
}


Expand Down
5 changes: 3 additions & 2 deletions src/celengine/timeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#pragma once

#include <memory>
#include <optional>
#include <vector>
#include "timelinephase.h"

Expand All @@ -27,8 +28,8 @@ class Timeline
const TimelinePhase::SharedConstPtr& getPhase(unsigned int n) const;
unsigned int phaseCount() const;

double startTime() const;
double endTime() const;
std::optional<double> startTime() const;
std::optional<double> endTime() const;
bool includes(double t) const;

void markChanged();
Expand Down
10 changes: 5 additions & 5 deletions src/celengine/timelinephase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
using namespace std;

TimelinePhase::TimelinePhase(Body* _body,
double _startTime,
double _endTime,
std::optional<double> _startTime,
std::optional<double> _endTime,
const ReferenceFrame::SharedConstPtr& _orbitFrame,
celestia::ephem::Orbit* _orbit,
const ReferenceFrame::SharedConstPtr& _bodyFrame,
Expand All @@ -47,15 +47,15 @@ TimelinePhase::TimelinePhase(Body* _body,
TimelinePhase::SharedConstPtr
TimelinePhase::CreateTimelinePhase(Universe& universe,
Body* body,
double startTime,
double endTime,
std::optional<double> startTime,
std::optional<double> endTime,
const ReferenceFrame::SharedConstPtr& orbitFrame,
celestia::ephem::Orbit& orbit,
const ReferenceFrame::SharedConstPtr& bodyFrame,
celestia::ephem::RotationModel& rotationModel)
{
// Validate the time range.
if (endTime <= startTime)
if (endTime.has_value() && startTime.has_value() && endTime.value() <= startTime.value())
return nullptr;

// Get the frame tree to add the new phase to. Verify that the reference frame
Expand Down
Loading