using System; using System.Collections.Generic; using LoaderCalculator.Data.Materials; using LoaderCalculator.Data.Materials.MaterialBuilders; using LoaderCalculator.Data.Ndms; using StructureHelperCommon.Infrastructures.Enums; using StructureHelperCommon.Infrastructures.Exceptions; using StructureHelperCommon.Infrastructures.Strings; using StructureHelperLogics.Models.Materials; using StructureHelperCommon.Models.Shapes; using StructureHelperLogics.Models.Primitives; using StructureHelper.Models.Materials; namespace StructureHelperLogics.NdmCalculations.Triangulations { public static class Triangulation { public static IEnumerable GetNdms(IEnumerable ndmPrimitives, ITriangulationOptions options) { List ndms = new List(); var headMaterials = GetPrimitiveMaterials(ndmPrimitives); Dictionary materials = GetMaterials(headMaterials, options); foreach (var ndmPrimitive in ndmPrimitives) { IHeadMaterial headMaterial = ndmPrimitive.HeadMaterial; IMaterial material; if (materials.TryGetValue(headMaterial.Id, out material) == false) { throw new Exception("Material dictionary is not valid"); } IEnumerable localNdms = GetNdmsByPrimitive(ndmPrimitive, material); ndms.AddRange(localNdms); } return ndms; } /// /// Returns dictionary of unique materials by collection of primitives /// /// /// private static Dictionary GetPrimitiveMaterials(IEnumerable ndmPrimitives) { Dictionary headMaterials = new Dictionary(); foreach (var ndmPrimitive in ndmPrimitives) { IHeadMaterial material = ndmPrimitive.HeadMaterial; if (!headMaterials.ContainsKey(material.Id)) { headMaterials.Add(material.Id, material); } } return headMaterials; } /// /// Return dictionary of ndm-materials by dictionary of primirive materials /// /// /// /// /// private static Dictionary GetMaterials(Dictionary PrimitiveMaterials, ITriangulationOptions options) { Dictionary materials = new Dictionary(); IEnumerable keyCollection = PrimitiveMaterials.Keys; IMaterial material; foreach (string id in keyCollection) { IHeadMaterial headMaterial; if (PrimitiveMaterials.TryGetValue(id, out headMaterial) == false) { throw new StructureHelperException("Material dictionary is not valid"); } material = headMaterial.GetLoaderMaterial(options.LimiteState, options.CalcTerm); materials.Add(id, material); } return materials; } private static IEnumerable GetNdmsByPrimitive(INdmPrimitive primitive, IMaterial material) { List ndms = new List(); ITriangulationLogicOptions options; ICenter center = primitive.Center; IShape shape = primitive.Shape; if (shape is IRectangleShape) { options = new RectangleTriangulationLogicOptions(primitive); ITriangulationLogic logic = new RectangleTriangulationLogic(options); ndms.AddRange(logic.GetNdmCollection(material)); } else if (shape is IPoint) { options = new PointTriangulationLogicOptions(primitive); IPointTriangulationLogic logic = new PointTriangulationLogic(options); ndms.AddRange(logic.GetNdmCollection(material)); } else { throw new StructureHelperException($"{ErrorStrings.ShapeIsNotCorrect} :{nameof(primitive.Shape)}"); } return ndms; } } }