Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
GeometryUtils.h
1 //
2 // GeometryUtils.h
3 //
4 // Copyright (c) 2011-2012 Virtual Terrain Project
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef VTOSG_GEOMETRYUTILSH
9 #define VTOSG_GEOMETRYUTILSH
10 
11 #include "vtlib/core/Structure3d.h"
12 
13 class vtBuilding3d;
14 class vtLevel;
15 
16 namespace OSGGeomUtils {
17 
18 osg::PrimitiveSet* MakeAPrimitiveSet(const int VertexType,
19  const osg::PrimitiveSet::Type Type, const osg::PrimitiveSet::Mode Mode);
20 
21 class Arrays
22 {
23 public:
24  int m_VertexType;
25  osg::ref_ptr<osg::Vec3Array> m_Vertices;
26  osg::ref_ptr<osg::Vec3Array> m_Normals;
27  osg::ref_ptr<osg::Vec2Array> m_TexCoords;
28 };
29 
30 class DATriangles : public osg::DrawArrays, public Arrays
31 {
32 public:
33  DATriangles(const int VertexType, const osg::PrimitiveSet::Mode Mode);
34 };
35 
36 class DALLineStrip : public osg::DrawArrayLengths, public Arrays
37 {
38 public:
39  DALLineStrip(const int VertexType, const osg::PrimitiveSet::Mode Mode);
40 };
41 
42 class DALTriangleFan : public osg::DrawArrayLengths, public Arrays
43 {
44 public:
45  DALTriangleFan(const int VertexType, const osg::PrimitiveSet::Mode Mode);
46 };
47 
48 class PrimSetCacheEntry : public osg::Referenced
49 {
50 public:
51  PrimSetCacheEntry(osg::PrimitiveSet* PrimitiveSet) : m_pPrimitiveSet(PrimitiveSet) {}
52  osg::ref_ptr<osg::PrimitiveSet> m_pPrimitiveSet;
53 protected:
54  ~PrimSetCacheEntry() {}
55 };
56 
57 class PrimSetCache : public std::vector<osg::ref_ptr<PrimSetCacheEntry> >,
58  public osg::Referenced
59 {
60 public:
61  osg::PrimitiveSet* FindOrCreatePrimitive(const int VertexType,
62  const vtMaterial* pMaterial, const osg::PrimitiveSet::Type Type,
63  const osg::PrimitiveSet::Mode Mode);
64 protected:
65  ~PrimSetCache() {}
66 };
67 
68 class StateSetCacheEntry : public osg::Referenced
69 {
70 public:
71  StateSetCacheEntry(osg::StateSet& StateSet) : m_StateSet(StateSet)
72  {
73  m_pPrimSetCache = new PrimSetCache;
74  }
75  osg::StateSet& m_StateSet;
76  osg::ref_ptr<PrimSetCache> m_pPrimSetCache;
77 protected:
78  ~StateSetCacheEntry() {}
79 };
80 
81 class StateSetCache : public std::vector<osg::ref_ptr<StateSetCacheEntry> >,
82  public osg::Referenced
83 {
84 public:
85  osg::PrimitiveSet* FindOrCreatePrimitive(const int VertexType,
86  vtMaterial* pMaterial, const osg::PrimitiveSet::Type Type,
87  const osg::PrimitiveSet::Mode Mode);
88 protected:
89  ~StateSetCache() {}
90 };
91 
92 class VertexTypeCacheEntry : public osg::Referenced
93 {
94 public:
95  VertexTypeCacheEntry(const int VertexType) : m_VertexType(VertexType)
96  {
97  m_pStateSetCache = new StateSetCache;
98  }
99  int m_VertexType;
100  osg::ref_ptr<StateSetCache> m_pStateSetCache;
101 protected:
103 };
104 
105 class PrimitiveCache : public std::vector<osg::ref_ptr<VertexTypeCacheEntry> >,
106  public osg::Referenced
107 {
108 public:
109  DATriangles* FindOrCreateDATriangles(const int VertexType,
110  vtMaterial* pMaterial);
111  DALLineStrip* FindOrCreateDALLineStrip(const int VertexType, vtMaterial* pMaterial);
112  DALTriangleFan* FindOrCreateDALTriangleFan(const int VertexType, vtMaterial* pMaterial);
113  osg::PrimitiveSet* FindOrCreatePrimitive(const int VertexType, vtMaterial* pMaterial,
114  const osg::PrimitiveSet::Type Type, const osg::PrimitiveSet::Mode Mode);
115  vtGeode* Realise(bool bUseVertexBufferObjects = false) const;
116 protected:
117  ~PrimitiveCache() {}
118 };
119 
120 class GeometryBuilder : public osg::Referenced,
121  public vtStructure3d // subclass vtStructure3d to get hold of shared material functions
122 {
123 public:
124  GeometryBuilder(const vtBuilding3d& Building) : m_Building(Building) {}
125  vtGeode* Generate();
126 protected:
127  void AddFlatRoof(const FPolygon3 &pp, const vtLevel *pLev);
128  void CreateUniformLevel(int iLevel, float fHeight, int iHighlightEdge);
129  bool MakeFacade(vtEdge *pEdge, FLine3 &quad, int stories);
130  osg::Vec3 Normal(const vtVec3 &p0, const vtVec3 &p1, const vtVec3 &p2);
131  void AddWallSection(vtEdge *pEdge, bool bUniform,
132  const FLine3 &quad, float vf1, float vf2, float hf1 = -1.0f);
133  void AddHighlightSection(vtEdge *pEdge, const FLine3 &quad);
134  float MakeFelkelRoof(const FPolygon3 &EavePolygons, const vtLevel *pLev);
135  bool Collinear2d(const FPoint3& Previous, const FPoint3& Current, const FPoint3& Next);
136  void CreateUpperPolygon(const vtLevel *lev, FPolygon3 &polygon, FPolygon3 &polygon2);
137  void CreateEdgeGeometry(const vtLevel *pLev, const FPolygon3 &polygon1,
138  const FPolygon3 &polygon2, int iEdge, bool bShowEdge);
139  void AddWallNormal(vtEdge *pEdge, vtEdgeFeature *pFeat, const FLine3 &quad);
140  void AddWindowSection(vtEdge *pEdge, vtEdgeFeature *pFeat, const FLine3 &quad);
141  void AddDoorSection(vtEdge *pEdge, vtEdgeFeature *pFeat, const FLine3 &quad);
142  int FindVertex(FPoint3 Point, FLine3 &RoofSection3D, vtArray<int> &iaVertices);
143  ~GeometryBuilder() {}
144  const vtBuilding3d& m_Building;
145  osg::ref_ptr<PrimitiveCache> m_pPrimitiveCache;
146 
147  // abstract memebers
148  osg::Node *GetContained(void) { return NULL; }
149  bool CreateNode(vtTerrain *) { return false; }
150  bool IsCreated(void) { return false; }
151  void DeleteNode(void) {}
152 };
153 
154 class osg::Geometry* FindOrCreateGeometryObject(osg::Geode *pGeode,
155  vtMaterial& Material, const int ArraysRequired);
156 class osg::PrimitiveSet* FindOrCreatePrimitiveSet(osg::Geometry* pGeometry,
157  const osg::PrimitiveSet::Mode Mode, const osg::PrimitiveSet::Type Type);
158 
159 }; // namespace OSGGeomUtils
160 
161 #endif // VTOSG_GEOMETRYUTILSH
162