To enhance the smoothness of your Bezier curve beyond G¹ continuity, you can aim for G² continuity. Achieving higher smoothness involves ensuring that not only the tangents but also the curvature (i.e., the rate of change of the tangents) are continuous across curve segments. Additionally, refining the way you compute the Tangent, Normal, and Binormal (TNB) frames can significantly improve the visual smoothness of the curve.
Here's how you can achieve this:
-
Ensure G² Continuity Across Segments:
For G² continuity (geometric continuity of second order), the second derivatives of the curve segments must also be continuous at the joints. This requires that the control points of consecutive Bezier segments are arranged such that the curvature is preserved across segments. Specifically, you need to adjust the control points so that the derivatives up to the second order match at the joining points.
-
Implement Rotation Minimizing Frames (RMF):
Instead of using the previous binormal to compute the normal vector, Rotation Minimizing Frames minimize the rotation of the frame along the curve, leading to smoother transitions of the frames. This avoids sudden twists and ensures that the Normal and Binormal vectors change smoothly.
Below is the revised evalBezier function incorporating these improvements:
#include <vector>
#include <iostream>
#include <Eigen/Dense>
#include <cmath>
// Assuming necessary using directives and Vector3f, CurvePoint, CurvePoints are defined appropriately
struct Vector3f {
float x, y, z;
Vector3f() : x(0), y(0), z(0) {}
Vector3f(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
Vector3f operator-(const Vector3f& other) const {
return Vector3f(x - other.x, y - other.y, z - other.z);
}
Vector3f operator+(const Vector3f& other) const {
return Vector3f(x + other.x, y + other.y, z + other.z);
}
Vector3f operator*(float s) const {
return Vector3f(x * s, y * s, z * s);
}
Vector3f cross(const Vector3f& other) const {
return Vector3f(
y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x
);
}
float dot(const Vector3f& other) const {
return x * other.x + y * other.y + z * other.z;
}
float norm() const {
return std::sqrt(x*x + y*y + z*z);
}
void normalize() {
float n = norm();
if (n > 1e-6) { // Avoid division by zero
x /= n;
y /= n;
z /= n;
}
}
// For Eigen interoperability
Eigen::Vector3f toEigen() const {
return Eigen::Vector3f(x, y, z);
}
// For printing
friend std::ostream& operator<<(std::ostream& os, const Vector3f& v) {
os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
return os;
}
};
struct CurvePoint {
Vector3f V; // Position
Vector3f T; // Tangent
Vector3f N; // Normal
Vector3f B; // Binormal
};
using CurvePoints = std::vector<CurvePoint>;
struct Curve {
// Placeholder for other members if necessary
CurvePoints points;
CurvePoints evalBezier(const std::vector< Vector3f >& P, unsigned steps);
};
CurvePoints Curve::evalBezier(const std::vector< Vector3f >& P, unsigned steps){
// Check
if( P.size() < 4 || P.size() % 3 != 1 )
{
std::cerr << "evalBezier must be called with 3n+1 control points." << std::endl;
exit( 0 );
}
std::cerr << "\t>>> evalBezier has been called with the following input:" << std::endl;
std::cerr << "\t>>> Control points (type vector< Vector3f >): "<< std::endl;
for( unsigned i = 0; i < P.size(); ++i )
{
std::cerr << "\t>>> " << P[i] << std::endl;
}
std::cerr << "\t>>> Steps (type steps): " << steps << std::endl;
CurvePoints res;
Matrix4f M_B (
1, -3, 3, -1,
0, 3, -6, 3,
0, 0, 3, -3,
0, 0, 0, 1
);
// Initialize the frame
bool firstSegment = true;
Vector3f prevT, prevN, prevB;
for (unsigned i = 0; i < P.size() - 3; i += 3) {
Matrix4f G(
P[i].x, P[i + 1].x, P[i + 2].x, P[i + 3].x,
P[i].y, P[i + 1].y, P[i + 2].y, P[i + 3].y,
P[i].z, P[i + 1].z, P[i + 2].z, P[i + 3].z,
1 , 1 , 1 , 1
);
for (unsigned j = 0; j <= steps; j++) {
// Avoid duplicating endpoints except for the first and last segments
if ((j == 0 || j == steps) && (i != 0) && (i + 3 != P.size())) continue;
float t = float(j)/ steps; // t in [0, 1]
Vector4f M(1, t, t*t, t*t*t); // Monomial basis
// Position
Vector3f V = (G * M_B * M).head<3>();
// Tangent
Vector4f dM(0, 1, 2*t, 3*t*t);
Vector3f T = (G * M_B * dM).head<3>();
T.normalize();
// Initialize or update the frame
Vector3f N, B;
if (firstSegment && j == 0) {
// Define an initial normal vector (e.g., global Y-axis) that's not parallel to T
Vector3f up(0, 1, 0);
if (std::abs(T.dot(up)) > 0.99f) { // If T is almost parallel to up
up = Vector3f(1, 0, 0);
}
N = up.cross(T);
N.normalize();
B = T.cross(N);
B.normalize();
prevT = T;
prevN = N;
prevB = B;
firstSegment = false;
} else {
// Rotation Minimizing Frames (RMF)
// Compute the rotation between prevT and T
float cosTheta = prevT.dot(T);
if (cosTheta < -1.0f) cosTheta = -1.0f;
if (cosTheta > 1.0f) cosTheta = 1.0f;
float theta = std::acos(cosTheta);
Vector3f axis;
if (theta > 1e-6) { // If not parallel
axis = prevT.cross(T);
axis.normalize();
} else {
axis = Vector3f(0, 0, 1); // Arbitrary
}
// Rotate the previous normal and binormal around the rotation axis
Eigen::AngleAxisf rotation(theta, axis.toEigen());
Eigen::Vector3f en = prevN.toEigen();
Eigen::Vector3f eb = prevB.toEigen();
Eigen::Vector3f newN = rotation * en;
Eigen::Vector3f newB = rotation * eb;
N = Vector3f(newN.x(), newN.y(), newN.z());
B = Vector3f(newB.x(), newB.y(), newB.z());
// Update previous frame
prevT = T;
prevN = N;
prevB = B;
}
res.push_back(CurvePoint{V, T, N, B});
}
}
return res;
}
Key Improvements Explained:
-
G² Continuity:
-
Control Points Arrangement: Ensure that the control points for consecutive Bezier segments are arranged to maintain continuous first and second derivatives. This typically involves setting the incoming and outgoing control points at the joints to be colinear and appropriately spaced.
-
Example Adjustment: If you have two consecutive segments defined by control points ( P_0, P_1, P_2, P_3 ) and ( P_3, P_4, P_5, P_6 ), ensure that ( P_2, P_3, P_4 ) are colinear and ( |P_2 - P_3| = |P_4 - P_3| ). This maintains ( C^1 ) continuity. For ( G^2 ) continuity, additional constraints on the second derivatives are required.
-
Rotation Minimizing Frames (RMF):
-
Initial Frame: Start with an initial Normal vector, typically using a global up vector (e.g., Y-axis). If the tangent is parallel to the up vector, switch to another axis to avoid degeneracy.
-
Frame Rotation: As you iterate through each step, rotate the previous Normal and Binormal vectors to align with the new Tangent vector using the Angle-Axis Rotation method. This minimizes the rotation of the frame, preventing sudden twists and ensuring smooth transitions.
-
Eigen Integration: The use of the Eigen library's AngleAxisf facilitates efficient and accurate rotation computations.
-
Frame Initialization and Updating:
-
First Segment Initialization: Define an initial frame based on a chosen up vector that's not parallel to the initial tangent.
-
Subsequent Frames: For each new tangent, compute the rotation required to align the previous tangent to the current one and apply this rotation to the previous Normal and Binormal vectors to obtain the new frame.
Additional Recommendations:
-
Control Point Adjustment: Ensure that when designing your control points, especially at the joints between segments, they satisfy the conditions required for ( G^2 ) continuity.
-
Numerical Stability: Handle cases where tangents might become degenerate or nearly parallel to avoid numerical issues in frame calculations.
-
Testing and Visualization: After implementing these changes, visualize the curve and its frames to ensure smoothness and correctness. Tools like OpenGL or visualization libraries can be invaluable for this purpose.
By implementing these enhancements, your Bezier curves will exhibit smoother transitions with higher-order continuity, resulting in more visually appealing and mathematically robust curves.