Skip to content

Commit

Permalink
Precision fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
portnov committed Jun 19, 2021
1 parent 6c19240 commit 5e75eb9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 32 deletions.
41 changes: 11 additions & 30 deletions utils/curve/nurbs_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

2 changes: 1 addition & 1 deletion utils/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion utils/surface/nurbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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([
Expand All @@ -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)

Expand Down

0 comments on commit 5e75eb9

Please sign in to comment.