我加了這個,希望能幫助別人找到同樣的問題。我用下面的代碼結束了對3.2的處理。雖然,我不確定它是否100%正確。它已經通過了我提出的所有測試。上面的原始代碼在許多相同的測試用例上失敗了。利用點,線,矩形和多維數據集對象
更明確的解決方案:
int axis = node.depth % node.k;
KdNode lesser = node.lesser;
KdNode greater = node.greater;
//Search children branches, if axis aligned distance is less than current distance
if (lesser!=null && !examined.contains(lesser)) {
examined.add(lesser);
boolean lineIntersectsRect = false;
Line line = null;
Cube cube = null;
if (axis==X_AXIS) {
line = new Line(new Point(value.x-lastDistance,value.y,value.z), new Point(value.x+lastDistance,value.y,value.z));
Point tul = new Point(Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY);
Point tur = new Point(node.id.x,Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY);
Point tlr = new Point(node.id.x,Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY);
Point tll = new Point(Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY);
Rectangle trect = new Rectangle(tul,tur,tlr,tll);
Point bul = new Point(Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY);
Point bur = new Point(node.id.x,Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY);
Point blr = new Point(node.id.x,Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);
Point bll = new Point(Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);
Rectangle brect = new Rectangle(bul,bur,blr,bll);
cube = new Cube(trect,brect);
lineIntersectsRect = cube.inserects(line);
} else if (axis==Y_AXIS) {
line = new Line(new Point(value.x,value.y-lastDistance,value.z), new Point(value.x,value.y+lastDistance,value.z));
Point tul = new Point(Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY);
Point tur = new Point(Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY);
Point tlr = new Point(Double.POSITIVE_INFINITY,node.id.y,Double.NEGATIVE_INFINITY);
Point tll = new Point(Double.NEGATIVE_INFINITY,node.id.y,Double.NEGATIVE_INFINITY);
Rectangle trect = new Rectangle(tul,tur,tlr,tll);
Point bul = new Point(Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY);
Point bur = new Point(Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY);
Point blr = new Point(Double.POSITIVE_INFINITY,node.id.y,Double.POSITIVE_INFINITY);
Point bll = new Point(Double.NEGATIVE_INFINITY,node.id.y,Double.POSITIVE_INFINITY);
Rectangle brect = new Rectangle(bul,bur,blr,bll);
cube = new Cube(trect,brect);
lineIntersectsRect = cube.inserects(line);
} else {
line = new Line(new Point(value.x,value.y,value.z-lastDistance), new Point(value.x,value.y,value.z+lastDistance));
Point tul = new Point(Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY);
Point tur = new Point(Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY);
Point tlr = new Point(Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY);
Point tll = new Point(Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY);
Rectangle trect = new Rectangle(tul,tur,tlr,tll);
Point bul = new Point(Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY,node.id.z);
Point bur = new Point(Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY,node.id.z);
Point blr = new Point(Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY,node.id.z);
Point bll = new Point(Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY,node.id.z);
Rectangle brect = new Rectangle(bul,bur,blr,bll);
cube = new Cube(trect,brect);
lineIntersectsRect = cube.inserects(line);
}
//Continue down lesser branch
if (lineIntersectsRect) {
searchNode(value,lesser,K,results,examined);
}
}
if (greater!=null && !examined.contains(greater)) {
examined.add(greater);
boolean lineIntersectsRect = false;
Line line = null;
Cube cube = null;
if (axis==X_AXIS) {
line = new Line(new Point(value.x-lastDistance,value.y,value.z), new Point(value.x+lastDistance,value.y,value.z));
Point tul = new Point(node.id.x,Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY);
Point tur = new Point(Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY);
Point tlr = new Point(Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY);
Point tll = new Point(node.id.x,Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY);
Rectangle trect = new Rectangle(tul,tur,tlr,tll);
Point bul = new Point(node.id.x,Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY);
Point bur = new Point(Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY);
Point blr = new Point(Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);
Point bll = new Point(node.id.x,Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);
Rectangle brect = new Rectangle(bul,bur,blr,bll);
cube = new Cube(trect,brect);
lineIntersectsRect = cube.inserects(line);
} else if (axis==Y_AXIS) {
line = new Line(new Point(value.x,value.y-lastDistance,value.z), new Point(value.x,value.y+lastDistance,value.z));
Point tul = new Point(Double.NEGATIVE_INFINITY,node.id.y,Double.NEGATIVE_INFINITY);
Point tur = new Point(Double.POSITIVE_INFINITY,node.id.y,Double.NEGATIVE_INFINITY);
Point tlr = new Point(Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY);
Point tll = new Point(Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY);
Rectangle trect = new Rectangle(tul,tur,tlr,tll);
Point bul = new Point(Double.NEGATIVE_INFINITY,node.id.y,Double.POSITIVE_INFINITY);
Point bur = new Point(Double.POSITIVE_INFINITY,node.id.y,Double.POSITIVE_INFINITY);
Point blr = new Point(Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);
Point bll = new Point(Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);
Rectangle brect = new Rectangle(bul,bur,blr,bll);
cube = new Cube(trect,brect);
lineIntersectsRect = cube.inserects(line);
} else {
line = new Line(new Point(value.x,value.y,value.z-lastDistance), new Point(value.x,value.y,value.z+lastDistance));
Point tul = new Point(Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY,node.id.z);
Point tur = new Point(Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY,node.id.z);
Point tlr = new Point(Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY,node.id.z);
Point tll = new Point(Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY,node.id.z);
Rectangle trect = new Rectangle(tul,tur,tlr,tll);
Point bul = new Point(Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY);
Point bur = new Point(Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY);
Point blr = new Point(Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);
Point bll = new Point(Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);
Rectangle brect = new Rectangle(bul,bur,blr,bll);
cube = new Cube(trect,brect);
lineIntersectsRect = cube.inserects(line);
}
//Continue down greater branch
if (lineIntersectsRect) {
searchNode(value,greater,K,results,examined);
}
}
我覺得this簡單的代碼也應該工作,已通過所有相同的測試上面的代碼。
int axis = node.depth % node.k;
KdNode lesser = node.lesser;
KdNode greater = node.greater;
//Search children branches, if axis aligned distance is less than current distance
if (lesser!=null && !examined.contains(lesser)) {
examined.add(lesser);
double p1 = Double.MIN_VALUE;
double p2 = Double.MIN_VALUE;
if (axis==X_AXIS) {
p1 = node.id.x;
p2 = value.x-lastDistance;
} else if (axis==Y_AXIS) {
p1 = node.id.y;
p2 = value.y-lastDistance;
} else {
p1 = node.id.z;
p2 = value.z-lastDistance;
}
boolean lineIntersectsCube = ((p2<=p1)?true:false);
//Continue down lesser branch
if (lineIntersectsCube) {
searchNode(value,lesser,K,results,examined);
}
}
if (greater!=null && !examined.contains(greater)) {
examined.add(greater);
double p1 = Double.MIN_VALUE;
double p2 = Double.MIN_VALUE;
if (axis==X_AXIS) {
p1 = node.id.x;
p2 = value.x+lastDistance;
} else if (axis==Y_AXIS) {
p1 = node.id.y;
p2 = value.y+lastDistance;
} else {
p1 = node.id.z;
p2 = value.z+lastDistance;
}
boolean lineIntersectsCube = ((p2>=p1)?true:false);
//Continue down greater branch
if (lineIntersectsCube) {
searchNode(value,greater,K,results,examined);
}
}
你測試過了嗎?爲什麼要問一個關於代碼的問題,你認爲它會做它應該做的事情?如果沒有自己測試代碼,我會說它看起來應該完成這項工作。而且,如果可能的話,我會避免while(true)循環。特別是因爲你的情況,在循環之後它始終是current = node。 – kutschkem
更好地測試代碼,然後提出有關理解算法的問題。如果它不起作用,那麼你可以發佈輸出,我們可以從那裏去。 –
@kutschkem我測試了代碼,它返回了正確的結果,但這並不意味着它正確地實現了算法。我的意思是O(n)算法會返回正確的結果,但不會是正確的。我會更新問題以幫助縮小問題的範圍。 – Justin