From 378223e28103a8af6e56d0246765691ec26cec0e Mon Sep 17 00:00:00 2001 From: Michael Huang Date: Sat, 2 Jul 2016 13:17:22 -0500 Subject: [PATCH 1/9] fix build for debug --- projects/g++/Makefile | 4 ++-- src/Menge/MengeCore/Agents/PrefVelocity.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/projects/g++/Makefile b/projects/g++/Makefile index edcd670c..c869fb4f 100644 --- a/projects/g++/Makefile +++ b/projects/g++/Makefile @@ -9,12 +9,12 @@ $(DIRS): mkdir $@ -release: +release: $(DIRS) ( cd build/release && cmake -DCMAKE_BUILD_TYPE=release ../.. && $(MAKE) --no-print-directory && make --no-print-directory install) # ctags -R --language-force=c++ *.* # ctags -eR --language-force=c++ *.* -debug: +debug: $(DIRS) ( cd build/debug && cmake -DCMAKE_BUILD_TYPE=debug ../.. && $(MAKE) --no-print-directory && make --no-print-directory install) # ctags -R --language-force=c++ *.* # ctags -eR --language-force=c++ *.* diff --git a/src/Menge/MengeCore/Agents/PrefVelocity.cpp b/src/Menge/MengeCore/Agents/PrefVelocity.cpp index e95b7bdb..1c507a14 100644 --- a/src/Menge/MengeCore/Agents/PrefVelocity.cpp +++ b/src/Menge/MengeCore/Agents/PrefVelocity.cpp @@ -3,7 +3,7 @@ License Menge -Copyright © and trademark ™ 2012-14 University of North Carolina at Chapel Hill. +Copyright � and trademark � 2012-14 University of North Carolina at Chapel Hill. All rights reserved. Permission to use, copy, modify, and distribute this software and its documentation @@ -89,11 +89,11 @@ namespace Menge { _preferred = preferred; #if _DEBUG const float SPAN_EPS = -1.e-4f; - #endif assert( det( right, left ) >= SPAN_EPS ); assert( det( right, preferred )>= SPAN_EPS ); assert( det( preferred, left ) >= SPAN_EPS ); + #endif } } // namespace Agents -} // namespace Menge \ No newline at end of file +} // namespace Menge From 60eae5691a3560249b101e7c6ed54a6c96eb8231 Mon Sep 17 00:00:00 2001 From: Michael Huang Date: Sat, 2 Jul 2016 23:41:23 -0500 Subject: [PATCH 2/9] add access to simulator interface fix type conflict with gazebo --- src/Menge/MengeCore/Runtime/SimSystem.h | 4 ++++ src/Menge/include/macros.h | 13 ++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Menge/MengeCore/Runtime/SimSystem.h b/src/Menge/MengeCore/Runtime/SimSystem.h index 3347cdd7..623d85e2 100644 --- a/src/Menge/MengeCore/Runtime/SimSystem.h +++ b/src/Menge/MengeCore/Runtime/SimSystem.h @@ -242,6 +242,10 @@ namespace Menge { * @returns The number of VisAgents updated by the system. */ size_t getAgentCount() const; + const Menge::Agents::SimulatorInterface* getSimulator() const + { + return _sim; + } protected: diff --git a/src/Menge/include/macros.h b/src/Menge/include/macros.h index 44071e92..0179c10b 100644 --- a/src/Menge/include/macros.h +++ b/src/Menge/include/macros.h @@ -21,7 +21,6 @@ #include #include - //FIXME: this should be handle automatically by cmake #define sprintf_s(buffer, buffer_size, stringbuffer, ...) (snprintf(buffer,buffer_size ,stringbuffer, __VA_ARGS__)) #define sscanf_s(buffer, buffer_size, stringbuffer, ...) (snprintf(buffer,buffer_size ,stringbuffer, __VA_ARGS__)) @@ -30,13 +29,17 @@ #ifndef DWORD #define WINAPI -typedef unsigned long DWORD; +//typedef unsigned long DWORD; +typedef uint32_t DWORD; typedef short WCHAR; typedef void * HANDLE; //#define MAX_PATH PATH_MAX -typedef unsigned char BYTE; -typedef unsigned short WORD; -typedef unsigned int BOOL; +typedef uint8_t BYTE; +//typedef unsigned char BYTE; +//typedef unsigned short WORD; +typedef uint16_t WORD; +typedef int32_t BOOL; +//typedef unsigned int BOOL; #endif #ifdef UNICODE From 9118aadc6816d2318704060757b501336eed49ec Mon Sep 17 00:00:00 2001 From: Michael Huang Date: Tue, 26 Jul 2016 15:24:45 -0500 Subject: [PATCH 3/9] add new scenes, follow state and conditions --- examples/core/4square/4squareS.xml | 4 +- examples/core/outside.xml | 9 + examples/core/outside/outsideB.xml | 193 ++++++++++++++++++ examples/core/outside/outsideS.xml | 105 ++++++++++ examples/core/outside/outsideV.xml | 16 ++ examples/core/random.xml | 10 + examples/core/random/randomB.xml | 121 +++++++++++ examples/core/random/randomS.xml | 92 +++++++++ examples/core/random/randomV.xml | 16 ++ .../core/{tradeshow => random}/tradeshow.nav | 0 .../MengeCore/BFSM/Actions/ActionDatabase.cpp | 7 +- .../MengeCore/BFSM/Actions/IncPropAction.cpp | 58 ++++++ .../MengeCore/BFSM/Actions/IncPropAction.h | 140 +++++++++++++ src/Menge/MengeCore/BFSM/FSM.cpp | 6 +- src/Menge/MengeCore/BFSM/FSM.h | 4 +- .../MengeCore/BFSM/Transitions/CondFollow.cpp | 108 ++++++++++ .../MengeCore/BFSM/Transitions/CondFollow.h | 172 ++++++++++++++++ .../BFSM/Transitions/CondProximity.cpp | 102 +++++++++ .../BFSM/Transitions/CondProximity.h | 180 ++++++++++++++++ .../MengeCore/BFSM/Transitions/CondVia.cpp | 108 ++++++++++ .../MengeCore/BFSM/Transitions/CondVia.h | 170 +++++++++++++++ .../BFSM/Transitions/ConditionDatabase.cpp | 11 +- .../MengeCore/Runtime/SimulatorDBEntry.cpp | 7 +- .../MengeCore/Runtime/SimulatorDBEntry.h | 11 +- 24 files changed, 1630 insertions(+), 20 deletions(-) create mode 100644 examples/core/outside.xml create mode 100644 examples/core/outside/outsideB.xml create mode 100644 examples/core/outside/outsideS.xml create mode 100644 examples/core/outside/outsideV.xml create mode 100644 examples/core/random.xml create mode 100644 examples/core/random/randomB.xml create mode 100644 examples/core/random/randomS.xml create mode 100644 examples/core/random/randomV.xml rename examples/core/{tradeshow => random}/tradeshow.nav (100%) create mode 100644 src/Menge/MengeCore/BFSM/Actions/IncPropAction.cpp create mode 100644 src/Menge/MengeCore/BFSM/Actions/IncPropAction.h create mode 100644 src/Menge/MengeCore/BFSM/Transitions/CondFollow.cpp create mode 100644 src/Menge/MengeCore/BFSM/Transitions/CondFollow.h create mode 100644 src/Menge/MengeCore/BFSM/Transitions/CondProximity.cpp create mode 100644 src/Menge/MengeCore/BFSM/Transitions/CondProximity.h create mode 100644 src/Menge/MengeCore/BFSM/Transitions/CondVia.cpp create mode 100644 src/Menge/MengeCore/BFSM/Transitions/CondVia.h diff --git a/examples/core/4square/4squareS.xml b/examples/core/4square/4squareS.xml index af5deea8..8218fb62 100644 --- a/examples/core/4square/4squareS.xml +++ b/examples/core/4square/4squareS.xml @@ -3,7 +3,7 @@ - + @@ -108,4 +108,4 @@ - \ No newline at end of file + diff --git a/examples/core/outside.xml b/examples/core/outside.xml new file mode 100644 index 00000000..f88d23cb --- /dev/null +++ b/examples/core/outside.xml @@ -0,0 +1,9 @@ + + + diff --git a/examples/core/outside/outsideB.xml b/examples/core/outside/outsideB.xml new file mode 100644 index 00000000..9a874e1d --- /dev/null +++ b/examples/core/outside/outsideB.xml @@ -0,0 +1,193 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/core/outside/outsideS.xml b/examples/core/outside/outsideS.xml new file mode 100644 index 00000000..26428a22 --- /dev/null +++ b/examples/core/outside/outsideS.xml @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/core/outside/outsideV.xml b/examples/core/outside/outsideV.xml new file mode 100644 index 00000000..036c9088 --- /dev/null +++ b/examples/core/outside/outsideV.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/examples/core/random.xml b/examples/core/random.xml new file mode 100644 index 00000000..b0b8cce5 --- /dev/null +++ b/examples/core/random.xml @@ -0,0 +1,10 @@ + + + + diff --git a/examples/core/random/randomB.xml b/examples/core/random/randomB.xml new file mode 100644 index 00000000..2a5ef354 --- /dev/null +++ b/examples/core/random/randomB.xml @@ -0,0 +1,121 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/core/random/randomS.xml b/examples/core/random/randomS.xml new file mode 100644 index 00000000..f67baa4c --- /dev/null +++ b/examples/core/random/randomS.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/core/random/randomV.xml b/examples/core/random/randomV.xml new file mode 100644 index 00000000..036c9088 --- /dev/null +++ b/examples/core/random/randomV.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/examples/core/tradeshow/tradeshow.nav b/examples/core/random/tradeshow.nav similarity index 100% rename from examples/core/tradeshow/tradeshow.nav rename to examples/core/random/tradeshow.nav diff --git a/src/Menge/MengeCore/BFSM/Actions/ActionDatabase.cpp b/src/Menge/MengeCore/BFSM/Actions/ActionDatabase.cpp index 48d529e2..9389a17a 100644 --- a/src/Menge/MengeCore/BFSM/Actions/ActionDatabase.cpp +++ b/src/Menge/MengeCore/BFSM/Actions/ActionDatabase.cpp @@ -3,7 +3,7 @@ License Menge -Copyright © and trademark ™ 2012-14 University of North Carolina at Chapel Hill. +Copyright � and trademark � 2012-14 University of North Carolina at Chapel Hill. All rights reserved. Permission to use, copy, modify, and distribute this software and its documentation @@ -40,7 +40,7 @@ Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu #include "Actions/ObstacleAction.h" #include "Actions/PropertyAction.h" #include "Actions/TeleportAction.h" - +#include "Actions/IncPropAction.h" #ifndef DOXYGEN_SHOULD_SKIP_THIS namespace Menge { @@ -57,7 +57,8 @@ namespace Menge { addFactory( new BFSM::OffsetPropertyActFactory() ); addFactory( new BFSM::ScalePropertyActFactory() ); addFactory( new BFSM::TeleportActFactory() ); + addFactory( new BFSM::IncPropActFactory() ); } } // namespace Menge -#endif // DOXYGEN_SHOULD_SKIP_THIS \ No newline at end of file +#endif // DOXYGEN_SHOULD_SKIP_THIS diff --git a/src/Menge/MengeCore/BFSM/Actions/IncPropAction.cpp b/src/Menge/MengeCore/BFSM/Actions/IncPropAction.cpp new file mode 100644 index 00000000..b087192c --- /dev/null +++ b/src/Menge/MengeCore/BFSM/Actions/IncPropAction.cpp @@ -0,0 +1,58 @@ + + +#include "Actions/IncPropAction.h" +#include "BaseAgent.h" + +namespace Menge { + + namespace BFSM { + + ///////////////////////////////////////////////////////////////////// + // Implementation of IncPropAction + ///////////////////////////////////////////////////////////////////// + + IncPropAction::IncPropAction():Action(),_increment(0.0f) { + } + + ///////////////////////////////////////////////////////////////////// + + IncPropAction::~IncPropAction() { + + } + + ///////////////////////////////////////////////////////////////////// + + void IncPropAction::onEnter( Agents::BaseAgent * agent ) { + if(agent->_radius<30)//TODO replace with property spec from xml + agent->_radius = agent->_radius+this->_increment; + } + + ///////////////////////////////////////////////////////////////////// + // Implementation of IncPropActFactory + ///////////////////////////////////////////////////////////////////// + + IncPropActFactory::IncPropActFactory():ActionFactory() { + _IncrementID = _attrSet.addFloatAttribute("increment", true, 0.0f); + } + + ///////////////////////////////////////////////////////////////////// + + Action * IncPropActFactory::instance() const { + return new IncPropAction(); + } + + ///////////////////////////////////////////////////////////////////// + + bool IncPropActFactory::setFromXML( Action * action, TiXmlElement * node, const std::string & behaveFldr ) const { + IncPropAction * IncAction = dynamic_cast< IncPropAction * >( action ); + assert( action != 0x0 && "Trying to set Inc action properties on an incompatible object" ); + + if ( ! ActionFactory::setFromXML( action, node, behaveFldr ) ) return false; + // Override undoing the growing regardless of what was set in the XML + IncAction->_undoOnExit = false; + IncAction->_increment = _attrSet.getFloat(_IncrementID); + return true; + } + + } // namespace BFSM +} // namespace Menge diff --git a/src/Menge/MengeCore/BFSM/Actions/IncPropAction.h b/src/Menge/MengeCore/BFSM/Actions/IncPropAction.h new file mode 100644 index 00000000..50ba8843 --- /dev/null +++ b/src/Menge/MengeCore/BFSM/Actions/IncPropAction.h @@ -0,0 +1,140 @@ + + +/*! + * @file IncPropAction.h + * @brief Defines a BFSM action that causes agents radius to increase + */ + +#ifndef __INCPROP_ACTION_H__ +#define __INCPROP_ACTION_H__ + +#include "CoreConfig.h" +#include "Actions/Action.h" +#include "Actions/ActionFactory.h" +#include "fsmCommon.h" + +namespace Menge { + + // forward declaration + + namespace Agents { + class BaseAgent; + } + + namespace BFSM { + // forward declaration + class IncPropActFactory; + + /*! + * @brief Causes the agents to grow bigger radius. + * + */ + class MENGE_API IncPropAction : public Action { + public: + /*! + * @brief Constructor + * + */ + IncPropAction(); + + /*! + * @brief Virtual destructor. + */ + virtual ~IncPropAction(); + + /*! + * @brief Upon entering the state, this is called -- it is the main work + * of the action. + * + * @param agent The agent to act on. + */ + virtual void onEnter( Agents::BaseAgent * agent ); + + friend class IncPropActFactory; + protected: + + /*! + * @brief The work to do upon state exit. + * + * @param agent The agent to act on. + */ + virtual void leaveAction( Agents::BaseAgent * agent ) {} + + //variables here + float _increment; + }; + + /*! + * @brief Factory for the IncPropAction. + */ + class MENGE_API IncPropActFactory : public ActionFactory { + public: + /*! + * @brief Constructor. + */ + IncPropActFactory(); + + /*! + * @brief The name of the action. + * + * The action's name must be unique among all registered actions. + * Each action factory must override this function. + * + * @returns A string containing the unique action name. + */ + virtual const char * name() const { return "increment_property"; } + + /*! + * @brief A description of the action. + * + * Each action factory must override this function. + * + * @returns A string containing the action description. + */ + virtual const char * description() const { + return "increments a property of an agent by a given step size"; + }; + + protected: + /*! + * @brief Create an instance of this class's action. + * + * All ActionFactory sub-classes must override this by creating (on the heap) + * a new instance of its corresponding action type. The various field values + * of the instance will be set in a subsequent call to ActionFactory::setFromXML. + * The caller of this function takes ownership of the memory. + * + * @returns A pointer to a newly instantiated Action class. + */ + Action * instance() const; + + /*! + * @brief Given a pointer to an Action instance, sets the appropriate fields + * from the provided XML node. + * + * It is assumed that the value of the `type` attribute is this Action's type. + * (i.e. ActionFactory::thisFactory has already been called and returned true.) + * If sub-classes of ActionFactory introduce *new* Action parameters, then the + * sub-class should override this method but explicitly call the parent class's + * version. + * + * @param action A pointer to the action whose attributes are to be set. + * @param node The XML node containing the action attributes. + * @param behaveFldr The path to the behavior file. If the action references + * resources in the file system, it should be defined relative + * to the behavior file location. This is the folder containing + * that path. + * @returns A boolean reporting success (true) or failure (false). + */ + virtual bool setFromXML( Action * action, TiXmlElement * node, const std::string & behaveFldr ) const; + size_t _IncrementID; + /*! + * @brief The identifier for the "exit_reset" boolean attribute. + */ + + }; + + } // namespace BFSM + +} // +#endif // __TELEPORT_ACTION_H__ diff --git a/src/Menge/MengeCore/BFSM/FSM.cpp b/src/Menge/MengeCore/BFSM/FSM.cpp index 7bcf69d5..ff3bb9bb 100644 --- a/src/Menge/MengeCore/BFSM/FSM.cpp +++ b/src/Menge/MengeCore/BFSM/FSM.cpp @@ -202,7 +202,7 @@ namespace Menge { ///////////////////////////////////////////////////////////////////// - const Goal * FSM::getGoal( size_t goalSet, size_t goalID ) { + Goal * FSM::getGoal( size_t goalSet, size_t goalID ) { if ( _goalSets.find( goalSet ) == _goalSets.end() ) { return 0x0; } @@ -211,7 +211,7 @@ namespace Menge { ///////////////////////////////////////////////////////////////////// - const GoalSet * FSM::getGoalSet( size_t goalSetID ) { + GoalSet * FSM::getGoalSet( size_t goalSetID ) { if ( _goalSets.find( goalSetID ) == _goalSets.end() ) { return 0x0; } @@ -314,4 +314,4 @@ namespace Menge { return ctx; } } // namespace BFSM -} // namespace Menge \ No newline at end of file +} // namespace Menge diff --git a/src/Menge/MengeCore/BFSM/FSM.h b/src/Menge/MengeCore/BFSM/FSM.h index fe5e0d75..fd6255ea 100644 --- a/src/Menge/MengeCore/BFSM/FSM.h +++ b/src/Menge/MengeCore/BFSM/FSM.h @@ -239,7 +239,7 @@ namespace Menge { * @returns A pointer to the corresponding goal. If no such goal exists * NULL is returned. */ - const Goal * getGoal( size_t goalSet, size_t goalID ); + Goal * getGoal( size_t goalSet, size_t goalID ); /*! * @brief Retrives the given goal set. @@ -248,7 +248,7 @@ namespace Menge { * @returns A pointer to the corresponding goal set. If no such goal set * exists, NULL is returned. */ - const GoalSet * getGoalSet( size_t goalSetID ); + GoalSet * getGoalSet( size_t goalSetID ); /*! * @brief Update the fsm state by one time step diff --git a/src/Menge/MengeCore/BFSM/Transitions/CondFollow.cpp b/src/Menge/MengeCore/BFSM/Transitions/CondFollow.cpp new file mode 100644 index 00000000..6997f013 --- /dev/null +++ b/src/Menge/MengeCore/BFSM/Transitions/CondFollow.cpp @@ -0,0 +1,108 @@ +/* + * CondQuad.cpp + * + * Created on: Jul 18, 2016 + * Author: michael + */ + +#include "CondFollow.h" + +#include "Core.h" +#include "BaseAgent.h" +#include +//TODO:include file with _sim reference(get agents ability) +#include "SimSystem.h" +#include "SimulatorDBEntry.h" +#include "Goals/Goal.h" +#include "GoalSet.h" +#include "FSM.h" +#include "Math/Vector2.h" +namespace Menge { + + namespace BFSM { + + + /////////////////////////////////////////////////////////////////////////// + // Implementation of FollowTargetCondition + /////////////////////////////////////////////////////////////////////////// + FollowTargetCondition::FollowTargetCondition(): Condition(),_acceptedRadius(0.f), _followGoalSetID(-1){ + + } + + + /////////////////////////////////////////////////////////////////////////// + FollowTargetCondition::FollowTargetCondition( const FollowTargetCondition & cond ) { + _acceptedRadius = cond._acceptedRadius; + _followGoalSetID = cond._followGoalSetID; + } + + /////////////////////////////////////////////////////////////////////////// + + FollowTargetCondition::~FollowTargetCondition() { + //TODO:What is this for? + } + + /////////////////////////////////////////////////////////////////////////// + + void FollowTargetCondition::onEnter( Agents::BaseAgent * agent ) { + + } + + /////////////////////////////////////////////////////////////////////////// + + void FollowTargetCondition::onLeave( Agents::BaseAgent * agent ) { + + } + + /////////////////////////////////////////////////////////////////////////// + + bool FollowTargetCondition::conditionMet( Agents::BaseAgent * agent, const Goal * goal ) { + const GoalSet * gs = Menge::SimulatorDBEntry::_fsm->getGoalSet(this->_followGoalSetID); + if(gs == NULL) + { + logger<<"null goal set"; + return false; + } + size_t num_goals=gs->size(); + Menge::Math::Vector2 pos=agent->_pos; + for(size_t i=0;igetIthGoal(i); + float dist=g->squaredDistance(pos); + if(dist<=_acceptedRadius) + return true; + } + + return false; + } + + /////////////////////////////////////////////////////////////////////////// + + Condition * FollowTargetCondition::copy() { + return new FollowTargetCondition( *this ); + } + + /////////////////////////////////////////////////////////////////////////// + // Implementation of FollowTargetCondFactory + ///////////////////////////////////////////////////////////////////// + FollowTargetCondFactory::FollowTargetCondFactory() : ConditionFactory() { + _distanceID = _attrSet.addFloatAttribute("distance", true /*required*/, 0.0 ); + _followGoalSetIDID = _attrSet.addIntAttribute("goalSetToFollow", true, 99 ); + + } + + /////////////////////////////////////////////////////////////////////////// + + bool FollowTargetCondFactory::setFromXML( Condition * condition, TiXmlElement * node, const std::string & behaveFldr ) const { + FollowTargetCondition * pCond = dynamic_cast< FollowTargetCondition * >( condition ); + assert( pCond != 0x0 && "Trying to set the properties of a follow condition on an incompatible object" ); + + if ( !ConditionFactory::setFromXML( pCond, node, behaveFldr ) ) return false; + float d=_attrSet.getFloat( _distanceID); + pCond->setMinDistance( d*d); + pCond->setGoalsToFollow( _attrSet.getInt(_followGoalSetIDID)); + + return true; + } + } // namespace BFSM +} // namespace Menge diff --git a/src/Menge/MengeCore/BFSM/Transitions/CondFollow.h b/src/Menge/MengeCore/BFSM/Transitions/CondFollow.h new file mode 100644 index 00000000..14b9fb0a --- /dev/null +++ b/src/Menge/MengeCore/BFSM/Transitions/CondFollow.h @@ -0,0 +1,172 @@ +/* + * CondQuad.h + * + * Created on: Jul 18, 2016 + * Author: michael + */ + +#ifndef MENGE_MENGECORE_BFSM_TRANSITIONS_CONDFOLLOW_H_ +#define MENGE_MENGECORE_BFSM_TRANSITIONS_CONDFOLLOW_H_ + +#include "CoreConfig.h" +#include "Transitions/Condition.h" +#include "Transitions/ConditionFactory.h" +#include "fsmCommon.h" +#include "ReadersWriterLock.h" +#include "SimulatorInterface.h" + +namespace Menge { + + namespace BFSM { + + /////////////////////////////////////////////////////////////////////////// + + /*! + * @brief The definition of the follow condition. + * + * The follow condition becomes active when the agent is near a target + * "follow" goal set. This can be used to simulate agents following a set of + * moving goals, e.g., quadcopters + */ + class MENGE_API FollowTargetCondition : public Condition { + public: + /*! + * @brief Constructor. + */ + FollowTargetCondition(); + FollowTargetCondition(const FollowTargetCondition & cond); + + + /*! + * @brief Create a copy of this condition. + * + * It is the responsibility of the caller to delete the object. + * + * @returns: A "deep copy" of this condition - such that there is no shared + * objects between this and its copy. + */ + virtual Condition * copy(); + + protected: + /*! + * @brief Constructor. + */ + ~FollowTargetCondition(); + + public: + /*! + * @brief Called when an agent enters a state with this exiting transition. + * + * Sub-classes should use this function as the opportunity to cache any + * particular per-agent data. + * + * @param agent The agent who has entered the state which uses + * this transition. + */ + virtual void onEnter( Agents::BaseAgent * agent ); + + /*! + * @brief Called when an agent exits the state with this transition. + * + * @param agent The agent who left the state. + */ + virtual void onLeave( Agents::BaseAgent * agent ); + + /*! + * @brief check if agent is near a quad + * + * @param agent The agent to test the transition for. + * @param goal The agent's goal (although this may be ignored). + * @returns True if the condition has been met, false otherwise. + */ + virtual bool conditionMet( Agents::BaseAgent * agent, const Goal * goal ); + virtual void setGoalsToFollow(int id){_followGoalSetID = id;} + void setMinDistance( float dist ) { _acceptedRadius = dist; } + friend class FollowTargetCondFactory; + protected: + + float _acceptedRadius; + size_t _followGoalSetID; + + }; + + /////////////////////////////////////////////////////////////////////////// + + /*! + * @brief The factory for creating the FollowTargetCondition + */ + class MENGE_API FollowTargetCondFactory : public ConditionFactory { + public: + /*! + * @brief Constructor. + */ + FollowTargetCondFactory(); + + /*! + * @brief The name of the action. + * + * The action's name must be unique among all registered actions. + * Each action factory must override this function. + * + * @returns A string containing the unique action name. + */ + virtual const char * name() const { return "follow"; } + /*! + * @brief A description of the action. + * + * Each action factory must override this function. + * + * @returns A string containing the action description. + */ + virtual const char * description() const { + return "The Quad condition. returns true if agent is within a radius of one of quadrotors"; + } + + + protected: + /*! + * @brief Create an instance of this class's condition. + * + * All ConditionFactory sub-classes must override this by creating (on the heap) + * a new instance of its corresponding condition type. The various field values + * of the instance will be set in a subsequent call to ConditionFactory::setFromXML. + * The caller of this function takes ownership of the memory. + * + * @returns A pointer to a newly instantiated Action class. + */ + virtual Condition * instance() const { return new FollowTargetCondition(); } + + /*! + * @brief Given a pointer to an Condition instance, sets the appropriate fields + * from the provided XML node. + * + * It is assumed that the value of the `type` attribute is this Tarnsitions's type. + * (i.e. ConditionFactory::thisFactory has already been called and returned true.) + * If sub-classes of ConditionFactory introduce *new* Condition parameters, then the + * sub-class should override this method but explicitly call the parent class's + * version. + * + * @param condition A pointer to the condition whose attributes are to be set. + * @param node The XML node containing the condition attributes. + * @param behaveFldr The path to the behavior file. If the condition references + * resources in the file system, it should be defined relative + * to the behavior file location. This is the folder containing + * that path. + * @returns A boolean reporting success (true) or failure (false). + */ + virtual bool setFromXML( Condition * condition, TiXmlElement * node, const std::string & behaveFldr ) const; + + /*! + * @brief The identifier for the minimum distance. + */ + size_t _distanceID; + + /*! + * @brief The identifier for the Goal set to follow. + */ + size_t _followGoalSetIDID; + }; + + } // namespace BFSM +} // namespace Menge +#endif //MENGE_MENGECORE_BFSM_TRANSITIONS_CONDFOLLOW_H_ diff --git a/src/Menge/MengeCore/BFSM/Transitions/CondProximity.cpp b/src/Menge/MengeCore/BFSM/Transitions/CondProximity.cpp new file mode 100644 index 00000000..11b8969c --- /dev/null +++ b/src/Menge/MengeCore/BFSM/Transitions/CondProximity.cpp @@ -0,0 +1,102 @@ +/* + * CondProximity.cpp + * + * Created on: Jul 18, 2016 + * Author: michael + */ + +#include "CondProximity.h" + +#include "Core.h" +#include "BaseAgent.h" +#include +//TODO:include file with _sim reference(get agents ability) +#include "SimSystem.h" +#include "SimulatorDBEntry.h" +namespace Menge { + + namespace BFSM { + + + /////////////////////////////////////////////////////////////////////////// + // Implementation of ProximityCondition + /////////////////////////////////////////////////////////////////////////// + ProximityCondition::ProximityCondition(): Condition(),_acceptedRadius(0.f), _AgentToAvoidID(-1), _inc(0.00){ + + } + + + /////////////////////////////////////////////////////////////////////////// + ProximityCondition::ProximityCondition( const ProximityCondition & cond ) { + _acceptedRadius = cond._acceptedRadius; + _AgentToAvoidID = -1; + _inc = 0.00; + } + + /////////////////////////////////////////////////////////////////////////// + + ProximityCondition::~ProximityCondition() { + //TODO:What is this for? + } + + /////////////////////////////////////////////////////////////////////////// + + void ProximityCondition::onEnter( Agents::BaseAgent * agent ) { + + } + + /////////////////////////////////////////////////////////////////////////// + + void ProximityCondition::onLeave( Agents::BaseAgent * agent ) { + + } + + /////////////////////////////////////////////////////////////////////////// + + bool ProximityCondition::conditionMet( Agents::BaseAgent * agent, const Goal * goal ) { + const Agents::BaseAgent *avoidAgent = Menge::SimulatorDBEntry::_sim->getAgent(this->_AgentToAvoidID); + if(avoidAgent == NULL) + { + logger<<"null void agent"; + return false; + } + if(avoidAgent->_radius>5 ) + _inc+=0.0001; + return std::sqrt(std::pow(std::abs(agent->_pos._x- avoidAgent->_pos._x),2)+std::pow(std::abs(agent->_pos._y - + avoidAgent->_pos._y),2))_radius+_acceptedRadius+_inc; + + } + + /////////////////////////////////////////////////////////////////////////// + + Condition * ProximityCondition::copy() { + return new ProximityCondition( *this ); + } + + /////////////////////////////////////////////////////////////////////////// + // Implementation of ProximityCondFactory + ///////////////////////////////////////////////////////////////////// + ProximityCondFactory::ProximityCondFactory() : ConditionFactory() { + _distanceID = _attrSet.addFloatAttribute("distance", true /*required*/, 0.0 ); + _AgentToAvoidID = _attrSet.addIntAttribute("agentToAvoid", true, 0 ); + + } + + /////////////////////////////////////////////////////////////////////////// + + bool ProximityCondFactory::setFromXML( Condition * condition, TiXmlElement * node, const std::string & behaveFldr ) const { + ProximityCondition * pCond = dynamic_cast< ProximityCondition * >( condition ); + assert( pCond != 0x0 && "Trying to set the properties of a proximity condition on an incompatible object" ); + + if ( !ConditionFactory::setFromXML( pCond, node, behaveFldr ) ) return false; + + pCond->setMinDistance( _attrSet.getFloat( _distanceID)); + //cycle through agents here to find disaster agent using agent to aboid in attrset, set the this_x and this_y from that thern return true + + pCond->setAgentToAvoid( _attrSet.getInt(_AgentToAvoidID)) ; + + + return true; + } + } // namespace BFSM +} // namespace Menge diff --git a/src/Menge/MengeCore/BFSM/Transitions/CondProximity.h b/src/Menge/MengeCore/BFSM/Transitions/CondProximity.h new file mode 100644 index 00000000..4486be5f --- /dev/null +++ b/src/Menge/MengeCore/BFSM/Transitions/CondProximity.h @@ -0,0 +1,180 @@ +/* + * CondProximity.h + * + * Created on: Jul 18, 2016 + * Author: michael + */ + +#ifndef SRC_MENGE_MENGECORE_BFSM_TRANSITIONS_CONDPROXIMITY_H_ +#define SRC_MENGE_MENGECORE_BFSM_TRANSITIONS_CONDPROXIMITY_H_ + +#include "CoreConfig.h" +#include "Transitions/Condition.h" +#include "Transitions/ConditionFactory.h" +#include "fsmCommon.h" +#include "ReadersWriterLock.h" +#include "SimulatorInterface.h" + +namespace Menge { + + namespace BFSM { + + // forward declarations + class ProximityCondFactory; + /////////////////////////////////////////////////////////////////////////// + + /*! + * @brief The definition of the proximity condition. + * + * The proximity condition becomes active when the agent is within a given + * distance from specified target agent. + */ + class MENGE_API ProximityCondition : public Condition { + public: + /*! + * @brief Constructor. + */ + ProximityCondition(); + ProximityCondition(const ProximityCondition & cond); + /*! + * @brief Copy Constructor. + * + * @param cond The condition to copy from. + */ + //ProximityCondition( const ProximityCondition & cond ); + + /*! + * @brief Create a copy of this condition. + * + * It is the responsibility of the caller to delete the object. + * + * @returns: A "deep copy" of this condition - such that there is no shared + * objects between this and its copy. + */ + virtual Condition * copy(); + + protected: + /*! + * @brief Constructor. + */ + ~ProximityCondition(); + + public: + /*! + * @brief Called when an agent enters a state with this exiting transition. + * + * Sub-classes should use this function as the opportunity to cache any + * particular per-agent data. + * + * @param agent The agent who has entered the state which uses + * this transition. + */ + virtual void onEnter( Agents::BaseAgent * agent ); + + /*! + * @brief Called when an agent exits the state with this transition. + * + * @param agent The agent who left the state. + */ + virtual void onLeave( Agents::BaseAgent * agent ); + + /*! + * @brief The AutoCondition is always met -- it is a tautology. + * + * @param agent The agent to test the transition for. + * @param goal The agent's goal (although this may be ignored). + * @returns True if the condition has been met, false otherwise. + */ + virtual bool conditionMet( Agents::BaseAgent * agent, const Goal * goal ); + virtual void setAgentToAvoid(int id){_AgentToAvoidID = id;} + void setMinDistance( float dist ) { _acceptedRadius = dist; } + friend class ProximityCondFactory; + + protected: + //the agnet we want to avoid + + int _AgentToAvoidID; + float _acceptedRadius; + float _inc; + }; + + /////////////////////////////////////////////////////////////////////////// + + /*! + * @brief The factory for creating the ProximityCondition + */ + class MENGE_API ProximityCondFactory : public ConditionFactory { + public: + /*! + * @brief Constructor. + */ + ProximityCondFactory(); + + /*! + * @brief The name of the action. + * + * The action's name must be unique among all registered actions. + * Each action factory must override this function. + * + * @returns A string containing the unique action name. + */ + virtual const char * name() const { return "proximity"; } + /*! + * @brief A description of the action. + * + * Each action factory must override this function. + * + * @returns A string containing the action description. + */ + virtual const char * description() const { + return "The Proximity condition. returns true if agent is within a radius of another specified agent"; + } + + + protected: + /*! + * @brief Create an instance of this class's condition. + * + * All ConditionFactory sub-classes must override this by creating (on the heap) + * a new instance of its corresponding condition type. The various field values + * of the instance will be set in a subsequent call to ConditionFactory::setFromXML. + * The caller of this function takes ownership of the memory. + * + * @returns A pointer to a newly instantiated Action class. + */ + virtual Condition * instance() const { return new ProximityCondition(); } + + /*! + * @brief Given a pointer to an Condition instance, sets the appropriate fields + * from the provided XML node. + * + * It is assumed that the value of the `type` attribute is this Tarnsitions's type. + * (i.e. ConditionFactory::thisFactory has already been called and returned true.) + * If sub-classes of ConditionFactory introduce *new* Condition parameters, then the + * sub-class should override this method but explicitly call the parent class's + * version. + * + * @param condition A pointer to the condition whose attributes are to be set. + * @param node The XML node containing the condition attributes. + * @param behaveFldr The path to the behavior file. If the condition references + * resources in the file system, it should be defined relative + * to the behavior file location. This is the folder containing + * that path. + * @returns A boolean reporting success (true) or failure (false). + */ + virtual bool setFromXML( Condition * condition, TiXmlElement * node, const std::string & behaveFldr ) const; + + /*! + * @brief The identifier for the minimum distance. + */ + size_t _distanceID; + + /*! + * @brief The identifier for the Agent to avoid. + */ + size_t _AgentToAvoidID; + }; + + } // namespace BFSM +} // namespace Menge +#endif // diff --git a/src/Menge/MengeCore/BFSM/Transitions/CondVia.cpp b/src/Menge/MengeCore/BFSM/Transitions/CondVia.cpp new file mode 100644 index 00000000..afdb125f --- /dev/null +++ b/src/Menge/MengeCore/BFSM/Transitions/CondVia.cpp @@ -0,0 +1,108 @@ +/* + * CondQuad.cpp + * + * Created on: Jul 18, 2016 + * Author: michael + */ + +#include "CondVia.h" + +#include "Core.h" +#include "BaseAgent.h" +#include +//TODO:include file with _sim reference(get agents ability) +#include "SimSystem.h" +#include "SimulatorDBEntry.h" +#include "Goals/Goal.h" +#include "GoalSet.h" +#include "FSM.h" +#include "Math/Vector2.h" +namespace Menge { + + namespace BFSM { + + + /////////////////////////////////////////////////////////////////////////// + // Implementation of ViaTargetCondition + /////////////////////////////////////////////////////////////////////////// + ViaTargetCondition::ViaTargetCondition(): Condition(),_acceptedRadius(0.f), _viaGoalSetID(-1){ + + } + + + /////////////////////////////////////////////////////////////////////////// + ViaTargetCondition::ViaTargetCondition( const ViaTargetCondition & cond ) { + _acceptedRadius = cond._acceptedRadius; + _viaGoalSetID = cond._viaGoalSetID; + } + + /////////////////////////////////////////////////////////////////////////// + + ViaTargetCondition::~ViaTargetCondition() { + //TODO:What is this for? + } + + /////////////////////////////////////////////////////////////////////////// + + void ViaTargetCondition::onEnter( Agents::BaseAgent * agent ) { + + } + + /////////////////////////////////////////////////////////////////////////// + + void ViaTargetCondition::onLeave( Agents::BaseAgent * agent ) { + + } + + /////////////////////////////////////////////////////////////////////////// + + bool ViaTargetCondition::conditionMet( Agents::BaseAgent * agent, const Goal * goal ) { + const GoalSet * gs = Menge::SimulatorDBEntry::_fsm->getGoalSet(this->_viaGoalSetID); + if(gs == NULL) + { + logger<<"null goal set"; + return false; + } + size_t num_goals=gs->size(); + Menge::Math::Vector2 pos=agent->_pos; + for(size_t i=0;igetIthGoal(i); + float dist=g->squaredDistance(pos); + if(dist<=_acceptedRadius) + return true; + } + + return false; + } + + /////////////////////////////////////////////////////////////////////////// + + Condition * ViaTargetCondition::copy() { + return new ViaTargetCondition( *this ); + } + + /////////////////////////////////////////////////////////////////////////// + // Implementation of ViaTargetCondFactory + ///////////////////////////////////////////////////////////////////// + ViaTargetCondFactory::ViaTargetCondFactory() : ConditionFactory() { + _distanceID = _attrSet.addFloatAttribute("distance", true /*required*/, 0.0 ); + _viaGoalSetIDID = _attrSet.addIntAttribute("goalsVia", true, 0 ); + + } + + /////////////////////////////////////////////////////////////////////////// + + bool ViaTargetCondFactory::setFromXML( Condition * condition, TiXmlElement * node, const std::string & behaveFldr ) const { + ViaTargetCondition * pCond = dynamic_cast< ViaTargetCondition * >( condition ); + assert( pCond != 0x0 && "Trying to set the properties of a timer condition on an incompatible object" ); + + if ( !ConditionFactory::setFromXML( pCond, node, behaveFldr ) ) return false; + float d=_attrSet.getFloat( _distanceID); + pCond->setMinDistance(d*d); + pCond->setGoalsToVia( _attrSet.getInt(_viaGoalSetIDID)); + + return true; + } + } // namespace BFSM +} // namespace Menge diff --git a/src/Menge/MengeCore/BFSM/Transitions/CondVia.h b/src/Menge/MengeCore/BFSM/Transitions/CondVia.h new file mode 100644 index 00000000..353cfd36 --- /dev/null +++ b/src/Menge/MengeCore/BFSM/Transitions/CondVia.h @@ -0,0 +1,170 @@ +/* + * CondQuad.h + * + * Created on: Jul 18, 2016 + * Author: michael + */ + +#ifndef MENGE_MENGECORE_BFSM_TRANSITIONS_CONDVIA_H_ +#define MENGE_MENGECORE_BFSM_TRANSITIONS_CONDVIA_H_ + +#include "CoreConfig.h" +#include "Transitions/Condition.h" +#include "Transitions/ConditionFactory.h" +#include "fsmCommon.h" +#include "ReadersWriterLock.h" +#include "SimulatorInterface.h" + +namespace Menge { + + namespace BFSM { + + /////////////////////////////////////////////////////////////////////////// + + /*! + * @brief The definition of the via target condition. + * + * The via target condition becomes active when the agent has been near to a specified "via target" goal + */ + class MENGE_API ViaTargetCondition : public Condition { + public: + /*! + * @brief Constructor. + */ + ViaTargetCondition(); + ViaTargetCondition(const ViaTargetCondition & cond); + + + /*! + * @brief Create a copy of this condition. + * + * It is the responsibility of the caller to delete the object. + * + * @returns: A "deep copy" of this condition - such that there is no shared + * objects between this and its copy. + */ + virtual Condition * copy(); + + protected: + /*! + * @brief Constructor. + */ + ~ViaTargetCondition(); + + public: + /*! + * @brief Called when an agent enters a state with this exiting transition. + * + * Sub-classes should use this function as the opportunity to cache any + * particular per-agent data. + * + * @param agent The agent who has entered the state which uses + * this transition. + */ + virtual void onEnter( Agents::BaseAgent * agent ); + + /*! + * @brief Called when an agent exits the state with this transition. + * + * @param agent The agent who left the state. + */ + virtual void onLeave( Agents::BaseAgent * agent ); + + /*! + * @brief check if agent is near a via goal + * + * @param agent The agent to test the transition for. + * @param goal The agent's goal (although this may be ignored). + * @returns True if the condition has been met, false otherwise. + */ + virtual bool conditionMet( Agents::BaseAgent * agent, const Goal * goal ); + virtual void setGoalsToVia(int id){_viaGoalSetID = id;} + void setMinDistance( float dist ) { _acceptedRadius = dist; } + friend class ViaTargetCondFactory; + protected: + + float _acceptedRadius; + size_t _viaGoalSetID; + + }; + + /////////////////////////////////////////////////////////////////////////// + + /*! + * @brief The factory for creating the ViaTargetCondition + */ + class MENGE_API ViaTargetCondFactory : public ConditionFactory { + public: + /*! + * @brief Constructor. + */ + ViaTargetCondFactory(); + + /*! + * @brief The name of the action. + * + * The action's name must be unique among all registered actions. + * Each action factory must override this function. + * + * @returns A string containing the unique action name. + */ + virtual const char * name() const { return "via"; } + /*! + * @brief A description of the action. + * + * Each action factory must override this function. + * + * @returns A string containing the action description. + */ + virtual const char * description() const { + return "The Quad condition. returns true if agent is within a radius of one of quadrotors"; + } + + + protected: + /*! + * @brief Create an instance of this class's condition. + * + * All ConditionFactory sub-classes must override this by creating (on the heap) + * a new instance of its corresponding condition type. The various field values + * of the instance will be set in a subsequent call to ConditionFactory::setFromXML. + * The caller of this function takes ownership of the memory. + * + * @returns A pointer to a newly instantiated Action class. + */ + virtual Condition * instance() const { return new ViaTargetCondition(); } + + /*! + * @brief Given a pointer to an Condition instance, sets the appropriate fields + * from the provided XML node. + * + * It is assumed that the value of the `type` attribute is this Tarnsitions's type. + * (i.e. ConditionFactory::thisFactory has already been called and returned true.) + * If sub-classes of ConditionFactory introduce *new* Condition parameters, then the + * sub-class should override this method but explicitly call the parent class's + * version. + * + * @param condition A pointer to the condition whose attributes are to be set. + * @param node The XML node containing the condition attributes. + * @param behaveFldr The path to the behavior file. If the condition references + * resources in the file system, it should be defined relative + * to the behavior file location. This is the folder containing + * that path. + * @returns A boolean reporting success (true) or failure (false). + */ + virtual bool setFromXML( Condition * condition, TiXmlElement * node, const std::string & behaveFldr ) const; + + /*! + * @brief The identifier for the minimum distance. + */ + size_t _distanceID; + + /*! + * @brief The identifier for the Agent to avoid. + */ + size_t _viaGoalSetIDID; + }; + + } // namespace BFSM +} // namespace Menge +#endif // MENGE_MENGECORE_BFSM_TRANSITIONS_CONDVIA_H_ diff --git a/src/Menge/MengeCore/BFSM/Transitions/ConditionDatabase.cpp b/src/Menge/MengeCore/BFSM/Transitions/ConditionDatabase.cpp index 6267dcca..5da467a1 100644 --- a/src/Menge/MengeCore/BFSM/Transitions/ConditionDatabase.cpp +++ b/src/Menge/MengeCore/BFSM/Transitions/ConditionDatabase.cpp @@ -3,7 +3,7 @@ License Menge -Copyright © and trademark ™ 2012-14 University of North Carolina at Chapel Hill. +Copyright � and trademark � 2012-14 University of North Carolina at Chapel Hill. All rights reserved. Permission to use, copy, modify, and distribute this software and its documentation @@ -42,7 +42,9 @@ Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu #include "Transitions/CondSpace.h" #include "Transitions/CondGoal.h" #include "Transitions/CondBoolean.h" - +#include "Transitions/CondProximity.h" +#include "Transitions/CondFollow.h" +#include "Transitions/CondVia.h" #ifndef DOXYGEN_SHOULD_SKIP_THIS namespace Menge { @@ -62,8 +64,11 @@ namespace Menge { addFactory( new BFSM::AndCondFactory() ); addFactory( new BFSM::OrCondFactory() ); addFactory( new BFSM::NotCondFactory() ); + addFactory( new BFSM::ProximityCondFactory() ); + addFactory( new BFSM::FollowTargetCondFactory() ); + addFactory( new BFSM::ViaTargetCondFactory() ); } } // namespace Menge -#endif // DOXYGEN_SHOULD_SKIP_THIS \ No newline at end of file +#endif // DOXYGEN_SHOULD_SKIP_THIS diff --git a/src/Menge/MengeCore/Runtime/SimulatorDBEntry.cpp b/src/Menge/MengeCore/Runtime/SimulatorDBEntry.cpp index d79199ce..f5c0c5c5 100644 --- a/src/Menge/MengeCore/Runtime/SimulatorDBEntry.cpp +++ b/src/Menge/MengeCore/Runtime/SimulatorDBEntry.cpp @@ -3,7 +3,7 @@ License Menge -Copyright © and trademark ™ 2012-14 University of North Carolina at Chapel Hill. +Copyright � and trademark � 2012-14 University of North Carolina at Chapel Hill. All rights reserved. Permission to use, copy, modify, and distribute this software and its documentation @@ -52,7 +52,8 @@ Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu #include "MengeException.h" namespace Menge { - + Agents::SimulatorInterface * SimulatorDBEntry::_sim = 0; + BFSM::FSM * SimulatorDBEntry::_fsm = 0; ///////////////////////////////////////////////////////////////////////////// // Implementation of SimulatorDBEntry //////////////////////////////////////////////////////////////////////////// @@ -224,4 +225,4 @@ namespace Menge { Agents::AgentInitializer * SimulatorDBEntry::getAgentInitalizer() const { return new Agents::AgentInitializer(); } -} // namespace Menge \ No newline at end of file +} // namespace Menge diff --git a/src/Menge/MengeCore/Runtime/SimulatorDBEntry.h b/src/Menge/MengeCore/Runtime/SimulatorDBEntry.h index 9df90226..b668c815 100644 --- a/src/Menge/MengeCore/Runtime/SimulatorDBEntry.h +++ b/src/Menge/MengeCore/Runtime/SimulatorDBEntry.h @@ -3,7 +3,7 @@ License Menge -Copyright © and trademark ™ 2012-14 University of North Carolina at Chapel Hill. +Copyright � and trademark � 2012-14 University of North Carolina at Chapel Hill. All rights reserved. Permission to use, copy, modify, and distribute this software and its documentation @@ -287,14 +287,17 @@ namespace Menge { * @brief A pointer to the simulator. The database entry is not responsible for * deleting it unless there is an error in initialization. */ - Agents::SimulatorInterface * _sim; + /*! * @brief A pointer to the behavior finite state machine. The database entry is *not* * responsible for deleting it unless there is an error in initialization. */ - BFSM::FSM * _fsm; + + public: + static BFSM::FSM * _fsm; + static Agents::SimulatorInterface * _sim; }; } // namespace Menge -#endif // __SIMULATOR_DB_ENTRY_H__ \ No newline at end of file +#endif // __SIMULATOR_DB_ENTRY_H__ From 257850b299556b0d7fd31e139e1313c734d5bd54 Mon Sep 17 00:00:00 2001 From: Michael Huang Date: Thu, 11 Aug 2016 08:29:37 -0500 Subject: [PATCH 4/9] limit simulation steps to specified global time This is needed to sync with other simulators external to Menge --- src/Menge/MengeCore/Runtime/SimSystem.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Menge/MengeCore/Runtime/SimSystem.cpp b/src/Menge/MengeCore/Runtime/SimSystem.cpp index 7352c1bf..237b8bfc 100644 --- a/src/Menge/MengeCore/Runtime/SimSystem.cpp +++ b/src/Menge/MengeCore/Runtime/SimSystem.cpp @@ -85,6 +85,7 @@ namespace Menge { _isRunning = false; } else { for ( size_t i = 0; i <= _sim->getSubSteps(); ++i ) { + if(_sim->getGlobalTime()>=time) break; try { _isRunning = !_fsm->doStep(); } catch ( BFSM::FSMFatalException & e ) { @@ -217,4 +218,4 @@ namespace Menge { //////////////////////////////////////////////////////////////////////////// -} // namespace Menge \ No newline at end of file +} // namespace Menge From 81ae67c8f115d0b4e4a2952e71edbf815de1c3fe Mon Sep 17 00:00:00 2001 From: Michael Huang Date: Thu, 11 Aug 2016 10:07:38 -0500 Subject: [PATCH 5/9] Add second nearest goal selector also fine tune parameters --- examples/core/outside/outsideB.xml | 114 ++++++++++++++---- examples/core/outside/outsideS.xml | 9 +- .../MengeCore/BFSM/Actions/IncPropAction.cpp | 2 +- .../GoalSelectors/GoalSelectorDatabase.cpp | 7 +- .../GoalSelectorSecondNearest.cpp | 46 +++++++ .../GoalSelectors/GoalSelectorSecondNearest.h | 71 +++++++++++ .../BFSM/GoalSelectors/GoalSelectorSet.cpp | 2 +- .../BFSM/Transitions/CondProximity.cpp | 4 +- 8 files changed, 220 insertions(+), 35 deletions(-) create mode 100644 src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorSecondNearest.cpp create mode 100644 src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorSecondNearest.h diff --git a/examples/core/outside/outsideB.xml b/examples/core/outside/outsideB.xml index 9a874e1d..2a80a66e 100644 --- a/examples/core/outside/outsideB.xml +++ b/examples/core/outside/outsideB.xml @@ -60,15 +60,22 @@ - - - - - + + + + + + + + + + + + - + @@ -98,7 +105,12 @@ - + + + + + + @@ -107,7 +119,7 @@ - + @@ -123,7 +135,10 @@ - + + + + @@ -152,42 +167,93 @@ - - + + + + + + - - + + + + + + + - - + + + - - + + + - + + + + + + + + - + - - + + + + + - - + + + + diff --git a/examples/core/outside/outsideS.xml b/examples/core/outside/outsideS.xml index 26428a22..33f3af24 100644 --- a/examples/core/outside/outsideS.xml +++ b/examples/core/outside/outsideS.xml @@ -18,7 +18,7 @@ - + @@ -29,7 +29,7 @@ - + @@ -38,7 +38,7 @@ - + + diff --git a/src/Menge/MengeCore/BFSM/Actions/IncPropAction.cpp b/src/Menge/MengeCore/BFSM/Actions/IncPropAction.cpp index b087192c..e888755f 100644 --- a/src/Menge/MengeCore/BFSM/Actions/IncPropAction.cpp +++ b/src/Menge/MengeCore/BFSM/Actions/IncPropAction.cpp @@ -23,7 +23,7 @@ namespace Menge { ///////////////////////////////////////////////////////////////////// void IncPropAction::onEnter( Agents::BaseAgent * agent ) { - if(agent->_radius<30)//TODO replace with property spec from xml + if(agent->_radius<20)//TODO replace with property spec from xml agent->_radius = agent->_radius+this->_increment; } diff --git a/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorDatabase.cpp b/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorDatabase.cpp index 41f5f1cc..8c895d94 100644 --- a/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorDatabase.cpp +++ b/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorDatabase.cpp @@ -3,7 +3,7 @@ License Menge -Copyright © and trademark ™ 2012-14 University of North Carolina at Chapel Hill. +Copyright � and trademark � 2012-14 University of North Carolina at Chapel Hill. All rights reserved. Permission to use, copy, modify, and distribute this software and its documentation @@ -48,7 +48,7 @@ Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu #include "GoalSelectors/GoalSelectorFarthest.h" #include "GoalSelectors/GoalSelectorFarthestNM.h" #include "GoalSelectors/GoalSelectorShared.h" - +#include "GoalSelectors/GoalSelectorSecondNearest.h" #ifndef DOXYGEN_SHOULD_SKIP_THIS namespace Menge { @@ -70,7 +70,8 @@ namespace Menge { addFactory( new BFSM::NearestNMGoalSelectorFactory() ); addFactory( new BFSM::FarthestNMGoalSelectorFactory() ); addFactory( new BFSM::SharedGoalSelectorFactory() ); + addFactory( new BFSM::SecondNearestGoalSelectorFactory() ); } } // namespace Menge -#endif // DOXYGEN_SHOULD_SKIP_THIS \ No newline at end of file +#endif // DOXYGEN_SHOULD_SKIP_THIS diff --git a/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorSecondNearest.cpp b/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorSecondNearest.cpp new file mode 100644 index 00000000..31e306bc --- /dev/null +++ b/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorSecondNearest.cpp @@ -0,0 +1,46 @@ +#include "GoalSelectors/GoalSelectorSecondNearest.h" +#include "Goals/Goal.h" +#include "GoalSet.h" +#include "BaseAgent.h" +#include + +namespace Menge { + + namespace BFSM { + + ///////////////////////////////////////////////////////////////////// + // Implementation of NearestGoalSelector + ///////////////////////////////////////////////////////////////////// + + Goal * SecondNearestGoalSelector::getGoal( const Agents::BaseAgent * agent ) const { + assert( agent != 0x0 && "NearestGoalGenerator requires a valid base agent!" ); + const size_t GOAL_COUNT = _goalSet->size(); + if ( GOAL_COUNT == 0 ) { + logger << Logger::ERR_MSG << "NearestGoalSelector was unable to provide a goal for agent " << agent->_id << ". There were no available goals in the goal set."; + return 0x0; + } + const Vector2 p = agent->_pos; + + Goal * bestGoal; + + bestGoal = _goalSet->getIthGoal( 0 ); + Goal * secondBestGoal; + + secondBestGoal = _goalSet->getIthGoal( 0 ); + Vector2 disp = bestGoal->getCentroid() - p; + float bestDist = absSq( disp ); + for ( size_t i = 1; i < GOAL_COUNT; ++i ) { + Goal * testGoal = _goalSet->getIthGoal( i ); + disp = testGoal->getCentroid() - p; + float testDist = absSq( disp ); + if ( testDist < bestDist ) { + bestDist = testDist; + secondBestGoal=bestGoal; + bestGoal = testGoal; + } + } + + return secondBestGoal; + } + } // namespace BFSM +} // namespace Menge diff --git a/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorSecondNearest.h b/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorSecondNearest.h new file mode 100644 index 00000000..69d8b727 --- /dev/null +++ b/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorSecondNearest.h @@ -0,0 +1,71 @@ + +/*! + * @file GoalSelectorNearest.h + * @brief The definition of the nearest goal selector. + */ + +#ifndef __GOAL_SELECTOR_SECOND_NEAREST_H__ +#define __GOAL_SELECTOR_SECOND_NEAREST_H__ + +#include "CoreConfig.h" +#include "fsmCommon.h" +#include "GoalSelectors/GoalSelectorSet.h" + +namespace Menge { + + namespace BFSM { + /*! + * @brief The nearest goal selector selects the goal from a goal set that + * is nearest to the agent in terms of Euclidian distance. + */ + class MENGE_API SecondNearestGoalSelector : public SetGoalSelector { + public: + /*! + * @brief Interface function for acquiring per-agent goals. + * + * @param agent The agent for whom a goal is extracted. + * @returns A pointer to a goal. + * // TODO: Figure out who owns this goal. + */ + virtual Goal * getGoal( const Agents::BaseAgent * agent ) const; + }; + + /*! + * @brief Factory for the NearestGoalSelector. + */ + class MENGE_API SecondNearestGoalSelectorFactory : public SetGoalSelectorFactory { + public: + /*! + * @brief The name of the goal selector type. + * + * The goal selector's name must be unique among all registered goal selectors. + * Each goal selector factory must override this function. + * + * @returns A string containing the unique goal selector name. + */ + virtual const char * name() const { return "second_nearest"; } + + /*! + * @brief A description of the goal selector. + * + * Each goal selector factory must override this function. + * + * @returns A string containing the goal selector description. + */ + virtual const char * description() const { + return "A goal selector. Assigns the agent the goal in the given " \ + "goal set that is *nearest* the agent (in Euclidian distance)."; + }; + + protected: + /*! + * @brief Create an instance of this class's goal selector. + * + * @returns A pointer to a newly instantiated GoalSelector class. + */ + GoalSelector * instance() const { return new SecondNearestGoalSelector(); } + }; + } // namespace BFSM +} // namespace Menge + +#endif // __GOAL_SELECTOR_NEAREST_H__ diff --git a/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorSet.cpp b/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorSet.cpp index f9748439..3a5addcc 100644 --- a/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorSet.cpp +++ b/src/Menge/MengeCore/BFSM/GoalSelectors/GoalSelectorSet.cpp @@ -3,7 +3,7 @@ License Menge -Copyright © and trademark ™ 2012-14 University of North Carolina at Chapel Hill. +Copyright � and trademark � 2012-14 University of North Carolina at Chapel Hill. All rights reserved. Permission to use, copy, modify, and distribute this software and its documentation diff --git a/src/Menge/MengeCore/BFSM/Transitions/CondProximity.cpp b/src/Menge/MengeCore/BFSM/Transitions/CondProximity.cpp index 11b8969c..73e811b1 100644 --- a/src/Menge/MengeCore/BFSM/Transitions/CondProximity.cpp +++ b/src/Menge/MengeCore/BFSM/Transitions/CondProximity.cpp @@ -60,8 +60,8 @@ namespace Menge { logger<<"null void agent"; return false; } - if(avoidAgent->_radius>5 ) - _inc+=0.0001; + if(avoidAgent->_radius>=3) + _inc+=0.0004; return std::sqrt(std::pow(std::abs(agent->_pos._x- avoidAgent->_pos._x),2)+std::pow(std::abs(agent->_pos._y - avoidAgent->_pos._y),2))_radius+_acceptedRadius+_inc; From c0273a206419338aa51ca4e7eecafd55d0a2a7c0 Mon Sep 17 00:00:00 2001 From: Michael Huang Date: Sat, 20 Aug 2016 20:29:23 -0500 Subject: [PATCH 6/9] add congregate scene --- examples/core/cong.xml | 9 ++++ examples/core/cong/congB.xml | 91 ++++++++++++++++++++++++++++++++++++ examples/core/cong/congS.xml | 64 +++++++++++++++++++++++++ examples/core/cong/congV.xml | 16 +++++++ 4 files changed, 180 insertions(+) create mode 100644 examples/core/cong.xml create mode 100644 examples/core/cong/congB.xml create mode 100644 examples/core/cong/congS.xml create mode 100644 examples/core/cong/congV.xml diff --git a/examples/core/cong.xml b/examples/core/cong.xml new file mode 100644 index 00000000..61d94970 --- /dev/null +++ b/examples/core/cong.xml @@ -0,0 +1,9 @@ + + + diff --git a/examples/core/cong/congB.xml b/examples/core/cong/congB.xml new file mode 100644 index 00000000..7be19ca3 --- /dev/null +++ b/examples/core/cong/congB.xml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/core/cong/congS.xml b/examples/core/cong/congS.xml new file mode 100644 index 00000000..a1aadd19 --- /dev/null +++ b/examples/core/cong/congS.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/core/cong/congV.xml b/examples/core/cong/congV.xml new file mode 100644 index 00000000..036c9088 --- /dev/null +++ b/examples/core/cong/congV.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + From c6fac72340c14d87101651f45104416900b91a33 Mon Sep 17 00:00:00 2001 From: Michael Huang Date: Fri, 9 Sep 2016 00:06:02 -0500 Subject: [PATCH 7/9] add new scene, made buildings simpler --- examples/core/outside/outsideB.xml | 50 +- examples/core/outside/outsideS.xml | 24 +- examples/core/predetermined.xml | 9 + .../core/predetermined/predeterminedB.xml | 464 ++++++++++++++++++ .../core/predetermined/predeterminedS.xml | 95 ++++ .../core/predetermined/predeterminedV.xml | 16 + .../BFSM/Transitions/CondProximity.cpp | 6 +- 7 files changed, 620 insertions(+), 44 deletions(-) create mode 100644 examples/core/predetermined.xml create mode 100644 examples/core/predetermined/predeterminedB.xml create mode 100644 examples/core/predetermined/predeterminedS.xml create mode 100644 examples/core/predetermined/predeterminedV.xml diff --git a/examples/core/outside/outsideB.xml b/examples/core/outside/outsideB.xml index 2a80a66e..223604c2 100644 --- a/examples/core/outside/outsideB.xml +++ b/examples/core/outside/outsideB.xml @@ -97,7 +97,15 @@ - + + + + + + + + + @@ -119,7 +127,7 @@ - + @@ -144,6 +152,12 @@ + + + + + + @@ -151,40 +165,32 @@ - - - - - - - + - + - + - + - + - + - - - + @@ -196,7 +202,7 @@ - + @@ -207,7 +213,7 @@ - + @@ -235,7 +241,7 @@ - + diff --git a/examples/core/outside/outsideS.xml b/examples/core/outside/outsideS.xml index 33f3af24..b8bd5832 100644 --- a/examples/core/outside/outsideS.xml +++ b/examples/core/outside/outsideS.xml @@ -24,7 +24,7 @@ - + @@ -58,30 +58,14 @@ + - - - - - - - + - - - - - - - - - - - - + diff --git a/examples/core/predetermined.xml b/examples/core/predetermined.xml new file mode 100644 index 00000000..cdea882a --- /dev/null +++ b/examples/core/predetermined.xml @@ -0,0 +1,9 @@ + + + diff --git a/examples/core/predetermined/predeterminedB.xml b/examples/core/predetermined/predeterminedB.xml new file mode 100644 index 00000000..77272bfc --- /dev/null +++ b/examples/core/predetermined/predeterminedB.xml @@ -0,0 +1,464 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/core/predetermined/predeterminedS.xml b/examples/core/predetermined/predeterminedS.xml new file mode 100644 index 00000000..3cc969a6 --- /dev/null +++ b/examples/core/predetermined/predeterminedS.xml @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/core/predetermined/predeterminedV.xml b/examples/core/predetermined/predeterminedV.xml new file mode 100644 index 00000000..036c9088 --- /dev/null +++ b/examples/core/predetermined/predeterminedV.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/Menge/MengeCore/BFSM/Transitions/CondProximity.cpp b/src/Menge/MengeCore/BFSM/Transitions/CondProximity.cpp index 73e811b1..da0e2759 100644 --- a/src/Menge/MengeCore/BFSM/Transitions/CondProximity.cpp +++ b/src/Menge/MengeCore/BFSM/Transitions/CondProximity.cpp @@ -60,8 +60,10 @@ namespace Menge { logger<<"null void agent"; return false; } - if(avoidAgent->_radius>=3) - _inc+=0.0004; + if(_acceptedRadius>=3) + _inc+=0.0004; + if(_acceptedRadius<20) + _acceptedRadius += 0.2; return std::sqrt(std::pow(std::abs(agent->_pos._x- avoidAgent->_pos._x),2)+std::pow(std::abs(agent->_pos._y - avoidAgent->_pos._y),2))_radius+_acceptedRadius+_inc; From 302c73c1d5a5285a423ffc3c30f2a3d0ef6ee317 Mon Sep 17 00:00:00 2001 From: Michael Huang Date: Sat, 10 Sep 2016 15:58:07 -0500 Subject: [PATCH 8/9] tune for case study --- examples/core/outside/outsideB.xml | 28 ++++--- examples/core/outside/outsideS.xml | 6 +- .../core/predetermined/predeterminedB.xml | 77 ++++++++++--------- 3 files changed, 59 insertions(+), 52 deletions(-) diff --git a/examples/core/outside/outsideB.xml b/examples/core/outside/outsideB.xml index 223604c2..b43e9db9 100644 --- a/examples/core/outside/outsideB.xml +++ b/examples/core/outside/outsideB.xml @@ -60,18 +60,24 @@ - - - - - + + + + + + - + + + + + + @@ -179,14 +185,14 @@ - + - + @@ -195,11 +201,11 @@ - + - + diff --git a/examples/core/outside/outsideS.xml b/examples/core/outside/outsideS.xml index b8bd5832..8dd594ea 100644 --- a/examples/core/outside/outsideS.xml +++ b/examples/core/outside/outsideS.xml @@ -80,10 +80,10 @@ - - - + + + diff --git a/examples/core/predetermined/predeterminedB.xml b/examples/core/predetermined/predeterminedB.xml index 77272bfc..621cef7c 100644 --- a/examples/core/predetermined/predeterminedB.xml +++ b/examples/core/predetermined/predeterminedB.xml @@ -60,18 +60,19 @@ - - - - - + + + + + + - - - - - + + + + + @@ -165,23 +166,23 @@ - + - + - + - + - + @@ -257,10 +258,10 @@ - + - + @@ -269,22 +270,22 @@ - + - + - + - + - + - + @@ -293,21 +294,21 @@ - + - + - + - + - + - + @@ -328,7 +329,7 @@ - + @@ -371,7 +372,7 @@ - + @@ -392,7 +393,7 @@ - + @@ -426,22 +427,22 @@ - + - + - + - + - + - + From 8b12f86f4e9c8c8ee0da8204c40fd467753895ac Mon Sep 17 00:00:00 2001 From: Michael Huang Date: Wed, 7 Dec 2016 19:50:27 -0600 Subject: [PATCH 9/9] rename states to be more meaningful --- examples/core/outside/outsideB.xml | 123 +++++++++-------------- examples/core/outside/outsideS.xml | 19 +++- examples/core/randomGoal/randomGoalB.xml | 12 +-- examples/core/randomGoal/randomGoalS.xml | 10 +- 4 files changed, 72 insertions(+), 92 deletions(-) diff --git a/examples/core/outside/outsideB.xml b/examples/core/outside/outsideB.xml index b43e9db9..eafe7d6d 100644 --- a/examples/core/outside/outsideB.xml +++ b/examples/core/outside/outsideB.xml @@ -60,19 +60,18 @@ - - - - - + + + + - - - - - + + + + + - - - - - - - - - - - + + - - + + @@ -247,24 +216,24 @@ - + - - + + - + - - + + - + - + diff --git a/examples/core/outside/outsideS.xml b/examples/core/outside/outsideS.xml index 8dd594ea..79c28847 100644 --- a/examples/core/outside/outsideS.xml +++ b/examples/core/outside/outsideS.xml @@ -36,7 +36,7 @@ - + - + + - + + + + + + + + @@ -67,12 +75,14 @@ + + @@ -80,7 +90,8 @@ - + + diff --git a/examples/core/randomGoal/randomGoalB.xml b/examples/core/randomGoal/randomGoalB.xml index 64bc9fd7..5b37903d 100644 --- a/examples/core/randomGoal/randomGoalB.xml +++ b/examples/core/randomGoal/randomGoalB.xml @@ -2,13 +2,13 @@ - - - + + + - - + + @@ -45,4 +45,4 @@ - \ No newline at end of file + diff --git a/examples/core/randomGoal/randomGoalS.xml b/examples/core/randomGoal/randomGoalS.xml index f6e975d8..4db6ad2e 100644 --- a/examples/core/randomGoal/randomGoalS.xml +++ b/examples/core/randomGoal/randomGoalS.xml @@ -31,9 +31,9 @@ @@ -43,8 +43,8 @@ - \ No newline at end of file +