source: trunk/vizservers/nanovis/Vector3.cpp @ 827

Last change on this file since 827 was 819, checked in by vrinside, 16 years ago

Added several member functions

File size: 4.0 KB
Line 
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
20Vector3::Vector3(float _x, float _y, float _z){
21        x = _x;
22        y = _y;
23        z = _z;
24}
25
26
27Vector3::Vector3(){}
28
29
30Vector3 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
38Vector3 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
46float Vector3::operator *(Vector3 &op2){
47        return x*op2.x +
48                   y*op2.y +
49                   z*op2.z;
50}
51
52float Vector3::dot(const Vector3& vec) const
53{
54        return x*vec.x +
55                   y*vec.y +
56                   z*vec.z;
57}
58
59bool Vector3::equal(Vector3 &op2){
60        return (x==op2.x)&&(y==op2.y)&&(z==op2.z);
61}
62
63/*
64float 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
71Vector3 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
76Vector3 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
85Vector3 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
94void Vector3::operator<(Vector3 &op2){
95        x = op2.x;
96        y = op2.y;
97        z = op2.z;
98}
99
100Vector3 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
108Vector3 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
117Vector3 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
126Vector3 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
135Vector3 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
144void Vector3::set(float newx, float newy, float newz){
145        x = newx;
146        y = newy;
147        z = newz;
148}
149
150void Vector3::print(){
151        fprintf(stderr, "x:%f, y:%f, z:%f\n", x, y, z);
152}
153
154float 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
161float 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
168float 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
175void 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
184float Vector3::length() const
185{
186
187    return sqrt(x * x + y * y + z * z);
188}
Note: See TracBrowser for help on using the repository browser.