| | 1 | | using Morpho25.Utility; |
| | 2 | | using MorphoGeometry; |
| | 3 | | using Newtonsoft.Json; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.ComponentModel; |
| | 7 | |
|
| | 8 | | namespace Morpho25.Geometry |
| | 9 | | { |
| | 10 | | [DisplayName("Soil")] |
| | 11 | | /// <summary> |
| | 12 | | /// Soil class. |
| | 13 | | /// </summary> |
| | 14 | | public class Soil : Entity, IEquatable<Soil> |
| | 15 | | { |
| | 16 | | [DisplayName("Name")] |
| | 17 | | [Description("Name of the soil group")] |
| | 18 | | [JsonProperty("name")] |
| | 19 | | /// <summary> |
| | 20 | | /// Name of the soil. |
| | 21 | | /// </summary> |
| 1 | 22 | | public override string Name { get; } |
| | 23 | |
|
| | 24 | | [DisplayName("Geometry")] |
| | 25 | | [Description("Flat or solid geometry")] |
| | 26 | | [JsonProperty("geometry", Required = Required.Always)] |
| | 27 | | /// <summary> |
| | 28 | | /// Geometry of the soil. |
| | 29 | | /// </summary> |
| 6 | 30 | | public FaceGroup Geometry { get; set; } |
| | 31 | |
|
| | 32 | | [JsonIgnore] |
| | 33 | | /// <summary> |
| | 34 | | /// Matrix 2D of the soil. |
| | 35 | | /// </summary> |
| 0 | 36 | | public Matrix2d IDmatrix { get; private set; } |
| | 37 | |
|
| | 38 | | [DisplayName("Material")] |
| | 39 | | [Description("Profile of the soil")] |
| | 40 | | [JsonProperty("material")] |
| | 41 | | /// <summary> |
| | 42 | | /// Material of the soil. |
| | 43 | | /// </summary> |
| | 44 | | public override Material Material |
| | 45 | | { |
| 9 | 46 | | get { return _material; } |
| | 47 | | protected set |
| 4 | 48 | | { |
| 4 | 49 | | if (value.IDs.Length != 1) |
| 0 | 50 | | throw new ArgumentOutOfRangeException( |
| 0 | 51 | | $"{nameof(value)} must contains 1 material code."); |
| | 52 | |
|
| 4 | 53 | | _material = value; |
| 4 | 54 | | } |
| | 55 | | } |
| | 56 | |
|
| | 57 | | [JsonConstructor] |
| | 58 | | /// <summary> |
| | 59 | | /// Create a new soil. |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="geometry">Geometry of the soil.</param> |
| | 62 | | /// <param name="id">Numerical ID.</param> |
| | 63 | | /// <param name="code">Code of the material.</param> |
| | 64 | | /// <param name="name">Name of the soil.</param> |
| 3 | 65 | | public Soil(FaceGroup geometry, |
| 3 | 66 | | int id, string code = null, |
| 3 | 67 | | string name = null) |
| 3 | 68 | | { |
| 3 | 69 | | ID = id; |
| 3 | 70 | | Geometry = geometry; |
| 3 | 71 | | Material = (code != null) |
| 3 | 72 | | ? CreateMaterial(Material.DEFAULT_SOIL, code) |
| 3 | 73 | | : CreateMaterial(Material.DEFAULT_SOIL); |
| 3 | 74 | | Name = name ?? "SoilGroup"; |
| 3 | 75 | | } |
| | 76 | |
|
| | 77 | | public void SetMatrix(Grid grid) |
| 0 | 78 | | { |
| 0 | 79 | | Matrix2d matrix = new Matrix2d(grid.Size.NumX, grid.Size.NumY, Material.DEFAULT_SOIL); |
| | 80 | |
|
| 0 | 81 | | List<Ray> rays = EnvimetUtility.GetRayFromFacegroupBbox(grid, Geometry); |
| | 82 | |
|
| 0 | 83 | | var intersection = EnvimetUtility.Raycasting2D(rays, Geometry, false, false); |
| 0 | 84 | | SetMatrix(intersection, grid, matrix, Material.IDs[0]); |
| | 85 | |
|
| 0 | 86 | | IDmatrix = matrix; |
| 0 | 87 | | } |
| | 88 | | /// <summary> |
| | 89 | | /// String representation of the soil. |
| | 90 | | /// </summary> |
| | 91 | | /// <returns>String representation.</returns> |
| | 92 | | public override string ToString() |
| 0 | 93 | | { |
| 0 | 94 | | return String.Format("Soil::{0}::{1}::{2}", |
| 0 | 95 | | Name, ID, Material.IDs[0]); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | public string Serialize() |
| 1 | 99 | | { |
| 1 | 100 | | return JsonConvert.SerializeObject(this); |
| 1 | 101 | | } |
| | 102 | |
|
| | 103 | | public static Soil Deserialize(string json) |
| 1 | 104 | | { |
| | 105 | | try |
| 1 | 106 | | { |
| 1 | 107 | | return JsonConvert.DeserializeObject<Soil>(json); |
| | 108 | | } |
| 0 | 109 | | catch (Exception e) |
| 0 | 110 | | { |
| 0 | 111 | | throw new Exception(e.Message); |
| | 112 | | } |
| 1 | 113 | | } |
| | 114 | |
|
| | 115 | | public bool Equals(Soil other) |
| 1 | 116 | | { |
| 1 | 117 | | if (other == null) |
| 0 | 118 | | return false; |
| | 119 | |
|
| 1 | 120 | | if (other != null |
| 1 | 121 | | && other.ID == this.ID |
| 1 | 122 | | && other.Material == this.Material |
| 1 | 123 | | && other.Geometry == this.Geometry) |
| 1 | 124 | | return true; |
| | 125 | | else |
| 0 | 126 | | return false; |
| 1 | 127 | | } |
| | 128 | |
|
| | 129 | | public override bool Equals(Object obj) |
| 14 | 130 | | { |
| 14 | 131 | | if (obj == null) |
| 0 | 132 | | return false; |
| | 133 | |
|
| 14 | 134 | | var soilObj = obj as Soil; |
| 14 | 135 | | if (soilObj == null) |
| 14 | 136 | | return false; |
| | 137 | | else |
| 0 | 138 | | return Equals(soilObj); |
| 14 | 139 | | } |
| | 140 | |
|
| | 141 | | public override int GetHashCode() |
| 0 | 142 | | { |
| | 143 | | unchecked |
| 0 | 144 | | { |
| 0 | 145 | | int hash = 17; |
| 0 | 146 | | hash = hash * 23 + ID.GetHashCode(); |
| 0 | 147 | | hash = hash * 23 + Material.GetHashCode(); |
| 0 | 148 | | hash = hash * 23 + Geometry.GetHashCode(); |
| 0 | 149 | | return hash; |
| | 150 | | } |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | public static bool operator ==(Soil soil1, Soil soil2) |
| 16 | 154 | | { |
| 16 | 155 | | if (((object)soil1) == null || ((object)soil2) == null) |
| 16 | 156 | | return Object.Equals(soil1, soil2); |
| | 157 | |
|
| 0 | 158 | | return soil1.Equals(soil2); |
| 16 | 159 | | } |
| | 160 | |
|
| | 161 | | public static bool operator !=(Soil soil1, Soil soil2) |
| 1 | 162 | | { |
| 1 | 163 | | return !(soil1 == soil2); |
| 1 | 164 | | } |
| | 165 | | } |
| | 166 | | } |