Class LayoutUtil

java.lang.Object
cloud.opencode.base.graph.layout.LayoutUtil

public final class LayoutUtil extends Object
Layout Util - Graph Layout Algorithms for Visualization 布局工具类 - 图可视化布局算法

Utility class providing layout algorithms for graph visualization. These algorithms compute 2D positions for graph vertices.

提供图可视化布局算法的工具类。这些算法为图顶点计算二维位置。

Layout Algorithms | 布局算法:

  • Force-Directed (Fruchterman-Reingold) - 力导向布局
  • Circular Layout - 环形布局
  • Grid Layout - 网格布局
  • Hierarchical Layout - 层次布局
  • Random Layout - 随机布局
  • Spring Layout - 弹簧布局

Features | 主要功能:

  • Force-directed layout using Fruchterman-Reingold algorithm - 使用Fruchterman-Reingold算法的力导向布局
  • Spring layout with configurable spring length and iterations - 弹簧布局,可配置弹簧长度和迭代次数
  • Circular, grid, hierarchical, and random layout algorithms - 环形、网格、层次和随机布局算法
  • Layout transformation utilities: center and scale - 布局变换工具:居中和缩放
  • Immutable Point2D record for vertex positioning - 不可变Point2D记录用于顶点定位

Usage Examples | 使用示例:

// Force-directed layout
Map<String, Point2D> positions = LayoutUtil.forceDirected(graph, 800, 600);

// Circular layout
Map<String, Point2D> positions = LayoutUtil.circular(graph, 400, 300, 250);

// Hierarchical layout
Map<String, Point2D> positions = LayoutUtil.hierarchical(graph, 800, 600);

// Apply layout to GEXF export
Map<String, VisualData> visuals = positions.entrySet().stream()
    .collect(Collectors.toMap(Map.Entry::getKey,
        e -> VisualData.position(e.getValue().x(), e.getValue().y())));

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes (returns empty map for null graph) - 空值安全: 是(null图返回空映射)

Performance | 性能特性:

  • Time complexity: O(V^2 * iterations) for force-directed - 时间复杂度: O(V^2 * iterations)(力导向布局)
  • Space complexity: O(V) - 空间复杂度: O(V)
Since:
JDK 25, opencode-base-graph V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

    • DEFAULT_ITERATIONS

      public static final int DEFAULT_ITERATIONS
      Default number of iterations for force-directed algorithms | 力导向算法默认迭代次数
      See Also:
    • DEFAULT_COOLING

      public static final double DEFAULT_COOLING
      Default cooling factor for simulated annealing | 模拟退火默认冷却因子
      See Also:
  • Method Details

    • forceDirected

      public static <V> Map<V, LayoutUtil.Point2D> forceDirected(Graph<V> graph, double width, double height)
      Compute force-directed layout using Fruchterman-Reingold algorithm. 使用Fruchterman-Reingold算法计算力导向布局。

      The algorithm simulates a physical system where vertices repel each other and edges act as springs pulling connected vertices together.

      该算法模拟一个物理系统,其中顶点相互排斥,边像弹簧一样将连接的顶点拉在一起。

      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      width - layout width | 布局宽度
      height - layout height | 布局高度
      Returns:
      map of vertex to position | 顶点到位置的映射
    • forceDirected

      public static <V> Map<V, LayoutUtil.Point2D> forceDirected(Graph<V> graph, double width, double height, int iterations)
      Compute force-directed layout with custom iterations. 使用自定义迭代次数计算力导向布局。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      width - layout width | 布局宽度
      height - layout height | 布局高度
      iterations - number of iterations | 迭代次数
      Returns:
      map of vertex to position | 顶点到位置的映射
    • spring

      public static <V> Map<V, LayoutUtil.Point2D> spring(Graph<V> graph, double width, double height, double springLength)
      Compute spring layout (simplified force-directed). 计算弹簧布局(简化的力导向)。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      width - layout width | 布局宽度
      height - layout height | 布局高度
      springLength - natural spring length | 弹簧自然长度
      Returns:
      map of vertex to position | 顶点到位置的映射
    • spring

      public static <V> Map<V, LayoutUtil.Point2D> spring(Graph<V> graph, double width, double height, double springLength, int iterations)
      Compute spring layout with custom parameters. 使用自定义参数计算弹簧布局。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      width - layout width | 布局宽度
      height - layout height | 布局高度
      springLength - natural spring length | 弹簧自然长度
      iterations - number of iterations | 迭代次数
      Returns:
      map of vertex to position | 顶点到位置的映射
    • circular

      public static <V> Map<V, LayoutUtil.Point2D> circular(Graph<V> graph, double centerX, double centerY, double radius)
      Compute circular layout. 计算环形布局。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      centerX - center x-coordinate | 中心x坐标
      centerY - center y-coordinate | 中心y坐标
      radius - circle radius | 圆半径
      Returns:
      map of vertex to position | 顶点到位置的映射
    • circular

      public static <V> Map<V, LayoutUtil.Point2D> circular(Graph<V> graph, double width, double height)
      Compute circular layout with default center. 使用默认中心计算环形布局。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      width - layout width | 布局宽度
      height - layout height | 布局高度
      Returns:
      map of vertex to position | 顶点到位置的映射
    • grid

      public static <V> Map<V, LayoutUtil.Point2D> grid(Graph<V> graph, double width, double height)
      Compute grid layout. 计算网格布局。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      width - layout width | 布局宽度
      height - layout height | 布局高度
      Returns:
      map of vertex to position | 顶点到位置的映射
    • hierarchical

      public static <V> Map<V, LayoutUtil.Point2D> hierarchical(Graph<V> graph, double width, double height)
      Compute hierarchical layout (top-down). 计算层次布局(自上而下)。

      This algorithm places vertices in layers based on their distance from root vertices (vertices with no incoming edges). Best suited for DAGs.

      该算法根据顶点到根顶点(无入边的顶点)的距离将顶点放置在不同层。最适合DAG。

      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      width - layout width | 布局宽度
      height - layout height | 布局高度
      Returns:
      map of vertex to position | 顶点到位置的映射
    • random

      public static <V> Map<V, LayoutUtil.Point2D> random(Graph<V> graph, double width, double height)
      Compute random layout. 计算随机布局。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      width - layout width | 布局宽度
      height - layout height | 布局高度
      Returns:
      map of vertex to position | 顶点到位置的映射
    • center

      public static <V> Map<V, LayoutUtil.Point2D> center(Map<V, LayoutUtil.Point2D> positions, double width, double height)
      Center layout in the given bounds. 在给定边界内居中布局。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      positions - the positions to center | 要居中的位置
      width - target width | 目标宽度
      height - target height | 目标高度
      Returns:
      centered positions | 居中后的位置
    • scale

      public static <V> Map<V, LayoutUtil.Point2D> scale(Map<V, LayoutUtil.Point2D> positions, double width, double height, double margin)
      Scale layout to fit in the given bounds. 缩放布局以适应给定边界。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      positions - the positions to scale | 要缩放的位置
      width - target width | 目标宽度
      height - target height | 目标高度
      margin - margin from edges | 边缘距离
      Returns:
      scaled positions | 缩放后的位置