Split path curve code into separate file

This commit is contained in:
Emma Broman
2020-01-16 10:30:13 -05:00
parent 83037f8c47
commit 6a6d2ca973
5 changed files with 201 additions and 121 deletions
+2
View File
@@ -30,6 +30,7 @@ set(HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/helperfunctions.h
${CMAKE_CURRENT_SOURCE_DIR}/pathspecification.h
${CMAKE_CURRENT_SOURCE_DIR}/pathsegment.h
${CMAKE_CURRENT_SOURCE_DIR}/pathcurves.h
)
source_group("Header Files" FILES ${HEADER_FILES})
@@ -40,6 +41,7 @@ set(SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/helperfunctions.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pathspecification.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pathsegment.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pathcurves.cpp
)
source_group("Source Files" FILES ${SOURCE_FILES})
+126
View File
@@ -0,0 +1,126 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2019 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/autonavigation/pathcurves.h>
#include <modules/autonavigation/helperfunctions.h>
#include <modules/autonavigation/pathsegment.h>
#include <openspace/query/query.h>
#include <openspace/scene/scenegraphnode.h>
#include <ghoul/Misc/interpolator.h>
#include <ghoul/logging/logmanager.h>
namespace {
constexpr const char* _loggerCat = "PathCurve";
} // namespace
namespace openspace::autonavigation {
PathCurve::~PathCurve() {}
BezierCurve::BezierCurve(CameraState& start, CameraState& end) {
glm::dvec3 startNodePos = sceneGraphNode(start.referenceNode)->worldPosition();
glm::dvec3 endNodePos = sceneGraphNode(start.referenceNode)->worldPosition();
// vectors pointing away from target nodes
glm::dvec3 startDirection = start.position - startNodePos;
glm::dvec3 endDirection = end.position - endNodePos;
_points.push_back(start.position);
_points.push_back(start.position + 10.0 * startDirection);
_points.push_back(end.position + 10.0 * endDirection);
_points.push_back(end.position);
}
glm::dvec3 BezierCurve::interpolate(double t) {
return interpolator::cubicBezier(t,
_points[0], _points[1], _points[2], _points[3]);
}
Bezier2Curve::Bezier2Curve(CameraState& start, CameraState& end) {
// START:
glm::dvec3 startNodePos = sceneGraphNode(start.referenceNode)->worldPosition();
glm::dvec3 startDirection = start.position - startNodePos;
// END:
glm::dvec3 endNodePos = sceneGraphNode(end.referenceNode)->worldPosition();
glm::dvec3 endDirection = end.position - endNodePos;
// MIDDLE: one knot and two control points parallell to target nodes
glm::dvec3 AB = endNodePos - startNodePos;
glm::dvec3 C = normalize(startDirection + endDirection);
glm::dvec3 CparAB = glm::dot(C, normalize(AB))* normalize(AB);
glm::dvec3 CortAB = normalize(C - CparAB);
double d = length(AB);
// TODO: set points that actually look good
_points.push_back(start.position);
_points.push_back(start.position + 2.0 * startDirection);
_points.push_back(start.position + 1.5 * d * CortAB);
_points.push_back(start.position + 1.5 * d * CortAB + 0.5 * AB);
_points.push_back(end.position + 1.5 * d * CortAB);
_points.push_back(end.position + 2.0 * endDirection);
_points.push_back(end.position);
}
glm::dvec3 Bezier2Curve::interpolate(double t) {
return interpolator::piecewiseCubicBezier(t, _points);
}
LinearCurve::LinearCurve(CameraState& start, CameraState& end) {
_points.push_back(start.position);
_points.push_back(end.position);
}
glm::dvec3 LinearCurve::interpolate(double t) {
return ghoul::interpolateLinear(t, _points[0], _points[1]);
}
Linear2Curve::Linear2Curve(CameraState& start, CameraState& end) {
// START:
glm::dvec3 startNodePos = sceneGraphNode(start.referenceNode)->worldPosition();
glm::dvec3 startDirection = start.position - startNodePos;
// END:
glm::dvec3 endNodePos = sceneGraphNode(end.referenceNode)->worldPosition();
glm::dvec3 endDirection = end.position - endNodePos;
// MIDDLE:
glm::dvec3 AB = endNodePos - startNodePos;
glm::dvec3 C = normalize(startDirection + endDirection);
glm::dvec3 CparAB = glm::dot(C, normalize(AB))* normalize(AB);
glm::dvec3 CortAB = normalize(C - CparAB);
double d = length(AB);
_points.push_back(start.position);
_points.push_back(start.position + 2.0 * d * CortAB + 0.5 * AB); //TODO: use scale instead of 2.0
_points.push_back(end.position);
}
glm::dvec3 Linear2Curve::interpolate(double t) {
return interpolator::piecewiseLinear(t, _points);
}
} // namespace openspace::autonavigation
+70
View File
@@ -0,0 +1,70 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2019 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifndef __OPENSPACE_MODULE_AUTONAVIGATION___PATHCURVE___H__
#define __OPENSPACE_MODULE_AUTONAVIGATION___PATHCURVE___H__
#include <ghoul/glm.h>
#include <vector>
namespace openspace::autonavigation {
struct CameraState;
class PathCurve {
public:
virtual ~PathCurve() = 0;
virtual glm::dvec3 interpolate(double t) = 0;
protected:
// the points used for creating the curve (e.g. control points of a Bezier curve)
std::vector<glm::dvec3> _points;
};
class BezierCurve : public PathCurve {
public :
BezierCurve(CameraState& start, CameraState& end);
glm::dvec3 interpolate(double t);
};
class Bezier2Curve : public PathCurve {
public:
Bezier2Curve(CameraState& start, CameraState& end);
glm::dvec3 interpolate(double t);
};
class LinearCurve : public PathCurve {
public:
LinearCurve(CameraState& start, CameraState& end);
glm::dvec3 interpolate(double t);
};
class Linear2Curve : public PathCurve {
public:
Linear2Curve(CameraState& start, CameraState& end);
glm::dvec3 interpolate(double t);
};
} // namespace openspace::autonavigation
#endif // __OPENSPACE_MODULE_AUTONAVIGATION___PATHCURVE___H__
+2 -88
View File
@@ -25,6 +25,7 @@
#include <modules/autonavigation/pathsegment.h>
#include <modules/autonavigation/helperfunctions.h>
#include <modules/autonavigation/pathcurves.h>
#include <openspace/engine/globals.h>
#include <openspace/interaction/navigationhandler.h>
#include <openspace/query/query.h>
@@ -39,93 +40,6 @@ namespace {
namespace openspace::autonavigation {
// TODO: move all these later --------------------
PathCurve::~PathCurve() {}
BezierCurve::BezierCurve(CameraState& start, CameraState& end) {
glm::dvec3 startNodePos = sceneGraphNode(start.referenceNode)->worldPosition();
glm::dvec3 endNodePos = sceneGraphNode(start.referenceNode)->worldPosition();
// vectors pointing away from target nodes
glm::dvec3 startDirection = start.position - startNodePos;
glm::dvec3 endDirection = end.position - endNodePos;
_points.push_back(start.position);
_points.push_back(start.position + 10.0 * startDirection);
_points.push_back(end.position + 10.0 * endDirection);
_points.push_back(end.position);
}
glm::dvec3 BezierCurve::interpolate(double t) {
return interpolator::cubicBezier(t,
_points[0], _points[1], _points[2], _points[3]);
}
Bezier2Curve::Bezier2Curve(CameraState& start, CameraState& end) {
// START:
glm::dvec3 startNodePos = sceneGraphNode(start.referenceNode)->worldPosition();
glm::dvec3 startDirection = start.position - startNodePos;
// END:
glm::dvec3 endNodePos = sceneGraphNode(end.referenceNode)->worldPosition();
glm::dvec3 endDirection = end.position - endNodePos;
// MIDDLE: one knot and two control points parallell to target nodes
glm::dvec3 AB = endNodePos - startNodePos;
glm::dvec3 C = normalize(startDirection + endDirection);
glm::dvec3 CparAB = glm::dot(C, normalize(AB))* normalize(AB);
glm::dvec3 CortAB = normalize(C - CparAB);
double d = length(AB);
// TODO: set points that actually look good
_points.push_back(start.position);
_points.push_back(start.position + 2.0 * startDirection);
_points.push_back(start.position + 1.5 * d * CortAB);
_points.push_back(start.position + 1.5 * d * CortAB + 0.5 * AB);
_points.push_back(end.position + 1.5 * d * CortAB);
_points.push_back(end.position + 2.0 * endDirection);
_points.push_back(end.position);
}
glm::dvec3 Bezier2Curve::interpolate(double t) {
return interpolator::piecewiseCubicBezier(t, _points);
}
LinearCurve::LinearCurve(CameraState& start, CameraState& end) {
_points.push_back(start.position);
_points.push_back(end.position);
}
glm::dvec3 LinearCurve::interpolate(double t) {
return ghoul::interpolateLinear(t, _points[0], _points[1]);
}
Linear2Curve::Linear2Curve(CameraState& start, CameraState& end) {
// START:
glm::dvec3 startNodePos = sceneGraphNode(start.referenceNode)->worldPosition();
glm::dvec3 startDirection = start.position - startNodePos;
// END:
glm::dvec3 endNodePos = sceneGraphNode(end.referenceNode)->worldPosition();
glm::dvec3 endDirection = end.position - endNodePos;
// MIDDLE:
glm::dvec3 AB = endNodePos - startNodePos;
glm::dvec3 C = normalize(startDirection + endDirection);
glm::dvec3 CparAB = glm::dot(C, normalize(AB))* normalize(AB);
glm::dvec3 CortAB = normalize(C - CparAB);
double d = length(AB);
_points.push_back(start.position);
_points.push_back(start.position + 2.0 * d * CortAB + 0.5 * AB); //TODO: use scale instead of 2.0
_points.push_back(end.position);
}
glm::dvec3 Linear2Curve::interpolate(double t) {
return interpolator::piecewiseLinear(t, _points);
}
PathSegment::PathSegment(
CameraState start, CameraState end, double startTime, CurveType type)
: _start(start), _end(end), _startTime(startTime), _curveType(type)
@@ -169,7 +83,7 @@ const double PathSegment::startTime() const { return _startTime; }
const glm::vec3 PathSegment::getPositionAt(double t) const {
t = easingfunctions::cubicEaseInOut(t);
return _curve.get()->interpolate(t);
return _curve->interpolate(t);
}
const glm::dquat PathSegment::getRotationAt(double t) const {
+1 -33
View File
@@ -43,39 +43,7 @@ enum CurveType {
Linear2
};
// TODO: move to their own file
class PathCurve {
public:
virtual ~PathCurve() = 0;
virtual glm::dvec3 interpolate(double t) = 0;
protected:
// the points used for creating the curve (e.g. control points of a Bezier curve)
std::vector<glm::dvec3> _points;
};
class BezierCurve : public PathCurve {
public :
BezierCurve(CameraState& start, CameraState& end);
glm::dvec3 interpolate(double t);
};
class Bezier2Curve : public PathCurve {
public:
Bezier2Curve(CameraState& start, CameraState& end);
glm::dvec3 interpolate(double t);
};
class LinearCurve : public PathCurve {
public:
LinearCurve(CameraState& start, CameraState& end);
glm::dvec3 interpolate(double t);
};
class Linear2Curve : public PathCurve {
public:
Linear2Curve(CameraState& start, CameraState& end);
glm::dvec3 interpolate(double t);
};
class PathCurve;
class PathSegment {
public: