Tanoda
Direction3.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
9using UnityEngine;
10
11namespace Leap.Unity.Geometry {
12
23 [System.Serializable]
24 public struct Direction3 {
25
26 [SerializeField]
27 private float x;
28
29 [SerializeField]
30 private float y;
31
32 [SerializeField]
33 private float z;
34
35 public Direction3(Vector3 v) {
36 x = v.x; y = v.y; z = v.z;
37 }
38
39 public Direction3(float x, float y, float z) {
40 this.x = x; this.y = y; this.z = z;
41 }
42
48 public bool isValid { get { return new Vector3(x, y, z) != Vector3.zero; } }
49
51 public Vector3 Vec() {
52 return this;
53 }
54
55 public static implicit operator Vector3(Direction3 dir) {
56 var normalized = new Vector3(dir.x, dir.y, dir.z).normalized;
57 if (normalized == Vector3.zero) {
58 return Vector3.forward;
59 }
60 return normalized;
61 }
62
63 public static implicit operator Direction3(Vector3 vec) {
64 return new Direction3(vec);
65 }
66
75 public static bool PointsInSameDirection(Direction3 A, Direction3 B) {
76 Vector3 aV = new Vector3(A.x, A.y, A.z);
77 Vector3 bV = new Vector3(B.x, B.y, B.z);
78 return Vector3.Cross(aV, bV).sqrMagnitude == 0f;
79 }
80
81 }
82
83}
A struct very similar to Vector3, but that prevents itself from ever being converted to Vector3....
Definition: Direction3.cs:24
bool isValid
Gets whether this Direction3 will normalize without issue (has nonzero magnitude),...
Definition: Direction3.cs:48
static bool PointsInSameDirection(Direction3 A, Direction3 B)
Returns whether two Direction3s point in the same direction without normalizing either of the underly...
Definition: Direction3.cs:75
Direction3(float x, float y, float z)
Definition: Direction3.cs:39
Vector3 Vec()
Explicitly converts this Direction3 to a Vector3.
Definition: Direction3.cs:51