I have a function that looks something like the code below. Its purpose is to take triangle facets one at a time from an array of points where each three points is one facet, and tessellate them, replacing the facet with a list of smaller facets whose side lengths don’t exceed nodeSize.
Naturally, this function is time consuming for any realistic facet mesh. I’d like to refactor it to use some coarse parallelization. However, Parallel.For doesn’t appear to have a way to step through indices in an array at intervals while preserving the index number.
Bearing in mind that the SplitTriangle function inside the loop is computationally not conducive itself to parallelization, how could I refactor this function?
Protected Shared Function SplitTriangles(Points As IEnumerable(Of Point3D), nodeSize As Single) As List(Of Point3D)
Dim resultList As New List(Of Point3D)
For i As Integer = 0 To Points.Count - 1 Step 3
resultList.AddRange(SplitTriangle(Points(i), Points(i + 1), Points(i + 2), nodeSize * 4))
Next
Return resultList
End Function
You could use the
Enumerable.Range()function to generate the indices for you. I’m not overly familiar with VB.NET, so I’m going to write this in C#.UPDATE
I think this version would be thread safe, but you should check to ensure that it is and that the results are in the correct order.