- Main Page
- Modules
- Classes
- Files
- Related Pages

- File List
- File Members

# src/vectorMath/bezier.h

Go to the documentation of this file.

    00001 #ifndef BEZIER_H_
    00002 #define BEZIER_H_
    00003
    00004 #include "../typedefs.h"
    00005
    00006 //defines granularity of the bezier line - number of points per meter of length
    00007 //of each sides of the triangle (start point, peak, end point)
    00008 #define BEZIER_PTS_PER_METER 1.25
    00009
    00011 Vector3r pointOnCubicBezier(const Vector3r &startPoint, const Vector3r &startCtrlPoint, const Vector3r &endPoint, const Vector3r &endCtrlPoint, float t)
    00012 {
    00013     float ax, bx, cx;
    00014     float ay, by, cy;
    00015     float tSquared, tCubed;
    00016     Vector3r result;
    00017
    00018     /* calculate the polynomial coefficients */
    00019     cx = 3.0 * (startCtrlPoint.x - startPoint.x);
    00020     bx = 3.0 * (endCtrlPoint.x - startCtrlPoint.x) - cx;
    00021     ax = endPoint.x - startPoint.x - cx - bx;
    00022     cy = 3.0 * (startCtrlPoint.y - startPoint.y);
    00023     by = 3.0 * (endCtrlPoint.y - startCtrlPoint.y) - cy;
    00024     ay = endPoint.y - startPoint.y - cy - by;
    00025
    00026     /* calculate the curve point at parameter value t */
    00027     tSquared = t * t;
    00028     tCubed = tSquared * t;
    00029     result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + startPoint.x;
    00030     result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + startPoint.y;
    00031     result.z = 0;
    00032     return result;
    00033 }
    00034
    00043 void computeBezier(Polygon &points, const Vector3r &startPoint, const Vector3r &startCtrlPoint, const Vector3r &endPoint, const Vector3r &endCtrlPoint, dReal ratio = 1)
    00044 {
    00045     float dt;
    00046     int i;
    00047
    00048     Vector3r startPtGuid = startCtrlPoint - startPoint;
    00049     Vector3r endPtGuid = endPoint - endCtrlPoint;
    00050     dReal dev = acos(startPtGuid.dotProduct(endPtGuid)/(startPtGuid.length()*endPtGuid.length()));
    00051
    00052     //adjust the length of control point vectors based on desired ratio
    00053     if (ratio < 0) ratio = 0;
    00054     if (ratio > 1) ratio = 1;
    00055     Vector3r tmp1 = startPoint + (startPtGuid * ratio);
    00056     Vector3r tmp2 = endPoint + (endPtGuid * -ratio);
    00057
    00058     //estimate number of points
    00059     dReal length = startPtGuid.length() + endPtGuid.length();
    00060 //  int numberOfPoints = (int)ceil((length * BEZIER_PTS_PER_METER) * dev / M_PI);
    00061     int numberOfPoints = (int)ceil((length * BEZIER_PTS_PER_METER));
    00062     //if two or less points should be computed, put in at least the start and end point and skip the whole computation
    00063     if (numberOfPoints < 2) {
    00064         points.push_back(startPoint);
    00065         points.push_back(endPoint);
    00066         return;
    00067     }
    00068
    00069 //  _DEBUG("numberOfPoints=" << numberOfPoints);
    00070     dt = 1.0 / (numberOfPoints - 1);
    00071     for (i = 0; i < numberOfPoints; i++)
    00072         points.push_back(pointOnCubicBezier(startPoint, tmp1, endPoint, tmp2, i * dt));
    00073 }
    00074
    00075 #endif /*BEZIER_H_*/