def _CreateOneSynapse(self, presynCell, synapsesType): presynCell.setPresynapticFocus() # highlight presynaptic cells form = GeomVertexFormat.getV3() vdata = GeomVertexData("SynapseLine", form, Geom.UHStatic) vdata.setNumRows(1) vertex = GeomVertexWriter(vdata, "vertex") vertex.addData3f(presynCell.getNode().getPos(self._node)) vertex.addData3f(0, 0, 0) prim = GeomLines(Geom.UHStatic) prim.addVertices(0, 1) geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode("Synapse_" + str(synapsesType)) node.addGeom(geom) nodePath = self._node.attachNewNode(node) nodePath.setRenderModeThickness(2) # color of the line if presynCell.active: nodePath.setColor(COL_DISTAL_SYNAPSES_ACTIVE) else: nodePath.setColor(COL_DISTAL_SYNAPSES_INACTIVE)
def _CreateOneSynapse(self, presynCell, synapsesType): form = GeomVertexFormat.getV3() vdata = GeomVertexData("SynapseLine", form, Geom.UHStatic) vdata.setNumRows(1) vertex = GeomVertexWriter(vdata, "vertex") vertex.addData3f(presynCell.getNode().getPos(self.__node)) vertex.addData3f(0, 0, 0) # vertex.addData3f(self.__node.getPos()) # printLog("inputObj:"+str(i)+"bits:"+str(y)) # printLog(inputObj[i].inputBits[y].getNode().getPos(self.__node)) prim = GeomLines(Geom.UHStatic) prim.addVertices(0, 1) geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode("Synapse_" + str(synapsesType)) node.addGeom(geom) nodePath = self.__cellsNodePath.attachNewNode(node) nodePath.setRenderModeThickness(2) # color of the line if presynCell.active: nodePath.setColor(COL_PROXIMAL_SYNAPSES_ACTIVE) else: nodePath.setColor(COL_PROXIMAL_SYNAPSES_INACTIVE)
def update_geometry(self): # The geosphere itself vertex = GeomVertexWriter(self.sphere_vdata, 'vertex') normal = GeomVertexWriter(self.sphere_vdata, 'normal') # u_map and v_map are in [-pi, pi] u_map_list = [(float(u) / float(self.res[0]) - 0.5) * 2.0 * pi for u in range(0, self.res[0] + 1)] v_map_list = [(float(v) / float(self.res[1]) - 0.5) * 2.0 * pi for v in range(0, self.res[1] + 1)] if self.unwrap_state == 0.0: # Flat map for v_map in v_map_list: for u_map in u_map_list: vertex.addData3f(u_map, 0.0, v_map / 2.0) normal.addData3f(0.0, -1.0, 0.0) else: # Non-flat map sphere_radius = 1.0 / self.unwrap_state sphere_offset = sphere_radius - self.unwrap_state for v_map in v_map_list: for u_map in u_map_list: u_sphere = u_map / sphere_radius v_sphere = v_map / sphere_radius # And this, kids, is why you should pay attention in trigonometry. v_x, v_y, v_z = sin(u_sphere) * cos(v_sphere/2.0) * sphere_radius, \ -cos(u_sphere) * cos(v_sphere/2.0) * sphere_radius + sphere_offset, \ sin(v_sphere / 2.0) * sphere_radius n_x_un, n_y_un, n_z_un = v_x, sphere_offset - v_y, v_z # FIXME: This is a lie. length = sqrt(n_x_un**2 + n_y_un**2 + n_z_un**2) n_x, n_y, n_z = n_x_un / length, n_y_un / length, n_z_un / length vertex.addData3f(v_x, v_y, v_z) normal.addData3f(n_x, n_y, n_z) # The connections between bases segs_per_connection = 30 vertex = GeomVertexWriter(self.connections_vdata, 'vertex') color = GeomVertexWriter(self.connections_vdata, 'color') for c_1_uv, c_2_uv in self.connections: # s will be [0.0, 1.0] for s in [float(c)/float(segs_per_connection+1) for c in range(0, segs_per_connection+2)]: u = (c_1_uv[0] * s) + (c_2_uv[0] * (1.0 - s)) v = (c_1_uv[1] * s) + (c_2_uv[1] * (1.0 - s)) (v_x, v_y, v_z), (n_x, n_y, n_z) = self.uv_to_xyz(u, v) min_height = 0.0001 * (1.0 - self.unwrap_state) max_height = (0.2 - min_height) * self.unwrap_state seg_height = (1.0 - (abs(s-0.5) * 2.0)**2.0) * max_height + min_height vertex.addData3f(v_x + n_x*seg_height, v_y + n_y*seg_height, v_z + n_z*seg_height) color.addData4f(1, 1, 1, 1) for c in range(0, len(self.connections)): for s in range(0, segs_per_connection+1): seg = GeomLines(Geom.UHDynamic) seg.addVertices(c*(segs_per_connection+2) + s, c*(segs_per_connection+2) + s + 1) seg.closePrimitive() self.connections_geom.addPrimitive(seg)
def makeGeomNode(): vdata = GeomVertexData('handleData', GeomVertexFormat.getV3(), Geom.UHStatic) v = GeomVertexWriter(vdata, 'vertex') length = 1.0 gapLen = 0.2 coneLen = 0.18 coneRad = 0.06 circRes = 10 v.addData3f(0, 0, -length/2.0) # back cone butt v.addData3f(0, 0, -length/2.0 + coneLen) # back cone point v.addData3f(0, 0, -gapLen/2.0) v.addData3f(0, 0, gapLen/2.0) v.addData3f(0, 0, length/2.0 - coneLen) # front cone butt v.addData3f(0, 0, length/2.0) # font cone point # Add vertices for the cone's circles. for z in [-length/2.0, length/2.0 - coneLen]: for i in xrange(circRes): theta = i*(2*pi/circRes) v.addData3f(coneRad*sin(theta), coneRad*cos(theta), z) lines = Geom(vdata) # Make prims for the two lines. for vertices in [(0, 2), (3, 5)]: line = GeomLines(Geom.UHStatic) line.addVertices(*vertices) line.closePrimitive() lines.addPrimitive(line) cones = Geom(vdata) # Make prims for the cones. for back, circ in [(0, 6), (4, 6 + circRes)]: point = back + 1 cone = GeomTriangles(Geom.UHStatic) for i in xrange(circRes): if i + 1 == circRes: cone.addVertices(back, circ, circ + i) cone.addVertices(point, circ + i, circ) else: cone.addVertices(back, circ + i + 1, circ + i) cone.addVertices(point, circ + i, circ + i + 1) cone.closePrimitive() cones.addPrimitive(cone) node = GeomNode('geomnode') node.addGeom(lines) node.addGeom(cones) return node
def makeGeomNode(): vdata = GeomVertexData('handleData', GeomVertexFormat.getV3(), Geom.UHStatic) v = GeomVertexWriter(vdata, 'vertex') length = 1.0 gapLen = 0.2 coneLen = 0.18 coneRad = 0.06 circRes = 10 v.addData3f(0, 0, -length / 2.0) # back cone butt v.addData3f(0, 0, -length / 2.0 + coneLen) # back cone point v.addData3f(0, 0, -gapLen / 2.0) v.addData3f(0, 0, gapLen / 2.0) v.addData3f(0, 0, length / 2.0 - coneLen) # front cone butt v.addData3f(0, 0, length / 2.0) # font cone point # Add vertices for the cone's circles. for z in [-length / 2.0, length / 2.0 - coneLen]: for i in xrange(circRes): theta = i * (2 * pi / circRes) v.addData3f(coneRad * sin(theta), coneRad * cos(theta), z) lines = Geom(vdata) # Make prims for the two lines. for vertices in [(0, 2), (3, 5)]: line = GeomLines(Geom.UHStatic) line.addVertices(*vertices) line.closePrimitive() lines.addPrimitive(line) cones = Geom(vdata) # Make prims for the cones. for back, circ in [(0, 6), (4, 6 + circRes)]: point = back + 1 cone = GeomTriangles(Geom.UHStatic) for i in xrange(circRes): if i + 1 == circRes: cone.addVertices(back, circ, circ + i) cone.addVertices(point, circ + i, circ) else: cone.addVertices(back, circ + i + 1, circ + i) cone.addVertices(point, circ + i, circ + i + 1) cone.closePrimitive() cones.addPrimitive(cone) node = GeomNode('geomnode') node.addGeom(lines) node.addGeom(cones) return node
def CreateTestScene(self): form = GeomVertexFormat.getV3() vdata = GeomVertexData("myLine", form, Geom.UHStatic) vdata.setNumRows(1) vertex = GeomVertexWriter(vdata, "vertex") vertex.addData3f(0, 0, 0) vertex.addData3f(60, 0, 50) prim = GeomLines(Geom.UHStatic) prim.addVertices(0, 1) geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode("gnode") node.addGeom(geom) nodePath = self.render.attachNewNode(node)
def CreateProximalSynapses(self, inputObjects, inputs, synapses): for child in self.__cellsNodePath.getChildren(): if child.getName() == "ProximalSynapseLine": child.removeNode() printLog("Creating proximal synapses", verbosityMedium) printLog("To inputs called:" + str(inputObjects), verbosityMedium) printLog("Synapses count:" + str(len(synapses)), verbosityMedium) printLog("active:" + str(sum([i for i in synapses])), verbosityHigh) # inputs are divided into separate items in list - [input1,input2,input3] # synapses are one united array [1,0,0,1,0,1,0...] # length is the same # synapses can be connected to one input or to several inputs # if to more than one - split synapses array synapsesDiv = [] offset = 0 for inputObj in inputObjects: synapsesDiv.append(synapses[offset:offset + inputs[inputObj].count]) offset += inputs[inputObj].count for i in range(len(synapsesDiv)): # for each input object inputs[ inputObjects[i]].resetProximalFocus() # clear color highlight for y in range(len(synapsesDiv[i]) ): # go through every synapse and check activity if synapsesDiv[i][y] == 1: form = GeomVertexFormat.getV3() vdata = GeomVertexData("ProximalSynapseLine", form, Geom.UHStatic) vdata.setNumRows(1) vertex = GeomVertexWriter(vdata, "vertex") vertex.addData3f( inputs[inputObjects[i]].inputBits[y].getNode().getPos( self.__node)) vertex.addData3f(0, 0, 0) # vertex.addData3f(self.__node.getPos()) # printLog("Inputs:"+str(i)+"bits:"+str(y)) # printLog(inputs[i].inputBits[y].getNode().getPos(self.__node)) # highlight inputs[inputObjects[i]].inputBits[y].setProximalFocus( ) # highlight connected bits prim = GeomLines(Geom.UHStatic) prim.addVertices(0, 1) geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode("ProximalSynapse") node.addGeom(geom) nodePath = self.__cellsNodePath.attachNewNode(node) nodePath.setRenderModeThickness(2) # color of the line if inputs[inputObjects[i]].inputBits[y].active: nodePath.setColor(COL_PROXIMAL_SYNAPSES_ACTIVE) else: nodePath.setColor(COL_PROXIMAL_SYNAPSES_INACTIVE)
def CreateProximalSynapses(self, inputNames, inputObj, permanences, thresholdConnected, createOnlyActive=False): self.DestroyProximalSynapses() printLog("Creating proximal permanences", verbosityMedium) printLog("To inputObj called:" + str(inputNames), verbosityMedium) printLog("permanences count:" + str(len(permanences)), verbosityMedium) printLog("active:" + str(sum([i for i in permanences])), verbosityHigh) # inputObj are divided into separate items in list - [input1,input2,input3] # permanences are one united array [1,0,0,1,0,1,0...] # length is the same # synapses can be connected to one input or to several inputObj # if to more than one - split synapses array synapsesDiv = [] offset = 0 for inputName in inputNames: synapsesDiv.append(permanences[offset : offset + inputObj[inputName].count]) offset += inputObj[inputName].count for i in range(len(synapsesDiv)): # for each input object inputObj[inputNames[i]].resetProximalFocus() # clear color highlight for y in range( len(synapsesDiv[i]) ): # go through every synapse and check if its connected (we are comparing permanences) if synapsesDiv[i][y] >= thresholdConnected: if createOnlyActive and not inputObj[inputNames[i]].inputBits[y].active:# we want to show only active synapses continue form = GeomVertexFormat.getV3() vdata = GeomVertexData("ProximalSynapseLine", form, Geom.UHStatic) vdata.setNumRows(1) vertex = GeomVertexWriter(vdata, "vertex") vertex.addData3f( inputObj[inputNames[i]] .inputBits[y] .getNode() .getPos(self.__node) ) vertex.addData3f(0, 0, 0) # vertex.addData3f(self.__node.getPos()) # printLog("inputObj:"+str(i)+"bits:"+str(y)) # printLog(inputObj[i].inputBits[y].getNode().getPos(self.__node)) # highlight inputObj[inputNames[i]].inputBits[ y ].setProximalFocus() # highlight connected bits prim = GeomLines(Geom.UHStatic) prim.addVertices(0, 1) geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode("ProximalSynapse") node.addGeom(geom) nodePath = self.__cellsNodePath.attachNewNode(node) nodePath.setRenderModeThickness(2) # color of the line if inputObj[inputNames[i]].inputBits[y].active: nodePath.setColor(COL_PROXIMAL_SYNAPSES_ACTIVE) else: nodePath.setColor(COL_PROXIMAL_SYNAPSES_INACTIVE)
def CreateDistalSynapses(self, HTMObject, layer, data, inputObjects): for child in self.__node.getChildren(): if child.getName() == "DistalSynapseLine": child.removeNode() printLog("Creating distal synapses", verbosityMedium) printLog("EXTERNAL DISTAL:" + str(inputObjects)) printLog("HTM inputs:" + str(HTMObject.inputs)) printLog("HTM layers:" + str(HTMObject.layers)) for segment in data: for presynCellID in segment: cellID = presynCellID % layer.nOfCellsPerColumn colID = (int)(presynCellID / layer.nOfCellsPerColumn) if colID < len(layer.minicolumns): presynCell = layer.minicolumns[colID].cells[ cellID] # it is within current layer else: # it is for external distal input cellID = presynCellID - len( layer.minicolumns) * layer.nOfCellsPerColumn for inputObj in inputObjects: if inputObj in HTMObject.inputs: if cellID < HTMObject.inputs[inputObj].count: presynCell = HTMObject.inputs[ inputObj].inputBits[cellID] break else: # not this one cellID -= HTMObject.inputs[inputObj].count elif inputObj in HTMObject.layers: if cellID < HTMObject.layers[ inputObj].nOfCellsPerColumn * len( HTMObject.layers[inputObj].minicolumns ): presynCell = HTMObject.layers[ inputObj].minicolumns[(int)( cellID / HTMObject.layers[inputObj]. nOfCellsPerColumn)].cells[ cellID % HTMObject. layers[inputObj].nOfCellsPerColumn] break else: # not this one cellID -= HTMObject.layers[ inputObj].nOfCellsPerColumn * len( HTMObject.layers[inputObj].minicolumns) presynCell.setPresynapticFocus( ) # highlight presynapctic cells form = GeomVertexFormat.getV3() vdata = GeomVertexData("DistalSynapseLine", form, Geom.UHStatic) vdata.setNumRows(1) vertex = GeomVertexWriter(vdata, "vertex") vertex.addData3f(presynCell.getNode().getPos(self.__node)) vertex.addData3f(0, 0, 0) prim = GeomLines(Geom.UHStatic) prim.addVertices(0, 1) geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode("DistalSynapse") node.addGeom(geom) nodePath = self.__node.attachNewNode(node) nodePath.setRenderModeThickness(2) # color of the line if presynCell.active: nodePath.setColor(COL_DISTAL_SYNAPSES_ACTIVE) else: nodePath.setColor(COL_DISTAL_SYNAPSES_INACTIVE)
def __init__(self, size, translation, rotation, color, text): self._visible = False wy, wx, wz = size[0], size[1], size[2] format = GeomVertexFormat.getV3c4() vdata = GeomVertexData('cu_points', format, Geom.UHStatic) vdata.setNumRows(8) self._pos_writer = GeomVertexWriter(vdata, 'vertex') self._color_writer = GeomVertexWriter(vdata, 'color') self._pos_writer.set_row(0) self._color_writer.set_row(0) self._pos_writer.addData3f(-0.5 * wx, -0.5 * wy, 0.) self._pos_writer.addData3f(-0.5 * wx, -0.5 * wy, wz) self._pos_writer.addData3f(0.5 * wx, -0.5 * wy, wz) self._pos_writer.addData3f(0.5 * wx, -0.5 * wy, 0.) self._pos_writer.addData3f(-0.5 * wx, 0.5 * wy, 0.) self._pos_writer.addData3f(-0.5 * wx, 0.5 * wy, wz) self._pos_writer.addData3f(0.5 * wx, 0.5 * wy, wz) self._pos_writer.addData3f(0.5 * wx, 0.5 * wy, 0.) for i in range(8): self._color_writer.addData4f(color[0], color[1], color[2], color[3]) lines = GeomLines(Geom.UHStatic) lines.addVertices(0, 1) lines.addVertices(1, 2) lines.addVertices(2, 3) lines.addVertices(3, 0) lines.addVertices(4, 5) lines.addVertices(5, 6) lines.addVertices(6, 7) lines.addVertices(7, 4) lines.addVertices(0, 4) lines.addVertices(1, 5) lines.addVertices(2, 6) lines.addVertices(3, 7) cuboid = Geom(vdata) cuboid.addPrimitive(lines) node = GeomNode('cuboid') node.addGeom(cuboid) self._node_path = NodePath(node) # self.title = OnscreenText(text=text, style=1, fg=(1, 1, 1, 1), pos=(-0.1, 0.1), scale=.05, # parent=self._node_path, align=TextNode.ARight) self._txt_node = TextNode('id') self._txt_node.setText(text) self._txt_node.setTextScale(0.2) self._txt_node.setCardColor(0, 0, 1, 1) self._txt_node.setCardAsMargin(0, 0, 0, 0) self._txt_node.setCardDecal(True) self._txt_node.set_align(2) text_geom = GeomNode('text') text_geom.addChild(self._txt_node) self._txt_np = NodePath(text_geom) self._txt_np.reparentTo(self._node_path) self.show() self.update_values(size, translation, rotation, color, text)