Skip to content

Commit

Permalink
Prototype of a particle visualizer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhuighuy committed Feb 7, 2025
1 parent f88f736 commit c6d73cb
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 14 deletions.
2 changes: 2 additions & 0 deletions source/titfront/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"react-feather": "^2.0.10",
"react-icons": "^5.4.0",
"tailwind-merge": "^3.0.1",
"three": "^0.173.0",
"zod": "^3.24.1"
},
"devDependencies": {
Expand All @@ -34,6 +35,7 @@
"@types/node": "^22.10.10",
"@types/react": "^19.0.8",
"@types/react-dom": "^19.0.3",
"@types/three": "^0.173.0",
"@vitejs/plugin-react": "^4.3.4",
"@vitest/coverage-v8": "3.0.4",
"async-mutex": "^0.5.0",
Expand Down
8 changes: 4 additions & 4 deletions source/titfront/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import {
import { BottomMenu, LeftMenu, MenuItem } from "./MainMenu";
import { PythonShell } from "./PythonShell";
import { TreeView, MockData } from "./TreeView";
import { Viewer } from "./Viewer";
import { ViewerComponent } from "./Viewer";

Check warning on line 19 in source/titfront/src/components/App.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/App.tsx#L16-L19

Added lines #L16 - L19 were not covered by tests

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

export const App: FC = () => {
const leftIconSize = 24;
const bottomIconSize = 16;
return (
<div className="h-screen w-screen flex flex-row select-none text-sm">
<div className="h-screen w-screen flex flex-row select-none text-sm transition-none">
<LeftMenu>
<MenuItem
name="Configuration"
Expand All @@ -49,8 +49,8 @@ export const App: FC = () => {
group={1}
/>
</LeftMenu>
<div className="flex-1 flex flex-col">
<Viewer />
<div className="flex-grow flex flex-col">
<ViewerComponent />
<BottomMenu>
<MenuItem
name="Python shell"
Expand Down
7 changes: 2 additions & 5 deletions source/titfront/src/components/Python.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ export type PyRunCode = (expression: string, onResponse: PyCallback) => void;
* @returns A function to run Python code.
*/
export function usePython(): PyRunCode {
const runCode = useContext(PyConnectionContext);
assert(runCode !== null, "runCode is null!");
return runCode;
return useContext(PyConnectionContext)!;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -116,8 +114,7 @@ export const PyConnectionProvider: FC<{ children: ReactNode }> = ({
);

// Invoke the callback.
const callback = pendingRequests.current.get(requestID);
assert(callback !== undefined, `No callback for request ${requestID}`);
const callback = pendingRequests.current.get(requestID)!;
if (status === "success") {
callback(result);
} else {
Expand Down
137 changes: 137 additions & 0 deletions source/titfront/src/components/ViewOrientation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *\
* Part of the Tit Solver project, under the MIT License.
* See /LICENSE.md for license information. SPDX-License-Identifier: MIT
\* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

import { FC, useEffect, useState } from "react";
import { Euler, Quaternion, Vector3 } from "three";

Check warning on line 7 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L6-L7

Added lines #L6 - L7 were not covered by tests

import { useViewer } from "./Viewer";
import { cn } from "../utils";

Check warning on line 10 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L9-L10

Added lines #L9 - L10 were not covered by tests

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

type Axis = "x" | "y" | "z";
type AxisSpec = {
unit: Vector3;
colorCn: string;
darkerColorCn: string;
borderColorCn: string;
};
const axes: Record<Axis, AxisSpec> = {
x: {
unit: new Vector3(+1, 0, 0),
colorCn: "bg-red-500/50 hover:bg-red-500/75",
darkerColorCn: "bg-red-500/25 hover:bg-red-500/50",
borderColorCn: "border-red-500/50",
},
y: {
unit: new Vector3(0, +1, 0),
colorCn: "bg-green-500/50 hover:bg-green-500/75",
darkerColorCn: "bg-green-500/25 hover:bg-green-500/50",
borderColorCn: "border-green-500/50",
},
z: {
unit: new Vector3(0, 0, -1),
colorCn: "bg-blue-500/50 hover:bg-blue-500/75",
darkerColorCn: "bg-blue-500/25 hover:bg-blue-500/50",
borderColorCn: "border-blue-500/50",
},
};

Check warning on line 40 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L21-L40

Added lines #L21 - L40 were not covered by tests

export const Orientation: FC = () => {
const viewer = useViewer();
const [rotation, setRotation] = useState<Euler | null>(null);

Check warning on line 44 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L42-L44

Added lines #L42 - L44 were not covered by tests

useEffect(() => {
const controls = viewer?.controls;
if (!controls) return;
const setRotationFromControls = () =>
setRotation(
new Euler(
viewer.controls.getPolarAngle() - Math.PI / 2,
viewer.controls.getAzimuthalAngle(),
0
)
);
setRotationFromControls();
controls.addEventListener("change", setRotationFromControls);
return () => {
controls.removeEventListener("change", setRotationFromControls);
};
}, [viewer]);

Check warning on line 62 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L46-L62

Added lines #L46 - L62 were not covered by tests

if (!viewer || !rotation) return null;

Check warning on line 64 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L64

Added line #L64 was not covered by tests

const q = new Quaternion().setFromEuler(rotation);
const OrientAxis = (axis: "x" | "y" | "z") => {
const { unit, colorCn, darkerColorCn, borderColorCn } = axes[axis];
const e = new Vector3().copy(unit).applyQuaternion(q);
e.y *= -1;

Check warning on line 70 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L66-L70

Added lines #L66 - L70 were not covered by tests

const baseZ = 20;
const diamPx = 30;
const distPx = 45;
const angleRad = Math.atan2(e.y, e.x);
const angledDistPx = distPx * Math.hypot(e.x, e.y) - diamPx / 2;

Check warning on line 76 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L72-L76

Added lines #L72 - L76 were not covered by tests

const Circle = (sign: number) => (
<button
className={cn(
"absolute flex items-center justify-center",
"rounded-full",
sign > 0
? cn("text-gray-700 font-black", colorCn)
: cn("border-4", darkerColorCn, borderColorCn)
)}
style={{
width: diamPx,
height: diamPx,
zIndex: Math.round(baseZ * (1 - sign * e.z)),
transform: `

Check warning on line 91 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L78-L91

Added lines #L78 - L91 were not covered by tests
translate(-50%, -50%)
translate(${sign * e.x * distPx}px, ${sign * e.y * distPx}px)

Check warning on line 93 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L93

Added line #L93 was not covered by tests
`,
}}

Check warning on line 95 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L95

Added line #L95 was not covered by tests
>
{sign > 0 ? axis.toUpperCase() : ""}
</button>

Check warning on line 98 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L97-L98

Added lines #L97 - L98 were not covered by tests
);

const Line = (sign: number) =>
Math.abs(angledDistPx) > 2 && (
<div
className={cn("absolute", colorCn)}
style={{
width: angledDistPx,
height: 4,
zIndex: Math.round(baseZ * (1 - sign * 0.5 * e.z)),
transform: `

Check warning on line 109 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L101-L109

Added lines #L101 - L109 were not covered by tests
translate(-50%, -50%)
rotate(${angleRad}rad)
translate(${sign * 50}%, 0)

Check warning on line 112 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L111-L112

Added lines #L111 - L112 were not covered by tests
`,
}}
/>

Check warning on line 115 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L114-L115

Added lines #L114 - L115 were not covered by tests
);

return (
<>
{Circle(+1)}
{Line(+1)}
{Line(-1)}
{Circle(-1)}
</>

Check warning on line 124 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L118-L124

Added lines #L118 - L124 were not covered by tests
);
};

Check warning on line 126 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L126

Added line #L126 was not covered by tests

return (
<div className="absolute top-20 right-20">
{OrientAxis("x")}
{OrientAxis("y")}
{OrientAxis("z")}
</div>

Check warning on line 133 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L128-L133

Added lines #L128 - L133 were not covered by tests
);
};

Check warning on line 135 in source/titfront/src/components/ViewOrientation.tsx

View check run for this annotation

Codecov / codecov/patch

source/titfront/src/components/ViewOrientation.tsx#L135

Added line #L135 was not covered by tests

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Loading

0 comments on commit c6d73cb

Please sign in to comment.