TexGen
MeshData.h
Go to the documentation of this file.
1/*=============================================================================
2TexGen: Geometric textile modeller.
3Copyright (C) 2006 Martin Sherburn
4
5This program is free software; you can redistribute it and/or
6modify it under the terms of the GNU General Public License
7as published by the Free Software Foundation; either version 2
8of the License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18=============================================================================*/
19
20#pragma once
21
22namespace TexGen
23{
24 using namespace std;
25
27 {
28 public:
30 {
33 };
34 CMeshDataBase(string Name, DATA_TYPE Type) : m_Name(Name), m_DataType(Type) {}
35 virtual ~CMeshDataBase() {}
36
37 virtual void InsertVTKData(TiXmlElement &Parent) const = 0;
38
39 string m_Name;
41 };
42
43 template <typename T>
45 {
46 public:
47 CMeshData(string Name, DATA_TYPE Type) : CMeshDataBase(Name, Type) {}
48
49 void InsertVTKData(TiXmlElement &Parent) const
50 {
51 ostringstream DataStream;
52 typename vector<T>::const_iterator itData;
53 for (itData = m_Data.begin(); itData != m_Data.end(); ++itData)
54 {
55 WriteVTKDataElement(DataStream, *itData) << " ";
56 }
57/* for(unsigned int i=0; i<m_Data.size(); ++i)
58 {
59 WriteVTKDataElement(DataStream, m_Data[i]) << " ";
60 }*/
61 TiXmlElement Element("DataArray");
62 {
63 Element.SetAttribute("type", GetVTKDataType());
64 Element.SetAttribute("NumberOfComponents", GetNumberOfComponents());
65 Element.SetAttribute("format", "ascii");
66 Element.SetAttribute("Name", m_Name.c_str());
67 Element.InsertEndChild(TiXmlText(DataStream.str()));
68 }
69 Parent.InsertEndChild(Element);
70 }
71 static string GetVTKDataType()
72 {
73 // Data type unknown?
74 assert(false);
75 return "";
76 }
78 {
79 return 1;
80 }
81 ostream &WriteVTKDataElement(ostream &output, T Element) const
82 {
83 output << Element;
84 return output;
85 }
86
87 vector<T> m_Data;
88 };
89
90 // Need some specialisations on the char to make sure it gets output as an integer rather than a character
91 template <>
92 inline ostream &CMeshData<char>::WriteVTKDataElement(ostream &output, char Element) const
93 {
94 output << (int)Element;
95 return output;
96 }
97 template <>
98 inline ostream &CMeshData<unsigned char>::WriteVTKDataElement(ostream &output, unsigned char Element) const
99 {
100 output << (int)Element;
101 return output;
102 }
103
104 #define DEFINE_INT_DATA_TYPE(TYPE) template <> inline string CMeshData<TYPE>::GetVTKDataType() {return "Int" + stringify(sizeof(TYPE)*8);}
105 #define DEFINE_UINT_DATA_TYPE(TYPE) template <> inline string CMeshData<TYPE>::GetVTKDataType() {return "UInt" + stringify(sizeof(TYPE)*8);}
106
111 DEFINE_UINT_DATA_TYPE(unsigned char)
112 DEFINE_UINT_DATA_TYPE(unsigned short)
113 DEFINE_UINT_DATA_TYPE(unsigned int)
114 DEFINE_UINT_DATA_TYPE(unsigned long)
115
116 #undef DEFINE_INT_DATA_TYPE
117 #undef DEFINE_UINT_DATA_TYPE
118
119 template <> string inline CMeshData<float>::GetVTKDataType() {return "Float" + stringify(sizeof(float)*8);}
120 template <> string inline CMeshData<double>::GetVTKDataType() {return "Float" + stringify(sizeof(double)*8);}
121
123 // DEFINE THE USER-DEFINED DATA TYPES //
125
127 template <>
129 {
131 }
132 template <>
134 {
135 return 3;
136 }
137 template <>
138 inline ostream &CMeshData<XYZ>::WriteVTKDataElement(ostream &output, XYZ Element) const
139 {
140 output << Element.x << " " << Element.y << " " << Element.z;
141 return output;
142 }
143
145 template <>
147 {
149 }
150 template <>
152 {
153 return 2;
154 }
155 template <>
156 inline ostream &CMeshData<XY>::WriteVTKDataElement(ostream &output, XY Element) const
157 {
158 output << Element.x << " " << Element.y;
159 return output;
160 }
161
162}; // namespace TexGen
163
164
165
166
167
168
169
170
171
172
173
174
175
#define DEFINE_UINT_DATA_TYPE(TYPE)
Definition: MeshData.h:105
#define DEFINE_INT_DATA_TYPE(TYPE)
Definition: MeshData.h:104
#define CLASS_DECLSPEC
Definition: Misc.h:35
virtual ~CMeshDataBase()
Definition: MeshData.h:35
virtual void InsertVTKData(TiXmlElement &Parent) const =0
DATA_TYPE m_DataType
Definition: MeshData.h:40
CMeshDataBase(string Name, DATA_TYPE Type)
Definition: MeshData.h:34
static int GetNumberOfComponents()
Definition: MeshData.h:77
vector< T > m_Data
Definition: MeshData.h:87
CMeshData(string Name, DATA_TYPE Type)
Definition: MeshData.h:47
void InsertVTKData(TiXmlElement &Parent) const
Definition: MeshData.h:49
static string GetVTKDataType()
Definition: MeshData.h:71
ostream & WriteVTKDataElement(ostream &output, T Element) const
Definition: MeshData.h:81
Namespace containing a series of customised math operations not found in the standard c++ library.
std::string stringify(const T &x, int iPrecision=12, bool bScientific=true)
Function to convert a value (e.g. int, double, etc...) to a string.
Definition: Misc.h:50
Struct for representing points in 2D space.
Definition: mymath.h:103
double x
Definition: mymath.h:104
double y
Definition: mymath.h:104
Struct for representing points in 3D space.
Definition: mymath.h:56
double z
Definition: mymath.h:57
double x
Definition: mymath.h:57
double y
Definition: mymath.h:57