< Summary

Information
Class: Morpho25.Geometry.Terrain
Assembly: Morpho25
File(s): D:\a\Morpho\Morpho\project\Morpho\Morpho25\Geometry\Terrain.cs
Line coverage
43%
Covered lines: 41
Uncovered lines: 54
Coverable lines: 95
Total lines: 197
Line coverage: 43.1%
Branch coverage
42%
Covered branches: 12
Total branches: 28
Branch coverage: 42.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Name()100%1100%
get_Geometry()100%1100%
get_IDmatrix()100%10%
get_TerrainIDrows()100%1100%
get_Pixels()100%1100%
get_Material()100%10%
set_Material(...)100%10%
.ctor(...)100%2100%
SetMatrix(...)100%10%
CalculatePixels(...)0%60%
GetTerrainRows()0%20%
ToString()100%10%
Serialize()100%1100%
Deserialize(...)100%157.14%
Equals(...)50%1080%
Equals(...)50%475%
GetHashCode()100%10%
op_Equality(...)75%480%
op_Inequality(...)100%1100%

File(s)

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

#LineLine coverage
 1using Morpho25.Utility;
 2using MorphoGeometry;
 3using Newtonsoft.Json;
 4using System;
 5using System.Collections.Generic;
 6using System.ComponentModel;
 7using System.Linq;
 8
 9namespace Morpho25.Geometry
 10{
 11    [DisplayName("Terrain")]
 12    /// <summary>
 13    /// Terrain class.
 14    /// </summary>
 15    public class Terrain : Entity, IEquatable<Terrain>
 16    {
 17        [DisplayName("Name")]
 18        [Description("Name of the terrain group")]
 19        [JsonProperty("name")]
 20        /// <summary>
 21        /// Name of the terrain.
 22        /// </summary>
 323        public override string Name { get; }
 24
 25        [DisplayName("Geometry")]
 26        [Description("Flat or solid geometry")]
 27        [JsonProperty("geometry", Required = Required.Always)]
 28        /// <summary>
 29        /// Geometry of the terrain.
 30        /// </summary>
 631        public FaceGroup Geometry { get; set; }
 32
 33        [JsonIgnore]
 34        /// <summary>
 35        /// Matrix 2D of the terrain.
 36        /// </summary>
 037        public Matrix2d IDmatrix { get; private set; }
 38
 39        [JsonIgnore]
 40        /// <summary>
 41        /// Collection of string based on Pixel and ID.
 42        /// </summary>
 343        public List<string> TerrainIDrows { get; private set; }
 44
 45        [JsonIgnore]
 46        /// <summary>
 47        /// Location of the terrain in the grid.
 48        /// </summary>
 349        public List<Pixel> Pixels { get; private set; }
 50
 51        [JsonIgnore]
 52        /// <summary>
 53        /// Material of the terrain.
 54        /// </summary>
 55        public override Material Material
 56        {
 057            get => throw new NotImplementedException();
 058            protected set => throw new NotImplementedException();
 59        }
 60        /// <summary>
 61        /// Create a new terrain.
 62        /// </summary>
 63        /// <param name="geometry">Geometry of the terrain.</param>
 64        /// <param name="id">Numerical ID.</param>
 65        /// <param name="name">Name of the terrain.</param>
 366        public Terrain(FaceGroup geometry,
 367            int id, string name = null)
 368        {
 369            ID = id;
 370            Geometry = geometry;
 371            Name = name ?? "TerrainGroup";
 372            TerrainIDrows = new List<string>();
 373            Pixels = new List<Pixel>();
 374        }
 75
 76        public void SetMatrix(Grid grid)
 077        {
 078            Matrix2d matrix = new Matrix2d(grid.Size.NumX, grid.Size.NumY, "0");
 79
 080            List<Ray> rays = EnvimetUtility.GetRayFromFacegroupBbox(grid, Geometry);
 81
 082            var intersection = EnvimetUtility.Raycasting2D(rays, Geometry, true, false);
 83            // 2D Part
 084            SetMatrix(intersection, grid, matrix, "");
 85            // 3D Part
 086            CalculatePixels(grid, intersection);
 87
 088            IDmatrix = matrix;
 089        }
 90
 91        private void CalculatePixels(Grid grid, IEnumerable<Vector> intersection)
 092        {
 093            var voxels = new List<Vector>();
 094            foreach (var pt in intersection)
 095            {
 096                var h = (pt.z < 0) ? 0 : pt.z;
 097                var zList = Util.FilterByMinMax(grid.Zaxis, h, 0);
 98
 099                foreach(var v in zList)
 0100                {
 0101                    voxels.Add(new Vector(pt.x, pt.y, Convert.ToSingle(v)));
 0102                }
 0103            }
 0104            Pixels = voxels
 0105                .Select(_ => _.ToPixel(grid))
 0106                .ToList();
 107
 0108            TerrainIDrows = GetTerrainRows()
 0109                .ToList();
 0110        }
 111
 112        private IEnumerable<string> GetTerrainRows()
 0113        {
 0114            foreach (var px in Pixels)
 0115            {
 0116                yield return String.Format("{0},{1},{2},{3}",
 0117                    px.I, px.J, px.K, "1.00000");
 0118            }
 0119        }
 120        /// <summary>
 121        /// String representation of the terrain.
 122        /// </summary>
 123        /// <returns>String representation.</returns>
 124        public override string ToString()
 0125        {
 0126            return String.Format("Terrain::{0}::{1}", Name, ID);
 0127        }
 128
 129        public string Serialize()
 1130        {
 1131            return JsonConvert.SerializeObject(this);
 1132        }
 133
 134        public static Terrain Deserialize(string json)
 1135        {
 136            try
 1137            {
 1138                return JsonConvert.DeserializeObject<Terrain>(json);
 139            }
 0140            catch (Exception e)
 0141            {
 0142                throw new Exception(e.Message);
 143            }
 1144        }
 145
 146        public bool Equals(Terrain other)
 1147        {
 1148            if (other == null)
 0149                return false;
 150
 1151            if (other != null
 1152                && other.ID == this.ID
 1153                && other.Name == this.Name
 1154                && other.Geometry == this.Geometry)
 1155                return true;
 156            else
 0157                return false;
 1158        }
 159
 160        public override bool Equals(Object obj)
 12161        {
 12162            if (obj == null)
 0163                return false;
 164
 12165            var terrainObj = obj as Terrain;
 12166            if (terrainObj == null)
 12167                return false;
 168            else
 0169                return Equals(terrainObj);
 12170        }
 171
 172        public override int GetHashCode()
 0173        {
 174            unchecked
 0175            {
 0176                int hash = 17;
 0177                hash = hash * 23 + ID.GetHashCode();
 0178                hash = hash * 23 + Name.GetHashCode();
 0179                hash = hash * 23 + Geometry.GetHashCode();
 0180                return hash;
 181            }
 0182        }
 183
 184        public static bool operator ==(Terrain terrain1, Terrain terrain2)
 14185        {
 14186            if (((object)terrain1) == null || ((object)terrain2) == null)
 14187                return Object.Equals(terrain1, terrain2);
 188
 0189            return terrain1.Equals(terrain2);
 14190        }
 191
 192        public static bool operator !=(Terrain terrain1, Terrain terrain2)
 1193        {
 1194            return !(terrain1 == terrain2);
 1195        }
 196    }
 197}