From 5e75eb98c8def722017d8ec30adb59b3898dfbc0 Mon Sep 17 00:00:00 2001 From: Ilya Portnov Date: Sat, 19 Jun 2021 11:44:17 +0500 Subject: [PATCH] Precision fixes. --- utils/curve/nurbs_algorithms.py | 41 +++++++++------------------------ utils/geom.py | 2 +- utils/surface/nurbs.py | 4 +++- 3 files changed, 15 insertions(+), 32 deletions(-) diff --git a/utils/curve/nurbs_algorithms.py b/utils/curve/nurbs_algorithms.py index 2296b9ebb1..5290d01ba9 100644 --- a/utils/curve/nurbs_algorithms.py +++ b/utils/curve/nurbs_algorithms.py @@ -124,38 +124,19 @@ def concatenate_nurbs_curves(curves): def nurbs_curve_to_xoy(curve): cpts = curve.get_control_points() + approx = linear_approximation(cpts) plane = approx.most_similar_plane() - #print(f"N: {plane.normal}") - plane_matrix = plane.get_matrix() - #plane_center = np.array(plane_matrix.translation) - matrix = np.array(plane_matrix.inverted()) - center = approx.center - #new_cpts = (matrix @ cpts.T).T - new_cpts = np.array([matrix @ (cpt - center) for cpt in cpts]) - - first = new_cpts[0] - last = new_cpts[-1] - direction = last - first - x = np.array([1.0, 0.0, 0.0]) - y = np.array([0.0, 1.0, 0.0]) - first_x = np.dot(direction, x) - first_y = np.dot(direction, y) - #print(f"Dir: {direction}, x: {first_x}, y: {first_y}") - - if abs(first_x) > abs(first_y): - negate = first_x > 0 - else: - negate = first_y > 0 + normal = plane.normal - #for i, cpt in enumerate(new_cpts): - # print(f"C[{i}]: {cpt}") + xx = cpts[-1] - cpts[0] + xx /= np.linalg.norm(xx) - #print(f"F: {first}, L: {last}") - if negate: - new_cpts[:,1] = - new_cpts[:,1] - - return SvNurbsMaths.build_curve(curve.get_nurbs_implementation(), - curve.get_degree(), curve.get_knotvector(), - new_cpts, curve.get_weights()) + yy = np.cross(normal, xx) + matrix = np.stack((xx, yy, normal)).T + matrix = np.linalg.inv(matrix) + center = approx.center + new_cpts = np.array([matrix @ (cpt - center) for cpt in cpts]) + return curve.copy(control_points = new_cpts) + diff --git a/utils/geom.py b/utils/geom.py index 313b297d5b..a7b92b779a 100644 --- a/utils/geom.py +++ b/utils/geom.py @@ -825,7 +825,7 @@ def two_vectors(self, normalize=False): def get_matrix(self, invert_y=False): x = self.second_vector().normalized() z = self.normal.normalized() - y = z.cross(x) + y = z.cross(x).normalized() if invert_y: y = - y return Matrix([x, y, z]).transposed() diff --git a/utils/surface/nurbs.py b/utils/surface/nurbs.py index 6b14ee877b..85b1b2195e 100644 --- a/utils/surface/nurbs.py +++ b/utils/surface/nurbs.py @@ -1014,7 +1014,7 @@ def nurbs_birail(path1, path2, profiles, scales = scales.flatten() placed_profiles = [] - for pt1, profile, scale, matrix in zip(points1, profiles, scales, matrices): + for pt1, pt2, profile, scale, matrix in zip(points1, points2, profiles, scales, matrices): if auto_rotate: profile = nurbs_curve_to_xoy(profile) @@ -1034,6 +1034,7 @@ def nurbs_birail(path1, path2, profiles, (0, 0, 1) ]) + src_scale = scale scale /= pr_length if scale_uniform: scale_m = np.array([ @@ -1049,6 +1050,7 @@ def nurbs_birail(path1, path2, profiles, ]) cpts = [matrix @ scale_m @ rotation @ (pt - pr_start) + pt1 for pt in profile.get_control_points()] cpts = np.array(cpts) + profile = profile.copy(control_points = cpts) placed_profiles.append(profile)