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