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

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

Some minor refactoring, also add some more fine grained config.h defines
(e.g. replace NV40 define with feature defines). Add tests for some required
OpenGL extensions (should always check for extensions or base version before
calling entry points from the extension). Also, clamp diffuse and specular
values on input and warn when they are out of range.

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