MATSIM
Cluster.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ActivityCluster.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.network.algorithms.intersectionSimplifier.containers;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.locationtech.jts.geom.Geometry;
29 import org.locationtech.jts.geom.Point;
30 import org.matsim.api.core.v01.Coord;
31 import org.matsim.api.core.v01.Id;
33 
42 public class Cluster implements Identifiable<Cluster>{
43  private final Logger log = LogManager.getLogger(Cluster.class);
44 
47  private final List<ClusterActivity> activities;
48  private Geometry concaveHull;
49 
50  public Cluster(Id<Cluster> clusterId){
51  this.clusterId = clusterId;
52  this.activities = new ArrayList<>();
53  }
54 
58  public void setCenterOfGravity(){
59  if(this.concaveHull == null){
60  if(!activities.isEmpty()){
61  double xTotal = 0;
62  double yTotal = 0;
63  for (ClusterActivity p : activities) {
64  xTotal += p.getCoord().getX();
65  yTotal += p.getCoord().getY();
66  }
67  double xCenter = xTotal / (double) activities.size();
68  double yCenter = yTotal / (double) activities.size();
69 
70  centerOfGravity = new Coord(xCenter, yCenter);
71  } else{
72  throw new IllegalArgumentException("Not enough points in cluster " + clusterId + " to calculate a center of gravity!");
73  }
74  } else{
75  /* Use the centroid of the hull.
76  * FIXME There is a chance that the hull is an empty geometry. */
77  Point centroid = this.concaveHull.getCentroid();
78  if(!centroid.isEmpty()){
79  centerOfGravity = new Coord(centroid.getX(), centroid.getY());
80  } else{
81  log.warn("Cannot set centre of gravity for an empty point!!");
82  log.debug(" --> Unique facility identifier: " + this.getId().toString());
83  log.debug(" --> No centre of gravity set.");
84  }
85  }
86  }
87 
89  return centerOfGravity;
90  }
91 
92  public void setClusterId(Id<Cluster> id){
93  this.clusterId = id;
94  }
95 
96  public List<ClusterActivity> getPoints() {
97  return activities;
98  }
99 
100  @Override
101  public Id<Cluster> getId() {
102  return this.clusterId;
103  }
104 
105  public void setConcaveHull(Geometry geometry){
106  this.concaveHull = geometry;
107  }
108 
109  public Geometry getConcaveHull(Geometry geometry){
110  return this.concaveHull;
111  }
112 }