I'm trying to get more familiar with Java 8 streams as they seem to be very powerful (and shorter), so I rewrote a method to use streams. However I'm not very satisfied with it and would like some suggestions on how to make better use of streams. The code itself has multiple nested loops, so it's not very easy. Also I'm forced to use final arrays to write the variables inside the lambda loops.
// t1, t2 are List<Triangle> passed as parameter
Triangle original = t1.get(0);
t2.forEach(transformed ->
{
for (int[] permutation : permutations)
{
Matrix trial = calculateMatrix(original.getVertices(), applyPermutation(transformed.getVertices(), permutation));
final double[] sum = {0};
points1.stream().map(v -> v.extend(1)).map(trial::multiply).forEach(v -> sum[0] += points2.stream().mapToDouble(v2 -> distanceCheated(v, v2)).min().orElse(0));
if (bestDeviation[0] == null || sum[0] < bestDeviation[0])
{
second[0] = transformed;
bestDeviation[0] = sum[0];
bestMatrix[0] = trial;
}
}
});
EDIT: So as I aparently left out many important information, i try to provide them here:
The first obvious missing information is the connection between points1/2 and t1/2. points1/2 are plain two lists of points contained in two images (original and transformed). t1/2 are lists of triangles built with delauney triangulation using the points from points1/2. The goal is to calculate the transformation matrix used, to create the second set of points.
I posted the whole class here: https://gist.github.com/anonymous/8f21597d8a51c8960de7 Vertex is used as a Vector (mathematical) Triangle consists of 3 Vertices