Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
vtTin.h
1 //
2 // vtTin.h
3 //
4 // Copyright (c) 2002-2009 Virtual Terrain Project
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef VTTINH
9 #define VTTINH
10 
11 #include "MathTypes.h"
12 #include "Projections.h"
13 #include "HeightField.h"
14 #include "vtString.h"
15 
16 // a type useful for the Merge algorithm
17 typedef vtArray<int> Bin;
18 
19 class BinArray
20 {
21 public:
22  BinArray(int cols, int rows) {
23  iData = new Bin[cols*rows];
24  iRows = rows;
25  iCols = cols;
26  }
27  ~BinArray() {
28  delete [] iData;
29  }
30  Bin *GetBin(int col, int row)
31  {
32  if (col < 0 || col >= iCols) return NULL;
33  if (row < 0 || row >= iRows) return NULL;
34  return iData + iCols*row + col;
35  }
36  int GetMemoryUsed() const
37  {
38  int bins = iCols * iRows;
39  int bytes = sizeof(BinArray) + sizeof(Bin*) * bins;
40  for (int i = 0; i < bins; i++)
41  bytes += (sizeof(int) * iData[i].GetSize());
42  return bytes;
43  }
44 private:
45  Bin *iData;
46  int iCols, iRows;
47 };
48 
58 class vtTin : public vtHeightField3d
59 {
60 public:
61  vtTin();
62  virtual ~vtTin();
63 
64  uint NumVerts() const { return m_vert.GetSize(); }
65  uint NumTris() const { return m_tri.GetSize()/3; }
66 
67  void AddVert(const DPoint2 &p, float z);
68  void AddVert(const DPoint2 &p, float z, FPoint3 &normal);
69  void AddTri(int v1, int v2, int v3, int surface_type = -1);
70  void RemVert(int v);
71  void RemTri(int t);
72 
73  // Native file I/O.
74  bool Read(const char *fname);
75  bool ReadHeader(const char *fname);
76  bool ReadBody(const char *fname);
77  bool Write(const char *fname, bool progress_callback(int) = NULL) const;
78 
79  // Import/Export.
80  bool ReadDXF(const char *fname, bool progress_callback(int) = NULL);
81  bool ReadADF(const char *fname, bool progress_callback(int) = NULL);
82  bool ReadGMS(const char *fname, bool progress_callback(int) = NULL);
83  bool ReadPLY(const char *fname, bool progress_callback(int) = NULL);
84  bool WriteGMS(const char *fname, bool progress_callback(int) = NULL) const;
85  bool WriteDAE(const char *fname, bool progress_callback(int) = NULL) const;
86  bool WriteWRL(const char *fname, bool progress_callback(int) = NULL) const;
87  bool WriteOBJ(const char *fname, bool progress_callback(int) = NULL) const;
88  bool WritePLY(const char *fname, bool progress_callback(int) = NULL) const;
89  bool WriteDXF(const char *fname, bool progress_callback(int) = NULL) const;
90  void FreeData();
91 
92  uint AddSurfaceType(const vtString &surface_texture, bool bTiled = false);
93  void SetSurfaceType(int iTri, int surface_type);
94 
95  bool ComputeExtents();
96  void Offset(const DPoint2 &p);
97  void Scale(float fFactor);
98  void VertOffset(float fAmount);
99  bool ConvertProjection(const vtProjection &proj_new);
100 
101  // Accessors
102  void GetVert(int v, DPoint2 &p, float &z) const { p = m_vert.GetAt(v); z = m_z[v]; }
103  void GetTri(int t, int &v0, int &v1, int &v2) const { v0 = m_tri[t*3]; v1 = m_tri[t*3+1]; v2 = m_tri[t*3+2]; }
104  int *GetAtTri(int t) const { return m_tri.GetData() + (t*3); }
105 
106  // Implement required vtHeightField methods
107  virtual bool FindAltitudeOnEarth(const DPoint2 &p, float &fAltitude,
108  bool bTrue = false) const;
109  virtual bool FindAltitudeAtPoint(const FPoint3 &p3, float &fAltitude,
110  bool bTrue = false, int iCultureFlags=0, FPoint3 *vNormal = NULL) const;
111 
112  // Avoid implementing HeightField3d virtual methods
113  bool CastRayToSurface(const FPoint3 &point, const FPoint3 &dir,
114  FPoint3 &result) const { return false; }
115 
116  void CleanupClockwisdom();
117  int RemoveUnusedVertices();
118  void AppendFrom(vtTin *pTin);
119  double GetTriMaxEdgeLength(int iTri) const;
120  void MergeSharedVerts(bool progress_callback(int) = NULL);
121  bool HasVertexNormals() const { return m_vert_normal.GetSize() != 0; }
122  int RemoveTrianglesBySegment(const DPoint2 &ep1, const DPoint2 &ep2);
123  void SetupTriangleBins(int bins, bool progress_callback(int) = NULL);
124  int MemoryNeededToLoad() const;
125 
126  vtProjection m_proj;
127 
128 protected:
129  bool TestTriangle(int tri, const DPoint2 &p, float &fAltitude) const;
130  bool _ReadTin(FILE *fp);
131  bool _ReadTinHeader(FILE *fp);
132  bool _ReadTinBody(FILE *fp);
133  bool _ReadTinOld(FILE *fp);
134 
135  void _UpdateIndicesInInBin(int bin);
136  void _CompareBins(int bin1, int bin2);
137 
138  DLine2 m_vert;
139  vtArray<float> m_z;
140  vtArray<int> m_tri;
141  FLine3 m_vert_normal;
142 
143  // Surface Types
144  vtArray<int> m_surfidx;
145  vtStringArray m_surftypes;
146  vtArray<bool> m_surftype_tiled;
147 
148  // These members are used only during MergeSharedVerts
149  int *m_bReplace;
150  Bin *m_vertbin;
151  Bin *m_tribin;
152 
153  // This is used to speed up FindAltitudeOnEarth
154  BinArray *m_trianglebins;
155  DPoint2 m_BinSize;
156 
157  int m_file_data_start, m_file_verts, m_file_tris; // Used while reading ITF
158 };
159 
160 
161 #endif // VTTINH