@cesarvenzac/figures 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,34 @@
1
+ import { type Mat3, type Vec3 } from "./figure-space.ts";
2
+ /**
3
+ * The cube figure's Rubik's turns, as it thinks: 27 cells, each holding the turn its cubelet has
4
+ * taken from where it started, so the shader can find any point's place in the solved cube and
5
+ * draw its colour and its eyes there (they travel with their cubelets). Pure.
6
+ *
7
+ * A move turns one layer (all cells with the same x, y or z: −1, 0 or 1) a quarter or a half
8
+ * round its axis. Cells are numbered (x + 1) + (y + 1)·3 + (z + 1)·9.
9
+ */
10
+ export type Move = {
11
+ axis: 0 | 1 | 2;
12
+ layer: -1 | 0 | 1;
13
+ turns: 1 | -1 | 2;
14
+ };
15
+ /** Each cell's cubelet's turn from where it started, column-major. */
16
+ export type Cube = Mat3[];
17
+ export declare const cellIndex: (x: number, y: number, z: number) => number;
18
+ export declare const cellAt: (index: number) => Vec3;
19
+ export declare const solved: () => Cube;
20
+ /** The cube after a move: each cell of the layer passes its cubelet on to where the turn takes it, turned with it. */
21
+ export declare function applyMove(cube: Cube, move: Move): Cube;
22
+ /** The move that undoes `move`. */
23
+ export declare const inverse: (move: Move) => Move;
24
+ /** Whether every cubelet is back where it started. */
25
+ export declare const isSolved: (cube: Cube) => boolean;
26
+ /**
27
+ * A few moves to scramble the cube by, the same for the same seed: never the same axis twice in a
28
+ * row (two turns of one axis read as one), quarter turns either way.
29
+ */
30
+ export declare function scramble(seed: number, count: number): Move[];
31
+ /** A scramble and its solution, one after the other: the cube thinks, then puts itself back. */
32
+ export declare const thinkingMoves: (seed: number, count?: number) => Move[];
33
+ /** The cells as the shader takes them: 27 column-major 3×3s. */
34
+ export declare function cellsOf(cube: Cube, into?: Float32Array<ArrayBuffer>): Float32Array<ArrayBuffer>;
package/dist/rubik.js ADDED
@@ -0,0 +1,54 @@
1
+ import { apply, IDENTITY, multiply, turnAbout } from "./figure-space.js";
2
+ export const cellIndex = (x, y, z) => x + 1 + (y + 1) * 3 + (z + 1) * 9;
3
+ export const cellAt = (index) => [(index % 3) - 1, (Math.floor(index / 3) % 3) - 1, Math.floor(index / 9) - 1];
4
+ export const solved = () => Array.from({ length: 27 }, () => [...IDENTITY]);
5
+ /** A quarter or half turn has whole numbers in it: rounded, so turns never drift. */
6
+ const whole = (values) => values.map((value) => Math.round(value) || 0);
7
+ /** The cube after a move: each cell of the layer passes its cubelet on to where the turn takes it, turned with it. */
8
+ export function applyMove(cube, move) {
9
+ const turn = whole(turnAbout(move.axis, (move.turns * Math.PI) / 2));
10
+ const next = cube.slice();
11
+ cube.forEach((orientation, index) => {
12
+ const cell = cellAt(index);
13
+ if (cell[move.axis] !== move.layer)
14
+ return;
15
+ const [x, y, z] = whole(apply(turn, cell));
16
+ next[cellIndex(x, y, z)] = whole(multiply(turn, orientation));
17
+ });
18
+ return next;
19
+ }
20
+ /** The move that undoes `move`. */
21
+ export const inverse = (move) => ({ ...move, turns: move.turns === 2 ? 2 : (-move.turns) });
22
+ /** Whether every cubelet is back where it started. */
23
+ export const isSolved = (cube) => cube.every((orientation) => orientation.every((value, i) => value === IDENTITY[i]));
24
+ /**
25
+ * A few moves to scramble the cube by, the same for the same seed: never the same axis twice in a
26
+ * row (two turns of one axis read as one), quarter turns either way.
27
+ */
28
+ export function scramble(seed, count) {
29
+ let state = seed >>> 0 || 1;
30
+ const next = () => {
31
+ state = (state * 1664525 + 1013904223) >>> 0;
32
+ return state / 4294967296;
33
+ };
34
+ const moves = [];
35
+ let last = -1;
36
+ while (moves.length < count) {
37
+ const axis = Math.floor(next() * 3);
38
+ if (axis === last)
39
+ continue;
40
+ last = axis;
41
+ moves.push({ axis, layer: (Math.floor(next() * 3) - 1), turns: next() < 0.5 ? 1 : -1 });
42
+ }
43
+ return moves;
44
+ }
45
+ /** A scramble and its solution, one after the other: the cube thinks, then puts itself back. */
46
+ export const thinkingMoves = (seed, count = 4) => {
47
+ const moves = scramble(seed, count);
48
+ return [...moves, ...[...moves].reverse().map(inverse)];
49
+ };
50
+ /** The cells as the shader takes them: 27 column-major 3×3s. */
51
+ export function cellsOf(cube, into = new Float32Array(27 * 9)) {
52
+ cube.forEach((orientation, index) => into.set(orientation, index * 9));
53
+ return into;
54
+ }
package/package.json CHANGED
@@ -1,6 +1,75 @@
1
1
  {
2
2
  "name": "@cesarvenzac/figures",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "Small 3D figures for the agents in your interface: a cube, a sphere, a pyramid, a block or a cloud with two eyes, saying what each is doing with their eyes and the way they move.",
5
+ "keywords": [
6
+ "agents",
7
+ "ai-agents",
8
+ "avatar",
9
+ "avatars",
10
+ "character",
11
+ "mascot",
12
+ "bot",
13
+ "figure",
14
+ "status",
15
+ "expression",
16
+ "eyes",
17
+ "3d",
18
+ "webgl",
19
+ "webgl2",
20
+ "shader",
21
+ "raymarching",
22
+ "animation",
23
+ "react"
24
+ ],
25
+ "homepage": "https://cesarvenzac.com/figures",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/cesarvenzac/figures.git"
29
+ },
30
+ "bugs": "https://github.com/cesarvenzac/figures/issues",
31
+ "author": "César Venzac (https://cesarvenzac.com)",
32
+ "license": "MIT",
33
+ "type": "module",
34
+ "sideEffects": false,
35
+ "exports": {
36
+ ".": {
37
+ "types": "./dist/index.d.ts",
38
+ "default": "./dist/index.js"
39
+ },
40
+ "./react": {
41
+ "types": "./dist/react.d.ts",
42
+ "default": "./dist/react.js"
43
+ },
44
+ "./package.json": "./package.json"
45
+ },
46
+ "files": [
47
+ "dist"
48
+ ],
49
+ "scripts": {
50
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
51
+ "typecheck": "tsc --noEmit",
52
+ "test": "vitest run",
53
+ "check": "sh check/run.sh",
54
+ "prepublishOnly": "npm run build"
55
+ },
56
+ "peerDependencies": {
57
+ "react": ">=18"
58
+ },
59
+ "peerDependenciesMeta": {
60
+ "react": {
61
+ "optional": true
62
+ }
63
+ },
64
+ "devDependencies": {
65
+ "@types/react": "^19.3.0",
66
+ "@types/react-dom": "^19.3.0",
67
+ "react": "^19.2.8",
68
+ "react-dom": "^19.2.8",
69
+ "typescript": "^5.9.3",
70
+ "vitest": "^5.0.3"
71
+ },
72
+ "publishConfig": {
73
+ "access": "public"
74
+ }
75
+ }