def polygon_simplify(self, polygon, arc_count): # loading the polygon will make all arcs discrete path = g.trimesh.load_path(polygon) # save the md5 before doing operations md5_pre = g.deepcopy(path.md5()) # this should return a copy of the path simplified = path.simplify() # make sure the simplify call didn't alter our original mesh assert path.md5() == md5_pre for garbage in range(2): # the simplified version shouldn't have lost area assert g.np.allclose(path.area, simplified.area, rtol=1e-3) # see if we fit as many arcs as existed in the original drawing new_count = sum( int(type(i).__name__ == 'Arc') for i in simplified.entities) if new_count != arc_count: print(new_count, arc_count) if arc_count > 1: g.log.info('originally were {} arcs, simplify found {}'.format( arc_count, new_count)) assert new_count > 0 assert new_count <= arc_count # dump the cache to make sure bookkeeping wasn't busted or wrong simplified._cache.clear() # make sure the simplify call didn't alter our original mesh assert path.md5() == md5_pre
def test_conversion(self): m = g.get_mesh('machinist.XAML') assert m.visual.kind == 'face' # unmerge vertices so we don't get average colors m.unmerge_vertices() # store initial face colors initial = g.deepcopy(m.visual.face_colors.copy()) # assign averaged vertex colors as default m.visual.vertex_colors = m.visual.vertex_colors assert m.visual.kind == 'vertex' m.visual._cache.clear() assert g.np.allclose(initial, m.visual.face_colors)
def polygon_simplify(self, polygon): path = g.trimesh.load_path(polygon) md5_pre = g.deepcopy(path.md5()) simplified = path.simplify() # make sure the simplify call didn't alter our original mesh self.assertTrue(path.md5() == md5_pre) area_ratio = path.area / simplified.area arc_count = len( [i for i in simplified.entities if type(i).__name__ == 'Arc']) g.log.info('simplify area ratio was %f with %d arcs', area_ratio, arc_count) self.assertTrue(abs(area_ratio - 1) < 1e-2)
def polygon_simplify(self, polygon, arc_count): # loading the polygon will make all arcs discrete path = g.trimesh.load_path(polygon) # save the md5 before doing operations md5_pre = g.deepcopy(path.md5()) # this should return a copy of the path simplified = path.simplify() # make sure the simplify call didn't alter our original mesh assert path.md5() == md5_pre for garbage in range(2): # the simplified version shouldn't have lost area assert g.np.allclose(path.area, simplified.area, rtol=1e-3) # see if we fit as many arcs as existed in the original drawing new_count = sum(int(type(i).__name__ == 'Arc') for i in simplified.entities) if new_count != arc_count: print(new_count, arc_count) if arc_count > 1: g.log.info('originally were {} arcs, simplify found {}'.format( arc_count, new_count)) assert new_count > 0 assert new_count <= arc_count # dump the cache to make sure bookkeeping wasn't busted or wrong simplified._cache.clear() # make sure the simplify call didn't alter our original mesh assert path.md5() == md5_pre
def test_meshes(self): self.meshes = g.get_meshes() has_gt = g.deepcopy(g.trimesh.graph._has_gt) if not has_gt: g.log.warning('No graph-tool to test!') g.log.info('Running tests on %d meshes', len(self.meshes)) for mesh in self.meshes: g.log.info('Testing %s', mesh.metadata['file_name']) self.assertTrue(len(mesh.faces) > 0) self.assertTrue(len(mesh.vertices) > 0) self.assertTrue(len(mesh.edges) > 0) self.assertTrue(len(mesh.edges_unique) > 0) self.assertTrue(len(mesh.edges_sorted) > 0) self.assertTrue(len(mesh.edges_face) > 0) self.assertFalse(mesh.euler_number is None) mesh.process() tic = [g.time.time()] if has_gt: g.trimesh.graph._has_gt = True split = g.trimesh.graph.split(mesh) tic.append(g.time.time()) facets = g.trimesh.graph.facets(mesh) tic.append(g.time.time()) g.trimesh.graph._has_gt = False split = g.trimesh.graph.split(mesh) tic.append(g.time.time()) facets = g.trimesh.graph.facets(mesh) tic.append(g.time.time()) facets, area = mesh.facets(return_area=True) self.assertTrue(len(facets) == len(area)) if len(facets) == 0: continue faces = facets[g.np.argmax(area)] outline = mesh.outline(faces) smoothed = mesh.smoothed() if has_gt: times = g.np.diff(tic) g.log.info('Graph-tool sped up split by %f and facets by %f', (times[2] / times[0]), (times[3] / times[1])) g.trimesh.graph._has_gt = g.deepcopy(has_gt) self.assertTrue(mesh.volume > 0.0) section = mesh.section(plane_normal=[0, 0, 1], plane_origin=mesh.centroid) hull = mesh.convex_hull volume_ok = hull.volume > 0.0 if not volume_ok: g.log.error('zero hull volume for %s', mesh.metadata['file_name']) self.assertTrue(volume_ok) sample = mesh.sample(1000) even_sample = g.trimesh.sample.sample_surface_even(mesh, 100) self.assertTrue(sample.shape == (1000, 3)) g.log.info('finished testing meshes') # make sure vertex kdtree and triangles rtree exist t = mesh.kdtree() self.assertTrue(hasattr(t, 'query')) g.log.info('Creating triangles tree') r = mesh.triangles_tree() self.assertTrue(hasattr(r, 'intersection')) g.log.info('Triangles tree ok') # some memory issues only show up when you copy the mesh a bunch # specifically, if you cache c- objects then deepcopy the mesh this # generally segfaults somewhat randomly copy_count = 200 g.log.info('Attempting to copy mesh %d times', copy_count) for i in range(copy_count): copied = mesh.copy() g.log.info('Multiple copies done') self.assertTrue(g.np.allclose(copied.identifier, mesh.identifier))