00001 #ifndef SPLINECURVE_H
00002 #define SPLINECURVE_H
00003
00004 #include <vector>
00005 #include <stdlib.h>
00006 #include <stdio.h>
00007 #include <fstream>
00008
00009 #include "Curve.h"
00010 #include "Overhauser.h"
00011
00016 template <class DataType, int dim>
00017 class SplineCurve : public Curve<DataType,dim>, public Spline<DataType,dim> {
00018 public:
00019 typedef Part<DataType,dim> PartType;
00020 typedef Curve<DataType,dim> CurveType;
00021 typedef Spline<DataType,dim> SplineType;
00022 typedef Connection ConBase;
00023 typedef Segment<DataType,dim> SegType;
00024 typedef Facet<DataType> FacetType;
00025
00026 SplineCurve() {
00027 CurveType::setName("spline");
00028 CurveType::centroid(0) = 0.; CurveType::centroid(1) = 0.; CurveType::centroid(2) = 0.;
00029 CurveType::length = DataType(0.);
00030 }
00031 ~SplineCurve() {}
00032
00033 virtual void read(const int type, std::string file) {
00034
00035
00036 ( mklog() << "\nREADING "<<file<<" type = "<<type<<" dim = "<<dim<<std::endl).flush();
00037 switch(type) {
00038 case PartType::pointList: {
00039
00040 DataType vtmp;
00041 #ifdef SPLINECURVE_DEBUG_ON
00042 ( mklog() << "filetype = pointList\n").flush();
00043 #endif
00044 int i=0;
00045 std::string tmp;
00046
00047 std::ifstream infile;
00048 infile.open(file.c_str(), std::ifstream::in);
00049 if (!infile.is_open()) {
00050 ( mklog() <<std::system("pwd")<< "\n\n\n\t\t\tSplineCurve read() CANNOT OPEN " << file << "\n\n\n").flush();
00051 std::cerr <<std::system("pwd")<< "\n\n\n\t\t\tSplineCurve read() CANNOT OPEN " << file << "\n\n\n";
00052 exit (-1);
00053
00054 }
00055
00056 infile >> tmp;
00057 infile >> tmp;
00058 CurveType::setName(tmp);
00059
00060 infile >> tmp; infile >> tmp; infile >> tmp;
00061
00062 while (infile.good()) {
00063
00064 if (!infile.good()) break;
00065
00066 PType p(0.);
00067
00068 infile >> vtmp; p(0) = vtmp;
00069 if (!infile.good()) break;
00070 infile >> vtmp; p(1) = vtmp;
00071
00072 #ifdef SPLINECURVE_DEBUG_ON
00073 printf(" read p(0) = %g p(1) = %g\n",p(0),p(1));
00074 #endif
00075 if (dim == 3) { infile >> vtmp; p(2) = vtmp; }
00076 CurveType::AddPoint(p);
00077
00078 i++;
00079 }
00080
00081 #ifdef SPLINECURVE_DEBUG_ON
00082 ( mklog() << "GetNthPoint(0) =" << CurveType::GetNthPoint(0)
00083 << " GetNthPoint(i-1) =" << CurveType::GetNthPoint(i-1) << std::endl
00084 << " norm_2(GetNthPoint(0)-GetNthPoint(i-1) ) ="
00085 << norm_2(CurveType::GetNthPoint(0)-CurveType::GetNthPoint(i-1) ) << std::endl ).flush();
00086 #endif
00087
00088 if ( norm_2(CurveType::GetNthPoint(0)-CurveType::GetNthPoint(i-1) ) < TOL ) {
00089
00090 CurveType::setClosure(1);
00091 }
00092 else
00093 {
00094
00095 CurveType::setClosure(0);
00096 }
00097
00098
00099
00100 for (register int i = 0; i < CurveType::GetNumPoints(); i++ ) {
00101 SplineType::AddSplineNode(CurveType::GetNthPoint(i) );
00102 }
00103
00104
00105
00106
00107 infile.close();
00108
00109 break;
00110 }
00111 case PartType::VTK_curve: {
00112
00113 break;
00114 }
00115 case PartType::Brep2D: {
00116
00117 break;
00118 }
00119 case PartType::Brep3D: {
00120
00121 break;
00122 }
00123 case PartType::STL: {
00124
00125 break;
00126 }
00127 }
00128 }
00129
00130 virtual void measure() {
00131 CurveType::measure();
00132 }
00133
00134 virtual void interpolate(int numip) {
00135 int i=0, k=0;
00136 double t=0.;
00137 double lt=0., ls=0.;
00138 int interval=0;
00139
00140 CurveType::cPoints.clear();
00141 CurveType::connectivity.clear();
00142
00143 #ifdef SPLINECURVE_DEBUG_ON
00144 int totInts=0, interval_s=0;
00145 printf("@@@@@@@ start interpolate %lu nodes\n",SplineType::nodes.size());
00146 #endif
00147
00148 PType p(0.), ip(0.), hp(0.);
00149 ip = SplineType::GetNthSplineNode(0);
00150 CurveType::AddPoint(ip);
00151
00152 #ifdef SPLINECURVE_DEBUG_ON
00153 printf("numip=%d\n",numip);
00154 #endif
00155 numip--;
00156 #ifdef SPLINECURVE_DEBUG_ON
00157 printf("DECREMENT numip=%d\n",numip);
00158 printf("length=%g\n",SplineType::length);
00159 for (int k = 0; k < SplineType::slength.size(); k++)
00160 printf("slength[%d]=%g ",k,SplineType::slength[k]);
00161 printf("\n");
00162 #endif
00163 for (i=1; i < numip; i++) {
00164 t = (double)i/(double)numip;
00165 interval = (int)(t / SplineType::delta_t);
00166 #ifdef SPLINECURVE_DEBUG_ON
00167 for (k = 0; k < SplineType::slength.size(); k++) {
00168 if (t < SplineType::slength[k]/CurveType::length ) {
00169 printf("k=%d t=%g slength[%d]=%g ratio=%g\n",k,t,k,SplineType::slength[k],SplineType::slength[k]/SplineType::length);
00170 break;
00171 }
00172 }
00173 totInts = (int)((double)1/SplineType::delta_t);
00174 #endif
00175 k--;
00176 interval = k;
00177
00178 if (t*SplineType::length < SplineType::slength[1]) {
00179 lt = (t - SplineType::delta_t*(double)interval) / SplineType::delta_t;
00180 #ifdef SPLINECURVE_DEBUG_ON
00181 printf("i=%d 1st interval %d:%d:%d : t=%g lt=%g\n",i,interval,k,totInts,t,lt);
00182 #endif
00183 ip = SplineType::nodes[0] + lt*(SplineType::nodes[1]-SplineType::nodes[0]);
00184
00185 ls = 0.5;
00186 hp = lt*(SplineType::nodes[1]-CurveType::cPoints[CurveType::cPoints.size()-1]);
00187 #ifdef SPLINECURVE_DEBUG_ON
00188 ( mklog() << " hp = " << hp << " nodes[0] = " << SplineType::nodes[0]
00189 << " nodes[1] = " << SplineType::nodes[1] << std::endl
00190 << " progress =" << t*SplineType::length << " 1st seg.progress ="
00191 << t*SplineType::length/SplineType::slength[1] << std::endl ).flush();
00192 #endif
00193 ls = t*SplineType::length/SplineType::slength[1];
00194
00195 ip = SplineType::nodes[0] + ls*(SplineType::nodes[1] - SplineType::nodes[0]);
00196 PType p(0.);
00197 p = ip;
00198 #ifdef SPLINECURVE_DEBUG_ON
00199 ( mklog() << p << std::endl ).flush();
00200 #endif
00201 CurveType::AddPoint(p);
00202 }
00203 else if (t*SplineType::length >= SplineType::slength[SplineType::slength.size()-2] ) {
00204
00205 lt = (t - SplineType::delta_t*(double)interval) / SplineType::delta_t;
00206
00207
00208 ls = (t*SplineType::length - SplineType::slength[SplineType::slength.size()-2]) / (SplineType::length-SplineType::slength[SplineType::slength.size()-2]);
00209 #ifdef SPLINECURVE_DEBUG_ON
00210 interval_s = (int)(t / SplineType::delta_t);
00211 printf("i=%d last interval %d:%d:%d : t=%g lt=%g delta_t=%g\n",i,interval,k,totInts,t,lt,SplineType::delta_t);
00212 printf("delta_s=%g interval_s=%d ls=%g t*length=%g slength[nodes.size()-2]=%g\n",SplineType::delta_s,interval_s,ls,t*SplineType::length,SplineType::slength[SplineType::nodes.size()-2] );
00213 #endif
00214 ip = SplineType::nodes[SplineType::nodes.size()-2] + ls*(SplineType::nodes[SplineType::nodes.size()-1]-SplineType::nodes[SplineType::nodes.size()-2]);
00215 PType p(0.);
00216 p = ip;
00217 #ifdef SPLINECURVE_DEBUG_ON
00218 ( mklog() << "nodes[nodes.size()-2]=" << SplineType::nodes[SplineType::nodes.size()-2] << std::endl
00219 << "nodes[nodes.size()-1]=" << SplineType::nodes[SplineType::nodes.size()-1] << std::endl
00220 << p << std::endl ).flush();
00221 #endif
00222 CurveType::AddPoint(p);
00223 }
00224 else {
00225 #ifdef SPLINECURVE_DEBUG_ON
00226 printf("i=%d Overhauser interval %d:%d : t=%g\n",i,interval,totInts,t);
00227 #endif
00228 PType p(0.);
00229 ip = SplineType::GetInterpolatedSplineNode_dist(t);
00230 p = ip;
00231 #ifdef SPLINECURVE_DEBUG_ON
00232 ( mklog() << p << std::endl ).flush();
00233 #endif
00234 CurveType::AddPoint(p);
00235 }
00236 }
00237
00238 ip = SplineType::GetNthSplineNode((SplineType::nodes.size()-1));
00239 p = ip;
00240 CurveType::AddPoint(p);
00241 #ifdef SPLINECURVE_DEBUG_ON
00242 ( mklog() << p << " last point added.\n" ).flush();
00243
00244 printf("cPoints.size()=%d\n",CurveType::GetNumPoints() );
00245 for (register int i = 0; i < CurveType::GetNumPoints(); i++) {
00246 ( mklog() << i << " : " << CurveType::GetNthPoint(i) << std::endl ).flush();
00247 }
00248 #endif
00249 CurveType::makeConnections();
00250
00251 CurveType::cPoints_org.clear();
00252 std::copy (CurveType::cPoints.begin (), CurveType::cPoints.end (), std::back_inserter (CurveType::cPoints_org));
00253
00254
00255
00256 }
00257
00258 virtual void interpolate_linear(int numip) {
00259 int i=0, k=0;
00260 double t=0.;
00261 double lt=0., ls=0.;
00262 int interval=0;
00263
00264 CurveType::cPoints.clear();
00265 CurveType::connectivity.clear();
00266 #ifdef SPLINECURVE_DEBUG_ON
00267 int totInts=0, interval_s=0;
00268 printf("@@@@@@@ start interpolate %lu nodes\n",SplineType::nodes.size());
00269 #endif
00270 PType p(0.), ip(0.), hp(0.);
00271 ip = SplineType::GetNthSplineNode(0);
00272 CurveType::AddPoint(ip);
00273 #ifdef SPLINECURVE_DEBUG_ON
00274 printf("numip=%d\n",numip);
00275 #endif
00276 numip--;
00277 #ifdef SPLINECURVE_DEBUG_ON
00278 printf("DECREMENT numip=%d\n",numip);
00279 printf("length=%g\n",SplineType::length);
00280 for (int k = 0; k < SplineType::slength.size(); k++)
00281 printf("slength[%d]=%g ",k,SplineType::slength[k]);
00282 printf("\n");
00283 #endif
00284 for (i=1; i < numip; i++) {
00285 t = (double)i/(double)numip;
00286 interval = (int)(t / SplineType::delta_t);
00287 #ifdef SPLINECURVE_DEBUG_ON
00288 for (k = 0; k < SplineType::slength.size(); k++) {
00289 if (t < SplineType::slength[k]/CurveType::length ) {
00290 printf("k=%d t=%g slength[%d]=%g ratio=%g\n",k,t,k,SplineType::slength[k],SplineType::slength[k]/SplineType::length);
00291 break;
00292 }
00293 }
00294 totInts = (int)((double)1/SplineType::delta_t);
00295 #endif
00296 k--;
00297 interval = k;
00298
00299 if (t*SplineType::length < SplineType::slength[1]) {
00300
00301 lt = (t - SplineType::delta_t*(double)interval) / SplineType::delta_t;
00302 #ifdef SPLINECURVE_DEBUG_ON
00303 printf("i=%d 1st interval %d:%d:%d : t=%g lt=%g\n",i,interval,k,totInts,t,lt);
00304 #endif
00305 ip = SplineType::nodes[0] + lt*(SplineType::nodes[1]-SplineType::nodes[0]);
00306
00307 ls = 0.5;
00308 hp = lt*(SplineType::nodes[1]-CurveType::cPoints[CurveType::cPoints.size()-1]);
00309 #ifdef SPLINECURVE_DEBUG_ON
00310 ( mklog() << " hp = " << hp << " nodes[0] = " << SplineType::nodes[0] << " nodes[1] = " << SplineType::nodes[1] << std::endl
00311 << " progress =" << t*SplineType::length << " 1st seg.progress =" << t*SplineType::length/SplineType::slength[1] << std::endl ).flush();
00312 #endif
00313 ls = t*SplineType::length/SplineType::slength[1];
00314
00315 ip = SplineType::nodes[0] + ls*(SplineType::nodes[1] - SplineType::nodes[0]);
00316 PType p(0.);
00317 p = ip;
00318 #ifdef SPLINECURVE_DEBUG_ON
00319 ( mklog() << p << std::endl ).flush();
00320 #endif
00321 CurveType::AddPoint(p);
00322 }
00323 else if (t*SplineType::length >= SplineType::slength[SplineType::slength.size()-2] ) {
00324 lt = (t - SplineType::delta_t*(double)interval) / SplineType::delta_t;
00325 ls = (t*SplineType::length - SplineType::slength[SplineType::slength.size()-2]) / (SplineType::length-SplineType::slength[SplineType::slength.size()-2]);
00326 #ifdef SPLINECURVE_DEBUG_ON
00327 interval_s = (int)(t / SplineType::delta_t);
00328 printf("i=%d last interval %d:%d:%d : t=%g lt=%g delta_t=%g\n",i,interval,k,totInts,t,lt,SplineType::delta_t);
00329 printf("delta_s=%g interval_s=%d ls=%g t*length=%g slength[nodes.size()-2]=%g\n",SplineType::delta_s,interval_s,ls,t*SplineType::length,SplineType::slength[SplineType::nodes.size()-2] );
00330 #endif
00331 ip = SplineType::nodes[SplineType::nodes.size()-2] + ls*(SplineType::nodes[SplineType::nodes.size()-1]-SplineType::nodes[SplineType::nodes.size()-2]);
00332 PType p(0.);
00333 p = ip;
00334 #ifdef SPLINECURVE_DEBUG_ON
00335 ( mklog() << "nodes[nodes.size()-2]=" << SplineType::nodes[SplineType::nodes.size()-2] << std::endl
00336 << "nodes[nodes.size()-1]=" << SplineType::nodes[SplineType::nodes.size()-1] << std::endl
00337 << p << std::endl ).flush();
00338 #endif
00339 CurveType::AddPoint(p);
00340 }
00341 else {
00342 #ifdef SPLINECURVE_DEBUG_ON
00343 printf("i=%d Overhauser interval %d:%d : t=%g\n",i,interval,totInts,t);
00344 #endif
00345 PType p(0.);
00346 ip = SplineType::GetInterpolatedSplineNode_distLinear(t);
00347 p = ip;
00348 #ifdef SPLINECURVE_DEBUG_ON
00349 ( mklog() << p << std::endl ).flush();
00350 #endif
00351 CurveType::AddPoint(p);
00352 }
00353 }
00354
00355 ip = SplineType::GetNthSplineNode((SplineType::nodes.size()-1));
00356 p = ip;
00357 CurveType::AddPoint(p);
00358 #ifdef SPLINECURVE_DEBUG_ON
00359 ( mklog() << p << " last point added.\n" ).flush();
00360
00361 printf("cPoints.size()=%d\n",CurveType::GetNumPoints() );
00362 for (register int i = 0; i < CurveType::GetNumPoints(); i++) {
00363 ( mklog() << i << " : " << CurveType::GetNthPoint(i) << std::endl ).flush();
00364 }
00365 #endif
00366 CurveType::makeConnections();
00367 CurveType::cPoints_org.clear();
00368 std::copy (CurveType::cPoints.begin (), CurveType::cPoints.end (), std::back_inserter (CurveType::cPoints_org));
00369 }
00370
00371 virtual void updatePart(DataType dt,DataType time) {}
00372
00373 virtual void setDeformation(bool val) { PartType::deformable = val; }
00374 virtual bool GetDeformation() const { return PartType::deformable; }
00375 virtual void deformPart(DataType dt,DataType time) {
00376
00377 }
00378
00379 virtual void Restart(std::ifstream& ifs, int& pos, double& t, double& dt) {
00380 CurveType::Restart(ifs,pos,t,dt);
00381 SplineType::Restart(ifs,pos,t,dt);
00382
00383 }
00384
00385 virtual void Checkpointing(std::ofstream& ofs) {
00386 CurveType::Checkpointing(ofs);
00387 SplineType::Checkpointing(ofs);
00388
00389 }
00390
00391 virtual std::vector<PType>& GetNodes() {
00392 return SplineType::nodes;
00393 }
00394
00395 protected:
00396
00397 };
00398
00399 #endif // SPLINECURVE_H