diff --git a/Assets/NanoBrain/LinearAlgebra/src/Spherical.cs b/Assets/NanoBrain/LinearAlgebra/src/Spherical.cs
index de06d24..1c92faa 100644
--- a/Assets/NanoBrain/LinearAlgebra/src/Spherical.cs
+++ b/Assets/NanoBrain/LinearAlgebra/src/Spherical.cs
@@ -3,21 +3,18 @@ using System;
using Vector3 = UnityEngine.Vector3;
#endif
-namespace LinearAlgebra
-{
+namespace LinearAlgebra {
///
/// A spherical vector
///
/// This is a struct such that it is a value type and cannot be null
- public struct Spherical
- {
+ public struct Spherical {
///
/// Create a spherical vector
///
/// The distance in meters
/// The direction of the vector
- public Spherical(float distance, Direction direction)
- {
+ public Spherical(float distance, Direction direction) {
this.distance = distance;
this.direction = direction;
}
@@ -29,15 +26,13 @@ namespace LinearAlgebra
/// The horizontal angle in degrees
/// The vertical angle in degrees
///
- public static Spherical Degrees(float distance, float horizontal, float vertical)
- {
+ public static Spherical Degrees(float distance, float horizontal, float vertical) {
Direction direction = Direction.Degrees(horizontal, vertical);
Spherical s = new(distance, direction);
return s;
}
- public static Spherical Radians(float distance, float horizontal, float vertical)
- {
+ public static Spherical Radians(float distance, float horizontal, float vertical) {
Direction direction = Direction.Radians(horizontal, vertical);
Spherical s = new(distance, direction);
return s;
@@ -62,79 +57,19 @@ namespace LinearAlgebra
///
public readonly static Spherical forward = new(1, Direction.forward);
-
- // public static Spherical FromVector3Float(Vector3Float v) {
- // float distance = v.magnitude;
- // if (distance == 0.0f)
- // return Spherical.zero;
- // else {
- // float verticalAngle = (float)((Angle.pi / 2 - Math.Acos(v.y / distance)) * Angle.Rad2Deg);
- // float horizontalAngle = (float)Math.Atan2(v.x, v.z) * Angle.Rad2Deg;
- // return Spherical.Degrees(distance, horizontalAngle, verticalAngle);
- // }
- // }
-
- public static Spherical FromVector3Float(Vector3Float v)
- {
- float distance = v.magnitude;
- if (distance == 0.0f)
- return Spherical.zero;
- else
- {
- float verticalAngle = (float)(Math.PI / 2 - Math.Acos(v.vertical / distance)) * AngleFloat.Rad2Deg;
- float horizontalAngle = (float)Math.Atan2(v.horizontal, v.depth) * AngleFloat.Rad2Deg;
- return Degrees(distance, horizontalAngle, verticalAngle);
- }
- }
-
- // public Vector3Float ToVector3Float() {
- // float verticalRad = (Angle.pi / 2) - this.direction.vertical * Angle.Deg2Rad;
- // float horizontalRad = this.direction.horizontal * Angle.Deg2Rad;
- // float cosVertical = (float)Math.Cos(verticalRad);
- // float sinVertical = (float)Math.Sin(verticalRad);
- // float cosHorizontal = (float)Math.Cos(horizontalRad);
- // float sinHorizontal = (float)Math.Sin(horizontalRad);
-
- // float x = this.distance * sinVertical * sinHorizontal;
- // float y = this.distance * cosVertical;
- // float z = this.distance * sinVertical * cosHorizontal;
-
- // Vector3Float v = new(x, y, z);
- // return v;
- // }
-
- public Vector3Float ToVector3Float()
- {
- float verticalRad = (AngleFloat.deg90 - this.direction.vertical).inRadians;
- float horizontalRad = this.direction.horizontal.inRadians;
- float cosVertical = (float)Math.Cos(verticalRad);
- float sinVertical = (float)Math.Sin(verticalRad);
- float cosHorizontal = (float)Math.Cos(horizontalRad);
- float sinHorizontal = (float)Math.Sin(horizontalRad);
-
- float x = this.distance * sinVertical * sinHorizontal;
- float y = this.distance * cosVertical;
- float z = this.distance * sinVertical * cosHorizontal;
-
- Vector3Float v = new(x, y, z);
- return v;
- }
#if UNITY_5_3_OR_NEWER
- public static Spherical FromVector3(Vector3 v)
- {
+ public static Spherical FromVector3(Vector3 v) {
float distance = v.magnitude;
if (distance == 0.0f)
return Spherical.zero;
- else
- {
+ else {
float verticalAngle = (float)(Math.PI / 2 - Math.Acos(v.y / distance)) * AngleFloat.Rad2Deg;
float horizontalAngle = (float)Math.Atan2(v.x, v.z) * AngleFloat.Rad2Deg;
return Degrees(distance, horizontalAngle, verticalAngle);
}
}
- public Vector3 ToVector3()
- {
+ public readonly Vector3 ToVector3() {
float verticalRad = (AngleFloat.deg90 - this.direction.vertical).inRadians;
float horizontalRad = this.direction.horizontal.inRadians;
float cosVertical = (float)Math.Cos(verticalRad);
@@ -149,21 +84,42 @@ namespace LinearAlgebra
Vector3 v = new(x, y, z);
return v;
}
-#endif
-
- public float magnitude
+#else
+ public static Spherical FromVector3(Vector3Float v)
{
- get
+ float distance = v.magnitude;
+ if (distance == 0.0f)
+ return Spherical.zero;
+ else
{
- return this.distance;
+ float verticalAngle = (float)(Math.PI / 2 - Math.Acos(v.vertical / distance)) * AngleFloat.Rad2Deg;
+ float horizontalAngle = (float)Math.Atan2(v.horizontal, v.depth) * AngleFloat.Rad2Deg;
+ return Degrees(distance, horizontalAngle, verticalAngle);
}
}
- public Spherical normalized
- {
- get
- {
- Spherical r = new()
- {
+
+ public readonly Vector3Float ToVector3() {
+ float verticalRad = (AngleFloat.deg90 - this.direction.vertical).inRadians;
+ float horizontalRad = this.direction.horizontal.inRadians;
+ float cosVertical = (float)Math.Cos(verticalRad);
+ float sinVertical = (float)Math.Sin(verticalRad);
+ float cosHorizontal = (float)Math.Cos(horizontalRad);
+ float sinHorizontal = (float)Math.Sin(horizontalRad);
+
+ float x = this.distance * sinVertical * sinHorizontal;
+ float y = this.distance * cosVertical;
+ float z = this.distance * sinVertical * cosHorizontal;
+
+ Vector3Float v = new(x, y, z);
+ return v;
+ }
+#endif
+
+ public readonly float magnitude => this.distance;
+
+ public Spherical normalized {
+ get {
+ Spherical r = new() {
distance = 1,
direction = this.direction
};
@@ -171,13 +127,13 @@ namespace LinearAlgebra
}
}
- public static Spherical operator +(Spherical s1, Spherical s2)
- {
+ public static Spherical operator +(Spherical s1, Spherical s2) {
// let's do it the easy way...
- Vector3Float v1 = s1.ToVector3Float();
- Vector3Float v2 = s2.ToVector3Float();
- Vector3Float v = v1 + v2;
- Spherical r = FromVector3Float(v);
+ // using vars to be compatible with both unity (Vector3) and native (Vector3Float)
+ var v1 = s1.ToVector3();
+ var v2 = s2.ToVector3();
+ var v = v1 + v2;
+ Spherical r = FromVector3(v);
return r;
}