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 | |
---|
26 | DistortionSetupStrategyProjectSyntropy::DistortionSetupStrategyProjectSyntropy() |
---|
27 | { |
---|
28 | _blendmapFilename = ""; |
---|
29 | _frustumFilename = ""; |
---|
30 | _vertexDistortionFilename = ""; |
---|
31 | _texCoordDistortionFilename = ""; |
---|
32 | _distortionType = DistortionSetupStrategy::UNDEFINED; |
---|
33 | |
---|
34 | _distortionInitialized=false; |
---|
35 | } |
---|
36 | |
---|
37 | DistortionSetupStrategyProjectSyntropy::~DistortionSetupStrategyProjectSyntropy() |
---|
38 | { |
---|
39 | |
---|
40 | } |
---|
41 | |
---|
42 | void DistortionSetupStrategyProjectSyntropy::setDistortionInputFiles( std::string vertexDistortionFile, std::string texCoordDistortionFile, std::string blendmapFile, std::string frustumFile, DistortionSetupStrategy::distortionType type) |
---|
43 | { |
---|
44 | _distortionType = type; |
---|
45 | _blendmapFilename = blendmapFile; |
---|
46 | _frustumFilename = frustumFile; |
---|
47 | _vertexDistortionFilename = vertexDistortionFile; |
---|
48 | _texCoordDistortionFilename = texCoordDistortionFile; |
---|
49 | } |
---|
50 | |
---|
51 | int DistortionSetupStrategyProjectSyntropy::readFrustumFromCSVFile(std::string filePath, int numValues, float* frustumValues) |
---|
52 | { |
---|
53 | int numSeparator=0; |
---|
54 | std::string line, valueString; |
---|
55 | char separator=';'; |
---|
56 | size_t valueStart, valueEnd; |
---|
57 | |
---|
58 | osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str())); |
---|
59 | |
---|
60 | for (int i=0;i<numValues; i++) frustumValues[i]=0.0; |
---|
61 | |
---|
62 | if (inputFile.is_open()) |
---|
63 | { |
---|
64 | inputFile.clear(); |
---|
65 | inputFile.seekg (0, std::ios::beg); |
---|
66 | |
---|
67 | while (!inputFile.eof()) |
---|
68 | { |
---|
69 | getline (inputFile,line); |
---|
70 | |
---|
71 | if((line.size()>0) && (line.find('x')==std::string::npos)) |
---|
72 | {new osg::Vec2Array; |
---|
73 | numSeparator=0; |
---|
74 | for (size_t numLineChar=0; numLineChar < line.length(); numLineChar++) |
---|
75 | { |
---|
76 | if(line.at(numLineChar)==separator) numSeparator++; |
---|
77 | } |
---|
78 | if(numSeparator!=numValues-1) //Number of Entries per Line is not correct |
---|
79 | { |
---|
80 | OSG_ALWAYS<< "Inconsistency in file " << filePath << " detected. Please check this file." <<std::endl; |
---|
81 | return 1; |
---|
82 | } |
---|
83 | valueStart=0; |
---|
84 | valueEnd=0; |
---|
85 | for (int j=0;j<numValues; j++) |
---|
86 | { |
---|
87 | if(j!=numValues-1) |
---|
88 | { |
---|
89 | if((valueEnd=line.find(separator,valueStart))!=std::string::npos) |
---|
90 | { |
---|
91 | valueString=line.substr(valueStart,valueEnd-valueStart); |
---|
92 | valueStart=valueEnd+1; |
---|
93 | } |
---|
94 | } |
---|
95 | else valueString=line.substr(valueStart,line.size()); |
---|
96 | |
---|
97 | frustumValues[j]=atof(const_cast<char*>(valueString.c_str())); |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | inputFile.close(); |
---|
102 | } |
---|
103 | else //File couldn't be loaded |
---|
104 | { |
---|
105 | OSG_ALWAYS<< "Unable to open " << filePath << std::endl; |
---|
106 | return 1; |
---|
107 | } |
---|
108 | return 0; |
---|
109 | } |
---|
110 | |
---|
111 | int DistortionSetupStrategyProjectSyntropy::readMeshDimensionsFromCSVFile(std::string filePath, int* meshRows, int* meshColumns) |
---|
112 | { |
---|
113 | int tempRows=0, tempCols=0, numSeparator=0, numValues=6; |
---|
114 | std::string line, valueString; |
---|
115 | char separator=';'; |
---|
116 | size_t valueStart, valueEnd; |
---|
117 | |
---|
118 | osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str())); |
---|
119 | |
---|
120 | if (inputFile.is_open()) |
---|
121 | { |
---|
122 | inputFile.clear(); |
---|
123 | inputFile.seekg (0, std::ios::beg); |
---|
124 | |
---|
125 | while (!inputFile.eof()) |
---|
126 | { |
---|
127 | getline (inputFile,line); |
---|
128 | |
---|
129 | if((line.size()>0) && (line.find('x')==std::string::npos)) |
---|
130 | { |
---|
131 | numSeparator=0; |
---|
132 | for (size_t numLineChar=0; numLineChar < line.length(); numLineChar++) |
---|
133 | { |
---|
134 | if(line.at(numLineChar)==separator) numSeparator++; |
---|
135 | } |
---|
136 | if(numSeparator!=numValues-1) //Number of Entries per Line is not correct |
---|
137 | { |
---|
138 | OSG_ALWAYS<< "Inconsistency in file " << filePath << " detected. Please check this file." <<std::endl; |
---|
139 | return 1; |
---|
140 | } |
---|
141 | //Get number of rows of the distortion grid, last valid line of input file contains actual value |
---|
142 | valueStart=line.rfind(separator,line.size()); |
---|
143 | valueString=line.substr(valueStart+1,line.size()-valueStart-1); |
---|
144 | tempRows=atoi(const_cast<char*>(valueString.c_str())); |
---|
145 | if(tempRows>*meshRows) *meshRows=tempRows; |
---|
146 | |
---|
147 | //Get number of columns of the distortion grid, last valid line of input file contains actual value |
---|
148 | valueEnd=valueStart-1; |
---|
149 | valueStart=line.rfind(separator,valueEnd); |
---|
150 | valueString=line.substr(valueStart+1,valueEnd-valueStart); |
---|
151 | tempCols=atoi(const_cast<char*>(valueString.c_str())); |
---|
152 | if(tempCols>*meshColumns) *meshColumns=tempCols; |
---|
153 | } |
---|
154 | |
---|
155 | } |
---|
156 | inputFile.close(); |
---|
157 | } |
---|
158 | else //File couldn't be loaded |
---|
159 | { |
---|
160 | OSG_ALWAYS<< "Unable to open " << filePath << std::endl; |
---|
161 | return 1; |
---|
162 | } |
---|
163 | |
---|
164 | *meshColumns+=1; |
---|
165 | *meshRows+=1; |
---|
166 | |
---|
167 | return 0; |
---|
168 | } |
---|
169 | |
---|
170 | int DistortionSetupStrategyProjectSyntropy::readMeshPointsFromCSVFile(std::string filePath, osg::Vec2Array* tmpMesh) |
---|
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 | // Store values into array |
---|
213 | tmpMesh->push_back(osg::Vec2(values[0], values[1])); |
---|
214 | } |
---|
215 | } |
---|
216 | inputFile.close(); |
---|
217 | } |
---|
218 | else |
---|
219 | { |
---|
220 | OSG_ALWAYS<< "Unable to open " << filePath << std::endl; |
---|
221 | return 1; |
---|
222 | } |
---|
223 | |
---|
224 | return 0; |
---|
225 | } |
---|
226 | |
---|
227 | void DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup(osgViewer::DistortionSet* distortionSet) |
---|
228 | { |
---|
229 | if(distortionSet == NULL) |
---|
230 | { |
---|
231 | OSG_ALWAYS<<"DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup : Invalid DistortionSet"<<std::endl; |
---|
232 | return; |
---|
233 | } |
---|
234 | |
---|
235 | if( _distortionType == DistortionSetupStrategy::UNDEFINED |
---|
236 | || _blendmapFilename.empty() |
---|
237 | || _frustumFilename.empty() |
---|
238 | || ( _vertexDistortionFilename.empty() && _distortionType == DistortionSetupStrategy::VERTEXDISTORTION ) |
---|
239 | || ( _texCoordDistortionFilename.empty() && _distortionType == DistortionSetupStrategy::TEXCOORDDISTORTION ) |
---|
240 | ) |
---|
241 | { |
---|
242 | OSG_ALWAYS<<"DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup : You have not specified the imput filenames and distortion type correctly!"<<std::endl; |
---|
243 | return; |
---|
244 | } |
---|
245 | |
---|
246 | //OSG_ALWAYS<<"drin"<<std::endl; |
---|
247 | |
---|
248 | if (_distortionInitialized==false ) |
---|
249 | { |
---|
250 | _distortionInitialized=true; |
---|
251 | distortionSet->dirtyMesh(); |
---|
252 | distortionSet->dirtyMatrix(); |
---|
253 | |
---|
254 | |
---|
255 | // ******************** // |
---|
256 | // *** IntensityMap *** // |
---|
257 | // ******************** // |
---|
258 | |
---|
259 | osg::Image* intensityMap=NULL; |
---|
260 | intensityMap=osgDB::readImageFile(_blendmapFilename); |
---|
261 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
---|
262 | if (!wsi) |
---|
263 | { |
---|
264 | OSG_NOTICE<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
---|
265 | return; |
---|
266 | } |
---|
267 | |
---|
268 | osg::GraphicsContext::ScreenIdentifier si; |
---|
269 | si.readDISPLAY(); |
---|
270 | |
---|
271 | if (si.displayNum<0) si.displayNum = 0; |
---|
272 | |
---|
273 | si.screenNum = 0; //Fixed value, may be replaced later |
---|
274 | |
---|
275 | unsigned int width=0, height=0; |
---|
276 | wsi->getScreenResolution(si, width, height); |
---|
277 | |
---|
278 | if(intensityMap->s()!=width || intensityMap->t()!=height) |
---|
279 | intensityMap->scaleImage(width, height, intensityMap->r()); |
---|
280 | |
---|
281 | // Copy image data and flag as dirty to update texture. |
---|
282 | osg::copyImage(intensityMap, 0, 0, 0, intensityMap->s(), intensityMap->t(), intensityMap->r(), distortionSet->getIntensityMap(), 0, 0, 0, true); |
---|
283 | distortionSet->getIntensityMap()->dirty(); |
---|
284 | |
---|
285 | // ******************************* // |
---|
286 | // *** Frustum and View Offset *** // |
---|
287 | // ******************************* // |
---|
288 | |
---|
289 | //....Read Files and set parameters |
---|
290 | int numFrustumValues=16; |
---|
291 | float* frustumValues = new float[numFrustumValues]; |
---|
292 | double zNear=0.1, zFar=100.0; |
---|
293 | |
---|
294 | readFrustumFromCSVFile(_frustumFilename, numFrustumValues, frustumValues); |
---|
295 | |
---|
296 | osg::Matrixd viewOffset=osg::Matrix::identity(); |
---|
297 | osg::Matrixd projectionOffset=osg::Matrixd(); |
---|
298 | |
---|
299 | // View Offset Parameters: x, y, z, heading, pitch, bank |
---|
300 | // Translational Parameters are defines in millimeters (->Correction required?!) |
---|
301 | // Coordinate System / Directions may be looked up in "Koordinatensystem.png", trans/rot order confirmed bei PS |
---|
302 | osg::Matrixd rotViewOffset=osg::Matrixd(); |
---|
303 | rotViewOffset.makeRotate( |
---|
304 | osg::DegreesToRadians(frustumValues[3]), osg::Vec3(0,1,0), // heading |
---|
305 | osg::DegreesToRadians(-frustumValues[4]), osg::Vec3(1,0,0), // pitch |
---|
306 | osg::DegreesToRadians(frustumValues[5]), osg::Vec3(0,0,1)); // roll |
---|
307 | |
---|
308 | osg::Matrixd transViewOffset=osg::Matrixd(); |
---|
309 | transViewOffset.makeTranslate( frustumValues[0], frustumValues[1], frustumValues[2]); |
---|
310 | |
---|
311 | viewOffset = viewOffset * rotViewOffset; // * transViewOffset; |
---|
312 | |
---|
313 | //Frustum Parameters: Left, Right, Bottom, Top, zNear, zFar |
---|
314 | projectionOffset.makeFrustum(osg::inDegrees(frustumValues[6]), osg::inDegrees(frustumValues[7]), osg::inDegrees(frustumValues[8]), osg::inDegrees(frustumValues[9]), zNear, zFar); |
---|
315 | |
---|
316 | distortionSet->setViewOffset(viewOffset); |
---|
317 | distortionSet->setProjectionOffset(projectionOffset); |
---|
318 | |
---|
319 | // ******************************* // |
---|
320 | // ******* Distortion Mesh ******* // |
---|
321 | // ******************************* // |
---|
322 | |
---|
323 | osg::Geometry* distortionMeshGeometry = distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry(); |
---|
324 | if(distortionMeshGeometry) |
---|
325 | { |
---|
326 | //*******************************************************************************************// |
---|
327 | //*** PRINT VERTEXARRAY/TEXCOORDARRAY for Debug Purposes ***// |
---|
328 | //*** both Arrays are listed in "HorizontalLine-after-HorizontalLine" order, ***// |
---|
329 | //*** sorted from topleft (0,0,0 vertices; 0,0 texcoords) to bottomright ***// |
---|
330 | //*** (1920,1080,0 vertices; 1,1 texcoords) ***// |
---|
331 | //*******************************************************************************************// |
---|
332 | |
---|
333 | #if 0 |
---|
334 | osg::Vec3Array* vertices = (osg::Vec3Array*) distortionMeshGeometry->getVertexArray(); |
---|
335 | osg::Vec2Array* texcoords0 = (osg::Vec2Array*) distortionMeshGeometry->getTexCoordArray(0); |
---|
336 | |
---|
337 | for (osg::Vec3Array::iterator it = vertices->begin(); it != vertices->end(); ++it) |
---|
338 | { |
---|
339 | OSG_ALWAYS<<"Vertice "<<(*it).x()<<" "<<(*it).y()<<" "<<(*it).z()<<std::endl; |
---|
340 | } |
---|
341 | |
---|
342 | for (osg::Vec2Array::iterator it = texcoords0->begin(); it != texcoords0->end(); ++it) |
---|
343 | { |
---|
344 | OSG_ALWAYS<<"Texcoord "<<(*it).x()<<" "<<(*it).y()<<std::endl; |
---|
345 | } |
---|
346 | #endif |
---|
347 | |
---|
348 | //*******************************************************************************************// |
---|
349 | //*** MESH WARPING ***// |
---|
350 | //*** Both Vertex Warping and Texcoord Warping is supported. Although there might ***// |
---|
351 | //*** be a preferred version for osgvisual, both options should be implemented in osg ***// |
---|
352 | //*******************************************************************************************// |
---|
353 | |
---|
354 | // Exctract mesh dimensions from file and pass them to the container. |
---|
355 | // If soley texCoordDistortion is used, _texCoordDistortionFilename is used to determine the dimensions, otherwise _vertexDistortionFilename |
---|
356 | int distortionMeshRows=0, distortionMeshColumns=0; |
---|
357 | readMeshDimensionsFromCSVFile(_distortionType==DistortionSetupStrategy::TEXCOORDDISTORTION?_texCoordDistortionFilename:_vertexDistortionFilename, &distortionMeshRows, &distortionMeshColumns); |
---|
358 | |
---|
359 | distortionSet->setDistortionMeshRows(distortionMeshRows); |
---|
360 | distortionSet->setDistortionMeshColumns(distortionMeshColumns); |
---|
361 | |
---|
362 | // If vertex- and texCoord distortion is combined: Ensure both files have the same dimensions |
---|
363 | if(_distortionType==DistortionSetupStrategy::COMBINEDDISTORTION) |
---|
364 | { |
---|
365 | int texCoordRows=0, texCoordColumns=0; |
---|
366 | readMeshDimensionsFromCSVFile(_texCoordDistortionFilename, &distortionMeshRows, &distortionMeshColumns); |
---|
367 | if(distortionMeshRows!=texCoordRows || distortionMeshColumns!=texCoordColumns) |
---|
368 | { |
---|
369 | OSG_ALWAYS<<"The mesh dimensions of the vertex- and textureCoordinate-file differ!"<<std::endl; |
---|
370 | return; |
---|
371 | } |
---|
372 | } |
---|
373 | |
---|
374 | // Read in the mesh arrays |
---|
375 | osg::Vec2Array* vertexMeshVec2 = new osg::Vec2Array; |
---|
376 | osg::Vec2Array* texCoordMeshVec2 = new osg::Vec2Array; |
---|
377 | osg::Vec4Array* distortionMesh = new osg::Vec4Array(distortionMeshRows*distortionMeshColumns); |
---|
378 | |
---|
379 | if((_distortionType==VERTEXDISTORTION) || (_distortionType==COMBINEDDISTORTION)) |
---|
380 | { |
---|
381 | readMeshPointsFromCSVFile(_vertexDistortionFilename, vertexMeshVec2); |
---|
382 | } |
---|
383 | |
---|
384 | if((_distortionType==TEXCOORDDISTORTION) || (_distortionType==COMBINEDDISTORTION)) |
---|
385 | { |
---|
386 | readMeshPointsFromCSVFile(_texCoordDistortionFilename, texCoordMeshVec2); |
---|
387 | } |
---|
388 | |
---|
389 | // Transfer the read values into the combinedArray |
---|
390 | for(int row=0;row<distortionMeshRows;row++) |
---|
391 | { |
---|
392 | for(int col=0;col<distortionMeshColumns;col++) |
---|
393 | { |
---|
394 | float x=(float)col/(float)(distortionMeshColumns-1); // Vertex X |
---|
395 | float y=(float)row/(float)(distortionMeshRows-1); // Vertex Y |
---|
396 | float z=(float)col/(float)(distortionMeshColumns-1); // TexCoord X |
---|
397 | float w=(float)row/(float)(distortionMeshRows-1); // TexCoord Y |
---|
398 | |
---|
399 | if(_distortionType==VERTEXDISTORTION || _distortionType==COMBINEDDISTORTION) |
---|
400 | { |
---|
401 | x = vertexMeshVec2->at(row*distortionMeshColumns + col).x(); |
---|
402 | y = vertexMeshVec2->at(row*distortionMeshColumns + col).y(); |
---|
403 | } |
---|
404 | if(_distortionType==TEXCOORDDISTORTION || _distortionType==COMBINEDDISTORTION) |
---|
405 | { |
---|
406 | z = texCoordMeshVec2->at(row*distortionMeshColumns + col).x(); |
---|
407 | w = texCoordMeshVec2->at(row*distortionMeshColumns + col).y(); |
---|
408 | } |
---|
409 | |
---|
410 | distortionMesh->at(row*distortionMeshColumns+col).set(x,y,z,w); |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | #if 1 |
---|
415 | for (osg::Vec4Array::iterator it = distortionMesh->begin(); it != distortionMesh->end(); ++it) |
---|
416 | { |
---|
417 | OSG_ALWAYS<<"final vector: "<<(*it).x()<<" "<<(*it).y()<<" "<<(*it).z()<<" "<<(*it).w()<<std::endl; |
---|
418 | } |
---|
419 | #endif |
---|
420 | |
---|
421 | |
---|
422 | distortionSet->setDistortionMesh(distortionMesh); |
---|
423 | //distortionSet->setDistortionMeshColumns(distortionMeshColumns); |
---|
424 | //distortionSet->setDistortionMeshRows(distortionMeshRows); |
---|
425 | distortionSet->setDistortionMeshDimensions(distortionMeshRows, distortionMeshColumns); |
---|
426 | } |
---|
427 | } |
---|
428 | } |
---|