[372] | 1 | /* osgVisual test. distortionNG, experimental. |
---|
| 2 | * |
---|
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 4 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 5 | * in the Software without restriction, including without limitation the rights |
---|
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 7 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 8 | * furnished to do so, subject to the following conditions: |
---|
| 9 | * |
---|
| 10 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 11 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 12 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 13 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 14 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 15 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 16 | * THE SOFTWARE. |
---|
| 17 | */ |
---|
| 18 | |
---|
[376] | 19 | #include <osgDB/ReadFile> |
---|
| 20 | #include <osg/Vec3d> |
---|
| 21 | #include <osgDB/fstream> |
---|
| 22 | |
---|
[372] | 23 | #include "DistortionSetupStrategyProjectSyntropy.h" |
---|
| 24 | |
---|
| 25 | DistortionSetupStrategyProjectSyntropy::DistortionSetupStrategyProjectSyntropy() |
---|
| 26 | { |
---|
[377] | 27 | _blendmapFilename = ""; |
---|
| 28 | _frustumFilename = ""; |
---|
| 29 | _distortionFilename = ""; |
---|
| 30 | _distortionType = DistortionSetupStrategy::UNDEFINED; |
---|
[376] | 31 | |
---|
| 32 | _distortionInitialized=false; |
---|
[372] | 33 | } |
---|
| 34 | |
---|
| 35 | DistortionSetupStrategyProjectSyntropy::~DistortionSetupStrategyProjectSyntropy() |
---|
| 36 | { |
---|
[377] | 37 | |
---|
[372] | 38 | } |
---|
| 39 | |
---|
[376] | 40 | void DistortionSetupStrategyProjectSyntropy::setDistortionInputFiles( std::string distortionFile, std::string blendmapFile, std::string frustumFile, DistortionSetupStrategy::distortionType type) |
---|
| 41 | { |
---|
[377] | 42 | _distortionType = type; |
---|
| 43 | _blendmapFilename = blendmapFile; |
---|
| 44 | _frustumFilename = frustumFile; |
---|
| 45 | _distortionFilename = distortionFile; |
---|
[376] | 46 | } |
---|
| 47 | |
---|
| 48 | int DistortionSetupStrategyProjectSyntropy::readFrustumFromCSVFile(std::string filePath, int numValues, float* frustumValues) |
---|
| 49 | { |
---|
| 50 | int numSeparator=0; |
---|
| 51 | std::string line, valueString; |
---|
| 52 | char separator=';'; |
---|
| 53 | size_t valueStart, valueEnd; |
---|
| 54 | |
---|
| 55 | osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str())); |
---|
| 56 | |
---|
| 57 | for (int i=0;i<numValues; i++) frustumValues[i]=0.0; |
---|
| 58 | |
---|
| 59 | if (inputFile.is_open()) |
---|
| 60 | { |
---|
| 61 | inputFile.clear(); |
---|
| 62 | inputFile.seekg (0, std::ios::beg); |
---|
| 63 | |
---|
| 64 | while (!inputFile.eof()) |
---|
| 65 | { |
---|
| 66 | getline (inputFile,line); |
---|
| 67 | |
---|
| 68 | if((line.size()>0) && (line.find('x')==std::string::npos)) |
---|
| 69 | {new osg::Vec2Array; |
---|
| 70 | numSeparator=0; |
---|
| 71 | for (size_t numLineChar=0; numLineChar < line.length(); numLineChar++) |
---|
| 72 | { |
---|
| 73 | if(line.at(numLineChar)==separator) numSeparator++; |
---|
| 74 | } |
---|
| 75 | if(numSeparator!=numValues-1) //Number of Entries per Line is not correct |
---|
| 76 | { |
---|
| 77 | OSG_ALWAYS<< "Inconsistency in file " << filePath << " detected. Please check this file." <<std::endl; |
---|
| 78 | return 1; |
---|
| 79 | } |
---|
| 80 | valueStart=0; |
---|
| 81 | valueEnd=0; |
---|
| 82 | for (int j=0;j<numValues; j++) |
---|
| 83 | { |
---|
| 84 | if(j!=numValues-1) |
---|
| 85 | { |
---|
| 86 | if((valueEnd=line.find(separator,valueStart))!=std::string::npos) |
---|
| 87 | { |
---|
| 88 | valueString=line.substr(valueStart,valueEnd-valueStart); |
---|
| 89 | valueStart=valueEnd+1; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | else valueString=line.substr(valueStart,line.size()); |
---|
| 93 | |
---|
| 94 | frustumValues[j]=atof(const_cast<char*>(valueString.c_str())); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | } |
---|
| 100 | inputFile.close(); |
---|
| 101 | } |
---|
| 102 | else //File couldn't be loaded |
---|
| 103 | { |
---|
| 104 | OSG_ALWAYS<< "Unable to open " << filePath << std::endl; |
---|
| 105 | return 1; |
---|
| 106 | } |
---|
| 107 | return 0; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | int DistortionSetupStrategyProjectSyntropy::readMeshDimensionsFromCSVFile(std::string filePath, int* meshRows, int* meshColumns) |
---|
| 111 | { |
---|
| 112 | int tempRows=0, tempCols=0, numSeparator=0, numValues=6; |
---|
| 113 | std::string line, valueString; |
---|
| 114 | char separator=';'; |
---|
| 115 | size_t valueStart, valueEnd; |
---|
| 116 | |
---|
| 117 | osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str())); |
---|
| 118 | |
---|
| 119 | if (inputFile.is_open()) |
---|
| 120 | { |
---|
| 121 | inputFile.clear(); |
---|
| 122 | inputFile.seekg (0, std::ios::beg); |
---|
| 123 | |
---|
| 124 | while (!inputFile.eof()) |
---|
| 125 | { |
---|
| 126 | getline (inputFile,line); |
---|
| 127 | |
---|
| 128 | if((line.size()>0) && (line.find('x')==std::string::npos)) |
---|
| 129 | { |
---|
| 130 | numSeparator=0; |
---|
| 131 | for (size_t numLineChar=0; numLineChar < line.length(); numLineChar++) |
---|
| 132 | { |
---|
| 133 | if(line.at(numLineChar)==separator) numSeparator++; |
---|
| 134 | } |
---|
| 135 | if(numSeparator!=numValues-1) //Number of Entries per Line is not correct |
---|
| 136 | { |
---|
| 137 | OSG_ALWAYS<< "Inconsistency in file " << filePath << " detected. Please check this file." <<std::endl; |
---|
| 138 | return 1; |
---|
| 139 | } |
---|
| 140 | //Get number of rows of the distortion grid, last valid line of input file contains actual value |
---|
| 141 | valueStart=line.rfind(separator,line.size()); |
---|
| 142 | valueString=line.substr(valueStart+1,line.size()-valueStart-1); |
---|
| 143 | tempRows=atoi(const_cast<char*>(valueString.c_str())); |
---|
| 144 | if(tempRows>*meshRows) *meshRows=tempRows; |
---|
| 145 | |
---|
| 146 | //Get number of columns of the distortion grid, last valid line of input file contains actual value |
---|
| 147 | valueEnd=valueStart-1; |
---|
| 148 | valueStart=line.rfind(separator,valueEnd); |
---|
| 149 | valueString=line.substr(valueStart+1,valueEnd-valueStart); |
---|
| 150 | tempCols=atoi(const_cast<char*>(valueString.c_str())); |
---|
| 151 | if(tempCols>*meshColumns) *meshColumns=tempCols; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | } |
---|
| 155 | inputFile.close(); |
---|
| 156 | } |
---|
| 157 | else //File couldn't be loaded |
---|
| 158 | { |
---|
| 159 | OSG_ALWAYS<< "Unable to open " << filePath << std::endl; |
---|
| 160 | return 1; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | *meshColumns+=1; |
---|
| 164 | *meshRows+=1; |
---|
| 165 | |
---|
| 166 | return 0; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | int DistortionSetupStrategyProjectSyntropy::readMeshPointsFromCSVFile(std::string filePath, osg::Vec2Array* meshPointsVec2, osg::Vec3Array* meshPointsVec3, int displayWidth, int displayHeight) |
---|
| 170 | { |
---|
| 171 | |
---|
| 172 | int numValues=6; |
---|
| 173 | std::string line, valueString; |
---|
| 174 | char separator=';'; |
---|
| 175 | float* values = new float[numValues]; |
---|
| 176 | size_t valueStart, valueEnd; |
---|
| 177 | osg::Vec2 tempVec2; |
---|
| 178 | osg::Vec3 tempVec3; |
---|
| 179 | |
---|
| 180 | osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str())); |
---|
| 181 | |
---|
| 182 | if (inputFile.is_open()) |
---|
| 183 | { |
---|
| 184 | inputFile.clear(); |
---|
| 185 | inputFile.seekg (0, std::ios::beg); |
---|
| 186 | |
---|
| 187 | while (!inputFile.eof()) |
---|
| 188 | { |
---|
| 189 | getline (inputFile,line); |
---|
| 190 | |
---|
| 191 | if((line.size()>0) && (line.find('x')==std::string::npos)) |
---|
| 192 | { |
---|
| 193 | for (int i=0;i<numValues; i++) values[i]=0.0; |
---|
| 194 | |
---|
| 195 | valueStart=0; |
---|
| 196 | valueEnd=0; |
---|
| 197 | for (int j=0;j<numValues; j++) |
---|
| 198 | { |
---|
| 199 | if(j!=numValues-1) |
---|
| 200 | { |
---|
| 201 | if((valueEnd=line.find(separator,valueStart))!=std::string::npos) |
---|
| 202 | { |
---|
| 203 | valueString=line.substr(valueStart,valueEnd-valueStart); |
---|
| 204 | valueStart=valueEnd+1; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | else valueString=line.substr(valueStart,line.size()); |
---|
| 208 | |
---|
| 209 | values[j]=atof(const_cast<char*>(valueString.c_str())); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | // Insertion of Values U and V (corresponding to values[2],values[3] in a temporary Array |
---|
[377] | 213 | // which is returned and merged into distortionSet later |
---|
| 214 | |
---|
| 215 | if((_distortionType==TEXCOORDDISTORTION) || (_distortionType==COMBINEDDISTORTION)) |
---|
[376] | 216 | { |
---|
| 217 | tempVec2.set(values[2],values[3]); |
---|
| 218 | meshPointsVec2->push_back(tempVec2); |
---|
| 219 | } |
---|
[377] | 220 | if((_distortionType==VERTEXDISTORTION) || (_distortionType==COMBINEDDISTORTION)) |
---|
[376] | 221 | { |
---|
| 222 | tempVec3.set(values[2]*displayWidth,values[3]*displayHeight,0.0); |
---|
| 223 | meshPointsVec3->push_back(tempVec3); |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | } |
---|
| 228 | inputFile.close(); |
---|
| 229 | } |
---|
| 230 | else |
---|
| 231 | { |
---|
| 232 | OSG_ALWAYS<< "Unable to open " << filePath << std::endl; |
---|
| 233 | return 1; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | return 0; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | |
---|
| 243 | |
---|
[372] | 244 | void DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup(osgViewer::DistortionSet* distortionSet) |
---|
| 245 | { |
---|
| 246 | if(distortionSet == NULL) |
---|
| 247 | { |
---|
| 248 | OSG_ALWAYS<<"DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup : Invalid DistortionSet"<<std::endl; |
---|
| 249 | return; |
---|
| 250 | } |
---|
| 251 | |
---|
[377] | 252 | if( _distortionType == DistortionSetupStrategy::UNDEFINED || _blendmapFilename.empty() || _frustumFilename.empty() || _distortionFilename.empty() ) |
---|
| 253 | { |
---|
| 254 | OSG_ALWAYS<<"DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup : You have not specified the imput filenames and distortion type!"<<std::endl; |
---|
| 255 | return; |
---|
| 256 | } |
---|
| 257 | |
---|
[376] | 258 | //OSG_ALWAYS<<"drin"<<std::endl; |
---|
[372] | 259 | |
---|
[377] | 260 | if (_distortionInitialized==false ) |
---|
[376] | 261 | { |
---|
| 262 | |
---|
| 263 | // ******************** // |
---|
| 264 | // *** IntensityMap *** // |
---|
| 265 | // ******************** // |
---|
| 266 | |
---|
| 267 | osg::Image* intensityMap=NULL; |
---|
[377] | 268 | intensityMap=osgDB::readImageFile(_blendmapFilename); |
---|
[376] | 269 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
---|
| 270 | if (!wsi) |
---|
| 271 | { |
---|
| 272 | OSG_NOTICE<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
---|
| 273 | return; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | osg::GraphicsContext::ScreenIdentifier si; |
---|
| 277 | si.readDISPLAY(); |
---|
| 278 | |
---|
| 279 | if (si.displayNum<0) si.displayNum = 0; |
---|
| 280 | |
---|
| 281 | si.screenNum = 0; //Fixed value, may be replaced later |
---|
| 282 | |
---|
| 283 | unsigned int width=0, height=0; |
---|
| 284 | wsi->getScreenResolution(si, width, height); |
---|
| 285 | |
---|
| 286 | if(intensityMap->s()!=width || intensityMap->t()!=height) |
---|
| 287 | intensityMap->scaleImage(width, height, intensityMap->r()); |
---|
| 288 | |
---|
| 289 | distortionSet->setIntensityMap(intensityMap); |
---|
| 290 | |
---|
| 291 | // ******************************* // |
---|
| 292 | // *** Frustum and View Offset *** // |
---|
| 293 | // ******************************* // |
---|
| 294 | |
---|
| 295 | //....Read Files and set parameters |
---|
| 296 | int numFrustumValues=16; |
---|
| 297 | float* frustumValues = new float[numFrustumValues]; |
---|
| 298 | double zNear=0.1, zFar=100.0; |
---|
| 299 | |
---|
[377] | 300 | readFrustumFromCSVFile(_frustumFilename, numFrustumValues, frustumValues); |
---|
| 301 | |
---|
[376] | 302 | osg::Matrixd viewOffset=osg::Matrixd(); |
---|
| 303 | osg::Matrixd projectionOffset=osg::Matrixd(); |
---|
| 304 | |
---|
| 305 | // View Offset Parameters: x, y, z, heading, pitch, bank |
---|
| 306 | // Translational Parameters are defines in millimeters (->Correction required?!) |
---|
| 307 | // Coordinate System / Directions may be looked up in "Koordinatensystem.png", trans/rot order confirmed bei PS |
---|
| 308 | viewOffset = osg::Matrixd::translate(frustumValues[0],frustumValues[1],frustumValues[2]); |
---|
| 309 | viewOffset*= osg::Matrixd::rotate(osg::inDegrees(frustumValues[3]), 0.0,0.0,1.0); //Heading |
---|
| 310 | viewOffset*= osg::Matrixd::rotate(osg::inDegrees(frustumValues[4]), 1.0,0.0,0.0); //Pitch |
---|
| 311 | viewOffset*= osg::Matrixd::rotate(osg::inDegrees(frustumValues[5]), 0.0,1.0,0.0); //Bank |
---|
| 312 | |
---|
| 313 | //Frustum Parameters: Left, Right, Bottom, Top, zNear, zFar |
---|
[377] | 314 | //Alternative: viewer->camera->setprojectionmatrixasfrustum? |
---|
[376] | 315 | projectionOffset.makeFrustum(osg::inDegrees(frustumValues[6]), osg::inDegrees(frustumValues[7]), osg::inDegrees(frustumValues[8]), osg::inDegrees(frustumValues[9]), zNear, zFar); |
---|
| 316 | |
---|
| 317 | distortionSet->setViewOffset(viewOffset); |
---|
| 318 | distortionSet->setProjectionOffset(projectionOffset); |
---|
| 319 | |
---|
| 320 | // ******************************* // |
---|
| 321 | // ******* Distortion Mesh ******* // |
---|
| 322 | // ******************************* // |
---|
| 323 | |
---|
| 324 | osg::Geometry* distortionMeshGeometry = distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry(); |
---|
| 325 | if(distortionMeshGeometry) |
---|
| 326 | { |
---|
| 327 | //*******************************************************************************************// |
---|
| 328 | //*** PRINT VERTEXARRAY/TEXCOORDARRAY for Debug Purposes ***// |
---|
| 329 | //*** both Arrays are listed in "HorizontalLine-after-HorizontalLine" order, ***// |
---|
| 330 | //*** sorted from topleft (0,0,0 vertices; 0,0 texcoords) to bottomright ***// |
---|
| 331 | //*** (1920,1080,0 vertices; 1,1 texcoords) ***// |
---|
| 332 | //*******************************************************************************************// |
---|
| 333 | |
---|
| 334 | if(0) |
---|
| 335 | { |
---|
| 336 | osg::Vec3Array* vertices = (osg::Vec3Array*) distortionMeshGeometry->getVertexArray(); |
---|
| 337 | osg::Vec2Array* texcoords0 = (osg::Vec2Array*) distortionMeshGeometry->getTexCoordArray(0); |
---|
| 338 | |
---|
| 339 | for (osg::Vec3Array::iterator it = vertices->begin(); it != vertices->end(); ++it) |
---|
| 340 | { |
---|
| 341 | OSG_ALWAYS<<"Vertice "<<(*it).x()<<" "<<(*it).y()<<" "<<(*it).z()<<std::endl; |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | for (osg::Vec2Array::iterator it = texcoords0->begin(); it != texcoords0->end(); ++it) |
---|
| 345 | { |
---|
| 346 | OSG_ALWAYS<<"Texcoord "<<(*it).x()<<" "<<(*it).y()<<std::endl; |
---|
| 347 | } |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | //*******************************************************************************************// |
---|
| 351 | //*** MESH WARPING ***// |
---|
| 352 | //*** Both Vertex Warping and Texcoord Warping is supported. Although there might ***// |
---|
| 353 | //*** be a preferred version for osgvisual, both options should be implemented in osg ***// |
---|
| 354 | //*******************************************************************************************// |
---|
| 355 | |
---|
| 356 | int distortionMeshRows=0, distortionMeshColumns=0; |
---|
| 357 | |
---|
[377] | 358 | readMeshDimensionsFromCSVFile(_distortionFilename, &distortionMeshRows, &distortionMeshColumns); |
---|
| 359 | |
---|
[376] | 360 | distortionSet->setDistortionMeshRows(distortionMeshRows); |
---|
| 361 | distortionSet->setDistortionMeshColumns(distortionMeshColumns); |
---|
| 362 | |
---|
| 363 | osg::Vec2Array* distortionMeshVec2 = new osg::Vec2Array; |
---|
| 364 | osg::Vec3Array* distortionMeshVec3 = new osg::Vec3Array; |
---|
| 365 | |
---|
[377] | 366 | if((_distortionType==TEXCOORDDISTORTION) || (_distortionType==COMBINEDDISTORTION)) |
---|
[376] | 367 | { |
---|
[377] | 368 | readMeshPointsFromCSVFile(_distortionFilename, distortionMeshVec2, distortionMeshVec3, width, height); |
---|
[376] | 369 | distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry()->setTexCoordArray(0,distortionMeshVec2); |
---|
| 370 | } |
---|
[377] | 371 | |
---|
| 372 | if((_distortionType==VERTEXDISTORTION) || (_distortionType==COMBINEDDISTORTION)) |
---|
[376] | 373 | { |
---|
[377] | 374 | readMeshPointsFromCSVFile(_distortionFilename, distortionMeshVec2, distortionMeshVec3, width, height); |
---|
[376] | 375 | distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry()->setVertexArray(distortionMeshVec3); |
---|
| 376 | |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | _distortionInitialized=true; |
---|
| 382 | |
---|
| 383 | |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | } |
---|