def testToPyString(): t = n.ObjectTable() assert (t.pyStr() == "{}") t[0] = '1' t['1'] = 0.0 t[2] = p.V3f(0, 1, 2) assert (t.pyStr() == '{"1": 0., 0: "1", 2: V3f(0, 1, 2)}')
def render(self, detailSize): # set up some buffers count = 20000 P = [] Cs = [] w = [] random.seed(997) for i in range(count): P.append( pimath.V3f(random.uniform(-self.dim, self.dim), random.uniform(-self.dim, self.dim), random.uniform(-self.dim, self.dim))) Cs.append( pimath.V3f(random.uniform(0.05, 1), random.uniform(0.05, 1), random.uniform(0.05, 1))) w.append(random.uniform(0.5 * self.point_radius, self.point_radius)) # form a table from our buffers with required renderman info t = n.ObjectTable() t['P'] = n.V3fBuffer(count, pimath.V3f(0, 0, 0)) t['P'].contents = P t['P'].attribs['token'] = 'P' if 1: # colour per point t['Cs'] = n.V3fBuffer(count, pimath.V3f(0, 0, 0)) t['Cs'].contents = Cs t['Cs'].attribs['token'] = 'vertex color Cs' else: # constant colour across all points t['Cs'] = n.AttribTable() t['Cs']['value'] = pimath.V3f(1, 0, 0) t['Cs']['token'] = 'constant color Cs' if 0: # varying width t['width'] = n.FloatBuffer(count, 0.0) t['width'].contents = w t['width'].attribs['token'] = 'varying float width' else: if 1: # constants either as attrib table t['width'] = n.AttribTable() t['width']['value'] = 0.05 t['width']['token'] = 'constant float constantwidth' else: # or buffer of length 1 t['width'] = n.FloatBuffer(1, 0.03) t['width'].attribs['token'] = 'constant float constantwidth' # render ri.TransformBegin() ri.Translate(self.centre[0], self.centre[1], self.centre[2]) nd.points(t) ri.TransformEnd()
def testToTupleString(): t = n.ObjectTable() assert (t.tupStr() == "{}") t[0] = '1' t['1'] = 0.0 t[2] = p.V3f(0, 1, 2) assert (t.tupStr() == '{"1": 0., 0: "1", 2: (0, 1, 2)}') exec("m=" + t.tupStr()) assert (m == {"1": 0., 0: "1", 2: (0, 1, 2)})
def testTableCloning(): t = n.ObjectTable() t[1] = 1 t["two"] = "222" t[3] = n.IntBuffer(100) t[4] = t[3] t[5] = t[3].clone() assert (t[3].storeUseCount() == 2) t2 = t.clone() assert (t.keys() == t2.keys()) assert (t2[1] == 1) assert (t2["two"] == "222") assert (t2[3] != t[3]) assert (t2[3].hasSharedStore(t[3])) assert (t2[4] == t2[3]) assert (t[3].storeUseCount() == 4)
def testDelayLoadAndPreCloning(ext): # create test data on disk filepath = "/tmp/clpre." + ext sz = 13 t_ = n.ObjectTable() b_ = n.IntBuffer(sz) b2_ = b_.clone() t_[1] = b_ t_[2] = b2_ n.save(t_, filepath) # test1: Make sure that when buffers are loaded, their cloned relationships are kept intact t = n.load(filepath) assert (t.keys() == t_.keys()) assert (t[1].hasSharedStore(t[2])) t[2][0] # force resident via zeroeth element read assert (t[1].clientSize() == sz) assert (t[1].hasSharedStore(t[2]))
def testDelayLoad(ext): # create test data on disk filepath = "/tmp/dl1." + ext sz = 100 t_ = n.ObjectTable() for i in range(10): t_[i] = n.IntBuffer(sz) n.save(t_, filepath) # test1: make sure a buffer's data isn't made resident until it's accessed t = n.load(filepath) expected_count = sz for i in t.iteritems(): i[1][0] # force resident via zeroeth element read count = 0 for j in t.iteritems(): count += j[1].clientSize() assert (count == expected_count) expected_count += sz
def render(self, detailSize): # form a table from our buffers with required renderman info t = n.ObjectTable() t['radius'] = self.radius t['zmin'] = -self.radius t['zmax'] = self.radius t['thetamax'] = 360.0 # constant colour t['Cs'] = n.AttribTable() t['Cs']['value'] = pimath.V3f(1, 0.5, 0) t['Cs']['token'] = 'constant color Cs' # render ri.TransformBegin() ri.Translate(self.centre[0], self.centre[1], self.centre[2]) nd.sphere(t) ri.TransformEnd()
def createImage(): w = 320 h = 240 b = n.V3fBuffer(w * h, p.V3f(0, 0, 0)) print '# creating pixel data' for y in range(h): for x in range(w): s = float(x) / w t = float(y) / h b[y * w + x] = p.V3f(s, t, 0) img = n.ObjectTable() img['xres'] = w img['yres'] = h img['pixels'] = b # check that this is a valid napalm image assert ni.isValid(img, True) return img
def testTable(): t = n.ObjectTable() assert (len(t) == 0) fail = False try: t["foo"] except KeyError: fail = True assert (fail) t[1] = 1.111 t["two"] = 68 t[3] = p.V3f(5, 6, 7) t["four"] = 'Q' assert (len(t) == 4) assert ("two" in t) assert ("blah" not in t) assert (t["two"] == 68) assert (t["four"] == 'Q') vdiff = t[3] - p.V3f(5, 6, 7) assert (vdiff.length() < 0.0001) t["buf"] = n.IntBuffer(100) t["buf"][50] = 50 assert (t["buf"][50] == 50) del t["four"] del t[3] assert (len(t) == 3) assert (3 not in t) keys = t.keys() keys2 = [] for k in t: keys2.append(k) assert (keys == keys2)
def createUberTable(): t = n.ObjectTable() t[1] = 1 # this is interpreted as an int t[2] = 2.2 # this is interpreted as a float t[3] = n.Double(3.333) t[4] = "hello" t[5] = n.Char(-10) # in napalm, a char is interpreted as a number t[6] = n.UChar(200) # as is an unsigned char t[7] = n.Short(-30000) t[8] = n.UShort(60000) t[9] = n.UInt(2000000000) # todo add the new imath types inc half t[10] = p.V3f() t[11] = p.V3d() t[12] = p.M33f() t[13] = p.M33d() t[14] = p.M44f() t[15] = p.M44d() sz = 100 t[16] = n.CharBuffer(sz) t[17] = n.FloatBuffer(sz) t[18] = n.DoubleBuffer(sz) t[19] = n.IntBuffer(sz) t[20] = n.V3fBuffer(sz) t[21] = n.V3dBuffer(sz) t[22] = n.M33fBuffer(sz) t[23] = n.M33dBuffer(sz) t[24] = n.M44fBuffer(sz) t[25] = n.M44dBuffer(sz) t[16].attribs["a1"] = "aaa1" t["hey"] = "ho" return t
def render(self, detailSize): # set up some buffers count = 200 P = [] Cs = [] w = [] nvertices = [] random.seed(997) for i in range(count): cv_count = random.randint(5, 20) nvertices.append(cv_count) p0 = pimath.V3f(random.uniform(-self.dim, self.dim), random.uniform(-self.dim, self.dim), random.uniform(-self.dim, self.dim)) for cv in range(cv_count): p0 = p0 + pimath.V3f(random.uniform(-self.dim, self.dim), random.uniform(-self.dim, self.dim), random.uniform(-self.dim, self.dim)) * 0.1 P.append(p0) Cs.append( pimath.V3f(random.uniform(0.05, 1), random.uniform(0.05, 1), random.uniform(0.05, 1))) w.append( random.uniform(0.1 * self.curve_width, self.curve_width)) # form a table from our buffers with required renderman info t = n.ObjectTable() #t['type'] = 'linear' t['type'] = 'cubic' #t['wrap'] = 'periodic' t['wrap'] = 'nonperiodic' t['nvertices'] = n.IntBuffer(count, len(nvertices)) t['nvertices'].contents = nvertices # t['nvertices'].attribs['to] t['P'] = n.V3fBuffer(count, pimath.V3f(0, 0, 0)) t['P'].contents = P t['P'].attribs['token'] = 'P' if 1: # colour per point t['Cs'] = n.V3fBuffer(count, pimath.V3f(0, 0, 0)) t['Cs'].contents = Cs t['Cs'].attribs['token'] = 'vertex color Cs' else: # constant colour across all points t['Cs'] = n.AttribTable() t['Cs']['value'] = pimath.V3f(1, 0, 0) t['Cs']['token'] = 'constant color Cs' if 1: # varying width t['width'] = n.FloatBuffer(count, 0.0) t['width'].contents = w t['width'].attribs['token'] = 'vertex float width' else: if 1: # constants either as attrib table t['width'] = n.AttribTable() t['width']['value'] = 0.05 t['width']['token'] = 'constant float constantwidth' else: # or buffer of length 1 t['width'] = n.FloatBuffer(1, 0.03) t['width'].attribs['token'] = 'constant float constantwidth' # render ri.TransformBegin() ri.Translate(self.centre[0], self.centre[1], self.centre[2]) nd.curves(t) ri.TransformEnd()
def writeNapalm(self, nap_file_name, curve_object, debug=False, map_file_name=None, software=None, app_version=None): """ This function will write curve object to napalm file and napalm channel file. :param nap_file_name: Filepath for writting napalm data :type nap_file_name: string :param curve_object: Curve object to convert as napalm file :type curve_object: curve class object :param debug: This option will turn on the debug output :type debug: bool :param map_file_name: Filepath for writting napalm channel data :type map_file_name: string .. note:: This function will support only few tangents and list is * flat * linear * spline * fixed * clamped .. warning:: This function will endup with error if you pass wrong curve object structure. .. seealso:: * :func:`writeMappingTable` .. versionchanged:: 0.0.5 Fixed the map_file_name fixed. .. todo:: More in-line comment should be added :return: Write Status,Nap File Path,Map File Path :rtype: bool,string,string Example >>> <ObjectTable @ 0x241f730> 0: <FloatBuffer at 0x23826e0 (FloatCpuStore[4] at 0x2417190)> 1: <StringBuffer at 0x24a48f0 (StringCpuStore[4] at 0x239c330)> 2: <FloatBuffer at 0x2411040 (FloatCpuStore[4] at 0x24bed30)> 3: <StringBuffer at 0x2410c80 (StringCpuStore[4] at 0x2410cc0)> 4: <FloatBuffer at 0x239a630 (FloatCpuStore[4] at 0x239a670)> 5: <FloatBuffer at 0x2398120 (FloatCpuStore[4] at 0x2398160)> 6: <FloatBuffer at 0x239bb80 (FloatCpuStore[4] at 0x239bbc0)> 7: <FloatBuffer at 0x2388c20 (FloatCpuStore[4] at 0x2388c60)> 8: <FloatBuffer at 0x2388e90 (FloatCpuStore[4] at 0x238af50)> 9: <FloatBuffer at 0x238b150 (FloatCpuStore[4] at 0x238b190)> 10: <FloatBuffer at 0x22405c0 (FloatCpuStore[4] at 0x2240600)> """ self.napalm_data = curve_object nap_main_table = nap_core.ObjectTable() nap_status = False nap_file = None map_file = None counter_index = 0 for each_node in self.napalm_data: curve_class = each_node[2] object_node = [] object_dict = {} for eachCurve in curve_class: flot_attr_list = ['time', 'key_value', 'in_weight', 'out_weight', 'in_angle', \ 'out_angle', 'in_slope', 'out_slope'] curve_attr = str(eachCurve[1]) map_data = {} for key in eachCurve[-1].keys(): dict_key_val = eachCurve[-1][key] if key in flot_attr_list: nap_main_table[counter_index] = nap_core.FloatBuffer( len(dict_key_val)) else: nap_main_table[counter_index] = nap_core.StringBuffer( len(dict_key_val)) nap_main_table[counter_index].contents = dict_key_val map_data.update({key: counter_index}) counter_index += 1 object_node.append([curve_attr, map_data]) object_dict.update({each_node[0]: [each_node[1], object_node]}) self.mapping_data.append(object_dict) if debug: nap_core.dump(nap_main_table) try: nap_core.save(nap_main_table, nap_file_name) map_file = self.writeMappingTable(nap_file_name, map_file_name, \ software = software, app_version = app_version) nap_file = nap_file_name nap_status = True self.napalm_data = [] self.mapping_data = [] except: traceback.print_exc() nap_status = False return (nap_status, map_file, nap_file)
def writeMappingTable(self, nap_file_name, map_file_name=None, software=None, app_version=None): """ This function will write curve object to napalm file and napalm channel file. :param nap_file_name: Filepath for writting napalm data' :type nap_file_name: string :param map_file_name: Filepath for writting napalm channel data :type map_file_name: string .. note:: This function will write out mapping data for the channel. Example >>> "pSphere2": <ObjectTable @ 0x1cfe910> "eye_val": "right" "rotateX": <ObjectTable @ 0x1cfea90> "in_angle": 99 "in_slope": 98 "in_tan_type": 93 "in_weight": 92 "key_value": 90 "out_angle": 94 "out_slope": 96 "out_tan_type": 91 "out_weight": 95 "time": 97 .. seealso:: * :func:`writeNapalm` .. versionchanged:: 0.0.5 Fixed the map_file_name fixed. .. todo:: More in-line comment should be added :return: Map File Path :rtype: string """ if not map_file_name: ext_spliter = os.path.splitext(os.path.basename(nap_file_name)) get_file_ext = ext_spliter[-1] set_file_base = "%s_map%s" % (ext_spliter[0], get_file_ext) map_file_name = "%s/%s" % (os.path.dirname(nap_file_name), set_file_base) map_main_table = nap_core.ObjectTable() for each_map_obj in self.mapping_data: object_keys = each_map_obj.keys() map_obj_table = nap_core.ObjectTable() for each_key in object_keys: obj_key_val = each_map_obj[each_key][-1] eye_value = each_map_obj[each_key][-0] map_obj_table["eye_val"] = eye_value for each_curve in obj_key_val: nap_map_table = nap_core.ObjectTable() dic_val = each_curve[-1].keys() for each_dict_key in dic_val: nap_map_table[each_dict_key] = each_curve[-1][ each_dict_key] map_obj_table[each_curve[0]] = nap_map_table map_main_table[each_key] = map_obj_table header_table = nap_core.ObjectTable() software_arg = str(software).split(",")[0] version = str(software).split(",")[1] date_time = time.strftime("%m/%d/%y-%H-%M-%S", time.localtime()) header_table["nap_file"] = nap_file_name header_table["date_time"] = date_time header_table["kip_version"] = os.getenv("DRD_KIP_VERSION") header_table["app_version"] = app_version header_table["client_software"] = software_arg header_table["client_version"] = version header_table["user"] = os.getenv("USER") map_main_table["header"] = header_table try: nap_core.save(map_main_table, map_file_name) map_file_name = map_file_name except: traceback.print_exc() map_file_name = None return map_file_name
import napalm.core as nc import napalm.parsing as np print("testing napalm v" + nc.getAPIVersion() + " (parsing)...") # create napalm table t = nc.ObjectTable() t[1] = "one" t["two"] = 2 t["tbl"] = nc.ObjectTable() t["tbl"][55] = 66 t["tbl"]["hey"] = "ho" # parse equivalent python dict s = "{1:'one', 'two':2, 'tbl':{55:66, 'hey':'ho'}}" t_pydict = np.parseDict(s) # parse equivalent xml # TODO this isn't useful until we have full collapse supported # test for equality assert (nc.areEqual(t, t_pydict)) # test for equality after clone t_ = t.clone() assert (nc.areEqual(t_, t_pydict)) # test for inequality after entry addition t_ = t.clone() t_[2] = "extra_entry" assert (not nc.areEqual(t_, t_pydict))