00001 
00002 
00003 #ifndef Terrain_H
00004 #define Terrain_H
00005 
00006 #define MAXFEATURES 10
00007 
00008 
00009 #include "Assembly.h"
00010 #include "DH_Chain.h"
00011 #include "Scene.h"
00012 #include "Point.h"
00013 
00021 class TerrainControl : public controlable {
00022 public:
00023   TerrainControl() {}
00024 
00025   ~TerrainControl() {}
00026 
00027   virtual void register_at(ControlDevice& Ctrl) {}
00028 
00029   virtual void register_at(ControlDevice& Ctrl, const std::string& prefix) {
00030     char VariableName[32];
00031 
00032     
00033     std::sprintf(VariableName,"Terrain");
00034 #ifdef TERRAIN_DEBUG_ON
00035     std::cout << "Registering " << VariableName << std::endl;
00036 #endif
00037     TLocCtrl = Ctrl.getSubDevice(std::string(VariableName));
00038     RegisterAt(TLocCtrl,"Terrain_name",terrain_name);
00039     RegisterAt(TLocCtrl,"Terrain_file",terrain_file);
00040     RegisterAt(TLocCtrl,"Terrain_filetype",terrain_filetype);
00041 
00042     RegisterAt(TLocCtrl,"n(0)",normal(0));
00043     RegisterAt(TLocCtrl,"n(1)",normal(1));
00044     RegisterAt(TLocCtrl,"n(2)",normal(2));
00045 
00046     
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057   }
00058 
00059   void print() {
00060     std::cout << " -=-=-=-= Terrain CONTROL DEVICE\n\n"
00061               << " terrain_name = " << terrain_name << std::endl;
00062 
00063   }
00064 
00065 public:
00066 
00067   PType normal;
00068 
00069   std::string terrain_name, terrain_file;
00070 
00071   int terrain_filetype;
00072 
00073   ControlDevice TLocCtrl;
00074 };
00075 
00076 
00077 class Terrain {
00078   typedef Assembly<DataType,3> AssemblyType;
00079   typedef Scene<DataType,3> SceneType;
00080   typedef Loft<DataType> LoftType;
00081   typedef Joint<DataType,3> JointType;
00082   typedef DH_Link<DataType,3> LinkType;
00083   typedef DH_Chain<DataType,3> ChainType;
00084 public:
00085   friend class Turbine;
00086 
00087   Terrain() {}
00088 
00089   ~Terrain() {}
00090 
00091 
00092   bool build(int i, std::string name, std::string file, int type) {
00093     Terrain0->GetNthPart(i)->read(type,file);
00094     Terrain0->GetNthPart(i)->setName(name);
00095   }
00096 
00097   virtual bool specificSetup(SceneType& ThisScene, TerrainControl * TerrainCtrl) {
00098 
00099     
00100 #ifdef TERRAIN_DEBUG_ON
00101     std::cout << "\n\n*********\t Terrain specificSetup \tBegin\n*********" << std::endl;
00102 #endif
00103     
00104     Terrain0 = new AssemblyType;
00105     std::string nameTmp;
00106     nameTmp = "hills";
00107     Terrain0->setName(TerrainCtrl->terrain_name);
00108 
00109     ThisScene.AddAssembly(*Terrain0);
00110 
00111     Terrain0 = ThisScene.GetNthAssembly(ThisScene.GetNumAssemblies()-1);
00112 
00113     LoftType * Ground;
00114     Ground = new LoftType;
00115     Terrain0->AddPart(Ground);
00116     Terrain0->GetNthPart(0)->read(TerrainCtrl->terrain_filetype,TerrainCtrl->terrain_file); 
00117     nameTmp = "ground_loft";
00118     Terrain0->GetNthPart(0)->setName(TerrainCtrl->terrain_name); 
00119     Ground = (LoftType *) Terrain0->GetNthPart(0);
00120 #ifdef TERRAIN_DEBUG_ON
00121     std::cout << "Terrain:= " << Ground->Name()  << " stringID = " << stringID( Ground->IdTag() ) << std::endl;
00122 #endif
00123     
00124     
00125 
00126     MType groundMat(4,4);
00127     PType groundNormal(0.);
00128     groundNormal(0) = 180;  groundNormal(1) = -90;  groundNormal(2) = 0;
00129     groundMat = rotMat3D(TerrainCtrl->normal);
00130     Ground->setDHMat(groundMat);
00131     Ground->setDHMat_old(groundMat);
00132     Ground->setMobility(0);
00133     Ground->setDeformation(0);
00134 
00135     Terrain0->updateAssembly(1,0,groundMat,0);
00136 #ifdef TERRAIN_DEBUG_ON
00137     std::cout << "\t Ground Mat \n\t" << Ground->GetDHMat()
00138               << " Ground->GetNumVelocities() = " << Ground->GetNumVelocities() << std::endl;
00139 #endif
00140 
00141   }
00142 
00143   virtual PType surfacePoint(PType I_A, DataType zmin, int& flag) {
00144     PType I_B(I_A);
00145     I_B(2) = zmin;
00146     int nump = 0, numc = 0;
00147     Terrain0->GetNumPC(nump,numc);
00148 #ifdef TERRAIN_DEBUG_ON
00149     std::cout << " I_A " << I_A << " I_B " << I_B << std::endl;
00150     std::cout << "SurfacePoint nump = " << nump << " numc = " << numc << std::endl;
00151 #endif
00152     
00153     
00154     
00155     
00156     
00157     
00158     Terrain0->makeVerticies(nump);
00159     Terrain0->makeConnections(numc);
00160 
00161     DataType scale = 1.;
00162     nump = 0;
00163     numc = 0;
00164     Terrain0->send2CPT(nump, numc, Terrain0->GetVerticies(), Terrain0->GetConnections(), scale);
00165     I_B = intersection_line2facet(I_A, I_B, nump, numc, Terrain0->GetVerticies(), Terrain0->GetConnections(),flag );
00166     return I_B;
00167   }
00168 
00169   virtual PType surfacePoint(PType I_A, DataType zmin, int flag, DataType off ) {
00170     PType I_B(I_A);
00171     I_B(2) = zmin;
00172     int nump = 0, numc = 0; 
00173     Terrain0->GetNumPC(nump,numc);
00174 #ifdef TERRAIN_DEBUG_ON
00175     std::cout << "TERRAIN I_A " << I_A << " I_B " << I_B << std::endl;
00176     std::cout << "SurfacePoint nump = " << nump << " numc = " << numc << std::endl;
00177 #endif
00178     
00179     
00180     
00181     
00182     
00183     
00184     Terrain0->makeVerticies(nump);
00185     Terrain0->makeConnections(numc);
00186 
00187     DataType scale = 1.;
00188     nump = 0;
00189     numc = 0;
00190     Terrain0->send2CPT(nump, numc, Terrain0->GetVerticies(), Terrain0->GetConnections(), scale);
00191     I_B = intersection_line2facet(I_A, I_B, nump, numc, Terrain0->GetVerticies(), Terrain0->GetConnections(),flag );
00192     I_A =(I_A-I_B);
00193     I_B = I_B + off*normalize(I_A);
00194 
00195     return I_B;
00196   }
00197 
00198   DataType height(PType& I_A, DataType zmin, int& flag) {
00199     DataType h=0.;
00200     PType surfPt;
00201 
00202     surfPt = surfacePoint(I_A, zmin, flag);
00203     h = norm_2(surfPt-I_A);
00204     I_A = surfPt;
00205     return h;
00206   }
00207 
00208   virtual void Restart(std::ifstream& ifs, int& pos, double& t, double& dt) {
00209     std::cout << "\nRestarting Terrain " << Terrain0->Name() << " time= "<< t << " dt = " << dt << std::endl;
00210     Terrain0->Restart(ifs,pos,t,dt);
00211     pos = ifs.tellg();
00212   }
00213 
00214   virtual void Checkpointing(std::ofstream& ofs) {
00215 #ifdef CHECKRESTART_DEBUG_ON
00216     std::cout << "\nCHECKPOINTING Terrain " << std::endl;
00217 #endif
00218     Terrain0->Checkpointing(ofs);
00219   }
00220 
00221 protected:
00222 
00223   AssemblyType * Terrain0;
00224 
00225 };
00226 
00227 #endif
00228 
00229