Files
esengine/packages/framework/spatial/src/ISpatialQuery.ts

332 lines
9.6 KiB
TypeScript
Raw Normal View History

/**
* @zh
* @en Spatial Query Interface
*
* @zh
* @en Provides core abstractions for spatial queries
*/
import type { IVector2 } from '@esengine/ecs-framework-math';
// =============================================================================
// 基础类型 | Basic Types
// =============================================================================
/**
* @zh
* @en Spatial bounding box
*/
export interface IBounds {
/**
* @zh X
* @en Minimum X coordinate
*/
readonly minX: number;
/**
* @zh Y
* @en Minimum Y coordinate
*/
readonly minY: number;
/**
* @zh X
* @en Maximum X coordinate
*/
readonly maxX: number;
/**
* @zh Y
* @en Maximum Y coordinate
*/
readonly maxY: number;
}
/**
* @zh
* @en Positionable object interface
*/
export interface IPositionable {
/**
* @zh
* @en Get object position
*/
readonly position: IVector2;
}
/**
* @zh
* @en Positionable object with bounds interface
*/
export interface IBoundable extends IPositionable {
/**
* @zh
* @en Get object bounds
*/
readonly bounds: IBounds;
}
/**
* @zh 线
* @en Raycast hit result
*/
export interface IRaycastHit<T> {
/**
* @zh
* @en Hit object
*/
readonly target: T;
/**
* @zh
* @en Hit point
*/
readonly point: IVector2;
/**
* @zh 线
* @en Normal at hit point
*/
readonly normal: IVector2;
/**
* @zh 线
* @en Distance from ray origin
*/
readonly distance: number;
}
/**
* @zh
* @en Filter function type
*/
export type SpatialFilter<T> = (item: T) => boolean;
// =============================================================================
// 空间查询接口 | Spatial Query Interface
// =============================================================================
/**
* @zh
* @en Spatial query interface
*
* @zh 线
* @en Provides spatial query capabilities including range queries, nearest neighbor queries, and raycasting
*/
export interface ISpatialQuery<T> {
/**
* @zh
* @en Find all objects within radius
*
* @param center - @zh @en Center point
* @param radius - @zh @en Radius
* @param filter - @zh @en Filter function
* @returns @zh @en Array of objects within radius
*/
findInRadius(center: IVector2, radius: number, filter?: SpatialFilter<T>): T[];
/**
* @zh
* @en Find all objects within rectangle
*
* @param bounds - @zh @en Bounding box
* @param filter - @zh @en Filter function
* @returns @zh @en Array of objects within bounds
*/
findInRect(bounds: IBounds, filter?: SpatialFilter<T>): T[];
/**
* @zh
* @en Find nearest object
*
* @param center - @zh @en Query center point
* @param maxDistance - @zh @en Maximum search distance
* @param filter - @zh @en Filter function
* @returns @zh null @en Nearest object or null
*/
findNearest(center: IVector2, maxDistance?: number, filter?: SpatialFilter<T>): T | null;
/**
* @zh K
* @en Find K nearest objects
*
* @param center - @zh @en Query center point
* @param k - @zh @en Number of objects to return
* @param maxDistance - @zh @en Maximum search distance
* @param filter - @zh @en Filter function
* @returns @zh K @en Array of K nearest objects
*/
findKNearest(center: IVector2, k: number, maxDistance?: number, filter?: SpatialFilter<T>): T[];
/**
* @zh 线
* @en Raycast
*
* @param origin - @zh 线 @en Ray origin
* @param direction - @zh 线@en Ray direction (should be normalized)
* @param maxDistance - @zh @en Maximum detection distance
* @param filter - @zh @en Filter function
* @returns @zh @en Array of hit results sorted by distance
*/
raycast(origin: IVector2, direction: IVector2, maxDistance: number, filter?: SpatialFilter<T>): IRaycastHit<T>[];
/**
* @zh 线
* @en Raycast (return first hit only)
*
* @param origin - @zh 线 @en Ray origin
* @param direction - @zh 线@en Ray direction (should be normalized)
* @param maxDistance - @zh @en Maximum detection distance
* @param filter - @zh @en Filter function
* @returns @zh null @en First hit result or null
*/
raycastFirst(origin: IVector2, direction: IVector2, maxDistance: number, filter?: SpatialFilter<T>): IRaycastHit<T> | null;
}
// =============================================================================
// 空间索引接口 | Spatial Index Interface
// =============================================================================
/**
* @zh
* @en Spatial index interface
*
* @zh
* @en Provides spatial index management capabilities
*/
export interface ISpatialIndex<T> extends ISpatialQuery<T> {
/**
* @zh
* @en Insert object
*
* @param item - @zh @en Object to insert
* @param position - @zh @en Object position
*/
insert(item: T, position: IVector2): void;
/**
* @zh
* @en Remove object
*
* @param item - @zh @en Object to remove
* @returns @zh @en Whether removal was successful
*/
remove(item: T): boolean;
/**
* @zh
* @en Update object position
*
* @param item - @zh @en Object to update
* @param newPosition - @zh @en New position
* @returns @zh @en Whether update was successful
*/
update(item: T, newPosition: IVector2): boolean;
/**
* @zh
* @en Clear index
*/
clear(): void;
/**
* @zh
* @en Get number of objects in index
*/
readonly count: number;
/**
* @zh
* @en Get all objects
*/
getAll(): T[];
}
// =============================================================================
// 工具函数 | Utility Functions
// =============================================================================
/**
* @zh
* @en Create bounding box
*/
export function createBounds(minX: number, minY: number, maxX: number, maxY: number): IBounds {
return { minX, minY, maxX, maxY };
}
/**
* @zh
* @en Create bounding box from center and size
*/
export function createBoundsFromCenter(center: IVector2, width: number, height: number): IBounds {
const halfWidth = width / 2;
const halfHeight = height / 2;
return {
minX: center.x - halfWidth,
minY: center.y - halfHeight,
maxX: center.x + halfWidth,
maxY: center.y + halfHeight
};
}
/**
* @zh
* @en Create bounding box from circle
*/
export function createBoundsFromCircle(center: IVector2, radius: number): IBounds {
return {
minX: center.x - radius,
minY: center.y - radius,
maxX: center.x + radius,
maxY: center.y + radius
};
}
/**
* @zh
* @en Check if point is inside bounds
*/
export function isPointInBounds(point: IVector2, bounds: IBounds): boolean {
return point.x >= bounds.minX && point.x <= bounds.maxX &&
point.y >= bounds.minY && point.y <= bounds.maxY;
}
/**
* @zh
* @en Check if two bounding boxes intersect
*/
export function boundsIntersect(a: IBounds, b: IBounds): boolean {
return a.minX <= b.maxX && a.maxX >= b.minX &&
a.minY <= b.maxY && a.maxY >= b.minY;
}
/**
* @zh
* @en Check if bounds intersects with circle
*/
export function boundsIntersectsCircle(bounds: IBounds, center: IVector2, radius: number): boolean {
const closestX = Math.max(bounds.minX, Math.min(center.x, bounds.maxX));
const closestY = Math.max(bounds.minY, Math.min(center.y, bounds.maxY));
const distanceX = center.x - closestX;
const distanceY = center.y - closestY;
return (distanceX * distanceX + distanceY * distanceY) <= (radius * radius);
}
/**
* @zh
* @en Calculate squared distance between two points
*/
export function distanceSquared(a: IVector2, b: IVector2): number {
const dx = a.x - b.x;
const dy = a.y - b.y;
return dx * dx + dy * dy;
}
/**
* @zh
* @en Calculate distance between two points
*/
export function distance(a: IVector2, b: IVector2): number {
return Math.sqrt(distanceSquared(a, b));
}