source: experimental/distortionNG/DistortionSetupStrategyProjectSyntropy.cpp @ 399

Last change on this file since 399 was 399, checked in by Torben Dannhauer, 12 years ago

Frustum korrigiert: PS-Strategy now uses tan values.

File size: 12.6 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
32        _distortionInitialized=false;
33}
34
35DistortionSetupStrategyProjectSyntropy::~DistortionSetupStrategyProjectSyntropy()
36{
37
38}
39
40void DistortionSetupStrategyProjectSyntropy::setDistortionInputFiles(std::string distortionFile, std::string blendmapFile, std::string frustumFile)
41{
42        _blendmapFilename = blendmapFile;
43        _frustumFilename = frustumFile;
44        _distortionFilename = distortionFile;
45}
46
47int DistortionSetupStrategyProjectSyntropy::readFrustumFromCSVFile(std::string filePath, int numValues, float* frustumValues)
48{
49        int numSeparator=0;
50        std::string line, valueString;
51        char separator=';';
52        size_t valueStart, valueEnd;
53
54        osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str()));
55
56        for (int i=0;i<numValues; i++) frustumValues[i]=0.0;
57
58        if (inputFile.is_open())
59        {
60                inputFile.clear();
61                inputFile.seekg (0, std::ios::beg);
62
63                while (!inputFile.eof())
64                {
65                        getline (inputFile,line);
66
67                        if((line.size()>0) && (line.find('x')==std::string::npos))
68                        {new osg::Vec2Array;
69                                numSeparator=0;
70                                for (size_t numLineChar=0; numLineChar < line.length(); numLineChar++)
71                                {
72                                        if(line.at(numLineChar)==separator) numSeparator++;
73                                }
74                                if(numSeparator!=numValues-1) //Number of Entries per Line is not correct
75                                {
76                                        OSG_ALWAYS<< "Inconsistency in file " << filePath << " detected. Please check this file." <<std::endl;
77                                        return 1;
78                                }
79                                valueStart=0;
80                                valueEnd=0;
81                                for (int j=0;j<numValues; j++)
82                                {
83                                        if(j!=numValues-1)
84                                        {
85                                                if((valueEnd=line.find(separator,valueStart))!=std::string::npos)
86                                                {
87                                                        valueString=line.substr(valueStart,valueEnd-valueStart);
88                                                        valueStart=valueEnd+1;
89                                                }
90                                        }
91                                        else valueString=line.substr(valueStart,line.size());
92
93                                        frustumValues[j]=atof(const_cast<char*>(valueString.c_str()));
94                                }
95                        }
96                }
97                inputFile.close();
98        }
99        else    //File couldn't be loaded
100        {
101                OSG_ALWAYS<< "Unable to open " << filePath << std::endl;
102                return 1;
103        }
104        return 0;
105}
106
107int DistortionSetupStrategyProjectSyntropy::readMeshDimensionsFromCSVFile(std::string filePath, int* meshRows, int* meshColumns)
108{
109        int tempRows=0, tempCols=0, numSeparator=0, numValues=6;
110        std::string line, valueString;
111        char separator=';';
112        size_t valueStart, valueEnd;
113
114        osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str()));
115
116        if (inputFile.is_open())
117        {
118                inputFile.clear();
119                inputFile.seekg (0, std::ios::beg);
120
121                while (!inputFile.eof())
122                {
123                        getline (inputFile,line);
124
125                        if((line.size()>0) && (line.find('x')==std::string::npos))
126                        {
127                                numSeparator=0;
128                                for (size_t numLineChar=0; numLineChar < line.length(); numLineChar++)
129                                {
130                                        if(line.at(numLineChar)==separator) numSeparator++;
131                                }
132                                if(numSeparator!=numValues-1) //Number of Entries per Line is not correct
133                                {
134                                        OSG_ALWAYS<< "Inconsistency in file " << filePath << " detected. Please check this file." <<std::endl;
135                                        return 1;
136                                }
137                                //Get number of rows of the distortion grid, last valid line of input file contains actual value
138                                valueStart=line.rfind(separator,line.size());
139                                valueString=line.substr(valueStart+1,line.size()-valueStart-1);
140                                tempRows=atoi(const_cast<char*>(valueString.c_str()));
141                                if(tempRows>*meshRows) *meshRows=tempRows;
142
143                                //Get number of columns of the distortion grid, last valid line of input file contains actual value
144                                valueEnd=valueStart-1;
145                                valueStart=line.rfind(separator,valueEnd);
146                                valueString=line.substr(valueStart+1,valueEnd-valueStart);
147                                tempCols=atoi(const_cast<char*>(valueString.c_str()));
148                                if(tempCols>*meshColumns) *meshColumns=tempCols;
149                        }
150
151                }
152                inputFile.close();
153        }
154        else    //File couldn't be loaded
155        {
156                OSG_ALWAYS<< "Unable to open " << filePath << std::endl;
157                return 1;
158        }
159
160        *meshColumns+=1;
161        *meshRows+=1;
162
163        return 0;
164}
165
166int DistortionSetupStrategyProjectSyntropy::readMeshPointsFromCSVFile(std::string filePath, osg::Vec4Array* tmpMesh)
167{
168        int numValues=6;
169        std::string line, valueString;
170        char separator=';';
171        float* values = new float[numValues]; 
172        size_t valueStart, valueEnd;
173        osg::Vec2 tempVec2;
174        osg::Vec3 tempVec3;
175
176        osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str()));
177
178        if (inputFile.is_open())
179        {
180                inputFile.clear();
181                inputFile.seekg (0, std::ios::beg);
182
183                while (!inputFile.eof())
184                {
185                        getline (inputFile,line);
186
187                        if((line.size()>0) && (line.find('x')==std::string::npos))
188                        {
189                                for (int i=0;i<numValues; i++) values[i]=0.0;
190
191                                valueStart=0;
192                                valueEnd=0;
193                                for (int j=0;j<numValues; j++)
194                                {
195                                        if(j!=numValues-1)
196                                        {
197                                                if((valueEnd=line.find(separator,valueStart))!=std::string::npos)
198                                                {
199                                                        valueString=line.substr(valueStart,valueEnd-valueStart);
200                                                        valueStart=valueEnd+1;
201                                                }
202                                        }
203                                        else valueString=line.substr(valueStart,line.size());
204
205                                        values[j]=atof(const_cast<char*>(valueString.c_str()));
206                                }
207
208                                // Store values into array
209                                tmpMesh->push_back(osg::Vec4(values[0], values[1], values[2], values[3]));
210                        }
211                }
212                inputFile.close();
213        }
214        else
215        {
216                OSG_ALWAYS<< "Unable to open " << filePath << std::endl;
217                return 1;
218        }
219
220        return 0;
221}
222
223void DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup(osgViewer::DistortionSet* distortionSet)
224{
225        if(distortionSet == NULL)
226        {
227                OSG_ALWAYS<<"DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup : Invalid DistortionSet"<<std::endl;
228                return;
229        }
230
231        if(      _blendmapFilename.empty() || _frustumFilename.empty() ||  _distortionFilename.empty() )
232        {
233                OSG_ALWAYS<<"DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup : You have not specified the imput filenames correctly!"<<std::endl;
234                return;
235        }
236
237        //OSG_ALWAYS<<"drin"<<std::endl;
238
239        if (_distortionInitialized==false )
240        {
241                _distortionInitialized=true;
242                distortionSet->dirtyMesh();
243                distortionSet->dirtyMatrix();
244
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::Matrix::identity();
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                osg::Matrixd rotViewOffset=osg::Matrixd();
294                rotViewOffset.makeRotate(
295                        osg::DegreesToRadians(frustumValues[3]), osg::Vec3(0,1,0),      // heading
296                        osg::DegreesToRadians(-frustumValues[4]), osg::Vec3(1,0,0),     // pitch
297                        osg::DegreesToRadians(frustumValues[5]), osg::Vec3(0,0,1));     // roll
298
299                osg::Matrixd transViewOffset=osg::Matrixd();
300                transViewOffset.makeTranslate( frustumValues[0], frustumValues[1], frustumValues[2]);
301
302                viewOffset = viewOffset * rotViewOffset; // * transViewOffset;
303               
304                //Frustum Parameters: Left, Right, Bottom, Top, zNear, zFar.
305                // Important: By definition of OpenGL, Left, Right, Bottom and Top must be set as tangens!
306                projectionOffset.makeFrustum(osg::inDegrees(frustumValues[10]), osg::inDegrees(frustumValues[11]), osg::inDegrees(frustumValues[12]), osg::inDegrees(frustumValues[13]), zNear, zFar);
307
308                distortionSet->setViewOffset(viewOffset);
309                distortionSet->setProjectionOffset(projectionOffset);
310
311                // ******************************* //
312                // ******* Distortion Mesh ******* //
313                // ******************************* //
314
315                osg::Geometry* distortionMeshGeometry = distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry();
316                if(distortionMeshGeometry)
317                {
318                        //*******************************************************************************************//
319                        //***    PRINT VERTEXARRAY/TEXCOORDARRAY for Debug Purposes                               ***//
320                        //***    both Arrays are listed in "HorizontalLine-after-HorizontalLine" order,           ***//
321                        //***    sorted from topleft (0,0,0 vertices; 0,0 texcoords) to bottomright               ***//
322                        //***    (1920,1080,0 vertices; 1,1 texcoords)                                                                                    ***//
323                        //*******************************************************************************************//
324                       
325#if 0
326                        osg::Vec3Array* vertices =  (osg::Vec3Array*) distortionMeshGeometry->getVertexArray();
327                        osg::Vec2Array* texcoords0 = (osg::Vec2Array*) distortionMeshGeometry->getTexCoordArray(0);
328
329                        for (osg::Vec3Array::iterator it = vertices->begin(); it != vertices->end(); ++it)
330                        {
331                                OSG_ALWAYS<<"Vertice "<<(*it).x()<<" "<<(*it).y()<<" "<<(*it).z()<<std::endl;
332                        }
333
334                        for (osg::Vec2Array::iterator it = texcoords0->begin(); it != texcoords0->end(); ++it)
335                        {
336                                OSG_ALWAYS<<"Texcoord "<<(*it).x()<<" "<<(*it).y()<<std::endl;
337                        }
338#endif
339
340                        //*******************************************************************************************//
341                        //***    MESH WARPING                                                                                                                                     ***//
342                        //***    Both Vertex Warping and Texcoord Warping is supported.
343                        //***    The input file contains . Although there might      ***//
344                        //***    be a preferred version for osgvisual, both options should be implemented in osg  ***//
345                        //*******************************************************************************************//
346
347                        // Exctract mesh dimensions from file and pass them to the container.
348
349                        int distortionMeshRows=0, distortionMeshColumns=0;
350                        readMeshDimensionsFromCSVFile(_distortionFilename, &distortionMeshRows, &distortionMeshColumns);
351
352                        distortionSet->setDistortionMeshRows(distortionMeshRows);
353                        distortionSet->setDistortionMeshColumns(distortionMeshColumns);
354
355                        // Read in the mesh arrays
356                        osg::Vec4Array* distortionMesh = new osg::Vec4Array;
357                        readMeshPointsFromCSVFile(_distortionFilename, distortionMesh);
358
359#if 0
360                        for (osg::Vec4Array::iterator it = distortionMesh->begin(); it != distortionMesh->end(); ++it)
361                        {
362                                OSG_ALWAYS<<"final vector: "<<(*it).x()<<" "<<(*it).y()<<" "<<(*it).z()<<" "<<(*it).w()<<std::endl;
363                        }
364#endif
365
366
367                        distortionSet->setDistortionMesh(distortionMesh);
368                        distortionSet->setDistortionMeshDimensions(distortionMeshRows, distortionMeshColumns);
369                }
370        }
371}
Note: See TracBrowser for help on using the repository browser.