| | 1 | | using Newtonsoft.Json; |
| | 2 | | using System; |
| | 3 | |
|
| | 4 | | namespace MorphoGeometry |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// Vector class. |
| | 8 | | /// </summary> |
| | 9 | | public class Vector: IEquatable<Vector> |
| | 10 | | { |
| | 11 | | [JsonProperty(Required = Required.Always)] |
| | 12 | | /// <summary> |
| | 13 | | /// X component. |
| | 14 | | /// </summary> |
| | 15 | | public float x; |
| | 16 | |
|
| | 17 | | [JsonProperty(Required = Required.Always)] |
| | 18 | | /// <summary> |
| | 19 | | /// Y component. |
| | 20 | | /// </summary> |
| | 21 | | public float y; |
| | 22 | |
|
| | 23 | | [JsonProperty(Required = Required.Always)] |
| | 24 | | /// <summary> |
| | 25 | | /// Z component. |
| | 26 | | /// </summary> |
| | 27 | | public float z; |
| | 28 | |
|
| | 29 | | [JsonConstructor] |
| | 30 | | /// <summary> |
| | 31 | | /// Create a new vector. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="x">X component.</param> |
| | 34 | | /// <param name="y">Y component.</param> |
| | 35 | | /// <param name="z">Z component.</param> |
| 445987 | 36 | | public Vector(float x, float y, float z) |
| 445987 | 37 | | { |
| 445987 | 38 | | this.x = x; |
| 445987 | 39 | | this.y = y; |
| 445987 | 40 | | this.z = z; |
| 445987 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Det 3x3. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="matr">Matrix to solve.</param> |
| | 47 | | /// <returns>Det.</returns> |
| | 48 | | public static float Det3x3(float[][] matr) |
| 0 | 49 | | { |
| 0 | 50 | | return matr[0][0] * matr[1][1] * matr[2][2] + |
| 0 | 51 | | matr[0][1] * matr[1][2] * matr[2][0] + |
| 0 | 52 | | matr[0][2] * matr[1][0] * matr[2][1] - |
| 0 | 53 | | ( |
| 0 | 54 | | matr[0][2] * matr[1][1] * matr[2][0] + |
| 0 | 55 | | matr[0][1] * matr[1][0] * matr[2][2] + |
| 0 | 56 | | matr[0][0] * matr[1][2] * matr[2][1] |
| 0 | 57 | | ); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Create a vector from array. |
| | 62 | | /// </summary> |
| | 63 | | /// <param name="arr">Array with 3 items.</param> |
| | 64 | | /// <returns>New vector.</returns> |
| | 65 | | public static Vector FromArray(float[] arr) |
| 0 | 66 | | { |
| 0 | 67 | | return new Vector(arr[0], arr[1], arr[2]); |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Convert Vector to array. |
| | 72 | | /// </summary> |
| | 73 | | /// <returns>Array.</returns> |
| | 74 | | public float[] ToArray() |
| 0 | 75 | | { |
| 0 | 76 | | return new[] { x, y, z }; |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Subtract vector. |
| | 81 | | /// </summary> |
| | 82 | | /// <param name="v">Vector to subtract.</param> |
| | 83 | | /// <returns>New vector.</returns> |
| | 84 | | public Vector Sub(Vector v) |
| 52 | 85 | | { |
| 52 | 86 | | return new Vector(this.x - v.x, |
| 52 | 87 | | this.y - v.y, |
| 52 | 88 | | this.z - v.z); |
| 52 | 89 | | } |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Dot product. |
| | 93 | | /// </summary> |
| | 94 | | /// <param name="v">Vector to multiply.</param> |
| | 95 | | /// <returns>Value.</returns> |
| | 96 | | public float Dot(Vector v) |
| 0 | 97 | | { |
| 0 | 98 | | return this.x * v.x + |
| 0 | 99 | | this.y * v.y + |
| 0 | 100 | | this.z * v.z; |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Cross product. |
| | 105 | | /// </summary> |
| | 106 | | /// <param name="v">Vector to multiply.</param> |
| | 107 | | /// <returns>New vector.</returns> |
| | 108 | | public Vector Cross(Vector v) |
| 26 | 109 | | { |
| 26 | 110 | | return new Vector( |
| 26 | 111 | | this.y * v.z - this.z * v.y, |
| 26 | 112 | | this.z * v.x - this.x * v.z, |
| 26 | 113 | | this.x * v.y - this.y * v.x |
| 26 | 114 | | ); |
| 26 | 115 | | } |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Vector from 2 points. |
| | 119 | | /// </summary> |
| | 120 | | /// <param name="vec1">First vector.</param> |
| | 121 | | /// <param name="vec2">Second vector.</param> |
| | 122 | | /// <returns>New Vector.</returns> |
| | 123 | | public static Vector VectorFrom2Points(Vector vec1, |
| | 124 | | Vector vec2) |
| 0 | 125 | | { |
| 0 | 126 | | return new Vector(vec2.x - vec1.x, |
| 0 | 127 | | vec2.y - vec1.y, vec2.z - vec1.z); |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// Magnitude of the vector. |
| | 132 | | /// </summary> |
| | 133 | | /// <returns>Value.</returns> |
| | 134 | | public float Length() |
| 26 | 135 | | { |
| 26 | 136 | | return (float) Math.Sqrt(x * x + |
| 26 | 137 | | y * y + z * z); |
| 26 | 138 | | } |
| | 139 | |
|
| | 140 | | /// <summary> |
| | 141 | | /// Normalize the vector. |
| | 142 | | /// </summary> |
| | 143 | | /// <returns>New vector.</returns> |
| | 144 | | public Vector Normalize() |
| 26 | 145 | | { |
| 26 | 146 | | float len = Length(); |
| 26 | 147 | | return new Vector(x / len, |
| 26 | 148 | | y / len, z / len); |
| 26 | 149 | | } |
| | 150 | |
|
| | 151 | | /// <summary> |
| | 152 | | /// String representation of the vector. |
| | 153 | | /// </summary> |
| | 154 | | /// <returns>String representation.</returns> |
| | 155 | | public override String ToString() |
| 0 | 156 | | { |
| 0 | 157 | | return string.Format("{0}, {1}, {2}", x, y, z); |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | public string Serialize() |
| 1 | 161 | | { |
| 1 | 162 | | return JsonConvert.SerializeObject(this); |
| 1 | 163 | | } |
| | 164 | |
|
| | 165 | | public static Vector Deserialize(string json) |
| 2 | 166 | | { |
| | 167 | | try |
| 2 | 168 | | { |
| 2 | 169 | | return JsonConvert.DeserializeObject<Vector>(json); |
| | 170 | | } |
| 1 | 171 | | catch (Exception e) |
| 1 | 172 | | { |
| 1 | 173 | | throw new Exception(e.Message); |
| | 174 | | } |
| 1 | 175 | | } |
| | 176 | |
|
| | 177 | | public bool Equals(Vector other) |
| 60 | 178 | | { |
| 60 | 179 | | if (other == null) |
| 0 | 180 | | return false; |
| | 181 | |
|
| 60 | 182 | | if (other != null |
| 60 | 183 | | && other.x == this.x |
| 60 | 184 | | && other.y == this.y |
| 60 | 185 | | && other.z == this.z) |
| 60 | 186 | | return true; |
| | 187 | | else |
| 0 | 188 | | return false; |
| 60 | 189 | | } |
| | 190 | |
|
| | 191 | | public override bool Equals(Object obj) |
| 0 | 192 | | { |
| 0 | 193 | | if (obj == null) |
| 0 | 194 | | return false; |
| | 195 | |
|
| 0 | 196 | | var vecObj = obj as Vector; |
| 0 | 197 | | if (vecObj == null) |
| 0 | 198 | | return false; |
| | 199 | | else |
| 0 | 200 | | return Equals(vecObj); |
| 0 | 201 | | } |
| | 202 | |
|
| | 203 | | public override int GetHashCode() |
| 0 | 204 | | { |
| | 205 | | unchecked |
| 0 | 206 | | { |
| 0 | 207 | | int hash = 17; |
| 0 | 208 | | hash = hash * 23 + x.GetHashCode(); |
| 0 | 209 | | hash = hash * 23 + y.GetHashCode(); |
| 0 | 210 | | hash = hash * 23 + z.GetHashCode(); |
| 0 | 211 | | return hash; |
| | 212 | | } |
| 0 | 213 | | } |
| | 214 | |
|
| | 215 | | public static bool operator ==(Vector vec1, Vector vec2) |
| 139 | 216 | | { |
| 139 | 217 | | if (((object)vec1) == null || ((object)vec2) == null) |
| 120 | 218 | | return Object.Equals(vec1, vec2); |
| | 219 | |
|
| 19 | 220 | | return vec1.Equals(vec2); |
| 139 | 221 | | } |
| | 222 | |
|
| | 223 | | public static bool operator !=(Vector vec1, Vector vec2) |
| 60 | 224 | | { |
| 60 | 225 | | return !(vec1 == vec2); |
| 60 | 226 | | } |
| | 227 | | } |
| | 228 | | } |