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

Revision 362, 2.5 KB checked in by mattausch, 19 years ago (diff)

added post merging bsp view cells
fixed bug at per ray subdivision

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