LR-splines  0.5
TestReadWrite.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <iostream>
00003 #include <string.h>
00004 #include <fstream>
00005 #include "LRSpline/LRSplineSurface.h"
00006 #include "LRSpline/LRSplineVolume.h"
00007 #include "LRSpline/MeshRectangle.h"
00008 #include "LRSpline/Meshline.h"
00009 #include "LRSpline/Profiler.h"
00010 #include "LRSpline/Element.h"
00011 
00012 using namespace Go;
00013 using namespace LR;
00014 using namespace std;
00015 
00016 int main(int argc, char **argv) {
00017 
00018         // set default parameter values
00019         int p1 = 3;
00020         int p2 = 3;
00021         int p3 = 3;
00022         int n1 = 9;
00023         int n2 = 9;
00024         int n3 = 9;
00025         int dim = 4;
00026         bool rat = false;
00027         bool vol = false;
00028         char *inputFileName = NULL;
00029         string parameters(" parameters: \n" \
00030                           "   -p1  <n>  polynomial ORDER (degree+1) in first parametric direction\n" \
00031                           "   -p2  <n>  polynomial order in second parametric direction\n" \
00032                           "   -p3  <n>  polynomial order in third parametric direction\n" \
00033                           "   -n1  <n>  number of basis functions in first parametric direction\n" \
00034                           "   -n2  <n>  number of basis functions in second parametric direction\n" \
00035                           "   -n3  <n>  number of basis functions in third parametric direction\n" \
00036                           "   -dim <n>  dimension of the controlpoints\n" \
00037                           "   -vol      enforce trivariate volumetric test case\n" \
00038                           "   -help     display (this) help screen\n");
00039         
00040         // read input
00041         for(int i=1; i<argc; i++) {
00042                 if(strcmp(argv[i], "-p1") == 0)
00043                         p1 = atoi(argv[++i]);
00044                 else if(strcmp(argv[i], "-p2") == 0)
00045                         p2 = atoi(argv[++i]);
00046                 else if(strcmp(argv[i], "-p3") == 0) {
00047                         p3 = atoi(argv[++i]);
00048                         vol = true;
00049                 } else if(strcmp(argv[i], "-n1") == 0)
00050                         n1 = atoi(argv[++i]);
00051                 else if(strcmp(argv[i], "-n2") == 0)
00052                         n2 = atoi(argv[++i]);
00053                 else if(strcmp(argv[i], "-n3") == 0) {
00054                         n3 = atoi(argv[++i]);
00055                         vol = true;
00056                 } else if(strcmp(argv[i], "-dim") == 0)
00057                         dim = atoi(argv[++i]);
00058                 else if(strcmp(argv[i], "-vol") == 0)
00059                         vol = true;
00060                 else if(strcmp(argv[i], "-help") == 0) {
00061                         cerr << "usage: " << argv[0] << " [inputfile] [parameters]" << endl << parameters;
00062                         exit(0);
00063                 } else {
00064                         if(inputFileName != NULL) {
00065                                 cerr << "usage: " << argv[0] << " [inputfile] [parameters]" << endl << parameters;
00066                                 exit(1);
00067                         } else {
00068                                 inputFileName = argv[i];
00069                         }
00070                 }
00071         }
00072 
00073         // do some error testing on input
00074         if(n1 < p1) {
00075                 cerr << "ERROR: n1 must be greater or equal to p1\n";
00076                 exit(2);
00077         } else if(n2 < p2) {
00078                 cerr << "ERROR: n2 must be greater or equal to p2\n";
00079                 exit(2);
00080         } else if(n3 < p3) {
00081                 cerr << "ERROR: n3 must be greater or equal to p3\n";
00082                 exit(2);
00083         }
00084 
00085                 
00086         // read the surface, or make one up if none specified
00087         LRSplineSurface *lrs = NULL;
00088         LRSplineVolume  *lrv = NULL;
00089         if(inputFileName == NULL) {
00090                 // make a uniform integer knot vector 
00091                 double knot_u[n1+p1];
00092                 double knot_v[n2+p2];
00093                 double knot_w[n3+p3];
00094                 for(int i=0; i<p1+n1; i++)
00095                         knot_u[i] = (i<p1) ? 0 : (i>n1) ? n1-p1+1 : i-p1+1;
00096                 for(int i=0; i<p2+n2; i++)
00097                         knot_v[i] = (i<p2) ? 0 : (i>n2) ? n2-p2+1 : i-p2+1;
00098                 for(int i=0; i<p3+n3; i++)
00099                         knot_w[i] = (i<p3) ? 0 : (i>n3) ? n3-p3+1 : i-p3+1;
00100         
00101                 // create a list of random control points (all between 0.1 and 1.1)
00102                 int nCP  = (vol) ? n1*n2*n3 : n1*n2;
00103                 nCP     *= (dim+rat);
00104                 double cp[nCP];
00105                 int k=0;
00106                 for(int i=0; i<nCP; i++) // 839 as a generator over Z_853 gives a period of 425. Should suffice
00107                         cp[k++] = (i*839 % 853) / 853.0 + 0.1;  // rational weights also random and thus we need >0
00108 
00109                 if(vol) {
00110                         lrv = new LRSplineVolume(n1, n2, n3, p1, p2, p3, knot_u, knot_v, knot_w, cp, dim, rat);
00111                         // insert a cross in the lower left corner element (should always be possible)
00112                         lrv->insert_line(new MeshRectangle(.5,0,0,  .5,1,1) );
00113                         lrv->insert_line(new MeshRectangle(0,.5,0,  1,.5,1) );
00114                         lrv->insert_line(new MeshRectangle(0,0,.5,  1,1,.5) );
00115                 } else {
00116                         lrs = new LRSplineSurface(n1, n2, p1, p2, knot_u, knot_v, cp, dim, rat);
00117                         // insert a cross in the lower left corner element (should always be possible)
00118                         lrs->insert_const_u_edge(.5, 0, 1);
00119                         lrs->insert_const_v_edge(.5, 0, 1);
00120                 }
00121 
00122 
00123         } else {
00124                 ifstream inputfile;
00125                 inputfile.open(inputFileName);
00126                 if(!inputfile.is_open()) {
00127                         cerr << "Error: could not open file " << inputFileName << endl;
00128                         exit(3);
00129                 }
00130                 lrs = new LRSplineSurface();
00131                 inputfile >> *lrs;
00132         }
00133 
00134         // test writing to file
00135         ofstream lrfile;
00136         lrfile.open("TestReadWrite.lr");
00137         lrfile.precision(16);
00138         if(vol) lrfile << *lrv << endl;
00139         else    lrfile << *lrs << endl;
00140         
00141         lrfile.close();
00142 
00143         // test reading from file
00144         LRSplineSurface inputSplineSurf;
00145         LRSplineVolume  inputSplineVol;
00146         ifstream inputFile;
00147         inputFile.open("TestReadWrite.lr");
00148         if(vol) inputSplineVol.read(inputFile);
00149         else    inputSplineSurf.read(inputFile);
00150         inputFile.close();
00151 
00152         // write this out again and see if the are the same
00153         ofstream lrfile2;
00154         lrfile2.open("TestReadWrite2.lr");
00155         if(vol) lrfile2 << inputSplineVol  << endl;
00156         else    lrfile2 << inputSplineSurf << endl;
00157         lrfile2.close();
00158 
00159         // take a (deep) copy, screw up the original and write the copied LR spline
00160         // should remain unchanged if it is a proper deep copy
00161         ofstream lrfile3;
00162         ofstream lrfile4;
00163         lrfile3.open("TestReadWrite3.lr");
00164         lrfile4.open("TestReadWrite4.lr"); // this SHOULD be different from 1-3. Reg test shouldn't check against this one
00165         if(vol) {
00166                 LRSplineVolume *copyVol = lrv->copy();
00167                 lrv->getBasisfunction(0)->getknots(0)[0] = -99999;
00168                 lrv->getElement(0)->setUmin(               -99999);
00169                 lrv->getMeshRectangle(0)->start_[0]      = -99999;
00170                 lrfile3 << *copyVol << endl;
00171                 lrfile4 << *lrv << endl;
00172         } else {
00173                 LRSplineSurface *copySurf = lrs->copy();
00174                 lrs->getBasisfunction(0)->getknots(0)[0] = -99999;
00175                 lrs->getElement(0)->setUmin(               -99999);
00176                 (*lrs->meshlineBegin())->start_          = -99999;
00177                 lrfile3 << *copySurf << endl;
00178                 lrfile4 << *lrs << endl;
00179         }
00180         lrfile3.close();
00181 
00182 }
00183