| | 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("Receptor")] |
| | 10 | | /// <summary> |
| | 11 | | /// Receptor class. |
| | 12 | | /// </summary> |
| | 13 | | public class Receptor : Entity, IEquatable<Receptor> |
| | 14 | | { |
| | 15 | | [DisplayName("Name")] |
| | 16 | | [Description("Name of the receptor")] |
| | 17 | | [JsonProperty("name", Required = Required.Always)] |
| | 18 | | /// <summary> |
| | 19 | | /// Name of the receptor. |
| | 20 | | /// </summary> |
| 3 | 21 | | public override string Name { get; } |
| | 22 | |
|
| | 23 | | [DisplayName("Geometry")] |
| | 24 | | [Description("Point geometry")] |
| | 25 | | [JsonProperty("geometry", Required = Required.Always)] |
| | 26 | | /// <summary> |
| | 27 | | /// Geometry of the receptor. |
| | 28 | | /// </summary> |
| 6 | 29 | | public Vector Geometry { get; set; } |
| | 30 | |
|
| | 31 | | [JsonIgnore] |
| | 32 | | /// <summary> |
| | 33 | | /// Location of the receptor in the grid. |
| | 34 | | /// </summary> |
| 0 | 35 | | public Pixel Pixel { get; private set; } |
| | 36 | |
|
| | 37 | | [JsonIgnore] |
| | 38 | | /// <summary> |
| | 39 | | /// Material of the receptor. |
| | 40 | | /// </summary> |
| | 41 | | public override Material Material |
| | 42 | | { |
| 0 | 43 | | get => throw new NotImplementedException(); |
| 0 | 44 | | protected set => throw new NotImplementedException(); |
| | 45 | | } |
| | 46 | | /// <summary> |
| | 47 | | /// Create a new receptor. |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="geometry">Geometry of the receptor.</param> |
| | 50 | | /// <param name="name">Name of the receptor.</param> |
| 3 | 51 | | public Receptor(Vector geometry, string name) |
| 3 | 52 | | { |
| 3 | 53 | | Geometry = geometry; |
| 3 | 54 | | Name = name; |
| 3 | 55 | | } |
| | 56 | |
|
| | 57 | | public void SetPixel(Grid grid) |
| 0 | 58 | | { |
| 0 | 59 | | Pixel = new Pixel |
| 0 | 60 | | { |
| 0 | 61 | | I = Util.ClosestValue(grid.Xaxis, Geometry.x), |
| 0 | 62 | | J = Util.ClosestValue(grid.Yaxis, Geometry.y), |
| 0 | 63 | | K = 0 |
| 0 | 64 | | }; |
| 0 | 65 | | } |
| | 66 | | /// <summary> |
| | 67 | | /// String representation of the receptor. |
| | 68 | | /// </summary> |
| | 69 | | /// <returns>String representation.</returns> |
| | 70 | | public override string ToString() |
| 0 | 71 | | { |
| 0 | 72 | | return String.Format("Receptor::{0}", Name); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public string Serialize() |
| 1 | 76 | | { |
| 1 | 77 | | return JsonConvert.SerializeObject(this); |
| 1 | 78 | | } |
| | 79 | |
|
| | 80 | | public static Receptor Deserialize(string json) |
| 1 | 81 | | { |
| | 82 | | try |
| 1 | 83 | | { |
| 1 | 84 | | return JsonConvert.DeserializeObject<Receptor>(json); |
| | 85 | | } |
| 0 | 86 | | catch (Exception e) |
| 0 | 87 | | { |
| 0 | 88 | | throw new Exception(e.Message); |
| | 89 | | } |
| 1 | 90 | | } |
| | 91 | |
|
| | 92 | | public bool Equals(Receptor other) |
| 1 | 93 | | { |
| 1 | 94 | | if (other == null) |
| 0 | 95 | | return false; |
| | 96 | |
|
| 1 | 97 | | if (other != null |
| 1 | 98 | | && other.Name == this.Name |
| 1 | 99 | | && other.Geometry == this.Geometry) |
| 1 | 100 | | return true; |
| | 101 | | else |
| 0 | 102 | | return false; |
| 1 | 103 | | } |
| | 104 | |
|
| | 105 | | public override bool Equals(Object obj) |
| 1 | 106 | | { |
| 1 | 107 | | if (obj == null) |
| 0 | 108 | | return false; |
| | 109 | |
|
| 1 | 110 | | var receptorObj = obj as Receptor; |
| 1 | 111 | | if (receptorObj == null) |
| 1 | 112 | | return false; |
| | 113 | | else |
| 0 | 114 | | return Equals(receptorObj); |
| 1 | 115 | | } |
| | 116 | |
|
| | 117 | | public override int GetHashCode() |
| 0 | 118 | | { |
| | 119 | | unchecked |
| 0 | 120 | | { |
| 0 | 121 | | int hash = 17; |
| 0 | 122 | | hash = hash * 23 + Name.GetHashCode(); |
| 0 | 123 | | hash = hash * 23 + Geometry.GetHashCode(); |
| 0 | 124 | | return hash; |
| | 125 | | } |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | public static bool operator ==(Receptor receptor1, Receptor receptor2) |
| 3 | 129 | | { |
| 3 | 130 | | if (((object)receptor1) == null || ((object)receptor2) == null) |
| 3 | 131 | | return Object.Equals(receptor1, receptor2); |
| | 132 | |
|
| 0 | 133 | | return receptor1.Equals(receptor2); |
| 3 | 134 | | } |
| | 135 | |
|
| | 136 | | public static bool operator !=(Receptor receptor1, Receptor receptor2) |
| 1 | 137 | | { |
| 1 | 138 | | return !(receptor1 == receptor2); |
| 1 | 139 | | } |
| | 140 | | } |
| | 141 | | } |