source: trunk/packages/vizservers/nanovis/Vector3.h @ 2822

Last change on this file since 2822 was 2822, checked in by ldelgass, 12 years ago

Const correctness fixes, pass vector/matrix objects by reference, various
formatting and style cleanups, don't spam syslog and uncomment openlog() call.
Still needs to be compiled with -DWANT_TRACE to get tracing, but now trace
output will be output to file in /tmp.

File size: 3.8 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * ----------------------------------------------------------------------
4 * Vector3.h : Vector class with 3 components
5 *
6 * ======================================================================
7 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
8 *           Purdue Rendering and Perceptualization Lab (PURPL)
9 *
10 *  Copyright (c) 2004-2006  Purdue Research Foundation
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 _VECTOR3_H_
17#define _VECTOR3_H_
18
19#include <math.h>
20#include "Mat4x4.h"
21
22class Vector3
23{
24public:
25    float x, y, z;
26
27    Vector3()
28    {}
29
30    Vector3(float x_val, float y_val, float z_val)
31    {
32        set(x_val, y_val, z_val);
33    }
34
35    void set(float x_val, float y_val, float z_val)
36    {
37        x = x_val, y = y_val, z = z_val;
38    }
39
40    void print() const
41    {
42        TRACE("(x:%f, y:%f, z:%f)\n", x, y, z);
43    }
44
45    Vector3 operator +(float scalar) const
46    {
47        return Vector3(x + scalar, y + scalar, z + scalar);
48    }
49
50    Vector3 operator -(float scalar) const
51    {
52        return Vector3(x - scalar, y - scalar, z - scalar);
53    }
54
55    Vector3 operator *(float scalar) const
56    {
57        return Vector3(x * scalar, y * scalar, z * scalar);
58    }
59
60    Vector3 operator /(float scalar) const
61    {
62        return Vector3(x / scalar, y / scalar, z / scalar);
63    }
64
65    Vector3 operator +(const Vector3& op2) const
66    {
67        return Vector3(x + op2.x, y + op2.y, z + op2.z);
68    }
69
70    Vector3 operator -(const Vector3& op2) const
71    {
72        return Vector3(x - op2.x, y - op2.y, z - op2.z);
73    }
74
75    float dot(const Vector3& op2) const
76    {
77        return x*op2.x + y*op2.y + z*op2.z;
78    }
79
80    float operator *(const Vector3& op2) const
81    {
82        return dot(op2);
83    }
84
85    Vector3 cross(const Vector3& op2) const
86    {
87        return Vector3(y*op2.z - z*op2.y, z*op2.x - x*op2.z, x*op2.y - y*op2.x);
88    }
89
90    Vector3 operator ^(const Vector3& op2) const
91    {
92        return cross(op2);
93    }
94
95    Vector3 normalize() const
96    {
97        float len = length();
98        return Vector3(x / len, y / len, z / len);
99    }
100
101    Vector3 rot_x(float degree) const
102    {
103        float rad = radians(degree);
104        return Vector3(x,
105                       y*cos(rad) - z*sin(rad),
106                       y*sin(rad) + z*cos(rad));
107    }
108
109    Vector3 rot_y(float degree) const
110    {
111        float rad = radians(degree);
112        return Vector3(x*cos(rad) + z*sin(rad),
113                       y,
114                       -x*sin(rad) + z*cos(rad));
115    }
116
117    Vector3 rot_z(float degree) const
118    {
119        float rad = radians(degree);
120        return Vector3(x*cos(rad) - y*sin(rad),
121                       x*sin(rad) + y*cos(rad),
122                       z);
123    }
124
125    float distance(const Vector3& v) const
126    {
127        return sqrtf(distanceSquare(v));
128    }
129
130    float distanceSquare(const Vector3& v) const
131    {
132        return (x-v.x)*(x-v.x) + (y-v.y)*(y-v.y) + (z-v.z)*(z-v.z);
133    }
134
135    float distanceSquare(float vx, float vy, float vz) const
136    {
137        return (x-vx)*(x-vx) + (y-vy)*(y-vy) + (z-vz)*(z-vz);
138    }
139
140    void transform(const Vector3& v, const Mat4x4& mat)
141    {
142        const float* m = mat.m;
143        x = m[0] * v.x + m[4] * v.y + m[8]  * v.z + m[12];
144        y = m[1] * v.x + m[5] * v.y + m[9]  * v.z + m[13];
145        z = m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14];
146    }
147
148    float length() const
149    {
150        return sqrt(x*x + y*y + z*z);
151    }
152
153    Vector3 scale(const Vector3& scale) const
154    {
155        Vector3 v;
156        v.x = x * scale.x;
157        v.y = y * scale.y;
158        v.z = z * scale.z;
159        return v;
160    }
161
162private:
163    float radians(float degree) const
164    {
165        return (M_PI * degree) / 180.0;
166    }
167};
168
169#endif
Note: See TracBrowser for help on using the repository browser.