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