| | | 1 | | using Newtonsoft.Json; |
| | | 2 | | using System; |
| | | 3 | | using System.ComponentModel.DataAnnotations; |
| | | 4 | | |
| | | 5 | | namespace Morpho25.Geometry |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Cell dimension struct. |
| | | 9 | | /// </summary> |
| | | 10 | | public class CellDimension : IEquatable<CellDimension> |
| | | 11 | | { |
| | | 12 | | [JsonConstructor] |
| | | 13 | | /// <summary> |
| | | 14 | | /// Create a new cell. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="x">X coordinate.</param> |
| | | 17 | | /// <param name="y">Y coordinate.</param> |
| | | 18 | | /// <param name="z">Z coordinate.</param> |
| | 12 | 19 | | public CellDimension(double x, double y, double z) |
| | 12 | 20 | | { |
| | 12 | 21 | | X = x; |
| | 12 | 22 | | Y = y; |
| | 12 | 23 | | Z = z; |
| | 12 | 24 | | } |
| | | 25 | | |
| | | 26 | | [Range(0.1, double.MaxValue, ErrorMessage = "Only positive number allowed.")] |
| | | 27 | | [JsonProperty("x")] |
| | | 28 | | /// <summary> |
| | | 29 | | /// X coordinate. |
| | | 30 | | /// </summary> |
| | 1818 | 31 | | public double X { get; } |
| | | 32 | | |
| | | 33 | | [Range(0.1, double.MaxValue, ErrorMessage = "Only positive number allowed.")] |
| | | 34 | | [JsonProperty("y")] |
| | | 35 | | /// <summary> |
| | | 36 | | /// Y coordinate. |
| | | 37 | | /// </summary> |
| | 1818 | 38 | | public double Y { get; } |
| | | 39 | | |
| | | 40 | | [Range(0.1, double.MaxValue, ErrorMessage = "Only positive number allowed.")] |
| | | 41 | | [JsonProperty("z")] |
| | | 42 | | /// <summary> |
| | | 43 | | /// Z coordinate. |
| | | 44 | | /// </summary> |
| | 44 | 45 | | public double Z { get; } |
| | | 46 | | |
| | | 47 | | public string Serialize() |
| | 0 | 48 | | { |
| | 0 | 49 | | return JsonConvert.SerializeObject(this); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | public static CellDimension Deserialize(string json) |
| | 0 | 53 | | { |
| | | 54 | | try |
| | 0 | 55 | | { |
| | 0 | 56 | | return JsonConvert.DeserializeObject<CellDimension>(json); |
| | | 57 | | } |
| | 0 | 58 | | catch (Exception e) |
| | 0 | 59 | | { |
| | 0 | 60 | | throw new Exception(e.Message); |
| | | 61 | | } |
| | 0 | 62 | | } |
| | | 63 | | public bool Equals(CellDimension other) |
| | 2 | 64 | | { |
| | 2 | 65 | | if (other == null) |
| | 0 | 66 | | return false; |
| | | 67 | | |
| | 2 | 68 | | if (other != null |
| | 2 | 69 | | && other.X == this.X |
| | 2 | 70 | | && other.Y == this.Y |
| | 2 | 71 | | && other.Z == this.Z) |
| | 2 | 72 | | return true; |
| | | 73 | | else |
| | 0 | 74 | | return false; |
| | 2 | 75 | | } |
| | | 76 | | |
| | | 77 | | public override bool Equals(Object obj) |
| | 0 | 78 | | { |
| | 0 | 79 | | if (obj == null) |
| | 0 | 80 | | return false; |
| | | 81 | | |
| | 0 | 82 | | var dimObj = obj as CellDimension; |
| | 0 | 83 | | if (dimObj == null) |
| | 0 | 84 | | return false; |
| | | 85 | | else |
| | 0 | 86 | | return Equals(dimObj); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | public override int GetHashCode() |
| | 0 | 90 | | { |
| | | 91 | | unchecked |
| | 0 | 92 | | { |
| | 0 | 93 | | int hash = 17; |
| | 0 | 94 | | hash = hash * 23 + X.GetHashCode(); |
| | 0 | 95 | | hash = hash * 23 + Y.GetHashCode(); |
| | 0 | 96 | | hash = hash * 23 + Z.GetHashCode(); |
| | 0 | 97 | | return hash; |
| | | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | public static bool operator ==(CellDimension dim1, CellDimension dim2) |
| | 6 | 102 | | { |
| | 6 | 103 | | if (((object)dim1) == null || ((object)dim2) == null) |
| | 4 | 104 | | return Object.Equals(dim1, dim2); |
| | | 105 | | |
| | 2 | 106 | | return dim1.Equals(dim2); |
| | 6 | 107 | | } |
| | | 108 | | |
| | | 109 | | public static bool operator !=(CellDimension dim1, CellDimension dim2) |
| | 2 | 110 | | { |
| | 2 | 111 | | return !(dim1 == dim2); |
| | 2 | 112 | | } |
| | | 113 | | } |
| | | 114 | | } |