source: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h @ 2743

Revision 2743, 9.7 KB checked in by mattausch, 16 years ago (diff)
Line 
1#ifndef _Preprocessor_H__
2#define _Preprocessor_H__
3
4#include <string>
5//
6#include "Containers.h"
7#include "Mesh.h"
8#include "KdTree.h"
9#include "SimpleRay.h"
10
11#define USE_RAY_POOL
12
13namespace GtpVisibilityPreprocessor {
14
15class PreprocessorThread; 
16class RenderSimulator;
17class SceneGraph;
18class Exporter;
19class ViewCellsManager;
20class BspTree;
21class VspOspTree;
22class VspBspTree;
23class RenderSimulator;
24struct VssRayContainer;
25class SamplingStrategy;
26class GlRendererBuffer;
27class VspTree;
28class HierarchyManager;
29class BvHierarchy;
30class Intersectable;
31class VssRay;
32class RayCaster;
33class GlobalLinesRenderer;
34class SceneGraphLeaf;
35class GlRendererWidget;
36class IntersectableGroup;
37class TriangleIntersectable;
38
39
40/** Hack: Visualization structure for the GVS algorithm.
41*/
42struct VizStruct
43{
44        VertexContainer enlargedTriangle;
45        TriangleIntersectable *originalTriangle;
46        VssRay *ray;
47};
48
49
50/** Namespace for the external visibility preprocessor
51
52    This namespace includes all classes which are created by the VUT (Vienna University
53    of Technology) for the External Visibility Preprocessor of the GTP (GameTools Project)
54    (www.gametools.org).
55*/
56 
57/** Main class of the visibility preprocessor. Responsible for loading and
58    saving of the input and output files. Initiates construction of the kD-tree,
59    viewcell loading/generation and the visibility computation itself.
60*/
61class Preprocessor
62{
63        friend class RayCaster;
64        friend class IntelRayCaster;
65        friend class InternalRayCaster;
66        friend class HavranRayCaster;
67
68public:
69        /** Default constructor initialising e.g., KD tree and BSP tree.
70        */
71        Preprocessor();
72
73        virtual ~Preprocessor();
74        /** Load the input scene.
75                @param filename file to load
76                @return true on success
77        */
78        virtual bool LoadScene(const string &filename);
79        /** Export all preprocessed data in a XML format understandable by the
80                PreprocessingInterface of the GtpVisibilityPreprocessor Module.
81                The file can be compressed depending on the environement settings.
82                @return true on successful export
83        */
84        virtual bool ExportPreprocessedData(const string &filename);
85        /** Build the KdTree of currently loaded occluders/occludees/viewcells. The construction
86                is driven by the environment settings, which also sais which of the three types of
87                entities should be used to drive the heuristical construction (only occluders by default)
88        */
89        virtual bool BuildKdTree();
90        /** Compute visibility method. This method has to be reimplemented by the actual
91                Preprocessor implementation (e.g. SamplingPreprocessor, ExactPreprocessor,
92                GlobalSamplingpreprocessor)
93        */
94        virtual bool ComputeVisibility() = 0;
95        /** Post Process the computed visibility. By default applys the visibility filter
96                (if specified in the environment and export the preprocessed data
97        */
98        virtual bool PostProcessVisibility();
99        /** View cells are either loaded or prepared for generation, according to the chosen environment
100                object. Important evironment options are, e.g, the view cell type.
101                Should be done after scene loading (i.e., some options are based on scene type).
102        */
103        bool PrepareViewCells();
104        /** Construct viewcells from the scratch
105        */
106        bool ConstructViewCells();
107        /** Returns the specified sample strategy, NULL if no valid strategy.
108        */
109        SamplingStrategy *GenerateSamplingStrategy(const int strategyId);
110        /** Export preprocessor data.
111        */
112        bool Export(const string &filename, const bool scene, const bool kdtree); 
113
114        virtual void KdTreeStatistics(ostream &s);
115        virtual void BspTreeStatistics(ostream &s);
116
117        /** Loads samples from file.
118        @param samples returns the stored sample rays
119        @param objects needed to associate the objects ids
120        @returns true if samples were loaded successfully
121        */
122        bool LoadSamples(VssRayContainer &samples, ObjectContainer &objects) const;
123
124        /** Exports samples to file.
125                @returns true if samples were written successfully
126        */
127        bool ExportSamples(const VssRayContainer &samples) const;
128
129        bool LoadKdTree(const string &filename);
130        bool ExportKdTree(const string &filename);
131
132        virtual bool
133        ExportRays(const char *filename,
134                           const VssRayContainer &vssRays,
135                           const int number,
136                           const bool exportScene = false
137                           );
138
139        bool ExportRayAnimation(const char *filename,
140                                                        const vector<VssRayContainer> &vssRays);
141
142        virtual int     GenerateRays(const int number,
143                                                         SamplingStrategy &strategy,
144                                                         SimpleRayContainer &rays);
145
146        virtual int     GenerateRays(const int number,
147                                                         SamplingStrategy &strategy,
148                                                         SimpleRayContainer &rays,
149                                                         int &invalidSamples);
150
151        virtual int GenerateRays(const int number,
152                                                         const int raysType,
153                                                         SimpleRayContainer &rays);
154
155        bool GenerateJitteredRays(SimpleRayContainer &rayBundle,
156                                                         const SimpleRay &mainRay,
157                                                         int number,
158                                                         int shuffleType,
159                                                         float scale) const;
160
161        virtual void CastRays(SimpleRayContainer &rays,
162                                                  VssRayContainer &vssRays,
163                                                  const bool castDoubleRays,
164                                                  const bool pruneInvalidRays = true);
165
166        virtual void
167                CastRaysWithHwGlobalLines(
168                SimpleRayContainer &rays,
169                VssRayContainer &vssRays,
170                const bool castDoubleRays,
171                const bool pruneInvalidRays
172                );
173
174        /** Compute pixel error of the current PVS solution by sampling given number of viewpoints.
175        */
176        virtual void ComputeRenderError();
177
178        /** Returns a view cells manager of the given name.
179        */
180        ViewCellsManager *CreateViewCellsManager(const char *name);
181
182        GlRendererBuffer *GetRenderer();
183
184        bool InitRayCast(const string &externKdTree, const string &internKdTree);
185
186        bool LoadInternKdTree(const string &internKdTree);
187
188        bool ExportObj(const string &filename, const ObjectContainer &objects);
189
190        Intersectable *GetParentObject(const int index) const;
191        Vector3 GetParentNormal(const int index) const;
192
193        /** Sets a preprocessor thread.
194        */
195        void SetThread(PreprocessorThread *t);
196
197        /** Returns a preprocessor thread.
198        */
199        PreprocessorThread *GetThread() const;
200
201        Intersectable *GetObjectById(const int id);
202
203        void PrepareHwGlobalLines();
204
205        virtual void DeterminePvsObjects(VssRayContainer &rays);
206
207        static bool LoadObjects(const string &filename,
208                                                        ObjectContainer &pvsObjects,
209                                                        const ObjectContainer &preprocessorObject);
210
211        /** Adds dynamic geometry
212        */
213        SceneGraphLeaf *LoadDynamicGeometry(const string &filename);
214
215        /** Register the dynamic object to be handled by the ray caster for visibility
216                computation.
217        */
218        void RegisterDynamicObject(SceneGraphLeaf *leaf);
219       
220        void PrepareObjectsForRayCaster(SceneGraphLeaf *l);
221       
222        void ScheduleUpdateDynamicObjects();
223        void UpdateDynamicObjects();
224        /** Notify the preprocessor that an object has been deleted
225        */
226        virtual void ObjectRemoved(SceneGraphLeaf *object);
227
228        SceneGraphLeaf *GenerateBoxGeometry(const AxisAlignedBox3 &box);
229
230        float _HackComputeRenderCost(ViewCell *vc);
231
232        /////////////////////////
233
234        bool mSynchronize;
235
236        /// scene graph loaded from file
237        SceneGraph *mSceneGraph;
238
239        /// raw array of objects
240        ObjectContainer mObjects;
241
242        /// kD-tree organizing the scene graph (occluders + occludees) + viewcells
243        KdTree *mKdTree;
244        AxisAlignedBox3 sceneBox;
245
246        /// View space partition bsp tree
247        VspBspTree *mVspBspTree;
248        /// BSP tree representing the viewcells
249        BspTree *mBspTree;
250
251        /// list of all loaded occluders
252        ObjectContainer mOccluders;
253        /// list of all loaded occludees
254        ObjectContainer mOccludees;
255        /// the view cells manager
256        ViewCellsManager *mViewCellsManager;
257
258        DynamicObjectsContainer mDynamicObjects;
259
260        /// greedy optimized hierarchy for both objects and view cells
261        VspOspTree *mVspOspTree;
262
263        bool mUseGlRenderer;
264        bool mUseGlDebugger;
265
266        bool mUseHwGlobalLines;
267        bool mLoadViewCells;
268
269        bool mDetectEmptyViewSpace;
270
271        bool mQuitOnFinish;
272        bool mLoadMeshes;
273        bool mComputeVisibility;
274
275        bool mExportVisibility;
276        string mVisibilityFileName;
277
278        bool mApplyVisibilityFilter;
279        bool mApplyVisibilitySpatialFilter;
280
281        float mVisibilityFilterWidth;
282
283        int mPass;
284
285        bool mStopComputation;
286
287        bool mExportObj;
288
289        bool mExportRays;
290        bool mExportAnimation;
291        int mExportNumRays;
292
293        ofstream mStats;
294
295        GlRendererBuffer *renderer;
296        GlRendererWidget *mRendererWidget;
297
298        int mTotalSamples;
299        int mCurrentSamples;
300
301        int mTotalTime;
302        int mSamplesPerPass;
303        int mSamplesPerEvaluation;
304 
305        int mTotalRaysCast;
306
307        RayCaster *mRayCaster;
308
309        // triangle pvs for gvs
310        // hack: should be in gvspreprocessor but pulled
311        // out here for visualization purpose
312        ObjectContainer mTrianglePvs;
313        vector<VizStruct> visTriangles;
314
315        // generic stats vector that can be used for anything
316        int mGenericStats[2];
317
318        //std::vector<int> mDummyBuffer;
319
320
321protected:
322
323        bool LoadBinaryObj(const std::string &filename,
324                       SceneGraphLeaf *root,
325                                           std::vector<FaceParentInfo> *parents,
326                                           float scale = -1.0f);
327
328        bool ExportBinaryObj(const std::string &filename, SceneGraphLeaf *root);
329
330        void SetupRay(Ray &ray, const Vector3 &point,
331                      const Vector3 &direction) const;
332
333        void EvalPvsStat();
334
335        virtual void EvalViewCellHistogram();
336
337        /** Notify the preprocessor that an object has moved and must be reevaluated.
338        */
339        virtual void ObjectMoved(SceneGraphLeaf *object);
340       
341
342
343        /////////////////////////
344
345        GlobalLinesRenderer *mGlobalLinesRenderer;
346
347        /// samples used for construction of the BSP view cells tree.
348        int mBspConstructionSamples;
349        /// samples used for construction of the VSP OSP tree.
350        int mVspOspConstructionSamples;
351        /// Simulates rendering of the scene.
352        RenderSimulator *mRenderSimulator;
353
354        vector<FaceParentInfo> mFaceParents;
355
356        /// if box around view space should be used
357        bool mUseViewSpaceBox;
358
359        PreprocessorThread *mThread;
360
361        bool mUpdateDynamicObjects;
362
363       
364};
365
366extern Preprocessor *preprocessor;
367
368}
369
370#endif
Note: See TracBrowser for help on using the repository browser.