2013-04-21 3 views
0

나는 3D 공간의 다른 점에 가장 가까운 점의 인덱스를 반환하는 메서드를 작성하려고합니다. 점들은 KD- 트리에 저장되어 있고, 나는 그것들을 나의 방법의 매개 변수 인 점 p와 비교하고 있습니다.특정 거리의 첫 번째 요소 찾기

public int NearestPointWithDistance(KDnnTreeNode node, Point p, Double distance){ 
    int index = -1; 
    if(node == null){ 
    return -1; 
    }else{ 
     double[] point_coordinates = data[node.eltIndex]; 
     Point q = new Point(point_coordinates[0],point_coordinates[1], point_coordinates[2]); 
     if(KDnnTree.distance(p, q) == distance){ 
      return index; 
     } 

     if(node.left != null){ 
      final int left_child = NearestPointWithDistance(node.left, p, distance); 
     } 
     if(node.right != null){ 
      final int right_child = NearestPointWithDistance(node.right, p, distance); 
     } 
    } 
    return index; 

}

문제는, 같은 거리에 여러 지점이있을 수 있습니다 : 여기에 방법이다. 내가 얻은 결과는 점의 색인 목록입니다 (아래 참조). 목록의 첫 번째 요소 만 원합니다 (아래 예제에서는 54510입니다).

54510 
54511 
54512 
54514 
54518 
54526 
54543 
54577 
65355 
76175 
54482 
54416 
54278 
21929 
54001 
74323 

이 방법은 KD 트리에서 두 개의 닫는 점을 검색하는 방법이 아니라는 것을 알고 있지만, 먼저이 방법을 시도하고 싶습니다.

+0

당신은 왼쪽과 오른쪽의 결과를 비교하고 하나를 취할 필요가있다. – BobTheBuilder

답변

0

인수로 java.lang.Double을 사용하지 마십시오. double을 사용하십시오.

왜냐하면 KDNTree.distance()Double을 반환하게되면 값이 아닌 개체 참조를 비교하게됩니다.

당신은 꽤 불편한 API를 가지고 있습니다. 어쨌든, 도우미 기능을합니다

public Point getNodePoint(Node node) 
{ 
    double[] point_coordinates = data[node.eltIndex]; 
    return new Point(point_coordinates[0],point_coordinates[1], point_coordinates[2]); 
} 

최고의 거리 availabale 선택을 사용

double result = KDnnTree.distance(p, q); 
if (result == distance) 
{ 
    return node.eltIndex; 
} 
index = node.eltIndex; // assume given node is best 
if (node.left != null) 
{ 
    final int left_child = NearestPointWithDistance(node.left, p, distance); 
    double left_distance = KDnnTree.distance(p, getNodePoint(left_child); 

    if (Math.abs(distance - result) > Math.abs(distance - left_distance)) 
    { 
     // result given is closer to wanted point 
     result = left_distance; 
     index = left_child; 
    } 
} 
if (node.right != null) 
{ 
    final int right_child = NearestPointWithDistance(node.right, p, distance); 
    double right_distance = KDnnTree.distance(p, getNodePoint(right_child); 

    if (Math.abs(distance - result) > Math.abs(distance - right_distance)) 
    { 
     // result given is closer to wanted point 
     result = right_distance; 
     index = right_child; 
    } 
} 
return index;