| | 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.ComponentModel.DataAnnotations; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | |
|
| | 11 | | namespace Morpho25.Geometry |
| | 12 | | { |
| | 13 | | [DisplayName("Building")] |
| | 14 | | /// <summary> |
| | 15 | | /// Building class. |
| | 16 | | /// </summary> |
| | 17 | | public class Building : Entity, IEquatable<Building> |
| | 18 | | { |
| | 19 | | [DisplayName("Name")] |
| | 20 | | [Description("Name of the building group")] |
| | 21 | | [JsonProperty("name")] |
| | 22 | | /// <summary> |
| | 23 | | /// Name of the building. |
| | 24 | | /// </summary> |
| 3 | 25 | | public override string Name { get; } |
| | 26 | |
|
| | 27 | | [DisplayName("Geometry")] |
| | 28 | | [Description("Solid geometry")] |
| | 29 | | [JsonProperty("geometry", Required = Required.Always)] |
| | 30 | | /// <summary> |
| | 31 | | /// Geometry of the building. |
| | 32 | | /// </summary> |
| 6 | 33 | | public FaceGroup Geometry { get; set; } |
| | 34 | |
|
| | 35 | | [JsonIgnore] |
| | 36 | | /// <summary> |
| | 37 | | /// 2D Matrix from the top. |
| | 38 | | /// </summary> |
| 0 | 39 | | public Matrix2d TopMatrix { get; private set; } |
| | 40 | | [JsonIgnore] |
| | 41 | | /// <summary> |
| | 42 | | /// 2D Matrix from the bottom. |
| | 43 | | /// </summary> |
| 0 | 44 | | public Matrix2d BottomMatrix { get; private set; } |
| | 45 | | [JsonIgnore] |
| | 46 | | /// <summary> |
| | 47 | | /// 2D Matrix with building ID. |
| | 48 | | /// </summary> |
| 0 | 49 | | public Matrix2d IDmatrix { get; private set; } |
| | 50 | | [JsonIgnore] |
| | 51 | | /// <summary> |
| | 52 | | /// Collection of string based on Pixel and ID. |
| | 53 | | /// </summary> |
| 0 | 54 | | public List<string> BuildingIDrows { get; private set; } |
| | 55 | | [JsonIgnore] |
| | 56 | | /// <summary> |
| | 57 | | /// Collection of string based on Pixel and wall/roof materials. |
| | 58 | | /// </summary> |
| 0 | 59 | | public List<string> BuildingWallRows { get; private set; } |
| | 60 | | [JsonIgnore] |
| | 61 | | /// <summary> |
| | 62 | | /// Collection of string based on Pixel and green wall/ green roof materials. |
| | 63 | | /// </summary> |
| 0 | 64 | | public List<string> BuildingGreenWallRows { get; private set; } |
| | 65 | |
|
| | 66 | | [DisplayName("Material")] |
| | 67 | | [Description("Facade and roof materials")] |
| | 68 | | [JsonProperty("material")] |
| | 69 | | /// <summary> |
| | 70 | | /// Material of the building. |
| | 71 | | /// </summary> |
| | 72 | | public override Material Material |
| | 73 | | { |
| 9 | 74 | | get { return _material; } |
| | 75 | | protected set |
| 3 | 76 | | { |
| 3 | 77 | | if (value.IDs.Length != 4) |
| 0 | 78 | | throw new ArgumentOutOfRangeException( |
| 0 | 79 | | $"{nameof(value)} should contain 4 materials. Use 'Building.CreateMaterial' method"); |
| | 80 | |
|
| 3 | 81 | | _material = value; |
| 3 | 82 | | } |
| | 83 | |
|
| | 84 | | } |
| | 85 | |
|
| | 86 | | [DisplayName("Observe BPS")] |
| | 87 | | [Description("Export BPS output")] |
| | 88 | | [JsonProperty("observeBPS")] |
| | 89 | | /// <summary> |
| | 90 | | /// Enable Building BPS output |
| | 91 | | /// </summary> |
| 1 | 92 | | public bool ObserveBPS { get; } |
| | 93 | |
|
| | 94 | | [JsonConstructor] |
| | 95 | | /// <summary> |
| | 96 | | /// Create a building. |
| | 97 | | /// </summary> |
| | 98 | | /// <param name="geometry">Geometry of the building.</param> |
| | 99 | | /// <param name="id">Numerical ID.</param> |
| | 100 | | /// <param name="material">Material of the building.</param> |
| | 101 | | /// <param name="name">Optional name.</param> |
| | 102 | | /// <param name="observeBPS">Enable BPS calculation.</param> |
| 3 | 103 | | public Building(FaceGroup geometry, |
| 3 | 104 | | int id, |
| 3 | 105 | | Material material = null, |
| 3 | 106 | | string name = null, |
| 3 | 107 | | bool observeBPS = false) |
| 3 | 108 | | { |
| 3 | 109 | | ID = id; |
| 3 | 110 | | Geometry = geometry; |
| 3 | 111 | | Material = material ?? CreateMaterial(null, null, null, null); |
| 3 | 112 | | Name = name ?? "Building"; |
| 3 | 113 | | ObserveBPS = observeBPS; |
| 3 | 114 | | } |
| | 115 | |
|
| | 116 | | public void SetMatrix(Grid grid) |
| 0 | 117 | | { |
| 0 | 118 | | Matrix2d topMatrix = new Matrix2d(grid.Size.NumX, grid.Size.NumY, "0"); |
| 0 | 119 | | Matrix2d bottomMatrix = new Matrix2d(grid.Size.NumX, grid.Size.NumY, "0"); |
| 0 | 120 | | Matrix2d idMatrix = new Matrix2d(grid.Size.NumX, grid.Size.NumY, "0"); |
| | 121 | |
|
| 0 | 122 | | List<Ray> rays = EnvimetUtility.GetRayFromFacegroupBbox(grid, Geometry); |
| | 123 | |
|
| 0 | 124 | | IEnumerable<Vector> intersectionTop = EnvimetUtility.Raycasting2D(rays, Geometry, true); |
| 0 | 125 | | IEnumerable<Vector> intersectionBottom = EnvimetUtility.Raycasting2D(rays, Geometry, false); |
| | 126 | |
|
| 0 | 127 | | SetMatrix(intersectionTop, grid, topMatrix, ""); |
| 0 | 128 | | SetMatrix(intersectionBottom, grid, bottomMatrix, ""); |
| 0 | 129 | | SetMatrix(intersectionTop, grid, idMatrix, ID.ToString()); |
| | 130 | |
|
| 0 | 131 | | TopMatrix = topMatrix; |
| 0 | 132 | | BottomMatrix = bottomMatrix; |
| 0 | 133 | | IDmatrix = idMatrix; |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | public string Serialize() |
| 1 | 137 | | { |
| 1 | 138 | | return JsonConvert.SerializeObject(this); |
| 1 | 139 | | } |
| | 140 | |
|
| | 141 | | public static Building Deserialize(string json) |
| 1 | 142 | | { |
| | 143 | | try |
| 1 | 144 | | { |
| 1 | 145 | | return JsonConvert.DeserializeObject<Building>(json); |
| | 146 | | } |
| 0 | 147 | | catch (Exception e) |
| 0 | 148 | | { |
| 0 | 149 | | throw new Exception(e.Message); |
| | 150 | | } |
| 1 | 151 | | } |
| | 152 | |
|
| | 153 | | public bool Equals(Building other) |
| 1 | 154 | | { |
| 1 | 155 | | if (other == null) |
| 0 | 156 | | return false; |
| | 157 | |
|
| 1 | 158 | | if (other != null |
| 1 | 159 | | && other.ID == this.ID |
| 1 | 160 | | && other.Name == this.Name |
| 1 | 161 | | && other.Material == this.Material |
| 1 | 162 | | && other.Geometry == this.Geometry) |
| 1 | 163 | | return true; |
| | 164 | | else |
| 0 | 165 | | return false; |
| 1 | 166 | | } |
| | 167 | |
|
| | 168 | | public override bool Equals(Object obj) |
| 14 | 169 | | { |
| 14 | 170 | | if (obj == null) |
| 0 | 171 | | return false; |
| | 172 | |
|
| 14 | 173 | | var buildObj = obj as Building; |
| 14 | 174 | | if (buildObj == null) |
| 14 | 175 | | return false; |
| | 176 | | else |
| 0 | 177 | | return Equals(buildObj); |
| 14 | 178 | | } |
| | 179 | |
|
| | 180 | | public override int GetHashCode() |
| 0 | 181 | | { |
| | 182 | | unchecked |
| 0 | 183 | | { |
| 0 | 184 | | int hash = 17; |
| 0 | 185 | | hash = hash * 23 + ID.GetHashCode(); |
| 0 | 186 | | hash = hash * 23 + Name.GetHashCode(); |
| 0 | 187 | | hash = hash * 23 + Material.GetHashCode(); |
| 0 | 188 | | hash = hash * 23 + Geometry.GetHashCode(); |
| 0 | 189 | | return hash; |
| | 190 | | } |
| 0 | 191 | | } |
| | 192 | |
|
| | 193 | | public static bool operator ==(Building build1, Building build2) |
| 16 | 194 | | { |
| 16 | 195 | | if (((object)build1) == null || ((object)build2) == null) |
| 16 | 196 | | return Object.Equals(build1, build2); |
| | 197 | |
|
| 0 | 198 | | return build1.Equals(build2); |
| 16 | 199 | | } |
| | 200 | |
|
| | 201 | | public static bool operator !=(Building build1, Building build2) |
| 1 | 202 | | { |
| 1 | 203 | | return !(build1 == build2); |
| 1 | 204 | | } |
| | 205 | |
|
| | 206 | | private IEnumerable<string> GetBuildingRows(List<Pixel> pixels) |
| 0 | 207 | | { |
| | 208 | | //pixels = pixels |
| | 209 | | // .OrderBy(_ => _.I) |
| | 210 | | // .OrderBy(_ => _.J) |
| | 211 | | // .OrderBy(_ => _.K) |
| | 212 | | // .ToList(); |
| 0 | 213 | | foreach (var px in pixels) |
| 0 | 214 | | { |
| 0 | 215 | | yield return String.Format("{0},{1},{2},{3},{4}", px.I, px.J, px.K, 1, ID); |
| 0 | 216 | | } |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | private IEnumerable<string> GetBuildingRows(List<Pixel> pixels, |
| | 220 | | Grid grid, string wall, string roof) |
| 0 | 221 | | { |
| 0 | 222 | | var nullPx = new Pixel(0, 0, 0); |
| 0 | 223 | | var one = 1; |
| | 224 | |
|
| | 225 | | // Matrix with default values |
| 0 | 226 | | var matrix = new Matrix3d<string[]>(grid.Size.NumX + one, |
| 0 | 227 | | grid.Size.NumY + one, grid.SequenceZ.Count() + one); |
| | 228 | |
|
| 0 | 229 | | for (int i = 0; i < matrix.GetLengthX(); i++) |
| 0 | 230 | | for (int j = 0; j < matrix.GetLengthY(); j++) |
| 0 | 231 | | for (int k = 0; k < matrix.GetLengthZ(); k++) |
| 0 | 232 | | matrix[i, j, k] = new string[] { null, null, null }; |
| | 233 | |
|
| 0 | 234 | | Parallel.For(0, pixels.Count, i => |
| 0 | 235 | | { |
| 0 | 236 | | var line = string.Empty; |
| 0 | 237 | | var px = pixels[i]; |
| 0 | 238 | |
|
| 0 | 239 | | var li = pixels.FirstOrDefault(_ => _.I == px.I - 1 && _.J == px.J && _.K == px.K); |
| 0 | 240 | | var lj = pixels.FirstOrDefault(_ => _.I == px.I && _.J == px.J - 1 && _.K == px.K); |
| 0 | 241 | | var bk = pixels.FirstOrDefault(_ => _.I == px.I && _.J == px.J && _.K == px.K - 1); |
| 0 | 242 | | var ri = pixels.FirstOrDefault(_ => _.I == px.I + 1 && _.J == px.J && _.K == px.K); |
| 0 | 243 | | var rj = pixels.FirstOrDefault(_ => _.I == px.I && _.J == px.J + 1 && _.K == px.K); |
| 0 | 244 | | var tk = pixels.FirstOrDefault(_ => _.I == px.I && _.J == px.J && _.K == px.K + 1); |
| 0 | 245 | |
|
| 0 | 246 | | var wallMat = (wall != Material.DEFAULT_GREEN_WALL) ? wall : null; |
| 0 | 247 | | var roofMat = (roof != Material.DEFAULT_GREEN_ROOF) ? roof : null; |
| 0 | 248 | |
|
| 0 | 249 | | // Limits |
| 0 | 250 | | var rlx = px.I + 1 < grid.Size.NumX; |
| 0 | 251 | | var rly = px.J + 1 < grid.Size.NumY; |
| 0 | 252 | |
|
| 0 | 253 | | if (li == nullPx) matrix[px.I, px.J, px.K][0] = wallMat; |
| 0 | 254 | | if (lj == nullPx) matrix[px.I, px.J, px.K][1] = wallMat; |
| 0 | 255 | | if (bk == nullPx) matrix[px.I, px.J, px.K][2] = roofMat; |
| 0 | 256 | | if (rlx && ri == nullPx) matrix[px.I + 1, px.J, px.K][0] = wallMat; |
| 0 | 257 | | if (rly && rj == nullPx) matrix[px.I, px.J + 1, px.K][1] = wallMat; |
| 0 | 258 | | if (tk == nullPx) matrix[px.I, px.J, px.K + 1][2] = roofMat; |
| 0 | 259 | | }); |
| 0 | 260 | | var rows = EnvimetUtility.GetStringRows(matrix); |
| 0 | 261 | | return rows; |
| 0 | 262 | | } |
| | 263 | |
|
| | 264 | | private static void ShiftBuildings(Grid grid, List<Pixel> pixels, |
| | 265 | | List<Pixel> terrainPixels) |
| 0 | 266 | | { |
| 0 | 267 | | var zLimit = grid.Size.NumZ - 1; |
| | 268 | | // Envimet Spaces behavior |
| 0 | 269 | | if (terrainPixels.Any()) |
| 0 | 270 | | { |
| 0 | 271 | | Parallel.For(0, pixels.Count, i => |
| 0 | 272 | | { |
| 0 | 273 | | var offset = 0; |
| 0 | 274 | | var pixel = new Pixel(0, 0, 0); |
| 0 | 275 | | var query = terrainPixels |
| 0 | 276 | | .Where(_ => _.I == pixels[i].I && _.J == pixels[i].J); |
| 0 | 277 | | if (query.Any()) offset = query.Select(_ => _.K).Max(); |
| 0 | 278 | | var x = pixels[i].I; |
| 0 | 279 | | var y = pixels[i].J; |
| 0 | 280 | | var z = pixels[i].K + offset; |
| 0 | 281 | |
|
| 0 | 282 | |
|
| 0 | 283 | | pixel.I = x; |
| 0 | 284 | | pixel.J = y; |
| 0 | 285 | | pixel.K = (z >= zLimit) ? zLimit : z; |
| 0 | 286 | | pixels[i] = pixel; |
| 0 | 287 | | }); |
| 0 | 288 | | } |
| 0 | 289 | | } |
| | 290 | |
|
| | 291 | | private static void ShiftBuildings(Grid grid, List<Pixel> pixels, |
| | 292 | | List<Pixel> terrainPixels, int offset = 0) |
| 0 | 293 | | { |
| 0 | 294 | | var zLimit = grid.Size.NumZ - 1; |
| 0 | 295 | | if (terrainPixels.Any()) |
| 0 | 296 | | { |
| 0 | 297 | | Parallel.For(0, pixels.Count, i => |
| 0 | 298 | | { |
| 0 | 299 | | var pixel = new Pixel(0, 0, 0); |
| 0 | 300 | | var x = pixels[i].I; |
| 0 | 301 | | var y = pixels[i].J; |
| 0 | 302 | | var z = pixels[i].K + offset; |
| 0 | 303 | |
|
| 0 | 304 | | pixel.I = x; |
| 0 | 305 | | pixel.J = y; |
| 0 | 306 | | pixel.K = (z >= zLimit) ? zLimit : z; |
| 0 | 307 | | pixels[i] = pixel; |
| 0 | 308 | | }); |
| 0 | 309 | | }; |
| 0 | 310 | | } |
| | 311 | |
|
| | 312 | | private IEnumerable<Pixel> GetPixels(Grid grid) |
| 0 | 313 | | { |
| 0 | 314 | | List<Ray> rays = EnvimetUtility.GetRayFromFacegroupBbox(grid, Geometry); |
| 0 | 315 | | var intersections = EnvimetUtility.Raycasting3D(rays, Geometry, false, false); |
| 0 | 316 | | var centroids = EnvimetUtility.GetCentroids(grid, intersections); |
| 0 | 317 | | var pixels = centroids.Select(_ => _.ToPixel(grid)).ToList(); |
| | 318 | |
|
| 0 | 319 | | return pixels; |
| 0 | 320 | | } |
| | 321 | |
|
| | 322 | | /// <summary> |
| | 323 | | /// Generate model 3D. It should run after generating buildings and terrains. |
| | 324 | | /// </summary> |
| | 325 | | /// <param name="grid">Morpho Grid.</param> |
| | 326 | | /// <param name="terrainPixels">Pixels of the terrain.</param> |
| | 327 | | public void SetMatrix3d(Grid grid, |
| | 328 | | List<Pixel> terrainPixels = null, |
| | 329 | | bool shiftEachVoxel = false) |
| 0 | 330 | | { |
| 0 | 331 | | Reset3Dmatrix(); |
| | 332 | |
|
| 0 | 333 | | var pixels = GetPixels(grid).ToList(); |
| 0 | 334 | | if (!pixels.Any()) return; |
| | 335 | |
|
| 0 | 336 | | if (terrainPixels != null) |
| 0 | 337 | | { |
| 0 | 338 | | var tPixels = FilterPixels(terrainPixels, pixels); |
| 0 | 339 | | if (shiftEachVoxel) |
| 0 | 340 | | { |
| 0 | 341 | | ShiftBuildings(grid, pixels, tPixels); |
| 0 | 342 | | } |
| | 343 | | else |
| 0 | 344 | | { |
| 0 | 345 | | var offset = 0; |
| 0 | 346 | | if (tPixels.Any()) |
| 0 | 347 | | { |
| 0 | 348 | | var groups = tPixels.GroupBy(_ => new { I = _.I, J = _.J }); |
| 0 | 349 | | offset = groups.Select(_ => _.Select(i => i.K).Max()).Min(); |
| 0 | 350 | | } |
| 0 | 351 | | ShiftBuildings(grid, pixels, tPixels, offset); |
| 0 | 352 | | } |
| 0 | 353 | | } |
| | 354 | |
|
| 0 | 355 | | BuildingIDrows.AddRange(GetBuildingRows(pixels)); |
| 0 | 356 | | var wallDB = GetBuildingRows(pixels, grid, |
| 0 | 357 | | Material.IDs[0], Material.IDs[1]); |
| 0 | 358 | | BuildingWallRows.AddRange(wallDB); |
| | 359 | |
|
| 0 | 360 | | if (Material.IDs[2] != Material.DEFAULT_GREEN_WALL || |
| 0 | 361 | | Material.IDs[3] != Material.DEFAULT_GREEN_WALL) |
| 0 | 362 | | { |
| 0 | 363 | | var greeningsDB = GetBuildingRows(pixels, grid, |
| 0 | 364 | | Material.IDs[0], Material.IDs[1]); |
| 0 | 365 | | BuildingGreenWallRows.AddRange(greeningsDB); |
| 0 | 366 | | } |
| 0 | 367 | | } |
| | 368 | |
|
| | 369 | | private List<Pixel> FilterPixels(List<Pixel> terrainPixels, |
| | 370 | | List<Pixel> pixels) |
| 0 | 371 | | { |
| 0 | 372 | | var allI = pixels.Select(_ => _.I).Distinct().ToList(); |
| 0 | 373 | | var allj = pixels.Select(_ => _.J).Distinct().ToList(); |
| | 374 | |
|
| 0 | 375 | | var tPixels = new List<Pixel>(); |
| 0 | 376 | | foreach (var pixel in terrainPixels) |
| 0 | 377 | | { |
| 0 | 378 | | if (!allI.Contains(pixel.I)) continue; |
| 0 | 379 | | if (!allj.Contains(pixel.J)) continue; |
| 0 | 380 | | tPixels.Add(pixel); |
| 0 | 381 | | } |
| | 382 | |
|
| 0 | 383 | | return tPixels; |
| 0 | 384 | | } |
| | 385 | |
|
| | 386 | | private void Reset3Dmatrix() |
| 0 | 387 | | { |
| 0 | 388 | | BuildingWallRows = new List<string>(); |
| 0 | 389 | | BuildingGreenWallRows = new List<string>(); |
| 0 | 390 | | BuildingIDrows = new List<string>(); |
| 0 | 391 | | } |
| | 392 | |
|
| | 393 | | /// <summary> |
| | 394 | | /// String representation of a building. |
| | 395 | | /// </summary> |
| | 396 | | /// <returns>String representation.</returns> |
| | 397 | | public override string ToString() |
| 0 | 398 | | { |
| 0 | 399 | | string name = (Name != " ") ? Name : "Building"; |
| 0 | 400 | | return String.Format("{0}::{1}::{2}", name, ID, String.Join(",", Material.IDs)); |
| 0 | 401 | | } |
| | 402 | |
|
| | 403 | | public static Material CreateMaterial(string wallCode, string roofCode, string greenWallCode, string greenRoofCo |
| 2 | 404 | | { |
| 2 | 405 | | string wall = wallCode ?? Material.DEFAULT_WALL; |
| 2 | 406 | | string roof = roofCode ?? Material.DEFAULT_ROOF; |
| 2 | 407 | | string greenWall = greenWallCode ?? Material.DEFAULT_GREEN_WALL; |
| 2 | 408 | | string greenRoof = greenRoofCode ?? Material.DEFAULT_GREEN_ROOF; |
| | 409 | |
|
| 2 | 410 | | return new Material(new string[] { wall, roof, greenWall, greenRoof }); |
| 2 | 411 | | } |
| | 412 | | } |
| | 413 | | } |