-
Notifications
You must be signed in to change notification settings - Fork 0
/
Attribute.cpp
171 lines (147 loc) · 4.88 KB
/
Attribute.cpp
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
/**
* @file Attribute.cpp
* @author Dan R. Lipsa
*
* Contains definition of class members for all types of attributes which
* can be attached to vertices, edges, faces and bodies.
*/
#include "Attribute.h"
#include "Debug.h"
ostream& IntegerArrayAttribute::Print (ostream& ostr) const
{
ostr << "[";
ostream_iterator<int> o (ostr, " ");
copy (m_values->begin (), m_values->end (), o);
ostr << "]";
return ostr;
}
ostream& RealArrayAttribute::Print (ostream& ostr) const
{
ostr << "[";
ostream_iterator<double> o (ostr, " ");
copy (m_values->begin (), m_values->end (), o);
ostr << "]";
return ostr;
}
// AttributeArrayAttribute
// ======================================================================
AttributeArrayAttribute::AttributeArrayAttribute ()
{
m_values.reset (new vector< boost::shared_ptr<Attribute> > ());
}
AttributeArrayAttribute::AttributeArrayAttribute (Attribute* element)
{
m_values.reset (new vector< boost::shared_ptr<Attribute> > (
1, boost::shared_ptr<Attribute> (element)));
}
AttributeArrayAttribute::AttributeArrayAttribute (size_t n, double value)
{
m_values.reset (new vector< boost::shared_ptr<Attribute> > (n));
for (size_t i = 0; i < m_values->size (); ++i)
(*m_values)[i] =
boost::shared_ptr<Attribute> (new RealAttribute (value));
}
AttributeArrayAttribute::AttributeArrayAttribute (size_t n)
{
m_values.reset (new vector< boost::shared_ptr<Attribute> > (n));
}
ostream& AttributeArrayAttribute::Print (ostream& ostr) const
{
ostr << "[";
vector< boost::shared_ptr<Attribute> >& va = *m_values;
BOOST_FOREACH (boost::shared_ptr<Attribute> atr, va)
{
atr->Print (ostr);
}
ostr << "]";
return ostr;
}
void AttributeArrayAttribute::CheckDimensions (
const vector<size_t>* dimensions) const
{
checkDimensions (dimensions, 0);
}
void AttributeArrayAttribute::GetDimensions (vector<size_t>* dimensions) const
{
getDimensions (dimensions, 0);
}
AttributeArrayAttribute* AttributeArrayAttribute::NewArray (
vector<size_t>* dimensions)
{
return newArray (dimensions, 0);
}
void AttributeArrayAttribute::checkDimensions (
const vector<size_t>* dimensions, size_t currentDimensionIndex) const
{
size_t dimensionsSize = dimensions->size ();
if (currentDimensionIndex >= dimensionsSize)
ThrowException ("Invalid index: ", currentDimensionIndex,
"should be less than ", dimensionsSize);
size_t dimension = m_values->size ();
size_t expectedDimension = (*dimensions)[currentDimensionIndex];
if (dimension != expectedDimension)
ThrowException ("Wrong array dimension: ", dimension,
" expected: ", expectedDimension);
if (dimensionsSize - 1 == currentDimensionIndex)
return;
BOOST_FOREACH (boost::shared_ptr<Attribute> attribute, *m_values)
{
boost::static_pointer_cast<AttributeArrayAttribute> (
attribute)->checkDimensions (
dimensions, currentDimensionIndex + 1);
}
}
void AttributeArrayAttribute::getDimensions (
vector<size_t>* dimensions, size_t currentDimensionIndex) const
{
size_t dimensionsSize = dimensions->size ();
if (currentDimensionIndex >= dimensionsSize)
dimensions->resize (currentDimensionIndex + 1);
size_t dimension = m_values->size ();
(*dimensions)[currentDimensionIndex] = dimension;
if ((*m_values)[0]->GetType () == REAL)
return;
// assume dimensions for all other indexes are the same
boost::static_pointer_cast<AttributeArrayAttribute> ((*m_values)[0])->
getDimensions (dimensions, currentDimensionIndex + 1);
}
AttributeArrayAttribute* AttributeArrayAttribute::newArray (
vector<size_t>* dimensions, size_t currentDimensionIndex)
{
size_t dimensionsSize = dimensions->size ();
if (currentDimensionIndex >= dimensionsSize)
ThrowException ("Invalid index: ", currentDimensionIndex,
"should be less than ", dimensionsSize);
if (currentDimensionIndex == dimensionsSize - 1)
return new AttributeArrayAttribute (
(*dimensions)[currentDimensionIndex], 0.0);
else
{
AttributeArrayAttribute* array =
new AttributeArrayAttribute ((*dimensions)[currentDimensionIndex]);
for (size_t i = 0; i < array->size (); ++i)
array->setElement (
i, newArray (dimensions, currentDimensionIndex + 1));
return array;
}
}
double AttributeArrayAttribute::Get (const vector<size_t>& index) const
{
const AttributeArrayAttribute* current = this;
boost::shared_ptr<Attribute> p;
for (size_t i = 0; i < index.size (); ++i)
{
p = current->getElement (index[i]);
current = boost::static_pointer_cast<AttributeArrayAttribute> (p).get ();
}
return static_cast<double> (
(*boost::static_pointer_cast<RealAttribute> (p)));
}
ostream& operator<< (ostream& ostr, const Attribute& attribute)
{
return attribute.Print(ostr);
}
ostream& operator<< (ostream& ostr, const Attribute* attribute)
{
return ostr << *attribute;
}