private static void ConvertToSecondaryArc(designGraph graph, Dictionary <int, List <NonAdjacentBlockings> > NonAdjacentBlocking) { foreach (var key in NonAdjacentBlocking.Keys.ToList()) { var dirs = (from gDir in DisassemblyDirections.Directions where 1 - Math.Abs(gDir.dotProduct(DisassemblyDirections.Directions[key])) < OverlappingFuzzification.CheckWithGlobDirsParall select DisassemblyDirections.Directions.IndexOf(gDir)).ToList(); var oppositeDir = dirs.Where(d => d != key).ToList(); foreach (var blockings in NonAdjacentBlocking[key]) { var from = graph.nodes.Where(n => n.name == blockings.blockingSolids[0].Name).Cast <Component>().ToList()[0]; // blocked var to = graph.nodes.Where(n => n.name == blockings.blockingSolids[1].Name).Cast <Component>().ToList()[0]; // blocking // if the opprosite direction exists for to and from instead of from and to, do not add the arc. // if a is blocked by b in z direction, b is blocked by a in -z direction. So no need to add an additional arc. if (dirs.Count > 1) { if ( graph.arcs.Where(arc => arc is SecondaryConnection) .Cast <SecondaryConnection>() .Any( SC => (SC.From == to && SC.To == from && SC.Directions.Contains(oppositeDir[0])) || (SC.From == from && SC.To == to && SC.Directions.Contains(key)))) { continue; } } var existing = graph.arcs.OfType <SecondaryConnection>() .Where(exist => (exist.From == from && exist.To == to) || (exist.To == from && exist.From == to)) .ToList(); if (existing.Any()) { if (existing[0].From == from && existing[0].To == to && !existing[0].Directions.Contains(key)) { existing[0].Directions.Add(key); } else { if (oppositeDir.Any() && !existing[0].Directions.Contains(oppositeDir[0])) { existing[0].Directions.Add(oppositeDir[0]); } } } else { graph.addArc(from, to, "", typeof(SecondaryConnection)); var a = (SecondaryConnection)graph.arcs.Last(); a.Directions.Add(key); //a.Distance = blockings.blockingDistance; } } } }
private static void UpdateGraphArcs(List <arc> reviewedArc) { foreach (Connection arc in reviewedArc) { //$ Incorperated the ability to add arcs, because otherwise resolving incomplete graphs // would be impossible bool reversed = false; var counterpart = (Connection)AssemblyGraph.arcs.FirstOrDefault(c => c.XmlFrom == arc.XmlFrom && c.XmlTo == arc.XmlTo); if (counterpart == null) { counterpart = (Connection)AssemblyGraph.arcs.FirstOrDefault(c => c.XmlTo == arc.XmlFrom && c.XmlFrom == arc.XmlTo); if (counterpart != null) { reversed = true; } } if (counterpart == null) { AssemblyGraph.addArc(AssemblyGraph.nodes.First(a => a.name == arc.XmlFrom), AssemblyGraph.nodes.First(a => a.name == arc.XmlTo), "", typeof(Connection)); counterpart = (Connection)AssemblyGraph.arcs.Last(); } else { if (arc.Certainty == 0) { AssemblyGraph.removeArc(counterpart); } } counterpart.FiniteDirections = AddDirections(arc.FiniteDirections); counterpart.InfiniteDirections = AddDirections(arc.InfiniteDirections); } }
private static void CheckToHaveConnectedGraph(designGraph assemblyGraph) { // The code will crash if the graph is not connected // let's take a look: var batches = new List <HashSet <Component> >(); var stack = new Stack <Component>(); var visited = new HashSet <Component>(); var globalVisited = new HashSet <Component>(); foreach (Component Component in assemblyGraph.nodes.Where(n => !globalVisited.Contains(n))) { stack.Clear(); visited.Clear(); stack.Push(Component); while (stack.Count > 0) { var pNode = stack.Pop(); visited.Add(pNode); globalVisited.Add(pNode); List <Connection> a2; lock (pNode.arcs) a2 = pNode.arcs.Where(a => a is Connection).Cast <Connection>().ToList(); foreach (Connection arc in a2) { if (!assemblyGraph.nodes.Contains(arc.From) || !assemblyGraph.nodes.Contains(arc.To)) { continue; } var otherNode = (Component)(arc.From == pNode ? arc.To : arc.From); if (visited.Contains(otherNode)) { continue; } stack.Push(otherNode); } } if (visited.Count == assemblyGraph.nodes.Count) { return; } batches.Add(new HashSet <Component>(visited)); } Console.WriteLine("\nSome of the assembly parts are not connected to the rest of the model."); var referenceBatch = batches[0]; var c = false; var visits = 0; var loop = 0; while (referenceBatch.Count < assemblyGraph.nodes.Count) { loop++; if (loop >= 15) { break; } foreach (var rb in referenceBatch) { for (var j = 1; j < batches.Count; j++) { foreach (var b in batches[j]) { foreach (var p1 in StartProcess.Solids[rb.name]) { foreach (var p2 in StartProcess.Solids[b.name]) { if (BlockingDetermination.BoundingBoxOverlap(p1, p2)) { if (BlockingDetermination.ConvexHullOverlap(p1, p2)) { visits++; if (visits == 1) { Console.WriteLine( "\n * Since the graph needs to be connected, the following connections are added by the software:"); } // add a connection with low cetainty between them var lastAdded = (Connection)assemblyGraph.addArc(rb, b, "", typeof(Connection)); lastAdded.Certainty = 0.1; referenceBatch.UnionWith(batches[j]); batches.RemoveAt(j); c = true; Console.WriteLine("\n - " + lastAdded.XmlFrom + lastAdded.XmlTo); } } if (c) { break; } } if (c) { break; } } if (c) { break; } } if (c) { break; } } if (c) { break; } } } if (loop < 15) { Console.WriteLine( "\n * When you are reviewing the connections, please pay a closer attention to the connections above"); } else { Console.WriteLine("\n * Some connections must be added manually between the following batches"); } for (int i = 0; i < batches.Count; i++) { var batch = batches[i]; Console.WriteLine("\n - Batch " + i + ":"); foreach (var component in batch) { Console.WriteLine(" + " + component.name); } } }
static void Main(string[] args) { var Solids = StartProcess.Solids; var solidsNoFastener = StartProcess.SolidsNoFastener; //PrintOutSomeInitialStats(); var globalDirPool = new List <int>(); // Detect gear mates //------------------------------------------------------------------------------------------ var gears = GearDetector.Run(StartProcess.PartsWithOneGeom, StartProcess.SolidPrimitive); var sw = new Stopwatch(); sw.Start(); // Add the solids as nodes to the graph. Exclude the fasteners //------------------------------------------------------------------------------------------ //DisassemblyDirections.Solids = new List<TessellatedSolid>(solidsNoFastener); AddingNodesToGraph(AssemblyGraph, solidsNoFastener); //, gears, screwsAndBolts); // Implementing region octree for every solid //------------------------------------------------------------------------------------------ PartitioningSolid.Partitions = new Dictionary <TessellatedSolid, Partition[]>(); PartitioningSolid.PartitionsAABB = new Dictionary <TessellatedSolid, PartitionAABB[]>(); PartitioningSolid.CreatePartitions(solidsNoFastener); // Part to part interaction to obtain removal directions between every connected pair //------------------------------------------------------------------------------------------ Console.WriteLine(" \n\nAdjacent Blocking Determination ..."); var width = 55; //LoadingBar.start(width, 0); BlockingDetermination.OverlappingSurfaces = new List <OverlappedSurfaces>(); var solidNofastenerList = solidsNoFastener.ToList(); long totalTriTobeChecked = 0; var overlapCheck = new HashSet <KeyValuePair <string, List <TessellatedSolid> >[]>(); for (var i = 0; i < solidsNoFastener.Count - 1; i++) { var subAssem1 = solidNofastenerList[i]; for (var j = i + 1; j < solidsNoFastener.Count; j++) { var subAssem2 = solidNofastenerList[j]; overlapCheck.Add(new[] { subAssem1, subAssem2 }); var tri2Sub1 = subAssem1.Value.Sum(s => s.Faces.Length); var tri2Sub2 = subAssem2.Value.Sum(s => s.Faces.Length); totalTriTobeChecked += tri2Sub1 * tri2Sub2; } } var total = overlapCheck.Count; var refresh = (int)Math.Ceiling(((float)total) / ((float)(width * 4))); var check = 0; long counter = 0; //foreach (var each in overlapCheck) Parallel.ForEach(overlapCheck, each => { if (check % refresh == 0) { //LoadingBar.refresh(width, ((float)check) / ((float)total)); } check++; var localDirInd = new List <int>(); for (var t = 0; t < StartProcess.Directions.Count; t++) { localDirInd.Add(t); } var connected = false; var certainty = 0.0; foreach (var solid1 in each[0].Value) { foreach (var solid2 in each[1].Value) { counter += solid1.Faces.Length * solid2.Faces.Length; double localCertainty; var blocked = BlockingDetermination.DefineBlocking(solid1, solid2, globalDirPool, localDirInd, out localCertainty); if (connected == false) { connected = blocked; } if (localCertainty > certainty) { certainty = localCertainty; } } } if (connected) { // I wrote the code in a way that "solid1" is always "Reference" and "solid2" is always "Moving". // Update the romoval direction if it is a gear mate: localDirInd = GearDetector.UpdateRemovalDirectionsIfGearMate(each[0].Value, each[1].Value, gears, localDirInd); List <int> finDirs, infDirs; NonadjacentBlockingDetermination.FiniteDirectionsBetweenConnectedPartsWithPartitioning( each[0].Value, each[1].Value, localDirInd, out finDirs, out infDirs); lock (AssemblyGraph) { var from = AssemblyGraph[each[1].Key]; // Moving var to = AssemblyGraph[each[0].Key]; // Reference AssemblyGraph.addArc((node)from, (node)to, "", typeof(Connection)); var a = (Connection)AssemblyGraph.arcs.Last(); a.Certainty = certainty; AddInformationToArc(a, finDirs, infDirs); } } }// ); //LoadingBar.refresh(width, 1); Console.WriteLine("\n"); FastenerFunctions.AddFastenersInformation(AssemblyGraph, solidsNoFastener, StartProcess.SolidPrimitive); // create oppositeDirections for global direction pool. FindingOppositeDirectionsForGlobalPool(globalDirPool); // Simplify the solids, before doing anything //------------------------------------------------------------------------------------------ foreach (var solid in solidsNoFastener) { SolidsNoFastenerSimplified.Add(solid.Key, SimplifiedSolids[solid.Key]); } SimplifySolids(SimplifiedSolids, 0.7); // Implementing region octree for every solid //------------------------------------------------------------------------------------------ PartitioningSolid.Partitions = new Dictionary <TessellatedSolid, Partition[]>(); PartitioningSolid.PartitionsAABB = new Dictionary <TessellatedSolid, PartitionAABB[]>(); PartitioningSolid.CreatePartitions(SimplifiedSolids); CheckToHaveConnectedGraph(AssemblyGraph); ///return globalDirPool; }
// This class is added as an alternative for current Nonadjacent blocking determination approach. // The overal approach is the same as before (ray shooting), but number of both rays and blocking // triangles are droped to speedup the function. // Rays: Instead of checking blockings for every direction, for every two parts, their possible // blocking directions are found based upon the planes that can seperate the two CVHs linearlly. // (If the CVHs are not linearly seperable we cannot apply this.) // Triangles: Number of triangles (of the blocking solid) is the most affecting factor in blocking // determination. Code gets really really slow when it goes to check intersection of the ray // and all the triangles of the solid. We are avoiding this problem here by partitionaning // our search space into k number of sections obtained originally from OBB of the solid. internal static void Run(designGraph graph, Dictionary <string, List <TessellatedSolid> > subAssems, List <int> gDir) { Console.WriteLine("\n\nNonadjacent Blocking Determination is running ...."); long totalCases = 0; var subAssemsToList = subAssems.ToList(); for (var i = 0; i < subAssems.Count - 1; i++) { var subAssem1 = subAssemsToList[i]; for (var j = i + 1; j < subAssems.Count; j++) { var subAssem2 = subAssemsToList[j]; var tri2Sub1 = subAssem1.Value.Sum(s => s.Faces.Length); var tri2Sub2 = subAssem2.Value.Sum(s => s.Faces.Length); totalCases += tri2Sub1 * tri2Sub2; } } ObbFacesHashSet = new Dictionary <TessellatedSolid, HashSet <PolygonalFace> >(); CombinedCVHForMultipleGeometries = new Dictionary <string, TVGLConvexHull>(); long counter = 0; foreach (var subAssem in subAssems) { List <BoundingBox> pairList = new List <BoundingBox>(); foreach (var s in subAssem.Value) { //CvhHashSet.Add(s, new HashSet<PolygonalFace>(s.ConvexHull.Faces)); //$ What was used previously /* * ObbFacesHashSet.Add(s, * new HashSet<PolygonalFace>( * PartitioningSolid.TwelveFaceGenerator( * BoundingGeometry.OrientedBoundingBoxDic.First(b=> b.Key.Name == s.Name).Value.CornerVertices.Select( * cv => new Vertex(cv.Position)).ToArray()))); */ KeyValuePair <TessellatedSolid, BoundingBox> pair = BoundingGeometry.OrientedBoundingBoxDic.FirstOrDefault(b => b.Key.Name == s.Name); if (!pair.Equals(default(KeyValuePair <TessellatedSolid, BoundingBox>))) { ObbFacesHashSet.Add(s, new HashSet <PolygonalFace>(PartitioningSolid.TwelveFaceGenerator(pair.Value.CornerVertices.Select(cv => new Vertex(cv.Position)).ToArray()))); } } } CreateCombinedCVHs(subAssems); var solidsL = subAssems.ToList(); int width = 55; int total = (solidsL.Count + 1) * (solidsL.Count / 2); int refresh = (int)Math.Ceiling(((float)total) / ((float)(width))); int check = 0; LoadingBar.start(width, 0); for (var i = 0; i < solidsL.Count; i++) { var solidMoving = solidsL[i].Value; for (var j = i + 1; j < solidsL.Count; j++) { if (check % refresh == 0) { LoadingBar.refresh(width, ((float)check) / ((float)total)); } check++; var blocked = false; // check the convex hull of these two solids to find the planes tha can linearly seperate them // solid1 is moving and solid2 is blocking var solidBlocking = solidsL[j].Value; counter += solidMoving.Sum(s => s.Faces.Length) * solidBlocking.Sum(s => s.Faces.Length); if ( graph.arcs.Any( a => a is Connection && ((a.From.name == solidsL[i].Key && a.To.name == solidsL[j].Key) || (a.From.name == solidsL[j].Key && a.To.name == solidsL[i].Key)))) { continue; } // Add a secondary arc to the var from = GetNode(graph, solidsL[i].Key); var to = GetNode(graph, solidsL[j].Key); graph.addArc(from, to, from.name + to.name, typeof(SecondaryConnection)); var lastAddedSecArc = (SecondaryConnection)graph.arcs.Last(); var filteredDirections = FilterGlobalDirections(solidMoving, solidBlocking, gDir); var oppositeFiltrdDirs = filteredDirections.Select(d => DisassemblyDirections.DirectionsAndOppositsForGlobalpool[d]).ToList(); // remember this: if solid2 is not blocking solid1, we need to check if solid1 is blocking 2 in the opposite direction. // if filteredDirections.Count == gDir.Count then the CVHs overlap // Only directions need to be checked which the moving part can move along them: var scndFilteredDirectionsMoving = FinalSetOfDirectionsFinder(graph, solidMoving, filteredDirections); var scndFilteredDirectionsBlocking = new List <int>(); scndFilteredDirectionsBlocking = FinalSetOfDirectionsFinder(graph, solidBlocking, filteredDirections.Count == gDir.Count ? filteredDirections : oppositeFiltrdDirs); foreach ( var d in scndFilteredDirectionsMoving.Where( d => !scndFilteredDirectionsBlocking.Contains( DisassemblyDirections.DirectionsAndOppositsForGlobalpool[d]))) { scndFilteredDirectionsBlocking.Add(DisassemblyDirections.DirectionsAndOppositsForGlobalpool[d]); } foreach ( var d in scndFilteredDirectionsBlocking.Where( d => !scndFilteredDirectionsMoving.Contains( DisassemblyDirections.DirectionsAndOppositsForGlobalpool[d]))) { scndFilteredDirectionsMoving.Add(DisassemblyDirections.DirectionsAndOppositsForGlobalpool[d]); } if (filteredDirections.Count == gDir.Count) { //continue; Parallel.ForEach(scndFilteredDirectionsMoving, filtDir => //foreach (var filtDir in filteredDirections) { var direction = DisassemblyDirections.Directions[filtDir]; blocked = BlockingDeterminationWithCvhOverlapping(direction, solidMoving, solidBlocking); if (blocked) { lock (lastAddedSecArc.Directions) lastAddedSecArc.Directions.Add(filtDir); if ( scndFilteredDirectionsBlocking.Contains( DisassemblyDirections.DirectionsAndOppositsForGlobalpool[filtDir])) { scndFilteredDirectionsBlocking.Remove( DisassemblyDirections.DirectionsAndOppositsForGlobalpool[filtDir]); } } }); Parallel.ForEach(scndFilteredDirectionsBlocking, filtDir => //foreach (var filtDir in filteredDirections) { var direction = DisassemblyDirections.Directions[filtDir]; blocked = BlockingDeterminationWithCvhOverlapping(direction, solidBlocking, solidMoving); if (blocked) { lock (lastAddedSecArc.Directions) lastAddedSecArc.Directions.Add(DisassemblyDirections.DirectionsAndOppositsForGlobalpool[filtDir]); } }); if (lastAddedSecArc.Directions.Count == 0) { graph.removeArc(lastAddedSecArc); } } else { //continue; // If CVHs dont overlap: //$ Made this non-parallel for debugging purposes - switch back later Parallel.ForEach(scndFilteredDirectionsMoving, filtDir => //foreach (var filtDir in filteredDirections) { var direction = DisassemblyDirections.Directions[filtDir]; blocked = BlockingDeterminationNoCvhOverlapping(direction, solidMoving, solidBlocking); if (blocked) { lock (lastAddedSecArc.Directions) lastAddedSecArc.Directions.Add(filtDir); if ( scndFilteredDirectionsBlocking.Contains( DisassemblyDirections.DirectionsAndOppositsForGlobalpool[filtDir])) { scndFilteredDirectionsBlocking.Remove( DisassemblyDirections.DirectionsAndOppositsForGlobalpool[filtDir]); } } }); Parallel.ForEach(scndFilteredDirectionsBlocking, filtDir => //foreach (var filtDir in filteredDirections) { var direction = DisassemblyDirections.Directions[filtDir]; blocked = BlockingDeterminationNoCvhOverlapping(direction, solidBlocking, solidMoving); if (blocked) { lock (lastAddedSecArc.Directions) lastAddedSecArc.Directions.Add(DisassemblyDirections.DirectionsAndOppositsForGlobalpool[filtDir]); } }); if (lastAddedSecArc.Directions.Count == 0) { graph.removeArc(lastAddedSecArc); } } } } LoadingBar.refresh(width, 1); CreateSameDirectionDictionary(gDir); }