Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
opvector3.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 1998,1999,2000 by Jorrit Tyberghein
3  Largely rewritten by Ivan Avramovic <ivan@avramovic.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public
16  License along with this library; if not, write to the Free
17  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 
20 #ifndef __CS_VECTOR3_H__
21 #define __CS_VECTOR3_H__
22 #include "cmd/collide2/Stdafx.h"
24 
28 class csVector3
29 {
30 public:
32  float x;
34  float y;
36  float z;
37 
43  csVector3 () {}
44 
50  csVector3 (float m) : x(m), y(m), z(m) {}
51 
53  csVector3 (float ix, float iy, float iz = 0) : x(ix), y(iy), z(iz) {}
54 
56  csVector3 (const csVector3& v) : x(v.x), y(v.y), z(v.z) {}
57  csVector3 (const Vector v): x(v.i), y(v.j), z(v.k) {}
60  inline csVector3 operator+ (const csVector3& v2) const
61  { return csVector3(x+v2.x, y+v2.y, z+v2.z); }
62 
64  inline csVector3 operator- (const csVector3& v2) const
65  { return csVector3(x-v2.x, y-v2.y, z-v2.z); }
66 
68 
70 
72  inline friend float operator* (const csVector3& v1, const csVector3& v2)
73  { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
74 
76  inline friend csVector3 operator% (const csVector3& v1, const csVector3& v2)
77  {
78  return csVector3 (v1.y*v2.z-v1.z*v2.y,
79  v1.z*v2.x-v1.x*v2.z,
80  v1.x*v2.y-v1.y*v2.x);
81  }
82 
84  void Cross (const csVector3 & px, const csVector3 & py)
85  {
86  x = px.y*py.z - px.z*py.y;
87  y = px.z*py.x - px.x*py.z;
88  z = px.x*py.y - px.y*py.x;
89  }
90 
91  inline void Cross (const Opcode::Point & px, const Opcode::Point & py)
92  {
93  x = px.y*py.z - px.z*py.y;
94  y = px.z*py.x - px.x*py.z;
95  z = px.x*py.y - px.y*py.x;
96  }
97 
99  inline friend csVector3 operator* (const csVector3& v, float f)
100  { return csVector3(v.x*f, v.y*f, v.z*f); }
101 
103  inline friend csVector3 operator* (float f, const csVector3& v)
104  { return csVector3(v.x*f, v.y*f, v.z*f); }
105 
106 
108  inline friend csVector3 operator* (const csVector3& v, int f)
109  { return csVector3(v.x*f, v.y*f, v.z*f); }
110 
112  inline friend csVector3 operator* (int f, const csVector3& v)
113  { return csVector3(v.x*f, v.y*f, v.z*f); }
114 
116  inline friend csVector3 operator/ (const csVector3& v, float f)
117  { f = 1.0f/f; return csVector3(v.x*f, v.y*f, v.z*f); }
118 
119 
121  inline friend csVector3 operator/ (const csVector3& v, int f)
122  { float F = 1.0f/f; return csVector3(v.x*F, v.y*F, v.z*F); }
123 
125  inline friend bool operator== (const csVector3& v1, const csVector3& v2)
126  { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z; }
127 
129  inline friend bool operator!= (const csVector3& v1, const csVector3& v2)
130  { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z; }
131 
133  inline friend csVector3 operator>> (const csVector3& v1, const csVector3& v2)
134  { return ((v1*v2)*v2)/(v2*v2); }
135 
137  inline friend csVector3 operator<< (const csVector3& v1, const csVector3& v2)
138  { return ((v1*v2)*v1)/(v1*v1); }
139 
141  inline friend bool operator< (const csVector3& v, float f)
142  { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
143 
145  inline friend bool operator> (float f, const csVector3& v)
146  { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
147 
149  inline float operator[] (int n) const { return !n?x:n&1?y:z; }
150 
152  inline float & operator[] (int n) { return !n?x:n&1?y:z; }
153 
156  {
157  x += v.x;
158  y += v.y;
159  z += v.z;
160 
161  return *this;
162  }
163 
166  {
167  x -= v.x;
168  y -= v.y;
169  z -= v.z;
170 
171  return *this;
172  }
173 
175  inline csVector3& operator*= (float f)
176  { x *= f; y *= f; z *= f; return *this; }
177 
179  inline csVector3& operator/= (float f)
180  { f = 1.0f / f; x *= f; y *= f; z *= f; return *this; }
181 
183  inline csVector3 operator+ () const { return *this; }
184 
186  inline csVector3 operator- () const { return csVector3(-x,-y,-z); }
187 
189  inline void Set (float sx, float sy, float sz) { x = sx; y = sy; z = sz; }
190 
192  inline void Set (const csVector3& v) { x = v.x; y = v.y; z = v.z; }
193 
195  float Norm () const;
196 
198  float SquaredNorm () const
199  { return x * x + y * y + z * z; }
200 
206  csVector3 Unit () const { return (*this)/(this->Norm()); }
207 
209  inline static float Norm (const csVector3& v) { return v.Norm(); }
210 
212  inline static csVector3 Unit (const csVector3& v) { return v.Unit(); }
213 
215  void Normalize ();
216 
218  inline bool IsZero () const
219  { return (x == 0) && (y == 0) && (z == 0); }
220 };
221 
222 #endif // __CS_VECTOR3_H__