Revision: 2631
          http://rigsofrods.svn.sourceforge.net/rigsofrods/?rev=2631&view=rev
Author:   ulteq
Date:     2012-05-22 15:43:30 +0000 (Tue, 22 May 2012)
Log Message:
-----------
-Codechange: Added getMaxHeight() and getMinHeight()
+cleanup

Modified Paths:
--------------
    trunk/source/main/physics/collision/heightfinder.h
    trunk/source/main/physics/collision/tsmheightfinder.cpp

Modified: trunk/source/main/physics/collision/heightfinder.h
===================================================================
--- trunk/source/main/physics/collision/heightfinder.h  2012-05-22 08:46:13 UTC 
(rev 2630)
+++ trunk/source/main/physics/collision/heightfinder.h  2012-05-22 15:43:30 UTC 
(rev 2631)
@@ -17,46 +17,41 @@
 You should have received a copy of the GNU General Public License
 along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
 */
-#ifndef __HeightFinder_H__
-#define __HeightFinder_H__
+#ifndef __HeightFinder_H_
+#define __HeightFinder_H_
 
 #include "RoRPrerequisites.h"
-#include <OgrePrerequisites.h>
-#include <OgreVector3.h>
 
-
-#include "OgreTerrain.h"
 #include "OgreTerrainGroup.h"
-//#include "DotSceneLoader.h"
 
-//using namespace Ogre;
 /**
- * This is the common Interface for all Scenemanager Specific Implementations 
of the Heightfinder
+ * This is the common interface for all Scene-Manager specific implementations 
of the Height-Finder
  */
 class HeightFinder
 {
 public:
+
        HeightFinder() {};
        virtual ~HeightFinder() {};
 
        virtual float getHeightAt(float x, float z) = 0;
-       virtual Ogre::Vector3 getNormalAt(float x, float y, float z, float 
precision=0.1)
+       virtual Ogre::Vector3 getNormalAt(float x, float y, float z, float 
precision = 0.1f)
        {
-               Ogre::Vector3 left(-precision, getHeightAt( x - precision, z ) 
- y, 0.0f);
-               Ogre::Vector3 down( 0.0f, getHeightAt( x, z + precision ) - y, 
precision);
-               down = left.crossProduct( down );
+               Ogre::Vector3 left(-precision, getHeightAt(x - precision, z) - 
y, 0.0f);
+               Ogre::Vector3 down(0.0f, getHeightAt(x, z + precision) - y, 
precision);
+               down = left.crossProduct(down);
                down.normalise();
                return down;
        }
 };
 
-// new terrain height finder. For the new terrain from Ogre 1.7
+/**
+ * New terrain Height-Finder. For the new terrain from Ogre 1.7
+ */
 class NTHeightFinder : public HeightFinder
 {
-protected:
-       Ogre::TerrainGroup *mTerrainGroup;
-       Ogre::Vector3 mTerrainPos;
 public:
+
        NTHeightFinder(Ogre::TerrainGroup *tg, Ogre::Vector3 tp) : 
mTerrainGroup(tg), mTerrainPos(tp)
        {
        }
@@ -69,58 +64,62 @@
        {
                return mTerrainGroup->getHeightAtWorldPosition(x, 1000, z);
        }
-};
 
-// new terrain height finder adapted to Ogitor scene loading
-#if 0
-// not used right now
-class OgitorSceneHeightFinder : public HeightFinder
-{
-protected:
-       DotSceneLoader *mLoader;
-public:
-       OgitorSceneHeightFinder(DotSceneLoader *loader) : mLoader(loader)
+       float getMaxHeight()
        {
+               float maxHeight = 0.0f;
+               Ogre::TerrainGroup::TerrainIterator ti = 
mTerrainGroup->getTerrainIterator();
+               while(ti.hasMoreElements())
+               {
+                       Ogre::Terrain* t = ti.getNext()->instance;
+                       maxHeight = std::max(maxHeight, t->getMaxHeight());
+               }
+               return maxHeight;
        }
-       
-       ~OgitorSceneHeightFinder()
-       {
-       }
 
-       float getHeightAt(float x, float z)
+       float getMinHeight()
        {
-               return mLoader->getTerrainGroup()->getHeightAtWorldPosition(x, 
1000, z);
+               float minHeight = 0.0f;
+               Ogre::TerrainGroup::TerrainIterator ti = 
mTerrainGroup->getTerrainIterator();
+               while(ti.hasMoreElements())
+               {
+                       Ogre::Terrain* t = ti.getNext()->instance;
+                       minHeight = std::min(t->getMaxHeight(), minHeight);
+               }
+               return minHeight;
        }
-};
-#endif //0
 
+protected:
 
+       Ogre::TerrainGroup *mTerrainGroup;
+       Ogre::Vector3 mTerrainPos;
+};
 
-// Scene-Manager Specific implementations
-
 /**
- * Heightfinder for the standart Ogre Terrain Mnager
+ * Height-Finder for the standard Ogre Terrain Manager
  */
 class TSMHeightFinder : public HeightFinder
 {
-protected:
-       Ogre::Vector3 scale;
-       Ogre::Vector3 inverse_scale;
-       int size;
-       int size1;
-       char cfgfilename[256];
-       unsigned short *data;
-       float defaulth;
-       float dx, dz;
-       bool flipped;
-       void loadSettings();
-
 public:
+
        TSMHeightFinder(char *cfgfilename, char *fname, float defaultheight);
        ~TSMHeightFinder();
 
        float getHeightAt(float x, float z);
 
+protected:
+
+       Ogre::String cfgfilename;
+       Ogre::Vector3 inverse_scale;
+       Ogre::Vector3 scale;
+       bool flipped;
+       float defaulth;
+       float dx, dz;
+       int size1;
+       int size;
+       unsigned short *data;
+
+       void loadSettings();
 };
 
-#endif
+#endif // __HeightFinder_H_

Modified: trunk/source/main/physics/collision/tsmheightfinder.cpp
===================================================================
--- trunk/source/main/physics/collision/tsmheightfinder.cpp     2012-05-22 
08:46:13 UTC (rev 2630)
+++ trunk/source/main/physics/collision/tsmheightfinder.cpp     2012-05-22 
15:43:30 UTC (rev 2631)
@@ -18,63 +18,61 @@
 along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
 */
 #include "heightfinder.h"
-//#include <OgreTerrainSceneManager.h>
 
-#include <Ogre.h>
 #include <OgreConfigFile.h>
-#include <OgreLogManager.h>
-#include <OgreStringConverter.h>
 
 using namespace Ogre;
 
-// this is the Height-Finder for the standart ogre Terrain Manager
+/**
+ * Height-Finder for the standard Ogre Terrain Manager
+ */
+TSMHeightFinder::TSMHeightFinder(char *cfgfilename, char *fname, float 
defaultheight) :
+         cfgfilename(cfgfilename)      
+       , defaulth(defaultheight)
+       , flipped(false)
+{
+       String val;
+       ConfigFile config;
 
-TSMHeightFinder::TSMHeightFinder(char *_cfgfilename, char *fname, float 
defaultheight)
-{
-       flipped=false;
-       //cfgfilename = _cfgfilename; this is a bug
-       strncpy(cfgfilename, _cfgfilename, 255);
-       cfgfilename[255] = '\0';
-       defaulth=defaultheight;
-//     scale=tsm->getScale();
-       //this is not correct, but it works
-//     size=tsm->getPageSize();
-    ConfigFile config;
-    String val;
-       ResourceGroupManager& rgm = ResourceGroupManager::getSingleton();
-       DataStreamPtr stream=rgm.openResource(cfgfilename, 
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
-    config.load( stream );
-    val = config.getSetting( "PageSize" );
-       if ( !val.empty() )
+    ResourceGroupManager& rgm = ResourceGroupManager::getSingleton();
+       DataStreamPtr stream = rgm.openResource(cfgfilename, 
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
+    config.load(stream);
+
+    val = config.getSetting("PageSize");
+       if (!val.empty())
        {
-               size=atoi(val.c_str());
+               size = atoi(val.c_str());
                size1 = size - 1;
        }
     scale = Vector3::UNIT_SCALE;
 
-    val = config.getSetting( "PageWorldX" );
-    if ( !val.empty() )
-        scale.x = atof( val.c_str() );
+    val = config.getSetting("PageWorldX");
+    if (!val.empty())
+       {
+        scale.x = atof(val.c_str());
+       }
 
-    val = config.getSetting( "MaxHeight" );
-    if ( !val.empty() )
+    val = config.getSetting("MaxHeight");
+    if (!val.empty())
        {
-        scale.y = atof( val.c_str() );
+        scale.y = atof(val.c_str());
        }
 
-    val = config.getSetting( "PageWorldZ" );
-    if ( !val.empty() )
-        scale.z = atof( val.c_str() );
-    // Scale x/z relative to pagesize
+    val = config.getSetting("PageWorldZ");
+    if (!val.empty())
+       {
+        scale.z = atof(val.c_str());
+       }
+    // Scale x/z relative to page size
        scale.x /= size1;
        scale.z /= size1;
 
-       inverse_scale.x=1.0/scale.x;
-       inverse_scale.y=scale.y/65535.0;
-       inverse_scale.z=1.0/scale.z;
+       inverse_scale.x= 1.0f / scale.x;
+       inverse_scale.y= scale.y / 65535.0f;
+       inverse_scale.z= 1.0f / scale.z;
 
-       data=(unsigned short*)malloc(size*size*2);
-       DataStreamPtr ds=rgm.openResource(fname, 
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
+       data = (unsigned short*)malloc(size*size*2);
+       DataStreamPtr ds = rgm.openResource(fname, 
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
        ds->read(data, size*size*2);
        // ds closes automatically, so do not close it explicitly here
        loadSettings();
@@ -85,13 +83,16 @@
        ConfigFile cfg;
        DataStreamPtr ds = 
ResourceGroupManager::getSingleton().openResource(String(cfgfilename), 
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
        cfg.load(ds, "\t:=", false);
-       flipped=(cfg.getSetting("Heightmap.flip")=="true");
+       flipped = (cfg.getSetting("Heightmap.flip") == "true");
        LOG("loading HeightFinder configuration from " + String(cfgfilename) + 
" flipped: " + TOSTRING(flipped));
 }
 
 TSMHeightFinder::~TSMHeightFinder()
 {
-       if (data) free(data);
+       if (data)
+       {
+               free(data);
+       }
 }
 
 float TSMHeightFinder::getHeightAt(float x, float z)
@@ -99,17 +100,17 @@
 
        if (x < 0 || z < 0) return defaulth;
 
-       float rx=x*inverse_scale.x;
-       float rz=z*inverse_scale.z;
+       float rx = x * inverse_scale.x;
+       float rz = z * inverse_scale.z;
 
        if (rx >= size1 || rz >= size1) return defaulth;
 
-       int x_index=(int)rx;
-       int z_index=(int)rz;
+       int x_index = (int)rx;
+       int z_index = (int)rz;
 
        //dx=irx; dz=irz;
-       float t1=0, t2=0, b1=0, b2=0;
-       if(!flipped)
+       float t1=0.0f, t2=0.0f, b1=0.0f, b2=0.0f;
+       if (!flipped)
        {
                int z_i = z_index * size;
                int z_iPlus1 = z_i + size;
@@ -120,29 +121,32 @@
        }
         else
        {
-               int rez_i = ( size1 - z_index ) * size;
+               int rez_i = (size1 - z_index) * size;
                int rez_i1 = rez_i - size;
-               t1 = data[x_index+rez_i];
-               t2 = data[x_index+1+rez_i];
-               b1 = data[x_index+rez_i1];
-               b2 = data[x_index+1+rez_i1];
+               t1 = data[x_index + rez_i];
+               t2 = data[x_index + 1 + rez_i];
+               b1 = data[x_index + rez_i1];
+               b2 = data[x_index +1 + rez_i1];
        }
 
-       //              dx=irx; dz=x_pct;
-       float x_pct=rx-x_index;
-       float z_pct=rz-z_index;
+       //dx=irx; dz=x_pct;
+       float x_pct = rx - x_index;
+       float z_pct = rz - z_index;
 
-       if (x_pct + z_pct <= 1) {
+       if (x_pct + z_pct <= 1)
+       {
                b2 = b1 + t2 - t1;
-       } else {
+       } else
+       {
                t1 = b1 + t2 - b2;
        }
 
-       float t = t1 + x_pct * ( t2 - t1 );
-       float b = b1 + x_pct * ( b2 - b1 );
+       float t = t1 + x_pct * (t2 - t1);
+       float b = b1 + x_pct * (b2 - b1);
 
-       float h = (t + z_pct * ( b - t )) * inverse_scale.y;
+       float h = (t + z_pct * (b - t)) * inverse_scale.y;
 
-       if (h<defaulth) h=defaulth;
+       h = std::max(defaulth, h);
+
        return h;
 }

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Rigsofrods-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rigsofrods-devel

Reply via email to