#ifndef _ViewCellsManager_H__ #define _ViewCellsManager_H__ #include "Ray.h" #include "VssRay.h" #include "Containers.h" class ViewCell; class Intersectable; class RenderSimulator; class Mesh; struct Triangle3; class SimulationStatistics; class BspTree; class KdTree; class VspKdTree; class VspBspTree; class KdNode; class KdLeaf; class VspKdTree; class AxisAlignedBox3; class BspLeaf; /** Manages different higher order operations on the view cells. */ class ViewCellsManager { public: /// view cell container types enum {BSP, KD, VSP_KD, VSP_BSP}; /** Constructor taking the maximal number of samples used for construction */ ViewCellsManager(const int constructionSamples); ViewCellsManager(); ~ViewCellsManager(); /** Constructs view cell container with a given number of samples. */ virtual int Construct(const ObjectContainer &objects, const VssRayContainer &rays, AxisAlignedBox3 *sceneBbox = NULL) = 0; /** Computes sample contributions of the rays to the view cells PVS. @param rays bundle of rays used to find intersections with view cells and adding the contribution @param castRays true if ray should be cast to gain the information, false if rays already hold the view cells intersection information and need not be recast. @param sampleContributions returns the number of sample contributions @param contributingSamples returns the number of contributingSamples */ void ComputeSampleContributions(const RayContainer &rays, int &sampleContributions, int &contributingSamples, const bool castRays = true); /** Computes sample contribution of a simgle ray to the view cells PVS. @param ray finds intersections with view cells and holds the contribution @param castRay true if ray should be cast to gain the information, false if ray is already holding the information and need not be recast. @returns number of sample contributions */ virtual int ComputeSampleContributions(Ray &ray, const bool castRay = true) = 0; /** Prints out statistics of the view cells. */ virtual void PrintStatistics(ostream &s) const = 0; /** Post processes view cells givem´a number of rays. */ virtual int PostProcess(const ObjectContainer &objects, const RayContainer &rays) = 0; /** Show visualization of the view cells. */ virtual void Visualize(const ObjectContainer &objects, const RayContainer &sampleRays) = 0; /** type of the view cell container. */ virtual int GetType() const = 0; /** Simulates rendering with the given view cell partition. @returns render time statistics */ SimulationStatistics SimulateRendering() const; /** Load the input viewcells. The input viewcells should be given as a collection of meshes. Each mesh is assume to form a bounded polyhedron defining the interior of the viewcell. The method then builds a BSP tree of these view cells. @param filename file to load @return true on success */ bool LoadViewCells(const string filename); /** Constructs view cell from base triangle. The ViewCell is extruded along the normal vector. @param the base triangle @param the height of the newly created view cell */ ViewCell *ExtrudeViewCell(const Triangle3 &baseTri, const float height) const; /** Merges two view cells. @note the piercing rays of the front and back will be ordered @returns new view cell based on the merging. */ ViewCell *MergeViewCells(ViewCell &front, ViewCell &back) const; /** Generates view cell of type specified by this manager */ virtual ViewCell *GenerateViewCell(Mesh *mesh = NULL) const; /** Adds a new view cell to the list of predefined view cells. */ void AddViewCell(ViewCell *viewCell); /** Derive view cells from objects. */ void DeriveViewCells(const ObjectContainer &objects, ViewCellContainer &viewCells, const int maxViewCells) const; /** Sets maximal number of samples used for the construction of the view cells. */ void SetVisualizationSamples(const int visSamples); /** Sets maximal number of samples used for the visualization of the view cells. */ void SetConstructionSamples(const int constructionSamples); /** Sets maximal number of samples used for the post processing of the view cells. */ void SetPostProcessSamples(const int postProcessingSamples); /** See set. */ int GetVisualizationSamples() const; /** See set. */ int GetConstructionSamples() const; /** See set. */ int GetPostProcessSamples() const; /** Returns true if view cells wer already constructed. */ virtual bool ViewCellsConstructed() const = 0; protected: /** Initialises the render time simulator. */ void InitRenderSimulator(); ///////////////////// /// the view cell corresponding to unbounded space ViewCell *mUnbounded; /// Simulates rendering RenderSimulator *mRenderSimulator; /// Loaded view cells ViewCellContainer mViewCells; /// maximum number of samples taken for construction of the view cells int mConstructionSamples; int mPostProcessSamples; int mVisualizationSamples; //-- thresholds used for view cells merge int mMinPvsDif; int mMinPvs; int mMaxPvs; }; /** Manages different higher order operations on the view cells. */ class BspViewCellsManager: public ViewCellsManager { public: BspViewCellsManager(BspTree *tree, int constructionSamples); int Construct(const ObjectContainer &objects, const VssRayContainer &rays, AxisAlignedBox3 *sceneBbox); int ComputeSampleContributions(Ray &ray, const bool castRay = false); int PostProcess(const ObjectContainer &objects, const RayContainer &rays); void Visualize(const ObjectContainer &objects, const RayContainer &sampleRays); int GetType() const; ViewCell *GenerateViewCell(Mesh *mesh = NULL) const; bool ViewCellsConstructed() const; void PrintStatistics(ostream &s) const; protected: /** Merges view cells front and back leaf view cell. */ bool MergeBspLeafViewCells(BspLeaf *front, BspLeaf *back) const; /** Returns true if front and back leaf should be merged. */ bool ShouldMerge(BspLeaf *front, BspLeaf *back) const; /// the BSP tree. BspTree *mBspTree; private: /** Exports visualization of the BSP splits. */ void ExportSplits(const ObjectContainer &objects, const RayContainer &sampleRays); /** Exports visualization of the BSP PVS. */ void ExportBspPvs(const ObjectContainer &objects, const RayContainer &sampleRays); }; /** Manages different higher order operations on the KD type view cells. */ class KdViewCellsManager: public ViewCellsManager { public: KdViewCellsManager(KdTree *tree); int Construct(const ObjectContainer &objects, const VssRayContainer &rays, AxisAlignedBox3 *sceneBbox); int CastRay(const Ray &ray); int PostProcess(const ObjectContainer &objects, const RayContainer &rays); void Visualize(const ObjectContainer &objects, const RayContainer &sampleRays); int GetType() const; bool ViewCellsConstructed() const; /** Prints out statistics of this approach. */ virtual void PrintStatistics(ostream &s) const; protected: KdNode *GetNodeForPvs(KdLeaf *leaf); int ComputeSampleContributions(Ray &ray, const bool castRay = true); /// the BSP tree. KdTree *mKdTree; /// depth of the KD tree nodes with represent the view cells int mKdPvsDepth; }; /** Manages different higher order operations on the view cells for vsp kd tree view cells. */ class VspKdViewCellsManager: public ViewCellsManager { public: VspKdViewCellsManager(VspKdTree *vspKdTree, int constructionSamples); int Construct(const ObjectContainer &objects, const VssRayContainer &rays, AxisAlignedBox3 *sceneBbox); int ComputeSampleContributions(Ray &ray, const bool castRay = true); int PostProcess(const ObjectContainer &objects, const RayContainer &rays); void Visualize(const ObjectContainer &objects, const RayContainer &sampleRays); int GetType() const; bool ViewCellsConstructed() const; virtual void PrintStatistics(ostream &s) const; protected: /// the BSP tree. VspKdTree *mVspKdTree; }; /** Manages different higher order operations on the view cells. */ class VspBspViewCellsManager: public ViewCellsManager { public: VspBspViewCellsManager(VspBspTree *tree, int constructionSamples); int Construct(const ObjectContainer &objects, const VssRayContainer &rays, AxisAlignedBox3 *sceneBbox); int ComputeSampleContributions(Ray &ray, const bool castRay = false); int PostProcess(const ObjectContainer &objects, const RayContainer &rays); void Visualize(const ObjectContainer &objects, const RayContainer &sampleRays); int GetType() const; ViewCell *GenerateViewCell(Mesh *mesh = NULL) const; bool ViewCellsConstructed() const; void PrintStatistics(ostream &s) const; protected: /** Merges view cells front and back leaf view cell. */ bool MergeVspBspLeafViewCells(BspLeaf *front, BspLeaf *back) const; /** Returns true if front and back leaf should be merged. */ bool ShouldMerge(BspLeaf *front, BspLeaf *back) const; /// the view space partition BSP tree. VspBspTree *mVspBspTree; private: /** Exports visualization of the BSP splits. */ void ExportSplits(const ObjectContainer &objects, const RayContainer &sampleRays); /** Exports visualization of the BSP PVS. */ void ExportBspPvs(const ObjectContainer &objects, const RayContainer &sampleRays); }; #endif