| | 1 | | using Newtonsoft.Json; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | namespace MorphoGeometry |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Facegroup class. |
| | 10 | | /// </summary> |
| | 11 | | public class FaceGroup : IEquatable<FaceGroup> |
| | 12 | | { |
| | 13 | | [JsonProperty("faces", Required = Required.Always)] |
| | 14 | | /// <summary> |
| | 15 | | /// Faces of the facegroup. |
| | 16 | | /// </summary> |
| 18 | 17 | | public List<Face> Faces { get; } |
| | 18 | |
|
| | 19 | | [JsonConstructor] |
| | 20 | | /// <summary> |
| | 21 | | /// Create a new facegroup. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="faces">Faces.</param> |
| 18 | 24 | | public FaceGroup(List<Face> faces) |
| 18 | 25 | | { |
| 18 | 26 | | Faces = TriangulateFaces(faces); |
| 18 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Convert facegroup to array of float. |
| | 31 | | /// </summary> |
| | 32 | | /// <returns>Jagged array.</returns> |
| | 33 | | public float[][][] ToArray() |
| 0 | 34 | | { |
| 0 | 35 | | return Faces |
| 0 | 36 | | .Select(face => { |
| 0 | 37 | | return face.Vertices |
| 0 | 38 | | .Select(_ => _.ToArray()) |
| 0 | 39 | | .ToArray(); |
| 0 | 40 | | }).ToArray(); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | private static List<Face> TriangulateFaces(List<Face> faces) |
| 18 | 44 | | { |
| 18 | 45 | | List<Face> outFaces = new List<Face>(); |
| 126 | 46 | | foreach (Face face in faces) |
| 36 | 47 | | { |
| 36 | 48 | | if (face.IsQuad()) |
| 0 | 49 | | { |
| 0 | 50 | | Face[] tFaces = Face.Triangulate(face); |
| 0 | 51 | | outFaces.Add(tFaces[0]); |
| 0 | 52 | | outFaces.Add(tFaces[1]); |
| 0 | 53 | | } |
| | 54 | | else |
| 36 | 55 | | { |
| 36 | 56 | | outFaces.Add(face); |
| 36 | 57 | | } |
| 36 | 58 | | } |
| 18 | 59 | | return outFaces; |
| 18 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// String representation of facegroup. |
| | 64 | | /// </summary> |
| | 65 | | /// <returns>String representation.</returns> |
| | 66 | | public override String ToString() |
| 0 | 67 | | { |
| 0 | 68 | | return string.Format("FaceGroup::{0}", Faces.Count); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public string Serialize() |
| 1 | 72 | | { |
| 1 | 73 | | return JsonConvert.SerializeObject(this); |
| 1 | 74 | | } |
| | 75 | |
|
| | 76 | | public static FaceGroup Deserialize(string json) |
| 2 | 77 | | { |
| | 78 | | try |
| 2 | 79 | | { |
| 2 | 80 | | return JsonConvert.DeserializeObject<FaceGroup>(json); |
| | 81 | | } |
| 1 | 82 | | catch (Exception e) |
| 1 | 83 | | { |
| 1 | 84 | | throw new Exception(e.Message); |
| | 85 | | } |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | public bool Equals(FaceGroup other) |
| 6 | 89 | | { |
| 6 | 90 | | if (other == null) |
| 0 | 91 | | return false; |
| | 92 | |
|
| 6 | 93 | | if (other != null |
| 6 | 94 | | && Enumerable.SequenceEqual(other.Faces, this.Faces)) |
| 6 | 95 | | return true; |
| | 96 | | else |
| 0 | 97 | | return false; |
| 6 | 98 | | } |
| | 99 | |
|
| | 100 | | public override bool Equals(Object obj) |
| 66 | 101 | | { |
| 66 | 102 | | if (obj == null) |
| 0 | 103 | | return false; |
| | 104 | |
|
| 66 | 105 | | var fgObj = obj as FaceGroup; |
| 66 | 106 | | if (fgObj == null) |
| 66 | 107 | | return false; |
| | 108 | | else |
| 0 | 109 | | return Equals(fgObj); |
| 66 | 110 | | } |
| | 111 | |
|
| | 112 | | public override int GetHashCode() |
| 0 | 113 | | { |
| | 114 | | unchecked |
| 0 | 115 | | { |
| 0 | 116 | | int hash = 17; |
| 0 | 117 | | hash = hash * 23 + Faces.GetHashCode(); |
| 0 | 118 | | return hash; |
| | 119 | | } |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | public static bool operator ==(FaceGroup faceGroup1, FaceGroup faceGroup2) |
| 83 | 123 | | { |
| 83 | 124 | | if (((object)faceGroup1) == null || ((object)faceGroup2) == null) |
| 78 | 125 | | return Object.Equals(faceGroup1, faceGroup2); |
| | 126 | |
|
| 5 | 127 | | return faceGroup1.Equals(faceGroup2); |
| 83 | 128 | | } |
| | 129 | |
|
| | 130 | | public static bool operator !=(FaceGroup faceGroup1, FaceGroup faceGroup2) |
| 6 | 131 | | { |
| 6 | 132 | | return !(faceGroup1 == faceGroup2); |
| 6 | 133 | | } |
| | 134 | | } |
| | 135 | | } |