Skip to content
Snippets Groups Projects
Commit a00f8d67 authored by Mária Kocúreková's avatar Mária Kocúreková
Browse files

#35 KdTree class - sortPoints changed

parent f674a01d
No related branches found
No related tags found
No related merge requests found
...@@ -8,11 +8,14 @@ import java.util.*; ...@@ -8,11 +8,14 @@ import java.util.*;
public class KdTreeImpl { public class KdTreeImpl {
private final KdNode root; private final KdNode root;
private List<MeshPoint> points;
private final static int X_AXIS = 0; private final static int X_AXIS = 0;
private final static int Y_AXIS = 1; private final static int Y_AXIS = 1;
private final static int Z_AXIS = 2; private final static int Z_AXIS = 2;
/*
private static class Pair { private static class Pair {
private MeshPoint p; private MeshPoint p;
private Integer index; private Integer index;
...@@ -29,7 +32,7 @@ public class KdTreeImpl { ...@@ -29,7 +32,7 @@ public class KdTreeImpl {
public Integer getIndex() { public Integer getIndex() {
return index; return index;
} }
} }*/
/** /**
private static final Comparator<Pair> X_COMPARATOR = (o1, o2) -> { private static final Comparator<Pair> X_COMPARATOR = (o1, o2) -> {
...@@ -57,23 +60,36 @@ public class KdTreeImpl { ...@@ -57,23 +60,36 @@ public class KdTreeImpl {
};**/ };**/
private final Comparator<Integer> XIndex_COMPARATOR = Comparator.comparingDouble(i -> this.points.get(i).getPosition().x);
private final Comparator<Integer> YIndex_COMPARATOR = Comparator.comparingDouble(i -> this.points.get(i).getPosition().y);
private final Comparator<Integer> ZIndex_COMPARATOR = Comparator.comparingDouble(i -> this.points.get(i).getPosition().z);
/*
private static final Comparator<Pair> X_COMPARATOR = Comparator.comparingDouble(o -> o.getP().getPosition().x); private static final Comparator<Pair> X_COMPARATOR = Comparator.comparingDouble(o -> o.getP().getPosition().x);
private static final Comparator<Pair> Y_COMPARATOR = Comparator.comparingDouble(o -> o.getP().getPosition().y); private static final Comparator<Pair> Y_COMPARATOR = Comparator.comparingDouble(o -> o.getP().getPosition().y);
private static final Comparator<Pair> Z_COMPARATOR = Comparator.comparingDouble(o -> o.getP().getPosition().z); private static final Comparator<Pair> Z_COMPARATOR = Comparator.comparingDouble(o -> o.getP().getPosition().z);
*/
public KdTreeImpl(List<MeshPoint> points) { public KdTreeImpl(List<MeshPoint> points) {
if(points == null || points.isEmpty()){ if(points == null || points.isEmpty()){
this.root = null; this.root = null;
this.points = null;
return; return;
} }
List<Integer> sortedByX = sortPoints( X_AXIS, points); this.points = points;
List<Integer> sortedByY = sortPoints( Y_AXIS, points);
List<Integer> sortedByZ = sortPoints( Z_AXIS, points); List<Integer> sortedByX = sortPoints( X_AXIS/*, points*/);
List<Integer> sortedByY = sortPoints( Y_AXIS/*, points*/);
List<Integer> sortedByZ = sortPoints( Z_AXIS/*, points*/);
root = buildTree(null, sortedByX, sortedByY, sortedByZ, 0, points); root = buildTree(null, sortedByX, sortedByY, sortedByZ, 0/*, points*/);
} }
...@@ -90,9 +106,13 @@ public class KdTreeImpl { ...@@ -90,9 +106,13 @@ public class KdTreeImpl {
} }
private List<Integer> sortPoints(/*List<Integer> points,*/ int level, List<MeshPoint> p){ private List<Integer> sortPoints(/*List<Integer> points,*/ int level /*List<MeshPoint> p*/){
List<Integer> sortedListIndex = new ArrayList<>(p.size()); List<Integer> sortedListIndex = new ArrayList<>(this.points.size());
List<Pair> sortedListPairs = new ArrayList<>(p.size()); for(int i = 0; i < points.size(); i++){
sortedListIndex.add(i);
}
/*List<Pair> sortedListPairs = new ArrayList<>(p.size());
for (int i = 0; i < p.size(); i++ ) { for (int i = 0; i < p.size(); i++ ) {
sortedListPairs.add(new Pair(p.get(i), i)); sortedListPairs.add(new Pair(p.get(i), i));
...@@ -109,12 +129,21 @@ public class KdTreeImpl { ...@@ -109,12 +129,21 @@ public class KdTreeImpl {
for(Pair pair : sortedListPairs) { for(Pair pair : sortedListPairs) {
sortedListIndex.add(pair.getIndex()); sortedListIndex.add(pair.getIndex());
} }
return sortedListIndex;*/
if(level % 3 == 0){
sortedListIndex.sort(XIndex_COMPARATOR);
}else if(level % 3 == 1){
sortedListIndex.sort(YIndex_COMPARATOR);
}else if(level % 3 == 2){
sortedListIndex.sort(ZIndex_COMPARATOR);
}
return sortedListIndex; return sortedListIndex;
} }
private KdNode buildTree(KdNode parent, List<Integer> byX, List<Integer> byY, List<Integer> byZ, int level, List<MeshPoint> points) { private KdNode buildTree(KdNode parent, List<Integer> byX, List<Integer> byY, List<Integer> byZ, int level/*, List<MeshPoint> points*/) {
KdNode node = null; KdNode node = null;
int mid; int mid;
int midIndex; int midIndex;
...@@ -131,19 +160,19 @@ public class KdTreeImpl { ...@@ -131,19 +160,19 @@ public class KdTreeImpl {
//but also keep the ordering by their axis //but also keep the ordering by their axis
if (level % 3 == 0){ if (level % 3 == 0){
midIndex = byX.get(mid); midIndex = byX.get(mid);
node = new KdNode(midIndex, level, points.get(byX.get(mid)), parent); node = new KdNode(midIndex, level, this.points.get(byX.get(mid)), parent);
splitTree(mid, leftX, byX, leftY, byY, leftZ, byZ); splitTree(mid, leftX, byX, leftY, byY, leftZ, byZ);
}else if(level % 3 == 1){ }else if(level % 3 == 1){
midIndex = byY.get(mid); midIndex = byY.get(mid);
node = new KdNode(midIndex, level, points.get(byY.get(mid)), parent); node = new KdNode(midIndex, level, this.points.get(byY.get(mid)), parent);
splitTree(mid, leftY, byY, leftX, byX, leftZ, byZ); splitTree(mid, leftY, byY, leftX, byX, leftZ, byZ);
}else{ }else{
midIndex = byZ.get(mid); midIndex = byZ.get(mid);
node = new KdNode(midIndex, level, points.get(byZ.get(mid)), parent); node = new KdNode(midIndex, level, this.points.get(byZ.get(mid)), parent);
splitTree(mid, leftZ, byZ, leftY, byY, leftX, byX); splitTree(mid, leftZ, byZ, leftY, byY, leftX, byX);
...@@ -157,8 +186,8 @@ public class KdTreeImpl { ...@@ -157,8 +186,8 @@ public class KdTreeImpl {
leftY.removeAll(Collections.singleton(midIndex)); leftY.removeAll(Collections.singleton(midIndex));
leftZ.removeAll(Collections.singleton(midIndex)); leftZ.removeAll(Collections.singleton(midIndex));
node.setLesser(buildTree(node, leftX, leftY, leftZ, level + 1, points)); node.setLesser(buildTree(node, leftX, leftY, leftZ, level + 1/*, points*/));
node.setGreater(buildTree(node, byX, byY, byZ, level + 1, points)); node.setGreater(buildTree(node, byX, byY, byZ, level + 1/*, points*/));
} }
return node; return node;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment