From 3576c63c246c0eaaacfc8800cf02171c3c5c2927 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Sat, 19 Mar 2022 00:39:22 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- create_latex_images.py | 28 ++++----- deodr/differentiable_renderer.py | 22 +++---- deodr/examples/depth_image_hand_fitting.py | 23 +++----- deodr/examples/mesh_viewer.py | 59 ++++++++----------- deodr/examples/rgb_image_hand_fitting.py | 27 ++++----- deodr/examples/rgb_multiview_hand.py | 29 ++++----- deodr/examples/triangle_soup_fitting.py | 16 ++--- deodr/mesh_fitter.py | 11 +--- deodr/opengl/moderngl.py | 7 +-- deodr/opengl/pyrender.py | 13 +--- deodr/pytorch/triangulated_mesh_pytorch.py | 10 +--- .../triangulated_mesh_tensorflow.py | 7 +-- deodr/tools.py | 12 ++-- deodr/triangulated_mesh.py | 29 +++------ setup.py | 6 +- tests/test_pixel_center_coordinates.py | 6 +- tests/test_render_mesh.py | 2 +- 17 files changed, 115 insertions(+), 192 deletions(-) diff --git a/create_latex_images.py b/create_latex_images.py index 419beff..8078258 100644 --- a/create_latex_images.py +++ b/create_latex_images.py @@ -37,6 +37,7 @@ """Script to render latex figure for each equation found in readme.md""" + import os import re import shutil @@ -49,7 +50,7 @@ dirpath = tempfile.mkdtemp() # ... do stuff with dirpath -print("temporary directory for latex compilation = %s" % dirpath) +print(f"temporary directory for latex compilation = {dirpath}") if len(sys.argv) == 1: texfile = "./readme.md" elif len(sys.argv) == 2: @@ -61,26 +62,25 @@ def formula_as_file(formula, file, negate=False, header=""): laxtex_tmp_file = os.path.join(dirpath, "tmp_equation.tex") pdf_tmp_file = os.path.join(dirpath, "tmp_equation.pdf") - latexfile = open(laxtex_tmp_file, "w") - latexfile.write("\\documentclass[preview]{standalone}") - # latexfile.write('\\input{header.tex}') - latexfile.write("\\usepackage{wasysym}") - latexfile.write("\\usepackage{amssymb}") - latexfile.write("\n\\begin{document}") - latexfile.write(" %s" % formula) - latexfile.write("\n\\end{document} ") - latexfile.close() + with open(laxtex_tmp_file, "w") as latexfile: + latexfile.write("\\documentclass[preview]{standalone}") + # latexfile.write('\\input{header.tex}') + latexfile.write("\\usepackage{wasysym}") + latexfile.write("\\usepackage{amssymb}") + latexfile.write("\n\\begin{document}") + latexfile.write(f" {formula}") + latexfile.write("\n\\end{document} ") os.system('pdflatex -output-directory="%s" %s' % (dirpath, laxtex_tmp_file)) if file.startswith("https://rawgithub.com") or file.startswith( "https://raw.githack.com" ): file = "./" + re.findall(r"""/master/(.*)""", file)[0] if file[-3:] == "svg": - os.system("pdf2svg %s %s" % (pdf_tmp_file, file)) + os.system(f"pdf2svg {pdf_tmp_file} {file}") elif file[-3:] == "pdf": shutil.copyfile(pdf_tmp_file, file) else: - os.system("convert -density 100 %s -quality 90 %s" % (pdf_tmp_file, file)) + os.system(f"convert -density 100 {pdf_tmp_file} -quality 90 {file}") raw = open(texfile) @@ -91,10 +91,10 @@ def formula_as_file(formula, file, negate=False, header=""): listname = set() for eqn in latex_equations: if eqn[1] in listname: - raise Exception("equation image file %s already used" % eqn[1]) + raise Exception(f"equation image file {eqn[1]} already used") listname.add(eqn[1]) - print("creating %s" % eqn[1]) + print(f"creating {eqn[1]}") formula_as_file(eqn[0], eqn[1]) print("done") diff --git a/deodr/differentiable_renderer.py b/deodr/differentiable_renderer.py index 99836c6..b25c547 100644 --- a/deodr/differentiable_renderer.py +++ b/deodr/differentiable_renderer.py @@ -23,8 +23,8 @@ def renderScene( # when installed from a wheel. this also make interactive debugging easier # for the library user - assert not (image is None) - assert not (z_buffer is None) + assert image is not None + assert z_buffer is not None height = image.shape[0] width = image.shape[1] nb_colors = image.shape[2] @@ -107,8 +107,8 @@ def renderSceneB( # when installed from a wheel. this also make inderactive debugginh easier # for the library user - assert not (image is None) - assert not (z_buffer is None) + assert image is not None + assert z_buffer is not None height = image.shape[0] width = image.shape[1] @@ -187,7 +187,7 @@ def renderSceneB( assert obs.shape[0] == height assert obs.shape[1] == width else: - assert not (image_b is None) + assert image_b is not None assert image_b.shape[0] == height assert image_b.shape[1] == width @@ -350,9 +350,7 @@ def project_points_backward( ) if depths_b is not None: p_camera_b[:, 2] += depths_b - points_3d_b = p_camera_b.dot(self.extrinsic[:3, :3].T) - - return points_3d_b + return p_camera_b.dot(self.extrinsic[:3, :3].T) def get_center(self): return -self.extrinsic[:3, :3].T.dot(self.extrinsic[:, 3]) @@ -763,8 +761,7 @@ def compute_vertices_luminosity(self): directional = np.zeros((self.mesh.nb_vertices)) if self.store_backward_current is not None: self.store_backward_current["compute_vertices_luminosity"] = directional - vertices_luminosity = directional + self.light_ambient - return vertices_luminosity + return directional + self.light_ambient def _compute_vertices_colors_with_illumination(self): @@ -884,10 +881,7 @@ def render(self, camera, return_z_buffer=False, backface_culling=True): self.edgeflags, ) # store this field as it could be overwritten when # rendering several views - if return_z_buffer: - return image, z_buffer - else: - return image + return (image, z_buffer) if return_z_buffer else image def render_backward(self, image_b): if self.perspective_correct: diff --git a/deodr/examples/depth_image_hand_fitting.py b/deodr/examples/depth_image_hand_fitting.py index f573cfd..152a21b 100644 --- a/deodr/examples/depth_image_hand_fitting.py +++ b/deodr/examples/depth_image_hand_fitting.py @@ -80,23 +80,16 @@ def run( combined_image = np.column_stack( (depth_image, synthetic_depth, 3 * diff_image) ) - if display: - cv2.imshow("animation", cv2.resize(combined_image, None, fx=2, fy=2)) - if save_images: - imsave( - os.path.join(iterfolder, f"depth_hand_iter_{niter}.png"), - combined_image, - ) + if display: + cv2.imshow("animation", cv2.resize(combined_image, None, fx=2, fy=2)) + if save_images: + imsave( + os.path.join(iterfolder, f"depth_hand_iter_{niter}.png"), + combined_image, + ) cv2.waitKey(1) - with open( - os.path.join( - iterfolder, - "depth_image_fitting_result_%s.json" - % str(datetime.datetime.now()).replace(":", "_"), - ), - "w", - ) as f: + with open(os.path.join(iterfolder, f'depth_image_fitting_result_{str(datetime.datetime.now()).replace(":", "_")}.json'), "w") as f: json.dump( { "label": f"{dl_library} {datetime.datetime.now()}", diff --git a/deodr/examples/mesh_viewer.py b/deodr/examples/mesh_viewer.py index 3e6f5d7..f1ab3d1 100644 --- a/deodr/examples/mesh_viewer.py +++ b/deodr/examples/mesh_viewer.py @@ -132,24 +132,23 @@ def mouse_callback(self, event, x, y, flags, param): self.y_last = y if self.right_is_down and not (self.ctrl_is_down): - if self.mode in ["camera_centered", "object_centered_trackball"]: - if np.abs(self.y_last - y) >= np.abs(self.x_last - x): - self.camera.extrinsic[2, 3] += self.z_translation_speed * ( - self.y_last - y - ) - else: - self.rotate( - [ - 0, - 0, - -self.rotation_speed * (self.x_last - x), - ] - ) - self.x_last = x - self.y_last = y + if self.mode not in ["camera_centered", "object_centered_trackball"]: + raise (BaseException(f"unknown camera mode {self.mode}")) + if np.abs(self.y_last - y) >= np.abs(self.x_last - x): + self.camera.extrinsic[2, 3] += self.z_translation_speed * ( + self.y_last - y + ) else: - raise (BaseException(f"unknown camera mode {self.mode}")) + self.rotate( + [ + 0, + 0, + -self.rotation_speed * (self.x_last - x), + ] + ) + self.x_last = x + self.y_last = y if self.middle_is_down or (self.left_is_down and self.ctrl_is_down): # translation @@ -171,8 +170,7 @@ def mouse_callback(self, event, x, y, flags, param): self.y_last = y def print_help(self): - help_str = "" - help_str += "Mouse:\n" + help_str = "" + "Mouse:\n" if self.mode == "object_centered_trackball": help_str += ( @@ -190,7 +188,6 @@ def print_help(self): help_str += "CTRL + mouse left + vertical motion: translate object along camera y axis\n" help_str += "CTRL + mouse left + horizontal motion: translate object along camera x axis\n" - help_str += "SHIFT + mouse left + vertical motion: change the camera field of view\n" else: help_str += ( "mouse right + vertical motion: translate camera along its z axis\n" @@ -204,8 +201,7 @@ def print_help(self): ) help_str += "CTRL + mouse left + vertical motion: translate camera along its y axis\n" help_str += "CTRL + mouse left + horizontal motion: translate camera along its x axis\n" - help_str += "SHIFT + mouse left + vertical motion: change the camera field of view\n" - + help_str += "SHIFT + mouse left + vertical motion: change the camera field of view\n" print(help_str) @@ -429,8 +425,7 @@ def print_fps(self, image, fps): def print_help(self): """Print the help message.""" - help_str = "" - help_str += "-----------------\n" + help_str = "" + "-----------------\n" help_str += "DEODR Mesh Viewer\n" help_str += "-----------------\n" help_str += "Keys:\n" @@ -471,14 +466,13 @@ def toggle_lights(self): light_directional=np.array(self.light_directional), light_ambient=self.light_ambient, ) + elif self.use_moderngl: + self.offscreen_renderer.set_light( + light_directional=(0, 0, 0), + light_ambient=1.0, + ) else: - if self.use_moderngl: - self.offscreen_renderer.set_light( - light_directional=(0, 0, 0), - light_ambient=1.0, - ) - else: - self.scene.set_light(light_directional=None, light_ambient=1.0) + self.scene.set_light(light_directional=None, light_ambient=1.0) def toggle_edge_overdraw_antialiasing(self): """Toggle edge overdraw anti-aliasing (DEODR rendering only).""" @@ -487,10 +481,7 @@ def toggle_edge_overdraw_antialiasing(self): else: self.use_antialiasing = not (self.use_antialiasing) print(f"use_antialiasing = {self.use_antialiasing}") - if self.use_antialiasing: - self.scene.sigma = 1.0 - else: - self.scene.sigma = 0.0 + self.scene.sigma = 1.0 if self.use_antialiasing else 0.0 def pickle_scene_and_cameras(self): """Save scene and camera in a pickle file.""" diff --git a/deodr/examples/rgb_image_hand_fitting.py b/deodr/examples/rgb_image_hand_fitting.py index 3e15721..8174da6 100644 --- a/deodr/examples/rgb_image_hand_fitting.py +++ b/deodr/examples/rgb_image_hand_fitting.py @@ -103,26 +103,19 @@ def run( combined_image = np.column_stack( (hand_image, image, np.tile(diff_image[:, :, None], (1, 1, 3))) ) - if display: - cv2.imshow( - "animation", - cv2.resize(combined_image[:, :, ::-1], None, fx=2, fy=2), - ) - if save_images: - imsave( - os.path.join(iterfolder, f"hand_iter_{niter}.png"), combined_image - ) + if display: + cv2.imshow( + "animation", + cv2.resize(combined_image[:, :, ::-1], None, fx=2, fy=2), + ) + if save_images: + imsave( + os.path.join(iterfolder, f"hand_iter_{niter}.png"), combined_image + ) cv2.waitKey(1) # save convergence curve - with open( - os.path.join( - iterfolder, - "rgb_image_fitting_result_%s.json" - % str(datetime.datetime.now()).replace(":", "_"), - ), - "w", - ) as f: + with open(os.path.join(iterfolder, f'rgb_image_fitting_result_{str(datetime.datetime.now()).replace(":", "_")}.json'), "w") as f: json.dump( { "label": f"{dl_library} {datetime.datetime.now()}", diff --git a/deodr/examples/rgb_multiview_hand.py b/deodr/examples/rgb_multiview_hand.py index af76a35..772d1b8 100644 --- a/deodr/examples/rgb_multiview_hand.py +++ b/deodr/examples/rgb_multiview_hand.py @@ -110,27 +110,20 @@ def run( ), ) ) - if display: - cv2.imshow( - "animation", - cv2.resize(combined_image[:, :, ::-1], None, fx=1, fy=1), - ) - if save_images: - imsave( - os.path.join(iterfolder, f"hand_iter_{niter}.png"), - (combined_image * 255).astype(np.uint8), - ) + if display: + cv2.imshow( + "animation", + cv2.resize(combined_image[:, :, ::-1], None, fx=1, fy=1), + ) + if save_images: + imsave( + os.path.join(iterfolder, f"hand_iter_{niter}.png"), + (combined_image * 255).astype(np.uint8), + ) cv2.waitKey(1) # save convergence curve - with open( - os.path.join( - iterfolder, - "rgb_image_fitting_result_%s.json" - % str(datetime.datetime.now()).replace(":", "_"), - ), - "w", - ) as f: + with open(os.path.join(iterfolder, f'rgb_image_fitting_result_{str(datetime.datetime.now()).replace(":", "_")}.json'), "w") as f: json.dump( { "label": f"{dl_library} {datetime.datetime.now()}", diff --git a/deodr/examples/triangle_soup_fitting.py b/deodr/examples/triangle_soup_fitting.py index 700decd..0dac37e 100644 --- a/deodr/examples/triangle_soup_fitting.py +++ b/deodr/examples/triangle_soup_fitting.py @@ -42,11 +42,9 @@ def create_example_scene(n_tri=30, width=200, height=200, clockwise=False): if np.linalg.det(np.vstack((tmp, np.ones((3))))) > 0: tmp = np.fliplr(tmp) - triangle = {} - triangle["ij"] = tmp.T - triangle["depths"] = np.random.rand(1) * np.ones( + triangle = {"ij": tmp.T, "depths": (np.random.rand(1) * np.ones( (3, 1) - ) # constant depth triangles to avoid collisions + ))} triangle["textured"] = np.random.rand(1) > 0.5 if triangle["textured"]: @@ -69,8 +67,9 @@ def create_example_scene(n_tri=30, width=200, height=200, clockwise=False): ) # all edges are discontinuity edges as no triangle pair share an edge triangles.append(triangle) - scene = {} - for key in [ + scene = {key: np.squeeze( + np.vstack([np.array(triangle[key]) for triangle in triangles]) + ) for key in [ "ij", "depths", "textured", @@ -79,10 +78,7 @@ def create_example_scene(n_tri=30, width=200, height=200, clockwise=False): "colors", "shaded", "edgeflags", - ]: - scene[key] = np.squeeze( - np.vstack([np.array(triangle[key]) for triangle in triangles]) - ) + ]} scene["faces"] = np.arange(3 * n_tri).reshape(-1, 3).astype(np.uint32) scene["faces_uv"] = np.arange(3 * n_tri).reshape(-1, 3).astype(np.uint32) diff --git a/deodr/mesh_fitter.py b/deodr/mesh_fitter.py index f02218b..8818334 100644 --- a/deodr/mesh_fitter.py +++ b/deodr/mesh_fitter.py @@ -112,8 +112,7 @@ def render(self): self.camera, depth_scale=self.depthScale, ) - depth = np.clip(self.depth_not_clipped, 0, self.scene.max_depth) - return depth + return np.clip(self.depth_not_clipped, 0, self.scene.max_depth) def render_backward(self, depth_b): self.scene.clear_gradients() @@ -304,8 +303,7 @@ def render(self): self.mesh.set_vertices_colors( np.tile(self.mesh_color, (self.mesh.nb_vertices, 1)) ) - image = self.scene.render(self.camera) - return image + return self.scene.render(self.camera) def render_backward(self, image_b): self.scene.clear_gradients() @@ -585,10 +583,7 @@ def energy_data(self, vertices, return_images=True): energy_datas[idframe] = coef_data * np.sum(diff_image[idframe]) self.render_backward(image_b) energy_data = np.sum(energy_datas) - if return_images: - return energy_data, image, diff_image - else: - return energy_data + return (energy_data, image, diff_image) if return_images else energy_data def step(self, check_gradient=False): diff --git a/deodr/opengl/moderngl.py b/deodr/opengl/moderngl.py index b710183..0ce7e5e 100644 --- a/deodr/opengl/moderngl.py +++ b/deodr/opengl/moderngl.py @@ -33,7 +33,7 @@ def opencv_to_opengl_perspective(camera, znear, zfar, integer_pixel_centers): np.testing.assert_array_equal( [[fx, 0, cx], [0, fy, cy], [0, 0, 1]], camera.intrinsic ) - m = np.array( + return np.array( [ [2.0 * fx / width, 0, 0, 0], [0, -2.0 * fy / height, 0, 0], @@ -46,7 +46,6 @@ def opencv_to_opengl_perspective(camera, znear, zfar, integer_pixel_centers): [0, 0, 2.0 * zfar * znear / (znear - zfar), 0.0], ] ) - return m class OffscreenRenderer: @@ -191,8 +190,6 @@ def render(self, camera): self.texture.use() self.vao.render() data = self.fbo.read(components=3, alignment=1) - array_rgb = np.frombuffer(data, dtype=np.uint8).reshape( + return np.frombuffer(data, dtype=np.uint8).reshape( camera.height, camera.width, 3 ) - - return array_rgb diff --git a/deodr/opengl/pyrender.py b/deodr/opengl/pyrender.py index 7da57f6..5445429 100644 --- a/deodr/opengl/pyrender.py +++ b/deodr/opengl/pyrender.py @@ -11,10 +11,7 @@ def arcsinc(x): - if abs(x) > 1e-6: - return np.arcsin(x) / x - else: - return 1 + return np.arcsin(x) / x if abs(x) > 1e-6 else 1 def min_rotation(vec1, vec2): @@ -32,12 +29,8 @@ def min_rotation(vec1, vec2): c = np.dot(a, b) s = np.linalg.norm(v) kmat = np.array([[0, -v[2], v[1]], [v[2], 0, -v[0]], [-v[1], v[0], 0]]) - if s > 1e-6: - d = (1 - c) / (s ** 2) - else: - d = 0.5 - rotation_matrix = np.eye(3) + kmat + kmat.dot(kmat) * d - return rotation_matrix + d = (1 - c) / (s ** 2) if s > 1e-6 else 0.5 + return np.eye(3) + kmat + kmat.dot(kmat) * d def deodr_directional_light_to_pyrender(deodr_directional_light): diff --git a/deodr/pytorch/triangulated_mesh_pytorch.py b/deodr/pytorch/triangulated_mesh_pytorch.py index 443a3e3..b53ba6f 100644 --- a/deodr/pytorch/triangulated_mesh_pytorch.py +++ b/deodr/pytorch/triangulated_mesh_pytorch.py @@ -37,14 +37,10 @@ def compute_face_normals(self, vertices): triangles = vertices[self.faces_torch, :] u = triangles[::, 1] - triangles[::, 0] v = triangles[::, 2] - triangles[::, 0] - if self.clockwise: - n = -torch.cross(u, v) - else: - n = torch.cross(u, v) + n = -torch.cross(u, v) if self.clockwise else torch.cross(u, v) l2 = (n ** 2).sum(dim=1) norm = l2.sqrt() - nn = n / norm[:, None] - return nn + return n / norm[:, None] def compute_vertex_normals(self, face_normals): n = self._vertices_faces_torch.mm(face_normals) @@ -86,7 +82,7 @@ def __init__( self.uv = uv self.texture = texture self.colors = colors - self.textured = not (self.texture is None) + self.textured = self.texture is not None def set_vertices_colors(self, colors): self.vertices_colors = colors diff --git a/deodr/tensorflow/triangulated_mesh_tensorflow.py b/deodr/tensorflow/triangulated_mesh_tensorflow.py index d4544de..c2ae95d 100644 --- a/deodr/tensorflow/triangulated_mesh_tensorflow.py +++ b/deodr/tensorflow/triangulated_mesh_tensorflow.py @@ -22,10 +22,7 @@ def compute_face_normals(self, vertices): tris = tf.gather(vertices, self.faces) u = tris[::, 1] - tris[::, 0] v = tris[::, 2] - tris[::, 0] - if self.clockwise: - n = -tf.linalg.cross(u, v) - else: - n = tf.linalg.cross(u, v) + n = -tf.linalg.cross(u, v) if self.clockwise else tf.linalg.cross(u, v) norm = tf.sqrt(tf.reduce_sum(n ** 2, axis=1)) return n / norm[:, None] @@ -68,7 +65,7 @@ def __init__( self.uv = uv self.texture = texture self.colors = colors - self.textured = not (self.texture is None) + self.textured = self.texture is not None def set_vertices_colors(self, colors): self.vertices_colors = colors diff --git a/deodr/tools.py b/deodr/tools.py index 625b3e4..9a480b0 100644 --- a/deodr/tools.py +++ b/deodr/tools.py @@ -7,13 +7,11 @@ def qrot(q, v): if q.ndim == 2: uv = np.cross(q[:, None, :3], v[None, :, :]) uuv = np.cross(q[:, None, :3], uv) - vr = v + 2 * (q[:, None, [3]] * uv + uuv) + return v + 2 * (q[:, None, [3]] * uv + uuv) else: uv = np.cross(q[:3], v) uuv = np.cross(q[:3], uv) - vr = v + 2 * (q[3] * uv + uuv) - - return vr + return v + 2 * (q[3] * uv + uuv) def qrot_backward(q, v, vr_b): @@ -31,8 +29,7 @@ def qrot_backward(q, v, vr_b): def normalize(x, axis=-1): n2 = np.sum(x ** 2, axis=axis) n = np.sqrt(n2) - xn = x / np.expand_dims(n, axis) - return xn + return x / np.expand_dims(n, axis) def normalize_backward(x, xn_b, axis=-1): @@ -40,8 +37,7 @@ def normalize_backward(x, xn_b, axis=-1): n = np.sqrt(n2) inv_n = 1 / n n_b = -np.sum(xn_b * x, axis=axis) * (inv_n ** 2) - x_b = (xn_b + x * np.expand_dims(n_b, axis)) * np.expand_dims(inv_n, axis) - return x_b + return (xn_b + x * np.expand_dims(n_b, axis)) * np.expand_dims(inv_n, axis) def cross_backward(u, v, c_b): diff --git a/deodr/triangulated_mesh.py b/deodr/triangulated_mesh.py index e6c9048..9544eb6 100644 --- a/deodr/triangulated_mesh.py +++ b/deodr/triangulated_mesh.py @@ -113,10 +113,7 @@ def compute_face_normals(self, vertices): triangles = vertices[self.faces, :] u = triangles[:, 1, :] - triangles[:, 0, :] v = triangles[:, 2, :] - triangles[:, 0, :] - if self.clockwise: - n = -np.cross(u, v) - else: - n = np.cross(u, v) + n = -np.cross(u, v) if self.clockwise else np.cross(u, v) normals = normalize(n, axis=1) self.store_backward["compute_face_normals"] = (u, v, n) return normals @@ -142,8 +139,7 @@ def compute_vertex_normals(self, face_normals): def compute_vertex_normals_backward(self, normals_b): n = self.store_backward["compute_vertex_normals"] n_b = normalize_backward(n, normals_b, axis=1) - face_normals_b = self._vertices_faces.T * n_b - return face_normals_b + return self._vertices_faces.T * n_b def edge_on_silhouette(self, vertices_2d): """Compute the a boolean for each of edges of each face that is true if @@ -152,10 +148,7 @@ def edge_on_silhouette(self, vertices_2d): triangles = vertices_2d[self.faces, :] u = triangles[:, 1, :] - triangles[:, 0, :] v = triangles[:, 2, :] - triangles[:, 0, :] - if self.clockwise: - face_visible = np.cross(u, v) > 0 - else: - face_visible = np.cross(u, v) < 0 + face_visible = np.cross(u, v) > 0 if self.clockwise else np.cross(u, v) < 0 edge_bool = (self.edges_faces_ones * face_visible) == 1 return edge_bool[self.faces_edges] @@ -188,10 +181,8 @@ def compute_adjacencies(self): self.faces, self.clockwise, nb_vertices=self.nb_vertices ) - if self.vertices is not None: - - if self.adjacencies.is_closed: - self.check_orientation() + if self.vertices is not None and self.adjacencies.is_closed: + self.check_orientation() def set_vertices(self, vertices): assert vertices.ndim == 2 @@ -283,13 +274,10 @@ def __init__( self.texture = texture self.vertices_colors = colors - self.textured = not (self.texture is None) + self.textured = self.texture is not None self.nb_colors = nb_colors if nb_colors is None: - if texture is None: - self.nb_colors = colors.shape[1] - else: - self.nb_colors = texture.shape[2] + self.nb_colors = colors.shape[1] if texture is None else texture.shape[2] def set_vertices_colors(self, colors): self.vertices_colors = colors @@ -430,10 +418,9 @@ def to_trimesh(self): material = trimesh.visual.material.SimpleMaterial(image=texture_pil) visual = trimesh.visual.texture.TextureVisuals(uv=uv, material=material) - trimesh_mesh = trimesh.Trimesh( + return trimesh.Trimesh( vertices=new_vertices, faces=new_faces, visual=visual ) - return trimesh_mesh @staticmethod def load(filename): diff --git a/setup.py b/setup.py index a149fe3..39b162f 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,6 @@ """Setup script for the DEODR project.""" + import os import re from setuptools import setup, find_packages @@ -27,8 +28,9 @@ with open(os.path.join(os.path.dirname(__file__), "deodr", "__init__.py")) as fp: for line in fp: - m = re.search(r'^\s*__version__\s*=\s*([\'"])([^\'"]+)\1\s*$', line) - if m: + if m := re.search( + r'^\s*__version__\s*=\s*([\'"])([^\'"]+)\1\s*$', line + ): version = m.group(2) break else: diff --git a/tests/test_pixel_center_coordinates.py b/tests/test_pixel_center_coordinates.py index cf82be2..6757f65 100644 --- a/tests/test_pixel_center_coordinates.py +++ b/tests/test_pixel_center_coordinates.py @@ -28,6 +28,9 @@ def test_upper_left_pixel_center_coordinates(): (0, height - 1), # lower left, (width - 1, height - 1), # lower right ] + eps = 0.001 + + clockwise = True for integer_pixel_centers in [False, True]: if integer_pixel_centers: point_coordinates = [ @@ -44,8 +47,6 @@ def test_upper_left_pixel_center_coordinates(): (width - 0.5, height - 0.5), # lower right ] - eps = 0.001 - depths = np.array([1, 1, 1]) shade = np.array([0, 0, 0]) shade = np.array([1, 1, 1]) @@ -55,7 +56,6 @@ def test_upper_left_pixel_center_coordinates(): shaded = np.array([0], dtype=np.bool) colors = np.array([[1], [1], [1]]) edgeflags = np.zeros((1, 3), dtype=np.bool) - clockwise = True faces = np.array([[0, 2, 1]], dtype=np.uint32) texture = np.ones((2, 2, 1)) diff --git a/tests/test_render_mesh.py b/tests/test_render_mesh.py index 424fda8..24cd5a7 100644 --- a/tests/test_render_mesh.py +++ b/tests/test_render_mesh.py @@ -14,7 +14,7 @@ def test_render_mesh_moderngl(): - if not os.name == "nt": # did not manage to install mesa on windows github action + if os.name != "nt": # did not manage to install mesa on windows github action example_moderngl(display=False)