1 | #include <stdio.h> |
---|
2 | #include "serializer.h" |
---|
3 | #include "field.h" |
---|
4 | #include "grid3d_rect.h" |
---|
5 | |
---|
6 | // |
---|
7 | // This example show how to create field & grid1d objects, put into |
---|
8 | // a serializer and ask for a serialized byte stream. |
---|
9 | // |
---|
10 | |
---|
11 | #define Num_points 20 |
---|
12 | |
---|
13 | static double points[20] = { |
---|
14 | 0.261243291268, |
---|
15 | 0.159055762008, |
---|
16 | 0.214550893827, |
---|
17 | 0.11741510008, |
---|
18 | 0.119735699467, |
---|
19 | 0.15196145742, |
---|
20 | 0.0245663797288, |
---|
21 | 0.128903081711, |
---|
22 | 0.0927746958394, |
---|
23 | 0.0465364541799, |
---|
24 | 0.531606236106, |
---|
25 | 0.252448742721, |
---|
26 | 0.348575614391, |
---|
27 | 0.180939456908, |
---|
28 | 0.0251118046222, |
---|
29 | 0.810354715199, |
---|
30 | 0.0980414196039, |
---|
31 | 0.392354903151, |
---|
32 | 0.151346474849, |
---|
33 | 0.368703495654 |
---|
34 | }; |
---|
35 | |
---|
36 | static double points2[20] = { |
---|
37 | 0.0245663797288, |
---|
38 | 0.128903081711, |
---|
39 | 0.0927746958394, |
---|
40 | 0.0465364541799, |
---|
41 | 0.531606236106, |
---|
42 | 0.252448742721, |
---|
43 | 0.348575614391, |
---|
44 | 0.180939456908, |
---|
45 | 0.0251118046222, |
---|
46 | 0.810354715199, |
---|
47 | 0.0980414196039, |
---|
48 | 0.392354903151, |
---|
49 | 0.151346474849, |
---|
50 | 0.368703495654, |
---|
51 | 0.261243291268, |
---|
52 | 0.159055762008, |
---|
53 | 0.214550893827, |
---|
54 | 0.11741510008, |
---|
55 | 0.119735699467, |
---|
56 | 0.15196145742 |
---|
57 | }; |
---|
58 | |
---|
59 | static double xy_pts[] = { |
---|
60 | 0.309504615287, 0.844069129715, |
---|
61 | 0.269863116215, 0.589394224151, |
---|
62 | 0.948725301748, 0.226146486693, |
---|
63 | 0.844001851438, 0.139117111982, |
---|
64 | 0.141301087635, 0.847379885543, |
---|
65 | 0.913736318664, 0.166307778641, |
---|
66 | 0.134835615817, 0.182195034894, |
---|
67 | 0.151951467689, 0.848317450773, |
---|
68 | 0.671395143807, 0.138181958412, |
---|
69 | 0.424175028887, 0.10971049923, |
---|
70 | 0.904360553671, 0.587825545849, |
---|
71 | 0.58394908746, 0.432312932998, |
---|
72 | 0.88346489886, 0.394555137211, |
---|
73 | 0.288191101648, 0.627845397977, |
---|
74 | 0.197603800892, 0.127081587039, |
---|
75 | 0.86023335618, 0.942017313532, |
---|
76 | 0.484988525736, 0.202152039484, |
---|
77 | 0.56932761081, 0.689154883236, |
---|
78 | 0.62612255133, 0.241720210408, |
---|
79 | 0.591576322723, 0.623256005171 |
---|
80 | }; |
---|
81 | |
---|
82 | static double xpts[2] = { |
---|
83 | 0.9, |
---|
84 | 2.19 |
---|
85 | }; |
---|
86 | |
---|
87 | static double ypts[5] = { |
---|
88 | 3.0, |
---|
89 | 3.2, |
---|
90 | 3.5, |
---|
91 | 7.9, |
---|
92 | 7.19 |
---|
93 | }; |
---|
94 | |
---|
95 | static double zpts[2] = { |
---|
96 | 1.0, |
---|
97 | 2.0 |
---|
98 | }; |
---|
99 | |
---|
100 | |
---|
101 | void writeToFile(char* buf, int len, const char* filename) |
---|
102 | { |
---|
103 | if (buf != NULL) { |
---|
104 | FILE* fp = fopen(filename, "w"); |
---|
105 | fwrite(buf, 1, len, fp); |
---|
106 | fclose(fp); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | int main() |
---|
111 | { |
---|
112 | int i, j, err, nbytes; |
---|
113 | |
---|
114 | const char* fname1 = "output.field(f1d1)"; |
---|
115 | const char* fname2 = "output.field(f1d2)"; |
---|
116 | const char* gname1 = "output.mesh(m1d)"; |
---|
117 | const char* gname2 = "output.mesh(m3d)"; |
---|
118 | |
---|
119 | printf("Testing field\n"); |
---|
120 | |
---|
121 | RpField* f1 = new RpField(fname1, 20); |
---|
122 | f1->addAllValues(&(points[0]), 20); |
---|
123 | f1->setMeshId(gname2); |
---|
124 | |
---|
125 | RpField* f2 = new RpField(fname2, 20); |
---|
126 | f2->addAllPoints(&(points2[0]), 20); |
---|
127 | f2->setMeshId(gname1); |
---|
128 | |
---|
129 | printf("Testing grid1d\n"); |
---|
130 | |
---|
131 | // use a regular 1d grid |
---|
132 | RpGrid1d* grid = new RpGrid1d(0, 1, 20); |
---|
133 | grid->objectName(gname1); |
---|
134 | |
---|
135 | printf("Testing grid2d\n"); |
---|
136 | |
---|
137 | RpGrid3dRect * mesh = new RpGrid3dRect(gname2); |
---|
138 | mesh->addAllPointsX(xpts, 2); |
---|
139 | mesh->addAllPointsY(ypts, 5); |
---|
140 | mesh->addAllPointsZ(zpts, 2); |
---|
141 | |
---|
142 | printf("Testing serializer\n"); |
---|
143 | |
---|
144 | RpSerializer myvis; |
---|
145 | myvis.addObject(f1); |
---|
146 | myvis.addObject(f2); |
---|
147 | myvis.addObject(grid); |
---|
148 | myvis.addObject(mesh); |
---|
149 | |
---|
150 | char* buf = myvis.serialize(); |
---|
151 | |
---|
152 | // write the byte stream to a file for verification |
---|
153 | nbytes = myvis.numBytes(); |
---|
154 | writeToFile(buf, nbytes, "out.f"); |
---|
155 | |
---|
156 | printf("Testing serializer::deserializer\n"); |
---|
157 | |
---|
158 | RpSerializer newvis; |
---|
159 | newvis.deserialize(buf); |
---|
160 | |
---|
161 | newvis.print(); |
---|
162 | |
---|
163 | printf("Testing serializer::getObject\n"); |
---|
164 | |
---|
165 | RpGrid1d* gptr = (RpGrid1d*) newvis.getObject(gname1); |
---|
166 | gptr->print(); |
---|
167 | |
---|
168 | RpGrid3d* g2ptr = (RpGrid3d*) newvis.getObject(gname2); |
---|
169 | g2ptr->print(); |
---|
170 | |
---|
171 | RpField* fptr = (RpField*) newvis.getObject(fname1); |
---|
172 | fptr->print(); |
---|
173 | |
---|
174 | RpField* f2ptr = (RpField*) newvis.getObject(fname2); |
---|
175 | f2ptr->print(); |
---|
176 | |
---|
177 | newvis.print(); |
---|
178 | |
---|
179 | printf("after clear()\n"); |
---|
180 | newvis.clear(); |
---|
181 | newvis.print(); |
---|
182 | |
---|
183 | return 0; |
---|
184 | } |
---|
185 | |
---|
186 | |
---|