MATSIM
QuadTrees.java
Go to the documentation of this file.
1 /*
2  * *********************************************************************** *
3  * project: org.matsim.*
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2019 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** *
19  */
20 
21 package org.matsim.core.utils.collections;
22 
23 import java.util.Collection;
24 import java.util.function.Function;
25 
27 import org.matsim.api.core.v01.Coord;
28 
29 import com.google.common.base.Preconditions;
30 
34 public class QuadTrees {
35  public static <E extends BasicLocation> QuadTree<E> createQuadTree(Collection<E> elements) {
36  return createQuadTree(elements, BasicLocation::getCoord, 0);
37  }
38 
39  public static <E> QuadTree<E> createQuadTree(Collection<E> elements, Function<E, Coord> coordFunction,
40  double buffer) {
41  Preconditions.checkArgument(buffer >= 0, "Only non-negative buffer allowed");
42  Preconditions.checkArgument(!elements.isEmpty(), "Elements must not be empty");
43  double minX = Double.POSITIVE_INFINITY;
44  double minY = Double.POSITIVE_INFINITY;
45  double maxX = Double.NEGATIVE_INFINITY;
46  double maxY = Double.NEGATIVE_INFINITY;
47 
48  for (E e : elements) {
49  Coord c = coordFunction.apply(e);
50  if (c.getX() < minX) {
51  minX = c.getX();
52  }
53  if (c.getY() < minY) {
54  minY = c.getY();
55  }
56  if (c.getX() > maxX) {
57  maxX = c.getX();
58  }
59  if (c.getY() > maxY) {
60  maxY = c.getY();
61  }
62  }
63 
64  QuadTree<E> quadTree = new QuadTree<E>(minX - buffer, minY - buffer, maxX + buffer, maxY + buffer);
65  for (E stop : elements) {
66  Coord c = coordFunction.apply(stop);
67  quadTree.put(c.getX(), c.getY(), stop);
68  }
69  return quadTree;
70  }
71 }
boolean put(final double x, final double y, final T value)
Definition: QuadTree.java:86
static< E > QuadTree< E > createQuadTree(Collection< E > elements, Function< E, Coord > coordFunction, double buffer)
Definition: QuadTrees.java:39
static< E extends BasicLocation > QuadTree< E > createQuadTree(Collection< E > elements)
Definition: QuadTrees.java:35