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(int size) |
---|
14 | { |
---|
15 | m_data.reserve(size); |
---|
16 | } |
---|
17 | |
---|
18 | // construct a grid object from a byte stream |
---|
19 | RpGrid1d::RpGrid1d(const char * buf) |
---|
20 | { |
---|
21 | deserialize(buf); |
---|
22 | } |
---|
23 | |
---|
24 | // |
---|
25 | // instantiate grid with a 1d array of doubles and number of items |
---|
26 | // |
---|
27 | RpGrid1d::RpGrid1d(DataValType* val, int nitems) |
---|
28 | { |
---|
29 | int sz = m_data.size(); |
---|
30 | |
---|
31 | if (val == NULL) // invalid data pointer |
---|
32 | return; |
---|
33 | |
---|
34 | if (sz != nitems) |
---|
35 | m_data.resize(nitems); |
---|
36 | |
---|
37 | // copy array val into m_data |
---|
38 | sz = nitems; |
---|
39 | void* ptr = &(m_data[0]); |
---|
40 | memcpy(ptr, (void*)val, sizeof(DataValType)*sz); |
---|
41 | } |
---|
42 | |
---|
43 | // |
---|
44 | // Add all points to grid1d object at once |
---|
45 | // |
---|
46 | RP_ERROR |
---|
47 | RpGrid1d::addAllPoints(DataValType* val, int nitems) |
---|
48 | { |
---|
49 | if (val == NULL) |
---|
50 | return RP_ERR_NULL_PTR; |
---|
51 | |
---|
52 | if (nitems != numPoints()) |
---|
53 | m_data.resize(nitems); |
---|
54 | |
---|
55 | for (int i=0; i < nitems; i++) |
---|
56 | m_data[i] = val[i]; |
---|
57 | |
---|
58 | return RP_SUCCESS; |
---|
59 | } |
---|
60 | |
---|
61 | // |
---|
62 | // add one point to grid1d |
---|
63 | // |
---|
64 | void RpGrid1d::addPoint(DataValType val) |
---|
65 | { |
---|
66 | m_data.push_back(val); |
---|
67 | } |
---|
68 | |
---|
69 | // |
---|
70 | // serialize Grid1d object |
---|
71 | // 16 bytes: Header (including RV identifier, version, object type id) |
---|
72 | // e.g., RV-A-FIELD, RV-A-MESH3D |
---|
73 | // 4 bytes: N: number of bytes in object name (to follow) |
---|
74 | // N bytes: object name (e.g., "output.grid(g1d))" |
---|
75 | // 4 bytes: number of points in array |
---|
76 | // rest: data array (x x x...) |
---|
77 | // |
---|
78 | char* |
---|
79 | RpGrid1d::serialize(int& nb) |
---|
80 | { |
---|
81 | char* buf; |
---|
82 | int nbytes = numBytes(); |
---|
83 | |
---|
84 | // total length = tagEncode + tagCompress + num + array data |
---|
85 | if ( (buf = (char*) malloc(nbytes)) == NULL) { |
---|
86 | RpAppendErr("RpGrid1d::serialize: malloc failed"); |
---|
87 | RpPrintErr(); |
---|
88 | return buf; |
---|
89 | } |
---|
90 | |
---|
91 | doSerialize(buf, nbytes); |
---|
92 | |
---|
93 | nb = nbytes; |
---|
94 | |
---|
95 | return buf; |
---|
96 | } |
---|
97 | |
---|
98 | // |
---|
99 | // serialize object data in Little Endian byte order |
---|
100 | // |
---|
101 | RP_ERROR |
---|
102 | RpGrid1d::doSerialize(char* buf, int nbytes) |
---|
103 | { |
---|
104 | // check buffer |
---|
105 | if (buf == NULL || nbytes < numBytes()) { |
---|
106 | RpAppendErr("RpGrid1d::serialize: invalid buffer"); |
---|
107 | RpPrintErr(); |
---|
108 | return RP_ERR_INVALID_ARRAY; |
---|
109 | } |
---|
110 | |
---|
111 | char * ptr = buf; |
---|
112 | |
---|
113 | writeHeader(ptr, RpGrid1d_current_version, nbytes); |
---|
114 | ptr += HEADER_SIZE + sizeof(int); |
---|
115 | |
---|
116 | writeObjectName(ptr, m_name); |
---|
117 | ptr += m_name.size() + sizeof(int); |
---|
118 | |
---|
119 | // copy int (number of points) |
---|
120 | |
---|
121 | writeArrayDouble(ptr, m_data, m_data.size()); |
---|
122 | |
---|
123 | return RP_SUCCESS; |
---|
124 | } |
---|
125 | |
---|
126 | RP_ERROR |
---|
127 | RpGrid1d::deserialize(const char* buf) |
---|
128 | { |
---|
129 | if (buf == NULL) { |
---|
130 | RpAppendErr("RpGrid1d::deserialize: null buf pointer"); |
---|
131 | RpPrintErr(); |
---|
132 | return RP_ERR_NULL_PTR; |
---|
133 | } |
---|
134 | |
---|
135 | char* ptr = (char*)buf; |
---|
136 | std::string header; |
---|
137 | int nbytes; |
---|
138 | |
---|
139 | readHeader(ptr, header, nbytes); |
---|
140 | ptr += HEADER_SIZE and sizeof(int); |
---|
141 | |
---|
142 | if (header == RpGrid1d_current_version) |
---|
143 | return doDeserialize(ptr); |
---|
144 | |
---|
145 | // deal with older versions |
---|
146 | return RP_FAILURE; |
---|
147 | } |
---|
148 | |
---|
149 | // |
---|
150 | // parse out the buffer, |
---|
151 | // stripped off the version and total #bytes already. |
---|
152 | // |
---|
153 | RP_ERROR |
---|
154 | RpGrid1d::doDeserialize(const char* buf) |
---|
155 | { |
---|
156 | char* ptr = (char*)buf; |
---|
157 | |
---|
158 | // parse object name and store name in m_name |
---|
159 | |
---|
160 | readObjectName(buf, m_name); |
---|
161 | ptr += sizeof(int) + m_name.size(); |
---|
162 | |
---|
163 | int npts; |
---|
164 | readArrayDouble(buf, m_data, npts); |
---|
165 | |
---|
166 | return RP_SUCCESS; |
---|
167 | } |
---|
168 | |
---|
169 | // |
---|
170 | // return pointer to data points |
---|
171 | // |
---|
172 | DataValType* |
---|
173 | RpGrid1d::getData() |
---|
174 | { |
---|
175 | return &m_data[0]; |
---|
176 | } |
---|
177 | |
---|
178 | // |
---|
179 | // return pointer to a copy of data points in consecutive memory locations |
---|
180 | // |
---|
181 | DataValType* |
---|
182 | RpGrid1d::getDataCopy() |
---|
183 | { |
---|
184 | int npts = numPoints(); |
---|
185 | |
---|
186 | DataValType* xy; |
---|
187 | if ( (xy = new DataValType(npts)) == NULL) { |
---|
188 | RpAppendErr("RpGrid1d::data: mem alloc failed"); |
---|
189 | RpPrintErr(); |
---|
190 | return xy; |
---|
191 | } |
---|
192 | |
---|
193 | for (int i=0; i < npts; i++) |
---|
194 | xy[i] = m_data[i]; |
---|
195 | |
---|
196 | return xy; |
---|
197 | } |
---|
198 | |
---|
199 | // |
---|
200 | // mashalling object into xml string |
---|
201 | // |
---|
202 | void |
---|
203 | RpGrid1d::xmlString(std::string& textString) |
---|
204 | { |
---|
205 | int i; |
---|
206 | int npts = m_data.size(); |
---|
207 | char cstr[256]; |
---|
208 | |
---|
209 | // clear input string |
---|
210 | textString.erase(); |
---|
211 | |
---|
212 | textString.append("<value>"); |
---|
213 | |
---|
214 | for (i=0; i < npts; i++) { |
---|
215 | sprintf(cstr, "\t%.15f\n", m_data[i]); |
---|
216 | textString.append(cstr); |
---|
217 | } |
---|
218 | textString.append("</value>\n"); |
---|
219 | } |
---|
220 | |
---|
221 | // |
---|
222 | // print the xml string from the object |
---|
223 | // |
---|
224 | void |
---|
225 | RpGrid1d::print() |
---|
226 | { |
---|
227 | string str; |
---|
228 | |
---|
229 | xmlString(str); |
---|
230 | |
---|
231 | printf("object name: %s", m_name.c_str()); |
---|
232 | printf("%s", str.c_str()); |
---|
233 | } |
---|
234 | |
---|
235 | // |
---|
236 | // set object name from a charactor string |
---|
237 | // |
---|
238 | void |
---|
239 | RpGrid1d::objectName(const char* str) |
---|
240 | { |
---|
241 | m_name = str; |
---|
242 | } |
---|
243 | |
---|
244 | // |
---|
245 | // get object name |
---|
246 | // |
---|
247 | const char* RpGrid1d::objectName() |
---|
248 | { |
---|
249 | return m_name.c_str(); |
---|
250 | } |
---|
251 | |
---|
252 | // |
---|
253 | // get object type |
---|
254 | // |
---|
255 | const char* RpGrid1d::objectType() |
---|
256 | { |
---|
257 | return RpObjectTypes[GRID1D]; |
---|
258 | } |
---|
259 | |
---|
260 | // |
---|
261 | // return total number of bytes when mashalling out as bytes |
---|
262 | // |
---|
263 | int RpGrid1d::numBytes() |
---|
264 | { |
---|
265 | int nbytes = HEADER_SIZE |
---|
266 | + sizeof(int) // total #bytes |
---|
267 | + sizeof(int) // #bytes in name |
---|
268 | + m_name.size() |
---|
269 | + sizeof(int) // #points in grid |
---|
270 | + m_data.size() * sizeof(DataValType); |
---|
271 | |
---|
272 | return nbytes; |
---|
273 | } |
---|
274 | |
---|