|
1 | | -import { useMemo, useState } from 'react'; |
| 1 | +import { useCallback, useMemo, useState } from 'react'; |
2 | 2 | import { AssignTrackerRequestT, BodyPart, RpcMessage } from 'solarxr-protocol'; |
3 | 3 | import { FlatDeviceTracker } from '@/hooks/app'; |
4 | 4 | import { useOnboarding } from '@/hooks/onboarding'; |
5 | 5 | import { useTrackers } from '@/hooks/tracker'; |
6 | 6 | import { useWebsocketAPI } from '@/hooks/websocket-api'; |
7 | | -import { MountingOrientationDegreesToQuatT } from '@/maths/quaternion'; |
| 7 | +import { |
| 8 | + MountingOrientationDegreesToQuatT, |
| 9 | + QuaternionFromQuatT, |
| 10 | + similarQuaternions, |
| 11 | +} from '@/maths/quaternion'; |
8 | 12 | import { Button } from '@/components/commons/Button'; |
9 | 13 | import { TipBox } from '@/components/commons/TipBox'; |
10 | 14 | import { Typography } from '@/components/commons/Typography'; |
@@ -62,10 +66,31 @@ export function ManualMountingPage() { |
62 | 66 | setSelectRole(BodyPart.NONE); |
63 | 67 | }; |
64 | 68 |
|
| 69 | + const getCurrRotation = useCallback( |
| 70 | + (role: BodyPart) => { |
| 71 | + if (role === BodyPart.NONE) return undefined; |
| 72 | + |
| 73 | + const trackers = trackerPartGrouped[role] || []; |
| 74 | + const [mountingOrientation, ...orientation] = trackers |
| 75 | + .map((td) => td.tracker.info?.mountingOrientation) |
| 76 | + .filter((orientation) => !!orientation) |
| 77 | + .map((orientation) => QuaternionFromQuatT(orientation)); |
| 78 | + |
| 79 | + const identicalOrientations = |
| 80 | + mountingOrientation !== undefined && |
| 81 | + orientation.every((quat) => |
| 82 | + similarQuaternions(quat, mountingOrientation) |
| 83 | + ); |
| 84 | + return identicalOrientations ? mountingOrientation : undefined; |
| 85 | + }, |
| 86 | + [trackerPartGrouped] |
| 87 | + ); |
| 88 | + |
65 | 89 | return ( |
66 | 90 | <> |
67 | 91 | <MountingSelectionMenu |
68 | 92 | bodyPart={selectedRole} |
| 93 | + currRotation={getCurrRotation(selectedRole)} |
69 | 94 | isOpen={selectedRole !== BodyPart.NONE} |
70 | 95 | onClose={() => setSelectRole(BodyPart.NONE)} |
71 | 96 | onDirectionSelected={onDirectionSelected} |
|
0 commit comments