1 /*******************************************************************************
2 Copyright (c) 2012, Jonathan Hiller
3 
4 This file is part of the AMF Tools suite. http://amf.wikispaces.com/
5 AMF Tools is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
6 AMF Tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
7 See <http://www.opensource.org/licenses/lgpl-3.0.html> for license details.
8 *******************************************************************************/
9 
10 #ifndef CX3D_FILE_H
11 #define CX3D_FILE_H
12 
13 #include <string>
14 #include <vector>
15 #include "SimpleImage.h"
16 //#include <QImage>
17 //#include <QColor>
18 
19 
20 class CXmlStreamRead;
21 
22 
23 class CX3D_File;
24 
25 enum X3dLoadResult {XLR_SUCCESS, XLR_BADFILEPATH, XLR_NOSHAPE, XLR_BADIMAGEPATH};
26 
27 class xAppearanceNode
28 {
29 public:
xAppearanceNode(void)30 	xAppearanceNode(void){Clear();}
~xAppearanceNode(void)31 	~xAppearanceNode(void){};
32 	xAppearanceNode& operator=(const xAppearanceNode& x) {ImageTexture=x.ImageTexture; MatColorRed=x.MatColorRed; MatColorGreen=x.MatColorGreen; MatColorBlue=x.MatColorBlue; repeatS=x.repeatS; repeatT=x.repeatT; return *this;} //overload equals
xAppearanceNode(const xAppearanceNode & x)33 	xAppearanceNode(const xAppearanceNode& x) {*this = x;}
Clear()34 	void Clear() {repeatS = true; repeatT = true; MatColorRed = -1; MatColorGreen = -1; MatColorBlue = -1;}
35 
36 	X3dLoadResult ReadXML(CXmlStreamRead* pXML, CX3D_File* pX3dFile, std::string* pRetMessage = NULL);
37 
38 	CSimpleImage ImageTexture;
39 //	QImage ImageTexture;
40 	bool repeatS, repeatT;
41 
42 	double MatColorRed;
43 	double MatColorGreen;
44 	double MatColorBlue;
45 };
46 
47 enum ColChan {CC_R=0, CC_G=1, CC_B=2, CC_A=3};
48 enum TexCoordAxis {TCA_S=0, TCA_T=1};
49 enum XYZAxis {AX_X=0, AX_Y=1, AX_Z=2};
50 
51 class xIndexedFaceSetNode
52 {
53 public:
xIndexedFaceSetNode(void)54 	xIndexedFaceSetNode(void){Clear();}
~xIndexedFaceSetNode(void)55 	~xIndexedFaceSetNode(void){};
56 	xIndexedFaceSetNode& operator=(const xIndexedFaceSetNode& x); //overload equals
xIndexedFaceSetNode(const xIndexedFaceSetNode & x)57 	xIndexedFaceSetNode(const xIndexedFaceSetNode& x) {*this = x;}
58 	void Clear();
59 
60 	X3dLoadResult ReadXML(CXmlStreamRead* pXML, std::string* pRetMessage = NULL);
61 	std::string CoordDef; //the name of this particular coordinate set
62 	std::string CoordUse; //another coordinate set to use
63 	std::vector<int> coordIndex;
64 	std::vector<double> Coordinate;
65 
66 	std::vector<int> texCoordIndex;
67 	std::vector<double> TextureCoordinate;
68 
69 	bool colorPerVertex;
70 	std::vector<int> colorIndex;
71 	std::vector<double> Color;
72 	std::vector<double> ColorRGBA;
73 
74 //information to load x3d!
75 	bool FillDerivedInfo(); //fills in these calculated other parameters for easy access later.
76 	int NumVertPerFacet; //triangles or quads?
77 	bool Colors;
78 	bool ColByIndex;
79 	bool HasAlpha;
80 	bool HasTexture;
81 	int GetNumTriangles(); //returns 2x number of quads if x3d has quads...
GetNumCoords()82 	int GetNumCoords(){return Coordinate.size()/3;} //return number of points (coords)
83 	int GetCoordInd(int TriNum, int VertNum); //VertNum is 0, 1, or 2
GetCoord(int CoordNum,XYZAxis Axis)84 	inline double GetCoord(int CoordNum, XYZAxis Axis) {return Coordinate[CoordNum*3+(int)Axis];}
85 
86 	double GetColorFace(int TriNum, ColChan Chan); //triangle color (make sure Color && !colorPerVertex and HasAlpha if requesting alpha)
87 	double GetColorVert(int CoordNum, ColChan Chan); //vertex color (make sure Color && colorPerVertex and HasAlpha if requesting alpha)
88 	double GetColorVert(int TriNum, int VertNum, ColChan Chan); //vertex color (make sure Color && colorPerVertex and Hasplpha if requesting alpha)
89 	int GetTexCoordInd(int TriNum, int VertNum);
GetTexCoord(int TriNum,int VertNum,TexCoordAxis Axis)90 	inline double GetTexCoord(int TriNum, int VertNum, TexCoordAxis Axis){ return TextureCoordinate[GetTexCoordInd(TriNum, VertNum)*2+(int)Axis];}
91 
92 
93 
94 
95 };
96 
97 class xShapeNode
98 {
99 public:
xShapeNode(void)100 	xShapeNode(void) {Clear();};
~xShapeNode(void)101 	~xShapeNode(void) {};
102 	xShapeNode& operator=(const xShapeNode& x) {xAppearance=x.xAppearance; xIndexedFaceSet=x.xIndexedFaceSet; return *this;} //overload equals
xShapeNode(const xShapeNode & x)103 	xShapeNode(const xShapeNode& x) {*this = x;}
Clear()104 	void Clear() {xAppearance.Clear(); xIndexedFaceSet.Clear();}
105 	X3dLoadResult ReadXML(CXmlStreamRead* pXML, CX3D_File* pX3dFile, std::string* pRetMessage = NULL);
106 
107 	xAppearanceNode xAppearance;
108 	xIndexedFaceSetNode xIndexedFaceSet;
IsIndexedFaceSet(void)109 	bool IsIndexedFaceSet(void) {return !(xIndexedFaceSet.Coordinate.size() == 0 && xIndexedFaceSet.coordIndex.size() == 0);}
110 };
111 
112 
113 class CX3D_File
114 {
115 public:
116 	CX3D_File(void);
117 	~CX3D_File(void);
118 	CX3D_File& operator=(const CX3D_File& x) {xShapes=x.xShapes; filePath=x.filePath; ImagePath=x.ImagePath; errors=x.errors; return *this;} //overload equals
CX3D_File(const CX3D_File & x)119 	CX3D_File(const CX3D_File& x) {*this = x;}
Clear()120 	void Clear() {xShapes.clear(); filePath=""; ImagePath=""; errors=""; IsLoaded=false;}
121 
122 	std::vector<xShapeNode> xShapes;
123 	std::string filePath, ImagePath, errors;
124 
125 	bool IsLoaded;
126 	X3dLoadResult Load(std::string filename, std::string ImgPathIn = "");
127 
128 	void GetMinMax(double& minX, double& minY, double& minZ, double& maxX, double& maxY, double& maxZ);
129 	void GetSize(double& sizeX, double& sizeY, double& sizeZ);
130 
131 	static void Str2Data(std::string* pS, std::vector<int>* pD); // {std::stringstream ss(*pS); int i; while (ss >> i){pD->push_back(i);}}
132 	static void Str2Data(std::string* pS, std::vector<double>* pD); // {std::stringstream ss(*pS); double i; while (ss >> i){pD->push_back(i);}}
133 
134 };
135 
136 
137 
138 
139 
140 #endif //CX3D_FILE_H
141