1 | #include "VssRay.h"
|
---|
2 | #include "Beam.h"
|
---|
3 |
|
---|
4 |
|
---|
5 |
|
---|
6 | void
|
---|
7 | Beam::Construct(const AxisAlignedBox3 &box,
|
---|
8 | const AxisAlignedBox3 &dBox)
|
---|
9 | {
|
---|
10 | // the frustum is defined by set of negative halfspace described by mPlanes
|
---|
11 |
|
---|
12 | // construct
|
---|
13 | Vector3 center = box.Center();
|
---|
14 | Vector3 dCenter = dBox.Center();
|
---|
15 |
|
---|
16 | // "back plane"
|
---|
17 | mPlanes.push_back(Plane3(-VssRay::GetDirection(dCenter.x, dCenter.y), center));
|
---|
18 |
|
---|
19 | Vector3 directions[4];
|
---|
20 | directions[0] = VssRay::GetDirection(dBox.Min().x, dBox.Min().y);
|
---|
21 | directions[1] = VssRay::GetDirection(dBox.Max().x, dBox.Min().y);
|
---|
22 | directions[2] = VssRay::GetDirection(dBox.Max().x, dBox.Max().y);
|
---|
23 | directions[3] = VssRay::GetDirection(dBox.Min().x, dBox.Max().y);
|
---|
24 |
|
---|
25 | // side planes
|
---|
26 | mPlanes.push_back(Plane3(CrossProd(directions[0], directions[1]), center));
|
---|
27 | mPlanes.push_back(Plane3(CrossProd(directions[1], directions[2]), center));
|
---|
28 | mPlanes.push_back(Plane3(CrossProd(directions[2], directions[3]), center));
|
---|
29 | mPlanes.push_back(Plane3(CrossProd(directions[3], directions[0]), center));
|
---|
30 |
|
---|
31 |
|
---|
32 | // now make sure all planes contain the spatial box
|
---|
33 | int i, j;
|
---|
34 | for (i=0; i < mPlanes.size(); i++) {
|
---|
35 | for (j=0; j < 8; j++) {
|
---|
36 | float dist = mPlanes[i].Distance(box.GetVertex(j));
|
---|
37 | if (dist > 0)
|
---|
38 | mPlanes[i].mD -= dist;
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | mBox = box;
|
---|
43 | mDirBox = dBox;
|
---|
44 |
|
---|
45 | SetValid();
|
---|
46 | }
|
---|
47 |
|
---|
48 | // conservative frustum box intersection
|
---|
49 | // returns
|
---|
50 | // 0 - box intersects the frustum
|
---|
51 | // -1 - box intersects the frustum and is fully inside
|
---|
52 | // 1 - box does not intersect the frustum
|
---|
53 |
|
---|
54 | int
|
---|
55 | Beam::ComputeIntersection(const AxisAlignedBox3 &box)
|
---|
56 | {
|
---|
57 | int i;
|
---|
58 | int result = -1;
|
---|
59 | for (i=0; i < mPlanes.size(); i++) {
|
---|
60 | int side = box.Side(mPlanes[i]);
|
---|
61 | if (side == 1)
|
---|
62 | return 1;
|
---|
63 | if (side > result)
|
---|
64 | result = side;
|
---|
65 | }
|
---|
66 | return result;
|
---|
67 | } |
---|