Skip to content

Commit

Permalink
Some tuning.
Browse files Browse the repository at this point in the history
  • Loading branch information
portnov committed Dec 10, 2022
1 parent 0d95c23 commit 9ce6f0a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
27 changes: 24 additions & 3 deletions tests/bezier_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,32 @@ def test_blend_second(self):
a0r = curve.second_derivative(0)
a1r = curve.second_derivative(1)

self.assert_numpy_arrays_equal(v0r, v0)
self.assert_numpy_arrays_equal(v1r, v1)
self.assert_numpy_arrays_equal(a0r, a0)
self.assert_numpy_arrays_equal(v0r, v0, precision=6)
self.assert_numpy_arrays_equal(v1r, v1, precision=6)
self.assert_numpy_arrays_equal(a0r, a0, precision=6)
self.assert_numpy_arrays_equal(a1r, a1, precision=8)

def test_blend_second_2(self):
p0 = np.array([-0.12, -1.75, 0])
v0 = np.array([0.69, -0.05, 0])
a0 = np.array([-2.75, 42.64, 0])
p1 = np.array([0.57, -1.5, 0])
v1 = np.array([-0.31, 0.07, 0])
a1 = np.array([0.15, 1.05, 0])

curve = SvBezierCurve.blend_second_derivatives(p0, v0, a0, p1, v1, a1)

v0r = curve.tangent(0)
v1r = curve.tangent(1)

a0r = curve.second_derivative(0)
a1r = curve.second_derivative(1)

self.assert_numpy_arrays_equal(v0r, v0, precision=6)
self.assert_numpy_arrays_equal(v1r, v1, precision=6)
self.assert_numpy_arrays_equal(a0r, a0, precision=6)
self.assert_numpy_arrays_equal(a1r, a1, precision=6)

def test_blend_third(self):
p0 = np.array([0, 0, 0])
p1 = np.array([3, 0, 0])
Expand Down
39 changes: 23 additions & 16 deletions utils/curve/fillet.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
SMOOTH_NORMAL = '2'
SMOOTH_CURVATURE = '3'

def calc_single_fillet(smooth, curve1, curve2, bulge_factor = 0.5, biarc_parameter = 1.0, planar_tolerance = 1e-6):
def calc_single_fillet(smooth, curve1, curve2, t_span, bulge_factor = 0.5, biarc_parameter = 1.0, planar_tolerance = 1e-6):
#t_span = 1.0
u1_max = curve1.get_u_bounds()[1]
u2_min = curve2.get_u_bounds()[0]
curve1_end = curve1.evaluate(u1_max)
curve2_begin = curve2.evaluate(u2_min)
tangent1_end = curve1.get_end_tangent()
tangent2_begin = curve2.get_start_tangent()
tangent1_end = t_span * curve1.get_end_tangent()
tangent2_begin = t_span * curve2.get_start_tangent()

if smooth == SMOOTH_POSITION:
return SvLine.from_two_points(curve1_end, curve2_begin)
Expand All @@ -57,16 +58,19 @@ def calc_single_fillet(smooth, curve1, curve2, bulge_factor = 0.5, biarc_paramet
biarc_parameter,
planar_tolerance = planar_tolerance)
elif smooth == SMOOTH_NORMAL:
second_1_end = curve1.second_derivative(u1_max)
second_2_begin = curve2.second_derivative(u2_min)
second_1_end = t_span**2 * curve1.second_derivative(u1_max)
second_2_begin = t_span**2 * curve2.second_derivative(u2_min)
#print(f"T: {t_span**2}")
#print(f"E: {curve1_end}, {tangent1_end}, {second_1_end}")
#print(f"B: {curve2_begin}, {tangent2_begin}, {second_2_begin}")
return SvBezierCurve.blend_second_derivatives(
curve1_end, tangent1_end, second_1_end,
curve2_begin, tangent2_begin, second_2_begin)
elif smooth == SMOOTH_CURVATURE:
second_1_end = curve1.second_derivative(u1_max)
second_2_begin = curve2.second_derivative(u2_min)
third_1_end = curve1.third_derivative_array(np.array([u1_max]))[0]
third_2_begin = curve2.third_derivative_array(np.array([u2_min]))[0]
second_1_end = t_span**2 * curve1.second_derivative(u1_max)
second_2_begin = t_span**2 * curve2.second_derivative(u2_min)
third_1_end = t_span**3 * curve1.third_derivative_array(np.array([u1_max]))[0]
third_2_begin = t_span**3 * curve2.third_derivative_array(np.array([u2_min]))[0]

return SvBezierCurve.blend_third_derivatives(
curve1_end, tangent1_end, second_1_end, third_1_end,
Expand All @@ -78,17 +82,17 @@ def cut_ends(curve, cut_offset, cut_start=True, cut_end=True):
u_min, u_max = curve.get_u_bounds()
p1, p2 = curve.get_end_points()
l = np.linalg.norm(p1 - p2)
dt = u_max - u_min
dt = cut_offset * (u_max - u_min)
if cut_start:
u1 = u_min + cut_offset*dt
u1 = u_min + dt
else:
u1 = u_min
if cut_end:
u2 = u_max - cut_offset*dt
u2 = u_max - dt
else:
u2 = u_max
#print(f"cut: {u_min} - {u_max} * cut_offset => {u1} - {u2}")
return curve.cut_segment(u1, u2)
return dt, curve.cut_segment(u1, u2)

def limit_filet_radiuses(vertices, radiuses, cyclic=False):
factor = 0.999
Expand Down Expand Up @@ -145,11 +149,14 @@ def fillet_nurbs_curve(curve, smooth, cut_offset,
cyclic = curve.is_closed()
segments = curve.split_at_fracture_points(tangent_tolerance = tangent_tolerance)
n = len(segments)
segments = [cut_ends(s, cut_offset, cut_start = (i > 0 or cyclic), cut_end = (i < n-1 or cyclic)) for i, s in enumerate(segments)]
fillets = [calc_single_fillet(smooth, s1, s2, bulge_factor, biarc_parameter, planar_tolerance) for s1, s2 in zip(segments, segments[1:])]
cuts = [cut_ends(s, cut_offset, cut_start = (i > 0 or cyclic), cut_end = (i < n-1 or cyclic)) for i, s in enumerate(segments)]
fillets = [calc_single_fillet(smooth, s1, s2, dt1+dt2, bulge_factor, biarc_parameter, planar_tolerance) for (dt1,s1), (dt2,s2) in zip(cuts, cuts[1:])]
if cyclic:
fillet = calc_single_fillet(smooth, segments[-1], segments[0], bulge_factor, biarc_parameter, planar_tolerance)
dt1, s1 = cuts[-1]
dt2, s2 = cuts[0]
fillet = calc_single_fillet(smooth, s1, s2, dt1+dt2, bulge_factor, biarc_parameter, planar_tolerance)
fillets.append(fillet)
segments = [cut[1] for cut in cuts]
new_segments = [[segment, fillet] for segment, fillet in zip(segments, fillets)]
if not cyclic:
new_segments.append([segments[-1]])
Expand Down

0 comments on commit 9ce6f0a

Please sign in to comment.