From 2567b810cf24bccaf9f7f3721a55877270fbe143 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 27 Mar 2008 13:21:36 +0000 Subject: [PATCH] Removed TileSystem class, and added support for TerrainTile's automatically registering and unregistering themseles with the enclosing Terrain node. --- include/osgTerrain/Terrain | 19 +++++++ include/osgTerrain/TerrainTile | 22 ++++++-- include/osgTerrain/TileSystem | 46 ---------------- src/osgTerrain/CMakeLists.txt | 2 - src/osgTerrain/Terrain.cpp | 53 +++++++++++++++++++ src/osgTerrain/TerrainTile.cpp | 26 ++++++++- src/osgTerrain/TileSystem.cpp | 29 ---------- src/osgWrappers/osgTerrain/Terrain.cpp | 23 ++++++++ src/osgWrappers/osgTerrain/TerrainTile.cpp | 11 ++-- src/osgWrappers/osgTerrain/TileSystem.cpp | 61 ---------------------- 10 files changed, 147 insertions(+), 145 deletions(-) delete mode 100644 include/osgTerrain/TileSystem delete mode 100644 src/osgTerrain/TileSystem.cpp delete mode 100644 src/osgWrappers/osgTerrain/TileSystem.cpp diff --git a/include/osgTerrain/Terrain b/include/osgTerrain/Terrain index 56a0af512..7a5553f66 100644 --- a/include/osgTerrain/Terrain +++ b/include/osgTerrain/Terrain @@ -46,13 +46,32 @@ class OSGTERRAIN_EXPORT Terrain : public osg::Group /** Get the const TerrainTechnique*/ const TerrainTechnique* getTerrainTechnique() const { return _terrainTechnique.get(); } + + /** Get the TerrainTile for a given TileID.*/ + TerrainTile* getTile(const TileID& tileID); + + /** Get the const TerrainTile for a given TileID.*/ + const TerrainTile* getTile(const TileID& tileID) const; + protected: virtual ~Terrain(); + + friend class TerrainTile; + + void registerTerrainTile(TerrainTile* tile); + void unregisterTerrainTile(TerrainTile* tile); + + typedef std::map< TileID, TerrainTile* > TerrainTileMap; + typedef std::set< TerrainTile* > TerrainTileSet; osg::ref_ptr _terrainTechnique; + TerrainTileSet _terrainTileSet; + TerrainTileMap _terrainTileMap; + + }; diff --git a/include/osgTerrain/TerrainTile b/include/osgTerrain/TerrainTile index 1fd9f093d..ec38787f2 100644 --- a/include/osgTerrain/TerrainTile +++ b/include/osgTerrain/TerrainTile @@ -39,6 +39,16 @@ class TileID x(in_x), y(in_y) {} + bool operator == (const TileID& rhs) const + { + return (layer==rhs.layer) && (x==rhs.x) && (y==rhs.y); + } + + bool operator != (const TileID& rhs) const + { + return (layer!=rhs.layer) || (x!=rhs.x) || (y!=rhs.y); + } + bool operator < (const TileID& rhs) const { if (layerrhs.x) return false; return y=0; } int layer; int x; @@ -74,7 +86,7 @@ class OSGTERRAIN_EXPORT TerrainTile : public osg::Group /** Set the Terrain that this Terrain tile is a member of.*/ - void setTerrain(Terrain* ts) { _terrain = ts; } + void setTerrain(Terrain* ts); /** Get the Terrain that this Terrain tile is a member of.*/ Terrain* getTerrain() { return _terrain; } @@ -83,11 +95,14 @@ class OSGTERRAIN_EXPORT TerrainTile : public osg::Group const Terrain* getTerrain() const { return _terrain; } - void setTileID(const TileID& tileID) { _tileID = tileID; } + /** Set the TileID (layer, x,y) of the TerrainTile. + * The TileID is used so it can be located by its neighbours + * via the enclosing Terrain node that manages a map of TileID to TerraiTiles.*/ + void setTileID(const TileID& tileID); + /** Get the TileID (layer, x,y) of the TerrainTile.*/ const TileID& getTileID() const { return _tileID; } - /** Set the TerrainTechnique*/ void setTerrainTechnique(TerrainTechnique* TerrainTechnique); @@ -155,6 +170,7 @@ class OSGTERRAIN_EXPORT TerrainTile : public osg::Group typedef std::vector< osg::ref_ptr > Layers; + friend class Terrain; Terrain* _terrain; diff --git a/include/osgTerrain/TileSystem b/include/osgTerrain/TileSystem deleted file mode 100644 index ab4b8bca1..000000000 --- a/include/osgTerrain/TileSystem +++ /dev/null @@ -1,46 +0,0 @@ -/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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. -*/ - -#ifndef OSGTERRAIN_TILESYSTEM -#define OSGTERRAIN_TILESYSTEM 1 - -#include - -#include -#include - -namespace osgTerrain { - -/** TileSystem provides the mechanism for computing the position in space of tiles.*/ -class OSGTERRAIN_EXPORT TileSystem : public osg::Object -{ - public: - - TileSystem(); - - TileSystem(const TileSystem&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); - - META_Object(osgTerrain, TileSystem); - - protected: - - virtual ~TileSystem(); - - typedef std::map< TileID, osg::observer_ptr > TileMap; - - TileMap _tileMap; -}; - -} - -#endif diff --git a/src/osgTerrain/CMakeLists.txt b/src/osgTerrain/CMakeLists.txt index 098d3e99a..60ead26c7 100644 --- a/src/osgTerrain/CMakeLists.txt +++ b/src/osgTerrain/CMakeLists.txt @@ -13,7 +13,6 @@ SET(LIB_PUBLIC_HEADERS ${HEADER_PATH}/TerrainTile ${HEADER_PATH}/TerrainTechnique ${HEADER_PATH}/Terrain - ${HEADER_PATH}/TileSystem ${HEADER_PATH}/GeometryTechnique ${HEADER_PATH}/ValidDataOperator ${HEADER_PATH}/Version @@ -28,7 +27,6 @@ ADD_LIBRARY(${LIB_NAME} TerrainTile.cpp TerrainTechnique.cpp Terrain.cpp - TileSystem.cpp GeometryTechnique.cpp Version.cpp ) diff --git a/src/osgTerrain/Terrain.cpp b/src/osgTerrain/Terrain.cpp index 6c66a5390..16816fe04 100644 --- a/src/osgTerrain/Terrain.cpp +++ b/src/osgTerrain/Terrain.cpp @@ -22,6 +22,15 @@ Terrain::Terrain() Terrain::~Terrain() { + for(TerrainTileSet::iterator itr = _terrainTileSet.begin(); + itr != _terrainTileSet.end(); + ++itr) + { + const_cast(*itr)->_terrain = 0; + } + + _terrainTileSet.clear(); + _terrainTileMap.clear(); } Terrain::Terrain(const Terrain& ts, const osg::CopyOp& copyop) @@ -32,3 +41,47 @@ void Terrain::traverse(osg::NodeVisitor& nv) { Group::traverse(nv); } + +TerrainTile* Terrain::getTile(const TileID& tileID) +{ + TerrainTileMap::iterator itr = _terrainTileMap.find(tileID); + if (itr != _terrainTileMap.end()) return 0; + + return itr->second; +} + +const TerrainTile* Terrain::getTile(const TileID& tileID) const +{ + TerrainTileMap::const_iterator itr = _terrainTileMap.find(tileID); + if (itr != _terrainTileMap.end()) return 0; + + return itr->second; +} + +void Terrain::registerTerrainTile(TerrainTile* tile) +{ + if (!tile) return; + + if (tile->getTileID().valid()) + { + _terrainTileMap[tile->getTileID()] = tile; + } + + _terrainTileSet.insert(tile); + + osg::notify(osg::INFO)<<"Terrain::registerTerrainTile "<getTileID().valid()) + { + _terrainTileMap.erase(tile->getTileID()); + } + + _terrainTileSet.erase(tile); + + osg::notify(osg::INFO)<<"Terrain::unregisterTerrainTile "<unregisterTerrainTile(this); + + _terrain = ts; + + if (_terrain) _terrain->registerTerrainTile(this); +} + +void TerrainTile::setTileID(const TileID& tileID) +{ + if (_tileID == tileID) return; + + if (_terrain) _terrain->unregisterTerrainTile(this); + + _tileID = tileID; + + if (_terrain) _terrain->registerTerrainTile(this); +} + + void TerrainTile::traverse(osg::NodeVisitor& nv) { if (!_hasBeenTraversal) @@ -64,7 +88,7 @@ void TerrainTile::traverse(osg::NodeVisitor& nv) if (ts) { osg::notify(osg::INFO)<<"Assigning terrain system "< - -using namespace osgTerrain; - -TileSystem::TileSystem() -{ -} - -TileSystem::TileSystem(const TileSystem& tileSystem,const osg::CopyOp& copyop): - osg::Object(tileSystem) -{ -} - -TileSystem::~TileSystem() -{ -} diff --git a/src/osgWrappers/osgTerrain/Terrain.cpp b/src/osgWrappers/osgTerrain/Terrain.cpp index 280992f80..1bddd4262 100644 --- a/src/osgWrappers/osgTerrain/Terrain.cpp +++ b/src/osgWrappers/osgTerrain/Terrain.cpp @@ -15,6 +15,7 @@ #include #include #include +#include // Must undefine IN and OUT macros defined in Windows headers #ifdef IN @@ -84,6 +85,28 @@ BEGIN_OBJECT_REFLECTOR(osgTerrain::Terrain) __C5_TerrainTechnique_P1__getTerrainTechnique, "Get the const TerrainTechnique. ", ""); + I_Method1(osgTerrain::TerrainTile *, getTile, IN, const osgTerrain::TileID &, tileID, + Properties::NON_VIRTUAL, + __TerrainTile_P1__getTile__C5_TileID_R1, + "Get the TerrainTile for a given TileID. ", + ""); + I_Method1(const osgTerrain::TerrainTile *, getTile, IN, const osgTerrain::TileID &, tileID, + Properties::NON_VIRTUAL, + __C5_TerrainTile_P1__getTile__C5_TileID_R1, + "Get the const TerrainTile for a given TileID. ", + ""); + I_ProtectedMethod1(void, registerTerrainTile, IN, osgTerrain::TerrainTile *, tile, + Properties::NON_VIRTUAL, + Properties::NON_CONST, + __void__registerTerrainTile__TerrainTile_P1, + "", + ""); + I_ProtectedMethod1(void, unregisterTerrainTile, IN, osgTerrain::TerrainTile *, tile, + Properties::NON_VIRTUAL, + Properties::NON_CONST, + __void__unregisterTerrainTile__TerrainTile_P1, + "", + ""); I_SimpleProperty(osgTerrain::TerrainTechnique *, TerrainTechnique, __TerrainTechnique_P1__getTerrainTechnique, __void__setTerrainTechnique__osgTerrain_TerrainTechnique_P1); diff --git a/src/osgWrappers/osgTerrain/TerrainTile.cpp b/src/osgWrappers/osgTerrain/TerrainTile.cpp index 0a8e6a7c0..60143760b 100644 --- a/src/osgWrappers/osgTerrain/TerrainTile.cpp +++ b/src/osgWrappers/osgTerrain/TerrainTile.cpp @@ -96,12 +96,12 @@ BEGIN_OBJECT_REFLECTOR(osgTerrain::TerrainTile) I_Method1(void, setTileID, IN, const osgTerrain::TileID &, tileID, Properties::NON_VIRTUAL, __void__setTileID__C5_TileID_R1, - "", - ""); + "Set the TileID (layer, x,y) of the TerrainTile. ", + "The TileID is used so it can be located by its neighbours via the enclosing Terrain node that manages a map of TileID to TerraiTiles. "); I_Method0(const osgTerrain::TileID &, getTileID, Properties::NON_VIRTUAL, __C5_TileID_R1__getTileID, - "", + "Get the TileID (layer, x,y) of the TerrainTile. ", ""); I_Method1(void, setTerrainTechnique, IN, osgTerrain::TerrainTechnique *, TerrainTechnique, Properties::NON_VIRTUAL, @@ -232,6 +232,11 @@ BEGIN_VALUE_REFLECTOR(osgTerrain::TileID) ____TileID__int__int__int, "", ""); + I_Method0(bool, valid, + Properties::NON_VIRTUAL, + __bool__valid, + "", + ""); I_PublicMemberProperty(int, layer); I_PublicMemberProperty(int, x); I_PublicMemberProperty(int, y); diff --git a/src/osgWrappers/osgTerrain/TileSystem.cpp b/src/osgWrappers/osgTerrain/TileSystem.cpp deleted file mode 100644 index a908ded7e..000000000 --- a/src/osgWrappers/osgTerrain/TileSystem.cpp +++ /dev/null @@ -1,61 +0,0 @@ -// *************************************************************************** -// -// Generated automatically by genwrapper. -// Please DO NOT EDIT this file! -// -// *************************************************************************** - -#include -#include -#include -#include - -#include -#include -#include - -// Must undefine IN and OUT macros defined in Windows headers -#ifdef IN -#undef IN -#endif -#ifdef OUT -#undef OUT -#endif - -BEGIN_OBJECT_REFLECTOR(osgTerrain::TileSystem) - I_DeclaringFile("osgTerrain/TileSystem"); - I_BaseType(osg::Object); - I_Constructor0(____TileSystem, - "", - ""); - I_ConstructorWithDefaults2(IN, const osgTerrain::TileSystem &, x, , IN, const osg::CopyOp &, copyop, osg::CopyOp::SHALLOW_COPY, - ____TileSystem__C5_TileSystem_R1__C5_osg_CopyOp_R1, - "", - ""); - I_Method0(osg::Object *, cloneType, - Properties::VIRTUAL, - __osg_Object_P1__cloneType, - "Clone the type of an object, with Object* return type. ", - "Must be defined by derived classes. "); - I_Method1(osg::Object *, clone, IN, const osg::CopyOp &, copyop, - Properties::VIRTUAL, - __osg_Object_P1__clone__C5_osg_CopyOp_R1, - "Clone an object, with Object* return type. ", - "Must be defined by derived classes. "); - I_Method1(bool, isSameKindAs, IN, const osg::Object *, obj, - Properties::VIRTUAL, - __bool__isSameKindAs__C5_osg_Object_P1, - "", - ""); - I_Method0(const char *, libraryName, - Properties::VIRTUAL, - __C5_char_P1__libraryName, - "return the name of the object's library. ", - "Must be defined by derived classes. The OpenSceneGraph convention is that the namespace of a library is the same as the library name. "); - I_Method0(const char *, className, - Properties::VIRTUAL, - __C5_char_P1__className, - "return the name of the object's class type. ", - "Must be defined by derived classes. "); -END_REFLECTOR -