diff --git a/modules/autonavigation/CMakeLists.txt b/modules/autonavigation/CMakeLists.txt index a1717b1ce0..85b7220af6 100644 --- a/modules/autonavigation/CMakeLists.txt +++ b/modules/autonavigation/CMakeLists.txt @@ -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 diff --git a/modules/autonavigation/autonavigationhandler.cpp b/modules/autonavigation/autonavigationhandler.cpp index f7b577cf4d..cc17c547d9 100644 --- a/modules/autonavigation/autonavigationhandler.cpp +++ b/modules/autonavigation/autonavigationhandler.cpp @@ -25,7 +25,7 @@ #include #include -#include +#include #include #include #include @@ -190,30 +190,20 @@ void AutoNavigationHandler::updateCamera(double deltaTime) { } } -void AutoNavigationHandler::createPath(PathInstruction& instruction) { - clearPath(); - - std::vector 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( - instruction.startPoint(), - waypointToAdd, - Path::CurveType(curveType), - instruction.duration() - ); + clearPath(); + try { + _currentPath = std::make_unique( + PathCreator::createPath(dictionary, Path::CurveType(curveType)) + ); + } + catch (const ghoul::RuntimeError& e) { + LERROR("Could not create path"); + return; + } LINFO("Successfully generated camera path"); } diff --git a/modules/autonavigation/autonavigationhandler.h b/modules/autonavigation/autonavigationhandler.h index 4af1daf40c..276ff8a1e4 100644 --- a/modules/autonavigation/autonavigationhandler.h +++ b/modules/autonavigation/autonavigationhandler.h @@ -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(); diff --git a/modules/autonavigation/autonavigationmodule.cpp b/modules/autonavigation/autonavigationmodule.cpp index 43423c1245..8e4ab34e78 100644 --- a/modules/autonavigation/autonavigationmodule.cpp +++ b/modules/autonavigation/autonavigationmodule.cpp @@ -123,13 +123,6 @@ void AutoNavigationModule::findRelevantNodes() { _relevantNodes = resultingNodes; } - -std::vector AutoNavigationModule::documentations() const { - return { - autonavigation::PathInstruction::Documentation() - }; -} - scripting::LuaLibrary AutoNavigationModule::luaLibrary() const { scripting::LuaLibrary res; res.name = "autonavigation"; diff --git a/modules/autonavigation/autonavigationmodule.h b/modules/autonavigation/autonavigationmodule.h index 59d99cfb4c..7267e01717 100644 --- a/modules/autonavigation/autonavigationmodule.h +++ b/modules/autonavigation/autonavigationmodule.h @@ -43,7 +43,6 @@ public: double minValidBoundingSphere() const; const std::vector& relevantNodes(); - std::vector documentations() const override; scripting::LuaLibrary luaLibrary() const override; private: diff --git a/modules/autonavigation/autonavigationmodule_lua.inl b/modules/autonavigation/autonavigationmodule_lua.inl index 5608ffb5df..4d8946d7fa 100644 --- a/modules/autonavigation/autonavigationmodule_lua.inl +++ b/modules/autonavigation/autonavigationmodule_lua.inl @@ -22,7 +22,6 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -#include #include #include #include @@ -154,10 +153,8 @@ int goTo(lua_State* L) { } } - PathInstruction instruction(insDict); - AutoNavigationModule* module = global::moduleEngine->module(); - 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(); - 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(); - 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(); - module->AutoNavigationHandler().createPath(instruction); + module->AutoNavigationHandler().createPath(dictionary); if (module->AutoNavigationHandler().hasCurrentPath()) { module->AutoNavigationHandler().startPath(); diff --git a/modules/autonavigation/pathinstruction.cpp b/modules/autonavigation/pathcreator.cpp similarity index 85% rename from modules/autonavigation/pathinstruction.cpp rename to modules/autonavigation/pathcreator.cpp index 0d2cd3bd02..85bf4b3808 100644 --- a/modules/autonavigation/pathinstruction.cpp +++ b/modules/autonavigation/pathcreator.cpp @@ -22,11 +22,11 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -#include +#include #include #include -#include +#include #include #include #include @@ -36,10 +36,11 @@ #include 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("autonavigation_pathinstruction"); -} - -PathInstruction::PathInstruction(const ghoul::Dictionary& dictionary) { +Path PathCreator::createPath(const ghoul::Dictionary& dictionary, Path::CurveType curveType) { const Parameters p = codegen::bake(dictionary); - _duration = p.duration; + std::optional 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 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 PathInstruction::waypoints() const { - return _waypoints; -} - -std::optional 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& relevantNodes = global::moduleEngine->module()->relevantNodes(); diff --git a/modules/autonavigation/pathinstruction.h b/modules/autonavigation/pathcreator.h similarity index 74% rename from modules/autonavigation/pathinstruction.h rename to modules/autonavigation/pathcreator.h index fa5483881c..891f26988a 100644 --- a/modules/autonavigation/pathinstruction.h +++ b/modules/autonavigation/pathcreator.h @@ -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 -#include +#include namespace openspace::autonavigation { -class PathInstruction { +class Waypoint; + +class PathCreator { public: - PathInstruction(const ghoul::Dictionary& dictionary); - - Waypoint startPoint() const; - std::vector waypoints() const; - std::optional 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 _waypoints; - std::optional _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__