Changeset 336 for trunk/src/mesh/grid2d.cc
- Timestamp:
- Mar 13, 2006 8:01:41 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/mesh/grid2d.cc
r302 r336 22 22 RpGrid2d::RpGrid2d(DataValType* val, int npoints) 23 23 { 24 m_data.reserve(npoints*2); 24 if (val == NULL) 25 return; 25 26 26 27 for (int i=0; i<2*npoints; i++) 27 m_data [i] = val[i];28 m_data.push_back(val[i]); 28 29 } 29 30 30 31 // 31 32 // constructor 33 // input: array of pointers to points 34 // 35 RpGrid2d::RpGrid2d(DataValType* val[], int npoints) 36 { 37 if (val == NULL) 38 return; 39 40 for (int i=0; i < npoints; i++) { 41 m_data.push_back(val[i][0]); 42 m_data.push_back(val[i][1]); 43 } 44 } 45 46 // 47 // constructor 48 // Input: 49 // spec for a uniform 2d grid 50 // X: origin, max, delta 51 // Y: origin, max, delta 52 // Result: 53 // expand points and add them to m_data 54 // 55 RpGrid2d::RpGrid2d(DataValType x_min, DataValType x_max, int x_delta, 56 DataValType y_min, DataValType y_max, int y_delta) 57 { 58 setUniformGrid(x_min, x_max, x_delta, y_min, y_max, y_delta); 59 } 60 61 RP_ERROR 62 RpGrid2d::setUniformGrid(DataValType x_min, DataValType x_max, int x_delta, 63 DataValType y_min, DataValType y_max, int y_delta) 64 { 65 // expand array, inclusive of origin and max 66 for (int i=0; i <= (int)((x_max-x_min)/x_delta); i++) { 67 for (int j=0; j <= (int)((y_max-y_min)/y_delta); j++) { 68 m_data.push_back(x_min + i*x_delta); 69 m_data.push_back(y_min + j*y_delta); 70 } 71 } 72 73 #ifdef DEBUG 74 printf("RpGrid2d uniform grid constructor: num=%d\n", m_data.size()/2); 75 #endif 76 return RP_FAILURE; 77 } 78 79 // 80 // constructor for rectilinear 2d grid 81 // 82 RpGrid2d::RpGrid2d(DataValType* x, int xdim, DataValType* y, int ydim) 83 { 84 setRectGrid(x, xdim, y, ydim); 85 } 86 87 // 88 // add a rectilinear 2d grid 32 89 // Input: 33 // xval: array of x values 34 // yval: array of y values 35 // npoints: number of points 36 // 37 RpGrid2d::RpGrid2d(DataValType* xval, DataValType* yval, int npoints) 38 { 39 m_data.resize(npoints*2); 40 41 for (int i=0; i<npoints; i++) 42 addPoint(xval[i], yval[i]); 43 } 44 45 // instantiate with byte stream 46 RpGrid2d::RpGrid2d(const char* buf) 47 { 48 deserialize(buf); 49 } 50 90 // x: points on x axis 91 // xdim: number of points on X 92 // y: points on y axis 93 // ydim: number of points on Y 94 // 95 // Result: 96 // expand data to (xdim * ydim) points 97 // 98 RP_ERROR 99 RpGrid2d::setRectGrid(DataValType* x, int xdim, 100 DataValType* y, int ydim) 101 { 102 if (x == NULL || y == NULL) { 103 RpAppendErr("RpGrid2d::setRectGrid: null input ptrs"); 104 RpPrintErr(); 105 return RP_ERR_NULL_PTR; 106 } 107 108 for (int i=0; i < xdim; i++) 109 for (int j=0; j < ydim; j++) { 110 m_data.push_back(x[i]); 111 m_data.push_back(y[j]); 112 } 113 114 return RP_SUCCESS; 115 } 51 116 52 117 // … … 60 125 61 126 // 62 // access data as a 1d array: x y x y x y... 63 // 64 DataValType* RpGrid2d::data() 65 { 66 return (DataValType*) &(m_data[0]); 67 } 68 69 // 70 // serialize data 71 // 1 byte: encoding 72 // 1 byte: compression 73 // 4 bytes: number of points 74 // rest: data points, x y x y x y 75 // 76 char * 77 RpGrid2d::serialize(RP_ENCODE_ALG encodeFlag, RP_COMPRESSION compressFlag) 78 { 79 int numVals = m_data.size(); 80 int npts = numVals/2; 81 82 // total length = tagEncode + tagCompress + num + data 83 char * buf = (char*) new char[numVals*sizeof(DataValType) + sizeof(int) + 2]; 127 // add all points to grid 128 // 129 RP_ERROR 130 RpGrid2d::addAllPoints(DataValType* points, int npts) 131 { 132 return (RpGrid1d::addAllPoints(points, 2*npts)); 133 } 134 135 // 136 // get pointer to array of points 137 // 138 DataValType* 139 RpGrid2d::getData() 140 { 141 return RpGrid1d::getData(); 142 } 143 144 // 145 // retrieve x y at index 146 // 147 RP_ERROR 148 RpGrid2d::getData(DataValType& x, DataValType& y, int index) 149 { 150 x = m_data.at(index); 151 y = m_data.at(index+1); 152 153 return RP_SUCCESS; 154 } 155 156 // 157 // serialize object to a byte stream 158 // Output: 159 // nbytes: total number of bytes in the byte stream 160 // return pointer to buffer holding the byte stream 161 // 162 char* 163 RpGrid2d::serialize(int& nbytes) 164 { 165 // call base class serialize() 166 char* buf = RpGrid1d::serialize(nbytes); 167 168 // now, override the header with correct object type 169 writeRpHeader(buf, RpCurrentVersion[GRID2D], nbytes); 170 171 return buf; 172 } 173 174 // 175 // unmarshalling object from byte stream pointed by 'buf' 176 // 177 // 178 RP_ERROR 179 RpGrid2d::deserialize(const char* buf) 180 { 84 181 if (buf == NULL) { 85 RpAppendErr("RpGrid2d::serialize: malloc failed"); 86 RpPrintErr(); 87 return NULL; 88 } 89 90 char* ptr = buf; 91 92 ptr[0] = 'N'; // init to no-encoding 93 switch(encodeFlag) { 94 case RP_UUENCODE: 95 ptr[0] = 'U'; 96 break; 97 case RP_NO_ENCODING: 98 default: 99 break; 100 } 101 102 ptr[1] = 'N'; // init to no compression 103 switch(compressFlag) { 104 case RP_ZLIB: 105 ptr[1] = 'Z'; 106 break; 107 case RP_NO_COMPRESSION: 108 default: 109 break; 110 } 111 112 // TODO encode, compression 113 // 114 115 // write to stream buffer 116 ptr += 2; // skip first two bytes 117 118 memcpy((void*)ptr, (void*)&npts, sizeof(int)); 119 ptr += sizeof(int); 120 121 memcpy((void*)ptr, (void*)&(m_data[0]), numVals*sizeof(DataValType)); 122 123 return buf; 124 } 125 126 int RpGrid2d::deserialize(const char* buf) 127 { 128 int npts; 129 130 if (buf == NULL) { 131 RpAppendErr("RpGrid1d::deserialize: null buf pointer"); 182 RpAppendErr("RpGrid2d::deserialize: null buf pointer"); 132 183 RpPrintErr(); 133 184 return RP_ERR_NULL_PTR; 134 185 } 135 186 136 // TODO: handle encoding, decompression 187 char* ptr = (char*)buf; 188 std::string header; 189 int nbytes; 190 191 readRpHeader(ptr, header, nbytes); 192 ptr += HEADER_SIZE + sizeof(int); 137 193 138 buf += 2; // skip 1st 2 bytes 139 140 // read number of points 141 memcpy((void*)&npts, (void*)buf, sizeof(int)); 142 buf += sizeof(int); 143 144 m_data.resize(npts*2); // set the array to be the right size 145 memcpy((void*)&(m_data[0]), (void*)buf, npts*2*sizeof(DataValType)); 146 147 /* TODO 148 if (ByteOrder::IsBigEndian()) { 149 for (int i=0; i<npts; i++) 150 { 151 // flip each x 152 // flip each y 153 } 154 } */ 155 156 return RP_SUCCESS; 157 } 158 159 160 // TODO 161 //int RpGrid2d::xmlPut() { }; 162 //int RpGrid2d::xmlGet() { }; 163 164 165 194 if (header == RpCurrentVersion[GRID2D]) 195 return doDeserialize(ptr); 196 197 // deal with older versions 198 return RP_FAILURE; 199 } 200 201 // 202 //// mashalling object into xml string 203 //// 204 void 205 RpGrid2d::xmlString(std::string& textString) 206 { 207 char cstr[256]; 208 209 // clear input string 210 textString.clear(); 211 212 textString.append("<points>"); 213 214 for (int i=0; i < (signed)m_data.size(); i++) { 215 sprintf(cstr, "\t%.15f\t%.15f\n", m_data.at(i), m_data.at(i+1)); 216 textString.append(cstr); 217 i++; 218 } 219 textString.append("</points>\n"); 220 } 221 222 // 223 // print the xml string from the object 224 // 225 void 226 RpGrid2d::print() 227 { 228 string str; 229 230 printf("object name: %s\n", m_name.c_str()); 231 232 xmlString(str); 233 234 printf("%s", str.c_str()); 235 } 236 237 // 238 // get object type 239 // 240 const char* 241 RpGrid2d::objectType() 242 { 243 return RpObjectTypes[GRID2D]; 244 } 245
Note: See TracChangeset
for help on using the changeset viewer.