00001 #include "StreamLineGrid.h"
00002
00003
00004 #include <iostream>
00005 #include <fstream>
00006
00007
00008 StreamLineGrid::StreamLineGrid( float dSep )
00009 {
00010
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
00027 int cellIdx = getCellIndex( point );
00028
00029
00030 if ( cellIdx >= 0 )
00031 {
00032 m_grid[ cellIdx ].points.push_back( point );
00033
00034
00035
00036
00037 return true;
00038 }
00039 else
00040
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
00051 int cellIdxX = getCellIndexX( point ) - 1;
00052 int cellIdxY = getCellIndexY( point ) - 1;
00053
00054
00055 for ( int i=0; i<(numNeighbors*numNeighbors); i++ )
00056 {
00057
00058 int cIdxX = cellIdxX + ( i % numNeighbors );
00059 int cIdxY = (i < numNeighbors) ? (cellIdxY) : ( ( i < (2*numNeighbors) ) ? (cellIdxY+1) : (cellIdxY+2) );
00060
00061
00062 neighborCells[ i ] = getCell( getCellIndex( cIdxX, cIdxY ) );
00063 }
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
00079
00080 float minDist = 1.1f;
00081
00082
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
00092 vec3 dPoint = neighborCells[i].points.at(j);
00093
00094 if ( useExceptionPoint && (dPoint == exceptionPoint) )
00095 {
00096
00097
00098 }
00099 else
00100 {
00101 float dist = point.dist( dPoint );
00102 if ( dist < minDist )
00103 {
00104 minDist = dist;
00105
00106 }
00107 }
00108
00109 }
00110 }
00111
00112 }
00113
00114 return (minDist <= 1.0) ? minDist : -1;
00115 }
00116
00117
00118
00119
00120
00121
00122
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
00141
00142
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
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
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
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 }