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 | _distortionFilename = ""; |
---|
31 | _distortionType = DistortionSetupStrategy::UNDEFINED; |
---|
32 | |
---|
33 | _distortionInitialized=false; |
---|
34 | } |
---|
35 | |
---|
36 | DistortionSetupStrategyProjectSyntropy::~DistortionSetupStrategyProjectSyntropy() |
---|
37 | { |
---|
38 | |
---|
39 | } |
---|
40 | |
---|
41 | void 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 | |
---|
49 | int 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 | |
---|
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* meshPointsVec2, osg::Vec3Array* meshPointsVec3, int displayWidth, int displayHeight) |
---|
171 | { |
---|
172 | |
---|
173 | int numValues=6; |
---|
174 | std::string line, valueString; |
---|
175 | char separator=';'; |
---|
176 | float* values = new float[numValues]; |
---|
177 | size_t valueStart, valueEnd; |
---|
178 | osg::Vec2 tempVec2; |
---|
179 | osg::Vec3 tempVec3; |
---|
180 | |
---|
181 | osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str())); |
---|
182 | |
---|
183 | if (inputFile.is_open()) |
---|
184 | { |
---|
185 | inputFile.clear(); |
---|
186 | inputFile.seekg (0, std::ios::beg); |
---|
187 | |
---|
188 | while (!inputFile.eof()) |
---|
189 | { |
---|
190 | getline (inputFile,line); |
---|
191 | |
---|
192 | if((line.size()>0) && (line.find('x')==std::string::npos)) |
---|
193 | { |
---|
194 | for (int i=0;i<numValues; i++) values[i]=0.0; |
---|
195 | |
---|
196 | valueStart=0; |
---|
197 | valueEnd=0; |
---|
198 | for (int j=0;j<numValues; j++) |
---|
199 | { |
---|
200 | if(j!=numValues-1) |
---|
201 | { |
---|
202 | if((valueEnd=line.find(separator,valueStart))!=std::string::npos) |
---|
203 | { |
---|
204 | valueString=line.substr(valueStart,valueEnd-valueStart); |
---|
205 | valueStart=valueEnd+1; |
---|
206 | } |
---|
207 | } |
---|
208 | else valueString=line.substr(valueStart,line.size()); |
---|
209 | |
---|
210 | values[j]=atof(const_cast<char*>(valueString.c_str())); |
---|
211 | } |
---|
212 | |
---|
213 | // Insertion of Values U and V (corresponding to values[2],values[3] in a temporary Array |
---|
214 | // which is returned and merged into distortionSet later |
---|
215 | |
---|
216 | if((_distortionType==TEXCOORDDISTORTION) || (_distortionType==COMBINEDDISTORTION)) |
---|
217 | { |
---|
218 | tempVec2.set(values[2],values[3]); |
---|
219 | meshPointsVec2->push_back(tempVec2); |
---|
220 | } |
---|
221 | if((_distortionType==VERTEXDISTORTION) || (_distortionType==COMBINEDDISTORTION)) |
---|
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 | |
---|
245 | void 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 | if( _distortionType == DistortionSetupStrategy::UNDEFINED || _blendmapFilename.empty() || _frustumFilename.empty() || _distortionFilename.empty() ) |
---|
254 | { |
---|
255 | OSG_ALWAYS<<"DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup : You have not specified the imput filenames and distortion type!"<<std::endl; |
---|
256 | return; |
---|
257 | } |
---|
258 | |
---|
259 | //OSG_ALWAYS<<"drin"<<std::endl; |
---|
260 | |
---|
261 | if (_distortionInitialized==false ) |
---|
262 | { |
---|
263 | _distortionInitialized=true; |
---|
264 | distortionSet->dirty(); |
---|
265 | |
---|
266 | // ******************** // |
---|
267 | // *** IntensityMap *** // |
---|
268 | // ******************** // |
---|
269 | |
---|
270 | osg::Image* intensityMap=NULL; |
---|
271 | intensityMap=osgDB::readImageFile(_blendmapFilename); |
---|
272 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
---|
273 | if (!wsi) |
---|
274 | { |
---|
275 | OSG_NOTICE<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
---|
276 | return; |
---|
277 | } |
---|
278 | |
---|
279 | osg::GraphicsContext::ScreenIdentifier si; |
---|
280 | si.readDISPLAY(); |
---|
281 | |
---|
282 | if (si.displayNum<0) si.displayNum = 0; |
---|
283 | |
---|
284 | si.screenNum = 0; //Fixed value, may be replaced later |
---|
285 | |
---|
286 | unsigned int width=0, height=0; |
---|
287 | wsi->getScreenResolution(si, width, height); |
---|
288 | |
---|
289 | if(intensityMap->s()!=width || intensityMap->t()!=height) |
---|
290 | intensityMap->scaleImage(width, height, intensityMap->r()); |
---|
291 | |
---|
292 | // Copy image data and flag as dirty to update texture. |
---|
293 | osg::copyImage(intensityMap, 0, 0, 0, intensityMap->s(), intensityMap->t(), intensityMap->r(), distortionSet->getIntensityMap(), 0, 0, 0, true); |
---|
294 | distortionSet->getIntensityMap()->dirty(); |
---|
295 | |
---|
296 | // ******************************* // |
---|
297 | // *** Frustum and View Offset *** // |
---|
298 | // ******************************* // |
---|
299 | |
---|
300 | //....Read Files and set parameters |
---|
301 | int numFrustumValues=16; |
---|
302 | float* frustumValues = new float[numFrustumValues]; |
---|
303 | double zNear=0.1, zFar=100.0; |
---|
304 | |
---|
305 | readFrustumFromCSVFile(_frustumFilename, numFrustumValues, frustumValues); |
---|
306 | |
---|
307 | osg::Matrixd viewOffset=osg::Matrixd(); |
---|
308 | osg::Matrixd projectionOffset=osg::Matrixd(); |
---|
309 | |
---|
310 | // View Offset Parameters: x, y, z, heading, pitch, bank |
---|
311 | // Translational Parameters are defines in millimeters (->Correction required?!) |
---|
312 | // Coordinate System / Directions may be looked up in "Koordinatensystem.png", trans/rot order confirmed bei PS |
---|
313 | viewOffset = osg::Matrixd::translate(frustumValues[0],frustumValues[1],frustumValues[2]); |
---|
314 | viewOffset*= osg::Matrixd::rotate(osg::inDegrees(frustumValues[3]), 0.0,0.0,1.0); //Heading |
---|
315 | viewOffset*= osg::Matrixd::rotate(osg::inDegrees(frustumValues[4]), 1.0,0.0,0.0); //Pitch |
---|
316 | viewOffset*= osg::Matrixd::rotate(osg::inDegrees(frustumValues[5]), 0.0,1.0,0.0); //Bank |
---|
317 | |
---|
318 | //Frustum Parameters: Left, Right, Bottom, Top, zNear, zFar |
---|
319 | //Alternative: viewer->camera->setprojectionmatrixasfrustum? |
---|
320 | projectionOffset.makeFrustum(osg::inDegrees(frustumValues[6]), osg::inDegrees(frustumValues[7]), osg::inDegrees(frustumValues[8]), osg::inDegrees(frustumValues[9]), zNear, zFar); |
---|
321 | |
---|
322 | distortionSet->setViewOffset(viewOffset); |
---|
323 | distortionSet->setProjectionOffset(projectionOffset); |
---|
324 | return; |
---|
325 | // ******************************* // |
---|
326 | // ******* Distortion Mesh ******* // |
---|
327 | // ******************************* // |
---|
328 | |
---|
329 | osg::Geometry* distortionMeshGeometry = distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry(); |
---|
330 | if(distortionMeshGeometry) |
---|
331 | { |
---|
332 | //*******************************************************************************************// |
---|
333 | //*** PRINT VERTEXARRAY/TEXCOORDARRAY for Debug Purposes ***// |
---|
334 | //*** both Arrays are listed in "HorizontalLine-after-HorizontalLine" order, ***// |
---|
335 | //*** sorted from topleft (0,0,0 vertices; 0,0 texcoords) to bottomright ***// |
---|
336 | //*** (1920,1080,0 vertices; 1,1 texcoords) ***// |
---|
337 | //*******************************************************************************************// |
---|
338 | |
---|
339 | if(0) |
---|
340 | { |
---|
341 | osg::Vec3Array* vertices = (osg::Vec3Array*) distortionMeshGeometry->getVertexArray(); |
---|
342 | osg::Vec2Array* texcoords0 = (osg::Vec2Array*) distortionMeshGeometry->getTexCoordArray(0); |
---|
343 | |
---|
344 | for (osg::Vec3Array::iterator it = vertices->begin(); it != vertices->end(); ++it) |
---|
345 | { |
---|
346 | OSG_ALWAYS<<"Vertice "<<(*it).x()<<" "<<(*it).y()<<" "<<(*it).z()<<std::endl; |
---|
347 | } |
---|
348 | |
---|
349 | for (osg::Vec2Array::iterator it = texcoords0->begin(); it != texcoords0->end(); ++it) |
---|
350 | { |
---|
351 | OSG_ALWAYS<<"Texcoord "<<(*it).x()<<" "<<(*it).y()<<std::endl; |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | //*******************************************************************************************// |
---|
356 | //*** MESH WARPING ***// |
---|
357 | //*** Both Vertex Warping and Texcoord Warping is supported. Although there might ***// |
---|
358 | //*** be a preferred version for osgvisual, both options should be implemented in osg ***// |
---|
359 | //*******************************************************************************************// |
---|
360 | |
---|
361 | int distortionMeshRows=0, distortionMeshColumns=0; |
---|
362 | |
---|
363 | readMeshDimensionsFromCSVFile(_distortionFilename, &distortionMeshRows, &distortionMeshColumns); |
---|
364 | |
---|
365 | distortionSet->setDistortionMeshRows(distortionMeshRows); |
---|
366 | distortionSet->setDistortionMeshColumns(distortionMeshColumns); |
---|
367 | |
---|
368 | osg::Vec2Array* distortionMeshVec2 = new osg::Vec2Array; |
---|
369 | osg::Vec3Array* distortionMeshVec3 = new osg::Vec3Array; |
---|
370 | |
---|
371 | if((_distortionType==TEXCOORDDISTORTION) || (_distortionType==COMBINEDDISTORTION)) |
---|
372 | { |
---|
373 | readMeshPointsFromCSVFile(_distortionFilename, distortionMeshVec2, distortionMeshVec3, width, height); |
---|
374 | distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry()->setTexCoordArray(0,distortionMeshVec2); |
---|
375 | } |
---|
376 | |
---|
377 | if((_distortionType==VERTEXDISTORTION) || (_distortionType==COMBINEDDISTORTION)) |
---|
378 | { |
---|
379 | readMeshPointsFromCSVFile(_distortionFilename, distortionMeshVec2, distortionMeshVec3, width, height); |
---|
380 | distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry()->setVertexArray(distortionMeshVec3); |
---|
381 | |
---|
382 | } |
---|
383 | |
---|
384 | } |
---|
385 | |
---|
386 | } |
---|
387 | |
---|
388 | } |
---|