public static Set<Double> edgeAngles(final Node<?> node) {
   final Set<Double> angles = new TreeSet<>(new NumericComparator<Double>());
   for (final Edge<?> edge : node.getInEdges()) {
     final double toAngle = edge.getToAngle();
     angles.add(toAngle);
   }
   for (final Edge<?> edge : node.getOutEdges()) {
     final double fromAngle = edge.getFromAngle();
     angles.add(fromAngle);
   }
   return angles;
 }
 @SuppressWarnings("unchecked")
 private static <T, V> V getField(
     final Node<T> node, final String name, final Function<Node<T>, V> function) {
   final String fieldName = NodeProperties.class.getName() + "." + name;
   if (!node.hasProperty(fieldName)) {
     final FunctionObjectPropertyProxy<Node<T>, V> proxy =
         new FunctionObjectPropertyProxy<>(function);
     node.setProperty(fieldName, proxy);
   }
   final V value = (V) node.getProperty(fieldName);
   return value;
 }
 /**
  * Only move the node if there is one of them
  *
  * @param graph2
  * @param maxDistance
  * @param node1
  * @return
  */
 public static <T> boolean movePointsWithinTolerance(
     final Map<Point, Point> movedNodes,
     final Graph<T> graph2,
     final double maxDistance,
     final Node<T> node1) {
   final Graph<T> graph1 = node1.getGraph();
   final List<Node<T>> nodes2 = graph2.getNodes(node1, maxDistance);
   if (nodes2.size() == 1) {
     final Node<T> node2 = nodes2.get(0);
     if (graph1.findNode(node2) == null) {
       final GeometryFactory precisionModel = graph1.getPrecisionModel();
       final Point midPoint = LineSegmentUtil.midPoint(precisionModel, node1, node2);
       if (!node1.equals(2, midPoint)) {
         if (movedNodes != null) {
           movedNodes.put(node1.newPoint2D(), midPoint);
         }
         node1.moveNode(midPoint);
       }
       if (!node2.equals(2, midPoint)) {
         if (movedNodes != null) {
           movedNodes.put(node2.newPoint2D(), midPoint);
         }
         node2.moveNode(midPoint);
       }
     }
   }
   return true;
 }
 public static Map<String, Set<Double>> edgeAnglesByType(final Node<?> node) {
   final Map<String, Set<Double>> anglesByType = new HashMap<>();
   for (final Edge<?> edge : node.getInEdges()) {
     final String typePath = edge.getTypeName();
     final double toAngle = edge.getToAngle();
     final Set<Double> angles = getAnglesForType(anglesByType, typePath);
     angles.add(toAngle);
   }
   for (final Edge<?> edge : node.getOutEdges()) {
     final String typePath = edge.getTypeName();
     final double fromAngle = edge.getFromAngle();
     final Set<Double> angles = getAnglesForType(anglesByType, typePath);
     angles.add(fromAngle);
   }
   return anglesByType;
 }
 public static Set<String> edgeTypeNames(final Node<?> node) {
   final Set<String> typePaths = new HashSet<>();
   for (final Edge<?> edge : node.getEdges()) {
     final String typePath = edge.getTypeName();
     typePaths.add(typePath);
   }
   return typePaths;
 }
 public static Set<RecordDefinition> edgeRecordDefinitions(final Node<?> node) {
   final Set<RecordDefinition> recordDefinitions = new HashSet<>();
   for (final Edge<?> edge : node.getEdges()) {
     final Object object = edge.getObject();
     if (object instanceof Record) {
       final Record record = (Record) object;
       final RecordDefinition recordDefinition = record.getRecordDefinition();
       recordDefinitions.add(recordDefinition);
     }
   }
   return recordDefinitions;
 }
 public static <T> Map<String, List<Edge<T>>> edgesByType(final Node<T> node) {
   final Map<String, List<Edge<T>>> edgesByType = new HashMap<>();
   for (final Edge<T> edge : node.getEdges()) {
     final String typePath = edge.getTypeName();
     List<Edge<T>> typeEdges = edgesByType.get(typePath);
     if (typeEdges == null) {
       typeEdges = new ArrayList<>();
       edgesByType.put(typePath, typeEdges);
     }
     typeEdges.add(edge);
   }
   return edgesByType;
 }
    public static <T> Map<String, Map<LineString, Set<Edge<T>>>> edgesByTypeNameAndLine(
        final Node<T> node) {
      final List<Edge<T>> edges = node.getEdges();
      final Map<String, Map<LineString, Set<Edge<T>>>> typeLineEdgeMap = new HashMap<>();
      for (final Edge<T> edge : new HashSet<>(edges)) {
        final String typePath = edge.getTypeName();
        Map<LineString, Set<Edge<T>>> lineEdgeMap = typeLineEdgeMap.get(typePath);
        if (lineEdgeMap == null) {
          lineEdgeMap = new HashMap<>();
          typeLineEdgeMap.put(typePath, lineEdgeMap);
        }

        Edge.addEdgeToEdgesByLine(node, lineEdgeMap, edge);
      }
      return typeLineEdgeMap;
    }
 public static <T> Map<LineString, Map<String, Set<Edge<T>>>> edgesByLineAndTypeName(
     final Node<T> node) {
   final List<Edge<T>> edges = node.getEdges();
   final Map<LineString, Map<String, Set<Edge<T>>>> lineEdgeMap = new HashMap<>();
   for (final Edge<T> edge : new HashSet<>(edges)) {
     LineString line = edge.getLine();
     Map<String, Set<Edge<T>>> edgesByType = edgesByTypeForLine(lineEdgeMap, line);
     if (edgesByType == null) {
       edgesByType = new HashMap<>();
       if (edge.getEnd(node).isTo()) {
         line = line.reverse();
       }
       lineEdgeMap.put(line, edgesByType);
     }
     Set<Edge<T>> typeEdges = edgesByType.get(edge.getTypeName());
     if (typeEdges == null) {
       typeEdges = new HashSet<>();
       final String typePath = edge.getTypeName();
       edgesByType.put(typePath, typeEdges);
     }
     typeEdges.add(edge);
   }
   return lineEdgeMap;
 }
  public static List<LineString> intersection(
      final GeometryFactory geometryFactory,
      final LineString points1,
      final LineString points2,
      final double maxDistance) {

    final LineStringGraph graph1 = new LineStringGraph(points1);
    graph1.setPrecisionModel(geometryFactory);
    final LineStringGraph graph2 = new LineStringGraph(points2);
    graph2.setPrecisionModel(geometryFactory);
    final Map<Point, Point> movedNodes = new HashMap<>();
    graph1.forEachNode((node) -> movePointsWithinTolerance(movedNodes, graph2, maxDistance, node));
    graph2.forEachNode((node) -> movePointsWithinTolerance(movedNodes, graph1, maxDistance, node));

    final Map<Edge<LineSegment>, List<Node<LineSegment>>> pointsOnEdge1 =
        graph1.getPointsOnEdges(graph2, maxDistance);
    final Map<Edge<LineSegment>, List<Node<LineSegment>>> pointsOnEdge2 =
        graph2.getPointsOnEdges(graph1, maxDistance);
    graph1.splitEdges(pointsOnEdge1);
    graph2.splitEdges(pointsOnEdge2);
    Point startPoint = points1.getPoint(0);
    if (movedNodes.containsKey(startPoint)) {
      startPoint = movedNodes.get(startPoint);
    }
    Point endPoint = points1.getPoint(points1.getVertexCount() - 1);
    if (movedNodes.containsKey(endPoint)) {
      endPoint = movedNodes.get(endPoint);
    }
    final List<LineString> intersections = new ArrayList<>();
    final List<Point> currentCoordinates = new ArrayList<>();
    Node<LineSegment> previousNode = graph1.getNode(startPoint);
    do {
      final List<Edge<LineSegment>> outEdges = previousNode.getOutEdges();
      if (outEdges.isEmpty()) {
        previousNode = null;
      } else if (outEdges.size() > 1) {
        throw new IllegalArgumentException("Cannot handle overlaps\n" + points1 + "\n " + points2);
      } else {
        final Edge<LineSegment> edge = outEdges.get(0);
        final LineSegment line = edge.getObject();
        final Node<LineSegment> nextNode = edge.getToNode();
        if (graph2.hasEdgeBetween(previousNode, nextNode)) {
          if (currentCoordinates.size() == 0) {
            currentCoordinates.add(line.getPoint(0));
          }
          currentCoordinates.add(line.getPoint(1));
        } else {
          if (currentCoordinates.size() > 0) {
            final LineString points =
                new LineStringDouble(points1.getAxisCount(), currentCoordinates);
            intersections.add(points);
            currentCoordinates.clear();
          }
        }
        previousNode = nextNode;
      }

    } while (previousNode != null && !endPoint.equals(2, startPoint));
    if (currentCoordinates.size() > 0) {
      final LineString points = new LineStringDouble(points1.getAxisCount(), currentCoordinates);
      intersections.add(points);
    }
    return intersections;
  }