import type { MultiFn1O } from "@thi.ng/defmulti" ; import { defmulti } from "@thi.ng/defmulti/defmulti" ; import type { AABBLike , IShape , PCLike } from "@thi.ng/geom-api" ; import { centerOfWeight2 } from "@thi.ng/geom-poly-utils/center-of-weight" ; import { centroid as _centroid } from "@thi.ng/geom-poly-utils/centroid" ; import type { Vec } from "@thi.ng/vectors" ; import { add } from "@thi.ng/vectors/add" ; import { divN } from "@thi.ng/vectors/divn" ; import { maddN } from "@thi.ng/vectors/maddn" ; import { mixN } from "@thi.ng/vectors/mixn" ; import { mulN } from "@thi.ng/vectors/muln" ; import { set } from "@thi.ng/vectors/set" ; import type { Circle } from "./api/circle.js" ; import type { Group } from "./api/group.js" ; import type { Line } from "./api/line.js" ; import type { Plane } from "./api/plane.js" ; import type { Polygon } from "./api/polygon.js" ; import type { Triangle } from "./api/triangle.js" ; import { bounds } from "./bounds.js" ; import { __dispatch } from "./internal/dispatch.js" ; export const centroid = defmulti < any , Vec | undefined , Vec | undefined > ( __dispatch , { arc : "circle" , aabb : "rect" , ellipse : "circle" , line3 : "line" , points3 : "points" , polyline : "points" , quad : "poly" , sphere : "circle" , text : "circle" , tri3 : "tri" , } , { circle : ( $ , out ? ) => set ( out || [ ] , $ . pos ) , group : ( $ ) => { const b = bounds ( $ ) ; return b ? centroid ( b ) : undefined ; } , line : ( { points } , out ? ) => mixN ( out || [ ] , points [ 0 ] , points [ 1 ] , 0.5 ) , points : ( $ , out ? ) => _centroid ( $ . points , out ) , plane : ( $ , out ? ) => mulN ( out || [ ] , $ . normal , $ . w ) , poly : ( $ , out ? ) => centerOfWeight2 ( $ . points , out ) , rect : ( $ , out ? ) => maddN ( out || [ ] , $ . size , 0.5 , $ . pos ) , tri : ( { points } , out ? ) => divN ( null , add ( null , add ( out || [ ] , points [ 0 ] , points [ 1 ] ) , points [ 2 ] ) , 3 ) , } ) ;