]> Dogcows Code - chaz/rasterize/blob - plane.hh
finishing fifth project
[chaz/rasterize] / plane.hh
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #ifndef _PLANE_HH_
9 #define _PLANE_HH_
10
11 #include "element.hh"
12
13
14 namespace rt {
15
16
17 /*
18 * A class for a plane object.
19 */
20 class plane : public element
21 {
22 vec_t norm;
23 scal_t dist;
24
25 public:
26
27 plane(vec_t n, scal_t d) :
28 norm(n), dist(d)
29 {}
30
31 plane(vec_t p, vec_t n) :
32 norm(vec_normalize(n)), dist(-vec_dot(norm, p))
33 {}
34
35 plane(vec_t a, vec_t b, vec_t c) :
36 norm(vec_normalize(vec_cross(vec_sub(b, a), vec_sub(c, a)))),
37 dist(-vec_dot(norm, a))
38 {}
39
40 virtual ~plane()
41 {}
42
43 /*
44 * Get the normal vector of the plane.
45 */
46 vec_t normal() const
47 {
48 return norm;
49 }
50
51 virtual bool intersect(ray_t ray, contact_t& hit) const
52 {
53 scal_t denom = vec_dot(ray.d, norm);
54
55 // check for parallel condition
56 if (denom == S(0.0)) {
57 if (scal_isequal(vec_dot(ray.o, norm), -dist)) {
58 hit.p = ray.o;
59 hit.d = S(0.0);
60 hit.n = VEC_ZERO;
61 return true; // the ray lies on the plane
62 }
63 return false; // no solution
64 }
65
66 scal_t numer = vec_dot(ray.o, norm) + dist;
67 hit.d = -numer / denom;
68 if (hit.d < S(0.0)) {
69 return false;
70 }
71
72 hit.p = ray_solve(ray, hit.d);
73 if (S(0.0) <= numer) {
74 hit.n = norm;
75 }
76 else {
77 hit.n = vec_neg(norm);
78 }
79 return true;
80 }
81
82 virtual vec_t txcoord(vec_t point) const
83 {
84 vec_t uv = VEC_ZERO;
85
86 point.x *= S(0.3);
87 point.y *= S(0.3);
88 uv.x = point.x - scal_floor(point.x);
89 uv.y = point.z - scal_floor(point.z);
90
91 return uv;
92 }
93 };
94
95
96 /*
97 * Destroy a new'd plane, releasing its memory.
98 */
99 INLINE_MAYBE
100 void plane_destroy(plane* p)
101 {
102 delete p;
103 }
104
105
106 } // namespace rt
107
108 #endif // _PLANE_HH_
109
This page took 0.038212 seconds and 4 git commands to generate.