source: experimental/distortionNG/DistortionSetupStrategyProjectSyntropy.cpp @ 382

Last change on this file since 382 was 382, checked in by Torben Dannhauer, 12 years ago
File size: 13.0 KB
Line 
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
19#include <osg/Vec3d>
20#include <osg/ImageUtils>
21#include <osgDB/ReadFile>
22#include <osgDB/fstream>
23
24#include "DistortionSetupStrategyProjectSyntropy.h"
25
26DistortionSetupStrategyProjectSyntropy::DistortionSetupStrategyProjectSyntropy()
27{
28        _blendmapFilename = "";
29        _frustumFilename = "";
30        _distortionFilename = "";
31        _distortionType = DistortionSetupStrategy::UNDEFINED;
32
33        _distortionInitialized=false;
34}
35
36DistortionSetupStrategyProjectSyntropy::~DistortionSetupStrategyProjectSyntropy()
37{
38
39}
40
41void DistortionSetupStrategyProjectSyntropy::setDistortionInputFiles( std::string distortionFile, std::string blendmapFile, std::string frustumFile, DistortionSetupStrategy::distortionType type)
42{
43        _distortionType = type;
44        _blendmapFilename = blendmapFile;
45        _frustumFilename = frustumFile;
46        _distortionFilename = distortionFile;
47}
48
49int DistortionSetupStrategyProjectSyntropy::readFrustumFromCSVFile(std::string filePath, int numValues, float* frustumValues)
50{
51        int numSeparator=0;
52        std::string line, valueString;
53        char separator=';';
54        size_t valueStart, valueEnd;
55
56        osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str()));
57
58        for (int i=0;i<numValues; i++) frustumValues[i]=0.0;
59
60        if (inputFile.is_open())
61        {
62                inputFile.clear();
63                inputFile.seekg (0, std::ios::beg);
64
65                while (!inputFile.eof())
66                {
67                        getline (inputFile,line);
68
69                        if((line.size()>0) && (line.find('x')==std::string::npos))
70                        {new osg::Vec2Array;
71                                numSeparator=0;
72                                for (size_t numLineChar=0; numLineChar < line.length(); numLineChar++)
73                                {
74                                        if(line.at(numLineChar)==separator) numSeparator++;
75                                }
76                                if(numSeparator!=numValues-1) //Number of Entries per Line is not correct
77                                {
78                                        OSG_ALWAYS<< "Inconsistency in file " << filePath << " detected. Please check this file." <<std::endl;
79                                        return 1;
80                                }
81                                valueStart=0;
82                                valueEnd=0;
83                                for (int j=0;j<numValues; j++)
84                                {
85                                        if(j!=numValues-1)
86                                        {
87                                                if((valueEnd=line.find(separator,valueStart))!=std::string::npos)
88                                                {
89                                                        valueString=line.substr(valueStart,valueEnd-valueStart);
90                                                        valueStart=valueEnd+1;
91                                                }
92                                        }
93                                        else valueString=line.substr(valueStart,line.size());
94
95                                        frustumValues[j]=atof(const_cast<char*>(valueString.c_str()));
96                                }
97                        }
98                }
99                inputFile.close();
100        }
101        else    //File couldn't be loaded
102        {
103                OSG_ALWAYS<< "Unable to open " << filePath << std::endl;
104                return 1;
105        }
106        return 0;
107}
108
109int DistortionSetupStrategyProjectSyntropy::readMeshDimensionsFromCSVFile(std::string filePath, int* meshRows, int* meshColumns)
110{
111        int tempRows=0, tempCols=0, numSeparator=0, numValues=6;
112        std::string line, valueString;
113        char separator=';';
114        size_t valueStart, valueEnd;
115
116        osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str()));
117
118        if (inputFile.is_open())
119        {
120                inputFile.clear();
121                inputFile.seekg (0, std::ios::beg);
122
123                while (!inputFile.eof())
124                {
125                        getline (inputFile,line);
126
127                        if((line.size()>0) && (line.find('x')==std::string::npos))
128                        {
129                                numSeparator=0;
130                                for (size_t numLineChar=0; numLineChar < line.length(); numLineChar++)
131                                {
132                                        if(line.at(numLineChar)==separator) numSeparator++;
133                                }
134                                if(numSeparator!=numValues-1) //Number of Entries per Line is not correct
135                                {
136                                        OSG_ALWAYS<< "Inconsistency in file " << filePath << " detected. Please check this file." <<std::endl;
137                                        return 1;
138                                }
139                                //Get number of rows of the distortion grid, last valid line of input file contains actual value
140                                valueStart=line.rfind(separator,line.size());
141                                valueString=line.substr(valueStart+1,line.size()-valueStart-1);
142                                tempRows=atoi(const_cast<char*>(valueString.c_str()));
143                                if(tempRows>*meshRows) *meshRows=tempRows;
144
145                                //Get number of columns of the distortion grid, last valid line of input file contains actual value
146                                valueEnd=valueStart-1;
147                                valueStart=line.rfind(separator,valueEnd);
148                                valueString=line.substr(valueStart+1,valueEnd-valueStart);
149                                tempCols=atoi(const_cast<char*>(valueString.c_str()));
150                                if(tempCols>*meshColumns) *meshColumns=tempCols;
151                        }
152
153                }
154                inputFile.close();
155        }
156        else    //File couldn't be loaded
157        {
158                OSG_ALWAYS<< "Unable to open " << filePath << std::endl;
159                return 1;
160        }
161
162        *meshColumns+=1;
163        *meshRows+=1;
164
165        return 0;
166}
167
168int DistortionSetupStrategyProjectSyntropy::readMeshPointsFromCSVFile(std::string filePath, osg::Vec2Array* tmpMesh)
169{
170        int numValues=6;
171        std::string line, valueString;
172        char separator=';';
173        float* values = new float[numValues]; 
174        size_t valueStart, valueEnd;
175        osg::Vec2 tempVec2;
176        osg::Vec3 tempVec3;
177
178        osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str()));
179
180        if (inputFile.is_open())
181        {
182                inputFile.clear();
183                inputFile.seekg (0, std::ios::beg);
184
185                while (!inputFile.eof())
186                {
187                        getline (inputFile,line);
188
189                        if((line.size()>0) && (line.find('x')==std::string::npos))
190                        {
191                                for (int i=0;i<numValues; i++) values[i]=0.0;
192
193                                valueStart=0;
194                                valueEnd=0;
195                                for (int j=0;j<numValues; j++)
196                                {
197                                        if(j!=numValues-1)
198                                        {
199                                                if((valueEnd=line.find(separator,valueStart))!=std::string::npos)
200                                                {
201                                                        valueString=line.substr(valueStart,valueEnd-valueStart);
202                                                        valueStart=valueEnd+1;
203                                                }
204                                        }
205                                        else valueString=line.substr(valueStart,line.size());
206
207                                        values[j]=atof(const_cast<char*>(valueString.c_str()));
208                                }
209
210                                // Store values into array
211                                tmpMesh->push_back(osg::Vec2(values[2], values[3]));
212                        }
213                }
214                inputFile.close();
215        }
216        else
217        {
218                OSG_ALWAYS<< "Unable to open " << filePath << std::endl;
219                return 1;
220        }
221
222        return 0;
223}
224
225void DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup(osgViewer::DistortionSet* distortionSet)
226{
227        if(distortionSet == NULL)
228        {
229                OSG_ALWAYS<<"DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup : Invalid DistortionSet"<<std::endl;
230                return;
231        }
232
233        if(     _distortionType == DistortionSetupStrategy::UNDEFINED || _blendmapFilename.empty() || _frustumFilename.empty() || _distortionFilename.empty() )
234        {
235                OSG_ALWAYS<<"DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup : You have not specified the imput filenames and distortion type!"<<std::endl;
236                return;
237        }
238
239        //OSG_ALWAYS<<"drin"<<std::endl;
240
241        if (_distortionInitialized==false )
242        {
243                _distortionInitialized=true;
244                distortionSet->dirty();
245
246                // ******************** //
247                // *** IntensityMap *** //
248                // ******************** //
249
250                osg::Image* intensityMap=NULL;
251                intensityMap=osgDB::readImageFile(_blendmapFilename);
252                osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
253                if (!wsi)
254                {
255                        OSG_NOTICE<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
256                        return;
257                }
258
259                osg::GraphicsContext::ScreenIdentifier si;
260                si.readDISPLAY();
261
262                if (si.displayNum<0) si.displayNum = 0;
263
264                si.screenNum = 0; //Fixed value, may be replaced later
265
266                unsigned int width=0, height=0;
267                wsi->getScreenResolution(si, width, height);
268
269                if(intensityMap->s()!=width || intensityMap->t()!=height)
270                        intensityMap->scaleImage(width, height, intensityMap->r());
271
272                // Copy image data and flag as dirty to update texture.
273                osg::copyImage(intensityMap, 0, 0, 0, intensityMap->s(), intensityMap->t(), intensityMap->r(), distortionSet->getIntensityMap(), 0, 0, 0, true);
274                distortionSet->getIntensityMap()->dirty();
275
276                // ******************************* //
277                // *** Frustum and View Offset *** //
278                // ******************************* //
279
280                //....Read Files and set parameters
281                int numFrustumValues=16;
282                float* frustumValues = new float[numFrustumValues];
283                double zNear=0.1, zFar=100.0;
284
285                readFrustumFromCSVFile(_frustumFilename, numFrustumValues, frustumValues);
286               
287                osg::Matrixd viewOffset=osg::Matrixd();
288                osg::Matrixd projectionOffset=osg::Matrixd();
289
290                // View Offset Parameters: x, y, z, heading, pitch, bank
291                // Translational Parameters are defines in millimeters (->Correction required?!)
292                // Coordinate System / Directions may be looked up in "Koordinatensystem.png", trans/rot order confirmed bei PS
293                viewOffset = osg::Matrixd::translate(frustumValues[0],frustumValues[1],frustumValues[2]);
294                viewOffset*= osg::Matrixd::rotate(osg::inDegrees(frustumValues[3]), 0.0,0.0,1.0); //Heading
295                viewOffset*= osg::Matrixd::rotate(osg::inDegrees(frustumValues[4]), 1.0,0.0,0.0); //Pitch
296                viewOffset*= osg::Matrixd::rotate(osg::inDegrees(frustumValues[5]), 0.0,1.0,0.0); //Bank
297
298                //Frustum Parameters: Left, Right, Bottom, Top, zNear, zFar
299                //Alternative: viewer->camera->setprojectionmatrixasfrustum?
300                projectionOffset.makeFrustum(osg::inDegrees(frustumValues[6]), osg::inDegrees(frustumValues[7]), osg::inDegrees(frustumValues[8]), osg::inDegrees(frustumValues[9]), zNear, zFar);
301
302                distortionSet->setViewOffset(viewOffset);
303                distortionSet->setProjectionOffset(projectionOffset);
304
305                // ******************************* //
306                // ******* Distortion Mesh ******* //
307                // ******************************* //
308
309                osg::Geometry* distortionMeshGeometry = distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry();
310                if(distortionMeshGeometry)
311                {
312                        //*******************************************************************************************//
313                        //***    PRINT VERTEXARRAY/TEXCOORDARRAY for Debug Purposes                               ***//
314                        //***    both Arrays are listed in "HorizontalLine-after-HorizontalLine" order,           ***//
315                        //***    sorted from topleft (0,0,0 vertices; 0,0 texcoords) to bottomright               ***//
316                        //***    (1920,1080,0 vertices; 1,1 texcoords)                                                                                    ***//
317                        //*******************************************************************************************//
318                       
319#if 1
320                        osg::Vec3Array* vertices =  (osg::Vec3Array*) distortionMeshGeometry->getVertexArray();
321                        osg::Vec2Array* texcoords0 = (osg::Vec2Array*) distortionMeshGeometry->getTexCoordArray(0);
322
323                        for (osg::Vec3Array::iterator it = vertices->begin(); it != vertices->end(); ++it)
324                        {
325                                OSG_ALWAYS<<"Vertice "<<(*it).x()<<" "<<(*it).y()<<" "<<(*it).z()<<std::endl;
326                        }
327
328                        for (osg::Vec2Array::iterator it = texcoords0->begin(); it != texcoords0->end(); ++it)
329                        {
330                                OSG_ALWAYS<<"Texcoord "<<(*it).x()<<" "<<(*it).y()<<std::endl;
331                        }
332#endif
333
334                        //*******************************************************************************************//
335                        //***    MESH WARPING                                                                                                                                     ***//
336                        //***    Both Vertex Warping and Texcoord Warping is supported. Although there might      ***//
337                        //***    be a preferred version for osgvisual, both options should be implemented in osg  ***//
338                        //*******************************************************************************************//
339
340                        // Exctract mesh dimensions from file and pass them to the container
341                        int distortionMeshRows=0, distortionMeshColumns=0;
342                        readMeshDimensionsFromCSVFile(_distortionFilename, &distortionMeshRows, &distortionMeshColumns);
343
344                        distortionSet->setDistortionMeshRows(distortionMeshRows);
345                        distortionSet->setDistortionMeshColumns(distortionMeshColumns);
346
347                        osg::Vec2Array* tmpMeshVec2 = new osg::Vec2Array;
348                        osg::Vec3Array* vertexMeshVec3 = new osg::Vec3Array;
349
350                        readMeshPointsFromCSVFile(_distortionFilename, tmpMeshVec2);
351
352                        if((_distortionType==TEXCOORDDISTORTION) || (_distortionType==COMBINEDDISTORTION))
353                        {
354                                distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry()->setTexCoordArray(0,tmpMeshVec2);
355                        }
356
357                        if((_distortionType==VERTEXDISTORTION) || (_distortionType==COMBINEDDISTORTION))
358                        {
359                                for(unsigned int i=0;i<tmpMeshVec2->size();i++)
360                                {
361                                        // Scale vector with the screen resolution
362                                        osg::Vec3 tmpVec = osg::Vec3(   tmpMeshVec2->at(i).x()*width,
363                                                                                                        tmpMeshVec2->at(i).y()*height,
364                                                                                                        0.0     );
365                                        vertexMeshVec3->push_back( tmpVec );
366                                }
367                                distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry()->setVertexArray(vertexMeshVec3);
368
369                        }
370                }
371        }
372}
Note: See TracBrowser for help on using the repository browser.