1 | /*
|
---|
2 | * ----------------------------------------------------------------------
|
---|
3 | * Vector3.cpp : 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 | #include "Vector3.h"
|
---|
16 | #include "Mat4x4.h"
|
---|
17 | #include <math.h>
|
---|
18 | #include <stdio.h>
|
---|
19 |
|
---|
20 | Vector3::Vector3(float _x, float _y, float _z){
|
---|
21 | x = _x;
|
---|
22 | y = _y;
|
---|
23 | z = _z;
|
---|
24 | }
|
---|
25 |
|
---|
26 |
|
---|
27 | Vector3::Vector3(){}
|
---|
28 |
|
---|
29 |
|
---|
30 | Vector3 Vector3::operator +(Vector3 &op2){
|
---|
31 | Vector3 ret;
|
---|
32 | ret.x = x + op2.x;
|
---|
33 | ret.y = y + op2.y;
|
---|
34 | ret.z = z + op2.z;
|
---|
35 | return ret;
|
---|
36 | }
|
---|
37 |
|
---|
38 | Vector3 Vector3::operator -(Vector3 &op2){
|
---|
39 | Vector3 ret;
|
---|
40 | ret.x = x - op2.x;
|
---|
41 | ret.y = y - op2.y;
|
---|
42 | ret.z = z - op2.z;
|
---|
43 | return ret;
|
---|
44 | }
|
---|
45 |
|
---|
46 | float Vector3::operator *(Vector3 &op2){
|
---|
47 | return x*op2.x +
|
---|
48 | y*op2.y +
|
---|
49 | z*op2.z;
|
---|
50 | }
|
---|
51 |
|
---|
52 | float Vector3::dot(const Vector3& vec) const
|
---|
53 | {
|
---|
54 | return x*vec.x +
|
---|
55 | y*vec.y +
|
---|
56 | z*vec.z;
|
---|
57 | }
|
---|
58 |
|
---|
59 | bool Vector3::equal(Vector3 &op2){
|
---|
60 | return (x==op2.x)&&(y==op2.y)&&(z==op2.z);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /*
|
---|
64 | float Vector3::operator *(Vector3 op2){
|
---|
65 | return double(x)*double(op2.x) +
|
---|
66 | double(y)*double(op2.y) +
|
---|
67 | double(z)*double(op2.z);
|
---|
68 | }
|
---|
69 | */
|
---|
70 |
|
---|
71 | Vector3 Vector3::cross(Vector3 op2){
|
---|
72 | return Vector3(double(y)*double(op2.z)-double(z)*double(op2.y), double(z)*double(op2.x)-double(x)*double(op2.z), double(x)*double(op2.y)-double(y)*double(op2.x));
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | Vector3 Vector3::operator *(float op2){
|
---|
77 | Vector3 ret;
|
---|
78 | ret.x = x*op2;
|
---|
79 | ret.y = y*op2;
|
---|
80 | ret.z = z*op2;
|
---|
81 | return ret;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | Vector3 Vector3::operator /(float op2){
|
---|
86 | Vector3 ret;
|
---|
87 | ret.x = x/double(op2);
|
---|
88 | ret.y = y/double(op2);
|
---|
89 | ret.z = z/double(op2);
|
---|
90 | return ret;
|
---|
91 | }
|
---|
92 |
|
---|
93 | //assign
|
---|
94 | void Vector3::operator<(Vector3 &op2){
|
---|
95 | x = op2.x;
|
---|
96 | y = op2.y;
|
---|
97 | z = op2.z;
|
---|
98 | }
|
---|
99 |
|
---|
100 | Vector3 Vector3::operator ^(Vector3 &op2){
|
---|
101 | Vector3 ret;
|
---|
102 | ret.x = y*op2.z-z*op2.y;
|
---|
103 | ret.y = z*op2.x-x*op2.z;
|
---|
104 | ret.z = x*op2.y-y*op2.x;
|
---|
105 | return ret;
|
---|
106 | }
|
---|
107 |
|
---|
108 | Vector3 Vector3::normalize(){
|
---|
109 | Vector3 ret;
|
---|
110 | float length = sqrt(x*x+y*y+z*z);
|
---|
111 | ret.x = x/length;
|
---|
112 | ret.y = y/length;
|
---|
113 | ret.z = z/length;
|
---|
114 | return ret;
|
---|
115 | }
|
---|
116 |
|
---|
117 | Vector3 Vector3::rot_x(float degree){
|
---|
118 | Vector3 ret;
|
---|
119 | float rad = 3.1415926*degree/180.;
|
---|
120 | ret.x = x;
|
---|
121 | ret.y = y*cos(rad) - z*sin(rad);
|
---|
122 | ret.z = y*sin(rad) + z*cos(rad);
|
---|
123 | return ret;
|
---|
124 | }
|
---|
125 |
|
---|
126 | Vector3 Vector3::rot_y(float degree){
|
---|
127 | Vector3 ret;
|
---|
128 | float rad = 3.1415926*degree/180.;
|
---|
129 | ret.x = x*cos(rad) + z*sin(rad);
|
---|
130 | ret.y = y;
|
---|
131 | ret.z = -x*sin(rad) + z*cos(rad);
|
---|
132 | return ret;
|
---|
133 | }
|
---|
134 |
|
---|
135 | Vector3 Vector3::rot_z(float degree){
|
---|
136 | Vector3 ret;
|
---|
137 | float rad = 3.1415926*degree/180.;
|
---|
138 | ret.x = x*cos(rad) - y*sin(rad);
|
---|
139 | ret.y = x*sin(rad) + y*cos(rad);
|
---|
140 | ret.z = z;
|
---|
141 | return ret;
|
---|
142 | }
|
---|
143 |
|
---|
144 | void Vector3::set(float newx, float newy, float newz){
|
---|
145 | x = newx;
|
---|
146 | y = newy;
|
---|
147 | z = newz;
|
---|
148 | }
|
---|
149 |
|
---|
150 | void Vector3::print(){
|
---|
151 | fprintf(stderr, "x:%f, y:%f, z:%f\n", x, y, z);
|
---|
152 | }
|
---|
153 |
|
---|
154 | float Vector3::distance(Vector3 &another) const
|
---|
155 | {
|
---|
156 | return sqrtf( (x - another.x) * (x - another.x)
|
---|
157 | + (y - another.y) * (y - another.y)
|
---|
158 | + (z - another.z) * (z - another.z) );
|
---|
159 | }
|
---|
160 |
|
---|
161 | float Vector3::distanceSquare(Vector3 &another) const
|
---|
162 | {
|
---|
163 | return ( (x - another.x) * (x - another.x)
|
---|
164 | + (y - another.y) * (y - another.y)
|
---|
165 | + (z - another.z) * (z - another.z) );
|
---|
166 | }
|
---|
167 |
|
---|
168 | float Vector3::distanceSquare(float vx, float vy, float vz) const
|
---|
169 | {
|
---|
170 | return ( (x - vx) * (x - vx)
|
---|
171 | + (y - vy) * (y - vy)
|
---|
172 | + (z - vz) * (z - vz) );
|
---|
173 | }
|
---|
174 |
|
---|
175 | void Vector3::transform(const Vector3& v, const Mat4x4& mat)
|
---|
176 | {
|
---|
177 | const float* m = mat.m;
|
---|
178 | x = m[0] * v.x + m[4] * v.y + m[8] * v.z + m[12];
|
---|
179 | y = m[1] * v.x + m[5] * v.y + m[9] * v.z + m[13];
|
---|
180 | z = m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14];
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | float Vector3::length() const
|
---|
185 | {
|
---|
186 |
|
---|
187 | return sqrt(x * x + y * y + z * z);
|
---|
188 | }
|
---|