Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <cv.h>
00043 #include "ccd/bspline.h"
00044 double BSpline::basic(int i,
00045 int k,
00046 double t)
00047 {
00048 double value;
00049 if (k == 1)
00050 ((t >= knots[i]) && (t<knots[i+1]))? value = 1 : value = 0;
00051 else{
00052 if((knots[i+k-1] == knots[i]) && (knots[i+k] == knots[i+1])) value = 0;
00053 else if(knots[i+k-1] == knots[i]) value = (knots[i+k] -t )/(knots[i+k] -knots[i+1])*basic(i+1,k -1 , t);
00054 else if(knots[i+k] == knots[i+1]) value = (t - knots[i])/(knots[i+k-1] - knots[i]) * basic(i,k-1, t);
00055 else value = (t - knots[i])/(knots[i+k-1] - knots[i]) * basic(i,k-1, t) + (knots[i+k] -t )/(knots[i+k] -knots[i+1])*basic(i+1,k -1 , t);
00056 }
00057 return value;
00058 }
00059
00060 double BSpline::basic(int i,
00061 int degree,
00062 double t,
00063 double *bp)
00064 {
00065 double temp = 0;
00066 double t_floor = t-floor(t);
00067
00068 if(degree == 3)
00069 {
00070 if(t -i >= 2 && t- i <= 3)
00071 {
00072 temp = 0.5*(1- t_floor)*(1-t_floor);
00073 *bp = t_floor - 1;
00074 }
00075 else if (t - i >= 1 && t-i< 2)
00076 {
00077 temp = -t_floor*t_floor + t_floor + 0.5;
00078 *bp = 1 - 2*t_floor;
00079 }
00080 else if (t-i >= 0 && t-i < 1)
00081 {
00082 temp = 0.5*t_floor*t_floor;
00083 *bp = t_floor;
00084 }
00085 else
00086 {
00087 temp = 0;
00088 *bp = 0;
00089 }
00090 }
00091 else if(degree == 4)
00092 {
00093
00094 if((t -i >= 3 && t- i <= 4))
00095 {
00096 temp = (-t_floor*t_floor*t_floor+3*t_floor*t_floor - 3*t_floor + 1)/6;
00097 *bp = -0.5*t_floor*t_floor + t_floor - 0.5;
00098 }
00099 else if (t - i >= 2 && t-i< 3)
00100 {
00101 temp = (3*t_floor*t_floor*t_floor - 6*t_floor*t_floor + 4)/6;
00102 *bp = 1.5*t_floor*t_floor - 2*t_floor;
00103 }
00104 else if (t-i >= 1 && t-i < 2)
00105 {
00106 temp = (-3*t_floor*t_floor*t_floor + 3*t_floor*t_floor + 3*t_floor +1)/6;
00107 *bp = -1.5*t_floor*t_floor + t_floor + 0.5;
00108 }
00109 else if(t-i >= 0 && t-i < 1)
00110 {
00111 temp = t_floor*t_floor*t_floor/6;
00112 *bp = 0.5*t_floor*t_floor;
00113 }
00114 else
00115 {
00116 temp = 0;
00117 *bp = 0;
00118 }
00119 }
00120 return temp;
00121 }
00122
00123
00124 void BSpline::computeKnots()
00125 {
00126
00127 for (size_t j = 0; j < knots.size() ; ++j){
00128 knots[j] = j;
00129
00130
00131
00132
00133
00134
00135
00136 }
00137 }
00138
00139 void BSpline::computePoint(
00140 std::vector<cv::Point3d> control,
00141 cv::Point3d *output,
00142 cv::Point3d *slope,
00143 double *mat_ptr,
00144 double t,
00145 int degree)
00146 {
00147 double b = 0, bp = 0;
00148
00149 output->x=0;
00150 output->y=0;
00151 output->z=0;
00152 slope->x = 0;
00153 slope->y = 0;
00154 slope->z = 0;
00155 for (size_t i = 0; i < control.size(); i++)
00156 {
00157
00158 b = basic(i, degree, t, &bp);
00159 mat_ptr[i] = b;
00160 output->x += control[i].x * b;
00161 output->y += control[i].y * b;
00162 output->z += control[i].z * b;
00163 slope->x += (control[i]).x * bp;
00164 slope->y += (control[i]).y * bp;
00165 slope->z += (control[i]).z * bp;
00166 }
00167 }
00168 BSpline::BSpline():curve_(NULL), tangent_(NULL){}
00169
00170 BSpline::BSpline(int n,
00171 int resolution,
00172 std::vector<cv::Point3d> control)
00173 :basic_mat_(cv::Mat(resolution, control.size(), CV_64FC1)),
00174 knots(std::vector<int>(control.size()+n, 0)),
00175 curve_((n>0 && resolution > 0)? new cv::Point3d[resolution]:NULL),
00176 tangent_((n>0 && resolution > 0)? new cv::Point3d[resolution]:NULL)
00177 {
00178 double increment, interval;
00179 cv::Point3d tmp_point, tmp_tangent;
00180 int m = control.size() - 1;
00181 computeKnots();
00182 increment = (double) (m - n + 1)/resolution;
00183
00184
00185
00186 interval = n -1;
00187 for (int i = 0; i < resolution; ++i){
00188 double *mat_ptr = basic_mat_.ptr<double>(i);
00189 computePoint(control, &tmp_point, &tmp_tangent, mat_ptr, interval, n);
00190 curve_[i].x = tmp_point.x;
00191 curve_[i].y = tmp_point.y;
00192 curve_[i].z = tmp_point.z;
00193
00194
00195
00196 tangent_[i].x = tmp_tangent.x;
00197 tangent_[i].y = tmp_tangent.y;
00198 tangent_[i].z = tmp_tangent.z;
00199
00200
00201
00202
00203
00204
00205
00206
00207 interval += increment;
00208 }
00209
00210
00211
00212 }
00213
00214
00215 BSpline::~BSpline(){
00216
00217
00218 if (curve_ != NULL) delete [] curve_;
00219 if (tangent_ != NULL) delete [] tangent_;
00220 }