< Summary

Information
Class: MorphoGeometry.Vector
Assembly: MorphoGeometry
File(s): D:\a\Morpho\Morpho\project\Morpho\MorphoGeometry\Vector.cs
Line coverage
53%
Covered lines: 53
Uncovered lines: 46
Coverable lines: 99
Total lines: 228
Line coverage: 53.5%
Branch coverage
44%
Covered branches: 8
Total branches: 18
Branch coverage: 44.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
Det3x3(...)100%10%
FromArray(...)100%10%
ToArray()100%10%
Sub(...)100%1100%
Dot(...)100%10%
Cross(...)100%1100%
VectorFrom2Points(...)100%10%
Length()100%1100%
Normalize()100%1100%
ToString()100%10%
Serialize()100%1100%
Deserialize(...)100%1100%
Equals(...)50%1080%
Equals(...)0%40%
GetHashCode()100%10%
op_Equality(...)75%4100%
op_Inequality(...)100%1100%

File(s)

D:\a\Morpho\Morpho\project\Morpho\MorphoGeometry\Vector.cs

#LineLine coverage
 1using Newtonsoft.Json;
 2using System;
 3
 4namespace 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>
 44598736        public Vector(float x, float y, float z)
 44598737        {
 44598738            this.x = x;
 44598739            this.y = y;
 44598740            this.z = z;
 44598741        }
 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)
 049        {
 050            return matr[0][0] * matr[1][1] * matr[2][2] +
 051                matr[0][1] * matr[1][2] * matr[2][0] +
 052                matr[0][2] * matr[1][0] * matr[2][1] -
 053                (
 054                    matr[0][2] * matr[1][1] * matr[2][0] +
 055                    matr[0][1] * matr[1][0] * matr[2][2] +
 056                    matr[0][0] * matr[1][2] * matr[2][1]
 057                );
 058        }
 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)
 066        {
 067            return new Vector(arr[0], arr[1], arr[2]);
 068        }
 69
 70        /// <summary>
 71        /// Convert Vector to array.
 72        /// </summary>
 73        /// <returns>Array.</returns>
 74        public float[] ToArray()
 075        {
 076            return new[] { x, y, z };
 077        }
 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)
 5285        {
 5286            return new Vector(this.x - v.x,
 5287                            this.y - v.y,
 5288                            this.z - v.z);
 5289        }
 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)
 097        {
 098            return this.x * v.x +
 099                   this.y * v.y +
 0100                   this.z * v.z;
 0101        }
 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)
 26109        {
 26110            return new Vector(
 26111                this.y * v.z - this.z * v.y,
 26112                this.z * v.x - this.x * v.z,
 26113                this.x * v.y - this.y * v.x
 26114            );
 26115        }
 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)
 0125        {
 0126            return new Vector(vec2.x - vec1.x,
 0127                vec2.y - vec1.y, vec2.z - vec1.z);
 0128        }
 129
 130        /// <summary>
 131        /// Magnitude of the vector.
 132        /// </summary>
 133        /// <returns>Value.</returns>
 134        public float Length()
 26135        {
 26136            return (float) Math.Sqrt(x * x +
 26137                y * y + z * z);
 26138        }
 139
 140        /// <summary>
 141        /// Normalize the vector.
 142        /// </summary>
 143        /// <returns>New vector.</returns>
 144        public Vector Normalize()
 26145        {
 26146            float len = Length();
 26147            return new Vector(x / len,
 26148                y / len, z / len);
 26149        }
 150
 151        /// <summary>
 152        /// String representation of the vector.
 153        /// </summary>
 154        /// <returns>String representation.</returns>
 155        public override String ToString()
 0156        {
 0157            return string.Format("{0}, {1}, {2}", x, y, z);
 0158        }
 159
 160        public string Serialize()
 1161        {
 1162            return JsonConvert.SerializeObject(this);
 1163        }
 164
 165        public static Vector Deserialize(string json)
 2166        {
 167            try
 2168            {
 2169                return JsonConvert.DeserializeObject<Vector>(json);
 170            }
 1171            catch (Exception e)
 1172            {
 1173                throw new Exception(e.Message);
 174            }
 1175        }
 176
 177        public bool Equals(Vector other)
 60178        {
 60179            if (other == null)
 0180                return false;
 181
 60182            if (other != null
 60183                && other.x == this.x
 60184                && other.y == this.y
 60185                && other.z == this.z)
 60186                return true;
 187            else
 0188                return false;
 60189        }
 190
 191        public override bool Equals(Object obj)
 0192        {
 0193            if (obj == null)
 0194                return false;
 195
 0196            var vecObj = obj as Vector;
 0197            if (vecObj == null)
 0198                return false;
 199            else
 0200                return Equals(vecObj);
 0201        }
 202
 203        public override int GetHashCode()
 0204        {
 205            unchecked
 0206            {
 0207                int hash = 17;
 0208                hash = hash * 23 + x.GetHashCode();
 0209                hash = hash * 23 + y.GetHashCode();
 0210                hash = hash * 23 + z.GetHashCode();
 0211                return hash;
 212            }
 0213        }
 214
 215        public static bool operator ==(Vector vec1, Vector vec2)
 139216        {
 139217            if (((object)vec1) == null || ((object)vec2) == null)
 120218                return Object.Equals(vec1, vec2);
 219
 19220            return vec1.Equals(vec2);
 139221        }
 222
 223        public static bool operator !=(Vector vec1, Vector vec2)
 60224        {
 60225            return !(vec1 == vec2);
 60226        }
 227    }
 228}