00001 #ifndef GEOMETRYMODULE_HPP_INCLUDED
00002 #define GEOMETRYMODULE_HPP_INCLUDED
00003
00004 #include "../Common.h"
00005 #include "../MeshRead/IMeshReader.h"
00006 #include "../Field.hpp"
00007
00014 class NoGeometryModule
00015 {
00016 public:
00017 static NoGeometryModule & Instance()
00018 {
00019 static NoGeometryModule _instance;
00020 return _instance;
00021 }
00022
00023 ~NoGeometryModule()
00024 {
00025 }
00026
00027 void Free()
00028 {
00029 }
00030
00031 int size() const { return 0; }
00032
00033 int ReadPoints(MeshRead::IMeshReader & reader)
00034 {
00035 return 0;
00036 }
00037
00038 void GetPointAt(IN OUT Tval coords[3])
00039 {}
00040
00041 bool test()
00042 {
00043 return true;
00044 }
00045 };
00046
00047 template< typename Tval = double>
00048 class FirstGeometryModule
00049 {
00050 typedef Memory::Field<3,Tval> TCoord;
00051 public:
00052
00053 static FirstGeometryModule & Instance()
00054 {
00055 static FirstGeometryModule _instance;
00056 return _instance;
00057 }
00058
00059 ~FirstGeometryModule()
00060 {
00061 Free();
00062 }
00063
00064 void Free()
00065 {
00066 safeDeleteArray(_points);
00067 _size = 0;
00068 }
00069
00070 int size() const { return _size; }
00071
00072 int ReadPoints(MeshRead::IMeshReader & reader)
00073 {
00074 if(reader.Init())
00075 {
00076 _size = reader.GetVerticesCount();
00077 std::cout << " -> Reading " << _size << " points";
00078
00079 delete[] _points;
00080
00081 if(_size > 0)
00082 {
00083 _points = new TCoord[_size];
00084
00085 bool ok(true);
00086 int i(0);
00087 for(; i < _size && ok; ++i)
00088 {
00089 ok = reader.GetNextVertex(_points[i].getArray());
00090 }
00091
00092 if(i < _size)
00093 {
00094 throw "GeometryModule::ReadPoints: error while reading: not all points readed!";
00095 }
00096
00097 }
00098
00099
00100 }
00101 return _size;
00102 }
00103
00104 void GetPointAt(IN OUT Tval coords[3])
00105 {
00106 findNearest(coords[0],coords[1],coords[2]);
00107 }
00108
00109 bool test()
00110 {
00111 Tval v[] ={1,1,1};
00112 Tval v2[] ={1,1,1};
00113 GetPointAt(v);
00114 return v != v2;
00115 }
00116
00117 private:
00118 int _size;
00119 TCoord* _points;
00120
00121 protected:
00122 FirstGeometryModule() : _size(0), _points(NULL) { }
00123 FirstGeometryModule(const FirstGeometryModule & other);
00124
00125 void findNearest(Tval & x, Tval & y, Tval & z)
00126 {
00127 if(_size > 0)
00128 {
00129
00130 TCoord last(_points[0]);
00131 Tval dist = ( (x-last[0])*(x-last[0])+(y-last[1])*(y-last[1])+(z-last[2])*(z-last[2]) );
00132 for(int i(1); i < _size; ++i)
00133 {
00134
00135 const Tval newdist= ( (x-_points[i][0])*(x-_points[i][0])
00136 +(y-_points[i][1])*(y-_points[i][1])
00137 +(z-_points[i][2])*(z-_points[i][2]) );
00138 if(dist > newdist)
00139 {
00140 last[0] = _points[i][0];
00141 last[1] = _points[i][1];
00142 last[2] = _points[i][2];
00143 dist = newdist;
00144 }
00145 }
00146
00147 x = last[0];
00148 y = last[1];
00149 z = last[2];
00150
00151 }
00152 }
00153
00154 };
00155
00156 template< typename Tval = double>
00157 class FileGeometryModule
00158 {
00159 typedef Memory::Field<3,Tval> TCoord;
00160 public:
00161
00162 static FileGeometryModule & Instance()
00163 {
00164 static FileGeometryModule _instance;
00165 return _instance;
00166 }
00167
00168 ~FileGeometryModule()
00169 {
00170 Free();
00171 }
00172
00173 void Free()
00174 {
00175
00176 _size = 0;
00177 }
00178
00179 int size() const { return _size; }
00180
00181 int ReadPoints(MeshRead::IMeshReader & reader)
00182 {
00183 _reader = & reader;
00184
00185 return _reader == NULL ? 0 : 1;
00186 }
00187
00188 void GetPointAt(IN OUT Tval coords[3])
00189 {
00190 findNearest(coords[0],coords[1],coords[2]);
00191 }
00192
00193 bool test()
00194 {
00195 Tval v[] ={1,1,1};
00196 Tval v2[] ={1,1,1};
00197 GetPointAt(v);
00198 return v != v2;
00199 }
00200
00201 private:
00202 int _size;
00203
00204 MeshRead::IMeshReader * _reader;
00205
00206 protected:
00207 FileGeometryModule() : _size(0) { }
00208 FileGeometryModule(const FileGeometryModule & other);
00209
00210 void findNearest(Tval & x, Tval & y, Tval & z)
00211 {
00212 if(_reader != NULL)
00213 {
00214
00215 if(_reader->Init())
00216 {
00217 _size = _reader->GetVerticesCount();
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 TCoord last;
00238 _reader->GetNextVertex(last.getArray());
00239 Tval dist = ( (x-last[0])*(x-last[0])+(y-last[1])*(y-last[1])+(z-last[2])*(z-last[2]) );
00240 TCoord current;
00241 for(int i(1); i < _size; ++i)
00242 {
00243 _reader->GetNextVertex(current.getArray());
00244
00245 Tval newdist= ( (x-current[0])*(x-current[0])+(y-current[1])*(y-current[1])+(z-current[2])*(z-current[2]) );
00246 if(dist > newdist)
00247 {
00248 last[0] = current[0];
00249 last[1] = current[1];
00250 last[2] = current[2];
00251 dist = newdist;
00252 }
00253 }
00254
00255 _reader->Free();
00256
00257 x = last[0];
00258 y = last[1];
00259 z = last[2];
00260 }
00261
00262
00263 }
00264
00265 }
00266
00267 };
00269 #endif // GEOMETRYMODULE_HPP_INCLUDED