77 lines
2.6 KiB
TypeScript
77 lines
2.6 KiB
TypeScript
export const PART_TYPES = [
|
|
{ type: "torso", label: "躯干", bilateral: false },
|
|
{ type: "head", label: "头", bilateral: false },
|
|
{ type: "waist", label: "腰", bilateral: false },
|
|
{ type: "arm", label: "臂", bilateral: true },
|
|
{ type: "hand", label: "手", bilateral: true },
|
|
{ type: "leg", label: "足", bilateral: true },
|
|
{ type: "foot", label: "脚", bilateral: true },
|
|
{ type: "tail", label: "尾", bilateral: false },
|
|
] as const;
|
|
export type PartType = (typeof PART_TYPES)[number]["type"];
|
|
export type PartSide = "0" | "l" | "r";
|
|
export interface PartIdentity {
|
|
type: PartType;
|
|
label: string;
|
|
side: PartSide;
|
|
key: string;
|
|
}
|
|
export const sideLabel = (side: PartSide) =>
|
|
({ "0": "不分左右", l: "左", r: "右" })[side];
|
|
export const PART_CATEGORY_OPTIONS = PART_TYPES.flatMap((entry) =>
|
|
(entry.bilateral ? ["l", "r"] : ["0"]).map((side) => ({
|
|
value: `${entry.type}_${side}`,
|
|
label: `${entry.label} · ${sideLabel(side as PartSide)}`,
|
|
})),
|
|
);
|
|
|
|
function identity(typeName: string, side: string): PartIdentity | null {
|
|
const entry = PART_TYPES.find(
|
|
(item) => item.type === typeName.toLowerCase() || item.label === typeName,
|
|
);
|
|
const normalizedSide = side.toLowerCase();
|
|
if (
|
|
!entry ||
|
|
!(entry.bilateral ? ["l", "r"] : ["0"]).includes(normalizedSide)
|
|
)
|
|
return null;
|
|
return {
|
|
type: entry.type,
|
|
label: entry.label,
|
|
side: normalizedSide as PartSide,
|
|
key: `${entry.type}_${normalizedSide}`,
|
|
};
|
|
}
|
|
|
|
/** Classify the main mechanical filename, never a folder, mesh or description. */
|
|
export function partIdentityFromFileName(
|
|
fileName: string,
|
|
): PartIdentity | null {
|
|
const match = /^([^_]+)_([RL0])_(.+)\.(urdf|step|stp)$/i.exec(fileName);
|
|
if (!match || !match[3].trim()) return null;
|
|
return identity(match[1], match[2]);
|
|
}
|
|
|
|
/** Recognize the twelve declared interface names; preserve the original frame name. */
|
|
export function interfaceIdentityFromLink(
|
|
linkName: string,
|
|
): PartIdentity | null {
|
|
const match =
|
|
/^LINK_(TORSO|HEAD|WAIST|ARM|HAND|LEG|FOOT|TAIL)_([RL0])$/i.exec(linkName);
|
|
return match ? identity(match[1], match[2]) : null;
|
|
}
|
|
|
|
/** URDF and STEP may have different descriptions, but must identify the same part. */
|
|
export function partIdentityFromSources(
|
|
urdfName?: string,
|
|
stepName?: string,
|
|
): PartIdentity | null {
|
|
const urdf = urdfName ? partIdentityFromFileName(urdfName) : null;
|
|
const step = stepName ? partIdentityFromFileName(stepName) : null;
|
|
if (urdf && step && urdf.key !== step.key)
|
|
throw new Error(
|
|
"STEP 与 URDF 文件名的部位或左右标识不一致,请核对后再保存。",
|
|
);
|
|
return urdf || step;
|
|
}
|