To create a G2 continuous curve between the end of arc a1, through a defined point X, to the start of arc a2, we'll need to ensure continuity in position (G0), tangent (G1), and curvature (G2). Here's a Python solution using numpy and scipy:
python
import numpy as np
from scipy.optimize import minimize
import matplotlib.pyplot as plt
def create_g2_continuous_curve(a1_end, a1_end_tangent, a1_end_curvature,
X_point,
a2_start, a2_start_tangent, a2_start_curvature,
num_points=100):
"""
Create a G2 continuous curve between two arcs.
Parameters:
- a1_end: End point of first arc (numpy array [x, y])
- a1_end_tangent: Tangent vector at end of first arc
- a1_end_curvature: Curvature at end of first arc
- X_point: The point the curve must pass through
- a2_start: Start point of second arc
- a2_start_tangent: Tangent vector at start of second arc
- a2_start_curvature: Curvature at start of second arc
- num_points: Number of points to generate for the curve
Returns:
- curve_points: Array of points along the G2 continuous curve
"""
# We'll use a cubic spline that satisfies all continuity conditions
# The curve will consist of two segments: a1_end to X, and X to a2_start
def cubic_bezier(p0, p1, p2, p3, t):
"""Evaluate a cubic Bezier curve at parameter t"""
return (1-t)**3 * p0 + 3*(1-t)**2*t * p1 + 3*(1-t)*t**2 * p2 + t**3 * p3
def bezier_derivative(p0, p1, p2, p3, t):
"""First derivative of cubic Bezier"""
return 3*(1-t)**2*(p1-p0) + 6*(1-t)*t*(p2-p1) + 3*t**2*(p3-p2)
def bezier_second_derivative(p0, p1, p2, p3, t):
"""Second derivative of cubic Bezier"""
return 6*(1-t)*(p2-2*p1+p0) + 6*t*(p3-2*p2+p1)
def curvature(p0, p1, p2, p3, t):
"""Calculate curvature of Bezier curve at t"""
d1 = bezier_derivative(p0, p1, p2, p3, t)
d2 = bezier_second_derivative(p0, p1, p2, p3, t)
cross = d1[0]*d2[1] - d1[1]*d2[0]
return cross / (np.linalg.norm(d1)**3)
# Objective function to minimize - ensures G2 continuity at both ends
def objective(params):
# Unpack control points
p1_1 = params[0:2] # First segment control point 1
p2_1 = params[2:4] # First segment control point 2
p1_2 = params[4:6] # Second segment control point 1
p2_2 = params[6:8] # Second segment control point 2
# First segment (a1_end to X)
# G1 continuity at a1_end
tangent_error1 = np.linalg.norm(bezier_derivative(a1_end, p1_1, p2_1, X_point, 0) - a1_end_tangent)
# G2 continuity at a1_end (curvature match)
curv_error1 = abs(curvature(a1_end, p1_1, p2_1, X_point, 0) - a1_end_curvature)
# Second segment (X to a2_start)
# G1 continuity at a2_start
tangent_error2 = np.linalg.norm(bezier_derivative(X_point, p1_2, p2_2, a2_start, 1) - a2_start_tangent)
# G2 continuity at a2_start (curvature match)
curv_error2 = abs(curvature(X_point, p1_2, p2_2, a2_start, 1) - a2_start_curvature)
# Also ensure C1 continuity at X (where the two segments meet)
tangent_X1 = bezier_derivative(a1_end, p1_1, p2_1, X_point, 1)
tangent_X2 = bezier_derivative(X_point, p1_2, p2_2, a2_start, 0)
tangent_X_error = np.linalg.norm(tangent_X1 - tangent_X2)
# Combine all errors with weights
return (tangent_error1 + 10*curv_error1 +
tangent_error2 + 10*curv_error2 +
5*tangent_X_error)
# Initial guess for control points (straight line segments)
initial_params = np.concatenate([
a1_end + 0.3 * (X_point - a1_end), # p1_1
X_point - 0.3 * (X_point - a1_end), # p2_1
X_point + 0.3 * (a2_start - X_point), # p1_2
a2_start - 0.3 * (a2_start - X_point) # p2_2
])
# Optimize control points to satisfy continuity conditions
result = minimize(objective, initial_params, method='L-BFGS-B')
params = result.x
# Extract optimized control points
p1_1 = params[0:2]
p2_1 = params[2:4]
p1_2 = params[4:6]
p2_2 = params[6:8]
# Generate points along the curve
t_values = np.linspace(0, 1, num_points//2)
curve_part1 = np.array([cubic_bezier(a1_end, p1_1, p2_1, X_point, t) for t in t_values])
curve_part2 = np.array([cubic_bezier(X_point, p1_2, p2_2, a2_start, t) for t in t_values])
# Combine both segments
curve_points = np.vstack([curve_part1, curve_part2])
return curve_points
# Example usage:
if __name__ == "__main__":
# Define example arcs and points
a1_end = np.array([1.0, 1.0])
a1_end_tangent = np.array([0.0, 1.0]) # Tangent vector (normalized)
a1_end_curvature = 0.5 # Curvature value
X_point = np.array([2.0, 2.0]) # Point the curve must pass through
a2_start = np.array([3.0, 1.0])
a2_start_tangent = np.array([1.0, 0.0]) # Tangent vector (normalized)
a2_start_curvature = -0.5 # Curvature value
# Generate the G2 continuous curve
curve = create_g2_continuous_curve(a1_end, a1_end_tangent, a1_end_curvature,
X_point,
a2_start, a2_start_tangent, a2_start_curvature)
# Plot the result
plt.figure(figsize=(8, 6))
plt.plot(curve[:, 0], curve[:, 1], 'b-', label='G2 Continuous Curve')
plt.plot([a1_end[0], X_point[0], a2_start[0]],
[a1_end[1], X_point[1], a2_start[1]], 'ro', label='Key Points')
# Plot tangent vectors
plt.quiver(a1_end[0], a1_end[1], a1_end_tangent[0], a1_end_tangent[1],
scale=5, color='g', label='Tangent at a1 end')
plt.quiver(a2_start[0], a2_start[1], a2_start_tangent[0], a2_start_tangent[1],
scale=5, color='g', label='Tangent at a2 start')
plt.axis('equal')
plt.grid(True)
plt.legend()
plt.title('G2 Continuous Curve Between Two Arcs')
plt.show()
How This Works:
Bezier Curves: We use cubic Bezier curves for each segment (a1_end to X