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

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

updated grid2d to work with grid1d/serializable

File size: 4.3 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        if (val == NULL)
25                return;
26
27        for (int i=0; i<2*npoints; i++)
28                m_data.push_back(val[i]);
29}
30
31//
32// constructor
33// input: array of pointers to points
34//
35RpGrid2d::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//
55RpGrid2d::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
61RP_ERROR
62RpGrid2d::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
74printf("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//
82RpGrid2d::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
89// Input:
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//
98RP_ERROR
99RpGrid2d::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}
116
117//
118// add a point (x, y) pair to grid
119//
120void RpGrid2d::addPoint(DataValType x, DataValType y)
121{
122        m_data.push_back(x);
123        m_data.push_back(y);
124}
125
126//
127// add all points to grid
128//
129RP_ERROR
130RpGrid2d::addAllPoints(DataValType* points, int npts)
131{
132        return (RpGrid1d::addAllPoints(points, 2*npts));
133}
134
135//
136// get pointer to array of points
137//
138DataValType*
139RpGrid2d::getData()
140{
141        return RpGrid1d::getData();
142}
143
144//
145// retrieve x y at index
146//
147RP_ERROR
148RpGrid2d::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//
162char*
163RpGrid2d::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//
178RP_ERROR
179RpGrid2d::deserialize(const char* buf)
180{
181        if (buf == NULL) {
182                RpAppendErr("RpGrid2d::deserialize: null buf pointer");
183                RpPrintErr();
184                return RP_ERR_NULL_PTR;
185        }
186
187        char* ptr = (char*)buf;
188        std::string header;
189        int nbytes;
190
191        readRpHeader(ptr, header, nbytes);
192        ptr += HEADER_SIZE + sizeof(int);
193       
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////
204void
205RpGrid2d::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//
225void
226RpGrid2d::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//
240const char*
241RpGrid2d::objectType()
242{
243        return RpObjectTypes[GRID2D];
244}
245
Note: See TracBrowser for help on using the repository browser.