From 39151bda2e145f4b2455eaa79b52a618db135366 Mon Sep 17 00:00:00 2001 From: Emma Broman Date: Mon, 30 Mar 2020 14:41:36 +0200 Subject: [PATCH] Minor refactor --- .../autonavigation/autonavigationhandler.cpp | 4 +--- .../autonavigation/autonavigationhandler.h | 2 +- modules/autonavigation/pathcurves.cpp | 2 -- modules/autonavigation/pathsegment.cpp | 23 +++++++++---------- modules/autonavigation/pathsegment.h | 10 ++++---- .../autonavigation/rotationinterpolator.cpp | 10 ++++---- 6 files changed, 22 insertions(+), 29 deletions(-) diff --git a/modules/autonavigation/autonavigationhandler.cpp b/modules/autonavigation/autonavigationhandler.cpp index 6eafba9cec..0a64da5059 100644 --- a/modules/autonavigation/autonavigationhandler.cpp +++ b/modules/autonavigation/autonavigationhandler.cpp @@ -133,7 +133,6 @@ void AutoNavigationHandler::createPath(PathSpecification& spec) { break; } - // OBS! Would it be better to save the spec in the handler class? _stopAtTargets = spec.stopAtTargets(); // Check if we have a specified start navigation state. If so, update first segment @@ -389,8 +388,7 @@ void AutoNavigationHandler::addPause(std::optional duration) { _pathSegments.push_back(std::unique_ptr(new PathSegment(segment))); } -void AutoNavigationHandler::addSegment(Waypoint& waypoint, std::optional duration) { - +void AutoNavigationHandler::addSegment(Waypoint& waypoint, std::optional duration){ // TODO: Improve how curve types are handled const int curveType = _defaultCurveOption; diff --git a/modules/autonavigation/autonavigationhandler.h b/modules/autonavigation/autonavigationhandler.h index 5463e3d288..3f0d25882d 100644 --- a/modules/autonavigation/autonavigationhandler.h +++ b/modules/autonavigation/autonavigationhandler.h @@ -91,4 +91,4 @@ private: } // namespace openspace::autonavigation -#endif // __OPENSPACE_CORE___NAVIGATIONHANDLER___H__ +#endif // __OPENSPACE_MODULE___AUTONAVIGATIONHANDLER___H__ diff --git a/modules/autonavigation/pathcurves.cpp b/modules/autonavigation/pathcurves.cpp index 6f505075dd..9fcf4487ad 100644 --- a/modules/autonavigation/pathcurves.cpp +++ b/modules/autonavigation/pathcurves.cpp @@ -79,7 +79,6 @@ std::vector PathCurve::getPoints() { } Bezier3Curve::Bezier3Curve(const Waypoint& start, const Waypoint& end) { - // default rotation interpolation _rotationInterpolator = RotationInterpolator{ start, end, this, LookAt }; glm::dvec3 startNodePos = start.node()->worldPosition(); @@ -110,7 +109,6 @@ Bezier3Curve::Bezier3Curve(const Waypoint& start, const Waypoint& end) { // Assuming we move straigh out to point to a distance proportional to radius, angle is enough to check collision risk double cosStartAngle = glm::dot(startTangentDirection, startToEndDirection); double cosEndAngle = glm::dot(endTangentDirection, startToEndDirection); - double cosStartDir = glm::dot(startTangentDirection, endTangentDirection); //TODO: investigate suitable values, could be risky close to surface.. bool TARGET_BEHIND_STARTNODE = cosStartAngle < -0.8; diff --git a/modules/autonavigation/pathsegment.cpp b/modules/autonavigation/pathsegment.cpp index 85d8561b3f..b792f5a483 100644 --- a/modules/autonavigation/pathsegment.cpp +++ b/modules/autonavigation/pathsegment.cpp @@ -80,13 +80,13 @@ CameraPose PathSegment::traversePath(double dt) { // In case there is an error in the speed VS distance VS duration relation, // make sure that we actually reach the end point. double displacement; - if (_currentTime > _duration && !hasReachedEnd()) { + if (_progressedTime > _duration && !hasReachedEnd()) { // TODO: reach the target in a reasonable amount of time and with smooth motion LWARNING("Did not reach the target in the given duration. Moving toward the target in constant speed. TODO: fix so that this does not happen"); // TODO: fix and then remove this displacement = dt * speedAtTime(_duration - _duration * dt); } else { - displacement = dt * speedAtTime(_currentTime); + displacement = dt * speedAtTime(_progressedTime); } _traveledDistance += displacement; @@ -99,12 +99,17 @@ CameraPose PathSegment::traversePath(double dt) { //LINFO(fmt::format("u = {}", relativeDisplacement)); //LINFO(fmt::format("currentTime = {}", _currentTime)); - _currentTime += dt; + _progressedTime += dt; return interpolatedPose(relativeDisplacement); } -bool PathSegment::hasReachedEnd() { +std::string PathSegment::getCurrentAnchor() const { + bool pastHalfway = (_traveledDistance / pathLength()) > 0.5; + return (pastHalfway) ? _end.nodeDetails.identifier : _start.nodeDetails.identifier; +} + +bool PathSegment::hasReachedEnd() const { return (_traveledDistance / pathLength()) >= 1.0; } @@ -114,7 +119,7 @@ bool PathSegment::hasReachedEnd() { * Thus, we scale according to the constraint in eq. 14 in Eberly 2007 * (https://www.geometrictools.com/Documentation/MovingAlongCurveSpecifiedSpeed.pdf) */ -double PathSegment::speedAtTime(double time) { +double PathSegment::speedAtTime(double time) const { ghoul_assert(time >= 0 && time <= _duration, "Time out of range [0, duration]"); double t = time / _duration; return (pathLength() * _speedFunction.value(t)) / _speedFunction.integratedSum; @@ -127,14 +132,8 @@ CameraPose PathSegment::interpolatedPose(double u) const { return cs; } -std::string PathSegment::getCurrentAnchor() const { - bool pastHalfway = (_traveledDistance / pathLength()) > 0.5; - return (pastHalfway) ? _end.nodeDetails.identifier : _start.nodeDetails.identifier; -} - // Initialise the curve, based on the start, end state and curve type void PathSegment::initCurve() { - // in case there already is a curve object, reset the pointer. _curve.reset(); switch (_curveType) { @@ -166,7 +165,7 @@ PathSegment::SpeedFunction::SpeedFunction(double duration) { integratedSum = speedSum; } -double PathSegment::SpeedFunction::value(double t) { +double PathSegment::SpeedFunction::value(double t) const { ghoul_assert(t >= 0 && t <= 1, "Variable t out of range [0,1]"); const double t1 = 0.2; diff --git a/modules/autonavigation/pathsegment.h b/modules/autonavigation/pathsegment.h index 6f36a8392d..9b6b0d8444 100644 --- a/modules/autonavigation/pathsegment.h +++ b/modules/autonavigation/pathsegment.h @@ -50,11 +50,11 @@ public: const std::vector getControlPoints() const; // TODO: remove this debugging function CameraPose traversePath(double dt); + std::string getCurrentAnchor() const; + bool hasReachedEnd() const; - bool hasReachedEnd(); - double speedAtTime(double time); + double speedAtTime(double time) const; CameraPose interpolatedPose(double u) const; - std::string getCurrentAnchor() const; private: void initCurve(); @@ -63,7 +63,7 @@ private: struct SpeedFunction { SpeedFunction() = default; SpeedFunction(double duration); - double value(double t); + double value(double t) const; // store the sum of the function over the duration of the segment, // so we don't need to recompue it every time we access the speed @@ -81,7 +81,7 @@ private: // Playback variables double _traveledDistance = 0.0; - double _currentTime = 0.0; // Time since playback started + double _progressedTime = 0.0; // Time since playback started }; } // namespace openspace::autonavigation diff --git a/modules/autonavigation/rotationinterpolator.cpp b/modules/autonavigation/rotationinterpolator.cpp index 2417caa044..a40b592449 100644 --- a/modules/autonavigation/rotationinterpolator.cpp +++ b/modules/autonavigation/rotationinterpolator.cpp @@ -37,10 +37,8 @@ namespace openspace::autonavigation { RotationInterpolator::RotationInterpolator( const Waypoint& start, const Waypoint& end, PathCurve* curve, RotationMethod method) - : _start(start), _end(end), _method(method) -{ - _curve = curve; -} + : _start(start), _end(end), _method(method), _curve(curve) +{} glm::dquat RotationInterpolator::rotationAt(double u) { switch (_method) @@ -70,8 +68,8 @@ glm::dquat RotationInterpolator::easedSlerp(double u) { return glm::slerp(_start.rotation(), _end.rotation(), uScaled); } -// Look at start node until tStart, then turn to look at end node from tEnd -// Will overwrite rotation of navigation states! +// Look at start node until tStart, then turn to look at end node from tEnd. +// OBS! Will overwrite rotation of navigation states!! glm::dquat RotationInterpolator::lookAtInterpolator(double u) { double tStart = 0.15; double tEnd = 0.7;