source: experimental/distortionNG/DistortionSetupStrategyProjectSyntropy.cpp @ 376

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