Refactor path instruction class

This commit is contained in:
Emma Broman
2021-06-21 12:54:58 +02:00
parent 37483c689e
commit 91e116bc46
8 changed files with 61 additions and 101 deletions

View File

@@ -29,8 +29,8 @@ set(HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/autonavigationmodule.h
${CMAKE_CURRENT_SOURCE_DIR}/helperfunctions.h
${CMAKE_CURRENT_SOURCE_DIR}/path.h
${CMAKE_CURRENT_SOURCE_DIR}/pathcreator.h
${CMAKE_CURRENT_SOURCE_DIR}/pathcurve.h
${CMAKE_CURRENT_SOURCE_DIR}/pathinstruction.h
${CMAKE_CURRENT_SOURCE_DIR}/waypoint.h
${CMAKE_CURRENT_SOURCE_DIR}/curves/avoidcollisioncurve.h
${CMAKE_CURRENT_SOURCE_DIR}/curves/zoomoutoverviewcurve.h
@@ -43,8 +43,8 @@ set(SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/autonavigationmodule_lua.inl
${CMAKE_CURRENT_SOURCE_DIR}/helperfunctions.cpp
${CMAKE_CURRENT_SOURCE_DIR}/path.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pathcreator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pathcurve.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pathinstruction.cpp
${CMAKE_CURRENT_SOURCE_DIR}/waypoint.cpp
${CMAKE_CURRENT_SOURCE_DIR}/curves/avoidcollisioncurve.cpp
${CMAKE_CURRENT_SOURCE_DIR}/curves/zoomoutoverviewcurve.cpp

View File

@@ -25,7 +25,7 @@
#include <modules/autonavigation/autonavigationhandler.h>
#include <modules/autonavigation/helperfunctions.h>
#include <modules/autonavigation/pathinstruction.h>
#include <modules/autonavigation/pathcreator.h>
#include <openspace/engine/globals.h>
#include <openspace/interaction/navigationhandler.h>
#include <openspace/scene/scenegraphnode.h>
@@ -190,30 +190,20 @@ void AutoNavigationHandler::updateCamera(double deltaTime) {
}
}
void AutoNavigationHandler::createPath(PathInstruction& instruction) {
clearPath();
std::vector<Waypoint> waypoints = instruction.waypoints();
Waypoint waypointToAdd;
if (waypoints.empty()) {
LWARNING("No path was created from instruction. Failed creating waypoints");
return;
}
else {
// TODO: allow for an instruction to represent a list of waypoints
waypointToAdd = waypoints[0];
}
void AutoNavigationHandler::createPath(const ghoul::Dictionary& dictionary) {
// TODO: Improve how curve types are handled
const int curveType = _defaultCurveOption;
_currentPath = std::make_unique<Path>(
instruction.startPoint(),
waypointToAdd,
Path::CurveType(curveType),
instruction.duration()
);
clearPath();
try {
_currentPath = std::make_unique<Path>(
PathCreator::createPath(dictionary, Path::CurveType(curveType))
);
}
catch (const ghoul::RuntimeError& e) {
LERROR("Could not create path");
return;
}
LINFO("Successfully generated camera path");
}

View File

@@ -37,8 +37,6 @@ namespace openspace {
namespace openspace::autonavigation {
class PathInstruction;
class AutoNavigationHandler : public properties::PropertyOwner {
public:
enum StopBehavior {
@@ -58,7 +56,7 @@ public:
bool hasFinished() const;
void updateCamera(double deltaTime);
void createPath(PathInstruction& spec);
void createPath(const ghoul::Dictionary& dictionary);
void clearPath();
void startPath();
void abortPath();

View File

@@ -123,13 +123,6 @@ void AutoNavigationModule::findRelevantNodes() {
_relevantNodes = resultingNodes;
}
std::vector<documentation::Documentation> AutoNavigationModule::documentations() const {
return {
autonavigation::PathInstruction::Documentation()
};
}
scripting::LuaLibrary AutoNavigationModule::luaLibrary() const {
scripting::LuaLibrary res;
res.name = "autonavigation";

View File

@@ -43,7 +43,6 @@ public:
double minValidBoundingSphere() const;
const std::vector<SceneGraphNode*>& relevantNodes();
std::vector<documentation::Documentation> documentations() const override;
scripting::LuaLibrary luaLibrary() const override;
private:

View File

@@ -22,7 +22,6 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/autonavigation/pathinstruction.h>
#include <modules/globebrowsing/globebrowsingmodule.h>
#include <modules/globebrowsing/src/renderableglobe.h>
#include <openspace/engine/globals.h>
@@ -154,10 +153,8 @@ int goTo(lua_State* L) {
}
}
PathInstruction instruction(insDict);
AutoNavigationModule* module = global::moduleEngine->module<AutoNavigationModule>();
module->AutoNavigationHandler().createPath(instruction);
module->AutoNavigationHandler().createPath(insDict);
if (module->AutoNavigationHandler().hasCurrentPath()) {
module->AutoNavigationHandler().startPath();
@@ -193,10 +190,8 @@ int goToHeight(lua_State* L) {
}
}
PathInstruction instruction(insDict);
AutoNavigationModule* module = global::moduleEngine->module<AutoNavigationModule>();
module->AutoNavigationHandler().createPath(instruction);
module->AutoNavigationHandler().createPath(insDict);
if (module->AutoNavigationHandler().hasCurrentPath()) {
module->AutoNavigationHandler().startPath();
@@ -252,10 +247,8 @@ int goToGeo(lua_State* L) {
}
}
PathInstruction instruction(insDict);
AutoNavigationModule* module = global::moduleEngine->module<AutoNavigationModule>();
module->AutoNavigationHandler().createPath(instruction);
module->AutoNavigationHandler().createPath(insDict);
if (module->AutoNavigationHandler().hasCurrentPath()) {
module->AutoNavigationHandler().startPath();
@@ -271,10 +264,9 @@ int generatePath(lua_State* L) {
ghoul::Dictionary dictionary;
ghoul::lua::luaDictionaryFromState(L, dictionary);
PathInstruction instruction(dictionary);
AutoNavigationModule* module = global::moduleEngine->module<AutoNavigationModule>();
module->AutoNavigationHandler().createPath(instruction);
module->AutoNavigationHandler().createPath(dictionary);
if (module->AutoNavigationHandler().hasCurrentPath()) {
module->AutoNavigationHandler().startPath();

View File

@@ -22,11 +22,11 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/autonavigation/pathinstruction.h>
#include <modules/autonavigation/pathcreator.h>
#include <modules/autonavigation/autonavigationmodule.h>
#include <modules/autonavigation/helperfunctions.h>
#include <openspace/documentation/verifier.h>
#include <modules/autonavigation/waypoint.h>
#include <openspace/engine/globals.h>
#include <openspace/engine/moduleengine.h>
#include <openspace/interaction/navigationhandler.h>
@@ -36,10 +36,11 @@
#include <ghoul/logging/logmanager.h>
namespace {
constexpr const char* _loggerCat = "PathInstruction";
constexpr const char* _loggerCat = "PathCreator";
constexpr const float Epsilon = 1e-5f;
struct [[codegen::Dictionary(Instruction)]] Parameters {
// TODO: where should this documentation be?
struct [[codegen::Dictionary(PathInstruction)]] Parameters {
enum class Type {
Node,
NavigationState
@@ -79,26 +80,25 @@ namespace openspace::autonavigation {
using NavigationState = interaction::NavigationHandler::NavigationState;
documentation::Documentation PathInstruction::Documentation() {
return codegen::doc<Parameters>("autonavigation_pathinstruction");
}
PathInstruction::PathInstruction(const ghoul::Dictionary& dictionary) {
Path PathCreator::createPath(const ghoul::Dictionary& dictionary, Path::CurveType curveType) {
const Parameters p = codegen::bake<Parameters>(dictionary);
_duration = p.duration;
std::optional<float> duration = p.duration;
bool hasStart = p.startState.has_value();
_startPoint = hasStart ? Waypoint(p.startState.value()) : waypointFromCamera();
Waypoint startPoint = hasStart ? Waypoint(p.startState.value()) : waypointFromCamera();
// TODO: also handle curve type here
std::vector<Waypoint> waypoints;
switch (p.type) {
case Parameters::Type::NavigationState: {
if (!p.navigationState.has_value()) {
throw ghoul::RuntimeError("A navigation state is required");
}
NavigationState navigationState = NavigationState(p.navigationState.value());
_waypoints = { Waypoint(navigationState) };
const NavigationState navigationState = NavigationState(p.navigationState.value());
waypoints = { Waypoint(navigationState) };
break;
}
case Parameters::Type::Node: {
@@ -106,7 +106,7 @@ PathInstruction::PathInstruction(const ghoul::Dictionary& dictionary) {
throw ghoul::RuntimeError("A target node is required");
}
std::string nodeIdentifier = p.target.value();
const std::string nodeIdentifier = p.target.value();
const SceneGraphNode* targetNode = sceneGraphNode(nodeIdentifier);
if (!targetNode) {
@@ -122,30 +122,22 @@ PathInstruction::PathInstruction(const ghoul::Dictionary& dictionary) {
p.useTargetUpDirection.value_or(false)
};
_waypoints = { computeDefaultWaypoint(info) };
waypoints = { computeDefaultWaypoint(info, startPoint) };
break;
}
default: {
LERROR(fmt::format("Uknown instruciton type: {}", p.type));
throw ghoul::MissingCaseException();
break;
}
}
// TODO: allow for an instruction to represent a list of waypoints
Waypoint waypointToAdd = waypoints[0];
return Path(startPoint, waypointToAdd, curveType, duration);
}
Waypoint PathInstruction::startPoint() const {
return _startPoint;
}
std::vector<Waypoint> PathInstruction::waypoints() const {
return _waypoints;
}
std::optional<double> PathInstruction::duration() const {
return _duration;
}
Waypoint PathInstruction::waypointFromCamera() const {
Waypoint PathCreator::waypointFromCamera() {
Camera* camera = global::navigationHandler->camera();
const glm::dvec3 pos = camera->positionVec3();
const glm::dquat rot = camera->rotationQuaternion();
@@ -153,9 +145,9 @@ Waypoint PathInstruction::waypointFromCamera() const {
return Waypoint{ pos, rot, node };
}
// OBS! The desired default waypoint may vary between curve types.
// TODO: let the curves update the default position if no exact position is required
Waypoint PathInstruction::computeDefaultWaypoint(const NodeInfo& info) const {
Waypoint PathCreator::computeDefaultWaypoint(const NodeInfo& info,
const Waypoint& startPoint)
{
const SceneGraphNode* targetNode = sceneGraphNode(info.identifier);
if (!targetNode) {
LERROR(fmt::format("Could not find target node '{}'", info.identifier));
@@ -188,7 +180,7 @@ Waypoint PathInstruction::computeDefaultWaypoint(const NodeInfo& info) const {
else {
// Go to a point that is being lit up by the sun, slightly offsetted from sun
// direction
const glm::dvec3 prevPos = _startPoint.position();
const glm::dvec3 prevPos = startPoint.position();
const glm::dvec3 targetToPrev = prevPos - nodePos;
const glm::dvec3 targetToSun = sunPos - nodePos;
@@ -221,8 +213,7 @@ Waypoint PathInstruction::computeDefaultWaypoint(const NodeInfo& info) const {
return Waypoint(targetPos, targetRot, info.identifier);
}
// Test if the node lies within a given proximity radius of any relevant node in the scene
SceneGraphNode* PathInstruction::findNodeNearTarget(const SceneGraphNode* node) const {
SceneGraphNode* PathCreator::findNodeNearTarget(const SceneGraphNode* node) {
const std::vector<SceneGraphNode*>& relevantNodes =
global::moduleEngine->module<AutoNavigationModule>()->relevantNodes();

View File

@@ -22,23 +22,20 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifndef __OPENSPACE_MODULE_AUTONAVIGATION___PATHINSTRUCTION___H__
#define __OPENSPACE_MODULE_AUTONAVIGATION___PATHINSTRUCTION___H__
#ifndef __OPENSPACE_MODULE_AUTONAVIGATION___PATHCREATOR___H__
#define __OPENSPACE_MODULE_AUTONAVIGATION___PATHCREATOR___H__
#include <modules/autonavigation/waypoint.h>
#include <optional>
#include <modules/autonavigation/path.h>
namespace openspace::autonavigation {
class PathInstruction {
class Waypoint;
class PathCreator {
public:
PathInstruction(const ghoul::Dictionary& dictionary);
Waypoint startPoint() const;
std::vector<Waypoint> waypoints() const;
std::optional<double> duration() const;
static documentation::Documentation Documentation();
// Create a path from a dictionary containing the instruction
static Path createPath(const ghoul::Dictionary& dictionary,
Path::CurveType curveType);
private:
struct NodeInfo {
@@ -48,15 +45,15 @@ private:
bool useTargetUpDirection;
};
Waypoint waypointFromCamera() const;
Waypoint computeDefaultWaypoint(const NodeInfo& info) const;
SceneGraphNode* findNodeNearTarget(const SceneGraphNode* node) const;
static Waypoint waypointFromCamera();
static Waypoint computeDefaultWaypoint(const NodeInfo& info,
const Waypoint& startPoint);
Waypoint _startPoint;
std::vector<Waypoint> _waypoints;
std::optional<double> _duration;
// Test if the node lies within a given proximity radius of any relevant node
// in the scene
static SceneGraphNode* findNodeNearTarget(const SceneGraphNode* node);
};
} // namespace openspace::autonavigation
#endif // __OPENSPACE_MODULE_AUTONAVIGATION___PATHINSTRUCTION___H__
#endif // __OPENSPACE_MODULE_AUTONAVIGATION___PATHCREATOR___H__