[185] | 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 | { |
---|
[254] | 15 | m_data.reserve(size); |
---|
[185] | 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 | // |
---|
[271] | 27 | RpGrid1d::RpGrid1d(DataValType* val, int nitems) |
---|
[185] | 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]); |
---|
[271] | 40 | memcpy(ptr, (void*)val, sizeof(DataValType)*sz); |
---|
[185] | 41 | } |
---|
| 42 | |
---|
[254] | 43 | // |
---|
| 44 | // Add all points to grid1d object at once |
---|
| 45 | // |
---|
| 46 | RP_ERROR |
---|
[271] | 47 | RpGrid1d::addAllPoints(DataValType* val, int nitems) |
---|
[254] | 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 | // |
---|
[271] | 64 | void RpGrid1d::addPoint(DataValType val) |
---|
[185] | 65 | { |
---|
| 66 | m_data.push_back(val); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | // |
---|
[281] | 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...) |
---|
[185] | 77 | // |
---|
| 78 | char* |
---|
[281] | 79 | RpGrid1d::serialize(int& nb) |
---|
[185] | 80 | { |
---|
[254] | 81 | char* buf; |
---|
[281] | 82 | int nbytes = numBytes(); |
---|
[185] | 83 | |
---|
| 84 | // total length = tagEncode + tagCompress + num + array data |
---|
[254] | 85 | if ( (buf = (char*) malloc(nbytes)) == NULL) { |
---|
[185] | 86 | RpAppendErr("RpGrid1d::serialize: malloc failed"); |
---|
| 87 | RpPrintErr(); |
---|
| 88 | return buf; |
---|
| 89 | } |
---|
| 90 | |
---|
[281] | 91 | doSerialize(buf, nbytes); |
---|
| 92 | |
---|
[254] | 93 | nb = nbytes; |
---|
| 94 | |
---|
| 95 | return buf; |
---|
| 96 | } |
---|
| 97 | |
---|
[281] | 98 | // |
---|
| 99 | // serialize object data in Little Endian byte order |
---|
| 100 | // |
---|
[254] | 101 | RP_ERROR |
---|
[281] | 102 | RpGrid1d::doSerialize(char* buf, int nbytes) |
---|
[254] | 103 | { |
---|
[281] | 104 | // check buffer |
---|
| 105 | if (buf == NULL || nbytes < numBytes()) { |
---|
[254] | 106 | RpAppendErr("RpGrid1d::serialize: invalid buffer"); |
---|
| 107 | RpPrintErr(); |
---|
| 108 | return RP_ERR_INVALID_ARRAY; |
---|
| 109 | } |
---|
[281] | 110 | |
---|
| 111 | char * ptr = buf; |
---|
[293] | 112 | |
---|
[294] | 113 | writeHeader(ptr, RpGrid1d_current_version, nbytes); |
---|
[293] | 114 | ptr += HEADER_SIZE + sizeof(int); |
---|
[254] | 115 | |
---|
[293] | 116 | writeObjectName(ptr, m_name); |
---|
| 117 | ptr += m_name.size() + sizeof(int); |
---|
[185] | 118 | |
---|
[281] | 119 | // copy int (number of points) |
---|
[185] | 120 | |
---|
[293] | 121 | writeArrayDouble(ptr, m_data, m_data.size()); |
---|
[271] | 122 | |
---|
[254] | 123 | return RP_SUCCESS; |
---|
[185] | 124 | } |
---|
| 125 | |
---|
[254] | 126 | RP_ERROR |
---|
[185] | 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 | |
---|
[281] | 135 | char* ptr = (char*)buf; |
---|
[293] | 136 | std::string header; |
---|
| 137 | int nbytes; |
---|
[281] | 138 | |
---|
[293] | 139 | readHeader(ptr, header, nbytes); |
---|
| 140 | ptr += HEADER_SIZE and sizeof(int); |
---|
[185] | 141 | |
---|
[294] | 142 | if (header == RpGrid1d_current_version) |
---|
[281] | 143 | return doDeserialize(ptr); |
---|
[185] | 144 | |
---|
[281] | 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 | |
---|
[293] | 158 | // parse object name and store name in m_name |
---|
[281] | 159 | |
---|
[293] | 160 | readObjectName(buf, m_name); |
---|
| 161 | ptr += sizeof(int) + m_name.size(); |
---|
[281] | 162 | |
---|
| 163 | int npts; |
---|
[293] | 164 | readArrayDouble(buf, m_data, npts); |
---|
[185] | 165 | |
---|
| 166 | return RP_SUCCESS; |
---|
| 167 | } |
---|
| 168 | |
---|
[281] | 169 | // |
---|
| 170 | // return pointer to data points |
---|
| 171 | // |
---|
[271] | 172 | DataValType* |
---|
[281] | 173 | RpGrid1d::getData() |
---|
[255] | 174 | { |
---|
[281] | 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 | { |
---|
[255] | 184 | int npts = numPoints(); |
---|
| 185 | |
---|
[271] | 186 | DataValType* xy; |
---|
| 187 | if ( (xy = new DataValType(npts)) == NULL) { |
---|
[255] | 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 | |
---|
[281] | 199 | // |
---|
| 200 | // mashalling object into xml string |
---|
| 201 | // |
---|
[254] | 202 | void |
---|
| 203 | RpGrid1d::xmlString(std::string& textString) |
---|
| 204 | { |
---|
| 205 | int i; |
---|
| 206 | int npts = m_data.size(); |
---|
| 207 | char cstr[256]; |
---|
[185] | 208 | |
---|
[254] | 209 | // clear input string |
---|
| 210 | textString.erase(); |
---|
[185] | 211 | |
---|
[254] | 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 | |
---|
[281] | 221 | // |
---|
| 222 | // print the xml string from the object |
---|
| 223 | // |
---|
[254] | 224 | void |
---|
| 225 | RpGrid1d::print() |
---|
| 226 | { |
---|
| 227 | string str; |
---|
[255] | 228 | |
---|
[254] | 229 | xmlString(str); |
---|
[255] | 230 | |
---|
[281] | 231 | printf("object name: %s", m_name.c_str()); |
---|
[254] | 232 | printf("%s", str.c_str()); |
---|
| 233 | } |
---|
| 234 | |
---|
[281] | 235 | // |
---|
| 236 | // set object name from a charactor string |
---|
| 237 | // |
---|
| 238 | void |
---|
| 239 | RpGrid1d::objectName(const char* str) |
---|
| 240 | { |
---|
| 241 | m_name = str; |
---|
| 242 | } |
---|
[254] | 243 | |
---|
[281] | 244 | // |
---|
| 245 | // get object name |
---|
| 246 | // |
---|
| 247 | const char* RpGrid1d::objectName() |
---|
| 248 | { |
---|
| 249 | return m_name.c_str(); |
---|
| 250 | } |
---|
[185] | 251 | |
---|
[281] | 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 | |
---|