root/src/core/Compartment.cpp

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. neuron
  2. get_id
  3. set_neuron
  4. get_neuron
  5. hide
  6. show
  7. is_visible
  8. size
  9. add_geometry
  10. add_voxel
  11. remove_voxel
  12. show_geometry
  13. hide_geometry
  14. show_all_geometries
  15. hide_all_geometries
  16. set_colors
  17. set_color

   1 #include "core/Compartment.hpp"
   2 
   3 Compartment::Compartment(const char * id) : id(id)
   4                                           , node(new osg::Switch())
   5                                           , voxel_group_node(new osg::Geode())
   6                                           , neuron(nullptr)
   7 {
   8     // by default, neuron shows all its compartments, hence true
   9     node -> addChild(voxel_group_node.get(), true);
  10 }
  11 
  12 Compartment::~Compartment()
  13 {
  14     for(Voxel * voxel : voxel_seq)
  15     {
  16         voxel -> set_compartment(nullptr);
  17     }
  18 }
  19 
  20 const char *
  21 Compartment::get_id()
  22 {
  23     return id.c_str();
  24 }
  25 
  26 
  27 void
  28 Compartment::set_neuron(Neuron * neuron)
  29 {
  30     this -> neuron = neuron;
  31 }
  32 
  33 Neuron *
  34 Compartment::get_neuron()
  35 {
  36     return neuron;
  37 }
  38 
  39 void
  40 Compartment::hide()
  41 {
  42     node -> setNodeMask(NODE_HIDE_MASK);
  43 }
  44 
  45 void
  46 Compartment::show()
  47 {
  48     node -> setNodeMask(NODE_SHOW_MASK);
  49 }
  50 
  51 bool
  52 Compartment::is_visible()
  53 {
  54     return (node -> getNodeMask() == NODE_SHOW_MASK ? true : false);
  55 }
  56 
  57 unsigned int
  58 Compartment::size()
  59 {
  60     return static_cast<unsigned int>(voxel_seq.size());
  61 }
  62 
  63 unsigned int
  64 Compartment::add_geometry( PyObject * distal
  65                          , PyObject * proximal
  66                          , PyObject * parent
  67                          )
  68 {
  69     Vec4f d(pysequence_to_vec4f(distal));
  70 
  71     osg::Geometry * geometry = new osg::Geometry();
  72     osg::Geode    * geode    = new osg::Geode();
  73 
  74     if(proximal == Py_None)
  75     {
  76         sphere( osg::Vec3f(d[0], d[1], d[2])
  77               , d[3]
  78               , geometry
  79               , SPHERICAL_COMPARTMENT_POINTS
  80               , SPHERICAL_COMPARTMENT_COLOR
  81               );
  82     }
  83     else
  84     {
  85         Vec4f p(pysequence_to_vec4f(proximal));
  86         cylinder( d
  87                 , p
  88                 , geometry
  89                 , CYLINDRICAL_COMPARTMENT_POINTS
  90                 , CYLINDRICAL_COMPARTMENT_COLOR
  91                 , pysequence_to_vec3f(parent)
  92                 );
  93     }
  94     geode -> addDrawable(geometry);
  95     node -> addChild(geode);
  96     return node -> getNumChildren();
  97 }
  98 
  99 unsigned int
 100 Compartment::add_voxel(Voxel * voxel)
 101 {
 102     auto iter = voxel_map.find(voxel -> get_id());
 103     if(iter != voxel_map.end())
 104     {
 105         RECORD_WARNING("Voxel already exists!");
 106     }
 107     else
 108     {
 109         voxel_map[voxel -> get_id()] = voxel;
 110         voxel_seq.push_back(voxel);
 111         voxel_group_node -> addDrawable(voxel -> node.get());
 112         voxel -> set_compartment(this);
 113 
 114     }
 115     return static_cast<unsigned int>(voxel_seq.size());
 116 }
 117 
 118 unsigned int
 119 Compartment::remove_voxel(Voxel * voxel)
 120 {
 121     auto iter = voxel_map.find(voxel -> get_id());
 122     if(iter == voxel_map.end())
 123     {
 124         RECORD_WARNING("Voxel does not exist!");
 125     }
 126     else
 127     {
 128         voxel_map.erase(iter);
 129         voxel_seq.erase( std::find( voxel_seq.begin()
 130                                   , voxel_seq.end()
 131                                   , voxel
 132                                   )
 133                              );
 134         voxel_group_node -> removeDrawable(voxel -> node.get());
 135         voxel -> set_compartment(nullptr);
 136     }
 137     return static_cast<unsigned int>(voxel_seq.size());
 138 }
 139 
 140 
 141 void
 142 Compartment::show_geometry(unsigned int geometry_index, bool hide_others)
 143 {
 144     if(hide_others) node -> setSingleChildOn(geometry_index);
 145     else            node -> setValue(geometry_index, true);
 146 
 147 }
 148 
 149 void
 150 Compartment::hide_geometry(unsigned int geometry_index)
 151 {
 152     node -> setValue(geometry_index, false);
 153 }
 154 
 155 void
 156 Compartment::show_all_geometries()
 157 {
 158     node -> setAllChildrenOn();
 159 }
 160 
 161 void
 162 Compartment::hide_all_geometries()
 163 {
 164     node -> setAllChildrenOff();
 165 }
 166 
 167 bool
 168 Compartment::set_colors(PyObject * colors)
 169 {
 170     if(PySequence_Check(colors) != 1)
 171     {
 172         RECORD_ERROR("Invalid data structure provided for setting voxel colors.");
 173         Py_RETURN_FALSE;
 174     }
 175 
 176     unsigned int limit = std::min( static_cast<unsigned int>(PySequence_Length(colors))
 177                                  , static_cast<unsigned int>(voxel_seq.size())
 178                                  );
 179     unsigned int i;
 180     for(i = 0; i < limit;++i)
 181     {
 182         voxel_seq[i] -> set_color(PySequence_GetItem(colors, i));
 183     }
 184     Py_RETURN_TRUE;
 185 }
 186 
 187 void
 188 Compartment::set_color(PyObject * color)
 189 {
 190     for(unsigned int i = 1; i < node -> getNumChildren(); ++i)
 191     {
 192         Geode * geode = node -> getChild(i) -> asGeode();
 193         for(unsigned int j = 0; j < geode -> getNumDrawables(); ++j)
 194         {
 195             Geometry * geometry = geode -> getDrawable(j) -> asGeometry();
 196             Vec4Array * colors = new Vec4Array();
 197             colors -> push_back(pysequence_to_vec4d(color));
 198             geometry -> setColorArray(colors, osg::Array::BIND_OVERALL);
 199             geometry -> setColorBinding(osg::Geometry::BIND_OVERALL);
 200         }
 201     }
 202 }
 203 

/* [<][>][^][v][top][bottom][index][help] */