private boolean insert(int index) { INDArray point = data.slice(index); if (!boundary.contains(point)) return false; cumSize++; double mult1 = (double) (cumSize - 1) / (double) cumSize; double mult2 = 1.0 / (double) cumSize; centerOfMass.muli(mult1); centerOfMass.addi(point.mul(mult2)); // If there is space in this quad tree and it is a leaf, add the object here if (isLeaf() && size < nodeCapacity) { this.index[size] = index; indices.add(point); size++; return true; } for (int i = 0; i < size; i++) { INDArray compPoint = data.slice(this.index[i]); if (compPoint.equals(point)) return true; } if (isLeaf()) subDivide(); // Find out where the point can be inserted for (int i = 0; i < numChildren; i++) { if (children[i].insert(index)) return true; } throw new IllegalStateException("Shouldn't reach this state"); }