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

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

moved to rappture/src/mesh

File size: 2.9 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.resize(npoints*2);
15}
16
17RpGrid2d::RpGrid2d(double* xval, double* yval, int npoints)
18{
19        int i;
20
21        m_data.resize(npoints*2);
22
23        for (i=0; i<npoints; i++) {
24                addPoint(xval[i], yval[i]);
25        }
26}
27
28//
29// Input:
30// pts: 2d array with x-y values
31//              x y
32//              x y
33//              x y
34//              ...
35//
36// Because of Fortran's column-major array order, need to transpose before putting in
37// the object storage.
38// notes: either in the API for Fortran: transpose before calling c++ functions
39// or use the transpose function here.
40//
41RpGrid2d::RpGrid2d(double** pts, int npoints)
42{
43        int i;
44
45        m_data.resize(npoints*2);
46
47        for (i=0; i<npoints; i++) {
48                addPoint(pts[i][0], pts[i][1]);
49        }
50}
51
52// instantiate with byte stream
53RpGrid2d::RpGrid2d(const char* buf)
54{
55        deserialize(buf);
56}
57
58
59//
60// add a point (x, y) pair to grid
61//
62void RpGrid2d::addPoint(double x, double y)
63{
64        m_data.push_back(x);
65        m_data.push_back(y);
66}
67
68//
69// access data as a 1d array: x y x y x y...
70//
71double* RpGrid2d::data()
72{
73        return (double*) &(m_data[0]);
74}
75
76//
77// serialize data
78// 1 byte: encoding
79// 1 byte: compression
80// 4 bytes: number of points
81// rest:   data points, x y x y x y
82//
83char *
84RpGrid2d::serialize(RP_ENCODE_ALG encodeFlag, RP_COMPRESSION compressFlag)
85{
86        int numVals = m_data.size();
87        int npts = numVals/2;
88
89        // total length = tagEncode + tagCompress + num + data
90        char * buf = (char*) malloc(numVals*sizeof(double) + sizeof(int) + 2);
91        if (buf == NULL) {
92                RpAppendErr("RpGrid2d::serialize: malloc failed");
93                RpPrintErr();
94                return NULL;
95        }
96
97        char* ptr = buf;
98       
99        ptr[0] = 'N'; // init to no-encoding
100        switch(encodeFlag) {
101                case RP_UUENCODE:
102                        ptr[0] = 'U';
103                        break;
104                case RP_NO_ENCODING:
105                default:
106                        break;
107        }
108
109        ptr[1] = 'N'; // init to no compression
110        switch(compressFlag) {
111                case RP_ZLIB:
112                        ptr[1] = 'Z';
113                        break;
114                case RP_NO_COMPRESSION:
115                default:
116                        break;
117        }
118
119        // TODO encode, compression
120        //
121
122        // write to stream buffer
123        ptr += 2; // skip first two bytes
124
125        memcpy((void*)ptr, (void*)&npts, sizeof(int));
126        ptr += sizeof(int);
127
128        memcpy((void*)ptr, (void*)&(m_data[0]), numVals*sizeof(double));
129
130        return buf;
131}
132
133int RpGrid2d::deserialize(const char* buf)
134{
135        int npts;
136
137        if (buf == NULL) {
138                RpAppendErr("RpGrid1d::deserialize: null buf pointer");
139                RpPrintErr();
140                return RP_ERR_NULL_PTR;
141        }
142
143        // TODO: handle encoding, decompression
144       
145        buf += 2; // skip 1st 2 bytes
146
147        // read number of points
148        memcpy((void*)&npts, (void*)buf, sizeof(int));
149        buf += sizeof(int);
150
151        m_data.resize(npts*2); // set the array to be the right size
152        memcpy((void*)&(m_data[0]), (void*)buf, npts*2*sizeof(double));
153
154        /* TODO
155        if (ByteOrder::IsBigEndian()) {
156                for (int i=0; i<npts; i++)
157                {
158                        // flip each x
159                        // flip each y
160                }
161        } */
162
163        return RP_SUCCESS;
164}
165
166
167// TODO
168//int RpGrid2d::xmlPut() { };
169//int RpGrid2d::xmlGet() { };
170
171
172
Note: See TracBrowser for help on using the repository browser.