ARGoS  3
A parallel, multi-engine simulator for swarm robotics
plugins/simulator/entities/led_entity.cpp
Go to the documentation of this file.
00001 
00007 #include "led_entity.h"
00008 #include <argos3/core/simulator/space/space.h>
00009 #include <argos3/plugins/simulator/media/led_medium.h>
00010 
00011 namespace argos {
00012 
00013    /****************************************/
00014    /****************************************/
00015 
00016    CLEDEntity::CLEDEntity(CComposableEntity* pc_parent) :
00017       CPositionalEntity(pc_parent) {}
00018 
00019    /****************************************/
00020    /****************************************/
00021 
00022    CLEDEntity::CLEDEntity(CComposableEntity* pc_parent,
00023                           const std::string& str_id,
00024                           const CVector3& c_position,
00025                           const CColor& c_color) :
00026       CPositionalEntity(pc_parent, str_id, c_position, CQuaternion()),
00027       m_cColor(c_color),
00028       m_cInitColor(c_color) {}
00029 
00030    /****************************************/
00031    /****************************************/
00032 
00033    void CLEDEntity::Init(TConfigurationNode& t_tree) {
00034       try {
00035          /* Parse XML */
00036          CPositionalEntity::Init(t_tree);
00037          GetNodeAttribute(t_tree, "color", m_cInitColor);
00038          m_cColor = m_cInitColor;
00039       }
00040       catch(CARGoSException& ex) {
00041          THROW_ARGOSEXCEPTION_NESTED("Error while initializing led entity", ex);
00042       }
00043    }
00044 
00045    /****************************************/
00046    /****************************************/
00047 
00048    void CLEDEntity::Reset() {
00049       CPositionalEntity::Reset();
00050       m_cColor = m_cInitColor;
00051    }
00052 
00053    /****************************************/
00054    /****************************************/
00055 
00056    void CLEDEntity::SetEnabled(bool b_enabled) {
00057       CEntity::SetEnabled(b_enabled);
00058       if(IsEnabled()) {
00059          m_cColor = m_cInitColor;
00060       }
00061    }
00062 
00063    /****************************************/
00064    /****************************************/
00065 
00066    void CLEDEntity::AddToMedium(CLEDMedium& c_medium) {
00067       c_medium.AddEntity(*this);
00068    }
00069 
00070    /****************************************/
00071    /****************************************/
00072 
00073    void CLEDEntity::RemoveFromMedium(CLEDMedium& c_medium) {
00074       c_medium.RemoveEntity(*this);
00075    }
00076 
00077    /****************************************/
00078    /****************************************/
00079 
00080    void CLEDEntitySpaceHashUpdater::operator()(CAbstractSpaceHash<CLEDEntity>& c_space_hash,
00081                                                CLEDEntity& c_element) {
00082       /* Discard LEDs switched off */
00083       if(c_element.GetColor() != CColor::BLACK) {
00084          /* Calculate the position of the LED in the space hash */
00085          c_space_hash.SpaceToHashTable(m_nI, m_nJ, m_nK, c_element.GetPosition());
00086          /* Update the corresponding cell */
00087          c_space_hash.UpdateCell(m_nI, m_nJ, m_nK, c_element);
00088       }
00089    }
00090 
00091    /****************************************/
00092    /****************************************/
00093 
00094    CLEDEntityGridUpdater::CLEDEntityGridUpdater(CGrid<CLEDEntity>& c_grid) :
00095       m_cGrid(c_grid) {}
00096 
00097    /****************************************/
00098    /****************************************/
00099 
00100    bool CLEDEntityGridUpdater::operator()(CLEDEntity& c_entity) {
00101       /* Discard LEDs switched off */
00102       if(c_entity.GetColor() != CColor::BLACK) {
00103          try {
00104             /* Calculate the position of the LED in the space hash */
00105             m_cGrid.PositionToCell(m_nI, m_nJ, m_nK, c_entity.GetPosition());
00106             /* Update the corresponding cell */
00107             m_cGrid.UpdateCell(m_nI, m_nJ, m_nK, c_entity);
00108          }
00109          catch(CARGoSException& ex) {
00110             THROW_ARGOSEXCEPTION_NESTED("While updating the LED grid for LED \"" << c_entity.GetContext() << c_entity.GetId() << "\"", ex);
00111          }
00112       }
00113       /* Continue with the other entities */
00114       return true;
00115    }
00116 
00117    /****************************************/
00118    /****************************************/
00119 
00120    REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY(CLEDEntity);
00121 
00122    /****************************************/
00123    /****************************************/
00124 
00125 }