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

Revision 352, 2.9 KB checked in by mattausch, 19 years ago (diff)
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        // one mesh per view cell
51        Mesh *mesh = new Mesh();
52       
53        //-- construct prism
54
55        // bottom
56        mesh->mFaces.push_back(new Face(2,1,0));
57        // top
58    mesh->mFaces.push_back(new Face(3,4,5));
59        // sides
60        mesh->mFaces.push_back(new Face(1, 4, 3, 0));
61        mesh->mFaces.push_back(new Face(2, 5, 4, 1));
62        mesh->mFaces.push_back(new Face(3, 5, 2, 0));
63
64        //--- extrude new vertices for top of prism
65        Vector3 triNorm = baseTri.GetNormal();
66
67        Triangle3 topTri;       
68
69        // add base vertices and calculate top vertices
70        for (int i = 0; i < 3; ++ i)
71                 mesh->mVertices.push_back(baseTri.mVertices[i]);
72       
73        // add top vertices     
74        for (i = 0; i < 3; ++ i)
75                mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm);
76       
77        mesh->Preprocess();
78
79        return new ViewCell(mesh);
80}
81
82ViewCell *ViewCell::MergeViewCells(const ViewCell &front, const ViewCell &back)
83{
84        /*stable_sort(front.mPiercingRays.begin(), front.mPiercingRays.end());
85        stable_sort(back.mPiercingRays.begin(), back.mPiercingRays.end());
86
87        ViewCell *vc = front.Merge(back);
88
89        if (vc)
90                return vc;
91
92        return back.Merge(front);*/return NULL;
93}
94
95ViewCell *ViewCell::Merge(const ViewCell &other) const
96{
97        //-- compute set differences
98        const float minDif = 10;
99
100        RayContainer diff;
101
102        set_difference(mPiercingRays.begin(), mPiercingRays.end(),
103                                   other.mPiercingRays.begin(), other.mPiercingRays.end(),
104                                   diff.begin());
105
106        if (diff.size() < minDif)
107        {
108                ViewCell *vc = new ViewCell();
109
110                RayContainer::const_iterator it, it_end = other.mPiercingRays.end();
111
112                for (it = other.mPiercingRays.begin(); it != it_end; ++ it)
113                        vc->mPiercingRays.push_back(*it);
114
115                while (!diff.empty())
116                {
117                        vc->mPiercingRays.push_back(diff.back());
118                        diff.pop_back();
119                }
120
121                return vc;
122        }
123
124        return NULL;
125}
126
127
128void ViewCell::AddPassingRay(const Ray &ray, const int contributions)
129{
130        mPassingRays.AddRay(ray, contributions);
131}
Note: See TracBrowser for help on using the repository browser.