Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Structure.h
Go to the documentation of this file.
1 //
2 // Structure.h
3 //
4 // Implements the vtStructure class which represents a single built structure.
5 //
6 // Copyright (c) 2001-2007 Virtual Terrain Project
7 // Free for all uses, see license.txt for details.
8 //
12 #ifndef STRUCTUREH
13 #define STRUCTUREH
14 
15 #include "MathTypes.h"
16 #include "Selectable.h"
17 #include "Content.h"
18 #include "FilePath.h"
19 
20 class vtBuilding;
21 class vtFence;
22 class vtStructInstance;
23 
24 // Well known material names
25 #define BMAT_NAME_PLAIN "Plain"
26 #define BMAT_NAME_WOOD "Wood"
27 #define BMAT_NAME_SIDING "Siding"
28 #define BMAT_NAME_BRICK "Brick"
29 #define BMAT_NAME_PAINTED_BRICK "Painted-Brick"
30 #define BMAT_NAME_ROLLED_ROOFING "Rolled-Roofing"
31 #define BMAT_NAME_CEMENT "Cement"
32 #define BMAT_NAME_CORRUGATED "Corrugated"
33 #define BMAT_NAME_DOOR "Door"
34 #define BMAT_NAME_WINDOW "Window"
35 #define BMAT_NAME_WINDOWWALL "WindowWall"
36 
37 enum vtMaterialColorEnum
38 {
39  VT_MATERIAL_COLOUR, // a single color
40  VT_MATERIAL_COLOURABLE, // any color, untextured
41  VT_MATERIAL_SELFCOLOURED_TEXTURE, // a single texture
42  VT_MATERIAL_COLOURABLE_TEXTURE // any color, textured
43 };
44 
49 {
50 public:
52  vtMaterialDescriptor(const char *name,
53  const vtString &SourceName,
54  const vtMaterialColorEnum Colorable = VT_MATERIAL_SELFCOLOURED_TEXTURE,
55  const float fUVScaleX=-1,
56  const float fUVScaleY=-1,
57  const bool bTwoSided = false,
58  const bool bAmbient = false,
59  const bool bBlended = false,
60  const RGBi &Color = RGBi(0,0,0));
62 
63  void SetName(const vtString& Name)
64  {
65  m_pName = &Name;
66  }
67  const vtString& GetName() const
68  {
69  return *m_pName;
70  }
79  void SetMatType(int type)
80  {
81  m_Type = type;
82  }
83  int GetMatType() const
84  {
85  return m_Type;
86  }
87  // UV Scale: texture units per meter, or -1 to scale to fit
88  void SetUVScale(const float fScaleX, const float fScaleY)
89  {
90  m_UVScale.Set(fScaleX, fScaleY);
91  }
92  FPoint2 GetUVScale() const
93  {
94  return m_UVScale;
95  }
96  void SetMaterialIndex(const int Index)
97  {
98  m_iMaterialIndex = Index;
99  }
100  const int GetMaterialIndex() const
101  {
102  return m_iMaterialIndex;
103  }
104  void SetColorable(const vtMaterialColorEnum Type)
105  {
106  m_Colorable = Type;
107  }
108  const vtMaterialColorEnum GetColorable() const
109  {
110  return m_Colorable;
111  }
112  void SetSourceName(const vtString &SourceName)
113  {
114  m_SourceName = SourceName;
115  }
116  const vtString& GetSourceName() const
117  {
118  return m_SourceName;
119  }
120  void SetRGB(const RGBi Color)
121  {
122  m_RGB = Color;
123  }
124  const RGBi GetRGB() const
125  {
126  return m_RGB;
127  }
128  void SetTwoSided(bool bTwoSided)
129  {
130  m_bTwoSided = bTwoSided;
131  }
132  const bool GetTwoSided()
133  {
134  return m_bTwoSided;
135  }
136  void SetAmbient(bool bAmbient)
137  {
138  m_bAmbient = bAmbient;
139  }
140  const bool GetAmbient()
141  {
142  return m_bAmbient;
143  }
144  void SetBlending(bool bBlending)
145  {
146  m_bBlending = bBlending;
147  }
148  const bool GetBlending()
149  {
150  return m_bBlending;
151  }
152  // Operator overloads
153  bool operator == (const vtMaterialDescriptor& rhs) const
154  {
155  return (*m_pName == *rhs.m_pName);
156  }
157  bool operator == (const vtMaterialDescriptor& rhs)
158  {
159  return (*m_pName == *rhs.m_pName);
160  }
161  void WriteToFile(FILE *fp);
162 
163 private:
164  const vtString *m_pName; // Name of material
165  int m_Type; // 0 for surface materials, >0 for classification type
166  vtMaterialColorEnum m_Colorable;
167  vtString m_SourceName; // Source of material
168  FPoint2 m_UVScale; // Texel scale;
169  RGBi m_RGB; // Color for VT_MATERIAL_COLOUR
170  bool m_bTwoSided; // default false
171  bool m_bAmbient; // default false
172  bool m_bBlending; // default false
173 
174  // The following field is only used in 3d construction, but it's not
175  // enough distinction to warrant creating a subclass to contain it.
176  int m_iMaterialIndex; // Starting or only index of this material in the shared materials array
177 };
178 
182 class vtMaterialDescriptorArray : public vtArray<vtMaterialDescriptor*>
183 {
184 public:
185  virtual ~vtMaterialDescriptorArray() { Empty(); free(m_Data); m_Data = NULL; m_MaxSize = 0; }
186  void DestructItems(uint first, uint last)
187  {
188  for (uint i = first; i <= last; i++)
189  delete GetAt(i);
190  }
191  bool LoadExternalMaterials();
192  const vtString *FindName(const char *matname) const;
193  void CreatePlain();
194 
195  bool Load(const char *szFileName);
196  bool Save(const char *szFileName);
197 };
198 
203 {
207  ST_NONE
208 };
209 
226 class vtStructure : public Selectable, public vtTagArray
227 {
228 public:
229  vtStructure();
230  virtual ~vtStructure();
231 
232  // use an explicit method to avoid assignment operator
233  void CopyFrom(const vtStructure &v);
234 
235  void SetType(vtStructureType t) { m_type = t; }
236  vtStructureType GetType() { return m_type; }
237 
238  void SetElevationOffset(float fOffset) { m_fElevationOffset = fOffset; }
239  float GetElevationOffset() const { return m_fElevationOffset; }
240  void SetAbsolute(bool b) { m_bAbsolute = b; }
241  bool GetAbsolute() const { return m_bAbsolute; }
242 
243  vtBuilding *GetBuilding() { if (m_type == ST_BUILDING) return (vtBuilding *)this; else return NULL; }
244  vtFence *GetFence() { if (m_type == ST_LINEAR) return (vtFence *)this; else return NULL; }
245  vtStructInstance *GetInstance() { if (m_type == ST_INSTANCE) return (vtStructInstance *)this; else return NULL; }
246 
247  virtual bool GetExtents(DRECT &rect) const = 0;
248  virtual bool IsContainedBy(const DRECT &rect) const = 0;
249  virtual void WriteXML(GZOutput &out, bool bDegrees) const = 0;
250 
251  void WriteTags(GZOutput &out) const;
252 
253 #ifdef VIAVTDATA
254  bool m_bIsVIAContributor;
255  bool m_bIsVIATarget;
256 #endif
257 
258 protected:
259  vtStructureType m_type;
260 
261  // Offset that the structure should be moved up or down relative to its
262  // default position on the ground
263  // for buildings this is (lowest corner of its base footprint)
264  // for linear features this is the lowest point of the feature.
265  // for instances this is the datum point
266  float m_fElevationOffset;
267 
268  // If true, elevation offset is relative to sealevel, not to the
269  // heightfield surface.
270  bool m_bAbsolute;
271 
272 private:
273  // Don't let unsuspecting users stumble into assuming that object
274  // copy semantics will work. Declare them private and never
275  // define them:
276  vtStructure( const vtStructure & );
277  vtStructure &operator=( const vtStructure & );
278 };
279 
292 {
293 public:
295 
296  // copy operator
298 
299  void WriteXML(GZOutput &out, bool bDegrees) const;
300  void Offset(const DPoint2 &delta) { m_p += delta; }
301 
302  bool GetExtents(DRECT &rect) const;
303  bool IsContainedBy(const DRECT &rect) const;
304 
305  virtual double DistanceToPoint(const DPoint2 &p, float fMaxRadius) const;
306 
307  void SetPoint(const DPoint2 &p) { m_p = p; }
308  DPoint2 GetPoint() const { return m_p; }
309  void SetRotation(float f) { m_fRotation = f; }
310  float GetRotation() const { return m_fRotation; }
311  void SetScale(float f) { m_fScale = f; }
312  float GetScale() const { return m_fScale; }
313  void SetItem(vtItem *pItem) { m_pItem = pItem; }
314  vtItem *GetItem() { return m_pItem; }
315 
316 protected:
317  DPoint2 m_p; // earth position
318  float m_fRotation; // in radians
319  float m_fScale; // meters per unit
320 
321  vtItem *m_pItem; // If this is an instance of a content item
322 };
323 
324 bool LoadGlobalMaterials();
325 void SetGlobalMaterials(vtMaterialDescriptorArray *mats);
326 vtMaterialDescriptorArray *GetGlobalMaterials();
327 void FreeGlobalMaterials();
328 
329 #endif // STRUCTUREH
330