-
Notifications
You must be signed in to change notification settings - Fork 0
/
Attribute.h
333 lines (311 loc) · 7.42 KB
/
Attribute.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* @file Attribute.h
* @author Dan R. Lipsa
* @brief Attributes that can be attached to vertices, edges, faces and bodies.
* @ingroup data model
*/
#ifndef __ATTRIBUTE_H__
#define __ATTRIBUTE_H__
#include "ParsingEnums.h"
/**
* @brief Attributes that can be attached to vertices, edges, faces and bodies.
*/
class Attribute
{
public:
enum Type
{
INTEGER,
REAL,
COLOR,
INTEGER_ARRAY,
REAL_ARRAY,
ATTRIBUTE_ARRAY,
COUNT
};
public:
/**
* Virtual destructor for attributes (so that everything gets deleted from
* derived classes)
*/
virtual ~Attribute () {}
/**
* Virtual function that knows how to print an attribute
* @param ostr where to print
* @return where to print something else
*/
virtual ostream& Print(ostream& ostr) const
{
return ostr << "Attribute placeholder" << endl;
}
virtual Type GetType () const
{
return COUNT;
}
};
/**
* @brief An integer attribute that can be attached to
* vertices, edges, faces and bodies.
*/
class IntegerAttribute : public Attribute
{
public:
typedef int value_type;
/**
* Constructor
* @param value the value of the attribute
*/
IntegerAttribute (int value) : m_value (value) {}
/**
* Pretty prints an integer attribute
*/
virtual ostream& Print (ostream& ostr) const
{
return ostr << m_value;
}
virtual Type GetType () const
{
return INTEGER;
}
operator int()
{
return m_value;
}
private:
/**
* Value of the integer attribute
*/
int m_value;
};
/**
* @brief A real attribute that can be attached to
* vertices, edges, faces and bodies.
*/
class RealAttribute : public Attribute
{
public:
typedef double value_type;
/**
* Constructor for the attribute with a real value
* @param value the value of the attribute
*/
RealAttribute (double value) : m_value (value) {}
/**
* Destructor for the real attribute
*/
virtual ~RealAttribute () {}
/**
* Pretty print the attribute
* @param ostr stream where to print
* @return stream where we printed
*/
virtual ostream& Print (ostream& ostr) const
{
return ostr << m_value;
}
virtual Type GetType () const
{
return REAL;
}
operator double ()
{
return m_value;
}
void set (double value)
{
m_value = value;
}
private:
/**
* The value of the attribute
*/
double m_value;
};
/**
* @brief A color attribute that can be attached to
* vertices, edges, faces and bodies.
*/
class ColorAttribute : public Attribute
{
public:
typedef Color::Enum value_type;
/**
* Constructor for the color attribute
* @param color the value of the attribute
*/
ColorAttribute (Color::Enum color) : m_color (color) {}
/**
* Pretty print
* @param ostr where to print
* @return where we printed
*/
virtual ostream& Print (ostream& ostr) const
{
return ostr << m_color;
}
virtual Type GetType () const
{
return COLOR;
}
/**
* Get the color of the attribute
* @return color value of the attribute
*/
operator Color::Enum ()
{
return m_color;
}
void set (Color::Enum color)
{
m_color = color;
}
private:
/**
* Color value of the attribute
*/
Color::Enum m_color;
};
/**
* @brief An array of integers attribute that can be attached to
* vertices, edges, faces and bodies.
*/
class IntegerArrayAttribute : public Attribute
{
public:
typedef const vector<int> value_type;
/**
* Constructs an integer array attribute
* @param values pointer to vector of integers.
* WARNING: it takes ownership of the values vector.
*/
IntegerArrayAttribute (vector<int>* values)
{
m_values.reset (values);
}
IntegerArrayAttribute (value_type values)
{
m_values.reset (new vector<int> (values));
}
void set (value_type values)
{
*m_values = values;
}
/**
* Pretty print an array of integers attributes
* @param ostr where to print
* @return where to print the next item
*/
virtual ostream& Print (ostream& ostr) const;
virtual Type GetType () const
{
return INTEGER_ARRAY;
}
operator const vector<int> ()
{
return *m_values;
}
private:
/**
* Stores a pointer to an array of integers
*/
boost::shared_ptr< vector<int> > m_values;
};
/**
* @brief An array of reals attribute that can be attached to
* vertices, edges, faces and bodies.
*/
class RealArrayAttribute : public Attribute
{
public:
/**
* Constructs an attribute that stores an array of reals
* @param values pointer to an array of reals.
* WARNING: Takes ownership of values vector
*/
RealArrayAttribute (vector<double>* values)
{
m_values.reset (values);
}
/**
* Pretty prints the attribute
* @param ostr where to print
* @return where we printed
*/
virtual ostream& Print (ostream& ostr) const;
virtual Type GetType () const
{
return REAL_ARRAY;
}
private:
boost::shared_ptr< vector<double> > m_values;
};
/**
* @brief An array of attributes that can be attached
* to vertices, edges, faces and bodies.
*
* This is used for multidimensional arrays. The last level stores
* RealAttributes
*/
class AttributeArrayAttribute : public Attribute
{
public:
/**
* Constructs an attribute that stores an array of attributes
*/
AttributeArrayAttribute ();
// takes ownership of the pointer.
AttributeArrayAttribute (Attribute* element);
AttributeArrayAttribute (size_t n, double value);
AttributeArrayAttribute (size_t n);
void CheckDimensions (const vector<size_t>* dimensions) const;
void GetDimensions (vector<size_t>* dimensions) const;
static AttributeArrayAttribute* NewArray (vector<size_t>* dimensions);
double Get (const vector<size_t>& index) const;
void AddElement (Attribute* element)
{
m_values->push_back (boost::shared_ptr<Attribute> (element));
}
/**
* Pretty prints the attribute
* @param ostr where to print
* @return where we printed
*/
virtual ostream& Print (ostream& ostr) const;
virtual Type GetType () const
{
return ATTRIBUTE_ARRAY;
}
size_t size ()
{
return m_values->size ();
}
private:
void setElement (size_t i, Attribute* element)
{
(*m_values)[i] = boost::shared_ptr<Attribute> (element);
}
boost::shared_ptr<Attribute> getElement (size_t i) const
{
return (*m_values)[i];
}
void checkDimensions (const vector<size_t>* dimensions,
size_t currentDimensionIndex) const;
void getDimensions (vector<size_t>* dimensions,
size_t currentDimensionIndex) const;
static AttributeArrayAttribute* newArray (vector<size_t>* dimensions,
size_t currentDimensionIndex);
private:
/**
* Pointer to a vector of attributes which are either values or vectors
*/
boost::shared_ptr< vector<boost::shared_ptr<Attribute> > > m_values;
};
/**
* Knows how to print an Attribute
*/
ostream& operator<< (ostream& ostr, const Attribute& attribute);
ostream& operator<< (ostream& ostr, const Attribute* attribute);
#endif //__ATTRIBUTE_H__
// Local Variables:
// mode: c++
// End: