-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitManager.cpp
More file actions
190 lines (150 loc) · 5.01 KB
/
Copy pathUnitManager.cpp
File metadata and controls
190 lines (150 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/***********************************************************************
UnitManager - Class to manage the initialization and creation of
structural unit classes
Copyright (c) 2005-2024 Oliver Kreylos
The Nanotech Construction Kit is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Nanotech Construction Kit 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
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Nanotech Construction Kit; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include <Misc/StdError.h>
#include <Misc/File.h>
#include <Misc/StandardValueCoders.h>
#include <Misc/ConfigurationFile.h>
#include "Triangle.h"
#include "Tetrahedron.h"
#include "Octahedron.h"
#include "Cylinder.h"
#include "Sphere.h"
#include "UnitManager.h"
namespace NCK {
/****************************
Methods of class UnitManager:
****************************/
int UnitManager::getNumUnitTypes(void)
{
return NUM_UNITTYPES;
}
const char* UnitManager::getUnitTypeName(int unitTypeIndex)
{
static const char* unitTypeNames[]={"Triangle","Tetrahedron","Octahedron","Cylinder","Sphere"};
return unitTypeNames[unitTypeIndex];
}
Scalar UnitManager::initializeUnitTypes(const Misc::ConfigurationFileSection& configFileSection)
{
/* Initialize base class: */
StructuralUnit::initClass(configFileSection);
/* Initialize triangle class: */
Triangle::initClass(configFileSection.getSection("./Triangle"));
/* Initialize tetrahedron class: */
Tetrahedron::initClass(configFileSection.getSection("./Tetrahedron"));
/* Initialize octahedron class: */
Octahedron::initClass(configFileSection.getSection("./Octahedron"));
/* Initialize cylinder class: */
Cylinder::initClass(configFileSection.getSection("./Cylinder"));
/* Initialize sphere class: */
Sphere::initClass(configFileSection.getSection("./Sphere"));
/* Determine radius of largest structural unit: */
Scalar maxUnitRadius=Triangle::getClassRadius();
if(maxUnitRadius<Tetrahedron::getClassRadius())
maxUnitRadius=Tetrahedron::getClassRadius();
if(maxUnitRadius<Octahedron::getClassRadius())
maxUnitRadius=Octahedron::getClassRadius();
if(maxUnitRadius<Cylinder::getClassRadius())
maxUnitRadius=Cylinder::getClassRadius();
if(maxUnitRadius<Sphere::getClassRadius())
maxUnitRadius=Sphere::getClassRadius();
return maxUnitRadius;
}
void UnitManager::deinitializeUnitTypes(void)
{
/* Deinitialize sphere class: */
Sphere::deinitClass();
/* Deinitialize cylinder class: */
Cylinder::deinitClass();
/* Deinitialize octahedron class: */
Octahedron::deinitClass();
/* Deinitialize tetrahedron class: */
Tetrahedron::deinitClass();
/* Deinitialize triangle class: */
Triangle::deinitClass();
/* Deinitialize base class: */
StructuralUnit::deinitClass();
}
StructuralUnit* UnitManager::createUnit(int unitType,const Point& position,const Rotation& orientation)
{
switch(unitType)
{
case TRIANGLE:
return new Triangle(position,orientation);
break;
case TETRAHEDRON:
return new Tetrahedron(position,orientation);
break;
case OCTAHEDRON:
return new Octahedron(position,orientation);
break;
case CYLINDER:
return new Cylinder(position,orientation);
break;
case SPHERE:
return new Sphere(position,orientation);
break;
default:
throw Misc::makeStdErr(__PRETTY_FUNCTION__,"Unknown unit type %d",unitType);
}
}
StructuralUnit* UnitManager::readUnit(Misc::File& file)
{
/* Determine unit's type: */
int unitType=file.read<int>();
/* Create and return new unit: */
switch(unitType)
{
case TRIANGLE:
return new Triangle(file);
break;
case TETRAHEDRON:
return new Tetrahedron(file);
break;
case OCTAHEDRON:
return new Octahedron(file);
break;
case CYLINDER:
return new Cylinder(file);
break;
case SPHERE:
return new Sphere(file);
break;
default:
throw Misc::makeStdErr(__PRETTY_FUNCTION__,"Unknown unit type %d encountered in file",unitType);
}
}
void UnitManager::writeUnit(StructuralUnit* unit,Misc::File& file)
{
/* Determine unit's type: */
int unitType=-1;
if(dynamic_cast<Triangle*>(unit)!=0)
unitType=TRIANGLE;
else if(dynamic_cast<Tetrahedron*>(unit)!=0)
unitType=TETRAHEDRON;
else if(dynamic_cast<Octahedron*>(unit)!=0)
unitType=OCTAHEDRON;
else if(dynamic_cast<Cylinder*>(unit)!=0)
unitType=CYLINDER;
else if(dynamic_cast<Sphere*>(unit)!=0)
unitType=SPHERE;
if(unitType<0)
throw Misc::makeStdErr(__PRETTY_FUNCTION__,"Unit of unkown type encountered");
/* Write unit's state to file: */
file.write<int>(unitType);
unit->writeState(file);
}
}