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

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

Remove XINETD define from nanovis. We only support server mode now, no glut
interaction loop with mouse/keyboard handlers. Fixes for trace logging to make
output closer to vtkvis: inlcude function name for trace messages, remove
newlines from format strings in macros since newlines get added by syslog.

File size: 4.4 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-2012  HUBzero Foundation, LLC
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
21#include <vector>
22
23#include "Mat4x4.h"
24
25class Vector3
26{
27public:
28    Vector3()
29    {}
30
31    Vector3(float x_val, float y_val, float z_val)
32    {
33        set(x_val, y_val, z_val);
34    }
35
36    void set(float x_val, float y_val, float z_val)
37    {
38        x = x_val, y = y_val, z = z_val;
39    }
40
41    void print() const
42    {
43        TRACE("(x:%f, y:%f, z:%f)", x, y, z);
44    }
45
46    bool operator==(const Vector3& op2) const
47    {
48        return (x == op2.x && y == op2.y && z == op2.z);
49    }
50
51    Vector3 operator -() const
52    {
53        return Vector3(-x, -y, -z);
54    }
55
56    Vector3 operator +(float scalar) const
57    {
58        return Vector3(x + scalar, y + scalar, z + scalar);
59    }
60
61    Vector3 operator -(float scalar) const
62    {
63        return Vector3(x - scalar, y - scalar, z - scalar);
64    }
65
66    Vector3 operator *(float scalar) const
67    {
68        return Vector3(x * scalar, y * scalar, z * scalar);
69    }
70
71    Vector3 operator /(float scalar) const
72    {
73        return Vector3(x / scalar, y / scalar, z / scalar);
74    }
75
76    Vector3 operator +(const Vector3& op2) const
77    {
78        return Vector3(x + op2.x, y + op2.y, z + op2.z);
79    }
80
81    Vector3 operator -(const Vector3& op2) const
82    {
83        return Vector3(x - op2.x, y - op2.y, z - op2.z);
84    }
85
86    float dot(const Vector3& op2) const
87    {
88        return x*op2.x + y*op2.y + z*op2.z;
89    }
90
91    float operator *(const Vector3& op2) const
92    {
93        return dot(op2);
94    }
95
96    Vector3 cross(const Vector3& op2) const
97    {
98        return Vector3(y*op2.z - z*op2.y, z*op2.x - x*op2.z, x*op2.y - y*op2.x);
99    }
100
101    Vector3 operator ^(const Vector3& op2) const
102    {
103        return cross(op2);
104    }
105
106    Vector3 normalize() const
107    {
108        float len = length();
109        if (len > 1.0e-6) {
110            return Vector3(x / len, y / len, z / len);
111        } else {
112            return *this;
113        }
114    }
115
116    Vector3 rotX(float degree) const
117    {
118        float rad = radians(degree);
119        return Vector3(x,
120                       y*cos(rad) - z*sin(rad),
121                       y*sin(rad) + z*cos(rad));
122    }
123
124    Vector3 rotY(float degree) const
125    {
126        float rad = radians(degree);
127        return Vector3(x*cos(rad) + z*sin(rad),
128                       y,
129                       -x*sin(rad) + z*cos(rad));
130    }
131
132    Vector3 rotZ(float degree) const
133    {
134        float rad = radians(degree);
135        return Vector3(x*cos(rad) - y*sin(rad),
136                       x*sin(rad) + y*cos(rad),
137                       z);
138    }
139
140    float distance(const Vector3& v) const
141    {
142        return sqrtf(distanceSquare(v));
143    }
144
145    float distanceSquare(const Vector3& v) const
146    {
147        return (x-v.x)*(x-v.x) + (y-v.y)*(y-v.y) + (z-v.z)*(z-v.z);
148    }
149
150    float distanceSquare(float vx, float vy, float vz) const
151    {
152        return (x-vx)*(x-vx) + (y-vy)*(y-vy) + (z-vz)*(z-vz);
153    }
154
155    void transform(const Vector3& v, const Mat4x4& mat)
156    {
157        const float *m = mat.m;
158        x = m[0] * v.x + m[4] * v.y + m[8]  * v.z + m[12];
159        y = m[1] * v.x + m[5] * v.y + m[9]  * v.z + m[13];
160        z = m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14];
161    }
162
163    float length() const
164    {
165        return sqrt(x*x + y*y + z*z);
166    }
167
168    Vector3 scale(const Vector3& scale) const
169    {
170        Vector3 v;
171        v.x = x * scale.x;
172        v.y = y * scale.y;
173        v.z = z * scale.z;
174        return v;
175    }
176
177    float x, y, z;
178
179private:
180    float radians(float degree) const
181    {
182        return (M_PI * degree) / 180.0;
183    }
184};
185
186typedef std::vector<Vector3> Vector3Array;
187
188/**
189 * \brief Linear interpolation of 2 vectors
190 */
191inline Vector3 vlerp(const Vector3& v1, const Vector3& v2, double t)
192{
193    return Vector3(v1.x * (1.0-t) + v2.x * t,
194                   v1.y * (1.0-t) + v2.y * t,
195                   v1.z * (1.0-t) + v2.z * t);
196}
197
198#endif
Note: See TracBrowser for help on using the repository browser.