forked from twiniars/RESpecTa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coordinates.h
213 lines (196 loc) · 6.22 KB
/
Coordinates.h
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
class Coordinates;
#ifndef COORDINATES_H
#define COORDINATES_H
#include "globals.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include "Pose.h"
QT_BEGIN_NAMESPACE
class QString;
class QXmlStreamReader;
QT_END_NAMESPACE
/**
* Class representing the Coordinates in the RunGenerator state.
*/
class Coordinates
{
public:
Coordinates(){}
Coordinates(Coordinates& old)
{
this->coordType=old.coordType;
this->motionType=old.motionType;
Pose * x;
for(unsigned int i=0;i<old.poses.size();i++)
{
x=new Pose(*(old.poses[i]));
this->poses.push_back(x);
}
}
~Coordinates()
{
for(unsigned int i = 0;i<poses.size();i++)
{
delete poses[i];
}
}
bool equals(Coordinates * other)
{
if(this->coordType==other->coordType)
{
if(this->motionType==other->motionType)
{
if(this->poses.size()==other->poses.size())
{
for(int i=0;i<poses.size();i++)
{
if(!poses[i]->equals(other->poses[i]))
return false;
}
return true;
}
}
}
return false;
}
CoordType getCoordType() {return coordType;}
void setCoordType(CoordType newCoordType) {coordType=newCoordType;}
MotionType getMotionType() {return motionType;}
void setMotionType(MotionType newMotionType) {motionType=newMotionType;}
std::vector<Pose *> getPoses(){return poses;}
void setPoses(std::vector<Pose *> newPoses){poses=newPoses;}
/**
* Creates a string describing the coordinates attributes.
* @returns String with the description of the Coordinates
*/
std::string Print()
{
std::string x;
if(poses.size()>0 )
{
x+="\nMotion Type:";
x+=MOTION_TYPE_TABLE[motionType];
x+="\nCoord Type:";
x+=COORD_TYPE_TABLE[coordType];
foreach(Pose * pos, poses)
{
x+=pos->Print();
}
}
return x;
}
/**
* Writes the data of the state to the XML stream.
* @param writer Stream to which the data is written
*/
void Print(QXmlStreamWriter * writer)
{
writer->writeStartElement("Trajectory");
writer->writeAttribute(QString("coordinateType"), QString().fromStdString(COORD_TYPE_TABLE[coordType]));
writer->writeAttribute(QString("motionType"), QString().fromStdString(MOTION_TYPE_TABLE[motionType]));
foreach(Pose * pos, poses)
{
pos->Print();
}
writer->writeEndElement();
}
/**
* Loads from XML Stream the data and passes it to the Poses(if any).
* @param reader Stream from which the data is read
* @returns List of errors, which occured while loading
*/
QStringList LoadFromXML(QXmlStreamReader * reader)
{
QStringList errors;
if(reader->attributes().hasAttribute("coordinateType")&&reader->attributes().hasAttribute("motionType"))
{
CoordType Cindex = (CoordType)(getCoordTypeTable().indexOf(reader->attributes().value("coordinateType").toString()));
if(isProper(Cindex))
{
coordType = Cindex;
}
else
{
coordType = (CoordType)0;
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("Out of bounds CoordType")+=linenum);
}
MotionType Mindex = (MotionType)(getMotionTypeTable().indexOf(reader->attributes().value("motionType").toString()));
if(isProper(Mindex))
{
motionType = Mindex;
}
else
{
motionType = (MotionType)0;
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("Out of bounds MotionType")+=linenum);
}
}
else
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("The trajectory has no coordinateType or motionType attribute")+=linenum);
return errors;
}
while (!reader->atEnd())
{
if(reader->name().toString()=="Trajectory"&&reader->isEndElement())
{
return errors;
}
reader->readNextStartElement();
if(reader->name().toString()=="Trajectory"&&reader->isEndElement())
{
return errors;
}
else if (reader->name()=="Pose"&&reader->isStartElement())
{
bool mark = true;
Pose * tmp = new Pose();
errors+=tmp->LoadFromXML(reader);
if(poses.size()>0)
{
Pose * pose = poses[0];
if(pose->getA().size()!=tmp->getA().size())
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("The new pose has a different vectors length than the previous one: ")+=linenum);
mark = false;
}
}
if(mark==true)
{
poses.push_back(tmp);
}
}
else if (reader->isStartElement())
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back((QString("unexpected name while reading <coordinates>: ")+=reader->name())+=linenum);
}
}
return errors;
}
private:
/**
* CoordinateType of the coordinates.
*/
CoordType coordType;
/**
* MotionType of the coordinates.
*/
MotionType motionType;
/**
* Vector of Poses of the coordinates.
*/
std::vector<Pose *> poses;
};
#endif // COORDINATES_H