vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SNCH_Object_Data.h
Go to the documentation of this file.
1 #ifndef SNCH_OBJECT_DATA_H
2 #define SNCH_OBJECT_DATA_H
3 
5 #include <vrecko/Ability.h>
6 #include <osg/Node>
7 #include <osg/Geode>
8 #include <osg/Geometry>
9 
10 using namespace vrecko;
11 
12 namespace APSpacePartitioning {
13 
14 // Flags for SNCH_Object_Data::FaceSNCH::dwSNCHFlags:
15 #define SNCH_FACE_DEGENERATED 0x00000001
16  // The triangle is degenerated. In our context it might mean not only that
17  // two points have the same coordinates, but also that triangle has surface
18  // of size 0.
19 
20 
21 // Flags for SNCH_Object_Data::FaceSNCH::edgeFlags:
22 #define SNCH_EDGE_CONCAVE_BREAK 0x00000001
23  // The edge forms a concave seam or the edge is a terminating one (belongs to only a single triangle)
24 
25 
26 // Id for no face group
27 #define FACESNCH_NO_GROUP 0xFFFFFFFF
28  // SNCH_Object_Data::createFaceGroups() method performs optimizations based on the fact,
29  // that FACESNCH_NO_GROUP is last unsigned int value... so DON'T CHANGE IT
30 
31 
33 public:
34  //
35  // constructors / destructors
36  //
39 
40  virtual bool isHierarchyCreated() { return bSNCHierarchyCreated; }
41 
42  struct VertexSNCH : public VertexBase {
43  std::vector<unsigned long> faces;
44  // ONLY for representants!
45  // A list of faces containing this vertex, sorted CLOCKWISE.
46  osg::Vec3 averageNormal;
47  unsigned long dwRepresentant;
48  // if several vertices lie on the same position, one of the vertices
49  // is chosen as a representant and all of the vertices are pointing
50  // to the selected vertex. ONLY that vertex will have correct information
51  // in the [faces] array.
52  // The representant itself and any vertex that is not sharing position with other vertex,
53  // are pointing to itself (being its own representant).
54  };
55  virtual unsigned long getVertexStructureSize() { return sizeof(VertexSNCH); }
56  virtual void allocateVertexArray(unsigned long numVertices) { vertices = new VertexSNCH[numVertices]; vertex_size = getVertexStructureSize(); }
57  virtual void deallocateVertexArray() { if (vertices) { delete[] ((VertexSNCH*)vertices); } }
58  virtual bool saveVerticesToFile(vrecko::BufferedFileWrite *bfile);
59  virtual bool loadVerticesFromFile(vrecko::BufferedFile *bfile);
60 
61  inline VertexSNCH* getSNCHVertex(unsigned long index) { return (VertexSNCH*)getVertex(index); }
62 
63  struct FaceSNCH : public FaceBase {
64  unsigned long dwSNCHFlags; // combination of SNCH_FACE_*
65  unsigned long fIndex[3]; // neighbouring faces (0xFFFFFFFF if none)
66  unsigned long edgeFlags[3]; // combination of SNCH_EDGE_*
67 
68  unsigned long dwGroup;
69  // Id of a group that contains given face. (FACESNCH_NO_GROUP is for "no group", though this will not be used in any face.)
70  };
71  virtual unsigned long getFaceStructureSize() { return sizeof(FaceSNCH); }
72  virtual void allocateFaceArray(unsigned long numFaces) { faces = new FaceSNCH[numFaces]; face_size = getFaceStructureSize(); }
73  virtual void deallocateFaceArray() { if (faces) { delete[] ((FaceSNCH*)faces); }}
74  virtual bool saveFacesToFile(vrecko::BufferedFileWrite *bfile);
75  virtual bool loadFacesFromFile(vrecko::BufferedFile *bfile);
76 
77  inline FaceSNCH* getSNCHFace(unsigned long index) { return (FaceSNCH*)getFace(index); }
78 
79 
80  struct STNodeSNCH : public STNodeSphere {
81  osg::Vec3 cone_axis;
82  float half_spread_angle; // radians perhaps?
83 
84  // NOTE: face_index variable from STNodeSphere is also used here FOR NON-LEAF NODES.
85  // It represents the "best" triangle, that will be used for quick answer if the subsequent
86  // tree tests were assumed unnecessary.
87  };
88  virtual void* allocateSTNode() { return new STNodeSNCH; }
89  virtual unsigned long getSTNodeStructureSize() { return sizeof(STNodeSNCH); }
90  virtual bool saveTreeNodeToFile(void* node, vrecko::BufferedFileWrite *bfile);
91  virtual bool loadTreeNodeFromFile(void* node, vrecko::BufferedFile *bfile);
92 
93 // typedef std::map<osg::Vec3, osg::Vec3> NormalsMap;
94 
95 protected:
96 
98 
99 // NormalsMap edge_normal;
100 
101 // virtual void createTreeFromGeometry(osg::Node *pNode);
102 
103  bool calculateGeometryProperties(void);
104  // Calculates the triangle flags etc.
105  // (= which triangle is degenerated, ...)
106 
107  bool generateVertexNeighbourhood(void);
108  // Iterates through all the vertices and generates a list of triangles in the neigbourhood
109  // of each vertex.
110  // Also fills out correct info into [dwRepresentant] and [averageNormal] vertex variables.
111 
112  bool createEdgeGroups(void);
113  // Considers all edges and searches for concave breaks.
114 
115  bool createFaceGroups(void);
116  // Assigns a group to every triangle based on some criteria.
117  // _PROBABLY_ mostly: similarity with nearby vertices.
118  // Neighbouring vertices with similar normal will be in the same group.
119  // ASSUMES that the face normals are normalized.
120 
121  bool computeSNCH(void *pSTNode);
122 
123  void calcSubtreeTriangleRepresentant(void *pNode, osg::Vec3 *axis, unsigned long *triangle, float *bestValue = NULL);
124  // Which triangle best represents the given subtree?
125  // [bestValue] is a temporary variable to help with the calculation. Must be NULL at start.
126 
128  DebugLevelStats() : cntNodes(0), cntAngle(0.0f), cntRadius(0.0f) {};
129 
130  int cntNodes;
131  float cntAngle;
132  float cntRadius;
133  };
134 
135  struct DebugStats {
136  DebugStats() : totalNodes(0), twoTotal(0), twoNeighbours(0) {};
137 
138  int totalNodes;
139  int twoTotal;
142  };
143 
144  void writeDebugStatistics();
145 
146  void wdsInfoRec(void *pSTNode, int level, DebugStats *ds);
147 
148 
149  virtual bool constructCacheDataName(char *pOutDataName, int* version);
150  virtual bool loadDataFromPersistentCache_Init();
151 
152 
153  virtual bool coreHierarchyCreation(void);
154 };
155 
156 }
157 
158 #endif
159 
160