| | 1 | | using Morpho25.Utility; |
| | 2 | | using MorphoGeometry; |
| | 3 | | using Newtonsoft.Json; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.ComponentModel; |
| | 7 | | using System.Linq; |
| | 8 | |
|
| | 9 | | namespace Morpho25.Geometry |
| | 10 | | { |
| | 11 | | [DisplayName("Terrain")] |
| | 12 | | /// <summary> |
| | 13 | | /// Terrain class. |
| | 14 | | /// </summary> |
| | 15 | | public class Terrain : Entity, IEquatable<Terrain> |
| | 16 | | { |
| | 17 | | [DisplayName("Name")] |
| | 18 | | [Description("Name of the terrain group")] |
| | 19 | | [JsonProperty("name")] |
| | 20 | | /// <summary> |
| | 21 | | /// Name of the terrain. |
| | 22 | | /// </summary> |
| 3 | 23 | | public override string Name { get; } |
| | 24 | |
|
| | 25 | | [DisplayName("Geometry")] |
| | 26 | | [Description("Flat or solid geometry")] |
| | 27 | | [JsonProperty("geometry", Required = Required.Always)] |
| | 28 | | /// <summary> |
| | 29 | | /// Geometry of the terrain. |
| | 30 | | /// </summary> |
| 6 | 31 | | public FaceGroup Geometry { get; set; } |
| | 32 | |
|
| | 33 | | [JsonIgnore] |
| | 34 | | /// <summary> |
| | 35 | | /// Matrix 2D of the terrain. |
| | 36 | | /// </summary> |
| 0 | 37 | | public Matrix2d IDmatrix { get; private set; } |
| | 38 | |
|
| | 39 | | [JsonIgnore] |
| | 40 | | /// <summary> |
| | 41 | | /// Collection of string based on Pixel and ID. |
| | 42 | | /// </summary> |
| 3 | 43 | | public List<string> TerrainIDrows { get; private set; } |
| | 44 | |
|
| | 45 | | [JsonIgnore] |
| | 46 | | /// <summary> |
| | 47 | | /// Location of the terrain in the grid. |
| | 48 | | /// </summary> |
| 3 | 49 | | public List<Pixel> Pixels { get; private set; } |
| | 50 | |
|
| | 51 | | [JsonIgnore] |
| | 52 | | /// <summary> |
| | 53 | | /// Material of the terrain. |
| | 54 | | /// </summary> |
| | 55 | | public override Material Material |
| | 56 | | { |
| 0 | 57 | | get => throw new NotImplementedException(); |
| 0 | 58 | | protected set => throw new NotImplementedException(); |
| | 59 | | } |
| | 60 | | /// <summary> |
| | 61 | | /// Create a new terrain. |
| | 62 | | /// </summary> |
| | 63 | | /// <param name="geometry">Geometry of the terrain.</param> |
| | 64 | | /// <param name="id">Numerical ID.</param> |
| | 65 | | /// <param name="name">Name of the terrain.</param> |
| 3 | 66 | | public Terrain(FaceGroup geometry, |
| 3 | 67 | | int id, string name = null) |
| 3 | 68 | | { |
| 3 | 69 | | ID = id; |
| 3 | 70 | | Geometry = geometry; |
| 3 | 71 | | Name = name ?? "TerrainGroup"; |
| 3 | 72 | | TerrainIDrows = new List<string>(); |
| 3 | 73 | | Pixels = new List<Pixel>(); |
| 3 | 74 | | } |
| | 75 | |
|
| | 76 | | public void SetMatrix(Grid grid) |
| 0 | 77 | | { |
| 0 | 78 | | Matrix2d matrix = new Matrix2d(grid.Size.NumX, grid.Size.NumY, "0"); |
| | 79 | |
|
| 0 | 80 | | List<Ray> rays = EnvimetUtility.GetRayFromFacegroupBbox(grid, Geometry); |
| | 81 | |
|
| 0 | 82 | | var intersection = EnvimetUtility.Raycasting2D(rays, Geometry, true, false); |
| | 83 | | // 2D Part |
| 0 | 84 | | SetMatrix(intersection, grid, matrix, ""); |
| | 85 | | // 3D Part |
| 0 | 86 | | CalculatePixels(grid, intersection); |
| | 87 | |
|
| 0 | 88 | | IDmatrix = matrix; |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private void CalculatePixels(Grid grid, IEnumerable<Vector> intersection) |
| 0 | 92 | | { |
| 0 | 93 | | var voxels = new List<Vector>(); |
| 0 | 94 | | foreach (var pt in intersection) |
| 0 | 95 | | { |
| 0 | 96 | | var h = (pt.z < 0) ? 0 : pt.z; |
| 0 | 97 | | var zList = Util.FilterByMinMax(grid.Zaxis, h, 0); |
| | 98 | |
|
| 0 | 99 | | foreach(var v in zList) |
| 0 | 100 | | { |
| 0 | 101 | | voxels.Add(new Vector(pt.x, pt.y, Convert.ToSingle(v))); |
| 0 | 102 | | } |
| 0 | 103 | | } |
| 0 | 104 | | Pixels = voxels |
| 0 | 105 | | .Select(_ => _.ToPixel(grid)) |
| 0 | 106 | | .ToList(); |
| | 107 | |
|
| 0 | 108 | | TerrainIDrows = GetTerrainRows() |
| 0 | 109 | | .ToList(); |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | private IEnumerable<string> GetTerrainRows() |
| 0 | 113 | | { |
| 0 | 114 | | foreach (var px in Pixels) |
| 0 | 115 | | { |
| 0 | 116 | | yield return String.Format("{0},{1},{2},{3}", |
| 0 | 117 | | px.I, px.J, px.K, "1.00000"); |
| 0 | 118 | | } |
| 0 | 119 | | } |
| | 120 | | /// <summary> |
| | 121 | | /// String representation of the terrain. |
| | 122 | | /// </summary> |
| | 123 | | /// <returns>String representation.</returns> |
| | 124 | | public override string ToString() |
| 0 | 125 | | { |
| 0 | 126 | | return String.Format("Terrain::{0}::{1}", Name, ID); |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | public string Serialize() |
| 1 | 130 | | { |
| 1 | 131 | | return JsonConvert.SerializeObject(this); |
| 1 | 132 | | } |
| | 133 | |
|
| | 134 | | public static Terrain Deserialize(string json) |
| 1 | 135 | | { |
| | 136 | | try |
| 1 | 137 | | { |
| 1 | 138 | | return JsonConvert.DeserializeObject<Terrain>(json); |
| | 139 | | } |
| 0 | 140 | | catch (Exception e) |
| 0 | 141 | | { |
| 0 | 142 | | throw new Exception(e.Message); |
| | 143 | | } |
| 1 | 144 | | } |
| | 145 | |
|
| | 146 | | public bool Equals(Terrain other) |
| 1 | 147 | | { |
| 1 | 148 | | if (other == null) |
| 0 | 149 | | return false; |
| | 150 | |
|
| 1 | 151 | | if (other != null |
| 1 | 152 | | && other.ID == this.ID |
| 1 | 153 | | && other.Name == this.Name |
| 1 | 154 | | && other.Geometry == this.Geometry) |
| 1 | 155 | | return true; |
| | 156 | | else |
| 0 | 157 | | return false; |
| 1 | 158 | | } |
| | 159 | |
|
| | 160 | | public override bool Equals(Object obj) |
| 12 | 161 | | { |
| 12 | 162 | | if (obj == null) |
| 0 | 163 | | return false; |
| | 164 | |
|
| 12 | 165 | | var terrainObj = obj as Terrain; |
| 12 | 166 | | if (terrainObj == null) |
| 12 | 167 | | return false; |
| | 168 | | else |
| 0 | 169 | | return Equals(terrainObj); |
| 12 | 170 | | } |
| | 171 | |
|
| | 172 | | public override int GetHashCode() |
| 0 | 173 | | { |
| | 174 | | unchecked |
| 0 | 175 | | { |
| 0 | 176 | | int hash = 17; |
| 0 | 177 | | hash = hash * 23 + ID.GetHashCode(); |
| 0 | 178 | | hash = hash * 23 + Name.GetHashCode(); |
| 0 | 179 | | hash = hash * 23 + Geometry.GetHashCode(); |
| 0 | 180 | | return hash; |
| | 181 | | } |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | public static bool operator ==(Terrain terrain1, Terrain terrain2) |
| 14 | 185 | | { |
| 14 | 186 | | if (((object)terrain1) == null || ((object)terrain2) == null) |
| 14 | 187 | | return Object.Equals(terrain1, terrain2); |
| | 188 | |
|
| 0 | 189 | | return terrain1.Equals(terrain2); |
| 14 | 190 | | } |
| | 191 | |
|
| | 192 | | public static bool operator !=(Terrain terrain1, Terrain terrain2) |
| 1 | 193 | | { |
| 1 | 194 | | return !(terrain1 == terrain2); |
| 1 | 195 | | } |
| | 196 | | } |
| | 197 | | } |