XdmfItemFactory.cpp 5.99 KB
Newer Older
1
#include <boost/tokenizer.hpp>
2
3
4
#include "XdmfAttribute.hpp"
#include "XdmfDomain.hpp"
#include "XdmfGeometry.hpp"
5
#include "XdmfGeometryType.hpp"
6
#include "XdmfGridCollection.hpp"
7
8
#include "XdmfGridCurvilinear.hpp"
#include "XdmfGridRectilinear.hpp"
9
#include "XdmfGridRegular.hpp"
10
#include "XdmfGridUnstructured.hpp"
11
#include "XdmfInformation.hpp"
12
#include "XdmfItemFactory.hpp"
13
#include "XdmfMap.hpp"
14
#include "XdmfSet.hpp"
15
#include "XdmfTime.hpp"
16
17
#include "XdmfTopology.hpp"

18
19
20
21
22
23
boost::shared_ptr<XdmfItemFactory> XdmfItemFactory::New()
{
	boost::shared_ptr<XdmfItemFactory> p(new XdmfItemFactory());
	return p;
}

24
25
26
27
28
29
30
31
XdmfItemFactory::XdmfItemFactory()
{
}

XdmfItemFactory::~XdmfItemFactory()
{
}

32
boost::shared_ptr<XdmfItem> XdmfItemFactory::createItem(const std::string & itemTag, const std::map<std::string, std::string> & itemProperties, const std::vector<boost::shared_ptr<XdmfItem> > & childItems) const
33
{
34
	boost::shared_ptr<XdmfItem> newItem = XdmfCoreItemFactory::createItem(itemTag, itemProperties, childItems);
35

36
	if(newItem)
37
	{
38
		return newItem;
39
	}
40
41

	if(itemTag.compare(XdmfAttribute::ItemTag) == 0)
42
	{
43
		return XdmfAttribute::New();
44
45
46
	}
	else if(itemTag.compare(XdmfDomain::ItemTag) == 0)
	{
47
		return XdmfDomain::New();
48
49
50
	}
	else if(itemTag.compare(XdmfGeometry::ItemTag) == 0)
	{
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
		std::map<std::string, std::string>::const_iterator type = itemProperties.find("Type");
		if(type == itemProperties.end())
		{
			type = itemProperties.find("GeometryType");
		}

		if(type != itemProperties.end())
		{
			const std::string typeVal = type->second;
			if(typeVal.compare("ORIGIN_DXDY") == 0 || typeVal.compare("ORIGIN_DXDYDZ") == 0)
			{
				boost::shared_ptr<XdmfArray> origin = boost::shared_ptr<XdmfArray>();
				boost::shared_ptr<XdmfArray> brickSize = boost::shared_ptr<XdmfArray>();
				for(std::vector<boost::shared_ptr<XdmfItem> >::const_iterator iter = childItems.begin(); iter != childItems.end(); ++iter)
				{
					if(boost::shared_ptr<XdmfArray> array = boost::shared_dynamic_cast<XdmfArray>(*iter))
					{
						if(!origin)
						{
							origin = array;
						}
						else if(!brickSize)
						{
							brickSize = array;
							break;
						}
					}
				}
				if(origin && brickSize)
				{
					return XdmfGridRegular::New(brickSize, boost::shared_ptr<XdmfArray>(), origin);
				}
				return boost::shared_ptr<XdmfItem>();
			}
85
86
87
88
89
90
91
92
93
94
95
96
			else if(typeVal.compare("VXVY") == 0 || typeVal.compare("VXVYVZ") == 0)
			{
				std::vector<boost::shared_ptr<XdmfArray> > coordinateValues;
				for(std::vector<boost::shared_ptr<XdmfItem> >::const_iterator iter = childItems.begin(); iter != childItems.end(); ++iter)
				{
					if(boost::shared_ptr<XdmfArray> array = boost::shared_dynamic_cast<XdmfArray>(*iter))
					{
						coordinateValues.push_back(array);
					}
				}
				return XdmfGridRectilinear::New(coordinateValues);
			}
97
		}
98
		return XdmfGeometry::New();
99
100
101
	}
	else if(itemTag.compare(XdmfGrid::ItemTag) == 0)
	{
102
		// For backwards compatibility with the old format, this tag can correspond to multiple XdmfItems.
103
104
105
		std::map<std::string, std::string>::const_iterator gridType = itemProperties.find("GridType");
		if(gridType != itemProperties.end() && gridType->second.compare("Collection") == 0)
		{
106
			return XdmfGridCollection::New();
107
108
109
		}
		else
		{
110
111
112
			// Find out what kind of grid we have
			for(std::vector<boost::shared_ptr<XdmfItem> >::const_iterator iter = childItems.begin(); iter != childItems.end(); ++iter)
			{
113
114
115
116
117
				if(boost::shared_ptr<XdmfGridCurvilinear> curvilinear = boost::shared_dynamic_cast<XdmfGridCurvilinear>(*iter))
				{
					return XdmfGridCurvilinear::New(0, 0);
				}
				else if(boost::shared_ptr<XdmfGridRegular> regularGrid = boost::shared_dynamic_cast<XdmfGridRegular>(*iter))
118
				{
119
					return XdmfGridRegular::New(0, 0, 0, 0, 0, 0);
120
				}
121
122
123
124
125
				else if(boost::shared_ptr<XdmfGridRectilinear> rectilinearGrid = boost::shared_dynamic_cast<XdmfGridRectilinear>(*iter))
				{
					std::vector<boost::shared_ptr<XdmfArray> > coordinateValues;
					return XdmfGridRectilinear::New(coordinateValues);
				}
126
			}
127
			return XdmfGridUnstructured::New();
128
129
		}
	}
130
131
	else if(itemTag.compare(XdmfInformation::ItemTag) == 0)
	{
132
		return XdmfInformation::New();
133
	}
134
135
	else if(itemTag.compare(XdmfMap::ItemTag) == 0)
	{
136
		return XdmfMap::New();
137
	}
138
139
	else if(itemTag.compare(XdmfSet::ItemTag) == 0)
	{
140
		return XdmfSet::New();
141
	}
142
143
	else if(itemTag.compare(XdmfTime::ItemTag) == 0)
	{
144
		return XdmfTime::New();
145
	}
146
147
	else if(itemTag.compare(XdmfTopology::ItemTag) == 0)
	{
148
149
150
151
152
153
154
155
156
157
		std::map<std::string, std::string>::const_iterator type = itemProperties.find("Type");
		if(type == itemProperties.end())
		{
			type = itemProperties.find("TopologyType");
		}

		if(type != itemProperties.end())
		{
			std::string typeVal = type->second;
			std::transform(typeVal.begin(), typeVal.end(), typeVal.begin(), (int(*)(int))std::toupper);
158
159
			if(typeVal.compare("2DCORECTMESH") == 0 || typeVal.compare("3DCORECTMESH") == 0
				|| typeVal.compare("2DSMESH") == 0 || typeVal.compare("3DSMESH") == 0)
160
161
162
163
164
165
166
167
168
169
170
171
172
			{
				boost::shared_ptr<XdmfArray> dimensionsArray = XdmfArray::New();
				std::string dimensionsString = "";
				std::map<std::string, std::string>::const_iterator dimensions = itemProperties.find("Dimensions");
				if(dimensions != itemProperties.end())
				{
					dimensionsString = dimensions->second;
				}
				boost::tokenizer<> tokens(dimensionsString);
				for(boost::tokenizer<>::const_iterator iter = tokens.begin(); iter != tokens.end(); ++iter)
				{
					dimensionsArray->pushBack<unsigned int>(atoi((*iter).c_str()));
				}
173
174
175
176
177
178
179
180
181
182
183
184
185
				if(typeVal.compare("2DCORECTMESH") == 0 || typeVal.compare("3DCORECTMESH") == 0)
				{
					return XdmfGridRegular::New(boost::shared_ptr<XdmfArray>(), dimensionsArray, boost::shared_ptr<XdmfArray>());
				}
				else
				{
					return XdmfGridCurvilinear::New(dimensionsArray);
				}
			}
			else if(typeVal.compare("2DRECTMESH") == 0 || typeVal.compare("3DRECTMESH") == 0)
			{
				std::vector<boost::shared_ptr<XdmfArray> > coordinateValues;
				return XdmfGridRectilinear::New(coordinateValues);
186
			}
187

188
		}
189
		return XdmfTopology::New();
190
	}
191
	return boost::shared_ptr<XdmfItem>();
192
}