Index: experimental/distortionNG/DistortionSetupStrategyProjectSyntropy.cpp
===================================================================
--- experimental/distortionNG/DistortionSetupStrategyProjectSyntropy.cpp	(revision 372)
+++ experimental/distortionNG/DistortionSetupStrategyProjectSyntropy.cpp	(revision 376)
@@ -17,8 +17,24 @@
 */
 
+#include <osgDB/ReadFile>
+#include <osg/Vec3d>
+#include <osgDB/fstream>
+
 #include "DistortionSetupStrategyProjectSyntropy.h"
 
 DistortionSetupStrategyProjectSyntropy::DistortionSetupStrategyProjectSyntropy()
 {
+	_switchDistortionTypeVortexToTexcoords=false;
+
+	_intensityMapFilename="./resources/ProjectSyntropy/blending_1.bmp";
+
+	_frustumVortexDistortionFilename="./resources/ProjectSyntropy/Vertices/frustum_1.csv";
+	_frustumTexcoordDistortionFilename="./resources/ProjectSyntropy/TexCoords/frustum_1.csv";
+
+	_meshVortexDistortionFilename="./resources/ProjectSyntropy/Vertices/warpmap_1.csv";
+	_meshTexcoordDistortionFilename="./resources/ProjectSyntropy/TexCoords/warpmap_1.csv";
+
+
+	_distortionInitialized=false;
 }
 
@@ -26,4 +42,204 @@
 {
 }
+
+void DistortionSetupStrategyProjectSyntropy::setDistortionInputFiles( std::string distortionFile, std::string blendmapFile, std::string frustumFile, DistortionSetupStrategy::distortionType type)
+{
+
+}
+
+int DistortionSetupStrategyProjectSyntropy::readFrustumFromCSVFile(std::string filePath, int numValues, float* frustumValues)
+{
+	int numSeparator=0;
+	std::string line, valueString;
+	char separator=';';
+	size_t valueStart, valueEnd;
+
+	osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str()));
+
+	for (int i=0;i<numValues; i++) frustumValues[i]=0.0;
+
+	if (inputFile.is_open())
+	{
+		inputFile.clear();
+		inputFile.seekg (0, std::ios::beg);
+
+		while (!inputFile.eof())
+		{
+			getline (inputFile,line);
+
+			if((line.size()>0) && (line.find('x')==std::string::npos))
+			{new osg::Vec2Array;
+				numSeparator=0;
+				for (size_t numLineChar=0; numLineChar < line.length(); numLineChar++)
+				{
+					if(line.at(numLineChar)==separator) numSeparator++;
+				}
+				if(numSeparator!=numValues-1) //Number of Entries per Line is not correct
+				{
+					OSG_ALWAYS<< "Inconsistency in file " << filePath << " detected. Please check this file." <<std::endl;
+					return 1;
+				}
+				valueStart=0;
+				valueEnd=0;
+				for (int j=0;j<numValues; j++)
+				{
+					if(j!=numValues-1)
+					{
+						if((valueEnd=line.find(separator,valueStart))!=std::string::npos)
+						{
+							valueString=line.substr(valueStart,valueEnd-valueStart);
+							valueStart=valueEnd+1;
+						}
+					}
+					else valueString=line.substr(valueStart,line.size());
+
+					frustumValues[j]=atof(const_cast<char*>(valueString.c_str()));
+				}
+
+			}
+
+		}
+		inputFile.close();
+	}
+	else	//File couldn't be loaded
+	{
+		OSG_ALWAYS<< "Unable to open " << filePath << std::endl;
+		return 1;
+	}
+	return 0;
+}
+
+int DistortionSetupStrategyProjectSyntropy::readMeshDimensionsFromCSVFile(std::string filePath, int* meshRows, int* meshColumns)
+{
+	int tempRows=0, tempCols=0, numSeparator=0, numValues=6;
+	std::string line, valueString;
+	char separator=';';
+	size_t valueStart, valueEnd;
+
+	osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str()));
+
+	if (inputFile.is_open())
+	{
+		inputFile.clear();
+		inputFile.seekg (0, std::ios::beg);
+
+		while (!inputFile.eof())
+		{
+			getline (inputFile,line);
+
+			if((line.size()>0) && (line.find('x')==std::string::npos))
+			{
+				numSeparator=0;
+				for (size_t numLineChar=0; numLineChar < line.length(); numLineChar++)
+				{
+					if(line.at(numLineChar)==separator) numSeparator++;
+				}
+				if(numSeparator!=numValues-1) //Number of Entries per Line is not correct
+				{
+					OSG_ALWAYS<< "Inconsistency in file " << filePath << " detected. Please check this file." <<std::endl;
+					return 1;
+				}
+				//Get number of rows of the distortion grid, last valid line of input file contains actual value 
+				valueStart=line.rfind(separator,line.size());
+				valueString=line.substr(valueStart+1,line.size()-valueStart-1);
+				tempRows=atoi(const_cast<char*>(valueString.c_str()));
+				if(tempRows>*meshRows) *meshRows=tempRows;
+
+				//Get number of columns of the distortion grid, last valid line of input file contains actual value
+				valueEnd=valueStart-1;
+				valueStart=line.rfind(separator,valueEnd);
+				valueString=line.substr(valueStart+1,valueEnd-valueStart);
+				tempCols=atoi(const_cast<char*>(valueString.c_str()));
+				if(tempCols>*meshColumns) *meshColumns=tempCols;
+			}
+
+		}
+		inputFile.close();
+	}
+	else	//File couldn't be loaded
+	{
+		OSG_ALWAYS<< "Unable to open " << filePath << std::endl;
+		return 1;
+	}
+
+	*meshColumns+=1;
+	*meshRows+=1;
+
+	return 0;
+}
+
+int DistortionSetupStrategyProjectSyntropy::readMeshPointsFromCSVFile(std::string filePath, osg::Vec2Array* meshPointsVec2, osg::Vec3Array* meshPointsVec3, int displayWidth, int displayHeight)
+{
+
+	int numValues=6;
+	std::string line, valueString;
+	char separator=';';
+	float* values = new float[numValues];  
+	size_t valueStart, valueEnd;
+	osg::Vec2 tempVec2;
+	osg::Vec3 tempVec3;
+
+	osgDB::ifstream inputFile (const_cast<char*>(filePath.c_str()));
+
+	if (inputFile.is_open())
+	{
+		inputFile.clear();
+		inputFile.seekg (0, std::ios::beg);
+
+		while (!inputFile.eof())
+		{
+			getline (inputFile,line);
+
+			if((line.size()>0) && (line.find('x')==std::string::npos))
+			{
+				for (int i=0;i<numValues; i++) values[i]=0.0;
+
+				valueStart=0;
+				valueEnd=0;
+				for (int j=0;j<numValues; j++)
+				{
+					if(j!=numValues-1)
+					{
+						if((valueEnd=line.find(separator,valueStart))!=std::string::npos)
+						{
+							valueString=line.substr(valueStart,valueEnd-valueStart);
+							valueStart=valueEnd+1;
+						}
+					}
+					else valueString=line.substr(valueStart,line.size());
+
+					values[j]=atof(const_cast<char*>(valueString.c_str()));
+				}
+
+				// Insertion of Values U and V (corresponding to values[2],values[3] in a temporary Array
+				// which is returned and merged into distortionSet later 
+				if(_switchDistortionTypeVortexToTexcoords)
+				{
+					tempVec2.set(values[2],values[3]);
+					meshPointsVec2->push_back(tempVec2);
+				}
+				else
+				{
+					tempVec3.set(values[2]*displayWidth,values[3]*displayHeight,0.0);
+					meshPointsVec3->push_back(tempVec3);
+				}
+			}
+
+		}
+		inputFile.close();
+	}
+	else
+	{
+		OSG_ALWAYS<< "Unable to open " << filePath << std::endl;
+		return 1;
+	}
+
+	return 0;
+}
+
+
+
+
+
 
 void DistortionSetupStrategyProjectSyntropy::delegateDistortionSetup(osgViewer::DistortionSet* distortionSet)
@@ -35,12 +251,133 @@
 	}
 
-	OSG_ALWAYS<<"drin"<<std::endl;
-
-	/* Todo: In diese Klasse das Einlesen und übertragen der Project Syntropy Dateien packen:
-	*
-	* 1. Blendmap einlesen, auf Screensize skalieren und in den distortioncontainer übertragen
-	* 2. Frustum CSV auslesen und die Werte in ViewMatrix und  Projectionmatrix übertragen
-	* 3. Meshkoordinaten aus CSV einlesen und in DistortionSet übertragen
-	*
-	*/
-}
+	//OSG_ALWAYS<<"drin"<<std::endl;
+
+	if (_distortionInitialized==false)
+	{
+
+		// ******************** //
+		// *** IntensityMap *** //
+		// ******************** //
+
+		osg::Image* intensityMap=NULL;
+		intensityMap=osgDB::readImageFile(_intensityMapFilename);
+		osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
+		if (!wsi)
+	    	{
+			OSG_NOTICE<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
+			return;
+	    	}
+
+		osg::GraphicsContext::ScreenIdentifier si;
+	    	si.readDISPLAY();
+
+	   	if (si.displayNum<0) si.displayNum = 0;
+
+	    	si.screenNum = 0; //Fixed value, may be replaced later
+
+	   	unsigned int width=0, height=0;
+	    	wsi->getScreenResolution(si, width, height);
+
+		if(intensityMap->s()!=width || intensityMap->t()!=height)
+			intensityMap->scaleImage(width, height, intensityMap->r());
+
+		distortionSet->setIntensityMap(intensityMap);
+
+		// ******************************* //
+		// *** Frustum and View Offset *** //
+		// ******************************* //
+
+		//....Read Files and set parameters
+		int numFrustumValues=16;
+		float* frustumValues = new float[numFrustumValues];
+		double zNear=0.1, zFar=100.0;
+
+		if(_switchDistortionTypeVortexToTexcoords)	readFrustumFromCSVFile(_frustumTexcoordDistortionFilename, numFrustumValues, frustumValues);
+		else						readFrustumFromCSVFile(_frustumVortexDistortionFilename, numFrustumValues, frustumValues);
+
+		osg::Matrixd viewOffset=osg::Matrixd();
+		osg::Matrixd projectionOffset=osg::Matrixd();
+
+		// View Offset Parameters: x, y, z, heading, pitch, bank
+		// Translational Parameters are defines in millimeters (->Correction required?!)
+		// Coordinate System / Directions may be looked up in "Koordinatensystem.png", trans/rot order confirmed bei PS
+		viewOffset = osg::Matrixd::translate(frustumValues[0],frustumValues[1],frustumValues[2]);
+		viewOffset*= osg::Matrixd::rotate(osg::inDegrees(frustumValues[3]), 0.0,0.0,1.0); //Heading
+		viewOffset*= osg::Matrixd::rotate(osg::inDegrees(frustumValues[4]), 1.0,0.0,0.0); //Pitch
+		viewOffset*= osg::Matrixd::rotate(osg::inDegrees(frustumValues[5]), 0.0,1.0,0.0); //Bank
+
+		//Frustum Parameters: Left, Right, Bottom, Top, zNear, zFar
+		projectionOffset.makeFrustum(osg::inDegrees(frustumValues[6]), osg::inDegrees(frustumValues[7]), osg::inDegrees(frustumValues[8]), osg::inDegrees(frustumValues[9]), zNear, zFar);
+
+		distortionSet->setViewOffset(viewOffset);
+		distortionSet->setProjectionOffset(projectionOffset);
+
+		// ******************************* //
+		// ******* Distortion Mesh ******* //
+		// ******************************* //
+
+		osg::Geometry* distortionMeshGeometry = distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry();
+		if(distortionMeshGeometry)
+		{
+			//*******************************************************************************************//
+			//***    PRINT VERTEXARRAY/TEXCOORDARRAY for Debug Purposes                               ***//
+			//***    both Arrays are listed in "HorizontalLine-after-HorizontalLine" order,           ***//
+			//***    sorted from topleft (0,0,0 vertices; 0,0 texcoords) to bottomright               ***//
+			//***    (1920,1080,0 vertices; 1,1 texcoords)						  ***//
+			//*******************************************************************************************//
+			
+			if(0)
+			{
+				osg::Vec3Array* vertices =  (osg::Vec3Array*) distortionMeshGeometry->getVertexArray();
+				osg::Vec2Array* texcoords0 = (osg::Vec2Array*) distortionMeshGeometry->getTexCoordArray(0);
+
+				for (osg::Vec3Array::iterator it = vertices->begin(); it != vertices->end(); ++it)
+				{
+					OSG_ALWAYS<<"Vertice "<<(*it).x()<<" "<<(*it).y()<<" "<<(*it).z()<<std::endl;
+				}
+
+				for (osg::Vec2Array::iterator it = texcoords0->begin(); it != texcoords0->end(); ++it)
+				{
+					OSG_ALWAYS<<"Texcoord "<<(*it).x()<<" "<<(*it).y()<<std::endl;
+				}
+			}
+
+			//*******************************************************************************************//
+			//***    MESH WARPING	 								  ***//
+			//***    Both Vertex Warping and Texcoord Warping is supported. Although there might      ***//
+			//***    be a preferred version for osgvisual, both options should be implemented in osg  ***//
+			//*******************************************************************************************//
+
+
+			int distortionMeshRows=0, distortionMeshColumns=0;
+			if(_switchDistortionTypeVortexToTexcoords)	readMeshDimensionsFromCSVFile(_meshTexcoordDistortionFilename, &distortionMeshRows, &distortionMeshColumns);
+			else						readMeshDimensionsFromCSVFile(_meshVortexDistortionFilename, &distortionMeshRows, &distortionMeshColumns);
+
+			distortionSet->setDistortionMeshRows(distortionMeshRows);
+			distortionSet->setDistortionMeshColumns(distortionMeshColumns);
+
+
+
+			osg::Vec2Array* distortionMeshVec2 = new osg::Vec2Array;
+			osg::Vec3Array* distortionMeshVec3 = new osg::Vec3Array;
+
+			if(_switchDistortionTypeVortexToTexcoords)
+			{
+				readMeshPointsFromCSVFile(_meshTexcoordDistortionFilename, distortionMeshVec2, distortionMeshVec3, width, height);
+				distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry()->setTexCoordArray(0,distortionMeshVec2);
+			}
+			else
+			{
+				readMeshPointsFromCSVFile(_meshVortexDistortionFilename, distortionMeshVec2, distortionMeshVec3, width, height);
+				distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry()->setVertexArray(distortionMeshVec3);
+
+			}
+
+		}
+
+		_distortionInitialized=true;
+
+
+	}
+
+}
