Hi,
I'm making a recursive function to create a solid which is the union of all solids that are given. Here's my code:
private static Solid SolidByUnion(List<Solid> solids) { Solid result; if (solids.Count > 2) { List<Solid> restOfSolids = solids.Skip(0).Take(solids.Count - 1).ToList(); Solid solid1 = solids[0]; Solid solid2 = SolidByUnion(restOfSolids); result = BooleanOperationsUtils.ExecuteBooleanOperation(solid1, solid2, BooleanOperationsType.Union); return result; } else { Solid solid1 = solids[0]; Solid solid2 = solids[1]; result = BooleanOperationsUtils.ExecuteBooleanOperation(solid1, solid2, BooleanOperationsType.Union); return result; } }
However what I'm getting as the result is only the union of first two ( or maybe last two ) solids in the list. It's probably a basic recursion mistake but I can't figure out what I'm doing wrong here. Any hints?
Thanks.
Solved! Go to Solution.
Solved by FAIR59. Go to Solution.
Solved by jeremytammik. Go to Solution.
Normally, iteration is easier to implement and understand than recursion.
Here is an iterative solution:
http://thebuildingcoder.typepad.com/blog/2013/04/room-and-furniture-loops-using-symbols.html#6
Solid union = null; foreach( GeometryObject obj in geo ) { Solid solid = obj as Solid; if( null != solid && 0 < solid.Faces.Size ) { if( null == union ) { union = solid; } else { union = BooleanOperationsUtils .ExecuteBooleanOperation( union, solid, BooleanOperationsType.Union ); } } }
Cheers,
Jeremy
Thanks Jeremy,
Your solution solves my problem.
Cheers.
PS: I'm still looking however to the recursive solution ( just for learning purposes ) if anyone has any ideas.
you misunderstand the use of List<>.Skip(). The value passed to the function is the number of elements to skip, and not the index of the listelement to needs to be skipped.
So in solids.Skip(0).Take(solids.Count - 1).ToList(); the skip part doesn't do anything. The Take part then selects the first Count-1 entries. You never process the last entry.
Correct code syntax to eliminate the first entry:
solids.GetRange(1,solids.Count-1); solids.Skip(1);
Can't find what you're looking for? Ask the community or share your knowledge.