MineLink
 All Data Structures Functions Variables Pages
blockmodel.h
1 #ifndef __BLOCKMODEL_H__
2 #define __BLOCKMODEL_H__
3 
4 #include <vector>
5 #include <set>
6 #include <string>
7 #include <map>
8 #include <iostream>
9 #include <fstream>
10 #include <sstream>
11 #include <exception>
12 #include "coords.h"
13 #include "functors.h"
14 #include "svnversion.h"
15 #include <stdlib.h>
16 #include <time.h>
17 #include "../../utils/ccols.h"
18 
19 
20 using namespace std;
21 
22 
23 
24 namespace delphos{
25 
26 
27 class BlockSelection;
28 
32 #ifdef ARCH32
33 typedef unsigned long BlockIndexType;
34 #else
35 typedef unsigned int BlockIndexType;
36 #endif
37 
38 class Block;
39 class BlockFilter;
40 
41 
63 {
64 
65 
66 public:
67 
77  BlockModel(const delphos::BlockIndexType & num_blocks);
78 
79  #ifndef DOXYGEN
80  BlockModel(const unsigned int & num_blocks,const bool& is_regular, const bool& without_coordinates);
81  #endif
82 
99  BlockModel(const char* file_name,const vector<string> & columns=delphos::BlockModel::EMPTY_STRING_VECT);
100 
117  BlockModel(const char* file_name,const double& ang);
118 
119 
123  static const delphos::BlockIndexType NO_INDEX;
124 
128  static const vector<string> EMPTY_STRING_VECT;
129 
133  virtual ~BlockModel();
134 
145  double GetValue(
146  const delphos::BlockIndexType & index,
147  const string & column) const;
148 
162  double GetValue(
163  const delphos::BlockIndexType & index,
164  const unsigned int & columnpos) const;
165 
174  void SetValue(
175  const delphos::BlockIndexType & index,
176  const string & column,
177  const double & value);
178 
187  void SetValue(
188  const delphos::BlockIndexType & index,
189  const unsigned int & columnpos,
190  const double & value);
191 
204  void AddColumn(const string & column, const double & default_value);
205 
206  #ifndef DOXYGEN
207 
220  void AddColumn(const string & column, delphos::BlockFunctionPtr fp);
221  #endif
222 
238  void AddColumn(const string & column, const string & formula);
239 
249  bool RemoveColumn(const string &column);
250 
258  virtual const delphos::BlockIndexType Size() const;
259 
264  virtual const delphos::BlockIndexType GetSize() const;
265 
273  const unsigned int NumColumns() const;
274 
279  const unsigned int GetNumColumns() const;
280 
292  string ColumnName(const unsigned int & column_index) const;
293 
303  string GetColumnName(const unsigned int & column_index) const;
304 
314  vector<string> GetColumnNames() const;
315 
328  const delphos::BlockIndexType ColumnPos(const string & column) const;
329 
339  const delphos::BlockIndexType GetColumnPos(const string & column) const;
340 
349  virtual void SetCoordinates(const delphos::BlockIndexType & index, const delphos::Coordinates & c);
350 
361  void SetCoordinates(
362  const long & index,
363  const double & x,
364  const double & y,
365  const double & z);
366 
377  virtual delphos::Coordinates GetCoordinates(const delphos::BlockIndexType & index) const;
378 
392  virtual delphos::BlockIndexType BlockIndex(const delphos::Coordinates & c) const;
393 
402  virtual delphos::BlockIndexType GetBlockIndex(const delphos::Coordinates & c) const;
403 
404  #ifndef DOXYGEN
405  virtual delphos::BlockIndexType UnsafeBlockIndex(const delphos::Coordinates & c) const;
406  #endif
407 
419  virtual delphos::BlockIndexType BlockIndex(const double & x, const double & y, const double & z) const;
420 
431  virtual delphos::BlockIndexType GetBlockIndex(const double & x, const double & y, const double & z) const;
432 
433  #ifndef DOXYGEN
434  virtual delphos::BlockIndexType UnsafeBlockIndex(const double & x, const double & y, const double & z) const;
435  #endif
436 
450  virtual delphos::BlockIndexType BlockIndex(const delphos::BlockIndexType& idx, const delphos::BlockIndexType& idy, const delphos::BlockIndexType& idz) const;
451 
464  virtual delphos::BlockIndexType GetBlockIndex(const delphos::BlockIndexType& idx, const delphos::BlockIndexType& idy, const delphos::BlockIndexType& idz) const;
465 
466 
467  #ifndef DOXYGEN
468 
480  virtual delphos::BlockIndexType UnsafeBlockIndex(const BlockIndexType& idx, const BlockIndexType& idy, const BlockIndexType& idz) const;
481  #endif
482 
495  double X(const delphos::BlockIndexType index) const;
496 
509  double Y(const delphos::BlockIndexType index) const;
510 
523  double Z(const delphos::BlockIndexType index) const;
524 
534  double GetX(const delphos::BlockIndexType index) const;
535 
545  double GetY(const delphos::BlockIndexType index) const;
546 
556  double GetZ(const delphos::BlockIndexType index) const;
557 
558 
578  bool SaveToTextFile( const char * bm_file_name,const unsigned int& precision = 4);
579 
597  bool SaveToTextFile( const char * bm_file_name, const vector<string> & columns,const unsigned int& precision=4,const string& delim="");
598 
618  bool SaveToVtkFile( const char * vtk_file_name, const vector<string> & columns,const unsigned int& precision=4);
643  bool SaveToVtkFile( const char * vtk_file_name, const string& filtercolumn,const float& li,const float& ls,const string& column,const unsigned int& precision=4);
671  vector<double>* SaveToVtkScalarsArray(const string& filtercolumn,const float& li,const float& ls,const string& column,int& DIMX,int& DIMY,int& DIMZ,int& THRESOLD,int& BMXINF,int& BMYINF,int& BMZINF,int& BMXSUP,int& BMYSUP,int& BMZSUP,const unsigned int& precision=4);
693  virtual bool SaveToVtkFileUGridView( const char * vtk_file_name,const string& filtercolumn,const float& li,const float& ls,const vector<string> & columns,const unsigned int& precision=4);
694 
716  bool SaveToModFile(const char * mod_file_name, const string tonCol, const unsigned int& precision=4);
717 
739  bool SaveToModFile(const char * mod_file_name, const string tonCol,
740  const string mcafCol, const string pcafCol, const unsigned int& precision=4);
741 
751  virtual delphos::Block GetBlock(const delphos::BlockIndexType & index);
752 
762  virtual delphos::Block GetBlock(const delphos::Coordinates & c);
763 
776  virtual delphos::Block GetBlock(const double & x, const double & y, const double & z);
777 
791  virtual delphos::Block GetBlock(const delphos::BlockIndexType& index_x, const delphos::BlockIndexType& index_y, const delphos::BlockIndexType& index_z);
792 
802  delphos::BlockModel* SubBlockModel(delphos::BlockSelection* bs);
803 
804  #ifndef DOXYGEN
805 
820  delphos::BlockModel* SubBlockModel(const string& filtercolumn,const float& li=0.9,const float& ls=1.1);
821 
837  delphos::BlockModel* SubBlockModel(const string& column,const string& filtercolumn,const float& li=0.9,const float& ls=1.1);
838 
848  delphos::BlockModel* SubBlockModel(const delphos::BlockFilter& bf,const string& column);
849  #endif
850 
859  delphos::BlockModel* SubBlockModel(const delphos::BlockFilter& bf);
860 
869  bool IsRegular() const ;
870 
881  bool Contains(const delphos::BlockIndexType& idx, const delphos::BlockIndexType& idy, const delphos::BlockIndexType& idz) const;
882 
890  bool Contains(const delphos::Coordinates & c) const;
891 
901  bool ContainsXYZ(const double & x, const double & y, const double & z) const;
902 
911  bool Contains(const delphos::Block& b) const;
912 
920  const Coordinates& GetReferenceBlockCoordinates() const;
921 
929  const double& GetDZ() const;
930 
938  const double& GetDY() const;
939 
947  const double& GetDX() const;
948 
949 
957  const BlockIndexType& GetZCount() const;
958 
966  const BlockIndexType& GetYCount() const;
967 
975  const BlockIndexType& GetXCount() const;
976 
991  const BlockIndexType& GetXMinIndex(const unsigned int& level) const;
992 
1007  const BlockIndexType& GetXMaxIndex(const unsigned int& level) const;
1008 
1023  const BlockIndexType& GetYMinIndex(const unsigned int& level) const;
1024 
1039  const BlockIndexType& GetYMaxIndex(const unsigned int& level) const;
1040 
1056  bool Regularize();
1057 
1070  bool UnRegularize();
1071 
1077  double GetMaxX();
1078 
1084  double GetMaxY();
1085 
1091  double GetMaxZ();
1092 
1098  double GetMinX();
1099 
1105  double GetMinY();
1106 
1107  #ifndef DOXYGEN
1108  double GetMinZ(const delphos::BlockIndexType& idx, const delphos::BlockIndexType& idy,const delphos::BlockFilter& bf,const double& z,const delphos::BlockIndexType& idz=2);
1109 
1110 
1111  double GetMinZ(const delphos::BlockIndexType& idx, const delphos::BlockIndexType& idy,const delphos::BlockFilter& bf);
1112  #endif
1113 
1118  double GetMinZ();
1119 
1125  double GetAvgX();
1126 
1132  double GetAvgY();
1133 
1139  double GetAvgZ();
1140 
1150  double GetMaxAttr(const string& column);
1151 
1161  double GetMinAttr(const string& column);
1162 
1172  double GetAvgAttr(const string& column);
1173 
1183  double GetMaxAttr(const unsigned int& column_index);
1184 
1194  double GetMinAttr(const unsigned int& column_index);
1195 
1205  double GetAvgAttr(const unsigned int& column_index);
1206 
1217  bool IsComputed(const string & column);
1218 
1229  bool IsComputed(const unsigned int & index);
1230 
1246  bool SaveMetaData(const string & filename, const string & bm_filename);
1247 
1261  bool LoadMetaData(const string & filename);
1262 
1263  #ifndef DOXYGEN
1264  string GetMD5(const string & file);
1265  #endif
1266 
1267 private:
1268 
1269 typedef struct data{
1270  double x;
1271  double y;
1272  double z;
1273  bool in;
1274 } Data;
1275 
1276 typedef struct poly{
1277  BlockIndexType pt1;
1278  BlockIndexType pt2;
1279  BlockIndexType pt3;
1280  BlockIndexType pt4;
1281  BlockIndexType bindex;
1282  BlockIndexType Z;
1283 } Poly;
1284 
1285  BlockIndexType _nb;
1286  vector<string>* _columns;
1287  vector<double*>* _values;
1288  Coordinates* _coords;
1289  map<string,BlockIndexType> * _coord_map;
1290 
1291  bool _is_regular;
1292  BlockIndexType*** _indexes;
1293  BlockIndexType* _height_min_indexes;
1294  BlockIndexType* _height_max_indexes;
1295  BlockIndexType* _width_min_indexes;
1296  BlockIndexType* _width_max_indexes;
1297  Coordinates _coord_b0;
1298  double _dx;
1299  double _dy;
1300  double _dz;
1301  BlockIndexType _height_index;
1302  BlockIndexType _width_index;
1303  BlockIndexType _depth_index;
1304  BlockIndexType* _index_to_coord;
1305 
1306  vector <double> _max_values;
1307  vector <double> _min_values;
1308  vector <double> _avg_values;
1309  double _maxX;
1310  double _maxY;
1311  double _maxZ;
1312  double _minX;
1313  double _minY;
1314  double _minZ;
1315  double _avgX;
1316  double _avgY;
1317  double _avgZ;
1318  vector <bool> _flag_stat_attr;
1319  bool _flag_stat_coord;
1320  void ComputeCoordStats();
1321  void ComputeAttrStats(const unsigned int& column_index);
1322  bool _is_full;
1323 
1324  map<string,Ccol> _ccols;
1325  vector< vector<string> > _cols_dep;
1326 
1330  void DeleteCoords();
1331 
1338  bool IsFull() const;
1339 
1340 #ifdef ARCH32
1341  BlockModel(const unsigned long & num_blocks,const bool is_regular);
1342 #else
1343  BlockModel(const unsigned int & num_blocks,const bool is_regular);
1344 #endif
1345 
1346  friend class BlockSelection;
1347 };
1348 
1357 private:
1358  BlockModel & _bm;
1359 public:
1366  #ifndef DOXYGEN
1368  #endif
1369 
1374  delphos::BlockModel & GetBlockModel();
1375 
1381  const delphos::BlockModel & GetBlockModel() const;
1382 };
1383 
1390 class Block : public BlockModelEntity {
1391 
1392 
1393 public:
1394 
1395 
1396 
1405  double X() const;
1413  double Y() const;
1421  double Z() const;
1422 
1427  double GetX() const;
1432  double GetY() const;
1437  double GetZ() const;
1438 
1439 
1449  double GetValue(const string & column) const;
1450 
1459  void SetValue(const string & column, const double & value);
1460 
1466  delphos::Coordinates GetCoordinates() const;
1467 
1477  double operator [] (const string & column) const;
1478 
1487  delphos::BlockIndexType Index() const;
1488 
1494  delphos::BlockIndexType GetIndex() const;
1495 
1496  friend class BlockModel;
1497 
1498 private:
1499  const BlockIndexType _index;
1500  Block(BlockModel & bm, const BlockIndexType & index);
1501 
1502 };
1503 
1504 
1509 class MineLinkException: public std::exception{
1510 public:
1515  explicit MineLinkException(const std::string& what):_msg(what){}
1523  MineLinkException(const std::string& what,string file, string function, int line )
1524  {
1525  std::stringstream ss;
1526  std::stringstream sf;
1527  ss << what ;
1528  _msg = ss.str();
1529  time_t now = time(0);
1530  struct tm tstruct;
1531  char buf[80];
1532  tstruct = *localtime(&now);
1533  strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
1534 
1535  char const *folder = getenv("TMPDIR");
1536  if (folder == 0) folder = getenv("TMP");
1537  if (folder == 0) folder = getenv("TEMP");
1538  if (folder == 0) folder = getenv("TEMPDIR");
1539  if (folder == 0) folder = "/tmp";
1540 #ifdef WINDOWS
1541  string sep("\\");
1542 #else
1543  string sep("/");
1544 #endif
1545  sf <<folder<<sep<< "MineLink.log";
1546  //cerr<<"logfile="<<sf.str()<<endl;
1547  ofstream myfile;
1548  myfile.open (sf.str().c_str(), ios::out);
1549  myfile << buf << " : "<<what << " -> in file: " << file << " in function: " << function << " at line: " << line <<endl;
1550  myfile.close();
1551  }
1555  virtual ~MineLinkException() throw() {}
1556 
1557  #ifndef DOXYGEN
1558  virtual const char * what() const throw()
1559  {
1560  return _msg.c_str();
1561  }
1562  #endif
1563 private:
1564  std::string _msg;
1565 };
1566 
1570 static string GetVersion(){
1571  std::stringstream ss;
1572  ss << "MineLink" ;
1573 #ifdef ACADEMIC
1574  ss << " Academic";
1575 #endif
1576 #ifdef DEBUG
1577  ss << " Debug";
1578 #endif
1579 #ifdef TEST
1580  ss << " Test";
1581 #endif
1582 #ifndef DEBUG
1583  #ifndef TEST
1584  ss << " Release";
1585  #endif
1586 #endif
1587  ss << " V1.0";//<<"."<<SVN_REV;
1588  return ss.str();
1589 }
1590 
1594 static string GetRevision(){
1595  std::stringstream ss;
1596  ss << SVN_REV;
1597  return ss.str();
1598 }
1599 
1600 
1601 }
1602 #endif
Abstracts a set of blocks (subset of a blockmodel).
Definition: blocksel.h:33
An utility class to abstract coordinates so they can be used as map and set keys by means of string c...
Definition: coords.h:20
A utility class to access blocks individually.
Definition: blockmodel.h:1390
Abstracts a container of blocks as a table with arbitrary number of attributes that can be accessed b...
Definition: blockmodel.h:62
static const vector< string > EMPTY_STRING_VECT
Empty string vector.
Definition: blockmodel.h:128
A base class for object associated to a given block model.
Definition: blockmodel.h:1356
Abstracts functions to do block selection.
Definition: filters.h:15
static const delphos::BlockIndexType NO_INDEX
Index of nonexistent Block.
Definition: blockmodel.h:123