1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * Vector3.h : Vector class with 3 components |
---|
4 | * |
---|
5 | * ====================================================================== |
---|
6 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
7 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
8 | * |
---|
9 | * Copyright (c) 2004-2006 Purdue Research Foundation |
---|
10 | * |
---|
11 | * See the file "license.terms" for information on usage and |
---|
12 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
13 | * ====================================================================== |
---|
14 | */ |
---|
15 | #ifndef _VECTOR3_H_ |
---|
16 | #define _VECTOR3_H_ |
---|
17 | |
---|
18 | /* |
---|
19 | * FIXME: The files that explicitly use the "rot*", "length", "distance", or |
---|
20 | * "normalize" methods, should include the following headers. Don't do it |
---|
21 | * here. |
---|
22 | */ |
---|
23 | #include <math.h> |
---|
24 | #include "Mat4x4.h" |
---|
25 | |
---|
26 | class Vector3{ |
---|
27 | private: |
---|
28 | float radians(float degree) const { |
---|
29 | return (M_PI * degree) / 180.0; |
---|
30 | } |
---|
31 | public: |
---|
32 | float x, y, z; |
---|
33 | Vector3(void) { |
---|
34 | /*empty*/ |
---|
35 | } |
---|
36 | Vector3(float x_val, float y_val, float z_val) { |
---|
37 | set(x_val, y_val, z_val); |
---|
38 | } |
---|
39 | Vector3 operator +(float scalar) { |
---|
40 | return Vector3(x + scalar, y + scalar, z + scalar); |
---|
41 | } |
---|
42 | Vector3 operator -(float scalar) { |
---|
43 | return Vector3(x - scalar, y - scalar, z - scalar); |
---|
44 | } |
---|
45 | Vector3 operator *(float scalar) { |
---|
46 | return Vector3(x * scalar, y * scalar, z * scalar); |
---|
47 | } |
---|
48 | Vector3 operator /(float scalar) { |
---|
49 | return Vector3(x / scalar, y / scalar, z / scalar); |
---|
50 | } |
---|
51 | Vector3 operator +(Vector3 &op2) { |
---|
52 | return Vector3(x + op2.x, y + op2.y, z + op2.z); |
---|
53 | } |
---|
54 | Vector3 operator -(Vector3 &op2) { |
---|
55 | return Vector3(x - op2.x, y - op2.y, z - op2.z); |
---|
56 | } |
---|
57 | float operator *(Vector3 &op2){ |
---|
58 | return x*op2.x + y*op2.y + z*op2.z; |
---|
59 | } |
---|
60 | float dot(const Vector3& vec) const { |
---|
61 | return x*vec.x + y*vec.y + z*vec.z; |
---|
62 | } |
---|
63 | bool equal(Vector3 &op2) { |
---|
64 | return (x==op2.x) && (y==op2.y) && (z==op2.z); |
---|
65 | } |
---|
66 | Vector3 cross(Vector3 op2) { |
---|
67 | return Vector3(y*op2.z - z*op2.y, z*op2.x - x*op2.z, x*op2.y - y*op2.x); |
---|
68 | } |
---|
69 | void operator<(Vector3 &op2) { |
---|
70 | set(op2.x, op2.y, op2.z); |
---|
71 | } |
---|
72 | Vector3 operator ^(Vector3 &op2) { |
---|
73 | return cross(op2); |
---|
74 | } |
---|
75 | Vector3 normalize(void) { |
---|
76 | float len = length(); |
---|
77 | return Vector3(x / len, y / len, z / len); |
---|
78 | } |
---|
79 | Vector3 rot_x(float degree) { |
---|
80 | float rad = radians(degree); |
---|
81 | return Vector3(x, |
---|
82 | y*cos(rad) - z*sin(rad), |
---|
83 | y*sin(rad) + z*cos(rad)); |
---|
84 | } |
---|
85 | Vector3 rot_y(float degree) { |
---|
86 | float rad = radians(degree); |
---|
87 | return Vector3(x*cos(rad) + z*sin(rad), |
---|
88 | y, |
---|
89 | -x*sin(rad) + z*cos(rad)); |
---|
90 | } |
---|
91 | Vector3 rot_z(float degree) { |
---|
92 | float rad = radians(degree); |
---|
93 | return Vector3(x*cos(rad) - y*sin(rad), |
---|
94 | x*sin(rad) + y*cos(rad), |
---|
95 | z); |
---|
96 | } |
---|
97 | void set(float x_val, float y_val, float z_val) { |
---|
98 | x = x_val, y = y_val, z = z_val; |
---|
99 | } |
---|
100 | void print(void){ |
---|
101 | fprintf(stderr, "x:%f, y:%f, z:%f\n", x, y, z); |
---|
102 | } |
---|
103 | float distance(Vector3 &v) const { |
---|
104 | return sqrtf(distanceSquare(v)); |
---|
105 | } |
---|
106 | float distanceSquare(Vector3 &v) const { |
---|
107 | return (x-v.x)*(x-v.x) + (y-v.y)*(y-v.y) + (z-v.z)*(z-v.z); |
---|
108 | } |
---|
109 | float distanceSquare(float vx, float vy, float vz) const { |
---|
110 | return (x-vx)*(x-vx) + (y-vy)*(y-vy) + (z-vz)*(z-vz); |
---|
111 | } |
---|
112 | void transform(const Vector3& v, const Mat4x4& mat) { |
---|
113 | const float* m = mat.m; |
---|
114 | x = m[0] * v.x + m[4] * v.y + m[8] * v.z + m[12]; |
---|
115 | y = m[1] * v.x + m[5] * v.y + m[9] * v.z + m[13]; |
---|
116 | z = m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14]; |
---|
117 | } |
---|
118 | float length(void) const { |
---|
119 | return sqrt(x*x + y*y + z*z); |
---|
120 | } |
---|
121 | }; |
---|
122 | |
---|
123 | |
---|
124 | #endif |
---|