< Summary

Information
Class: MorphoGeometry.Face
Assembly: MorphoGeometry
File(s): D:\a\Morpho\Morpho\project\Morpho\MorphoGeometry\Face.cs
Line coverage
55%
Covered lines: 52
Uncovered lines: 41
Coverable lines: 93
Total lines: 217
Line coverage: 55.9%
Branch coverage
54%
Covered branches: 13
Total branches: 24
Branch coverage: 54.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_A()100%1100%
get_B()100%1100%
get_C()100%1100%
get_D()100%10%
get_Vertices()100%1100%
set_Vertices(...)100%4100%
IsPointBehind(...)0%40%
get_Normal()100%1100%
.ctor(...)100%1100%
IsQuad()100%1100%
Min()100%10%
Max()100%10%
Triangulate(...)100%10%
ToString()100%10%
Serialize()100%1100%
Deserialize(...)100%1100%
Equals(...)50%877.77%
Equals(...)50%475%
GetHashCode()100%10%
op_Equality(...)75%480%
op_Inequality(...)100%1100%

File(s)

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

#LineLine coverage
 1using Newtonsoft.Json;
 2using System;
 3using System.Linq;
 4
 5namespace MorphoGeometry
 6{
 7    /// <summary>
 8    /// Face class.
 9    /// </summary>
 10    public class Face : IEquatable<Face>
 11    {
 12        private Vector[] _vertices;
 13
 14        [JsonIgnore]
 15        /// <summary>
 16        /// First vertex.
 17        /// </summary>
 5218        public Vector A => Vertices[0];
 19
 20        [JsonIgnore]
 21        /// <summary>
 22        /// Second vertex.
 23        /// </summary>
 2624        public Vector B => Vertices[1];
 25
 26        [JsonIgnore]
 27        /// <summary>
 28        /// Third vertex.
 29        /// </summary>
 2630        public Vector C => Vertices[2];
 31
 32        [JsonIgnore]
 33        /// <summary>
 34        /// Fourth vertex
 35        /// </summary>
 036        public Vector D => Vertices[3];
 37
 38        [JsonProperty("vertices", Required = Required.Always)]
 39        /// <summary>
 40        /// Vertices of the face.
 41        /// </summary>
 42        public Vector[] Vertices
 43        {
 53744            get { return _vertices; }
 45            private set
 8918746            {
 8918747                if (value.Length == 3 || value.Length == 4)
 8918548                    _vertices = value;
 49                else
 250                    throw new ArgumentOutOfRangeException(
 251                          $"{nameof(value)} has contain 3 or 4 vectors.");
 8918552            }
 53        }
 54
 55        /// <summary>
 56        /// Is point behind a Face.
 57        /// </summary>
 58        /// <param name="point">Point for testing.</param>
 59        /// <returns>0, 1, -1. 0 onto the face. 1 in front
 60        /// of the face. -1 behind the face</returns>
 61        public int IsPointBehind(Vector point)
 062        {
 063            var v = Vector.VectorFrom2Points(A, point);
 064            if (Normal.Dot(v) > 0) return 1;
 065            else if (Normal.Dot(v) < 0) return -1;
 066            else return 0;
 067        }
 68
 69        [JsonIgnore]
 70        /// <summary>
 71        /// Normal vector.
 72        /// </summary>
 73        public Vector Normal
 74        {
 75            get
 2676            {
 2677                var dir = (B.Sub(A)).Cross(C.Sub(A));
 2678                var norm = dir.Normalize();
 2679                return norm;
 2680            }
 81        }
 82
 83        [JsonConstructor]
 84        /// <summary>
 85        /// Create a new face.
 86        /// </summary>
 87        /// <param name="vertices">Vertices.</param>
 8918788        public Face(Vector[] vertices)
 8918789        {
 8918790            Vertices = vertices;
 8918591        }
 92
 93        /// <summary>
 94        /// Face with 4 vertices.
 95        /// </summary>
 96        /// <returns>True or false.</returns>
 97        public bool IsQuad()
 3698        {
 3699            return (Vertices.Length == 4);
 36100        }
 101
 102        /// <summary>
 103        /// Minimun point of the face.
 104        /// </summary>
 105        /// <returns>Vector.</returns>
 106        public Vector Min()
 0107        {
 0108            var x = Vertices.Select(_ => _.x).Min();
 0109            var y = Vertices.Select(_ => _.y).Min();
 0110            var z = Vertices.Select(_ => _.z).Min();
 111
 0112            return new Vector(x, y, z);
 0113        }
 114
 115        /// <summary>
 116        /// Maximum point of the face.
 117        /// </summary>
 118        /// <returns>Vector.</returns>
 119        public Vector Max()
 0120        {
 0121            var x = Vertices.Select(_ => _.x).Max();
 0122            var y = Vertices.Select(_ => _.y).Max();
 0123            var z = Vertices.Select(_ => _.z).Max();
 124
 0125            return new Vector(x, y, z);
 0126        }
 127
 128        /// <summary>
 129        /// From quadrangular face to triangular face.
 130        /// </summary>
 131        /// <param name="face">Face to divide.</param>
 132        /// <returns>Array of triangular faces.</returns>
 133        public static Face[] Triangulate(Face face)
 0134        {
 0135            return new Face[]
 0136            {
 0137                new Face( new Vector[3] { face.A, face.B, face.C } ),
 0138                new Face( new Vector[3] { face.C, face.D, face.A } )
 0139            };
 0140        }
 141
 142        /// <summary>
 143        /// String representation of the face.
 144        /// </summary>
 145        /// <returns>String representation.</returns>
 146        public override String ToString()
 0147        {
 0148            return string.Format("Face::{0}", Vertices.Length);
 0149        }
 150
 151        public string Serialize()
 1152        {
 1153            return JsonConvert.SerializeObject(this);
 1154        }
 155
 156        public static Face Deserialize(string json)
 2157        {
 158            try
 2159            {
 2160                return JsonConvert.DeserializeObject<Face>(json);
 161            }
 1162            catch (Exception e)
 1163            {
 1164                throw new Exception(e.Message);
 165            }
 1166        }
 167
 168        public bool Equals(Face other)
 13169        {
 13170            if (other == null)
 0171                return false;
 172
 13173            if (other != null
 13174                && Enumerable.SequenceEqual(other.Vertices, this.Vertices)
 13175                && other.Normal == this.Normal)
 13176                return true;
 177            else
 0178                return false;
 13179        }
 180
 181        public override bool Equals(Object obj)
 53182        {
 53183            if (obj == null)
 0184                return false;
 185
 53186            var faceObj = obj as Face;
 53187            if (faceObj == null)
 53188                return false;
 189            else
 0190                return Equals(faceObj);
 53191        }
 192
 193        public override int GetHashCode()
 0194        {
 195            unchecked
 0196            {
 0197                int hash = 17;
 0198                hash = hash * 23 + Vertices.GetHashCode();
 0199                hash = hash * 23 + Normal.GetHashCode();
 0200                return hash;
 201            }
 0202        }
 203
 204        public static bool operator ==(Face face1, Face face2)
 79205        {
 79206            if (((object)face1) == null || ((object)face2) == null)
 79207                return Object.Equals(face1, face2);
 208
 0209            return face1.Equals(face2);
 79210        }
 211
 212        public static bool operator !=(Face face1, Face face2)
 13213        {
 13214            return !(face1 == face2);
 13215        }
 216    }
 217}