| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 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 | |
|---|
| 28 | void Layer::write(DataOutputStream* out) |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | out->writeInt(IVELAYER); |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 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 | |
|---|
| 72 | void Layer::read(DataInputStream* in) |
|---|
| 73 | { |
|---|
| 74 | |
|---|
| 75 | int id = in->peekInt(); |
|---|
| 76 | if (id != IVELAYER) |
|---|
| 77 | in_THROW_EXCEPTION("Layer::read(): Expected Layer identification."); |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | id = in->readInt(); |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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<osg::Object> object = osgDB::readObjectFile(filename+".gdal"); |
|---|
| 194 | osgTerrain::ProxyLayer* proxyLayer = dynamic_cast<osgTerrain::ProxyLayer*>(object.get()); |
|---|
| 195 | |
|---|
| 196 | osg::ref_ptr<osgTerrain::Locator> locator = readLocator(in); |
|---|
| 197 | unsigned int minLevel = in->readUInt(); |
|---|
| 198 | unsigned int maxLevel = in->readUInt(); |
|---|
| 199 | |
|---|
| 200 | if (proxyLayer) |
|---|
| 201 | { |
|---|
| 202 | if (locator.valid()) proxyLayer->setLocator(locator.get()); |
|---|
| 203 | |
|---|
| 204 | proxyLayer->setMinLevel(minLevel); |
|---|
| 205 | proxyLayer->setMaxLevel(maxLevel); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | return proxyLayer; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | return new osgTerrain::ImageLayer; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | void LayerHelper::writeLocator(DataOutputStream* out, osgTerrain::Locator* locator) |
|---|
| 215 | { |
|---|
| 216 | if (locator) |
|---|
| 217 | { |
|---|
| 218 | out->writeBool(true); |
|---|
| 219 | |
|---|
| 220 | ((ive::Locator*)(locator))->write(out); |
|---|
| 221 | } |
|---|
| 222 | else |
|---|
| 223 | { |
|---|
| 224 | out->writeBool(false); |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | osgTerrain::Locator* LayerHelper::readLocator(DataInputStream* in) |
|---|
| 229 | { |
|---|
| 230 | bool locatorExist = in->readBool(); |
|---|
| 231 | if (!locatorExist) return 0; |
|---|
| 232 | |
|---|
| 233 | osgTerrain::Locator* locator = new osgTerrain::Locator; |
|---|
| 234 | |
|---|
| 235 | ((ive::Locator*)(locator))->read(in); |
|---|
| 236 | |
|---|
| 237 | return locator; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | void Layer::writeValidDataOperator(DataOutputStream* out, osgTerrain::ValidDataOperator* validDataOperator) |
|---|
| 241 | { |
|---|
| 242 | if (validDataOperator) |
|---|
| 243 | { |
|---|
| 244 | out->writeBool(true); |
|---|
| 245 | osgTerrain::ValidRange* validRange = dynamic_cast<osgTerrain::ValidRange*>(validDataOperator); |
|---|
| 246 | if (validRange) |
|---|
| 247 | { |
|---|
| 248 | out->writeInt(IVEVALIDRANGE); |
|---|
| 249 | out->writeFloat(validRange->getMinValue()); |
|---|
| 250 | out->writeFloat(validRange->getMaxValue()); |
|---|
| 251 | } |
|---|
| 252 | else |
|---|
| 253 | { |
|---|
| 254 | osgTerrain::NoDataValue* noDataValue = dynamic_cast<osgTerrain::NoDataValue*>(validDataOperator); |
|---|
| 255 | if (noDataValue) |
|---|
| 256 | { |
|---|
| 257 | out->writeInt(IVENODATAVALUE); |
|---|
| 258 | out->writeFloat(noDataValue->getValue()); |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | else |
|---|
| 263 | { |
|---|
| 264 | out->writeBool(false); |
|---|
| 265 | } |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | osgTerrain::ValidDataOperator* Layer::readValidDataOperator(DataInputStream* in) |
|---|
| 269 | { |
|---|
| 270 | bool hasOperator = in->readBool(); |
|---|
| 271 | if (!hasOperator) return 0; |
|---|
| 272 | |
|---|
| 273 | int id = in->peekInt(); |
|---|
| 274 | if (id==IVEVALIDRANGE) |
|---|
| 275 | { |
|---|
| 276 | id = in->readInt(); |
|---|
| 277 | float minValue = in->readFloat(); |
|---|
| 278 | float maxValue = in->readFloat(); |
|---|
| 279 | return new osgTerrain::ValidRange(minValue,maxValue); |
|---|
| 280 | } |
|---|
| 281 | else if (id==IVENODATAVALUE) |
|---|
| 282 | { |
|---|
| 283 | id = in->readInt(); |
|---|
| 284 | float value = in->readFloat(); |
|---|
| 285 | return new osgTerrain::NoDataValue(value); |
|---|
| 286 | } |
|---|
| 287 | else |
|---|
| 288 | { |
|---|
| 289 | return 0; |
|---|
| 290 | } |
|---|
| 291 | } |
|---|