1 | /* |
---|
2 | SWWReader |
---|
3 | |
---|
4 | A C++ class for reading PyVolution SWW files from within |
---|
5 | OpenSceneGraph. |
---|
6 | |
---|
7 | An SWW file is the visualization output of the pyVolution |
---|
8 | Shallow Water Wave equation solver. SWW files contain bedslope |
---|
9 | geometry and a sequence of stages at known timesteps. The |
---|
10 | internal format is NetCDF: |
---|
11 | |
---|
12 | netcdf demo.sww { |
---|
13 | dimensions: |
---|
14 | number_of_volumes = <integer> ; |
---|
15 | number_of_vertices = 3 ; |
---|
16 | number_of_points = <integer> ; |
---|
17 | number_of_timesteps = UNLIMITED ; |
---|
18 | variables: |
---|
19 | float x(number_of_points) ; |
---|
20 | float y(number_of_points) ; |
---|
21 | float z(number_of_points) ; |
---|
22 | int volumes(number_of_volumes, number_of_vertices) ; |
---|
23 | float time(number_of_timesteps) ; |
---|
24 | float stage(number_of_timesteps, number_of_points) ; |
---|
25 | |
---|
26 | |
---|
27 | Authored under contract to Geoscience Australia by: |
---|
28 | |
---|
29 | Darran Edmundson |
---|
30 | ANU Supercomputer Facility Vizlab |
---|
31 | Australian National University |
---|
32 | Canberra, ACT 2600 |
---|
33 | |
---|
34 | copyright (C) 2004 Geoscience Australia |
---|
35 | */ |
---|
36 | |
---|
37 | #ifndef SWWREADER_H |
---|
38 | #define SWWREADER_H |
---|
39 | |
---|
40 | #include <string> |
---|
41 | #include <project.h> |
---|
42 | #include <iostream> |
---|
43 | #include <osg/Geometry> |
---|
44 | |
---|
45 | |
---|
46 | // needed to create a .lib file under win32/Visual Studio |
---|
47 | #if defined(_MSC_VER) |
---|
48 | #define SWWREADER_EXPORT __declspec(dllexport) |
---|
49 | #else |
---|
50 | #define SWWREADER_EXPORT |
---|
51 | #endif |
---|
52 | |
---|
53 | |
---|
54 | typedef std::vector<unsigned int> triangle_list; |
---|
55 | |
---|
56 | |
---|
57 | class SWWREADER_EXPORT SWWReader |
---|
58 | { |
---|
59 | |
---|
60 | public: |
---|
61 | |
---|
62 | SWWReader(const std::string& filename); |
---|
63 | |
---|
64 | virtual bool isValid() {return _valid;} |
---|
65 | |
---|
66 | // bedslope |
---|
67 | virtual osg::ref_ptr<osg::Vec3Array> getBedslopeVertexArray() {return _bedslopevertices;} |
---|
68 | virtual osg::ref_ptr<osg::Vec3Array> getBedslopeNormalArray() {return _bedslopenormals;} |
---|
69 | virtual osg::ref_ptr<osg::DrawElementsUInt> getBedslopeIndexArray() {return _bedslopeindices;} |
---|
70 | virtual osg::ref_ptr<osg::Vec3Array> getBedslopeCentroidArray() {return _bedslopecentroids;} |
---|
71 | virtual osg::ref_ptr<osg::Vec2Array> getBedslopeTextureCoords(); |
---|
72 | |
---|
73 | virtual bool hasBedslopeTexture() {return (_state.bedslopetexturefilename != NULL);} |
---|
74 | virtual void setBedslopeTexture( std::string filename ); |
---|
75 | virtual osg::Image* getBedslopeTexture(); |
---|
76 | |
---|
77 | |
---|
78 | // stage |
---|
79 | virtual osg::ref_ptr<osg::Vec3Array> getStageVertexArray(unsigned int index); |
---|
80 | virtual osg::ref_ptr<osg::Vec3Array> getStageVertexNormalArray() {return _stagevertexnormals;} |
---|
81 | virtual osg::ref_ptr<osg::Vec3Array> getStageCentroidsArray() {return _stagecentroids;} |
---|
82 | virtual osg::ref_ptr<osg::Vec4Array> getStageColorArray() {return _stagecolors;} |
---|
83 | |
---|
84 | |
---|
85 | virtual float getTime(unsigned int index) {return *(_ptime+index);} |
---|
86 | virtual size_t getNumberOfVertices() {return _npoints;} |
---|
87 | virtual unsigned int getNumberOfTimesteps() {return _ntimesteps;} |
---|
88 | |
---|
89 | virtual float getAlphaMin() {return _state.alphamin;} |
---|
90 | virtual float getAlphaMax() {return _state.alphamax;} |
---|
91 | virtual float getHeightMin() {return _state.heightmin;} |
---|
92 | virtual float getHeightMax() {return _state.heightmax;} |
---|
93 | virtual float getCullAngle() {return _state.cullangle;} |
---|
94 | |
---|
95 | virtual void setAlphaMin( float value ) {_state.alphamin = value;} |
---|
96 | virtual void setAlphaMax( float value ) {_state.alphamax = value;} |
---|
97 | virtual void setHeightMin( float value ) {_state.heightmin = value;} |
---|
98 | virtual void setHeightMax( float value ) {_state.heightmax = value;} |
---|
99 | |
---|
100 | virtual void setCullAngle( float value ) {_state.cullangle = value;} |
---|
101 | virtual void toggleCulling() {_state.culling = _state.culling ? false : true;} |
---|
102 | virtual bool getCulling() {return _state.culling;} |
---|
103 | virtual void setCulling(bool value) {_state.culling = value;} |
---|
104 | |
---|
105 | virtual triangle_list getConnectivity(unsigned int index) {return _connectivity.at(index);} |
---|
106 | |
---|
107 | const std::string getSwollenDir() {return *(_state.swollendirectory);} |
---|
108 | virtual void setSwollenDir(const std::string path) {_state.swollendirectory = new std::string(path);} |
---|
109 | |
---|
110 | |
---|
111 | protected: |
---|
112 | |
---|
113 | virtual ~SWWReader(); |
---|
114 | |
---|
115 | |
---|
116 | // state contains all the info needed to serialize |
---|
117 | struct |
---|
118 | { |
---|
119 | float alphamax; // define alpha (transparency) function |
---|
120 | float alphamin; |
---|
121 | float heightmax; |
---|
122 | float heightmin; |
---|
123 | |
---|
124 | float cullangle; // cull triangles with steepness angle above this value |
---|
125 | bool culling; // culling is on or off |
---|
126 | |
---|
127 | std::string* swwfilename; |
---|
128 | std::string* bedslopetexturefilename; |
---|
129 | std::string* swollendirectory; |
---|
130 | |
---|
131 | } _state; |
---|
132 | |
---|
133 | |
---|
134 | // constructor determines SWW validity (netcdf + proper structure) |
---|
135 | bool _valid; |
---|
136 | |
---|
137 | // netCDF file id |
---|
138 | int _ncid; |
---|
139 | |
---|
140 | // netcdf dimension ids |
---|
141 | int _nvolumesid, _nverticesid, _npointsid, _ntimestepsid; |
---|
142 | |
---|
143 | // netcdf dimension values |
---|
144 | size_t _nvolumes, _nvertices, _npoints, _ntimesteps; |
---|
145 | |
---|
146 | // netcdf variable ids |
---|
147 | int _xid, _yid, _zid, _volumesid, _timeid, _stageid; |
---|
148 | |
---|
149 | // netcdf variable values (allocated in constructor) |
---|
150 | float *_px, *_py, *_pz, *_ptime, *_pstage; |
---|
151 | unsigned int *_pvolumes; |
---|
152 | |
---|
153 | // fixed geometry |
---|
154 | osg::ref_ptr<osg::Vec3Array> _bedslopevertices; |
---|
155 | osg::ref_ptr<osg::Vec3Array> _bedslopenormals; |
---|
156 | osg::ref_ptr<osg::DrawElementsUInt> _bedslopeindices; |
---|
157 | osg::ref_ptr<osg::Vec3Array> _bedslopecentroids; |
---|
158 | osg::ref_ptr<osg::Vec2Array> _bedslopetexcoords; |
---|
159 | |
---|
160 | // geometry that changes per timestep |
---|
161 | osg::ref_ptr<osg::Vec3Array> _stagevertices; |
---|
162 | osg::ref_ptr<osg::Vec3Array> _stageprimitivenormals; |
---|
163 | osg::ref_ptr<osg::Vec3Array> _stagevertexnormals; |
---|
164 | osg::ref_ptr<osg::Vec3Array> _stagecentroids; |
---|
165 | osg::ref_ptr<osg::Vec4Array> _stagecolors; |
---|
166 | |
---|
167 | // optional geodata for bedslope texture map |
---|
168 | struct |
---|
169 | { |
---|
170 | bool hasData; |
---|
171 | int xresolution, yresolution; |
---|
172 | float xorigin, yorigin; |
---|
173 | float xpixel, ypixel; |
---|
174 | float rotation; |
---|
175 | } _bedslopegeodata; |
---|
176 | |
---|
177 | |
---|
178 | // bedslope vertex scale and shift factors (for normalizing to unit cube) |
---|
179 | float _xscale, _yscale, _zscale, _scale; |
---|
180 | float _xoffset, _yoffset, _zoffset; |
---|
181 | float _xcenter, _ycenter, _zcenter; |
---|
182 | |
---|
183 | // sww file can contain optional global offset attributes |
---|
184 | float _xllcorner, _yllcorner; |
---|
185 | |
---|
186 | // stack of return values from netcdf function calls |
---|
187 | std::vector<int> _status; |
---|
188 | |
---|
189 | // error checker (iterates through _status stack) |
---|
190 | bool _statusHasError(); |
---|
191 | |
---|
192 | // triangle connectivity, list (indexed by vertex number) of |
---|
193 | // lists (indices of triangles sharing this vertex) |
---|
194 | std::vector<triangle_list> _connectivity; |
---|
195 | |
---|
196 | }; |
---|
197 | |
---|
198 | #endif // SWWREADER_H |
---|