| | 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("Source")] |
| | 11 | | /// <summary> |
| | 12 | | /// Source class. |
| | 13 | | /// </summary> |
| | 14 | | public class Source : Entity, IEquatable<Source> |
| | 15 | | { |
| | 16 | | [DisplayName("Name")] |
| | 17 | | [Description("Name of the source group")] |
| | 18 | | [JsonProperty("name")] |
| | 19 | | /// <summary> |
| | 20 | | /// Name of the source. |
| | 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 source. |
| | 29 | | /// </summary> |
| 6 | 30 | | public FaceGroup Geometry { get; set; } |
| | 31 | |
|
| | 32 | | [JsonIgnore] |
| | 33 | | /// <summary> |
| | 34 | | /// Matrix of the source. |
| | 35 | | /// </summary> |
| 0 | 36 | | public Matrix2d IDmatrix { get; private set; } |
| | 37 | |
|
| | 38 | | [DisplayName("Material")] |
| | 39 | | [Description("Source type")] |
| | 40 | | [JsonProperty("material")] |
| | 41 | | /// <summary> |
| | 42 | | /// Material of the source. |
| | 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 source. |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="geometry">Geometry of the source.</param> |
| | 63 | | /// <param name="id">Numerical ID.</param> |
| | 64 | | /// <param name="code">Code of the material.</param> |
| | 65 | | /// <param name="name">Name of the source.</param> |
| 3 | 66 | | public Source(FaceGroup geometry, |
| 3 | 67 | | int id, string code = null, |
| 3 | 68 | | string name = null) |
| 3 | 69 | | { |
| 3 | 70 | | ID = id; |
| 3 | 71 | | Geometry = geometry; |
| 3 | 72 | | Material = (code != null) |
| 3 | 73 | | ? CreateMaterial(Material.DEFAULT_SOURCE, code) |
| 3 | 74 | | : CreateMaterial(Material.DEFAULT_SOURCE); |
| 3 | 75 | | Name = name ?? "SourceGroup"; |
| 3 | 76 | | } |
| | 77 | |
|
| | 78 | | public void SetMatrix(Grid grid) |
| 0 | 79 | | { |
| 0 | 80 | | Matrix2d matrix = new Matrix2d(grid.Size.NumX, grid.Size.NumY, ""); |
| | 81 | |
|
| 0 | 82 | | List<Ray> rays = EnvimetUtility.GetRayFromFacegroupBbox(grid, Geometry); |
| | 83 | |
|
| 0 | 84 | | var intersection = EnvimetUtility.Raycasting2D(rays, Geometry, false, false); |
| 0 | 85 | | SetMatrix(intersection, grid, matrix, Material.IDs[0]); |
| | 86 | |
|
| 0 | 87 | | IDmatrix = matrix; |
| 0 | 88 | | } |
| | 89 | | /// <summary> |
| | 90 | | /// String representation of the source. |
| | 91 | | /// </summary> |
| | 92 | | /// <returns>String representation.</returns> |
| | 93 | | public override string ToString() |
| 0 | 94 | | { |
| 0 | 95 | | return String.Format("Source::{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 Source Deserialize(string json) |
| 1 | 105 | | { |
| | 106 | | try |
| 1 | 107 | | { |
| 1 | 108 | | return JsonConvert.DeserializeObject<Source>(json); |
| | 109 | | } |
| 0 | 110 | | catch (Exception e) |
| 0 | 111 | | { |
| 0 | 112 | | throw new Exception(e.Message); |
| | 113 | | } |
| 1 | 114 | | } |
| | 115 | |
|
| | 116 | | public bool Equals(Source 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 sourceObj = obj as Source; |
| 14 | 137 | | if (sourceObj == null) |
| 14 | 138 | | return false; |
| | 139 | | else |
| 0 | 140 | | return Equals(sourceObj); |
| 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 ==(Source source1, Source source2) |
| 16 | 157 | | { |
| 16 | 158 | | if (((object)source1) == null || ((object)source2) == null) |
| 16 | 159 | | return Object.Equals(source1, source2); |
| | 160 | |
|
| 0 | 161 | | return source1.Equals(source2); |
| 16 | 162 | | } |
| | 163 | |
|
| | 164 | | public static bool operator !=(Source source1, Source source2) |
| 1 | 165 | | { |
| 1 | 166 | | return !(source1 == source2); |
| 1 | 167 | | } |
| | 168 | |
|
| | 169 | | } |
| | 170 | | } |