#ifndef _ViewCellsManager_H__ #define _ViewCellsManager_H__ #include "Ray.h" #include "VssRay.h" #include "Containers.h" #include "ViewCell.h" class ViewCell; class Intersectable; class RenderSimulator; class Renderer; class Mesh; struct Triangle3; class SimulationStatistics; class BspTree; class KdTree; class VspKdTree; class VspBspTree; class KdNode; class KdLeaf; class VspKdTree; class AxisAlignedBox3; class BspLeaf; class ViewCellsStatistics; class Exporter; class Beam; struct BspRay; /** Manages different higher order operations on the view cells. */ class ViewCellsManager { public: struct PvsStatistics { int minPvs; int maxPvs; float avgPvs; int viewcells; }; /// 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(); virtual ~ViewCellsManager(); /** Constructs view cells container taking a preprocessor @returns construction rays. */ int Construct(VssRayContainer &rays); /** Constructs view cell container with a given number of samples. */ virtual int Construct(const ObjectContainer &objects, const VssRayContainer &rays) = 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 VssRayContainer &rays); /** 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 void ComputeSampleContributions(VssRay &ray); /** Prints out statistics of the view cells. */ virtual void PrintStatistics(ostream &s) const; /** Post processes view cells givem´a number of rays. */ virtual int PostProcess(const ObjectContainer &objects, const VssRayContainer &rays) = 0; /** Show visualization of the view cells. */ virtual void Visualize(const ObjectContainer &objects, const VssRayContainer &sampleRays) = 0; /** type of the view cell container. */ virtual int GetType() const = 0; /** 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 */ virtual bool LoadViewCells(const string filename, ObjectContainer *objects); /** 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; /** cast line segment to get a list of unique viewcells which are intersected by this line segment */ virtual int CastLineSegment(const Vector3 &origin, const Vector3 &termination, ViewCellContainer &viewcells ) = 0; virtual void GetPvsStatistics(PvsStatistics &stat); /** Get a viewcell containing the specified point */ virtual ViewCell *GetViewCell(const Vector3 &point) = 0; virtual void PrintPvsStatistics(ostream &s); /** Returns probability that view point lies in one view cell. */ virtual float GetProbability(ViewCell *viewCell) = 0; /** Returns render cost of a single view cell given the render cost of an object. */ virtual float GetRendercost(ViewCell *viewCell, float objRendercost) const = 0; /** Returns vector of loaded / generated view cells. */ ViewCellContainer &GetViewCells(); /** Helper function used to split ray sets uniformly into one that is currently used and the other that is saved for later processing. @param sourceRays the input ray set @param maxSize the maximal number of rays that will be used @param usedRays returns the used ray set @param savedRays if not null, returns the saved ray set */ void GetRaySets(const VssRayContainer &sourceRays, const int maxSize, VssRayContainer &usedRays, VssRayContainer *savedRays = NULL) const; /** Returns accumulated area of all view cells. */ float GetAccVcArea(); /** Returns area of one view cell. */ virtual float GetArea(ViewCell *viewCell) const; /** Returns volume of view cells. */ virtual float GetVolume(ViewCell *viewCell) const; /** Sets the current renderer mainly for view cells statistics. */ void SetRenderer(Renderer *renderer); /** Computes a (random) view point in the valid view space. @returns true if valid view point was found */ virtual bool GetViewPoint(Vector3 &viewPoint) const; /** Returns true if this view point is in the valid view space. */ virtual bool ViewPointValid(const Vector3 &viewPoint) const; /** Sets a view space boundary. */ void SetViewSpaceBox(const AxisAlignedBox3 &box); AxisAlignedBox3 GetViewSpaceBox() const; /** Creates mesh for this view cell. */ virtual void CreateMesh(ViewCell *vc) = NULL; /** Writes view cells to disc. */ virtual bool ExportViewCells(const string filename); /** Casts beam to collect view cells. */ virtual int CastBeam(Beam &beam); /** Checks if view cell is considered as valid. */ virtual bool CheckValid(ViewCell *vc) const; /** Returns maximal allowed pvs size. */ int GetMaxPvsSize() const; /** Exports view cell geometry. */ virtual void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const = 0; protected: void ParseEnvironment(); /** Creates unique view cell ids. */ void CreateUniqueViewCellIds(); /** Recollects view cells and resets statistics. */ void ResetViewCells(); /** Collects the view cells in the view cell container. */ virtual void CollectViewCells() = 0; /** Evaluates view cells statistics and stores it in mViewCellsStatistics. */ void EvaluateViewCellsStats(); //-- helper functions for view cell visualization /** Exports the view cell partition. */ void ExportViewCellsForViz(Exporter *exporter) const; /** Sets exporter color. */ virtual void ExportColor(Exporter *exporter, ViewCell *vc) const = 0; /** Creates meshes from the view cells. */ void CreateViewCellMeshes(); /** Exports single view cell. NOTE: should be in exporter!! */ void ExportViewCell(ViewCell *viewCell, ofstream &stream); /// the view cell corresponding to space ouside the valid view space //ViewCell *mOutOfBoundsCell; /// Renders the view cells. Renderer *mRenderer; /// 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; float mTotalAreaValid; float mTotalArea; int mMaxPvsSize; float mMaxPvsRatio; ViewCellsStatistics mViewCellsStats; /// the scene bounding box AxisAlignedBox3 mViewSpaceBox; /// holds the view cell meshes MeshContainer mMeshContainer; /// if view cells should be exported bool mExportViewCells; //-- visualization options /// color code for view cells int mColorCode; bool mExportGeometry; bool mExportRays; bool mViewCellsFinished; }; /** Manages different higher order operations on the view cells. */ class BspViewCellsManager: public ViewCellsManager { public: /** Constructor taking the bsp tree and the number of samples used to construct the bsp tree. */ BspViewCellsManager(BspTree *tree, int constructionSamples); ~BspViewCellsManager(); int Construct(const ObjectContainer &objects, const VssRayContainer &rays); int PostProcess(const ObjectContainer &objects, const VssRayContainer &rays); void Visualize(const ObjectContainer &objects, const VssRayContainer &sampleRays); int GetType() const; ViewCell *GenerateViewCell(Mesh *mesh = NULL) const; bool ViewCellsConstructed() const; //void PrintStatistics(ostream &s) const; int CastLineSegment(const Vector3 &origin, const Vector3 &termination, ViewCellContainer &viewcells); float GetProbability(ViewCell *viewCell); float GetRendercost(ViewCell *viewCell, float objRendercost) const; /** Get a viewcell containing the specified point */ ViewCell *GetViewCell(const Vector3 &point); void CreateMesh(ViewCell *vc); void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const; protected: void CollectViewCells(); /** 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; void ConstructBspRays(const VssRayContainer &rays, const int numSamples); void ExportColor(Exporter *exporter, ViewCell *vc) const; /// the BSP tree. BspTree *mBspTree; vector mBspRays; private: /** Exports visualization of the BSP splits. */ void ExportSplits(const ObjectContainer &objects); /** Exports visualization of the BSP PVS. */ void ExportBspPvs(const ObjectContainer &objects); }; /** 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); int CastLineSegment(const Vector3 &origin, const Vector3 &termination, ViewCellContainer &viewcells); int PostProcess(const ObjectContainer &objects, const VssRayContainer &rays); void Visualize(const ObjectContainer &objects, const VssRayContainer &sampleRays); int GetType() const; bool ViewCellsConstructed() const; /** Prints out statistics of this approach. */ // virtual void PrintStatistics(ostream &s) const; ViewCell *GetViewCell(const Vector3 &point) { return NULL; } float GetProbability(ViewCell *viewCell); float GetRendercost(ViewCell *viewCell, float objRendercost) const; void CreateMesh(ViewCell *vc); void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const; protected: void CollectViewCells(); KdNode *GetNodeForPvs(KdLeaf *leaf); void ExportColor(Exporter *exporter, ViewCell *vc) const; /// 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); int PostProcess(const ObjectContainer &objects, const VssRayContainer &rays); void Visualize(const ObjectContainer &objects, const VssRayContainer &sampleRays); int GetType() const; bool ViewCellsConstructed() const; //virtual void PrintStatistics(ostream &s) const; ViewCell *GenerateViewCell(Mesh *mesh) const; int CastLineSegment(const Vector3 &origin, const Vector3 &termination, ViewCellContainer &viewcells); ViewCell *GetViewCell(const Vector3 &point) { return NULL; } float GetProbability(ViewCell *viewCell); float GetRendercost(ViewCell *viewCell, float objRendercost) const; void CreateMesh(ViewCell *vc); void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const; protected: void ExportLeaves(const ObjectContainer &objects, const VssRayContainer &sampleRays); void CollectViewCells(); void ExportColor(Exporter *exporter, ViewCell *vc) const; /// the BSP tree. VspKdTree *mVspKdTree; }; /** Manages different higher order operations on the view cells. */ class VspBspViewCellsManager: public ViewCellsManager { public: VspBspViewCellsManager(VspBspTree *tree, int constructionSamples); ~VspBspViewCellsManager(); int Construct(const ObjectContainer &objects, const VssRayContainer &rays); int PostProcess(const ObjectContainer &objects, const VssRayContainer &rays); void Visualize(const ObjectContainer &objects, const VssRayContainer &sampleRays); int GetType() const; ViewCell *GenerateViewCell(Mesh *mesh = NULL) const; bool ViewCellsConstructed() const; int CastLineSegment(const Vector3 &origin, const Vector3 &termination, ViewCellContainer &viewcells); float GetProbability(ViewCell *viewCell); float GetRendercost(ViewCell *viewCell, float objRendercost) const; ViewCell *GetViewCell(const Vector3 &point); bool GetViewPoint(Vector3 &viewPoint) const; bool ViewPointValid(const Vector3 &viewPoint) const; void CreateMesh(ViewCell *vc); bool LoadViewCells(const string filename, ObjectContainer *objects); bool ExportViewCells(const string filename); int CastBeam(Beam &beam); void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const; protected: /** Merges the view cells. */ void MergeViewCells(const VssRayContainer &rays, const ObjectContainer &objects); void RefineViewCells(const VssRayContainer &rays); void CollectViewCells(); /** Returns maximal depth difference of view cell leaves in tree. */ int GetMaxTreeDiff(ViewCell *vc) const; void ExportColor(Exporter *exporter, ViewCell *vc) const; /// the view space partition BSP tree. VspBspTree *mVspBspTree; private: /** Exports visualization of the BSP splits. */ void ExportSplits(const ObjectContainer &objects, const VssRayContainer &rays); /** Exports visualization of the BSP PVS. */ void ExportBspPvs(const ObjectContainer &objects, const VssRayContainer &rays); }; #endif