1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12 */
13
14 #include "Exception.h"
15 #include "Layer.h"
16 #include "Locator.h"
17 #include "Object.h"
18 #include "Locator.h"
19 #include "ImageLayer.h"
20 #include "HeightFieldLayer.h"
21 #include "CompositeLayer.h"
22 #include "SwitchLayer.h"
23
24 #include <osgDB/ReadFile>
25
26 using namespace ive;
27
write(DataOutputStream * out)28 void Layer::write(DataOutputStream* out)
29 {
30 // Write Layer's identification.
31 out->writeInt(IVELAYER);
32
33 // If the osg class is inherited by any other class we should also write this to file.
34 osg::Object* object = dynamic_cast<osg::Object*>(this);
35 if (object)
36 ((ive::Object*)(object))->write(out);
37 else
38 out_THROW_EXCEPTION("Layer::write(): Could not cast this osgLayer::Layer to an osg::Object.");
39
40
41 if (out->getVersion() >= VERSION_0023)
42 {
43 out->writeLocator(getLocator());
44
45 if (out->getVersion() >= VERSION_0034)
46 {
47 out->writeUInt(getMinFilter());
48 out->writeUInt(getMagFilter());
49 }
50 else
51 {
52 out->writeUInt((getMagFilter()==osg::Texture::LINEAR) ? 1 : 0);
53 }
54 }
55 else
56 {
57 LayerHelper helper;
58 helper.writeLocator(out, getLocator());
59 }
60
61
62
63 out->writeUInt(getMinLevel());
64 out->writeUInt(getMaxLevel());
65
66 if (out->getVersion() >= VERSION_0027)
67 {
68 writeValidDataOperator(out,getValidDataOperator());
69 }
70 }
71
read(DataInputStream * in)72 void Layer::read(DataInputStream* in)
73 {
74 // Peek on Layer's identification.
75 int id = in->peekInt();
76 if (id != IVELAYER)
77 in_THROW_EXCEPTION("Layer::read(): Expected Layer identification.");
78
79 // Read Layer's identification.
80 id = in->readInt();
81
82 // If the osg class is inherited by any other class we should also read this from file.
83 osg::Object* object = dynamic_cast<osg::Object*>(this);
84 if(object)
85 ((ive::Object*)(object))->read(in);
86 else
87 in_THROW_EXCEPTION("Layer::read(): Could not cast this osgLayer::Layer to an osg::Group.");
88
89 if (in->getVersion() >= VERSION_0023)
90 {
91 setLocator(in->readLocator());
92
93 if (in->getVersion() >= VERSION_0034)
94 {
95 setMinFilter(osg::Texture::FilterMode(in->readUInt()));
96 setMagFilter(osg::Texture::FilterMode(in->readUInt()));
97 }
98 else
99 {
100 setMagFilter(in->readUInt()==0 ? osg::Texture::NEAREST : osg::Texture::LINEAR);
101 }
102 }
103 else
104 {
105 LayerHelper helper;
106 setLocator(helper.readLocator(in));
107 }
108
109 setMinLevel(in->readUInt());
110 setMaxLevel(in->readUInt());
111
112 if (in->getVersion() >= VERSION_0027)
113 {
114 setValidDataOperator(readValidDataOperator(in));
115 }
116 }
117
writeLayer(DataOutputStream * out,osgTerrain::Layer * layer)118 void LayerHelper::writeLayer(DataOutputStream* out, osgTerrain::Layer* layer)
119 {
120 if (layer)
121 {
122 out->writeBool(true);
123
124 if (dynamic_cast<osgTerrain::HeightFieldLayer*>(layer))
125 {
126 ((ive::HeightFieldLayer*)(layer))->write(out);
127 }
128 else if (dynamic_cast<osgTerrain::ImageLayer*>(layer))
129 {
130 ((ive::ImageLayer*)(layer))->write(out);
131 }
132 else if (dynamic_cast<osgTerrain::SwitchLayer*>(layer))
133 {
134 ((ive::SwitchLayer*)(layer))->write(out);
135 }
136 else if (dynamic_cast<osgTerrain::CompositeLayer*>(layer))
137 {
138 ((ive::CompositeLayer*)(layer))->write(out);
139 }
140 else if (dynamic_cast<osgTerrain::ProxyLayer*>(layer))
141 {
142 out->writeInt(IVEPROXYLAYER);
143 out->writeString(layer->getFileName());
144
145 osgTerrain::Locator* locator = layer->getLocator();
146 bool writeOutLocator = locator && !locator->getDefinedInFile();
147 writeLocator(out, writeOutLocator ? locator : 0 );
148
149 out->writeUInt(layer->getMinLevel());
150 out->writeUInt(layer->getMaxLevel());
151 }
152
153 }
154 else
155 {
156 out->writeBool(false);
157 }
158 }
159
readLayer(DataInputStream * in)160 osgTerrain::Layer* LayerHelper::readLayer(DataInputStream* in)
161 {
162 bool layerExist = in->readBool();
163 if (!layerExist) return 0;
164
165 int id = in->peekInt();
166 if (id==IVEHEIGHTFIELDLAYER)
167 {
168 osgTerrain::HeightFieldLayer* layer = new osgTerrain::HeightFieldLayer;
169 ((ive::HeightFieldLayer*)(layer))->read(in);
170 return layer;
171 }
172 else if (id==IVEIMAGELAYER)
173 {
174 osgTerrain::ImageLayer* layer = new osgTerrain::ImageLayer;
175 ((ive::ImageLayer*)(layer))->read(in);
176 return layer;
177 }
178 else if (id==IVESWITCHLAYER)
179 {
180 osgTerrain::SwitchLayer* layer = new osgTerrain::SwitchLayer;
181 ((ive::SwitchLayer*)(layer))->read(in);
182 return layer;
183 }
184 else if (id==IVECOMPOSITELAYER)
185 {
186 osgTerrain::CompositeLayer* layer = new osgTerrain::CompositeLayer;
187 ((ive::CompositeLayer*)(layer))->read(in);
188 return layer;
189 }
190 else if (id==IVEPROXYLAYER)
191 {
192 std::string filename = in->readString();
193 osg::ref_ptr<osgTerrain::ProxyLayer> proxyLayer = osgDB::readRefFile<osgTerrain::ProxyLayer>(filename+".gdal");
194
195 osg::ref_ptr<osgTerrain::Locator> locator = readLocator(in);
196 unsigned int minLevel = in->readUInt();
197 unsigned int maxLevel = in->readUInt();
198
199 if (proxyLayer)
200 {
201 if (locator.valid()) proxyLayer->setLocator(locator.get());
202
203 proxyLayer->setMinLevel(minLevel);
204 proxyLayer->setMaxLevel(maxLevel);
205 }
206
207 return proxyLayer.release();
208 }
209
210 return new osgTerrain::ImageLayer;
211 }
212
writeLocator(DataOutputStream * out,osgTerrain::Locator * locator)213 void LayerHelper::writeLocator(DataOutputStream* out, osgTerrain::Locator* locator)
214 {
215 if (locator)
216 {
217 out->writeBool(true);
218
219 ((ive::Locator*)(locator))->write(out);
220 }
221 else
222 {
223 out->writeBool(false);
224 }
225 }
226
readLocator(DataInputStream * in)227 osgTerrain::Locator* LayerHelper::readLocator(DataInputStream* in)
228 {
229 bool locatorExist = in->readBool();
230 if (!locatorExist) return 0;
231
232 osgTerrain::Locator* locator = new osgTerrain::Locator;
233
234 ((ive::Locator*)(locator))->read(in);
235
236 return locator;
237 }
238
writeValidDataOperator(DataOutputStream * out,osgTerrain::ValidDataOperator * validDataOperator)239 void Layer::writeValidDataOperator(DataOutputStream* out, osgTerrain::ValidDataOperator* validDataOperator)
240 {
241 if (validDataOperator)
242 {
243 out->writeBool(true);
244 osgTerrain::ValidRange* validRange = dynamic_cast<osgTerrain::ValidRange*>(validDataOperator);
245 if (validRange)
246 {
247 out->writeInt(IVEVALIDRANGE);
248 out->writeFloat(validRange->getMinValue());
249 out->writeFloat(validRange->getMaxValue());
250 }
251 else
252 {
253 osgTerrain::NoDataValue* noDataValue = dynamic_cast<osgTerrain::NoDataValue*>(validDataOperator);
254 if (noDataValue)
255 {
256 out->writeInt(IVENODATAVALUE);
257 out->writeFloat(noDataValue->getValue());
258 }
259 }
260 }
261 else
262 {
263 out->writeBool(false);
264 }
265 }
266
readValidDataOperator(DataInputStream * in)267 osgTerrain::ValidDataOperator* Layer::readValidDataOperator(DataInputStream* in)
268 {
269 bool hasOperator = in->readBool();
270 if (!hasOperator) return 0;
271
272 int id = in->peekInt();
273 if (id==IVEVALIDRANGE)
274 {
275 id = in->readInt();
276 float minValue = in->readFloat();
277 float maxValue = in->readFloat();
278 return new osgTerrain::ValidRange(minValue,maxValue);
279 }
280 else if (id==IVENODATAVALUE)
281 {
282 id = in->readInt();
283 float value = in->readFloat();
284 return new osgTerrain::NoDataValue(value);
285 }
286 else
287 {
288 return 0;
289 }
290 }
291