import type { AABB } from '@antv/g'; import type { Anchor, NodeLikeData, Placement, Point, RelativePlacement } from '../types'; import { parseAnchor } from './anchor'; import { parsePlacement } from './placement'; /** * 获取节点/ combo 的位置坐标 * * Get the position of node/combo * @param datum - 节点/ combo 的数据 | data of node/combo * @returns - 坐标 | position */ export function positionOf(datum: NodeLikeData): Point { const { x = 0, y = 0, z = 0 } = datum.style || {}; return [+x, +y, +z]; } /** * 检查数据是否有位置坐标 * * Check if the data has a position coordinate * @param datum - 节点/ combo 的数据 | data of node/combo * @returns - 是否有位置坐标 | Whether there is a position coordinate */ export function hasPosition(datum: NodeLikeData): boolean { const { x, y, z } = datum.style || {}; return x !== undefined || y !== undefined || z !== undefined; } /** * 获取相对位置坐标 * * Get position by relative placement * @param bbox - 元素包围盒 | element bounding box * @param placement - 相对于元素的位置 | Point relative to element * @returns - 坐标 | position */ export function getXYByRelativePlacement(bbox: AABB, placement: RelativePlacement): Point { const [x, y] = placement; const { min, max } = bbox; return [min[0] + x * (max[0] - min[0]), min[1] + y * (max[1] - min[1])]; } /** * 获取位置坐标 * * Get position by placement * @param bbox - 元素包围盒 | element bounding box * @param placement - 相对于元素的位置 | Point relative to element * @returns - 坐标 | position */ export function getXYByPlacement(bbox: AABB, placement: Placement = 'center'): Point { const relativePlacement = parsePlacement(placement); return getXYByRelativePlacement(bbox, relativePlacement); } /** * 获取锚点坐标 * * Get anchor position * @param bbox - 元素包围盒 | element bounding box * @param anchor - 锚点位置 | Anchor * @returns - 坐标 | position */ export function getXYByAnchor(bbox: AABB, anchor: Anchor): Point { const parsedAnchor = parseAnchor(anchor); return getXYByRelativePlacement(bbox, parsedAnchor as RelativePlacement); } /** * 通过 rect points 路径点获取 position 方位配置. * * The rect points command is used to obtain the position and orientation configuration. * @param points Points * @returns `{ left: number; right: number; top: number; bottom: number }` */ export const getPositionByRectPoints = (points: Point[]) => { const [p1, p2] = points; return { left: Math.min(p1[0], p2[0]), right: Math.max(p1[0], p2[0]), top: Math.min(p1[1], p2[1]), bottom: Math.max(p1[1], p2[1]), }; };