• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/DGVDiophantineVTK.cpp

Go to the documentation of this file.
00001 
00022 #include "DGVDiophantineVTK.h"
00023 
00024 #include <QApplication>
00025 
00026 #include "DGVFile.h"
00027 
00028 DGVDiophantineVTK::DGVDiophantineVTK(QWidget *parent) : QVTKWidget(parent)
00029 {
00030     latticeSet = false;
00031     generatedBefore = false;
00032     manual = false;
00033 
00034     pointOnLine.resize(3);
00035     pointOnLine = 0;
00036 
00037     intercept = 0;
00038 
00039     displayProperties = vtkProperty::New();
00040 }
00041 
00042 DGVDiophantineVTK::~DGVDiophantineVTK()
00043 {
00044     if(generatedBefore)
00045     {
00046         line->Delete();
00047         mapper->Delete();
00048         actor->Delete();
00049     }
00050     displayProperties->Delete();
00051 }
00052 
00053 void DGVDiophantineVTK::setLattice(DGVLatticeVTK *lattice)
00054 {
00055     gamma = lattice;
00056 
00057     start.resize(gamma->getDimension());
00058     start = 0.0;
00059     end.resize(gamma->getDimension());
00060     end = 1.0;
00061 
00062     latticeSet = true;
00063 }
00064 
00065 void DGVDiophantineVTK::setCoefficients(DiophantineType x, DiophantineType y, DiophantineType z)
00066 {
00067     xCoefficient = x;
00068     yCoefficient = y;
00069     zCoefficient = z;
00070     manual = false;
00071 }
00072 
00073 void DGVDiophantineVTK::setPointOnLine(DiophantineType x, DiophantineType y, DiophantineType z)
00074 {
00075     pointOnLine = x, y, z;
00076 
00078     intercept = yCoefficient*pointOnLine(1) - xCoefficient*pointOnLine(0);
00079     cerr << "Intercept: " << intercept << endl;
00080     cerr << "WARNING: 3 Dimensions not supported for lines yet!" << endl;
00081 
00082 }
00083 
00084 void DGVDiophantineVTK::setStartPoint(Array<DiophantineType,1> &point)
00085 {
00086     if(latticeSet)
00087     {
00088         start = 1.0*point;
00089         manual = true;
00090     }
00091 }
00092 
00093 void DGVDiophantineVTK::setStartPoint(DiophantineType x, DiophantineType y)
00094 {
00095     if(latticeSet)
00096     {
00097         if(gamma->getDimension() == 2)
00098         {
00099             start(0) = x;
00100             start(1) = y;
00101             manual = true;
00102         }
00103     }
00104 }
00105 
00106 void DGVDiophantineVTK::setEndPoint(Array<DiophantineType,1> &point)
00107 {
00108     if(latticeSet)
00109     {
00110         end = 1.0*point;
00111         manual = true;
00112     }
00113 }
00114 
00115 void DGVDiophantineVTK::setEndPoint(DiophantineType u, DiophantineType v)
00116 {
00117     if(latticeSet)
00118     {
00119         if(gamma->getDimension() == 2)
00120         {
00121             end(0) = u;
00122             end(1) = v;
00123             manual = true;
00124         }
00125     }
00126 }
00127 
00128 void DGVDiophantineVTK::setColour(double red, double green, double blue)
00129 {
00130     displayProperties->SetColor(red,green,blue);
00131 }
00132 
00133 DGVLatticeVTK* DGVDiophantineVTK::loadFile(QString filename)
00134 {
00136         int xSize_Primary = 32, ySize_Primary = 32, xSize_Secondary = 0, ySize_Secondary = 0, red = 0, green = 0, blue = 0;
00137         Array<long,1> integersRead;
00138         DGVLatticeVTK *lattice = new DGVLatticeVTK;
00139         DGVFile inFile;
00140 
00141         inFile.open(filename,"r");
00142 
00144     qApp->processEvents();
00145         inFile.readNextLineAsIntegers(integersRead);
00146         while(integersRead.size() == 0) //Ignore comments
00147         inFile.readNextLineAsIntegers(integersRead);
00148 
00150     if(integersRead.size() != 5)
00151     {
00152         cerr << ">| File FORMAT ERROR: Check file format at Primary Lattice!" << endl;
00153         return lattice;
00154     }
00155 
00156     xSize_Primary = integersRead(0);
00157     ySize_Primary = integersRead(1);
00158     red = integersRead(2);
00159     green = integersRead(3);
00160     blue = integersRead(4);
00161     cerr << ">| Primary: " << xSize_Primary << "x" << ySize_Primary << endl;
00162 
00163     gamma = lattice;
00164         lattice->setLatticeSize(xSize_Primary,ySize_Primary);
00165         lattice->setLatticeColour(red,green,blue);
00166         lattice->generateLattice();
00167         lattice->viewAsMatrix();
00168         lattice->enableAxes();
00169         lattice->resize(minWindowSize,minWindowSize);
00170         latticeSet = true;
00171 
00173     inFile.readNextLineAsIntegers(integersRead);
00174         while(integersRead.size() == 0) //Ignore comments
00175         inFile.readNextLineAsIntegers(integersRead);
00176 
00178     if(integersRead.size() != 5)
00179     {
00180         cerr << ">| File FORMAT ERROR: Check file format at Secondary Lattice!" << endl;
00181         return lattice;
00182     }
00183     xSize_Secondary = integersRead(0);
00184     ySize_Secondary = integersRead(1);
00185     red = integersRead(2);
00186     green = integersRead(3);
00187     blue = integersRead(4);
00188     cerr << ">| Secondary: " << xSize_Secondary << "x" << ySize_Secondary << endl;
00189 
00190     DiophantineType xStart = xSize_Primary/2 - xSize_Secondary/2, yStart = ySize_Primary/2 - ySize_Secondary/2;
00191         for(int j = xStart; j < xSize_Secondary+xStart; j ++)
00192         for(int k = yStart; k < ySize_Secondary+xStart; k ++)
00193         {
00194             qApp->processEvents();
00195             lattice->setLatticePointColour(j,k,red,green,blue);
00196         }
00197 
00199     inFile.readNextLineAsIntegers(integersRead);
00200         while(integersRead.size() == 0) //Ignore comments
00201         inFile.readNextLineAsIntegers(integersRead);
00202 
00204     if(integersRead.size() != 3)
00205     {
00206         cerr << ">| File FORMAT ERROR: Check file format at Background Colour!" << endl;
00207         return lattice;
00208     }
00209     red = integersRead(0);
00210     green = integersRead(1);
00211     blue = integersRead(2);
00212     cerr << ">| Background Colour: " << red << ", " << green << ", " << blue << endl;
00213     if(red >= 0 && blue >= 0 && green >= 0)
00214         lattice->setBackgroundColour(red,green,blue);
00215 
00216         //--------------------------------------------
00218         Array<DGVDiophantineVTK*,1> lines;
00219         int noOfLines = 0, noOfPoints = 0;
00220         DiophantineType p = 0, q = 0, x = 0, y = 0;
00221 
00222     while(!inFile.atEnd())
00223     {
00224         qApp->processEvents();
00225 
00227         inFile.readNextLineAsIntegers(integersRead);
00228         while(integersRead.size() == 0 && !inFile.atEnd()) //Ignore comments
00229             inFile.readNextLineAsIntegers(integersRead);
00230 
00232         if(integersRead.size() == 3) //Colour
00233         {
00234             red = integersRead(0);
00235             green = integersRead(1);
00236             blue = integersRead(2);
00237         }
00238         else if(integersRead.size() == 4) //Vector
00239         {
00240             x = integersRead(0)+xStart;
00241             y = integersRead(1)+yStart;
00242             q = integersRead(2)+xStart;
00243             p = integersRead(3)+yStart;
00244             lines.resizeAndPreserve(noOfLines+1);
00245             lines(noOfLines) = new DGVDiophantineVTK;
00246             lines(noOfLines)->setLattice(lattice);
00247             lines(noOfLines)->setStartPoint(x,y);
00248             lines(noOfLines)->setEndPoint(integersRead(0)+q,integersRead(1)+p);
00249             lines(noOfLines)->setColour(red,green,blue);
00250             lines(noOfLines)->generateLine();
00251             noOfLines ++;
00252         }
00253         else if(integersRead.size() == 5) //Point
00254         {
00255             x = integersRead(0)+xStart;
00256             y = integersRead(1)+yStart;
00257             red = integersRead(2);
00258             green = integersRead(3);
00259             blue = integersRead(4);
00260             lattice->setLatticePointColour(x,y,red,green,blue);
00261             noOfPoints ++;
00262         }
00263         else if(integersRead.size() == 7) //Line
00264         {
00265             x = integersRead(0)+xStart;
00266             y = integersRead(1)+yStart;
00267             red = integersRead(2);
00268             green = integersRead(3);
00269             blue = integersRead(4);
00270             p = integersRead(5);
00271             q = integersRead(6);
00272             lines.resizeAndPreserve(noOfLines+1);
00273             lines(noOfLines) = new DGVDiophantineVTK;
00274             lines(noOfLines)->setLattice(lattice);
00275             lines(noOfLines)->setCoefficients(p,q,0);
00276             lines(noOfLines)->setPointOnLine(x,y,0);
00277             lines(noOfLines)->setColour(red,green,blue);
00278             lines(noOfLines)->generateLine();
00279             noOfLines ++;
00280         }
00281         else if(integersRead.size() == 0)
00282             continue;
00283         else
00284         {
00285             cerr << ">| File FORMAT ERROR: Check file format at Line Parameters!" << endl;
00286             cerr << ">| No more points/lines will be done!" << endl;
00287             break;
00288         }
00289     }
00290 
00291     cerr << ">| No. Of Lines: " << noOfLines << ", No. of Points: " << noOfPoints << endl;
00292     return lattice;
00293 }
00294 
00295 void DGVDiophantineVTK::generateLine()
00296 {
00297     if(latticeSet)
00298     {
00299         Array<latticeType,1> parameters(gamma->getLatticeParameters());
00300         double p1_x, p1_y;
00301 
00302 
00303         if(!generatedBefore)
00304         {
00305             line = vtkLineSource::New();
00306             mapper = vtkPolyDataMapper::New();
00307             actor = vtkActor::New();
00308             generatedBefore = true;
00309         }
00310 
00311         parameters = gamma->getLatticeParameters();
00312         if(!manual)
00313         {
00315             Array<int,1> extents(gamma->getDimension());
00316 
00317             extents = gamma->getLatticeSize();
00318             if(gamma->getDimension() == 2)
00319             {
00320                 p1_x = 0;
00321                 p1_y = static_cast<double>(xCoefficient)/yCoefficient*p1_x + static_cast<double>(intercept)/yCoefficient;
00322                 if(p1_y >= 0 && p1_y < extents(1))
00323                 {
00324                     start(0) = p1_x;
00325                     start(1) = p1_y;
00326                 }
00327                 p1_x = extents(0)-1;
00328                 p1_y = static_cast<double>(xCoefficient)/yCoefficient*p1_x + static_cast<double>(intercept)/yCoefficient;
00329                 if(p1_y >= 0 && p1_y < extents(1))
00330                 {
00331                     end(0) = p1_x;
00332                     end(1) = p1_y;
00333                 }
00334                 p1_y = 0;
00335                 p1_x = static_cast<double>(yCoefficient)/xCoefficient*p1_y - static_cast<double>(intercept)/xCoefficient;
00336                 if(p1_x >= 0 && p1_x < extents(0))
00337                 {
00338                     start(0) = p1_x;
00339                     start(1) = p1_y;
00340                 }
00341                 p1_y = extents(1)-1;
00342                 p1_x = static_cast<double>(yCoefficient)/xCoefficient*p1_y - static_cast<double>(intercept)/xCoefficient;
00343                 if(p1_x >= 0 && p1_x < extents(0))
00344                 {
00345                     end(0) = p1_x;
00346                     end(1) = p1_y;
00347                 }
00348             }
00349             else
00350             {
00351                 cerr << "ERROR: Dimension not supported for lines yet!" << endl;
00352                 return;
00353             }
00354         }
00355 
00356         line->SetPoint1(start(0)*parameters(0),start(1)*parameters(1),0); 
00357         line->SetPoint2(end(0)*parameters(0),end(1)*parameters(1),0);
00358         line->Update();
00359 
00360         mapper->SetInput(line->GetOutput());
00361         //mapper->SetScalarRange(0, radius);
00362         mapper->Update();
00363 
00364         actor->SetMapper(mapper);
00365         actor->SetProperty(displayProperties);
00366         gamma->addActorToLattice(actor);
00367     }
00368 }

Generated on Wed Sep 8 2010 01:36:51 for DGV by  doxygen 1.7.1