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 <math.h>
|
---|
17 | #include <stdio.h>
|
---|
18 |
|
---|
19 | Vector3::Vector3(float _x, float _y, float _z){
|
---|
20 | x = _x;
|
---|
21 | y = _y;
|
---|
22 | z = _z;
|
---|
23 | }
|
---|
24 |
|
---|
25 |
|
---|
26 | Vector3::Vector3(){}
|
---|
27 |
|
---|
28 |
|
---|
29 | Vector3 Vector3::operator +(Vector3 &op2){
|
---|
30 | Vector3 ret;
|
---|
31 | ret.x = x + op2.x;
|
---|
32 | ret.y = y + op2.y;
|
---|
33 | ret.z = z + op2.z;
|
---|
34 | return ret;
|
---|
35 | }
|
---|
36 |
|
---|
37 | Vector3 Vector3::operator -(Vector3 &op2){
|
---|
38 | Vector3 ret;
|
---|
39 | ret.x = x - op2.x;
|
---|
40 | ret.y = y - op2.y;
|
---|
41 | ret.z = z - op2.z;
|
---|
42 | return ret;
|
---|
43 | }
|
---|
44 |
|
---|
45 | float Vector3::operator *(Vector3 &op2){
|
---|
46 | return x*op2.x +
|
---|
47 | y*op2.y +
|
---|
48 | z*op2.z;
|
---|
49 | }
|
---|
50 |
|
---|
51 | bool Vector3::equal(Vector3 &op2){
|
---|
52 | return (x==op2.x)&&(y==op2.y)&&(z==op2.z);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /*
|
---|
56 | float Vector3::operator *(Vector3 op2){
|
---|
57 | return double(x)*double(op2.x) +
|
---|
58 | double(y)*double(op2.y) +
|
---|
59 | double(z)*double(op2.z);
|
---|
60 | }
|
---|
61 | */
|
---|
62 |
|
---|
63 | Vector3 Vector3::cross(Vector3 op2){
|
---|
64 | 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));
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | Vector3 Vector3::operator *(float op2){
|
---|
69 | Vector3 ret;
|
---|
70 | ret.x = x*op2;
|
---|
71 | ret.y = y*op2;
|
---|
72 | ret.z = z*op2;
|
---|
73 | return ret;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | Vector3 Vector3::operator /(float op2){
|
---|
78 | Vector3 ret;
|
---|
79 | ret.x = x/double(op2);
|
---|
80 | ret.y = y/double(op2);
|
---|
81 | ret.z = z/double(op2);
|
---|
82 | return ret;
|
---|
83 | }
|
---|
84 |
|
---|
85 | //assign
|
---|
86 | void Vector3::operator<(Vector3 &op2){
|
---|
87 | x = op2.x;
|
---|
88 | y = op2.y;
|
---|
89 | z = op2.z;
|
---|
90 | }
|
---|
91 |
|
---|
92 | Vector3 Vector3::operator ^(Vector3 &op2){
|
---|
93 | Vector3 ret;
|
---|
94 | ret.x = y*op2.z-z*op2.y;
|
---|
95 | ret.y = z*op2.x-x*op2.z;
|
---|
96 | ret.z = x*op2.y-y*op2.x;
|
---|
97 | return ret;
|
---|
98 | }
|
---|
99 |
|
---|
100 | Vector3 Vector3::normalize(){
|
---|
101 | Vector3 ret;
|
---|
102 | float length = sqrt(x*x+y*y+z*z);
|
---|
103 | ret.x = x/length;
|
---|
104 | ret.y = y/length;
|
---|
105 | ret.z = z/length;
|
---|
106 | return ret;
|
---|
107 | }
|
---|
108 |
|
---|
109 | Vector3 Vector3::rot_x(float degree){
|
---|
110 | Vector3 ret;
|
---|
111 | float rad = 3.1415926*degree/180.;
|
---|
112 | ret.x = x;
|
---|
113 | ret.y = y*cos(rad) - z*sin(rad);
|
---|
114 | ret.z = y*sin(rad) + z*cos(rad);
|
---|
115 | return ret;
|
---|
116 | }
|
---|
117 |
|
---|
118 | Vector3 Vector3::rot_y(float degree){
|
---|
119 | Vector3 ret;
|
---|
120 | float rad = 3.1415926*degree/180.;
|
---|
121 | ret.x = x*cos(rad) + z*sin(rad);
|
---|
122 | ret.y = y;
|
---|
123 | ret.z = -x*sin(rad) + z*cos(rad);
|
---|
124 | return ret;
|
---|
125 | }
|
---|
126 |
|
---|
127 | Vector3 Vector3::rot_z(float degree){
|
---|
128 | Vector3 ret;
|
---|
129 | float rad = 3.1415926*degree/180.;
|
---|
130 | ret.x = x*cos(rad) - y*sin(rad);
|
---|
131 | ret.y = x*sin(rad) + y*cos(rad);
|
---|
132 | ret.z = z;
|
---|
133 | return ret;
|
---|
134 | }
|
---|
135 |
|
---|
136 | void Vector3::set(float newx, float newy, float newz){
|
---|
137 | x = newx;
|
---|
138 | y = newy;
|
---|
139 | z = newz;
|
---|
140 | }
|
---|
141 |
|
---|
142 | void Vector3::print(){
|
---|
143 | fprintf(stderr, "x:%f, y:%f, z:%f\n", x, y, z);
|
---|
144 | }
|
---|
145 |
|
---|
146 | float Vector3::distance(Vector3 &another){
|
---|
147 | return sqrtf( (x - another.x) * (x - another.x)
|
---|
148 | + (y - another.y) * (y - another.y)
|
---|
149 | + (z - another.z) * (z - another.z) );
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 |
|
---|