LR-splines  0.5
refine.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <iostream>
00003 #include <cstring>
00004 #include <sstream>
00005 #include <fstream>
00006 #ifdef HAS_GOTOOLS
00007         #include <GoTools/geometry/SplineSurface.h>
00008         #include <GoTools/geometry/ObjectHeader.h>
00009 #endif
00010 
00011 #include "LRSpline/LRSpline.h"
00012 #include "LRSpline/LRSplineSurface.h"
00013 #include "LRSpline/LRSplineVolume.h"
00014 #include "LRSpline/Element.h"
00015 #include "LRSpline/Meshline.h"
00016 
00017 using namespace LR;
00018 using namespace std;
00019 
00020 int main(int argc, char **argv) {
00021         vector<int> el(1,0);
00022         int m      = 1;
00023         int scheme = 2;
00024         bool dumpfile = false;
00025         string filename("");
00026         string outfile("result.lr");
00027         stringstream parameters;
00028         parameters << " parameters: \n"
00029                    << "   -el     <n[]> comma-seperated list of element (or basisfunction) indices \n"
00030                    << "   -m       <n>  knot multiplicity\n"
00031                    << "   -out     <s>  output file name\n"
00032                    << "   -scheme  <n>  refinement scheme (0=FULLSPAN, 1=MINSPAN, 2=STRUCT)\n"
00033                    << "   -dumpfile     create .eps and .m files of the meshes (Surfaces only)\n"
00034                    << "   -help         displays (this) help screen\n"
00035                    << " default values:\n"
00036                    << "   el     = {" << el[0]   << "}\n"
00037                    << "   m      = "  << m       <<  "\n"
00038                    << "   scheme = "  << scheme  <<  "\n"
00039                    << "   out    = "  << outfile <<  "\n";
00040 
00041         // read input arguments
00042         for(int i=1; i<argc; i++) {
00043                 if(strcmp(argv[i], "-scheme") == 0)
00044                         scheme = atoi(argv[++i]);
00045                 else if(strcmp(argv[i], "-m") == 0)
00046                         m = atoi(argv[++i]);
00047                 else if(strcmp(argv[i], "-out") == 0)
00048                         outfile = argv[++i];
00049                 else if(strcmp(argv[i], "-el") == 0) {
00050                         el.clear();
00051                         char *tok = strtok(argv[++i], ",");
00052                         while(tok != NULL) {
00053                                 el.push_back(atoi(tok));
00054                                 tok = strtok(NULL, ",");
00055                         }
00056                 } else if(strcmp(argv[i], "-dumpfile") == 0)
00057                         dumpfile = true;
00058                 else if(strcmp(argv[i], "-help") == 0) {
00059                         cout << "usage: " << argv[0] << " [parameters] " << endl << parameters.str() << endl;
00060                         exit(0);
00061                 } else if(filename.length() > 0) {
00062                         cout << "usage: " << argv[0] << " [parameters] " << endl << parameters.str() << endl;
00063                         exit(1);
00064                 } else {
00065                         filename = argv[i];
00066                 }
00067         }
00068 
00069         // error test input
00070         if(filename.length() == 0) {
00071                 cout << "usage: " << argv[0] << " [parameters] " << endl << parameters.str() << endl;
00072                 exit(1);
00073         }
00074         if(scheme < 0)
00075                 scheme = 0;
00076         if(scheme > 2)
00077                 scheme = 2;
00078         
00079         // read input file
00080         ifstream inputfile;
00081         inputfile.open(filename.c_str());
00082         if(!inputfile.is_open()) {
00083                 cerr << "Error: could not open file " << filename << endl;
00084                 exit(2);
00085         }
00086         LRSpline        *lr;
00087         LRSplineSurface *lrs = NULL;
00088         LRSplineVolume  *lrv = NULL;
00089         char buffer[512];
00090         inputfile.getline(buffer, 512); // peek the first line to figure out if it's an LRSpline or a GoTools spline
00091         inputfile.seekg(ios_base::beg);
00092         if(strncmp(buffer, "# LRSPLINE VOLUME",17)==0) {
00093                 lr = lrv = new LRSplineVolume();
00094                 inputfile >> *lrv;
00095         } else if(strncmp(buffer, "# LRSPLINE",10)==0) {
00096                 lr = lrs = new LRSplineSurface();
00097                 inputfile >> *lrs;
00098         } else {
00099 #ifdef HAS_GOTOOLS
00100                 Go::ObjectHeader   head;
00101                 inputfile >> head;
00102                 if(head.classType() == Go::Class_SplineVolume) {
00103                         Go::SplineVolume     sv;
00104                         inputfile >> sv;
00105                         lr = lrv = new LRSplineVolume(&sv);
00106                 } else if(head.classType() == Go::Class_SplineSurface) {
00107                         Go::SplineSurface    ss;
00108                         inputfile >> ss;
00109                         lr = lrs = new LRSplineSurface(&ss);
00110                 }  else {
00111                         std::cerr << "Unsupported GoTools object\n";
00112                         exit(3);
00113                 }
00114 #endif
00115         }
00116         
00117         // setup refinement parameters
00118         lr->setRefMultiplicity(m);
00119         if(scheme == 0)
00120                 lr->setRefStrat(LR_FULLSPAN);
00121         else if(scheme == 1)
00122                 lr->setRefStrat(LR_MINSPAN);
00123         else if(scheme == 2)
00124                 lr->setRefStrat(LR_STRUCTURED_MESH);
00125 
00126         // do actual refinement
00127         if(scheme == 2)
00128                 lr->refineBasisFunction(el);
00129         else
00130                 lr->refineElement(el);
00131 
00132         // write result to file
00133         ofstream refinedFile;
00134         refinedFile.open(outfile.c_str());
00135         refinedFile << *lr;
00136         refinedFile.close();
00137         cout << "Written refined LR-spline to " << outfile << endl;
00138 
00139         // dump debug eps files
00140         if(dumpfile && lrs != NULL) {
00141                 ofstream meshfile;
00142                 meshfile.open("mesh.eps");
00143                 lrs->writePostscriptMesh(meshfile);
00144                 meshfile.close();
00145 
00146                 ofstream functionfile;
00147                 functionfile.open("functions.eps");
00148                 lrs->writePostscriptFunctionSpace(functionfile);
00149                 functionfile.close();
00150 
00151                 ofstream domainfile;
00152                 domainfile.open("domain.eps");
00153                 lrs->writePostscriptElements(domainfile, 10, 10);
00154                 domainfile.close();
00155 
00156                 ofstream controlmesh;
00157                 controlmesh.open("controlmesh.eps");
00158                 lrs->writePostscriptMeshWithControlPoints(controlmesh, 10, 10);
00159                 controlmesh.close();
00160 
00161                 cout << endl;
00162                 cout << "Written mesh to mesh.eps, functions.eps, domain.eps and controlmesh.eps \n";
00163         }
00164 }