Tanoda
Vector.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9namespace Leap {
10 using System;
11
15 public static class Constants {
16 public const float PI = 3.1415926536f;
17 public const float DEG_TO_RAD = 0.0174532925f;
18 public const float RAD_TO_DEG = 57.295779513f;
19 public const float EPSILON = 1.192092896e-07f;
20 }
21
35 [Serializable]
36 public struct Vector : IEquatable<Vector> {
37
38 public static Vector operator +(Vector v1, Vector v2) {
39 return new Vector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
40 }
41
42 public static Vector operator -(Vector v1, Vector v2) {
43 return new Vector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
44 }
45
46 public static Vector operator *(Vector v1, float scalar) {
47 return new Vector(v1.x * scalar, v1.y * scalar, v1.z * scalar);
48 }
49
50 public static Vector operator *(float scalar, Vector v1) {
51 return new Vector(v1.x * scalar, v1.y * scalar, v1.z * scalar);
52 }
53
54 public static Vector operator /(Vector v1, float scalar) {
55 return new Vector(v1.x / scalar, v1.y / scalar, v1.z / scalar);
56 }
57
58 public static Vector operator -(Vector v1) {
59 return new Vector(-v1.x, -v1.y, -v1.z);
60 }
61
62 public static bool operator ==(Vector v1, Vector v2) {
63 return v1.Equals(v2);
64 }
65
66 public static bool operator !=(Vector v1, Vector v2) {
67 return !v1.Equals(v2);
68 }
69
70 public float[] ToFloatArray() {
71 return new float[] { x, y, z };
72 }
73
78 public Vector(float x, float y, float z) :
79 this() {
80 this.x = x;
81 this.y = y;
82 this.z = z;
83 }
84
89 public Vector(Vector vector) :
90 this() {
91 x = vector.x;
92 y = vector.y;
93 z = vector.z;
94 }
95
102 public float DistanceTo(Vector other) {
103 return (float)Math.Sqrt((x - other.x) * (x - other.x) +
104 (y - other.y) * (y - other.y) +
105 (z - other.z) * (z - other.z));
106
107 }
108
120 public float AngleTo(Vector other) {
121 float denom = MagnitudeSquared * other.MagnitudeSquared;
122 if (denom <= Constants.EPSILON) {
123 return 0.0f;
124 }
125 float val = Dot(other) / (float)Math.Sqrt(denom);
126 if (val >= 1.0f) {
127 return 0.0f;
128 } else if (val <= -1.0f) {
129 return Constants.PI;
130 }
131 return (float)Math.Acos(val);
132 }
133
141 public float Dot(Vector other) {
142 return (x * other.x) + (y * other.y) + (z * other.z);
143 }
144
155 public Vector Cross(Vector other) {
156 return new Vector((y * other.z) - (z * other.y),
157 (z * other.x) - (x * other.z),
158 (x * other.y) - (y * other.x));
159 }
160
165 public override string ToString() {
166 return "(" + x + ", " + y + ", " + z + ")";
167 }
168
173 public bool Equals(Vector v) {
174 return x.NearlyEquals(v.x) && y.NearlyEquals(v.y) && z.NearlyEquals(v.z);
175 }
176
177 public override bool Equals(Object obj) {
178 return obj is Vector && Equals((Vector)obj);
179 }
180
186 public bool IsValid() {
187 return !(float.IsNaN(x) || float.IsInfinity(x) ||
188 float.IsNaN(y) || float.IsInfinity(y) ||
189 float.IsNaN(z) || float.IsInfinity(z));
190 }
191
197 public float this[uint index] {
198 get {
199 if (index == 0)
200 return x;
201 if (index == 1)
202 return y;
203 if (index == 2)
204 return z;
205 throw new IndexOutOfRangeException();
206 }
207 set {
208 if (index == 0)
209 x = value;
210 if (index == 1)
211 y = value;
212 if (index == 2)
213 z = value;
214 throw new IndexOutOfRangeException();
215 }
216 }
217
218 public float x;
219 public float y;
220 public float z;
221
229 public float Magnitude {
230 get { return (float)Math.Sqrt(x * x + y * y + z * z); }
231 }
232
237 public float MagnitudeSquared {
238 get { return x * x + y * y + z * z; }
239 }
240
252 public float Pitch {
253 get { return (float)Math.Atan2(y, -z); }
254 }
255
272 public float Roll {
273 get { return (float)Math.Atan2(x, -y); }
274 }
275
287 public float Yaw {
288 get { return (float)Math.Atan2(x, -z); }
289 }
290
300 get {
301 float denom = MagnitudeSquared;
302 if (denom <= Constants.EPSILON) {
303 return Zero;
304 }
305 denom = 1.0f / (float)Math.Sqrt(denom);
306 return new Vector(x * denom, y * denom, z * denom);
307 }
308 }
309
313 public static readonly Vector Zero = new Vector(0, 0, 0);
314
318 public static readonly Vector Ones = new Vector(1, 1, 1);
319
323 public static readonly Vector XAxis = new Vector(1, 0, 0);
324
328 public static readonly Vector YAxis = new Vector(0, 1, 0);
329
333 public static readonly Vector ZAxis = new Vector(0, 0, 1);
334
338 public static readonly Vector Forward = new Vector(0, 0, -1);
339
343 public static readonly Vector Backward = new Vector(0, 0, 1);
344
348 public static readonly Vector Left = new Vector(-1, 0, 0);
349
353 public static readonly Vector Right = new Vector(1, 0, 0);
354
358 public static readonly Vector Up = new Vector(0, 1, 0);
359
363 public static readonly Vector Down = new Vector(0, -1, 0);
364
365
366 public static Vector Lerp(Vector a, Vector b, float t) {
367 return new Vector(
368 a.x + t * (b.x - a.x),
369 a.y + t * (b.y - a.y),
370 a.z + t * (b.z - a.z)
371 );
372 }
373
374 public override int GetHashCode() {
375 unchecked // Overflow is fine, just wrap
376 {
377 int hash = 17;
378 hash = hash * 23 + x.GetHashCode();
379 hash = hash * 23 + y.GetHashCode();
380 hash = hash * 23 + z.GetHashCode();
381
382 return hash;
383 }
384 }
385 }
386}
Es.InkPainter.Math Math
Definition: PaintTest.cs:7
UnityEngine.Object Object
The Vector struct represents a three-component mathematical vector or point such as a direction or po...
Definition: Vector.cs:36
static readonly Vector Zero
The zero vector: (0, 0, 0)
Definition: Vector.cs:313
override bool Equals(Object obj)
Definition: Vector.cs:177
override string ToString()
Returns a string containing this vector in a human readable format: (x, y, z).
Definition: Vector.cs:165
static readonly Vector Ones
The ones vector: (1, 1, 1)
Definition: Vector.cs:318
Vector(Vector vector)
Copies the specified Vector.
Definition: Vector.cs:89
Vector Normalized
A normalized copy of this vector.
Definition: Vector.cs:299
static readonly Vector ZAxis
The z-axis unit vector: (0, 0, 1)
Definition: Vector.cs:333
Vector(float x, float y, float z)
Creates a new Vector with the specified component values.
Definition: Vector.cs:78
static readonly Vector Forward
The unit vector pointing forward along the negative z-axis: (0, 0, -1)
Definition: Vector.cs:338
float y
Definition: Vector.cs:219
static Vector Lerp(Vector a, Vector b, float t)
Definition: Vector.cs:366
float AngleTo(Vector other)
The angle between this vector and the specified vector in radians.
Definition: Vector.cs:120
static readonly Vector Down
The unit vector pointing down along the negative y-axis: (0, -1, 0)
Definition: Vector.cs:363
static readonly Vector Left
The unit vector pointing left along the negative x-axis: (-1, 0, 0)
Definition: Vector.cs:348
static Vector operator-(Vector v1, Vector v2)
Definition: Vector.cs:42
static Vector operator/(Vector v1, float scalar)
Definition: Vector.cs:54
float Dot(Vector other)
The dot product of this vector with another vector.
Definition: Vector.cs:141
float DistanceTo(Vector other)
The distance between the point represented by this Vector object and a point represented by the speci...
Definition: Vector.cs:102
static readonly Vector Right
The unit vector pointing right along the positive x-axis: (1, 0, 0)
Definition: Vector.cs:353
float x
Definition: Vector.cs:218
override int GetHashCode()
Definition: Vector.cs:374
bool IsValid()
Returns true if all of the vector's components are finite. If any component is NaN or infinite,...
Definition: Vector.cs:186
bool Equals(Vector v)
Compare Vector equality component-wise.
Definition: Vector.cs:173
static bool operator!=(Vector v1, Vector v2)
Definition: Vector.cs:66
static readonly Vector YAxis
The y-axis unit vector: (0, 1, 0)
Definition: Vector.cs:328
float Yaw
The yaw angle in radians.
Definition: Vector.cs:287
float Pitch
The pitch angle in radians.
Definition: Vector.cs:252
Vector Cross(Vector other)
The cross product of this vector and the specified vector.
Definition: Vector.cs:155
static readonly Vector XAxis
The x-axis unit vector: (1, 0, 0)
Definition: Vector.cs:323
static readonly Vector Backward
The unit vector pointing backward along the positive z-axis: (0, 0, 1)
Definition: Vector.cs:343
static readonly Vector Up
The unit vector pointing up along the positive y-axis: (0, 1, 0)
Definition: Vector.cs:358
static bool operator==(Vector v1, Vector v2)
Definition: Vector.cs:62
static Vector operator*(Vector v1, float scalar)
Definition: Vector.cs:46
float MagnitudeSquared
The square of the magnitude, or length, of this vector.
Definition: Vector.cs:237
static Vector operator+(Vector v1, Vector v2)
Definition: Vector.cs:38
float Roll
The roll angle in radians.
Definition: Vector.cs:272
float z
Definition: Vector.cs:220
float Magnitude
The magnitude, or length, of this vector.
Definition: Vector.cs:229
float[] ToFloatArray()
Definition: Vector.cs:70