00001 #ifndef _RR_TERRAINLOADER_H_
00002 #define _RR_TERRAINLOADER_H_
00003
00004 #include <stdio.h>
00005
00006 #include <vector>
00007
00008 #include <vrs/sharedobj.h>
00009 #include <vrs/vector.h>
00010
00011 namespace VRS
00012 {
00013 class SceneThing;
00014 }
00015
00016
00017 namespace random_racer
00018 {
00019
00020 class TerrainGenerator;
00021
00026 class TerrainLoader : public VRS::SharedObj
00027 {
00028 protected:
00033 VRS::Vector m_position;
00034
00038 double m_boxSize;
00039
00043 double m_viewSize;
00044
00048 VRS::SO<VRS::SceneThing> m_thing;
00049
00050 public:
00068 TerrainLoader(
00069 VRS::SO<VRS::SceneThing> thing,
00070 double boxSize,
00071 double viewSize,
00072 const VRS::Vector& initialPosition = VRS::Vector());
00073
00077 virtual ~TerrainLoader();
00078
00082 inline double boxSize() { return m_boxSize; }
00083
00087 inline double viewSize() { return m_viewSize; }
00088
00098 virtual void replaceTerrain(
00099 const VRS::Vector& position, const VRS::Vector& center) = 0;
00100
00114 template <class T>
00115 static unsigned int nearestIndex(
00116 double x, double y, T* cache, unsigned int size, bool debug = false)
00117 {
00118 double t1, t2, diff;
00119 unsigned int nearest;
00120
00121 for(unsigned int i = 0; i < size; i++)
00122 {
00123
00124 t1 = cache[i].x - x;
00125 t2 = cache[i].y - y;
00126 t1 *= t1;
00127 t1 += t2*t2;
00128
00129
00130 if(i == 0 || t1 < diff)
00131 {
00132 diff = t1;
00133 nearest = i;
00134 }
00135 }
00136
00137 if(debug)
00138 printf("nearest is: %u with %lf\n", nearest, diff);
00139
00140 return nearest;
00141 }
00142 };
00143
00144 }
00145
00146 #endif // _RR_TERRAINLOADER_H_