public Dictionary <int, List <Mesh> > GenerateJointMeshes(bool label = true) { JointMeshes.Clear(); JointArmLabel.Clear(); var separateJointMeshes = new Dictionary <int, List <Mesh> >(); var jointArmCounter = new Dictionary <int, int>(); var jointCorePoints = new Dictionary <int, List <double[]> >(); for (int v = 0; v < Vertices.Count; v++) { separateJointMeshes[v] = new List <Mesh>(); jointCorePoints[v] = new List <double[]>(); jointArmCounter[v] = 1; } foreach (Tuple <int, int> e in Edges) { if (!RodMeshes.ContainsKey(e)) { continue; } Curve c = new LineCurve(Vertices[e.Item1], Vertices[e.Item2]); double len = c.GetLength(); if (len == 0) { throw new Exception("Joint not created. Joint lengths greater than rod length. Try reducing joint lengths or radius."); } Curve startCurve; Curve endCurve; try { startCurve = c.Trim(CurveEnd.End, len - (Offsets[e] + JointLength)); endCurve = c.Trim(CurveEnd.Start, len - (Offsets[Tuple.Create(e.Item2, e.Item1)] + JointLength)); if (startCurve == null || endCurve == null) { throw new Exception(); } } catch { throw new Exception("Joint not created. Joint lengths greater than rod length. Try reducing joint lengths or radius."); } // Convex hull points var vector = Point3d.Subtract(Vertices[e.Item2], Vertices[e.Item1]); vector.Unitize(); Polyline outerPoly; var outerProfile = GetProfile(OuterWallRadius, vector); outerProfile.TryGetPolyline(out outerPoly); foreach (var l in outerPoly.Skip(1)) { jointCorePoints[e.Item1].Add(new double[] { l.X + Vertices[e.Item1].X, l.Y + Vertices[e.Item1].Y, l.Z + Vertices[e.Item1].Z }); jointCorePoints[e.Item2].Add(new double[] { l.X + Vertices[e.Item2].X, l.Y + Vertices[e.Item2].Y, l.Z + Vertices[e.Item2].Z }); } // Create hollow joint arms var startMesh = GenerateJointArm(Vertices[e.Item1], vector, startCurve.GetLength(), Offsets[e]); var endMesh = GenerateJointArm(Vertices[e.Item2], vector, -endCurve.GetLength(), -Offsets[Tuple.Create(e.Item2, e.Item1)]); separateJointMeshes[e.Item1].Add(startMesh); separateJointMeshes[e.Item2].Add(endMesh); if (label) { // Create joint label var startLabel = GenerateJointArmLabel(Vertices[e.Item1], vector, e.Item1.ToString() + ((char)(jointArmCounter[e.Item1] + 64)).ToString(), startCurve.GetLength()); var endLabel = GenerateJointArmLabel(Vertices[e.Item2], vector, e.Item2.ToString() + ((char)(jointArmCounter[e.Item2] + 64)).ToString(), -endCurve.GetLength()); jointArmCounter[e.Item1]++; jointArmCounter[e.Item2]++; if (startLabel != null) { separateJointMeshes[e.Item1].Add(startLabel); } if (endLabel != null) { separateJointMeshes[e.Item2].Add(endLabel); } } } foreach (KeyValuePair <int, List <double[]> > kvp in jointCorePoints) { try { var scaling = Math.Floor(1000 / kvp.Value.SelectMany(p => p).Max()); var convHullRes = ConvexHull.Create(kvp.Value.Select(p => p.Select(pi => pi * scaling).ToArray()).ToList()); var hullPoints = convHullRes.Result.Points.ToList(); var hullFaces = convHullRes.Result.Faces.ToList(); var newMesh = new Mesh(); newMesh.Vertices.AddVertices(hullPoints.Select(p => new Point3d(p.Position[0] / scaling, p.Position[1] / scaling, p.Position[2] / scaling))); newMesh.Faces.AddFaces(hullFaces.Select(f => new MeshFace(hullPoints.IndexOf(f.Vertices[0]), hullPoints.IndexOf(f.Vertices[1]), hullPoints.IndexOf(f.Vertices[2])))); newMesh.Normals.ComputeNormals(); newMesh.UnifyNormals(); newMesh.Normals.ComputeNormals(); newMesh.Compact(); separateJointMeshes[kvp.Key].Insert(0, newMesh); // Add conv hull to beginning } catch { } } foreach (KeyValuePair <int, List <Mesh> > kvp in separateJointMeshes) { if (kvp.Value.Count > 0) { var mesh = kvp.Value.First(); foreach (var m in kvp.Value.Skip(1)) { mesh.Append(m); } mesh.Weld(Tolerance); JointMeshes[kvp.Key] = new List <Mesh>() { mesh }; } } return(JointMeshes); }
public void ClearModelGeometries() { RodMeshes.Clear(); RodCentrelines.Clear(); JointMeshes.Clear(); }