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 (_bedslopetexture != 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 _alphamin;} |
---|
90 | virtual float getAlphaMax() {return _alphamax;} |
---|
91 | virtual float getHeightMin() {return _heightmin;} |
---|
92 | virtual float getHeightMax() {return _heightmax;} |
---|
93 | virtual float getCullAngle() {return _cullangle;} |
---|
94 | |
---|
95 | virtual void setAlphaMin( float value ) {_alphamin = value;} |
---|
96 | virtual void setAlphaMax( float value ) {_alphamax = value;} |
---|
97 | virtual void setHeightMin( float value ) {_heightmin = value;} |
---|
98 | virtual void setHeightMax( float value ) {_heightmax = value;} |
---|
99 | |
---|
100 | virtual void setCullAngle( float value ) {_cullangle = value;} |
---|
101 | virtual void toggleCulling() {_culling = _culling ? false : true;} |
---|
102 | |
---|
103 | virtual triangle_list getConnectivity(unsigned int index) {return _connectivity.at(index);} |
---|
104 | |
---|
105 | const std::string getSwollenDir() {return *_swollendir;} |
---|
106 | virtual void setSwollenDir(const std::string path) {_swollendir = new std::string(path);} |
---|
107 | |
---|
108 | |
---|
109 | protected: |
---|
110 | |
---|
111 | virtual ~SWWReader(); |
---|
112 | |
---|
113 | std::string* _filename; |
---|
114 | std::string* _swollendir; |
---|
115 | |
---|
116 | // constructor determines SWW validity (netcdf + proper structure) |
---|
117 | bool _valid; |
---|
118 | |
---|
119 | // netCDF file id |
---|
120 | int _ncid; |
---|
121 | |
---|
122 | // netcdf dimension ids |
---|
123 | int _nvolumesid, _nverticesid, _npointsid, _ntimestepsid; |
---|
124 | |
---|
125 | // netcdf dimension values |
---|
126 | size_t _nvolumes, _nvertices, _npoints, _ntimesteps; |
---|
127 | |
---|
128 | // netcdf variable ids |
---|
129 | int _xid, _yid, _zid, _volumesid, _timeid, _stageid; |
---|
130 | |
---|
131 | // netcdf variable values (allocated in constructor) |
---|
132 | float *_px, *_py, *_pz, *_ptime, *_pstage; |
---|
133 | unsigned int *_pvolumes; |
---|
134 | |
---|
135 | // fixed geometry |
---|
136 | osg::ref_ptr<osg::Vec3Array> _bedslopevertices; |
---|
137 | osg::ref_ptr<osg::Vec3Array> _bedslopenormals; |
---|
138 | osg::ref_ptr<osg::DrawElementsUInt> _bedslopeindices; |
---|
139 | osg::ref_ptr<osg::Vec3Array> _bedslopecentroids; |
---|
140 | osg::ref_ptr<osg::Vec2Array> _bedslopetexcoords; |
---|
141 | |
---|
142 | // geometry that changes per timestep |
---|
143 | osg::ref_ptr<osg::Vec3Array> _stagevertices; |
---|
144 | osg::ref_ptr<osg::Vec3Array> _stageprimitivenormals; |
---|
145 | osg::ref_ptr<osg::Vec3Array> _stagevertexnormals; |
---|
146 | osg::ref_ptr<osg::Vec3Array> _stagecentroids; |
---|
147 | osg::ref_ptr<osg::Vec4Array> _stagecolors; |
---|
148 | |
---|
149 | // optional bedslope texture map |
---|
150 | std::string* _bedslopetexture; |
---|
151 | |
---|
152 | // optional geodata for bedslope texture map |
---|
153 | struct |
---|
154 | { |
---|
155 | bool hasData; |
---|
156 | int xresolution, yresolution; |
---|
157 | float xorigin, yorigin; |
---|
158 | float xpixel, ypixel; |
---|
159 | float rotation; |
---|
160 | } _bedslopegeodata; |
---|
161 | |
---|
162 | |
---|
163 | // bedslope vertex scale and shift factors (for normalizing to unit cube) |
---|
164 | float _xscale, _yscale, _zscale, _scale; |
---|
165 | float _xoffset, _yoffset, _zoffset; |
---|
166 | float _xcenter, _ycenter, _zcenter; |
---|
167 | |
---|
168 | // sww file can contain optional global offset attributes |
---|
169 | float _xllcorner, _yllcorner; |
---|
170 | |
---|
171 | // stack of return values from netcdf function calls |
---|
172 | std::vector<int> _status; |
---|
173 | |
---|
174 | // error checker (iterates through _status stack) |
---|
175 | bool _statusHasError(); |
---|
176 | |
---|
177 | // triangle connectivity, list (indexed by vertex number) of |
---|
178 | // lists (indices of triangles sharing this vertex) |
---|
179 | std::vector<triangle_list> _connectivity; |
---|
180 | |
---|
181 | // define alpha (transparency) function |
---|
182 | float _alphamax, _alphamin, _heightmax, _heightmin; |
---|
183 | |
---|
184 | // cull triangles with steepness angle above this value |
---|
185 | float _cullangle; |
---|
186 | bool _culling; |
---|
187 | |
---|
188 | }; |
---|
189 | |
---|
190 | #endif // SWWREADER_H |
---|