G2 continuity means that the curve connecting the two arcs should have:
G0: positional continuity (i.e., the curve passes through the endpoints of the arcs and point X)
G1: tangential continuity (i.e., the curve's tangent direction matches the arcs' tangents at the connection points)
G2: curvature continuity (i.e., the curve's curvature matches the arcs' curvatures at the connection points)
We'll use a Cubic Bézier curve to achieve G2 continuity. Why? Because:
A Cubic Bézier has 4 control points, which allows us to satisfy the 3 conditions (G0, G1, G2) at both ends.
It's a popular, well-behaved curve type, easy to compute and evaluate.
Here's the plan:
Extract data from your 2D arcs (a1 and a2):
Endpoints: a1_end and a2_start
Tangents (1st derivatives) at these points: a1_end_tangent and a2_start_tangent
Curvatures (2nd derivatives) at these points: a1_end_curvature and a2_start_curvature
Compute intermediate control points for the Cubic Bézier:
Use the given point X as the second control point (G0 condition)
Calculate the first and third control points to satisfy G1 and G2 conditions
Construct the Cubic Bézier curve using the 4 control points
Evaluate the curve using NumPy
Let's dive into Python code! I'll use NumPy for vectorized computations.
python
import numpy as np
import matplotlib.pyplot as plt
def compute_cubic_bezier_control_points(a1_end, a1_end_tangent, a1_end_curvature,
X,
a2_start, a2_start_tangent, a2_start_curvature):
"""
Compute control points for a Cubic Bézier curve connecting two arcs with G2 continuity.
:param a1_end: End point of arc 1 (2D vector)
:param a1_end_tangent: Tangent direction at a1_end (2D unit vector)
:param a1_end_curvature: Curvature at a1_end (scalar)
:param X: Defined point the curve must pass through (2D vector)
:param a2_start: Start point of arc 2 (2D vector)
:param a2_start_tangent: Tangent direction at a2_start (2D unit vector)
:param a2_start_curvature: Curvature at a2_start (scalar)
:return: 4 control points of the Cubic Bézier curve (2D vectors)
"""
# Step 1: Compute distances between points to estimate suitable magnitudes for tangents
dist_a1_to_X = np.linalg.norm(X - a1_end)
dist_X_to_a2 = np.linalg.norm(a2_start - X)
# Step 2: Calculate the first control point (P1) using G1 and G2 conditions at a1_end
k1 = 1 / (3 * a1_end_curvature) # "speed" parameter, linked to curvature
P1 = a1_end + k1 * a1_end_tangent
# Step 3: Calculate the third control point (P2) using G1 and G2 conditions at a2_start
k2 = 1 / (3 * a2_start_curvature) # "speed" parameter, linked to curvature
P2 = a2_start - k2 * a2_start_tangent
# Step 4: Set the second control point (P1) to the given point X (G0 condition)
P0 = a1_end # First control point is the end of arc 1
P3 = a2_start # Last control point is the start of arc 2
return np.array([P0, P1, P2, P3])
def evaluate_cubic_bezier(control_points, num_samples=50):
"""
Evaluate a Cubic Bézier curve at uniformly spaced intervals.
:param control_points: 4 control points (2D vectors)
:param num_samples: Number of curve points to generate
:return: Curve points (2D vectors) as a (num_samples, 2) array
"""
t = np.linspace(0, 1, num_samples)
t2 = t ** 2
t3 = t ** 3
mt = 1 - t
mt2 = mt ** 2
mt3 = mt ** 3
curve_points = (mt3[:, None] * control_points[0] +
3 * mt2[:, None] * t[:, None] * control_points[1] +
3 * mt[:, None] * t2[:, None] * control_points[2] +
t3[:, None] * control_points[3])
return curve_points
# Example usage
if __name__ == "__main__":
# Define your arcs' endpoints, tangents, and curvatures (example values)
a1_end = np.array([0.0, 0.0])
a1_end_tangent = np.array([1.0, 0.5]) / np.linalg.norm([1.0, 0.5]) # Unit vector
a1_end_curvature = 0.5
X = np.array([3.0, 2.0]) # The point your curve must pass through
a2_start = np.array([6.0, 0.0])
a2_start_tangent = np.array([-1.0, 0.8]) / np.linalg.norm([-1.0, 0.8]) # Unit vector
a2_start_curvature = 0.3
# Compute control points for the Cubic Bézier curve
control_points = compute_cubic_bezier_control_points(
a1_end, a1_end_tangent, a1_end_curvature,
X,
a2_start, a2_start_tangent, a2_start_curvature
)
# Evaluate the Cubic Bézier curve
curve_points = evaluate_cubic_bezier(control_points)
# Plot everything
plt.figure(figsize=(8, 6))
plt.plot(curve_points[:, 0], curve_points[:, 1], label='G2 Continuous Curve', color='blue')
# Arc 1 endpoint and tangent
plt.plot(a1_end[0], a1_end[1], 'go', label='Arc 1 End')
plt.arrow(a1_end[0], a1_end[1], a1_end_tangent[0], a1_end_tangent[1], head_width=0.1, color='green')
# Point X
plt.plot(X[0], X[1], 'ro', label='Point X')
# Arc 2 start point and tangent
plt.plot(a2_start[0], a2_start[1], 'bo', label='Arc 2 Start')
plt.arrow(a2_start[0], a2_start[1], a2_start_tangent[0], a2_start_tangent[1], head_width=0.1, color='blue')
plt.legend()
plt.axis('equal') # To ensure aspect ratio is equal
plt.show()
Explanation:
The compute_cubic_bezier_control_points function calculates the Cubic Bézier control points. It uses the curvatures to estimate suitable "speeds" (k1, k2) for the tangents, ensuring G2 continuity.
The evaluate_cubic_bezier function generates num_samples points along the Cubic Bézier curve using the Bernstein basis polynomials.
In the example usage, we define two arcs' endpoints, tangents, and curvatures. We also specify point X.
We compute the control points, evaluate the curve, and plot the result.
This code should provide a smooth, G2 continuous curve connecting your two arcs through point X.
Feel free to adjust the example values to match your actual arc data!
Hope this helps! Let me know if you have questions or need further clarification.