| | 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("Simple Plant")] |
| | 11 | | /// <summary> |
| | 12 | | /// Plant 2D class. |
| | 13 | | /// </summary> |
| | 14 | | public class Plant2d : Entity, IEquatable<Plant2d> |
| | 15 | | { |
| | 16 | | [DisplayName("Name")] |
| | 17 | | [Description("Name of the simple plant group")] |
| | 18 | | [JsonProperty("name")] |
| | 19 | | /// <summary> |
| | 20 | | /// Name of the plant 2D. |
| | 21 | | /// </summary> |
| 3 | 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 plant 2D. |
| | 29 | | /// </summary> |
| 6 | 30 | | public FaceGroup Geometry { get; set; } |
| | 31 | |
|
| | 32 | | [JsonIgnore] |
| | 33 | | /// <summary> |
| | 34 | | /// Matrix 2D of the plant 2D. |
| | 35 | | /// </summary> |
| 0 | 36 | | public Matrix2d IDmatrix { get; private set; } |
| | 37 | |
|
| | 38 | | [DisplayName("Material")] |
| | 39 | | [Description("Simple plant type")] |
| | 40 | | [JsonProperty("material")] |
| | 41 | | /// <summary> |
| | 42 | | /// Material of the plant 2D. |
| | 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 | |
|
| | 58 | | [JsonConstructor] |
| | 59 | | /// <summary> |
| | 60 | | /// Create a new plant 2D. |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="geometry">Geometry of the plant 2D.</param> |
| | 63 | | /// <param name="id">Numerical ID.</param> |
| | 64 | | /// <param name="code">Code of the material.</param> |
| | 65 | | /// <param name="name">Name of the plant 2D.</param> |
| 3 | 66 | | public Plant2d(FaceGroup geometry, |
| 3 | 67 | | int id, string code = null, string name = null) |
| 3 | 68 | | { |
| 3 | 69 | | ID = id; |
| 3 | 70 | | Geometry = geometry; |
| 3 | 71 | | Material = (code != null) |
| 3 | 72 | | ? CreateMaterial(Material.DEFAULT_PLANT_2D, code) |
| 3 | 73 | | : CreateMaterial(Material.DEFAULT_PLANT_2D); |
| 3 | 74 | | Name = name ?? "PlantGroup"; |
| 3 | 75 | | } |
| | 76 | |
|
| | 77 | | public void SetMatrix(Grid grid) |
| 0 | 78 | | { |
| 0 | 79 | | Matrix2d matrix = new Matrix2d(grid.Size.NumX, grid.Size.NumY, ""); |
| | 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 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// String representation of the plant 2D. |
| | 91 | | /// </summary> |
| | 92 | | /// <returns>String representation.</returns> |
| | 93 | | public override string ToString() |
| 0 | 94 | | { |
| 0 | 95 | | return String.Format("Plant2D::{0}::{1}::{2}", |
| 0 | 96 | | Name, ID, Material.IDs[0]); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | public string Serialize() |
| 1 | 100 | | { |
| 1 | 101 | | return JsonConvert.SerializeObject(this); |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | public static Plant2d Deserialize(string json) |
| 1 | 105 | | { |
| | 106 | | try |
| 1 | 107 | | { |
| 1 | 108 | | return JsonConvert.DeserializeObject<Plant2d>(json); |
| | 109 | | } |
| 0 | 110 | | catch (Exception e) |
| 0 | 111 | | { |
| 0 | 112 | | throw new Exception(e.Message); |
| | 113 | | } |
| 1 | 114 | | } |
| | 115 | |
|
| | 116 | | public bool Equals(Plant2d other) |
| 1 | 117 | | { |
| 1 | 118 | | if (other == null) |
| 0 | 119 | | return false; |
| | 120 | |
|
| 1 | 121 | | if (other != null |
| 1 | 122 | | && other.ID == this.ID |
| 1 | 123 | | && other.Name == this.Name |
| 1 | 124 | | && other.Material == this.Material |
| 1 | 125 | | && other.Geometry == this.Geometry) |
| 1 | 126 | | return true; |
| | 127 | | else |
| 0 | 128 | | return false; |
| 1 | 129 | | } |
| | 130 | |
|
| | 131 | | public override bool Equals(Object obj) |
| 14 | 132 | | { |
| 14 | 133 | | if (obj == null) |
| 0 | 134 | | return false; |
| | 135 | |
|
| 14 | 136 | | var plantObj = obj as Plant2d; |
| 14 | 137 | | if (plantObj == null) |
| 14 | 138 | | return false; |
| | 139 | | else |
| 0 | 140 | | return Equals(plantObj); |
| 14 | 141 | | } |
| | 142 | |
|
| | 143 | | public override int GetHashCode() |
| 0 | 144 | | { |
| | 145 | | unchecked |
| 0 | 146 | | { |
| 0 | 147 | | int hash = 17; |
| 0 | 148 | | hash = hash * 23 + ID.GetHashCode(); |
| 0 | 149 | | hash = hash * 23 + Name.GetHashCode(); |
| 0 | 150 | | hash = hash * 23 + Material.GetHashCode(); |
| 0 | 151 | | hash = hash * 23 + Geometry.GetHashCode(); |
| 0 | 152 | | return hash; |
| | 153 | | } |
| 0 | 154 | | } |
| | 155 | |
|
| | 156 | | public static bool operator ==(Plant2d plant1, Plant2d plant2) |
| 16 | 157 | | { |
| 16 | 158 | | if (((object)plant1) == null || ((object)plant2) == null) |
| 16 | 159 | | return Object.Equals(plant1, plant2); |
| | 160 | |
|
| 0 | 161 | | return plant1.Equals(plant2); |
| 16 | 162 | | } |
| | 163 | |
|
| | 164 | | public static bool operator !=(Plant2d plant1, Plant2d plant2) |
| 1 | 165 | | { |
| 1 | 166 | | return !(plant1 == plant2); |
| 1 | 167 | | } |
| | 168 | | } |
| | 169 | | } |