source: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp @ 419

Revision 419, 2.8 KB checked in by mattausch, 19 years ago (diff)
RevLine 
[235]1#include "ViewCell.h"
2#include "Mesh.h"
[308]3#include "Intersectable.h"
[235]4#include "MeshKdTree.h"
5#include "Triangle3.h"
6
[366]7ViewCell::HierarchyType ViewCell::sHierarchy = BSP;
8
[352]9ViewCell::ViewCell(): MeshInstance(NULL), mPiercingRays(0)
[265]10{
11}
[235]12
[372]13ViewCell::ViewCell(Mesh *mesh): MeshInstance(mesh), mPiercingRays(0)
14{
15}
16
17ViewCell *ViewCell::Generate(Mesh *mesh)
18{
19        switch(sHierarchy)
20        {
21        case KD:
22                return new ViewCell(mesh);
23        case BSP:
24                return new BspViewCell(mesh);
25        default:
26                Debug << "should not come here" << endl;
27                return NULL;
28        }
29}
30
[419]31const ViewCellPvs &ViewCell::GetPvs() const
32{
33        return mPvs;
34}
35
[372]36ViewCellPvs &ViewCell::GetPvs()
37{
38        return mPvs;
39}
40
[235]41int ViewCell::Type() const
42{
[308]43        return VIEW_CELL;
[235]44}
45
46void ViewCell::DeriveViewCells(const ObjectContainer &objects,
47                                                           ViewCellContainer &viewCells,
[239]48                                                           const int maxViewCells)
[235]49{
[237]50        // maximal max viewcells
[366]51        int limit = maxViewCells > 0 ?
52                Min((int)objects.size(), maxViewCells) : (int)objects.size();
[237]53
[338]54        for (int i = 0; i < limit; ++ i)
[235]55        {
56                Intersectable *object = objects[i];
[256]57               
[235]58                // extract the mesh instances
59                if (object->Type() == Intersectable::MESH_INSTANCE)
60                {
61                        MeshInstance *inst = dynamic_cast<MeshInstance *>(object);
62
[366]63                        ViewCell *viewCell = Generate(inst->GetMesh());
[235]64                        viewCells.push_back(viewCell);
65                }
[237]66                //TODO: transformed meshes
[235]67        }
[372]68}
69
70ViewCell *ViewCell::ExtrudeViewCell(const Triangle3 &baseTri, const float height)
[260]71{
[384]72        // one mesh per view cell
[260]73        Mesh *mesh = new Mesh();
[261]74       
75        //-- construct prism
[260]76
[261]77        // bottom
[264]78        mesh->mFaces.push_back(new Face(2,1,0));
[261]79        // top
[264]80    mesh->mFaces.push_back(new Face(3,4,5));
[261]81        // sides
[264]82        mesh->mFaces.push_back(new Face(1, 4, 3, 0));
83        mesh->mFaces.push_back(new Face(2, 5, 4, 1));
84        mesh->mFaces.push_back(new Face(3, 5, 2, 0));
[261]85
86        //--- extrude new vertices for top of prism
87        Vector3 triNorm = baseTri.GetNormal();
88
89        Triangle3 topTri;       
90
91        // add base vertices and calculate top vertices
[384]92        for (int i = 0; i < 3; ++ i)
93                mesh->mVertices.push_back(baseTri.mVertices[i]);
[312]94       
[261]95        // add top vertices     
[384]96        for (int i = 0; i < 3; ++ i)
[312]97                mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm);
[261]98       
[263]99        mesh->Preprocess();
100
[372]101        return Generate(mesh);
102}
103
104ViewCell *ViewCell::Merge(ViewCell &front, ViewCell &back)
105{
[366]106        ViewCell *vc = Generate();
[362]107        // merge pvs
108        vc->mPvs.Merge(front.mPvs, back.mPvs);
109
110        // merge ray sets
111        stable_sort(front.mPiercingRays.begin(), front.mPiercingRays.end());
[352]112        stable_sort(back.mPiercingRays.begin(), back.mPiercingRays.end());
113
[362]114        std::merge(front.mPiercingRays.begin(), front.mPiercingRays.end(),
115                           back.mPiercingRays.begin(), back.mPiercingRays.end(),
116                           vc->mPiercingRays.begin());
[352]117
[372]118        return vc;
119}
120
121void ViewCell::AddPassingRay(const Ray &ray, const int contributions)
122{
123        mPassingRays.AddRay(ray, contributions);
[366]124}
Note: See TracBrowser for help on using the repository browser.