1 | /* |
---|
2 | |
---|
3 | Brian Curless |
---|
4 | |
---|
5 | Computer Graphics Laboratory |
---|
6 | Stanford University |
---|
7 | |
---|
8 | --------------------------------------------------------------------- |
---|
9 | |
---|
10 | Copyright (1997) The Board of Trustees of the Leland Stanford Junior |
---|
11 | University. Except for commercial resale, lease, license or other |
---|
12 | commercial transactions, permission is hereby given to use, copy, |
---|
13 | modify this software for academic purposes only. No part of this |
---|
14 | software or any derivatives thereof may be used in the production of |
---|
15 | computer models for resale or for use in a commercial |
---|
16 | product. STANFORD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND |
---|
17 | CONCERNING THIS SOFTWARE. No support is implied or provided. |
---|
18 | |
---|
19 | */ |
---|
20 | |
---|
21 | |
---|
22 | #ifndef _VEC3F_ |
---|
23 | #define _VEC3F_ |
---|
24 | |
---|
25 | #include <math.h> |
---|
26 | #include "defines.h" |
---|
27 | #include <iostream> |
---|
28 | #include <stdio.h> |
---|
29 | |
---|
30 | ////////////////////////////////////////////////////////////////////////////// |
---|
31 | // |
---|
32 | // Class: Vec3f |
---|
33 | // |
---|
34 | // 3D vector used to represet points or directions. Each component of |
---|
35 | // the vector is a float. |
---|
36 | // |
---|
37 | ////////////////////////////////////////////////////////////////////////////// |
---|
38 | |
---|
39 | class Vec3f { |
---|
40 | |
---|
41 | // protected: |
---|
42 | |
---|
43 | public: |
---|
44 | float x, y, z; |
---|
45 | |
---|
46 | Vec3f() { } |
---|
47 | Vec3f(const float v[3]) { setValue(v); } |
---|
48 | Vec3f(float vx, float vy, float vz) { setValue(vx, vy, vz); } |
---|
49 | |
---|
50 | float dot(const Vec3f &v) const |
---|
51 | {return (x*v.x + y*v.y + z*v.z);} |
---|
52 | |
---|
53 | Vec3f cross(const Vec3f &v) const |
---|
54 | {return Vec3f(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);} |
---|
55 | |
---|
56 | Vec3f max(const Vec3f &v) const |
---|
57 | {return Vec3f(MAX(x, v.x), MAX(y, v.y), MAX(z, v.z));} |
---|
58 | |
---|
59 | Vec3f min(const Vec3f &v) const |
---|
60 | {return Vec3f(MIN(x, v.x), MIN(y, v.y), MIN(z, v.z));} |
---|
61 | |
---|
62 | float length() const {return sqrtf(this->dot(*this));} |
---|
63 | |
---|
64 | float lengthSquared() const {return this->dot(*this);} |
---|
65 | |
---|
66 | void normalize() |
---|
67 | { float len = length(); if (len > 0) *this /= len; } |
---|
68 | |
---|
69 | void negate() {*this *= -1;} |
---|
70 | |
---|
71 | void getValue(float &vx, float &vy, float &vz) |
---|
72 | const {vx = x; vy = y; vz = z;} |
---|
73 | |
---|
74 | void getValue(float *v) |
---|
75 | const {v[0] = x; v[1] = y; v[2] = z;} |
---|
76 | |
---|
77 | void print() |
---|
78 | {printf("%f %f %f\n", x, y, z);} |
---|
79 | |
---|
80 | Vec3f & clear() |
---|
81 | {setValue(0,0,0); return *this;} |
---|
82 | |
---|
83 | Vec3f & setValue(const float v[3]) |
---|
84 | { // handle: v = 0; |
---|
85 | if (v) { x = v[0]; y = v[1]; z = v[2]; } else x = y = z = 0; |
---|
86 | return *this; |
---|
87 | } |
---|
88 | |
---|
89 | Vec3f & setValue(float vx, float vy, float vz) |
---|
90 | { x = vx; y = vy; z = vz; return *this; } |
---|
91 | |
---|
92 | Vec3f & setValue(const Vec3f &v1) |
---|
93 | { x = v1.x; y = v1.y; z = v1.z; return *this; } |
---|
94 | |
---|
95 | Vec3f & translate(float vx, float vy, float vz) |
---|
96 | { x += vx; y += vy; z += vz; return *this; } |
---|
97 | |
---|
98 | Vec3f & operator *=(float d) |
---|
99 | { x *= d; y *= d; z *= d; return *this; } |
---|
100 | |
---|
101 | Vec3f & operator /=(float d) |
---|
102 | { return *this *= (1.0 / d); } |
---|
103 | |
---|
104 | Vec3f & operator +=(const Vec3f &v1) |
---|
105 | { x += v1.x; y += v1.y; z += v1.z; return *this; } |
---|
106 | |
---|
107 | Vec3f & operator -=(const Vec3f &v1) |
---|
108 | { x -= v1.x; y -= v1.y; z -= v1.z; return *this; } |
---|
109 | |
---|
110 | const float & operator [](int i) const |
---|
111 | { return (*(&x + i)); } |
---|
112 | |
---|
113 | friend int operator ==(const Vec3f &v1, const Vec3f &v2) |
---|
114 | { return ((v1.x == v2.x)&&(v1.y == v2.y) |
---|
115 | &&(v1.z == v2.z)); } |
---|
116 | |
---|
117 | friend int operator !=(const Vec3f &v1, const Vec3f &v2) |
---|
118 | { return !(v1 == v2); } |
---|
119 | |
---|
120 | friend Vec3f operator +(const Vec3f &v1, const Vec3f &v2) |
---|
121 | { return Vec3f(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); } |
---|
122 | |
---|
123 | friend Vec3f operator -(const Vec3f &v1, const Vec3f &v2) |
---|
124 | { return Vec3f(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); } |
---|
125 | |
---|
126 | friend Vec3f operator *(const float d, const Vec3f &v1) |
---|
127 | { return Vec3f(d * v1.x, d * v1.y, d * v1.z); } |
---|
128 | |
---|
129 | friend Vec3f operator *(const Vec3f &v1, const float d) |
---|
130 | { return d * v1; } |
---|
131 | }; |
---|
132 | |
---|
133 | std::ostream &operator<<(std::ostream &, const Vec3f &); |
---|
134 | |
---|
135 | #endif |
---|
136 | |
---|