source: trunk/src/mesh/grid2d.cc @ 302

Last change on this file since 302 was 302, checked in by cxsong, 18 years ago

updated using RpSerializable?

File size: 2.8 KB
Line 
1//
2// class for 2D grid
3//
4
5#include "grid2d.h"
6
7// Constructors
8RpGrid2d::RpGrid2d()
9{
10}
11
12RpGrid2d::RpGrid2d(int npoints)
13{
14        m_data.reserve(npoints*2);
15}
16
17// constructor
18// Input:
19//      val: array of x y values (x1 y1 x2 y2 ...)
20//      number of points
21//
22RpGrid2d::RpGrid2d(DataValType* val, int npoints)
23{
24        m_data.reserve(npoints*2);
25
26        for (int i=0; i<2*npoints; i++)
27                m_data[i] = val[i];
28}
29
30//
31// constructor
32// Input:
33//      xval: array of x values
34//      yval: array of y values
35//      npoints: number of points
36//
37RpGrid2d::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
46RpGrid2d::RpGrid2d(const char* buf)
47{
48        deserialize(buf);
49}
50
51
52//
53// add a point (x, y) pair to grid
54//
55void RpGrid2d::addPoint(DataValType x, DataValType y)
56{
57        m_data.push_back(x);
58        m_data.push_back(y);
59}
60
61//
62// access data as a 1d array: x y x y x y...
63//
64DataValType* 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//
76char *
77RpGrid2d::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];
84        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
126int RpGrid2d::deserialize(const char* buf)
127{
128        int npts;
129
130        if (buf == NULL) {
131                RpAppendErr("RpGrid1d::deserialize: null buf pointer");
132                RpPrintErr();
133                return RP_ERR_NULL_PTR;
134        }
135
136        // TODO: handle encoding, decompression
137       
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
Note: See TracBrowser for help on using the repository browser.