1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * ---------------------------------------------------------------------- |
---|
4 | * Vector4.h: Vector4 class |
---|
5 | * |
---|
6 | * ====================================================================== |
---|
7 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
8 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
9 | * |
---|
10 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
11 | * |
---|
12 | * See the file "license.terms" for information on usage and |
---|
13 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | * ====================================================================== |
---|
15 | */ |
---|
16 | #ifndef VECTOR4_H |
---|
17 | #define VECTOR4_H |
---|
18 | |
---|
19 | #include <vector> |
---|
20 | |
---|
21 | #include "Trace.h" |
---|
22 | |
---|
23 | class Vector4 |
---|
24 | { |
---|
25 | public: |
---|
26 | Vector4() |
---|
27 | {} |
---|
28 | |
---|
29 | Vector4(float x_val, float y_val, float z_val, float w_val) |
---|
30 | { |
---|
31 | set(x_val, y_val, z_val, w_val); |
---|
32 | } |
---|
33 | |
---|
34 | void set(float x_val, float y_val, float z_val, float w_val) |
---|
35 | { |
---|
36 | x = x_val, y = y_val, z = z_val, w = w_val; |
---|
37 | } |
---|
38 | |
---|
39 | void perspectiveDivide() |
---|
40 | { |
---|
41 | /* Divide vector by w */ |
---|
42 | x /= w, y /= w, z /= w, w = 1.; |
---|
43 | } |
---|
44 | |
---|
45 | void print() const |
---|
46 | { |
---|
47 | TRACE("Vector4: (%.3f, %.3f, %.3f, %.3f)", x, y, z, w); |
---|
48 | } |
---|
49 | |
---|
50 | bool operator==(const Vector4& op2) const |
---|
51 | { |
---|
52 | return (x == op2.x && y == op2.y && z == op2.z && w == op2.w); |
---|
53 | } |
---|
54 | |
---|
55 | Vector4 operator +(const Vector4& op2) const |
---|
56 | { |
---|
57 | return Vector4(x + op2.x, y + op2.y, z + op2.z, w + op2.w); |
---|
58 | } |
---|
59 | |
---|
60 | Vector4 operator -(const Vector4& op2) const |
---|
61 | { |
---|
62 | return Vector4(x - op2.x, y - op2.y, z - op2.z, w - op2.w); |
---|
63 | } |
---|
64 | |
---|
65 | float operator *(const Vector4& op2) const |
---|
66 | { |
---|
67 | return (x * op2.x) + (y * op2.y) + (z * op2.z) + (w * op2.w); |
---|
68 | } |
---|
69 | |
---|
70 | Vector4 operator *(float op2) const |
---|
71 | { |
---|
72 | return Vector4(x * op2, y * op2, z * op2, w * op2); |
---|
73 | } |
---|
74 | |
---|
75 | Vector4 operator /(float op2) const |
---|
76 | { |
---|
77 | return Vector4(x / op2, y / op2, z / op2, w / op2); |
---|
78 | } |
---|
79 | |
---|
80 | float x, y, z, w; |
---|
81 | }; |
---|
82 | |
---|
83 | typedef std::vector<Vector4> Vector4Array; |
---|
84 | |
---|
85 | /** |
---|
86 | * \brief Linear interpolation of 2 points |
---|
87 | */ |
---|
88 | inline Vector4 vlerp(const Vector4& v1, const Vector4& v2, double t) |
---|
89 | { |
---|
90 | return Vector4(v1.x * (1.0-t) + v2.x * t, |
---|
91 | v1.y * (1.0-t) + v2.y * t, |
---|
92 | v1.z * (1.0-t) + v2.z * t, |
---|
93 | v1.w * (1.0-t) + v2.w * t); |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * \brief Finds the intersection of a line through |
---|
98 | * p1 and p2 with the given plane. |
---|
99 | * |
---|
100 | * If the line lies in the plane, an arbitrary |
---|
101 | * point on the line is returned. |
---|
102 | * |
---|
103 | * Reference: |
---|
104 | * http://astronomy.swin.edu.au/pbourke/geometry/planeline/ |
---|
105 | * |
---|
106 | * \param[in] pt1 First point |
---|
107 | * \param[in] pt2 Second point |
---|
108 | * \param[in] plane Plane equation coefficients |
---|
109 | * \param[out] ret Point of intersection if a solution was found |
---|
110 | * \return Bool indicating if an intersection was found |
---|
111 | */ |
---|
112 | inline bool |
---|
113 | planeLineIntersection(const Vector4& pt1, const Vector4& pt2, |
---|
114 | const Vector4& plane, Vector4& ret) |
---|
115 | { |
---|
116 | float a = plane.x; |
---|
117 | float b = plane.y; |
---|
118 | float c = plane.z; |
---|
119 | float d = plane.w; |
---|
120 | |
---|
121 | Vector4 p1 = pt1; |
---|
122 | float x1 = p1.x; |
---|
123 | float y1 = p1.y; |
---|
124 | float z1 = p1.z; |
---|
125 | |
---|
126 | Vector4 p2 = pt2; |
---|
127 | float x2 = p2.x; |
---|
128 | float y2 = p2.y; |
---|
129 | float z2 = p2.z; |
---|
130 | |
---|
131 | float uDenom = a * (x1 - x2) + b * (y1 - y2) + c * (z1 - z2); |
---|
132 | float uNumer = a * x1 + b * y1 + c * z1 + d; |
---|
133 | |
---|
134 | if (uDenom == 0.0f) { |
---|
135 | // Plane parallel to line |
---|
136 | if (uNumer == 0.0f) { |
---|
137 | // Line within plane |
---|
138 | ret = p1; |
---|
139 | return true; |
---|
140 | } |
---|
141 | // No solution |
---|
142 | TRACE("No intersection between line and plane"); |
---|
143 | return false; |
---|
144 | } |
---|
145 | |
---|
146 | float u = uNumer / uDenom; |
---|
147 | ret.x = x1 + u * (x2 - x1); |
---|
148 | ret.y = y1 + u * (y2 - y1); |
---|
149 | ret.z = z1 + u * (z2 - z1); |
---|
150 | ret.w = 1; |
---|
151 | return true; |
---|
152 | } |
---|
153 | |
---|
154 | #endif |
---|