EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Vector3D.h
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: eve-moo
24  */
25 
30 #ifndef VECTOR3D_H
31 #define VECTOR3D_H
32 
33 #include "utils/misc.h"
34 
35 #include <math.h>
36 
37 class Vector3D
38 {
39 public:
40  double x, y, z;
41 
43  {
44  x = y = z = 0;
45  }
46 
47  Vector3D(double nx, double ny, double nz)
48  {
49  x = nx;
50  y = ny;
51  z = nz;
52  }
53 
54  Vector3D(const Vector3D &v)
55  {
56  x = v.x;
57  y = v.y;
58  z = v.z;
59  }
60 
65  Vector3D copy() const
66  {
67  return Vector3D(*this);
68  }
69 
74  double magnitude() const;
75 
76  double length() const
77  {
78  return magnitude();
79  }
80 
81  double lengthSquared() const
82  {
83  return (x * x) + (y * y) + (z * z);
84  }
85 
89  void normalize();
90 
98  double dotProduct(const Vector3D &v) const;
99 
107  Vector3D crossProduct(const Vector3D &v) const;
108 
114  Vector3D reflection(Vector3D norm) const;
115 
122  Vector3D refraction(Vector3D norm, double fact) const;
123 
130  void set(double nx, double ny, double nz);
135  void set(const Vector3D &v);
136 
137  bool operator==(const Vector3D &v)
138  {
139  return v.x == x && v.y == y && v.z == z;
140  }
141  bool operator!=(const Vector3D &v)
142  {
143  return v.x != x || v.y != y || v.z != z;
144  }
145 
146  Vector3D& operator=(const Vector3D &v);
147 
148  Vector3D operator+(const Vector3D &v) const;
149  Vector3D& operator+=(const Vector3D &v);
150  Vector3D operator-(const Vector3D &v) const;
151  Vector3D& operator-=(const Vector3D &v);
152  Vector3D operator*(const Vector3D &v) const;
153  Vector3D& operator*=(const Vector3D &v);
154  Vector3D operator/(const Vector3D &v) const;
155  Vector3D& operator/=(const Vector3D &v);
156 
157  //scale the Vector3D
158  Vector3D operator*(const double &k) const;
159  Vector3D& operator*=(const double &k);
160  Vector3D operator/(const double &k) const;
161  Vector3D& operator/=(const double &k);
162 
163  bool isNotZero()
164  {
165  return x != 0 || y != 0 || z != 0;
166  }
167 
168  // Public functions for manipulating 3D coordinates in space:
169  // Take existing (x,y,z) point and use that as the center of a sphere of 'radius' and
170  // modify it to be a new (x,y,z) point randomly placed on that sphere about the original
171  // center coordinate: (x,y,z)
172 
173  void MakeRandomPointOnSphere(double radius)
174  {
175  double theta = MakeRandomFloat(0.0, (2 * M_PI));
176  double phi = MakeRandomFloat(0.0, (2 * M_PI));
177  x += radius * sin(theta) * cos(phi);
178  y += radius * sin(theta) * sin(phi);
179  z += radius * cos(theta);
180  }
181 
182  // Take existing (x,y,z) point and use that as the center of two spheres of 'radiusInner', the
183  // smaller radius sphere, and 'radiusOuter', the larger radius sphere, and modify
184  // the original coordinate to be a new (x,y,z) point randomly placed somewhere inside the volume
185  // enclosed between the smaller sphere and the large sphere
186 
187  void MakeRandomPointOnSphereLayer(double radiusInner, double radiusOuter)
188  {
189  double theta = MakeRandomFloat(0.0, (2 * M_PI));
190  double phi = MakeRandomFloat(0.0, (2 * M_PI));
191  double intermediateRadius = MakeRandomFloat(radiusInner, radiusOuter);
192  x += intermediateRadius * sin(theta) * cos(phi);
193  y += intermediateRadius * sin(theta) * sin(phi);
194  z += intermediateRadius * cos(theta);
195  }
196 };
197 
198 #endif
199 
void MakeRandomPointOnSphereLayer(double radiusInner, double radiusOuter)
Definition: Vector3D.h:187
double z
Definition: Vector3D.h:40
double MakeRandomFloat(double low, double high)
Generates random real from interval [low; high].
Definition: misc.cpp:114
void normalize()
Definition: Vector3D.cpp:39
Vector3D & operator=(const Vector3D &v)
Definition: Vector3D.cpp:109
Vector3D(double nx, double ny, double nz)
Definition: Vector3D.h:47
Vector3D operator*(const Vector3D &v) const
Definition: Vector3D.cpp:143
double length() const
Definition: Vector3D.h:76
Vector3D operator+(const Vector3D &v) const
Definition: Vector3D.cpp:117
void MakeRandomPointOnSphere(double radius)
Definition: Vector3D.h:173
Vector3D copy() const
Definition: Vector3D.h:65
bool operator!=(const Vector3D &v)
Definition: Vector3D.h:141
Vector3D & operator-=(const Vector3D &v)
Definition: Vector3D.cpp:135
void set(double nx, double ny, double nz)
Definition: Vector3D.cpp:95
Vector3D refraction(Vector3D norm, double fact) const
Definition: Vector3D.cpp:80
double lengthSquared() const
Definition: Vector3D.h:81
double y
Definition: Vector3D.h:40
bool operator==(const Vector3D &v)
Definition: Vector3D.h:137
Vector3D(const Vector3D &v)
Definition: Vector3D.h:54
double magnitude() const
Definition: Vector3D.cpp:28
double dotProduct(const Vector3D &v) const
Definition: Vector3D.cpp:59
Vector3D reflection(Vector3D norm) const
Definition: Vector3D.cpp:72
Vector3D & operator/=(const Vector3D &v)
Definition: Vector3D.cpp:161
Vector3D()
Definition: Vector3D.h:42
Vector3D operator/(const Vector3D &v) const
Definition: Vector3D.cpp:156
Vector3D operator-(const Vector3D &v) const
Definition: Vector3D.cpp:130
double x
Definition: Vector3D.h:40
bool isNotZero()
Definition: Vector3D.h:163
Vector3D crossProduct(const Vector3D &v) const
Definition: Vector3D.cpp:64
Vector3D & operator+=(const Vector3D &v)
Definition: Vector3D.cpp:122
Vector3D & operator*=(const Vector3D &v)
Definition: Vector3D.cpp:148