1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Insoo Woo <iwoo@purdue.edu> |
---|
6 | */ |
---|
7 | #ifndef VRVECTOR4F_H |
---|
8 | #define VRVECTOR4F_H |
---|
9 | |
---|
10 | #include <vrmath/Vector3f.h> |
---|
11 | #include <vrmath/Vector4f.h> |
---|
12 | |
---|
13 | namespace vrmath { |
---|
14 | |
---|
15 | class Vector4f |
---|
16 | { |
---|
17 | public: |
---|
18 | Vector4f() : |
---|
19 | x(0.0f), y(0.0f), z(0.0f), w(0.0f) |
---|
20 | {} |
---|
21 | |
---|
22 | Vector4f(const Vector4f& v4) : |
---|
23 | x(v4.x), y(v4.y), z(v4.z), w(v4.w) |
---|
24 | {} |
---|
25 | |
---|
26 | Vector4f(const Vector3f& v3, float w1) : |
---|
27 | x(v3.x), y(v3.y), z(v3.z), w(w1) |
---|
28 | {} |
---|
29 | |
---|
30 | Vector4f(float x1, float y1, float z1, float w1) : |
---|
31 | x(x1), y(y1), z(z1), w(w1) |
---|
32 | {} |
---|
33 | |
---|
34 | void set(float x1, float y1, float z1, float w1); |
---|
35 | |
---|
36 | void set(const Vector3f& v, float w); |
---|
37 | |
---|
38 | void set(const Vector4f& v4); |
---|
39 | |
---|
40 | bool operator==(const Vector4f& op2) const |
---|
41 | { |
---|
42 | return (x == op2.x && y == op2.y && z == op2.z && w == op2.w); |
---|
43 | } |
---|
44 | |
---|
45 | Vector4f operator+(const Vector4f& op2) const |
---|
46 | { |
---|
47 | return Vector4f(x + op2.x, y + op2.y, z + op2.z, w + op2.w); |
---|
48 | } |
---|
49 | |
---|
50 | Vector4f operator-(const Vector4f& op2) const |
---|
51 | { |
---|
52 | return Vector4f(x - op2.x, y - op2.y, z - op2.z, w - op2.w); |
---|
53 | } |
---|
54 | |
---|
55 | float operator*(const Vector4f& op2) const |
---|
56 | { |
---|
57 | return (x * op2.x) + (y * op2.y) + (z * op2.z) + (w * op2.w); |
---|
58 | } |
---|
59 | |
---|
60 | Vector4f operator*(float op2) const |
---|
61 | { |
---|
62 | return Vector4f(x * op2, y * op2, z * op2, w * op2); |
---|
63 | } |
---|
64 | |
---|
65 | Vector4f operator/(float op2) const |
---|
66 | { |
---|
67 | return Vector4f(x / op2, y / op2, z / op2, w / op2); |
---|
68 | } |
---|
69 | |
---|
70 | void divideByW(); |
---|
71 | |
---|
72 | float dot(const Vector4f& vec) const; |
---|
73 | |
---|
74 | float x, y, z, w; |
---|
75 | }; |
---|
76 | |
---|
77 | inline void Vector4f::divideByW() |
---|
78 | { |
---|
79 | if (w != 0) { |
---|
80 | x /= w; y /= w; z /= w; |
---|
81 | w = 1.0f; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | inline void Vector4f::set(float x1, float y1, float z1, float w1) |
---|
86 | { |
---|
87 | x = x1; |
---|
88 | y = y1; |
---|
89 | z = z1; |
---|
90 | w = w1; |
---|
91 | } |
---|
92 | |
---|
93 | inline void Vector4f::set(const Vector4f& v4) |
---|
94 | { |
---|
95 | x = v4.x; |
---|
96 | y = v4.y; |
---|
97 | z = v4.z; |
---|
98 | w = v4.w; |
---|
99 | } |
---|
100 | |
---|
101 | inline void Vector4f::set(const Vector3f& v, float w1) |
---|
102 | { |
---|
103 | x = v.x; |
---|
104 | y = v.y; |
---|
105 | z = v.z; |
---|
106 | w = w1; |
---|
107 | } |
---|
108 | |
---|
109 | inline float Vector4f::dot(const Vector4f& vec) const |
---|
110 | { |
---|
111 | return (x * vec.x + y * vec.y + z * vec.z + w * vec.w); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * \brief Linear interpolation of 2 points |
---|
116 | */ |
---|
117 | inline Vector4f vlerp(const Vector4f& v1, const Vector4f& v2, double t) |
---|
118 | { |
---|
119 | return Vector4f(v1.x * (1.0-t) + v2.x * t, |
---|
120 | v1.y * (1.0-t) + v2.y * t, |
---|
121 | v1.z * (1.0-t) + v2.z * t, |
---|
122 | v1.w * (1.0-t) + v2.w * t); |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * \brief Finds the intersection of a line through |
---|
127 | * p1 and p2 with the given plane. |
---|
128 | * |
---|
129 | * If the line lies in the plane, an arbitrary |
---|
130 | * point on the line is returned. |
---|
131 | * |
---|
132 | * Reference: |
---|
133 | * http://astronomy.swin.edu.au/pbourke/geometry/planeline/ |
---|
134 | * |
---|
135 | * \param[in] pt1 First point |
---|
136 | * \param[in] pt2 Second point |
---|
137 | * \param[in] plane Plane equation coefficients |
---|
138 | * \param[out] ret Point of intersection if a solution was found |
---|
139 | * \return Bool indicating if an intersection was found |
---|
140 | */ |
---|
141 | inline bool |
---|
142 | planeLineIntersection(const Vector4f& pt1, const Vector4f& pt2, |
---|
143 | const Vector4f& plane, Vector4f& ret) |
---|
144 | { |
---|
145 | float a = plane.x; |
---|
146 | float b = plane.y; |
---|
147 | float c = plane.z; |
---|
148 | float d = plane.w; |
---|
149 | |
---|
150 | Vector4f p1 = pt1; |
---|
151 | float x1 = p1.x; |
---|
152 | float y1 = p1.y; |
---|
153 | float z1 = p1.z; |
---|
154 | |
---|
155 | Vector4f p2 = pt2; |
---|
156 | float x2 = p2.x; |
---|
157 | float y2 = p2.y; |
---|
158 | float z2 = p2.z; |
---|
159 | |
---|
160 | float uDenom = a * (x1 - x2) + b * (y1 - y2) + c * (z1 - z2); |
---|
161 | float uNumer = a * x1 + b * y1 + c * z1 + d; |
---|
162 | |
---|
163 | if (uDenom == 0.0f) { |
---|
164 | // Plane parallel to line |
---|
165 | if (uNumer == 0.0f) { |
---|
166 | // Line within plane |
---|
167 | ret = p1; |
---|
168 | return true; |
---|
169 | } |
---|
170 | // No solution |
---|
171 | return false; |
---|
172 | } |
---|
173 | |
---|
174 | float u = uNumer / uDenom; |
---|
175 | ret.x = x1 + u * (x2 - x1); |
---|
176 | ret.y = y1 + u * (y2 - y1); |
---|
177 | ret.z = z1 + u * (z2 - z1); |
---|
178 | ret.w = 1; |
---|
179 | return true; |
---|
180 | } |
---|
181 | |
---|
182 | } |
---|
183 | |
---|
184 | #endif |
---|