| | 1 | | using System; |
| | 2 | | using System.ComponentModel; |
| | 3 | | using System.ComponentModel.DataAnnotations; |
| | 4 | | using MorphoGeometry; |
| | 5 | | using Newtonsoft.Json; |
| | 6 | |
|
| | 7 | | namespace Morpho25.Geometry |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Grid size struct. |
| | 11 | | /// </summary> |
| | 12 | | public class Size : IEquatable<Size> |
| | 13 | | { |
| | 14 | | [JsonConstructor] |
| | 15 | | /// <summary> |
| | 16 | | /// Create a new size object. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="origin">Origin of the grid.</param> |
| | 19 | | /// <param name="cellDimension">Cell dimension.</param> |
| | 20 | | /// <param name="numX">Number of X cells.</param> |
| | 21 | | /// <param name="numY">Number of Y cells.</param> |
| | 22 | | /// <param name="numZ">Number of Z cells.</param> |
| 10 | 23 | | public Size(Vector origin, |
| 10 | 24 | | CellDimension cellDimension, |
| 10 | 25 | | int numX, int numY, |
| 10 | 26 | | int numZ) |
| 10 | 27 | | { |
| 10 | 28 | | Origin = origin; |
| 10 | 29 | | NumX = numX; |
| 10 | 30 | | NumY = numY; |
| 10 | 31 | | NumZ = numZ; |
| | 32 | |
|
| 10 | 33 | | CellDimension = cellDimension; |
| | 34 | |
|
| 10 | 35 | | MinX = Origin.x; |
| 9 | 36 | | MinY = Origin.y; |
| 9 | 37 | | MaxX = Origin.x + (NumX * cellDimension.X); |
| 9 | 38 | | MaxY = Origin.y + (NumY * cellDimension.Y); |
| 9 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Default constructor. |
| | 43 | | /// </summary> |
| 2 | 44 | | public Size() |
| 2 | 45 | | { |
| 2 | 46 | | Origin = new Vector(0, 0, 0); |
| 2 | 47 | | NumX = 50; |
| 2 | 48 | | NumY = 50; |
| 2 | 49 | | NumZ = 25; |
| | 50 | |
|
| 2 | 51 | | CellDimension = new CellDimension(3.0, 3.0, 3.0); |
| | 52 | |
|
| 2 | 53 | | MinX = Origin.x; |
| 2 | 54 | | MinY = Origin.y; |
| 2 | 55 | | MaxX = Origin.x + (NumX * CellDimension.X); |
| 2 | 56 | | MaxY = Origin.y + (NumY * CellDimension.Y); |
| 2 | 57 | | } |
| | 58 | |
|
| | 59 | | [DisplayName("Cell Dimension")] |
| | 60 | | [Description("Size of the cell.")] |
| | 61 | | [JsonProperty("cellDimension")] |
| | 62 | | /// <summary> |
| | 63 | | /// Number of X cells. |
| | 64 | | /// </summary> |
| 3648 | 65 | | public CellDimension CellDimension { get; } |
| | 66 | |
|
| | 67 | | [DisplayName("Num X")] |
| | 68 | | [Description("Number of cells in X.")] |
| | 69 | | [Range(0, int.MaxValue, ErrorMessage = "Only positive number allowed.")] |
| | 70 | | [JsonProperty("numX")] |
| | 71 | | /// <summary> |
| | 72 | | /// Number of X cells. |
| | 73 | | /// </summary> |
| 1856 | 74 | | public int NumX { get; } |
| | 75 | |
|
| | 76 | | [DisplayName("Num Y")] |
| | 77 | | [Description("Number of cells in Y.")] |
| | 78 | | [Range(0, int.MaxValue, ErrorMessage = "Only positive number allowed.")] |
| | 79 | | [JsonProperty("numY")] |
| | 80 | | /// <summary> |
| | 81 | | /// Number of Y cells. |
| | 82 | | /// </summary> |
| 1856 | 83 | | public int NumY { get; } |
| | 84 | |
|
| | 85 | | [DisplayName("Num Z")] |
| | 86 | | [Description("Number of cells in Z.")] |
| | 87 | | [Range(0, int.MaxValue, ErrorMessage = "Only positive number allowed.")] |
| | 88 | | [JsonProperty("numZ")] |
| | 89 | | /// <summary> |
| | 90 | | /// Number of Z cells. |
| | 91 | | /// </summary> |
| 26 | 92 | | public int NumZ { get; } |
| | 93 | |
|
| | 94 | | [JsonIgnore] |
| | 95 | | /// <summary> |
| | 96 | | /// X of the lower left corner of the grid. |
| | 97 | | /// </summary> |
| 1800 | 98 | | public double MinX { get; } |
| | 99 | |
|
| | 100 | | [JsonIgnore] |
| | 101 | | /// <summary> |
| | 102 | | /// Y of the lower left corner of the grid. |
| | 103 | | /// </summary> |
| 1800 | 104 | | public double MinY { get; } |
| | 105 | |
|
| | 106 | | [JsonIgnore] |
| | 107 | | /// <summary> |
| | 108 | | /// X of the upper right corner of the grid. |
| | 109 | | /// </summary> |
| 0 | 110 | | public double MaxX { get; } |
| | 111 | |
|
| | 112 | | [JsonIgnore] |
| | 113 | | /// <summary> |
| | 114 | | /// Y of the upper right corner of the grid. |
| | 115 | | /// </summary> |
| 0 | 116 | | public double MaxY { get; } |
| | 117 | |
|
| | 118 | | [JsonIgnore] |
| | 119 | | /// <summary> |
| | 120 | | /// X dimension of the cell. |
| | 121 | | /// </summary> |
| 1800 | 122 | | public double DimX => CellDimension.X; |
| | 123 | |
|
| | 124 | | [JsonIgnore] |
| | 125 | | /// <summary> |
| | 126 | | /// Y dimension of the cell. |
| | 127 | | /// </summary> |
| 1800 | 128 | | public double DimY => CellDimension.Y; |
| | 129 | |
|
| | 130 | | [JsonIgnore] |
| | 131 | | /// <summary> |
| | 132 | | /// Z dimension of the cell. |
| | 133 | | /// </summary> |
| 37 | 134 | | public double DimZ => CellDimension.Z; |
| | 135 | |
|
| | 136 | | [DisplayName("Origin")] |
| | 137 | | [Description("Origin of the grid.")] |
| | 138 | | [JsonProperty("origin")] |
| | 139 | | /// <summary> |
| | 140 | | /// Origin of the grid. Lower left corner. |
| | 141 | | /// </summary> |
| 52 | 142 | | public Vector Origin { get; } |
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// String representation of the Grid Size. |
| | 146 | | /// </summary> |
| | 147 | | /// <returns>String representation.</returns> |
| | 148 | | public override string ToString() |
| 0 | 149 | | { |
| 0 | 150 | | return String.Format("Size::{0},{1},{2}::{3},{4},{5}", |
| 0 | 151 | | NumX, NumY, NumZ, DimX, DimY, DimZ); |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | public string Serialize() |
| 1 | 155 | | { |
| 1 | 156 | | return JsonConvert.SerializeObject(this); |
| 1 | 157 | | } |
| | 158 | |
|
| | 159 | | public static Size Deserialize(string json) |
| 0 | 160 | | { |
| | 161 | | try |
| 0 | 162 | | { |
| 0 | 163 | | return JsonConvert.DeserializeObject<Size>(json); |
| | 164 | | } |
| 0 | 165 | | catch (Exception e) |
| 0 | 166 | | { |
| 0 | 167 | | throw new Exception(e.Message); |
| | 168 | | } |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | public bool Equals(Size other) |
| 2 | 172 | | { |
| 2 | 173 | | if (other == null) |
| 0 | 174 | | return false; |
| | 175 | |
|
| 2 | 176 | | if (other != null |
| 2 | 177 | | && other.Origin == this.Origin |
| 2 | 178 | | && other.CellDimension == this.CellDimension |
| 2 | 179 | | && other.NumX == this.NumX |
| 2 | 180 | | && other.NumY == this.NumY |
| 2 | 181 | | && other.NumZ == this.NumZ) |
| 2 | 182 | | return true; |
| | 183 | | else |
| 0 | 184 | | return false; |
| 2 | 185 | | } |
| | 186 | |
|
| | 187 | | public override bool Equals(Object obj) |
| 6 | 188 | | { |
| 6 | 189 | | if (obj == null) |
| 0 | 190 | | return false; |
| | 191 | |
|
| 6 | 192 | | var sizeObj = obj as Size; |
| 6 | 193 | | if (sizeObj == null) |
| 6 | 194 | | return false; |
| | 195 | | else |
| 0 | 196 | | return Equals(sizeObj); |
| 6 | 197 | | } |
| | 198 | |
|
| | 199 | | public override int GetHashCode() |
| 0 | 200 | | { |
| | 201 | | unchecked |
| 0 | 202 | | { |
| 0 | 203 | | int hash = 17; |
| 0 | 204 | | hash = hash * 23 + NumX.GetHashCode(); |
| 0 | 205 | | hash = hash * 23 + NumY.GetHashCode(); |
| 0 | 206 | | hash = hash * 23 + NumZ.GetHashCode(); |
| 0 | 207 | | hash = hash * 23 + CellDimension.GetHashCode(); |
| 0 | 208 | | hash = hash * 23 + Origin.GetHashCode(); |
| 0 | 209 | | return hash; |
| | 210 | | } |
| 0 | 211 | | } |
| | 212 | |
|
| | 213 | | public static bool operator ==(Size size1, Size size2) |
| 12 | 214 | | { |
| 12 | 215 | | if (((object)size1) == null || ((object)size2) == null) |
| 10 | 216 | | return Object.Equals(size1, size2); |
| | 217 | |
|
| 2 | 218 | | return size1.Equals(size2); |
| 12 | 219 | | } |
| | 220 | |
|
| | 221 | | public static bool operator !=(Size size1, Size size2) |
| 2 | 222 | | { |
| 2 | 223 | | return !(size1 == size2); |
| 2 | 224 | | } |
| | 225 | | } |
| | 226 | | } |