9f1ca7780c6211366cb124a6688377ca6a961ffa
[MicroTrace.git] / Box.cxx
1 #include "Box.hxx"
2 #include "InfinitePlane.hxx"
3
4 Box::Box() : m_min(Infinity, Infinity, Infinity),
5 m_max(-Infinity, -Infinity, -Infinity)
6 {
7 }
8
9 Box::Box(Vec3f min, Vec3f max) : m_min(min), m_max(max)
10 {
11 }
12
13 Box::~Box()
14 {
15 }
16
17 Box::Box(const Box& b)
18 {
19 m_min = b.m_min;
20 m_max = b.m_max;
21 }
22
23 Box&
24 Box::operator=(const Box& b)
25 {
26 m_min = b.m_min;
27 m_max = b.m_max;
28 return *this;
29 }
30
31 void
32 Box::Clear()
33 {
34 }
35
36 void
37 Box::Extend(const Vec3f& a)
38 {
39 // for all three coordinates, move m_max or m_min to the point
40 for(size_t i = 0; i < 3; i++) {
41 if(a[i] > m_max[i]) {
42 m_max[i] = a[i];
43 } else if(a[i] < m_min[i]) {
44 m_min[i] = a[i];
45 } // else: do nothing, coordinate is inside the box
46 }
47 }
48
49 void
50 Box::Extend(const Box& box)
51 {
52 }
53
54 bool
55 Box::Overlaps(const Box& b) const
56 {
57 return false;
58 }
59
60 void
61 Box::Clip(const Ray& ray, float& tnear, float& tfar) const
62 {
63 // Note: equations here are the same as in InfinitePlane::Intersect()
64 // t = ((o-a)*n) / (d*n)
65 // o = ray.origin(), d = ray.direction(),
66 // n = surface normal of plane, a = anchor point of plane
67 Vec3f diff_min = m_min - ray.origin(); // o-a
68 Vec3f diff_max = m_max - ray.origin();
69
70 float cos_theta;
71 float tx_near, tx_far;
72
73 // clip along x axis
74 if(cos_theta = ray.direction().dot(Vec3f(1,0,0)) != 0) // if not parallel...
75 tx_near = diff_min.dot(Vec3f(1,0,0)) / cos_theta;
76 if(cos_theta = ray.direction().dot(Vec3f(1,0,0)) != 0)
77 tx_far = diff_max.dot(Vec3f(1,0,0)) / cos_theta;
78
79 // clip along y axis
80 if(cos_theta = ray.direction().dot(Vec3f(0,1,0)) != 0)
81 ty_near = diff_min.dot(Vec3f(0,1,0)) / cos_theta;
82 if(cos_theta = ray.direction().dot(Vec3f(0,0,1)) != 0)
83 ty_far = diff_max.dot(Vec3f(0,1,0)) / cos_theta;
84
85 // ray intersects box iff it intersects projection on xy plane
86 // in this case: tx_near <= ty_near <= tx_far <= ty_far
87 // or: tx_far <= ty_near <= tx_near <= ty_far
88 // or: tx_far <= ty_far <= tx_near <= ty_near
89 // or: tx_near <= ty_far <= tx_far <= ty_near
90 if(tx_near <= ty_near && tx_far <= ty_near &&
91 tx_near <= ty_far && tx_far <= ty_far) {
92
93 // clip along z axis
94 if (cos_theta = ray.direction().dot(Vec3f(0, 1, 0)) != 0)
95 tz_near = diff_min.dot(Vec3f(0, 1, 0)) / cos_theta;
96 if (cos_theta = ray.direction().dot(Vec3f(0, 0, 1)) != 0)
97 tz_far = diff_max.dot(Vec3f(0, 1, 0)) / cos_theta;
98
99 } else {
100
101 }
102
103 //////////////////////////
104
105 }
106
107 const Vec3f&
108 Box::min() const
109 {
110 return m_min;
111 }
112
113 const Vec3f&
114 Box::max() const
115 {
116 return m_max;
117 }
This page took 0.057672 seconds and 3 git commands to generate.