/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * OpenSceneGraph Public License for more details. */ #include "Exception.h" #include "Layer.h" #include "Locator.h" #include "Object.h" #include "Locator.h" #include "ImageLayer.h" #include "HeightFieldLayer.h" #include "CompositeLayer.h" #include using namespace ive; void Layer::write(DataOutputStream* out) { // Write Layer's identification. out->writeInt(IVELAYER); // If the osg class is inherited by any other class we should also write this to file. osg::Object* object = dynamic_cast(this); if (object) ((ive::Object*)(object))->write(out); else throw Exception("Layer::write(): Could not cast this osgLayer::Layer to an osg::Object."); LayerHelper helper; helper.writeLocator(out, getLocator()); if (out->getVersion() >= VERSION_0023) { out->writeInt(getTextureUnit()); } out->writeUInt(getMinLevel()); out->writeUInt(getMaxLevel()); } void Layer::read(DataInputStream* in) { // Peek on Layer's identification. int id = in->peekInt(); if (id != IVELAYER) throw Exception("Layer::read(): Expected Layer identification."); // Read Layer's identification. id = in->readInt(); // If the osg class is inherited by any other class we should also read this from file. osg::Object* object = dynamic_cast(this); if(object) ((ive::Object*)(object))->read(in); else throw Exception("Layer::read(): Could not cast this osgLayer::Layer to an osg::Group."); LayerHelper helper; setLocator(helper.readLocator(in)); if (in->getVersion() >= VERSION_0023) { setTextureUnit(in->readInt()); } setMinLevel(in->readUInt()); setMaxLevel(in->readUInt()); } void LayerHelper::writeLayer(DataOutputStream* out, osgTerrain::Layer* layer) { if (layer) { out->writeBool(true); if (dynamic_cast(layer)) { ((ive::HeightFieldLayer*)(layer))->write(out); } else if (dynamic_cast(layer)) { ((ive::ImageLayer*)(layer))->write(out); } else if (dynamic_cast(layer)) { ((ive::CompositeLayer*)(layer))->write(out); } else if (dynamic_cast(layer)) { out->writeInt(IVEPROXYLAYER); out->writeString(layer->getFileName()); osgTerrain::Locator* locator = layer->getLocator(); bool writeOutLocator = locator && !locator->getDefinedInFile(); writeLocator(out, writeOutLocator ? locator : 0 ); out->writeUInt(layer->getMinLevel()); out->writeUInt(layer->getMaxLevel()); } } else { out->writeBool(false); } } osgTerrain::Layer* LayerHelper::readLayer(DataInputStream* in) { bool layerExist = in->readBool(); if (!layerExist) return 0; int id = in->peekInt(); if (id==IVEHEIGHTFIELDLAYER) { osgTerrain::HeightFieldLayer* layer = new osgTerrain::HeightFieldLayer; ((ive::HeightFieldLayer*)(layer))->read(in); return layer; } else if (id==IVEIMAGELAYER) { osgTerrain::ImageLayer* layer = new osgTerrain::ImageLayer; ((ive::ImageLayer*)(layer))->read(in); return layer; } else if (id==IVECOMPOSITELAYER) { osgTerrain::CompositeLayer* layer = new osgTerrain::CompositeLayer; ((ive::CompositeLayer*)(layer))->read(in); return layer; } else if (id==IVEPROXYLAYER) { std::string filename = in->readString(); osg::ref_ptr object = osgDB::readObjectFile(filename+".gdal"); osgTerrain::ProxyLayer* proxyLayer = dynamic_cast(object.get()); osg::ref_ptr locator = readLocator(in); unsigned int minLevel = in->readUInt(); unsigned int maxLevel = in->readUInt(); if (proxyLayer) { if (locator.valid()) proxyLayer->setLocator(locator.get()); proxyLayer->setMinLevel(minLevel); proxyLayer->setMaxLevel(maxLevel); } return proxyLayer; } return new osgTerrain::ImageLayer; } void LayerHelper::writeLocator(DataOutputStream* out, osgTerrain::Locator* locator) { if (locator) { out->writeBool(true); ((ive::Locator*)(locator))->write(out); } else { out->writeBool(false); } } osgTerrain::Locator* LayerHelper::readLocator(DataInputStream* in) { bool locatorExist = in->readBool(); if (!locatorExist) return 0; osgTerrain::Locator* locator = new osgTerrain::Locator; ((ive::Locator*)(locator))->read(in); return locator; }