VEHwang's picture
Upload CoCogni4D-Bench (questions, media, scripts, render records, code) (part 4)
9d57416 verified
Raw History Blame Contribute Delete
7.56 kB
import {useEffect, useMemo, useRef} from 'react';
import type {PointerEvent, MouseEvent} from 'react';
import type {SegmentView, Span} from './contract.ts';
import type {ReviewClock} from './clock.ts';
import {actorStyle, KIND_LABELS, shapePath} from './visual.ts';
import './motion-timelines.css';
type MotionClass = 'moving' | 'slow' | 'still';
type MotionRun = {start: number; end: number; cls: MotionClass};
export function motionRuns(t_s: number[], speed: number[], moving = 0.35, still = 0.01): MotionRun[] {
const runs: MotionRun[] = [];
for (let i = 0; i < t_s.length - 1 && i < speed.length; i++) {
const cls = speed[i] >= moving ? 'moving' : speed[i] <= still ? 'still' : 'slow';
const previous = runs[runs.length - 1];
if (previous?.cls === cls) previous.end = t_s[i + 1];
else runs.push({start: t_s[i], end: t_s[i + 1], cls});
}
return runs;
}
export function speedPoints(t_s: number[], speed: number[]): [number, number][] {
return t_s.map((t, i) => [t, speed[i]]);
}
export function MotionTimelines({view, clock, visible, range, setRange, onRange, highlight}: {
view: SegmentView; clock: ReviewClock; visible: Set<string>;
range: Span | null; setRange: (range: Span) => void; onRange: (range: Span) => void; highlight?: Span | null;
}) {
const root = useRef<HTMLElement>(null), span = clock.end - clock.start;
const selecting = useRef<{x: number; rect: DOMRect; start: number} | null>(null), suppressClick = useRef(false);
const tracks = useMemo(() => view.actors.filter(a => visible.has(a.id)).map(actor => {
const track = view.tracks[actor.id];
return {actor, style: actorStyle(actor), runs: motionRuns(track.t_s, track.speed_mps), points: speedPoints(track.t_s, track.speed_mps)};
}), [view, visible]);
let maximum = 1;
for (const track of tracks) for (const [, speed] of track.points) maximum = Math.max(maximum, speed);
const ymax = Math.ceil(maximum / 0.5) * 0.5;
const x = (time: number) => (time - clock.start) / span * 100;
const y = (speed: number) => 160 * (1 - speed / ymax);
useEffect(() => clock.subscribe(({time}) => {
root.current?.querySelectorAll<HTMLElement>('[data-motion-cursor]').forEach(node => {node.style.left = `${(time - clock.start) / span * 100}%`;});
}), [clock, span]);
const seek = (event: MouseEvent<HTMLDivElement>) => {
if (suppressClick.current) {suppressClick.current = false; return;}
const rect = event.currentTarget.getBoundingClientRect();
if (!Number.isFinite(rect.width) || rect.width <= 0) return;
const time = clock.start + (event.clientX - rect.left) / rect.width * span;
if (!Number.isFinite(time)) return;
clock.setPlaying(false); clock.seek(time);
};
const endTime = (event: PointerEvent<HTMLDivElement>, rect: DOMRect) => clock.start + Math.max(0, Math.min(1, (event.clientX - rect.left) / rect.width)) * span;
const interaction = {
onClick: seek,
onPointerDown: (e: PointerEvent<HTMLDivElement>) => {const rect = e.currentTarget.getBoundingClientRect(); if (rect.width <= 0) return; clock.setPlaying(false); selecting.current = {x: e.clientX, rect, start: endTime(e, rect)}; e.currentTarget.setPointerCapture(e.pointerId);},
onPointerMove: (e: PointerEvent<HTMLDivElement>) => {const start = selecting.current; if (!start || Math.abs(e.clientX - start.x) < 3) return; const end = endTime(e, start.rect); setRange([Math.min(start.start, end), Math.max(start.start, end)]);},
onPointerUp: (e: PointerEvent<HTMLDivElement>) => {const start = selecting.current; selecting.current = null; if (!start || Math.abs(e.clientX - start.x) < 3) return; const end = endTime(e, start.rect); const selected: Span = [Math.min(start.start, end), Math.max(start.start, end)]; suppressClick.current = true; setRange(selected); onRange(selected);},
onPointerCancel: () => {selecting.current = null;},
onKeyDown: (e: React.KeyboardEvent<HTMLDivElement>) => {if (e.key === 'ArrowRight' || e.key === 'ArrowLeft') {e.preventDefault(); clock.setPlaying(false); clock.step(e.key === 'ArrowRight' ? 1 : -1);}},
};
const overlays = () => <>
<div className="motion-cursor" data-motion-cursor />
{highlight && <div className="motion-selection motion-question" style={{left: `${x(highlight[0])}%`, width: `${(highlight[1] - highlight[0]) / span * 100}%`}} />}
{range && <div className="motion-selection" data-motion-selected-span={range.join(',')} style={{left: `${x(range[0])}%`, width: `${(range[1] - range[0]) / span * 100}%`}} />}
<div className="motion-ticks">{Array.from({length: 7}, (_, i) => <span key={i} style={{left: `${i / 6 * 100}%`}}>{(clock.start + i / 6 * span).toFixed(0)}s</span>)}</div>
</>;
return <section className="motion-timelines" ref={root}>
<h2>运动与速度</h2>
<h3>运动 / 停止</h3>
<p className="motion-key">实色 运动(≥0.35 m/s)· 半透明 慢移 · 淡色 停止(≤0.01 m/s)</p>
<div className="motion-grid">
<div className="motion-names">{tracks.map(({actor, style}) => <div key={actor.id}>
<svg width="18" height="18" viewBox="-1.4 -1.4 2.8 2.8" aria-hidden="true"><path d={shapePath(style.shape)} fill={style.color} /></svg>
<span>{actor.id} <small>{KIND_LABELS[actor.kind] || actor.kind}</small></span>
</div>)}</div>
<div className="motion-plot" {...interaction} role="slider" tabIndex={0} aria-label="运动 / 停止时间轴" aria-valuemin={clock.start} aria-valuemax={clock.end}>
{tracks.map(({actor, style, runs}) => <div key={actor.id} className="motion-row" data-motion-actor={actor.id}>
{runs.map((run, i) => <div key={i} className="motion-run" data-motion-class={run.cls} style={{left: `${x(run.start)}%`, width: `${(run.end - run.start) / span * 100}%`, background: style.color, opacity: {moving: 1, slow: 0.45, still: 0.12}[run.cls]}} title={`${actor.id} · ${{moving: '运动', slow: '慢移', still: '停止'}[run.cls]} · ${run.start.toFixed(2)}–${run.end.toFixed(2)} 秒`} />)}
</div>)}
{overlays()}
</div>
</div>
<h3>速度 m/s</h3>
<div className="motion-grid">
<div className="speed-labels">{Array.from({length: ymax / 0.5 + 1}, (_, i) => i * 0.5).map(value => <span key={value} style={{top: `${y(value)}px`}}>{value.toFixed(1)}</span>)}</div>
<div className="motion-plot speed-plot" {...interaction} role="slider" tabIndex={0} aria-label="速度时间轴,观察者用虚线" aria-valuemin={clock.start} aria-valuemax={clock.end}>
<svg viewBox="0 0 1000 160" preserveAspectRatio="none" aria-label="各实体速度折线">
{Array.from({length: ymax / 0.5 + 1}, (_, i) => i * 0.5).map(value => <line key={value} x1="0" x2="1000" y1={y(value)} y2={y(value)} className="speed-grid" vectorEffect="non-scaling-stroke" />)}
<line x1="0" x2="1000" y1={y(0.35)} y2={y(0.35)} className="speed-threshold" strokeDasharray="4 3" vectorEffect="non-scaling-stroke" />
{tracks.map(({actor, style, points}) => <polyline key={actor.id} data-speed-actor={actor.id} points={points.map(([time, speed]) => `${x(time) * 10},${y(speed)}`).join(' ')} fill="none" stroke={style.color} strokeWidth="1.5" strokeDasharray={actor.kind.startsWith('observer') ? '4 3' : undefined} vectorEffect="non-scaling-stroke"><title>{`${actor.id} · ${KIND_LABELS[actor.kind] || actor.kind}`}</title></polyline>)}
</svg>
<span className="speed-threshold-label" style={{top: `${y(0.35)}px`}}>运动门槛 0.35</span>
{overlays()}
</div>
</div>
</section>;
}