1 | // |
---|
2 | // class for 1D grid |
---|
3 | // |
---|
4 | |
---|
5 | #include "grid1d.h" |
---|
6 | |
---|
7 | |
---|
8 | // constructor |
---|
9 | RpGrid1d::RpGrid1d() |
---|
10 | { |
---|
11 | } |
---|
12 | |
---|
13 | RpGrid1d::RpGrid1d(const char* name) |
---|
14 | { |
---|
15 | objectName(name); |
---|
16 | } |
---|
17 | |
---|
18 | RpGrid1d::RpGrid1d(int size) |
---|
19 | { |
---|
20 | m_data.reserve(size); |
---|
21 | } |
---|
22 | |
---|
23 | // constructor with object name |
---|
24 | RpGrid1d::RpGrid1d(const char* name, int size) |
---|
25 | { |
---|
26 | m_data.reserve(size); |
---|
27 | objectName(name); |
---|
28 | } |
---|
29 | |
---|
30 | // |
---|
31 | // instantiate grid with a 1d array of doubles and number of items |
---|
32 | // |
---|
33 | RpGrid1d::RpGrid1d(DataValType* val, int nitems) |
---|
34 | { |
---|
35 | if (val == NULL) // invalid data pointer |
---|
36 | return; |
---|
37 | |
---|
38 | // copy array val into m_data |
---|
39 | for (int i=0; i<nitems; i++) |
---|
40 | m_data.push_back(val[i]); |
---|
41 | } |
---|
42 | |
---|
43 | // |
---|
44 | // constructor for a regular grid |
---|
45 | // |
---|
46 | RpGrid1d::RpGrid1d(DataValType startPoint, DataValType delta, int npts) |
---|
47 | { |
---|
48 | // expand array |
---|
49 | for (int i=0; i < npts; i++) |
---|
50 | m_data.push_back(startPoint + i*delta); |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | // |
---|
55 | // Add all points to grid1d object at once |
---|
56 | // |
---|
57 | RP_ERROR |
---|
58 | RpGrid1d::addAllPoints(DataValType* val, int nitems) |
---|
59 | { |
---|
60 | if (val == NULL) |
---|
61 | return RP_ERR_NULL_PTR; |
---|
62 | |
---|
63 | m_data.clear(); |
---|
64 | m_data.reserve(nitems); |
---|
65 | |
---|
66 | for (int i=0; i < nitems; i++) |
---|
67 | m_data.push_back(val[i]); |
---|
68 | |
---|
69 | return RP_SUCCESS; |
---|
70 | } |
---|
71 | |
---|
72 | // |
---|
73 | // add one point to grid1d |
---|
74 | // |
---|
75 | void RpGrid1d::addPoint(DataValType val) |
---|
76 | { |
---|
77 | m_data.push_back(val); |
---|
78 | } |
---|
79 | |
---|
80 | // |
---|
81 | // serialize Grid1d object |
---|
82 | // 16 bytes: Header (including RV identifier, version, object type id) |
---|
83 | // e.g., RV-A-FIELD, RV-A-MESH3D |
---|
84 | // 4 bytes: N: number of bytes in object name (to follow) |
---|
85 | // N bytes: object name (e.g., "output.grid(g1d))" |
---|
86 | // 4 bytes: number of points in array |
---|
87 | // rest: data array (x x x...) |
---|
88 | // |
---|
89 | char* |
---|
90 | RpGrid1d::serialize(int& nb) |
---|
91 | { |
---|
92 | char* buf; |
---|
93 | int nbytes = numBytes(); |
---|
94 | |
---|
95 | if ( (buf = new char[nbytes]) == NULL) { |
---|
96 | RpAppendErr("RpGrid1d::serialize: new char[] failed"); |
---|
97 | RpPrintErr(); |
---|
98 | return buf; |
---|
99 | } |
---|
100 | |
---|
101 | doSerialize(buf, nbytes); |
---|
102 | |
---|
103 | nb = nbytes; |
---|
104 | |
---|
105 | return buf; |
---|
106 | } |
---|
107 | |
---|
108 | // |
---|
109 | // serialize object data in Little Endian byte order |
---|
110 | // |
---|
111 | RP_ERROR |
---|
112 | RpGrid1d::doSerialize(char* buf, int nbytes) |
---|
113 | { |
---|
114 | // check buffer |
---|
115 | if (buf == NULL || nbytes < numBytes()) { |
---|
116 | RpAppendErr("RpGrid1d::serialize: invalid buffer"); |
---|
117 | RpPrintErr(); |
---|
118 | return RP_ERR_INVALID_ARRAY; |
---|
119 | } |
---|
120 | |
---|
121 | char * ptr = buf; |
---|
122 | |
---|
123 | // write object header (version and typename) |
---|
124 | writeRpHeader(ptr, RpCurrentVersion[GRID1D], nbytes); |
---|
125 | ptr += HEADER_SIZE + sizeof(int); |
---|
126 | |
---|
127 | // write object name and its length |
---|
128 | writeString(ptr, m_name); |
---|
129 | ptr += m_name.size() + sizeof(int); |
---|
130 | |
---|
131 | // write number of points and array data |
---|
132 | writeArrayDouble(ptr, m_data, m_data.size()); |
---|
133 | |
---|
134 | return RP_SUCCESS; |
---|
135 | } |
---|
136 | |
---|
137 | RP_ERROR |
---|
138 | RpGrid1d::deserialize(const char* buf) |
---|
139 | { |
---|
140 | if (buf == NULL) { |
---|
141 | RpAppendErr("RpGrid1d::deserialize: null buf pointer"); |
---|
142 | RpPrintErr(); |
---|
143 | return RP_ERR_NULL_PTR; |
---|
144 | } |
---|
145 | |
---|
146 | char* ptr = (char*)buf; |
---|
147 | std::string header; |
---|
148 | int nbytes; |
---|
149 | |
---|
150 | readRpHeader(ptr, header, nbytes); |
---|
151 | ptr += HEADER_SIZE + sizeof(int); |
---|
152 | |
---|
153 | if (header == RpCurrentVersion[GRID1D]) |
---|
154 | return doDeserialize(ptr); |
---|
155 | |
---|
156 | // deal with older versions |
---|
157 | return RP_FAILURE; |
---|
158 | } |
---|
159 | |
---|
160 | // |
---|
161 | // parse out the buffer, |
---|
162 | // stripped off the version and total #bytes already. |
---|
163 | // |
---|
164 | RP_ERROR |
---|
165 | RpGrid1d::doDeserialize(const char* buf) |
---|
166 | { |
---|
167 | char* ptr = (char*)buf; |
---|
168 | |
---|
169 | // parse object name and store name in m_name |
---|
170 | |
---|
171 | readString(ptr, m_name); |
---|
172 | ptr += sizeof(int) + m_name.size(); |
---|
173 | |
---|
174 | int npts; |
---|
175 | readArrayDouble(ptr, m_data, npts); |
---|
176 | |
---|
177 | return RP_SUCCESS; |
---|
178 | } |
---|
179 | |
---|
180 | // |
---|
181 | // return pointer to data points |
---|
182 | // |
---|
183 | DataValType* |
---|
184 | RpGrid1d::getData() |
---|
185 | { |
---|
186 | return &m_data[0]; |
---|
187 | } |
---|
188 | |
---|
189 | // |
---|
190 | // return pointer to a copy of data points in consecutive memory locations |
---|
191 | // |
---|
192 | DataValType* |
---|
193 | RpGrid1d::getDataCopy() |
---|
194 | { |
---|
195 | int nitems = m_data.size(); |
---|
196 | |
---|
197 | DataValType* xy = new DataValType[nitems]; |
---|
198 | if ( xy == NULL) { |
---|
199 | RpAppendErr("RpGrid1d::data: new failed"); |
---|
200 | RpPrintErr(); |
---|
201 | return xy; |
---|
202 | } |
---|
203 | |
---|
204 | for (int i=0; i < nitems; i++) |
---|
205 | xy[i] = m_data[i]; |
---|
206 | |
---|
207 | return xy; |
---|
208 | } |
---|
209 | |
---|
210 | // |
---|
211 | // mashalling object into xml string |
---|
212 | // |
---|
213 | void |
---|
214 | RpGrid1d::xmlString(std::string& textString) |
---|
215 | { |
---|
216 | int i; |
---|
217 | int npts = m_data.size(); |
---|
218 | char cstr[256]; |
---|
219 | |
---|
220 | // clear input string |
---|
221 | textString.clear(); |
---|
222 | |
---|
223 | textString.append("<points>"); |
---|
224 | |
---|
225 | for (i=0; i < npts; i++) { |
---|
226 | sprintf(cstr, "\t%.15f\n", m_data[i]); |
---|
227 | textString.append(cstr); |
---|
228 | } |
---|
229 | textString.append("</points>\n"); |
---|
230 | } |
---|
231 | |
---|
232 | // |
---|
233 | // print the xml string from the object |
---|
234 | // |
---|
235 | void |
---|
236 | RpGrid1d::print() |
---|
237 | { |
---|
238 | string str; |
---|
239 | |
---|
240 | printf("object name: %s\n", m_name.c_str()); |
---|
241 | printf("num points: %d\n", numPoints()); |
---|
242 | |
---|
243 | xmlString(str); |
---|
244 | |
---|
245 | printf("%s", str.c_str()); |
---|
246 | } |
---|
247 | |
---|
248 | // |
---|
249 | // set object name from a charactor string |
---|
250 | // |
---|
251 | void |
---|
252 | RpGrid1d::objectName(const char* str) |
---|
253 | { |
---|
254 | m_name.assign(str); |
---|
255 | } |
---|
256 | |
---|
257 | // |
---|
258 | // get object name |
---|
259 | // |
---|
260 | const char* RpGrid1d::objectName() |
---|
261 | { |
---|
262 | return m_name.c_str(); |
---|
263 | } |
---|
264 | |
---|
265 | // |
---|
266 | // get object type |
---|
267 | // |
---|
268 | const char* RpGrid1d::objectType() |
---|
269 | { |
---|
270 | return RpObjectTypes[GRID1D]; |
---|
271 | } |
---|
272 | |
---|
273 | // |
---|
274 | // return total number of bytes when mashalling out as bytes |
---|
275 | // |
---|
276 | int RpGrid1d::numBytes() |
---|
277 | { |
---|
278 | int nbytes = HEADER_SIZE |
---|
279 | + sizeof(int) // total #bytes |
---|
280 | + sizeof(int) // #bytes in name |
---|
281 | + m_name.size() |
---|
282 | + sizeof(int) // #points in grid |
---|
283 | + m_data.size() * sizeof(DataValType); |
---|
284 | |
---|
285 | return nbytes; |
---|
286 | } |
---|
287 | |
---|
288 | // |
---|
289 | // Remove all points |
---|
290 | // |
---|
291 | void RpGrid1d::clear() |
---|
292 | { |
---|
293 | m_data.clear(); |
---|
294 | m_name.clear(); |
---|
295 | } |
---|
296 | |
---|
297 | // |
---|
298 | // override operator = |
---|
299 | // |
---|
300 | RpGrid1d RpGrid1d::operator=(const RpGrid1d& g) |
---|
301 | { |
---|
302 | m_data = g.m_data; |
---|
303 | m_name = g.m_name; |
---|
304 | |
---|
305 | return (*this); |
---|
306 | } |
---|
307 | |
---|
308 | // |
---|
309 | // retrieve x y values at index |
---|
310 | // |
---|
311 | DataValType RpGrid1d::getData(int index) |
---|
312 | { |
---|
313 | return m_data.at(index); |
---|
314 | } |
---|
315 | |
---|
316 | void |
---|
317 | RpGrid1d::copyArray(DataValType* val, int dim, vector<DataValType>& vec) |
---|
318 | { |
---|
319 | vec.clear(); |
---|
320 | |
---|
321 | for (int i=0; i < dim; i++) |
---|
322 | vec.push_back(val[i]); |
---|
323 | } |
---|
324 | |
---|