70ab61806e71791f10920681073abece6ad53129
2 #include "InfinitePlane.hxx"
4 Box::Box() : m_min(Infinity
, Infinity
, Infinity
),
5 m_max(-Infinity
, -Infinity
, -Infinity
)
9 Box::Box(Vec3f min
, Vec3f max
) : m_min(min
), m_max(max
)
17 Box::Box(const Box
& b
)
24 Box::operator=(const Box
& b
)
37 Box::Extend(const Vec3f
& a
)
39 // for all three coordinates, move m_max or m_min to the point
40 for(size_t i
= 0; i
< 3; i
++) {
43 } else if(a
[i
] < m_min
[i
]) {
45 } // else: do nothing, coordinate is inside the box
50 Box::Extend(const Box
& box
)
55 Box::Overlaps(const Box
& b
) const
61 Box::Clip(const Ray
& ray
, float& tnear
, float& tfar
) const
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();
70 float cos_theta
, temp
;
71 float tx_near
= -Infinity
, tx_far
= Infinity
,
72 ty_near
= -Infinity
, ty_far
= Infinity
,
73 tz_near
= -Infinity
, tz_far
= Infinity
;
76 if((cos_theta
= ray
.direction().dot(Vec3f(1,0,0))) != 0) // if not parallel...
77 tx_near
= diff_min
.dot(Vec3f(1,0,0)) / cos_theta
;
78 if((cos_theta
= ray
.direction().dot(Vec3f(1,0,0))) != 0)
79 tx_far
= diff_max
.dot(Vec3f(1,0,0)) / cos_theta
;
81 // we don't know which is nearer, m_min or m_max, so swap them if neccessary
82 if(tx_near
> tx_far
) {
88 // do the same for y axis
89 if((cos_theta
= ray
.direction().dot(Vec3f(0,1,0))) != 0)
90 ty_near
= diff_min
.dot(Vec3f(0,1,0)) / cos_theta
;
91 if((cos_theta
= ray
.direction().dot(Vec3f(0,1,0))) != 0)
92 ty_far
= diff_max
.dot(Vec3f(0,1,0)) / cos_theta
;
93 if(ty_near
> ty_far
) {
100 if((cos_theta
= ray
.direction().dot(Vec3f(0,0,1))) != 0)
101 tz_near
= diff_min
.dot(Vec3f(0,0,1)) / cos_theta
;
102 if((cos_theta
= ray
.direction().dot(Vec3f(0,0,1))) != 0)
103 tz_far
= diff_max
.dot(Vec3f(0,0,1)) / cos_theta
;
104 if(tz_near
> tz_far
) {
110 // now the maximum of "near"s is the entry point of the ray, the minimum
111 // of "far"s is the ray's exit point. Visually speaking: the ray must cross
112 // all three "near" planes before it can be inside the box.
113 // Note: If t_near_max > t_far_min, the ray does not intersect the box
114 tnear
= fmax(fmax(tx_near
, ty_near
), tz_near
);
115 tfar
= fmin(fmin(tx_far
, ty_far
), tz_far
);
This page took 0.048195 seconds and 3 git commands to generate.