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

Revision 384, 2.7 KB checked in by mattausch, 19 years ago (diff)

fixed bug in pvs criterium, computing real area

Line 
1#include "ViewCell.h"
2#include "Mesh.h"
3#include "Intersectable.h"
4#include "MeshKdTree.h"
5#include "Triangle3.h"
6
7ViewCell::HierarchyType ViewCell::sHierarchy = BSP;
8
9ViewCell::ViewCell(): MeshInstance(NULL), mPiercingRays(0)
10{
11}
12
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
31ViewCellPvs &ViewCell::GetPvs()
32{
33        return mPvs;
34}
35
36int ViewCell::Type() const
37{
38        return VIEW_CELL;
39}
40
41void ViewCell::DeriveViewCells(const ObjectContainer &objects,
42                                                           ViewCellContainer &viewCells,
43                                                           const int maxViewCells)
44{
45        // maximal max viewcells
46        int limit = maxViewCells > 0 ?
47                Min((int)objects.size(), maxViewCells) : (int)objects.size();
48
49        for (int i = 0; i < limit; ++ i)
50        {
51                Intersectable *object = objects[i];
52               
53                // extract the mesh instances
54                if (object->Type() == Intersectable::MESH_INSTANCE)
55                {
56                        MeshInstance *inst = dynamic_cast<MeshInstance *>(object);
57
58                        ViewCell *viewCell = Generate(inst->GetMesh());
59                        viewCells.push_back(viewCell);
60                }
61                //TODO: transformed meshes
62        }
63}
64
65ViewCell *ViewCell::ExtrudeViewCell(const Triangle3 &baseTri, const float height)
66{
67        // one mesh per view cell
68        Mesh *mesh = new Mesh();
69       
70        //-- construct prism
71
72        // bottom
73        mesh->mFaces.push_back(new Face(2,1,0));
74        // top
75    mesh->mFaces.push_back(new Face(3,4,5));
76        // sides
77        mesh->mFaces.push_back(new Face(1, 4, 3, 0));
78        mesh->mFaces.push_back(new Face(2, 5, 4, 1));
79        mesh->mFaces.push_back(new Face(3, 5, 2, 0));
80
81        //--- extrude new vertices for top of prism
82        Vector3 triNorm = baseTri.GetNormal();
83
84        Triangle3 topTri;       
85
86        // add base vertices and calculate top vertices
87        for (int i = 0; i < 3; ++ i)
88                mesh->mVertices.push_back(baseTri.mVertices[i]);
89       
90        // add top vertices     
91        for (int i = 0; i < 3; ++ i)
92                mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm);
93       
94        mesh->Preprocess();
95
96        return Generate(mesh);
97}
98
99ViewCell *ViewCell::Merge(ViewCell &front, ViewCell &back)
100{
101        ViewCell *vc = Generate();
102        // merge pvs
103        vc->mPvs.Merge(front.mPvs, back.mPvs);
104
105        // merge ray sets
106        stable_sort(front.mPiercingRays.begin(), front.mPiercingRays.end());
107        stable_sort(back.mPiercingRays.begin(), back.mPiercingRays.end());
108
109        std::merge(front.mPiercingRays.begin(), front.mPiercingRays.end(),
110                           back.mPiercingRays.begin(), back.mPiercingRays.end(),
111                           vc->mPiercingRays.begin());
112
113        return vc;
114}
115
116void ViewCell::AddPassingRay(const Ray &ray, const int contributions)
117{
118        mPassingRays.AddRay(ray, contributions);
119}
Note: See TracBrowser for help on using the repository browser.