ManageProject.cpp 6.23 KB
#include "trueeyes.h"
#include "GlobalValues.h"
#include "VolumeDataStruct.h"
#include "VolumeShaderStruct.h"
#include <fstream>
#include <qstring.h>
#include <qdir.h>
using namespace std;

#define OBJECT_TYPE_VOLUME	0
#define OBJECT_TYPE_SHADER	1
#define OBJECT_TYPE_SOURCE	2
#define OBJECT_TYPE_TEXTURE	3

void SaveProject(string filename)
{
	//determine the project directory
	QDir projectDir(QString(filename.c_str()));
	cout<<"Project directory: "<<string(projectDir.absolutePath().toAscii())<<endl;
	//create a project file for output
	ofstream out;
	out.open(filename.c_str());

	//save the global variables
	out<<StepSize<<endl;
	out<<gpVolSize[0]<<" "<<gpVolSize[1]<<" "<<gpVolSize[2]<<endl;
	out<<gpCropMin[0]<<" "<<gpCropMin[1]<<" "<<gpCropMin[2]<<endl;
	out<<gpCropMax[0]<<" "<<gpCropMax[1]<<" "<<gpCropMax[2]<<endl;

	//write volume data
	for(int v=0; v<VolumeList.size(); v++)
	{
		if(v > 0) out<<endl;
		VolumeData outVol = VolumeList[v];
		//specify the object as a volume
		out<<OBJECT_TYPE_VOLUME<<endl;
		out<<outVol.Name<<endl;
		out<<outVol.FileType<<endl;
		out<<outVol.Filenames.size()<<endl;
		for(int f=0; f<outVol.Filenames.size(); f++)
		{
			//out<<outVol.Filenames[f].getString()<<endl;
			QString fullPath = QString(outVol.Filenames[f].getString().c_str());
			QString relativePath = projectDir.relativeFilePath(fullPath);
			out<<string(relativePath.toAscii())<<endl;
		}
		out<<outVol.Dim.x<<endl;
		out<<outVol.Dim.y<<endl;
		out<<outVol.Dim.z<<endl;
		out<<outVol.HeaderSize<<endl;
		out<<outVol.BitType<<endl;
		out<<outVol.ExternalComponents<<endl;
		out<<outVol.ExternalDatatype<<endl;
		out<<outVol.InternalComponents<<endl;
		out<<outVol.InternalDatatype<<endl;
		out<<outVol.Normalized;
	}

	//write shader data
	for(int s=0; s<ShaderList.size(); s++)
	{
		VolumeShader outShader = ShaderList[s];
		out<<endl;
		out<<OBJECT_TYPE_SHADER<<endl;
		out<<outShader.Name<<endl;

		QString fullPath = QString(outShader.Filename.c_str());
		QString relativePath = projectDir.relativeFilePath(fullPath);
		out<<string(relativePath.toAscii());
	}

	//write source data
	for(int s=0; s<SourceList.size(); s++)
	{
		VolumeSource outSource = SourceList[s];
		out<<endl;
		out<<OBJECT_TYPE_SOURCE<<endl;
		out<<outSource.Name<<endl;
		point3D<float> position = outSource.S.getPosition();
		point3D<float> lookat = outSource.S.getLookAt();
		vector3D<float> up = outSource.S.getUp();
		out<<position.x<<" "<<position.y<<" "<<position.z<<endl;
		out<<lookat.x<<" "<<lookat.y<<" "<<lookat.z<<endl;
		out<<up.x<<" "<<up.y<<" "<<up.z;
	}

	//write texture data
	for(int t=0; t<TextureList.size(); t++)
	{
		TextureData outTexture = TextureList[t];
		out<<endl;
		out<<OBJECT_TYPE_TEXTURE<<endl;
		out<<outTexture.Name<<endl;

		//save the file information (convert to a relative path)
		QString fullPath = QString(outTexture.Filename.getString().c_str());
		QString relativePath = projectDir.relativeFilePath(fullPath);
		out<<string(relativePath.toAscii())<<endl;

		out<<outTexture.sX<<endl;
		out<<outTexture.sY<<endl;
		out<<outTexture.ExternalComponents<<endl;
		out<<outTexture.ExternalDatatype<<endl;
		out<<outTexture.InternalComponents<<endl;
		out<<outTexture.InternalDatatype<<endl;
		out<<outTexture.Normalized;

	}

	//close the file
	out.close();
}

void LoadProject(string filename)
{
	//determine the project directory
	QFileInfo projectFile(QString(filename.c_str()));
	QDir projectDir = projectFile.dir();
	QString test = projectDir.path();

	//first clear all previous list data
	int v, s;
	for(v=0; v<VolumeList.size(); v++)
		VolumeList[v].Texture.Clean();
	VolumeList.clear();
	for(s=0; s<ShaderList.size(); s++)
		ShaderList[s].glProgram.Clean();
	ShaderList.clear();

	SourceList.clear();

	//open the file for reading
	ifstream in;
	in.open(filename.c_str());
	if(!in.is_open())
    {
        cout<<"Error opening file."<<endl;
        return;
    }

	//grab the global variables
	in>>StepSize;
	in>>gpVolSize[0];
	in>>gpVolSize[1];
	in>>gpVolSize[2];
	in>>gpCropMin[0];
	in>>gpCropMin[1];
	in>>gpCropMin[2];
	in>>gpCropMax[0];
	in>>gpCropMax[1];
	in>>gpCropMax[2];

	while(!in.eof())
	{
		//get the object type
		int t;
		char c[1024];
		in>>t;
		if(t == OBJECT_TYPE_VOLUME)
		{
			VolumeData inVol;
			in>>inVol.Name;
			in>>inVol.FileType;
			int num_files;
			in>>num_files;
			for(int f=0; f<num_files; f++)
			{
                //get the file name
				in>>c;
				QString relativePath = QString(c);
				QString fullPath = projectDir.absoluteFilePath(relativePath);
				inVol.Filenames.push_back(string(fullPath.toAscii()));
			}
			in>>inVol.Dim.x;
			in>>inVol.Dim.y;
			in>>inVol.Dim.z;
			in>>inVol.HeaderSize;
			in>>inVol.BitType;
			in>>inVol.ExternalComponents;
			in>>inVol.ExternalDatatype;
			in>>inVol.InternalComponents;
			in>>inVol.InternalDatatype;
			in>>inVol.Normalized;

			//add the volume data structure to the list
			if(inVol.FileType == VOLUME_FILE_RAW)
				LoadRawVolume(inVol);
			if(inVol.FileType == VOLUME_FILE_IMAGES)
				LoadImages(inVol);
		}
		else if(t == OBJECT_TYPE_TEXTURE)
		{
			TextureData inTex;
			in>>inTex.Name;

			//read the file name
			in>>c;
			QString relativePath = QString(c);
			QString fullPath = projectDir.absoluteFilePath(relativePath);
			inTex.Filename = string(fullPath.toAscii());

			in>>inTex.sX;
			in>>inTex.sY;

			in>>inTex.ExternalComponents;
			in>>inTex.ExternalDatatype;
			in>>inTex.InternalComponents;
			in>>inTex.InternalDatatype;
			in>>inTex.Normalized;

			//add the texture data structure to the list
			LoadTexture(inTex);
			TextureList.push_back(inTex);
		}
		else if(t == OBJECT_TYPE_SHADER)
		{
			VolumeShader inShader;
			in>>inShader.Name;
			in>>c;
			QString relativePath = QString(c);
			QString fullPath = projectDir.absoluteFilePath(relativePath);
			inShader.Filename = string(fullPath.toAscii());

			ShaderList.push_back(inShader);
		}
		else if(t == OBJECT_TYPE_SOURCE)
		{
			VolumeSource inSource;
			in>>inSource.Name;

			point3D<float> position;
			point3D<float> lookat;
			vector3D<float> up;

			in>>position.x;
			in>>position.y;
			in>>position.z;
			in>>lookat.x;
			in>>lookat.y;
			in>>lookat.z;
			in>>up.x;
			in>>up.y;
			in>>up.z;

			inSource.S.setPosition(position);
			inSource.S.LookAt(lookat, up);
			SourceList.push_back(inSource);
		}
	}
	//close the file
	in.close();

	//reload everything
	ReloadShaders();

}