2016-10-14 3 views
1

私は、四角形の交差テストを扱うための四分木のバージョンを実装しています。しかし、アルゴリズムをテストした後、ノイズがたくさんあるようです。例はこちらをご覧ください: a preview can be seen here私が知っていることから、四分木は、交差する四分円内のオブジェクトを返すだけでなく、残っているエッジがあります。クワッドツリーのエッジをクリーンアップする方法は?

私のコードはここで見ることができます:

public class QuadTree extends BoundingBox implements IntersectionAlgorithm { 

@Setter 
private int MAX_OBJECTS = 10; 
@Setter 
private int MAX_LEVELS = 12; 

private int level; 
private List<BoundingBox> objects; 
private QuadTree[] nodes; 

public QuadTree(int width, int height){ 
    this(0, 0, 0, width, height); 
} 

private QuadTree(int pLevel, int x, int y, int width, int height) { 
    super(x, y, width, height); 
    this.level = pLevel; 
    this.objects = new ArrayList<>(); 
    this.nodes = new QuadTree[4]; 
} 

@Override 
public void insert(BoundingBox box) { 
    if (nodes[0] != null) { 
     int index = getIndex(box); 

     if (index != -1) { 
      nodes[index].insert(box); 

      return; 
     } 
    } 

    objects.add(box); 

    if (objects.size() > MAX_OBJECTS && level < MAX_LEVELS) { 
     if (nodes[0] == null) { 
      split(); 
     } 

     int i = 0; 
     while (i < objects.size()) { 
      int index = getIndex(objects.get(i)); 
      if (index != -1) { 
       nodes[index].insert(objects.remove(i)); 
      } else { 
       i++; 
      } 
     } 
    } 
} 

@Override 
public List<BoundingBox> retrieve(BoundingBox box) { 
    return this.retrieve(box, new ArrayList<>()); 
} 

private List<BoundingBox> retrieve(BoundingBox box, List<BoundingBox> returns) { 
    int index = getIndex(box); 
    if (index != -1 && nodes[0] != null) { 
     nodes[index].retrieve(box, returns); 
    } 

    returns.addAll(objects); 

    return returns; 
} 

private void split() { 
    int subWidth = (width/2); 
    int subHeight = (height/2); 

    nodes[0] = new QuadTree(level + 1, x + subWidth, y, subWidth, subHeight); 
    nodes[1] = new QuadTree(level + 1, x, y, subWidth, subHeight); 
    nodes[2] = new QuadTree(level + 1, x, y + subHeight, subWidth, subHeight); 
    nodes[3] = new QuadTree(level + 1, x + subWidth, y + subHeight, subWidth, subHeight); 
} 

private int getIndex(BoundingBox pRect) { 
    int index = -1; 
    double verticalMidpoint = x + (width/2); 
    double horizontalMidpoint = y + (width/ 2); 

    boolean topQuadrant = (pRect.getY() < horizontalMidpoint && pRect.getY() + pRect.getHeight() < horizontalMidpoint); 
    boolean bottomQuadrant = (pRect.getY() > horizontalMidpoint); 

    if (pRect.getX() < verticalMidpoint && pRect.getX() + pRect.getWidth() < verticalMidpoint) { 
     if (topQuadrant) { 
      index = 1; 
     } else if (bottomQuadrant) { 
      index = 2; 
     } 
    } 
    else if (pRect.getX() > verticalMidpoint) { 
     if (topQuadrant) { 
      index = 0; 
     } else if (bottomQuadrant) { 
      index = 3; 
     } 
    } 

    return index; 
} 

私は私が間違ってアルゴリズムを実装したり、間違ったアルゴリズムを使用していますかどうかわからないのですが、任意の助けをいただければ幸いです! 私は、ボックスをより多くの象限に基づいて変更し、縁のような問題を解決したいと考えています。あなたは

returns.addAll(objects); 

あなたは、各オブジェクトが実際にバウンディングボックスと交差する(あるいはそれがバウンディングボックス内だ場合、どのオブジェクトに応じて、あなたはそれを返すようにしたい)場合に追加する前に確認する必要がある必要があり

答えて

0

returns

+0

私は理解していますが、問題は実際の交差点をチェックすることではなく、交差点自体をチェックすることです。 [link](http://donar.umiacs.umd.edu/quadtree/rectangles/cifquad.html)しかし、このアルゴリズムはエッジなしで行うことができます。 –

関連する問題