< Summary

Information
Class: Morpho25.Geometry.Grid
Assembly: Morpho25
File(s): D:\a\Morpho\Morpho\project\Morpho\Morpho25\Geometry\Grid.cs
Line coverage
84%
Covered lines: 169
Uncovered lines: 31
Coverable lines: 200
Total lines: 385
Line coverage: 84.5%
Branch coverage
70%
Covered branches: 45
Total branches: 64
Branch coverage: 70.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%2100%
.ctor(...)100%2100%
get_Size()100%1100%
get_NestingGrids()100%1100%
get_Telescope()100%1100%
set_Telescope(...)50%466.66%
get_StartTelescopeHeight()100%1100%
set_StartTelescopeHeight(...)50%266.66%
ToList()0%60%
get_CombineGridType()100%1100%
get_Xaxis()100%1100%
get_Yaxis()100%1100%
get_Zaxis()100%1100%
get_SequenceZ()100%1100%
get_IsSplitted()100%1100%
ToString()100%10%
SetXaxis()100%2100%
SetYaxis()100%2100%
SetSequenceAndExtension()87.5%8100%
Serialize()100%1100%
Deserialize(...)100%1100%
Equals(...)50%1483.33%
Equals(...)75%487.5%
GetHashCode()100%10%
op_Equality(...)75%480%
op_Inequality(...)100%1100%
GetEquidistantSequence()100%4100%
GetTelescopeSequence(...)100%4100%
GetCombinedSequence(...)100%6100%

File(s)

D:\a\Morpho\Morpho\project\Morpho\Morpho25\Geometry\Grid.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.ComponentModel.DataAnnotations;
 5using System.Linq;
 6using Morpho25.Utility;
 7using Newtonsoft.Json;
 8
 9namespace Morpho25.Geometry
 10{
 11    [DisplayName("Grid")]
 12    /// <summary>
 13    /// Grid class.
 14    /// </summary>
 15    public class Grid
 16    {
 17        /// <summary>
 18        /// Create a new Grid.
 19        /// </summary>
 20        /// <param name="size">Grid size object.</param>
 21        /// <param name="nestingGrids">Optional nestring grids.</param>
 822        public Grid(Size size,
 823            NestingGrids nestingGrids = null)
 824        {
 825            Size = size;
 826            Telescope = 0.0;
 827            StartTelescopeHeight = 0.0;
 828            CombineGridType = false;
 29
 830            SetSequenceAndExtension();
 831            SetXaxis();
 832            SetYaxis();
 33
 834            if (nestingGrids == null)
 235                NestingGrids = new NestingGrids();
 36            else
 637                NestingGrids = nestingGrids;
 838        }
 39
 40        [JsonConstructor]
 41        /// <summary>
 42        /// Create a new Grid.
 43        /// </summary>
 44        /// <param name="size">Grid size object.</param>
 45        /// <param name="telescope">Vertical increment to use for a telescopic grid.
 46        /// If 0.0 it will use equidistant grid.</param>
 47        /// <param name="startTelescopeHeight">Start increment z dimension at.</param>
 48        /// <param name="combineGridType">True to split the first cell.</param>
 49        /// <param name="nestingGrids">Optional nesting grids.</param>
 1150        public Grid(Size size,
 1151            double telescope,
 1152            double startTelescopeHeight,
 1153            bool combineGridType,
 1154            NestingGrids nestingGrids = null)
 1155        {
 1156            Size = size;
 1157            Telescope = telescope;
 1158            StartTelescopeHeight = startTelescopeHeight;
 1159            CombineGridType = combineGridType;
 60
 1161            SetSequenceAndExtension();
 1162            SetXaxis();
 1163            SetYaxis();
 64
 1165            if (nestingGrids == null)
 266                NestingGrids = new NestingGrids();
 67            else
 968                NestingGrids = nestingGrids;
 1169        }
 70
 71        private double _telescope;
 72        private double _startTelescopeHeight;
 73
 74        [DisplayName("Grid size")]
 75        [Description("Settings of the grid unit")]
 76        [JsonProperty("size", Required = Required.Always)]
 77        /// <summary>
 78        /// Grid size.
 79        /// </summary>
 1093880        public Size Size { get; }
 81
 82
 83        [DisplayName("Nesting grids")]
 84        [Description("Addictional grids for calculation scope.")]
 85        [JsonProperty("nestingGrids")]
 86        /// <summary>
 87        /// Nesting grids.
 88        /// </summary>
 2589        public NestingGrids NestingGrids { get; set; }
 90
 91        [DisplayName("Telescope Factor")]
 92        [Description("Set a telescopic grid.")]
 93        [Range(0, double.MaxValue, ErrorMessage = "Only positive number allowed.")]
 94        [JsonProperty("telescope")]
 95        /// <summary>
 96        /// Telescope value.
 97        /// </summary>
 98        public double Telescope
 99        {
 105100            get { return _telescope; }
 101            set
 19102            {
 19103                if (value < 0.0 || value > 18.0)
 0104                    throw new ArgumentOutOfRangeException(
 0105                          $"{nameof(value)} must be between 0 and 24.");
 106
 19107                _telescope = value;
 19108            }
 109        }
 110
 111        [DisplayName("Start height")]
 112        [Description("Set a telescopic grid at.")]
 113        [Range(0, double.MaxValue, ErrorMessage = "Only positive number allowed.")]
 114        [JsonProperty("startTelescopeHeight")]
 115        /// <summary>
 116        /// Start telescopic grid at.
 117        /// </summary>
 118        public double StartTelescopeHeight
 119        {
 48120            get { return _startTelescopeHeight; }
 121            set
 19122            {
 19123                if (value < 0.0)
 0124                    throw new ArgumentOutOfRangeException(
 0125                          $"{nameof(value)} must be positive.");
 126
 19127                _startTelescopeHeight = value;
 19128            }
 129        }
 130
 131        /// <summary>
 132        /// Convert to List of double.
 133        /// </summary>
 134        /// <returns>List of double.</returns>
 135        public List<double[]> ToList()
 0136        {
 0137            var list = new List<double[]>();
 0138            foreach (var x in Xaxis)
 0139                foreach (var y in Yaxis)
 0140                    foreach (var z in Zaxis)
 0141                    {
 0142                        list.Add(new[] { x, y, z });
 0143                    }
 144
 0145            return list;
 0146        }
 147
 148        [DisplayName("Split First Cell")]
 149        [Description("If telescopic grid split first cell.")]
 150        [JsonProperty("combineGridType")]
 151        /// <summary>
 152        /// Is telescopic grid + first cell splitted?
 153        /// </summary>
 35154        public bool CombineGridType { get; }
 155
 156        [JsonIgnore]
 157        /// <summary>
 158        /// X axis of the grid.
 159        /// </summary>
 23160        public double[] Xaxis { get; private set; }
 161
 162        [JsonIgnore]
 163        /// <summary>
 164        /// Y axis of the grid.
 165        /// </summary>
 23166        public double[] Yaxis { get; private set; }
 167
 168        [JsonIgnore]
 169        /// <summary>
 170        /// Z axis of the grid.
 171        /// </summary>
 23172        public double[] Zaxis { get; private set; }
 173
 174        [JsonIgnore]
 175        /// <summary>
 176        /// Height of cells.
 177        /// </summary>
 61178        public double[] SequenceZ { get; private set; }
 179
 180        [JsonIgnore]
 181        /// <summary>
 182        /// Is the grid splitted?
 183        /// </summary>
 22184        public bool IsSplitted { get; private set; }
 185
 186        /// <summary>
 187        /// String representation of the grid.
 188        /// </summary>
 189        /// <returns>String representation.</returns>
 190        public override string ToString()
 0191        {
 0192            return String.Format("Grid::Size {0},{1},{2}", Size.NumX, Size.NumY, Size.NumZ);
 0193        }
 194
 195        private void SetXaxis()
 19196        {
 19197            double[] sequence = new double[Size.NumX];
 3638198            for (int i = 0; i < Size.NumX; i++)
 1800199                sequence[i] = (Size.DimX * i) + Size.MinX;
 19200            Xaxis = sequence;
 19201        }
 202
 203        private void SetYaxis()
 19204        {
 19205            double[] sequence = new double[Size.NumY];
 3638206            for (int i = 0; i < Size.NumY; i++)
 1800207                sequence[i] = (Size.DimY * i) + Size.MinY;
 19208            Yaxis = sequence;
 19209        }
 210
 211        private void SetSequenceAndExtension()
 19212        {
 19213            if (CombineGridType && Telescope > 0.0)
 9214            {
 9215                SequenceZ = GetCombinedSequence(Telescope, StartTelescopeHeight);
 9216                IsSplitted = true;
 9217            }
 10218            else if (CombineGridType == false && Telescope > 0.0)
 1219            {
 1220                SequenceZ = GetTelescopeSequence(Telescope, StartTelescopeHeight);
 1221                IsSplitted = false;
 1222            }
 223            else
 9224            {
 9225                SequenceZ = GetEquidistantSequence();
 9226                IsSplitted = true;
 9227            }
 228
 19229            var accumulated = Util.Accumulate(SequenceZ)
 19230                .ToArray();
 494231            Zaxis = accumulated.Zip(SequenceZ, (a, b) => a - (b / 2))
 19232                .ToArray();
 19233        }
 234
 235        public string Serialize()
 2236        {
 2237            return JsonConvert.SerializeObject(this);
 2238        }
 239
 240        public static Grid Deserialize(string json)
 3241        {
 242            try
 3243            {
 3244                return JsonConvert.DeserializeObject<Grid>(json);
 245            }
 1246            catch (Exception e)
 1247            {
 1248                throw new Exception(e.Message);
 249            }
 2250        }
 251
 252        public bool Equals(Grid other)
 2253        {
 2254            if (other == null)
 0255                return false;
 256
 2257            if (other != null
 2258                && other.Size == this.Size
 2259                && other.Telescope == other.Telescope
 2260                && other.StartTelescopeHeight == other.StartTelescopeHeight
 2261                && other.CombineGridType == other.CombineGridType
 2262                && other.NestingGrids == other.NestingGrids)
 2263                return true;
 264            else
 0265                return false;
 2266        }
 267
 268        public override bool Equals(Object obj)
 10269        {
 10270            if (obj == null)
 0271                return false;
 272
 10273            var gridObj = obj as Grid;
 10274            if (gridObj == null)
 8275                return false;
 276            else
 2277                return Equals(gridObj);
 10278        }
 279
 280        public override int GetHashCode()
 0281        {
 282            unchecked
 0283            {
 0284                int hash = 17;
 0285                hash = hash * 23 + Size.GetHashCode();
 0286                hash = hash * 23 + Telescope.GetHashCode();
 0287                hash = hash * 23 + StartTelescopeHeight.GetHashCode();
 0288                hash = hash * 23 + CombineGridType.GetHashCode();
 0289                hash = hash * 23 + NestingGrids.GetHashCode();
 0290                return hash;
 291            }
 0292        }
 293
 294        public static bool operator ==(Grid grid1, Grid grid2)
 14295        {
 14296            if (((object)grid1) == null || ((object)grid2) == null)
 14297                return Object.Equals(grid1, grid2);
 298
 0299            return grid1.Equals(grid2);
 14300        }
 301
 302        public static bool operator !=(Grid grid1, Grid grid2)
 2303        {
 2304            return !(grid1 == grid2);
 2305        }
 306
 307        #region Sequence
 308        private double[] GetEquidistantSequence()
 9309        {
 9310            var baseCell = Size.DimZ / 5;
 9311            var cell = Size.DimZ;
 312
 9313            double[] sequence = new double[Size.NumZ];
 314
 468315            for (int k = 0; k < sequence.Length; k++)
 225316            {
 225317                if (k < 5)
 45318                    sequence[k] = baseCell;
 319                else
 180320                    sequence[k] = cell;
 225321            }
 322
 9323            return sequence;
 9324        }
 325
 326        private double[] GetTelescopeSequence(double telescope, double start)
 1327        {
 1328            var cell = Size.DimZ;
 329
 1330            double[] sequence = new double[Size.NumZ];
 331
 1332            double val = cell;
 333
 52334            for (int k = 0; k < sequence.Length; k++)
 25335            {
 25336                if (val * k < start)
 2337                {
 2338                    sequence[k] = cell;
 2339                }
 340                else
 23341                {
 23342                    sequence[k] = val + (val * telescope / 100);
 23343                    val = sequence[k];
 23344                }
 25345            }
 346
 1347            return sequence;
 1348        }
 349
 350        private double[] GetCombinedSequence(double telescope, double start)
 9351        {
 9352            var cell = Size.DimZ;
 9353            var baseCell = Size.DimZ / 5;
 9354            double val = cell;
 355
 9356            double[] firstSequence = new double[5];
 9357            double[] sequence = new double[Size.NumZ - 1];
 358
 108359            for (int k = 0; k < 5; k++)
 45360                firstSequence[k] = baseCell;
 361
 450362            for (int k = 0; k < sequence.Length; k++)
 216363            {
 216364                if (val * (k + 1) < start)
 9365                {
 9366                    sequence[k] = cell;
 9367                }
 368                else
 207369                {
 207370                    sequence[k] = val + (val * telescope / 100);
 207371                    val = sequence[k];
 207372                }
 216373            }
 374
 9375            double[] completeSequence = new double[sequence.Length + firstSequence.Length];
 376
 9377            firstSequence.CopyTo(completeSequence, 0);
 9378            sequence.CopyTo(completeSequence, firstSequence.Length);
 9379            Array.Resize(ref completeSequence, sequence.Length + 1);
 380
 9381            return completeSequence;
 9382        }
 383        #endregion
 384    }
 385}