/// Copyright (c) 2021 Iiro Iivanainen, Harri Linna, Jere Pakkanen, Riikka Vilavaara /// /// Permission is hereby granted, free of charge, to any person obtaining /// a copy of this software and associated documentation files (the /// "Software"), to deal in the Software without restriction, including /// without limitation the rights to use, copy, modify, merge, publish, /// distribute, sublicense, and/or sell copies of the Software, and to /// permit persons to whom the Software is furnished to do so, subject to /// the following conditions: /// /// The above copyright notice and this permission notice shall be included /// in all copies or substantial portions of the Software. /// /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, /// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF /// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. /// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY /// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, /// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE /// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. /// using System; using System.Collections.Generic; using System.Text.Json.Serialization; namespace CoreLibrary { /// /// Attachments class contains list of the attached data and also methods of processing the said data. /// Responsible for handling the whole list. For example getting the data relative to single slice. /// public class Attachments { public List AttachmentList { get; set; } /// /// Constructor for JSON serialization and deserialization. Note that the /// parameter names must match the attribute names (non case sensitive). /// /// [JsonConstructor] public Attachments(List attachmentlist) { AttachmentList = attachmentlist; } /// /// Parameterless constructor that only initializes the object. /// public Attachments() { AttachmentList = new (); } /// /// Changes filepaths to be relative to given filepath, /// /// Filepath that the paths are relative to public void SetAttachmentRelation(string relativeTo) { foreach(AttachedData data in AttachmentList) { data.SetDataRelation(relativeTo); } } /// /// Adds attached data to this object, /// /// Attached data to be added. public void AddData(AttachedData AD) { AttachmentList.Add(AD); } /// /// Removes data by attached data name. /// /// Name of the removed data. public void RemoveDataByName(string dataName) { var toBeRemoved = AttachmentList.Find(a => a.DataName == dataName); AttachmentList.Remove(toBeRemoved); } /// /// Removes attached data from list. /// /// Attached data to be removed public void RemoveData(AttachedData AD) { AttachmentList.Remove(AD); } /// /// Counts the number of attached data per coordinate. /// Doesn't count data that has coordinates smaller than 0 or above depth range /// /// Depth of X-axis. /// Depth of Y-axis. /// Depth of Z-axis. /// public int[][] AttachementCount(int xDepth, int yDepth, int zDepth) { var marks = new int[3][]; marks[0] = new int[xDepth]; marks[1] = new int[yDepth]; marks[2] = new int[zDepth]; foreach (var AD in AttachmentList) { foreach (var vec in AD.AttachmentPoints) { int x = (int)Math.Round(vec.X-1, 0); int y = (int)Math.Round(vec.Y-1, 0); int z = (int)Math.Round(vec.Z-1, 0); if (x >= 0 && x < marks[0].Length && y >= 0 && y < marks[1].Length && z >= 0 && z < marks[2].Length) { marks[0][x] += 1; marks[1][y] += 1; marks[2][z] += 1; } } } return marks; } /// /// Searches attachments with speficied Z coordinate. /// /// Z coordinate /// List of attachments that fit the criteria. internal List AttByZCoordinate(int z) { List ls = new(); foreach (AttachedData att in AttachmentList) { if (att.AttachmentPoints.Count <= 0) continue ; if (att.AttachmentPoints[0].Z == z) ls.Add(att); } return ls; } /// /// Returns Attached data by integer X-coordinates. /// /// X-axis depth. /// Attached data by X-coordinates. internal List AttByXCoordinate(int x) { List ls = new(); foreach (AttachedData att in AttachmentList) { if (att.AttachmentPoints.Count <= 0) continue; if (att.AttachmentPoints[0].X == x) ls.Add(att); } return ls; } /// /// Returns Attached data by integer Y-coordinates. /// /// Y-axis depth. /// Attached data by Y-coordinates. internal List AttByYCoordinate(int y) { List ls = new(); foreach (AttachedData att in AttachmentList) { if (att.AttachmentPoints.Count <= 0) continue; if (att.AttachmentPoints[0].Y == y) ls.Add(att); } return ls; } #nullable enable internal AttachedData? GetAttachment(string name) { foreach(AttachedData att in AttachmentList) { if (att.DataName.Equals(name.Trim())) return att; } return null; } #nullable disable } }