/* osgVisual test. distortionNG, experimental. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include #include #include #include #include "DistortionSetupStrategyProjectSyntropy.h" DistortionSetupStrategyProjectSyntropy::DistortionSetupStrategyProjectSyntropy() { _blendmapFilename = ""; _frustumFilename = ""; _vertexDistortionFilename = ""; _texCoordDistortionFilename = ""; _distortionType = DistortionSetupStrategy::UNDEFINED; _distortionInitialized=false; } DistortionSetupStrategyProjectSyntropy::~DistortionSetupStrategyProjectSyntropy() { } void DistortionSetupStrategyProjectSyntropy::setDistortionInputFiles( std::string vertexDistortionFile, std::string texCoordDistortionFile, std::string blendmapFile, std::string frustumFile, DistortionSetupStrategy::distortionType type) { _distortionType = type; _blendmapFilename = blendmapFile; _frustumFilename = frustumFile; _vertexDistortionFilename = vertexDistortionFile; _texCoordDistortionFilename = texCoordDistortionFile; } int DistortionSetupStrategyProjectSyntropy::readFrustumFromCSVFile(std::string filePath, int numValues, float* frustumValues) { int numSeparator=0; std::string line, valueString; char separator=';'; size_t valueStart, valueEnd; osgDB::ifstream inputFile (const_cast(filePath.c_str())); for (int i=0;i0) && (line.find('x')==std::string::npos)) {new osg::Vec2Array; numSeparator=0; for (size_t numLineChar=0; numLineChar < line.length(); numLineChar++) { if(line.at(numLineChar)==separator) numSeparator++; } if(numSeparator!=numValues-1) //Number of Entries per Line is not correct { OSG_ALWAYS<< "Inconsistency in file " << filePath << " detected. Please check this file." <(valueString.c_str())); } } } inputFile.close(); } else //File couldn't be loaded { OSG_ALWAYS<< "Unable to open " << filePath << std::endl; return 1; } return 0; } int DistortionSetupStrategyProjectSyntropy::readMeshDimensionsFromCSVFile(std::string filePath, int* meshRows, int* meshColumns) { int tempRows=0, tempCols=0, numSeparator=0, numValues=6; std::string line, valueString; char separator=';'; size_t valueStart, valueEnd; osgDB::ifstream inputFile (const_cast(filePath.c_str())); if (inputFile.is_open()) { inputFile.clear(); inputFile.seekg (0, std::ios::beg); while (!inputFile.eof()) { getline (inputFile,line); if((line.size()>0) && (line.find('x')==std::string::npos)) { numSeparator=0; for (size_t numLineChar=0; numLineChar < line.length(); numLineChar++) { if(line.at(numLineChar)==separator) numSeparator++; } if(numSeparator!=numValues-1) //Number of Entries per Line is not correct { OSG_ALWAYS<< "Inconsistency in file " << filePath << " detected. Please check this file." <(valueString.c_str())); if(tempRows>*meshRows) *meshRows=tempRows; //Get number of columns of the distortion grid, last valid line of input file contains actual value valueEnd=valueStart-1; valueStart=line.rfind(separator,valueEnd); valueString=line.substr(valueStart+1,valueEnd-valueStart); tempCols=atoi(const_cast(valueString.c_str())); if(tempCols>*meshColumns) *meshColumns=tempCols; } } inputFile.close(); } else //File couldn't be loaded { OSG_ALWAYS<< "Unable to open " << filePath << std::endl; return 1; } *meshColumns+=1; *meshRows+=1; return 0; } int DistortionSetupStrategyProjectSyntropy::readMeshPointsFromCSVFile(std::string filePath, osg::Vec2Array* tmpMesh) { int numValues=6; std::string line, valueString; char separator=';'; float* values = new float[numValues]; size_t valueStart, valueEnd; osg::Vec2 tempVec2; osg::Vec3 tempVec3; osgDB::ifstream inputFile (const_cast(filePath.c_str())); if (inputFile.is_open()) { inputFile.clear(); inputFile.seekg (0, std::ios::beg); while (!inputFile.eof()) { getline (inputFile,line); if((line.size()>0) && (line.find('x')==std::string::npos)) { for (int i=0;i(valueString.c_str())); } // Store values into array tmpMesh->push_back(osg::Vec2(values[0], values[1])); } } inputFile.close(); } else { OSG_ALWAYS<< "Unable to open " << filePath << std::endl; return 1; } return 0; } void DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup(osgViewer::DistortionSet* distortionSet) { if(distortionSet == NULL) { OSG_ALWAYS<<"DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup : Invalid DistortionSet"<dirtyMesh(); distortionSet->dirtyMatrix(); // ******************** // // *** IntensityMap *** // // ******************** // osg::Image* intensityMap=NULL; intensityMap=osgDB::readImageFile(_blendmapFilename); osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); if (!wsi) { OSG_NOTICE<<"Error, no WindowSystemInterface available, cannot create windows."<getScreenResolution(si, width, height); if(intensityMap->s()!=width || intensityMap->t()!=height) intensityMap->scaleImage(width, height, intensityMap->r()); // Copy image data and flag as dirty to update texture. osg::copyImage(intensityMap, 0, 0, 0, intensityMap->s(), intensityMap->t(), intensityMap->r(), distortionSet->getIntensityMap(), 0, 0, 0, true); distortionSet->getIntensityMap()->dirty(); // ******************************* // // *** Frustum and View Offset *** // // ******************************* // //....Read Files and set parameters int numFrustumValues=16; float* frustumValues = new float[numFrustumValues]; double zNear=0.1, zFar=100.0; readFrustumFromCSVFile(_frustumFilename, numFrustumValues, frustumValues); osg::Matrixd viewOffset=osg::Matrix::identity(); osg::Matrixd projectionOffset=osg::Matrixd(); // View Offset Parameters: x, y, z, heading, pitch, bank // Translational Parameters are defines in millimeters (->Correction required?!) // Coordinate System / Directions may be looked up in "Koordinatensystem.png", trans/rot order confirmed bei PS osg::Matrixd rotViewOffset=osg::Matrixd(); rotViewOffset.makeRotate( osg::DegreesToRadians(frustumValues[3]), osg::Vec3(0,1,0), // heading osg::DegreesToRadians(-frustumValues[4]), osg::Vec3(1,0,0), // pitch osg::DegreesToRadians(frustumValues[5]), osg::Vec3(0,0,1)); // roll osg::Matrixd transViewOffset=osg::Matrixd(); transViewOffset.makeTranslate( frustumValues[0], frustumValues[1], frustumValues[2]); viewOffset = viewOffset * rotViewOffset; // * transViewOffset; //Frustum Parameters: Left, Right, Bottom, Top, zNear, zFar projectionOffset.makeFrustum(osg::inDegrees(frustumValues[6]), osg::inDegrees(frustumValues[7]), osg::inDegrees(frustumValues[8]), osg::inDegrees(frustumValues[9]), zNear, zFar); distortionSet->setViewOffset(viewOffset); distortionSet->setProjectionOffset(projectionOffset); // ******************************* // // ******* Distortion Mesh ******* // // ******************************* // osg::Geometry* distortionMeshGeometry = distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry(); if(distortionMeshGeometry) { //*******************************************************************************************// //*** PRINT VERTEXARRAY/TEXCOORDARRAY for Debug Purposes ***// //*** both Arrays are listed in "HorizontalLine-after-HorizontalLine" order, ***// //*** sorted from topleft (0,0,0 vertices; 0,0 texcoords) to bottomright ***// //*** (1920,1080,0 vertices; 1,1 texcoords) ***// //*******************************************************************************************// #if 0 osg::Vec3Array* vertices = (osg::Vec3Array*) distortionMeshGeometry->getVertexArray(); osg::Vec2Array* texcoords0 = (osg::Vec2Array*) distortionMeshGeometry->getTexCoordArray(0); for (osg::Vec3Array::iterator it = vertices->begin(); it != vertices->end(); ++it) { OSG_ALWAYS<<"Vertice "<<(*it).x()<<" "<<(*it).y()<<" "<<(*it).z()<begin(); it != texcoords0->end(); ++it) { OSG_ALWAYS<<"Texcoord "<<(*it).x()<<" "<<(*it).y()<setDistortionMeshRows(distortionMeshRows); distortionSet->setDistortionMeshColumns(distortionMeshColumns); // If vertex- and texCoord distortion is combined: Ensure both files have the same dimensions if(_distortionType==DistortionSetupStrategy::COMBINEDDISTORTION) { int texCoordRows=0, texCoordColumns=0; readMeshDimensionsFromCSVFile(_texCoordDistortionFilename, &distortionMeshRows, &distortionMeshColumns); if(distortionMeshRows!=texCoordRows || distortionMeshColumns!=texCoordColumns) { OSG_ALWAYS<<"The mesh dimensions of the vertex- and textureCoordinate-file differ!"<at(row*distortionMeshColumns + col).x(); y = vertexMeshVec2->at(row*distortionMeshColumns + col).y(); } if(_distortionType==TEXCOORDDISTORTION || _distortionType==COMBINEDDISTORTION) { z = texCoordMeshVec2->at(row*distortionMeshColumns + col).x(); w = texCoordMeshVec2->at(row*distortionMeshColumns + col).y(); } distortionMesh->at(row*distortionMeshColumns+col).set(x,y,z,w); } } #if 1 for (osg::Vec4Array::iterator it = distortionMesh->begin(); it != distortionMesh->end(); ++it) { OSG_ALWAYS<<"final vector: "<<(*it).x()<<" "<<(*it).y()<<" "<<(*it).z()<<" "<<(*it).w()<setDistortionMesh(distortionMesh); //distortionSet->setDistortionMeshColumns(distortionMeshColumns); //distortionSet->setDistortionMeshRows(distortionMeshRows); distortionSet->setDistortionMeshDimensions(distortionMeshRows, distortionMeshColumns); } } }