C:/Projekte/C++/FlowVIS_107/src/StreamLineGrid.cpp

Go to the documentation of this file.
00001 #include "StreamLineGrid.h"
00002 
00003 // needed for writing log-file
00004 #include <iostream>
00005 #include <fstream>
00006 
00007 
00008 StreamLineGrid::StreamLineGrid( float dSep )
00009 {
00010   //cout << "Create new SL-grid..." << endl;
00011 
00012   m_cellSize = dSep;
00013   m_dim = (int) ceil( (float)( 1.0f/m_cellSize ) );
00014   m_grid = new slgGridCell[ m_dim*m_dim ];
00015 }
00016 
00017 
00018 StreamLineGrid::~StreamLineGrid()
00019 {
00020   delete [] m_grid;
00021 }
00022 
00023 
00024 bool StreamLineGrid::addPoint( vec3 point )
00025 {
00026   // calculate cell-index
00027   int cellIdx = getCellIndex( point );
00028 
00029   // add point to cell
00030   if ( cellIdx >= 0 ) 
00031   {
00032     m_grid[ cellIdx ].points.push_back( point );
00033 
00034     //cout << "Added point " << point << " to cell " << cellIdx << endl;
00035     //cout << "Number of points in this cell: " << m_grid[ cellIdx ].points.size() << endl;
00036 
00037     return true;
00038   }
00039   else 
00040     // invalid index
00041     return false;
00042 }
00043 
00044 
00045 slgGridCell* StreamLineGrid::getSurroundingCells( vec3 point )
00046 {
00047   int numNeighbors = NUM_NEIGHBORS;
00048   slgGridCell *neighborCells = new slgGridCell[ numNeighbors*numNeighbors ];
00049 
00050   // get index of lower-left cell
00051   int cellIdxX = getCellIndexX( point ) - 1;
00052   int cellIdxY = getCellIndexY( point ) - 1;
00053 
00054   // fetch points from grid
00055   for ( int i=0; i<(numNeighbors*numNeighbors); i++ )
00056   {
00057     // calculate new cell index
00058     int cIdxX = cellIdxX + ( i % numNeighbors );
00059     int cIdxY = (i < numNeighbors) ? (cellIdxY) : ( ( i < (2*numNeighbors) ) ? (cellIdxY+1) : (cellIdxY+2) );
00060 
00061     // add cell
00062     neighborCells[ i ] = getCell( getCellIndex( cIdxX, cIdxY ) );
00063   } // for 
00064 
00065   return neighborCells;
00066 }
00067 
00068 
00069 float StreamLineGrid::getMinimalDistance( vec3 point, vec3 exceptionPoint )
00070 {
00071   int numNeighbors = NUM_NEIGHBORS;
00072   slgGridCell *neighborCells = getSurroundingCells( point );
00073 
00074   bool useExceptionPoint = true;
00075 
00076   if ( exceptionPoint[X]==-1 && exceptionPoint[Y]==-1 && exceptionPoint[Z]==-1 ) useExceptionPoint = false;
00077 
00078   //cout << "excPt(" << exceptionPoint << "), use? " << useExceptionPoint << "\n";
00079 
00080   float minDist = 1.1f; // set to maximum distance
00081 
00082   // calculate min distance
00083   for ( int i=0; i<(numNeighbors*numNeighbors); i++ )
00084   {
00085     int nrPts = (int)neighborCells[i].points.size();
00086 
00087     if ( nrPts > 0 ) 
00088     {
00089       for ( int j=0; j<nrPts; j++ )
00090       {
00091         // calculate dist to every point
00092         vec3 dPoint = neighborCells[i].points.at(j);
00093 
00094         if ( useExceptionPoint && (dPoint == exceptionPoint) ) 
00095         {
00096           // skip this point
00097           //cout << "skipping exceptionPoint\n";
00098         }
00099         else
00100         {
00101           float dist = point.dist( dPoint );
00102           if ( dist < minDist ) 
00103           {
00104             minDist = dist;
00105             //cout << "new minDist: " << minDist << "\n";
00106           }
00107         }
00108 
00109       } // for every point
00110     }
00111 
00112   } // for all neighbor-cells
00113 
00114   return (minDist <= 1.0) ? minDist : -1;
00115 }
00116 
00117 
00118 /*
00119 bool StreamLineGrid::isValidPoint( vec3 point )
00120 {
00121   // dummy
00122   return false;
00123 }
00124 */
00125 
00126 slgGridCell StreamLineGrid::getCell( int cellIdx )
00127 {
00128   if ( cellIdx < 0 )
00129   {
00130     slgGridCell gc; 
00131     gc.points.clear();
00132 
00133     return gc;
00134   }
00135   else
00136     return m_grid[ cellIdx ];
00137 }
00138 
00139 /*
00140 vector<vec3> StreamLineGrid::getCellPoints( int cellIdx )
00141 {
00142   return m_grid[ cellIdx ].points;
00143 }
00144 */
00145 
00146 int StreamLineGrid::getCellIndexX( vec3 point )
00147 {
00148   return (int)floor( point[X] / m_cellSize );
00149 }
00150 
00151 
00152 int StreamLineGrid::getCellIndexY( vec3 point )
00153 {
00154   return (int)floor( point[Y] / m_cellSize );
00155 }
00156 
00157 
00158 int StreamLineGrid::getCellIndex( vec3 point )
00159 {
00160   int idxX = getCellIndexX( point );
00161   int idxY = getCellIndexY( point );
00162 
00163   return getCellIndex( idxX, idxY );
00164 }
00165 
00166 
00167 int StreamLineGrid::getCellIndex( int idxX, int idxY )
00168 {
00169   if ( idxX < 0      ||
00170        idxY < 0      ||
00171        idxX >= m_dim ||
00172        idxY >= m_dim 
00173      )
00174     // invalid index
00175     return -1;
00176 
00177   return idxX + idxY * m_dim;
00178 }
00179 
00180 
00181 
00182 void StreamLineGrid::printGrid()
00183 {
00184   cout << "Writing the grids attached points to log-file (this may take a while - please be patient!) ... ";
00185 
00186   ofstream gridPointsLogFile;
00187 
00188   gridPointsLogFile.open ("slGrid_points.log");
00189   gridPointsLogFile << "Grid with Points in m_grid:\n\nIdx:\n";
00190 
00191 
00192   // for all grid-cells
00193   for ( int i=0; i<m_dim*m_dim; i++ )
00194   {
00195     gridPointsLogFile << i << ":\n";
00196 
00197     int nrPts = (int)m_grid[i].points.size();
00198     if ( nrPts > 0 )
00199     {
00200       // for all points attached to cell
00201       for ( int j=0; j<nrPts; j++ )
00202       {
00203         gridPointsLogFile << "  > " << m_grid[i].points.at( j ) << "\n";
00204       }
00205     }
00206     else 
00207       gridPointsLogFile << "  > empty\n";
00208   }
00209 
00210 
00211   gridPointsLogFile.close();
00212   cout << " done. " << endl;
00213 }

Generated on Mon Jan 21 14:50:12 2008 for VisLU by  doxygen 1.5.4