Skip to content

Commit

Permalink
feat(serialize): mat and building
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonelloDN committed Nov 2, 2023
1 parent 287800f commit 152a518
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 15 deletions.
97 changes: 85 additions & 12 deletions project/Morpho/Morpho25/Geometry/Building.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Morpho25.Utility;
using MorphoGeometry;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -10,48 +11,52 @@ namespace Morpho25.Geometry
/// <summary>
/// Building class.
/// </summary>
public class Building : Entity
public class Building : Entity, IEquatable<Building>
{
[JsonProperty("observeBPS")]
/// <summary>
/// Enable Building BPS output
/// </summary>
public bool ObserveBPS { get; }

[JsonProperty("geometry")]
/// <summary>
/// Geometry of the building.
/// </summary>
public FaceGroup Geometry { get; }

[JsonIgnore]
/// <summary>
/// 2D Matrix from the top.
/// </summary>
public Matrix2d TopMatrix { get; private set; }

[JsonIgnore]
/// <summary>
/// 2D Matrix from the bottom.
/// </summary>
public Matrix2d BottomMatrix { get; private set; }

[JsonIgnore]
/// <summary>
/// 2D Matrix with building ID.
/// </summary>
public Matrix2d IDmatrix { get; private set; }

[JsonIgnore]
/// <summary>
/// Collection of string based on Pixel and ID.
/// </summary>
public List<string> BuildingIDrows { get; private set; }

[JsonIgnore]
/// <summary>
/// Collection of string based on Pixel and wall/roof materials.
/// </summary>
public List<string> BuildingWallRows { get; private set; }

[JsonIgnore]
/// <summary>
/// Collection of string based on Pixel and green wall/ green roof materials.
/// </summary>
public List<string> BuildingGreenWallRows { get; private set; }

[JsonProperty("material")]
/// <summary>
/// Material of the building.
/// </summary>
Expand All @@ -69,22 +74,22 @@ protected set

}

[JsonProperty("name")]
/// <summary>
/// Name of the building.
/// </summary>
public override string Name { get; }

[JsonConstructor]
/// <summary>
/// Create a building.
/// </summary>
/// <param name="grid">Grid object.</param>
/// <param name="geometry">Geometry of the building.</param>
/// <param name="id">Numerical ID.</param>
/// <param name="material">Material of the building.</param>
/// <param name="name">Optional name.</param>
/// <param name="observeBPS">Enable BPS calculation.</param>
public Building(Grid grid,
FaceGroup geometry,
public Building(FaceGroup geometry,
int id,
Material material = null,
string name = null,
Expand All @@ -95,11 +100,9 @@ public Building(Grid grid,
Material = material ?? CreateMaterial(null, null, null, null);
Name = name ?? "Building";
ObserveBPS = observeBPS;

SetMatrix(grid);
}

private void SetMatrix(Grid grid)
public void SetMatrix(Grid grid)
{
Matrix2d topMatrix = new Matrix2d(grid.Size.NumX, grid.Size.NumY, "0");
Matrix2d bottomMatrix = new Matrix2d(grid.Size.NumX, grid.Size.NumY, "0");
Expand All @@ -119,6 +122,76 @@ private void SetMatrix(Grid grid)
IDmatrix = idMatrix;
}

public string Serialize()
{
return JsonConvert.SerializeObject(this);
}

public static Building Deserialize(string json)
{
try
{
return JsonConvert.DeserializeObject<Building>(json);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}

public bool Equals(Building other)
{
if (other == null)
return false;

if (other != null
&& other.ID == this.ID
&& other.Name == this.Name
&& other.Material == this.Material
&& other.Geometry == this.Geometry)
return true;
else
return false;
}

public override bool Equals(Object obj)
{
if (obj == null)
return false;

var buildObj = obj as Building;
if (buildObj == null)
return false;
else
return Equals(buildObj);
}

public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 23 + ID.GetHashCode();
hash = hash * 23 + Name.GetHashCode();
hash = hash * 23 + Material.GetHashCode();
hash = hash * 23 + Geometry.GetHashCode();
return hash;
}
}

public static bool operator ==(Building build1, Building build2)
{
if (((object)build1) == null || ((object)build2) == null)
return Object.Equals(build1, build2);

return build1.Equals(build2);
}

public static bool operator !=(Building build1, Building build2)
{
return !(build1 == build2);
}

private IEnumerable<string> GetBuildingRows(List<Pixel> pixels)
{
//pixels = pixels
Expand Down
2 changes: 2 additions & 0 deletions project/Morpho/Morpho25/Geometry/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Morpho25.Utility;
using MorphoGeometry;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

Expand All @@ -24,6 +25,7 @@ public abstract class Entity
/// </summary>
public abstract string Name { get; }

[JsonProperty("id")]
/// <summary>
/// ID of the entity.
/// </summary>
Expand Down
82 changes: 79 additions & 3 deletions project/Morpho/Morpho25/Geometry/Material.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,62 @@
using System;
using Newtonsoft.Json;
using System;
using System.Linq;

namespace Morpho25.Geometry
{
/// <summary>
/// Material class.
/// </summary>
public class Material
public class Material : IEquatable<Material>
{
[JsonIgnore]
/// <summary>
/// Default wall material.
/// </summary>
public const string DEFAULT_WALL = "000000";
[JsonIgnore]
/// <summary>
/// Default roof material.
/// </summary>
public const string DEFAULT_ROOF = "000000";
[JsonIgnore]
/// <summary>
/// Default green roof material.
/// </summary>
public const string DEFAULT_GREEN_ROOF = " ";
[JsonIgnore]
/// <summary>
/// Default green wall material.
/// </summary>
public const string DEFAULT_GREEN_WALL = " ";
[JsonIgnore]
/// <summary>
/// Default soil material.
/// </summary>
public const string DEFAULT_SOIL = "000000";
[JsonIgnore]
/// <summary>
/// Default plant 2D material.
/// </summary>
public const string DEFAULT_PLANT_2D = "0000XX";
[JsonIgnore]
/// <summary>
/// Default source material.
/// </summary>
public const string DEFAULT_SOURCE = "0000FT";
[JsonIgnore]
/// <summary>
/// Default plant 3D material.
/// </summary>
public const string DEFAULT_PLANT_3D = "0000C2";

[JsonProperty("ids", Required = Required.Always)]
/// <summary>
/// Material array of ID.
/// </summary>
public string[] IDs { get; }
public string[] IDs { get; private set; }

[JsonConstructor]
/// <summary>
/// Create a new material.
/// </summary>
Expand All @@ -62,5 +74,69 @@ public override string ToString()
{
return String.Format("Material::{0}", String.Join(",", IDs));
}

public string Serialize()
{
return JsonConvert.SerializeObject(this);
}

public static Material Deserialize(string json)
{
try
{
return JsonConvert.DeserializeObject<Material>(json);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}

public bool Equals(Material other)
{
if (other == null)
return false;

if (other != null
&& Enumerable.SequenceEqual(other.IDs, this.IDs))
return true;
else
return false;
}

public override bool Equals(Object obj)
{
if (obj == null)
return false;

var materialObj = obj as Material;
if (materialObj == null)
return false;
else
return Equals(materialObj);
}

public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 23 + IDs.GetHashCode();
return hash;
}
}

public static bool operator ==(Material mat1, Material mat2)
{
if (((object)mat1) == null || ((object)mat2) == null)
return Object.Equals(mat1, mat2);

return mat1.Equals(mat2);
}

public static bool operator !=(Material mat1, Material mat2)
{
return !(mat1 == mat2);
}
}
}
2 changes: 2 additions & 0 deletions project/Morpho/Morpho25/Settings/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public override string ToString()

private void SetBuilding()
{
BuildingObjects.ForEach(_ => _.SetMatrix(Grid));

List<Building> buildings = BuildingObjects.OrderBy(b => b.ID).ToList();
List<Matrix2d> topMatrixList = buildings.Select(b => b.TopMatrix).ToList();
List<Matrix2d> bottomMatrixList = buildings.Select(b => b.BottomMatrix).ToList();
Expand Down
Loading

0 comments on commit 152a518

Please sign in to comment.