Class KdNode

  1 /*
  2  * Copyright (c) 2016 Vivid Solutions.
  3  *
  4  * All rights reserved. This program and the accompanying materials
  5  * are made available under the terms of the Eclipse Public License 2.0
  6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  8  * and the Eclipse Distribution License is available at
  9  *
 10  * http://www.eclipse.org/org/documents/edl-v10.php.
 11  */
 12  
 13 package org.locationtech.jts.index.kdtree;
 14  
 15 import org.locationtech.jts.geom.Coordinate;
 16  
 17 /**
 18  * A node of a {@link KdTree}, which represents one or more points in the same location.
 19  * 
 20  * @author dskea
 21  */
 22 public class KdNode {
 23  
 24     private Coordinate p = null;
 25     private Object     data;
 26     private KdNode     left;
 27     private KdNode     right;
 28     private int        count;
 29  
 30     /**
 31      * Creates a new KdNode.
 32      * 
 33      * @param _x coordinate of point
 34      * @param _y coordinate of point
 35      * @param data a data objects to associate with this node
 36      */
 37     public KdNode(double _x, double _y, Object data) {
 38         p = new Coordinate(_x, _y);
 39         left = null;
 40         right = null;
 41         count = 1;
 42         this.data = data;
 43     }
 44  
 45     /**
 46      * Creates a new KdNode.
 47      * 
 48      * @param p point location of new node
 49      * @param data a data objects to associate with this node
 50      */
 51     public KdNode(Coordinate p, Object data) {
 52         this.p = new Coordinate(p);
 53         left = null;
 54         right = null;
 55         count = 1;
 56         this.data = data;
 57     }
 58  
 59     /**
 60      * Returns the X coordinate of the node
 61      * 
 62      * @return X coordinate of the node
 63      */
 64     public double getX() {
 65         return p.x;
 66     }
 67  
 68     /**
 69      * Returns the Y coordinate of the node
 70      * 
 71      * @return Y coordinate of the node
 72      */
 73     public double getY() {
 74         return p.y;
 75     }
 76  
 77     /**
 78      * Returns the location of this node
 79      * 
 80      * @return p location of this node
 81      */
 82     public Coordinate getCoordinate() {
 83         return p;
 84     }
 85  
 86     /**
 87      * Gets the user data object associated with this node.
 88      * @return
 89      */
 90     public Object getData() {
 91         return data;
 92     }
 93  
 94     /**
 95      * Returns the left node of the tree
 96      * 
 97      * @return left node
 98      */
 99     public KdNode getLeft() {
100         return left;
101     }
102  
103     /**
104      * Returns the right node of the tree
105      * 
106      * @return right node
107      */
108     public KdNode getRight() {
109         return right;
110     }
111  
112     // Increments counts of points at this location
113     void increment() {
114         count = count + 1;
115     }
116  
117     /**
118      * Returns the number of inserted points that are coincident at this location.
119      * 
120      * @return number of inserted points that this node represents
121      */
122     public int getCount() {
123         return count;
124     }
125  
126     /**
127      * Tests whether more than one point with this value have been inserted (up to the tolerance)
128      * 
129      * @return true if more than one point have been inserted with this value
130      */
131     public boolean isRepeated() {
132         return count > 1;
133     }
134  
135     // Sets left node value
136     void setLeft(KdNode _left) {
137         left = _left;
138     }
139  
140     // Sets right node value
141     void setRight(KdNode _right) {
142         right = _right;
143     }
144 }
145