diff options
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/data/spatial')
8 files changed, 231 insertions, 56 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/data/spatial/Polygon.java b/src/de/lmu/ifi/dbs/elki/data/spatial/Polygon.java index 3bb771c1..eef19e82 100644 --- a/src/de/lmu/ifi/dbs/elki/data/spatial/Polygon.java +++ b/src/de/lmu/ifi/dbs/elki/data/spatial/Polygon.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.data.spatial; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -25,12 +25,9 @@ package de.lmu.ifi.dbs.elki.data.spatial; import java.util.Iterator; import java.util.List; -import java.util.ListIterator; import de.lmu.ifi.dbs.elki.math.linearalgebra.Vector; -import de.lmu.ifi.dbs.elki.utilities.iterator.ReverseListIterator; -import de.lmu.ifi.dbs.elki.utilities.iterator.UnmodifiableIterator; -import de.lmu.ifi.dbs.elki.utilities.iterator.UnmodifiableListIterator; +import de.lmu.ifi.dbs.elki.utilities.datastructures.iterator.ArrayListIter; /** * Class representing a simple polygon. While you can obviously store non-simple @@ -41,7 +38,7 @@ import de.lmu.ifi.dbs.elki.utilities.iterator.UnmodifiableListIterator; * * @apiviz.composedOf Vector */ -public class Polygon implements Iterable<Vector>, SpatialComparable { +public class Polygon implements SpatialComparable { /** * The actual points */ @@ -66,15 +63,15 @@ public class Polygon implements Iterable<Vector>, SpatialComparable { super(); this.points = points; // Compute the bounds. - if(points.size() > 0) { + if (points.size() > 0) { final Iterator<Vector> iter = points.iterator(); final Vector first = iter.next(); final int dim = first.getDimensionality(); min = first.getArrayCopy(); max = first.getArrayCopy(); - while(iter.hasNext()) { + while (iter.hasNext()) { Vector next = iter.next(); - for(int i = 0; i < dim; i++) { + for (int i = 0; i < dim; i++) { final double cur = next.get(i); min[i] = Math.min(min[i], cur); max[i] = Math.max(max[i], cur); @@ -90,27 +87,13 @@ public class Polygon implements Iterable<Vector>, SpatialComparable { this.max = new double[] { maxx, maxy }; } - @Override - public Iterator<Vector> iterator() { - return new UnmodifiableIterator<Vector>(points.iterator()); - } - /** - * Get a list iterator. + * Get an iterator to the vector contents. * - * @return List iterator. + * @return Iterator */ - public ListIterator<Vector> listIterator() { - return new UnmodifiableListIterator<Vector>(points.listIterator()); - } - - /** - * Return an iterator that iterates the list backwards. - * - * @return Reversed iterator - */ - public ListIterator<Vector> descendingIterator() { - return new UnmodifiableListIterator<Vector>(new ReverseListIterator<Vector>(points)); + public ArrayListIter<Vector> iter() { + return new ArrayListIter<>(points); } /** @@ -120,15 +103,15 @@ public class Polygon implements Iterable<Vector>, SpatialComparable { */ public void appendToBuffer(StringBuilder buf) { Iterator<Vector> iter = points.iterator(); - while(iter.hasNext()) { + while (iter.hasNext()) { double[] data = iter.next().getArrayRef(); - for(int i = 0; i < data.length; i++) { - if(i > 0) { + for (int i = 0; i < data.length; i++) { + if (i > 0) { buf.append(","); } buf.append(data[i]); } - if(iter.hasNext()) { + if (iter.hasNext()) { buf.append(" "); } } @@ -181,7 +164,7 @@ public class Polygon implements Iterable<Vector>, SpatialComparable { * @return -1, 0, 1 for counterclockwise, undefined and clockwise. */ public int testClockwise() { - if(points.size() < 3) { + if (points.size() < 3) { return 0; } final int size = points.size(); @@ -189,7 +172,7 @@ public class Polygon implements Iterable<Vector>, SpatialComparable { int c = 0; // TODO: faster when using an iterator? - for(int i = 0; i < size; i++) { + for (int i = 0; i < size; i++) { // Three consecutive points final int j = (i + 1) % size; final int k = (i + 2) % size; @@ -198,20 +181,17 @@ public class Polygon implements Iterable<Vector>, SpatialComparable { final double dyji = points.get(j).get(1) - points.get(i).get(1); final double dxkj = points.get(k).get(0) - points.get(j).get(0); final double z = (dxji * dykj) - (dyji * dxkj); - if(z < 0) { + if (z < 0) { c--; - } - else if(z > 0) { + } else if (z > 0) { c++; } } - if(c > 0) { + if (c > 0) { return -1; - } - else if(c < 0) { + } else if (c < 0) { return +1; - } - else { + } else { return 0; } } @@ -238,13 +218,13 @@ public class Polygon implements Iterable<Vector>, SpatialComparable { public boolean intersects2DIncomplete(Polygon other) { assert (this.getDimensionality() == 2); assert (other.getDimensionality() == 2); - for(Vector v : this.points) { - if(other.containsPoint2D(v)) { + for (Vector v : this.points) { + if (other.containsPoint2D(v)) { return true; } } - for(Vector v : other.points) { - if(this.containsPoint2D(v)) { + for (Vector v : other.points) { + if (this.containsPoint2D(v)) { return true; } } @@ -269,14 +249,14 @@ public class Polygon implements Iterable<Vector>, SpatialComparable { Iterator<Vector> it = points.iterator(); Vector pre = points.get(points.size() - 1); - while(it.hasNext()) { + while (it.hasNext()) { final Vector cur = it.next(); final double curx = cur.get(0); final double cury = cur.get(1); final double prex = pre.get(0); final double prey = pre.get(1); - if(((cury > testy) != (prey > testy))) { - if((testx < (prex - curx) * (testy - cury) / (prey - cury) + curx)) { + if (((cury > testy) != (prey > testy))) { + if ((testx < (prex - curx) * (testy - cury) / (prey - cury) + curx)) { c = !c; } } @@ -284,4 +264,4 @@ public class Polygon implements Iterable<Vector>, SpatialComparable { } return c; } -}
\ No newline at end of file +} diff --git a/src/de/lmu/ifi/dbs/elki/data/spatial/PolygonsObject.java b/src/de/lmu/ifi/dbs/elki/data/spatial/PolygonsObject.java index f329c948..dbda87bd 100644 --- a/src/de/lmu/ifi/dbs/elki/data/spatial/PolygonsObject.java +++ b/src/de/lmu/ifi/dbs/elki/data/spatial/PolygonsObject.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.data.spatial; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialComparable.java b/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialComparable.java index c81ce10e..0783a748 100644 --- a/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialComparable.java +++ b/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialComparable.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.data.spatial; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -23,12 +23,13 @@ package de.lmu.ifi.dbs.elki.data.spatial; along with this program. If not, see <http://www.gnu.org/licenses/>. */ - - /** * Defines the required methods needed for comparison of spatial objects. * * @author Elke Achtert + * + * @apiviz.landmark + * @apiviz.excludeSubtypes */ public interface SpatialComparable { /** @@ -55,4 +56,4 @@ public interface SpatialComparable { * @return the maximum coordinate at the specified dimension */ double getMax(int dimension); -}
\ No newline at end of file +} diff --git a/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialSingleMaxComparator.java b/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialSingleMaxComparator.java new file mode 100644 index 00000000..8e491b97 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialSingleMaxComparator.java @@ -0,0 +1,64 @@ +package de.lmu.ifi.dbs.elki.data.spatial; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +import java.util.Comparator; + +/** + * Comparator for sorting spatial objects by the maximum value in a single + * dimension. + * + * @author Erich Schubert + * + * @apiviz.uses SpatialComparable + */ +public class SpatialSingleMaxComparator implements Comparator<SpatialComparable> { + /** + * Current dimension. + */ + int dim; + + /** + * Constructor. + * + * @param dim Dimension to sort by. + */ + public SpatialSingleMaxComparator(int dim) { + super(); + this.dim = dim; + } + + /** + * Set the dimension to sort by. + * + * @param dim Dimension + */ + public void setDimension(int dim) { + this.dim = dim; + } + + @Override + public int compare(SpatialComparable o1, SpatialComparable o2) { + return Double.compare(o1.getMax(dim), o2.getMax(dim)); + } +} diff --git a/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialSingleMeanComparator.java b/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialSingleMeanComparator.java new file mode 100644 index 00000000..ec8f4a38 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialSingleMeanComparator.java @@ -0,0 +1,66 @@ +package de.lmu.ifi.dbs.elki.data.spatial; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +import java.util.Comparator; + +/** + * Comparator for sorting spatial objects by the mean value in a single + * dimension. + * + * @author Erich Schubert + * + * @apiviz.uses SpatialComparable + */ +public class SpatialSingleMeanComparator implements Comparator<SpatialComparable> { + /** + * Current dimension. + */ + int dim; + + /** + * Constructor. + * + * @param dim Dimension to sort by. + */ + public SpatialSingleMeanComparator(int dim) { + super(); + this.dim = dim; + } + + /** + * Set the dimension to sort by. + * + * @param dim Dimension + */ + public void setDimension(int dim) { + this.dim = dim; + } + + @Override + public int compare(SpatialComparable o1, SpatialComparable o2) { + final double v1 = o1.getMin(dim) + o1.getMax(dim); + final double v2 = o2.getMin(dim) + o2.getMax(dim); + return Double.compare(v1, v2); + } +} diff --git a/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialSingleMinComparator.java b/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialSingleMinComparator.java new file mode 100644 index 00000000..8156cbd1 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialSingleMinComparator.java @@ -0,0 +1,64 @@ +package de.lmu.ifi.dbs.elki.data.spatial; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +import java.util.Comparator; + +/** + * Comparator for sorting spatial objects by the minimum value in a single + * dimension. + * + * @author Erich Schubert + * + * @apiviz.uses SpatialComparable + */ +public class SpatialSingleMinComparator implements Comparator<SpatialComparable> { + /** + * Current dimension. + */ + int dim; + + /** + * Constructor. + * + * @param dim Dimension to sort by. + */ + public SpatialSingleMinComparator(int dim) { + super(); + this.dim = dim; + } + + /** + * Set the dimension to sort by. + * + * @param dim Dimension + */ + public void setDimension(int dim) { + this.dim = dim; + } + + @Override + public int compare(SpatialComparable o1, SpatialComparable o2) { + return Double.compare(o1.getMin(dim), o2.getMin(dim)); + } +} diff --git a/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialUtil.java b/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialUtil.java index 607acd1c..3d50f6e2 100644 --- a/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialUtil.java +++ b/src/de/lmu/ifi/dbs/elki/data/spatial/SpatialUtil.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.data.spatial; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/data/spatial/package-info.java b/src/de/lmu/ifi/dbs/elki/data/spatial/package-info.java index e418bcaf..a208bd04 100644 --- a/src/de/lmu/ifi/dbs/elki/data/spatial/package-info.java +++ b/src/de/lmu/ifi/dbs/elki/data/spatial/package-info.java @@ -5,7 +5,7 @@ This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures -Copyright (C) 2012 +Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team |