diff --git a/fourDShape.js b/fourDShape.js index 00040d8..b32c1be 100644 --- a/fourDShape.js +++ b/fourDShape.js @@ -3,7 +3,7 @@ import * as THREE from 'three'; const HYPERPLANE = 2; -const NODE_SIZE = 0.08; +const NODE_SIZE = 0.02; const LINK_SIZE = 0.02; diff --git a/main.js b/main.js index ccc3e27..acdcd22 100644 --- a/main.js +++ b/main.js @@ -7,7 +7,7 @@ import { FourDShape } from './fourDShape.js'; import { GUI } from 'lil-gui'; -const DEFAULT_SHAPE = '16-cell'; +const DEFAULT_SHAPE = '120-cell'; // hacky stuff for 4d rotations @@ -120,7 +120,7 @@ const LINK_OPACITY = 0.7; // 2 M 2 (0-2) B const node_ms = [ - new THREE.MeshStandardMaterial( { color: 0x707070 } ), + new THREE.MeshStandardMaterial( { color: 0xc0c0c0 } ), new THREE.MeshStandardMaterial( { color: 0x20dddd } ), new THREE.MeshStandardMaterial( { color: 0xdddd20 } ), new THREE.MeshStandardMaterial( { color: 0xdd20dd } ), diff --git a/permute.js b/permute.js index 53de758..2662f51 100644 --- a/permute.js +++ b/permute.js @@ -86,14 +86,14 @@ function permutations_even(a) { // [ [1, 1, 0, 0 ], [ -1, 1, 0, 0 ], [ 1, -1, 0, 0 ], [-1, -1, 0, 0 ]] // ie don't do it on the zeros -function expand_sign(a) { +function expand_sign(a, label) { const expanded = []; const exv = a.map((v) => v ? [ -v, v ] : [ 0 ]); for( const xv of exv[0] ) { for( const yv of exv[1] ) { for( const zv of exv[2] ) { for( const wv of exv[3] ) { - expanded.push({x: xv, y:yv, z:zv, w:wv}); + expanded.push({label: label, x: xv, y:yv, z:zv, w:wv}); } } } @@ -102,14 +102,86 @@ function expand_sign(a) { } -export function coordinates(a, even=false) { +export function coordinates(a, id0=1, even=false) { const ps = even ? permutations_even(a) : permutations(a); const coords = []; for( const p of ps ) { - const expanded = expand_sign(p); + const expanded = expand_sign(p, 0); coords.push(...expanded); } return coords; } + + +function scale_and_index(nodes, scale) { + let i = 1; + for( const n of nodes ) { + n["id"] = i; + i++; + for( const a of [ 'x', 'y', 'z', 'w' ] ) { + n[a] = scale * n[a]; + } + } + return nodes; +} + + + +function make_120cell_vertices() { + const phi = 0.5 * (1 + Math.sqrt(5)); + const r5 = Math.sqrt(5); + const phi2 = phi * phi; + const phiinv = 1 / phi; + const phi2inv = 1 / phi2; + + const nodes = [ + coordinates([2, 2, 0, 0], 0), + coordinates([r5, 1, 1, 1], 1), + coordinates([phi, phi, phi, phi2inv], 2), + coordinates([phi, phiinv, phiinv, phiinv], 0), + + coordinates([phi2, phi2inv, 1, 0], 1, true), + coordinates([r5, phiinv, phi, 0], 2, true), + coordinates([2, 1, phi, phiinv], 0, true), + ].flat(); + return scale_and_index(nodes, 0.5); +} + + +function dist2(n1, n2) { + return (n1.x - n2.x) ** 2 + (n1.y - n2.y) ** 2 + (n1.z - n2.z) ** 2 + (n1.w - n2.w) ** 2; +} + +function make_120cell_edges(nodes) { + const seen = {}; + const nnodes = nodes.length; + const links = []; + let id = 1; + for( let i = 0; i < nnodes - 1; i++ ) { + const d2 = []; + for( let j = 0; j < nnodes; j++ ) { + d2.push({ d2: dist2(nodes[i], nodes[j]), id: j }); + } + d2.sort((a, b) => b.d2 - a.d2); + const closest = d2.slice(1, 4); + for( const e in closest ) { + const ids = [ nodes[i].id, e.id ]; + ids.sort(); + const fp = ids.join(','); + if( !seen[fp] ) { + seen[fp] = true; + links.push({ id: id, label: 0, source: nodes[i].id, target: e.id }); + id++; + } + } + } + return links; +} + + +const nodes = make_120cell_vertices(); +const links = make_120cell_edges(nodes); + +console.log(links); \ No newline at end of file diff --git a/polytopes.js b/polytopes.js index 9fe189a..8952e93 100644 --- a/polytopes.js +++ b/polytopes.js @@ -1,6 +1,19 @@ import * as PERMUTE from './permute.js'; +function scale_and_index(nodes, scale) { + let i = 1; + for( const n of nodes ) { + n["id"] = i; + i++; + for( const a of [ 'x', 'y', 'z', 'w' ] ) { + n[a] = scale * n[a]; + } + } + return nodes; +} + + export const cell5 = () => { const r5 = Math.sqrt(5); const r2 = Math.sqrt(2) / 2; @@ -232,38 +245,67 @@ export const cell24 = () => { function make_120cell_vertices() { - const phi = 0.5 * (1 + Math.sqrt(5)); 3 - const r5 = Math.sqrt(5); 5 - const phi2 = phi * phi; 4 - const phiinv = 1 / phi; 6 - const phi2inv = 1 / phi2; 7 + const phi = 0.5 * (1 + Math.sqrt(5)); + const r5 = Math.sqrt(5); + const phi2 = phi * phi; + const phiinv = 1 / phi; + const phi2inv = 1 / phi2; const nodes = [ - PERMUTE.coordinates([2, 2, 0, 0]), - PERMUTE.coordinates([r5, 1, 1, 1]), - PERMUTE.coordinates([phi, phi, phi, phi2inv]), - PERMUTE.coordinates([phi, phiinv, phiinv, phiinv]), + PERMUTE.coordinates([2, 2, 0, 0], 0), + PERMUTE.coordinates([r5, 1, 1, 1], 1), + PERMUTE.coordinates([phi, phi, phi, phi2inv], 2), + PERMUTE.coordinates([phi, phiinv, phiinv, phiinv], 0), - PERMUTE.coordinates([phi2, phi2inv, 1, 0], true), - PERMUTE.coordinates([r5, phiinv, phi, 0], true), - PERMUTE.coordinates([2, 1, phi, phiinv], true), + PERMUTE.coordinates([phi2, phi2inv, 1, 0], 1, true), + PERMUTE.coordinates([r5, phiinv, phi, 0], 2, true), + PERMUTE.coordinates([2, 1, phi, phiinv], 0, true), ].flat(); - let i = 1; - for( const n of nodes ) { - n["id"] = i; - i++; - } - return nodes; + return scale_and_index(nodes, 0.5); } +function dist2(n1, n2) { + return (n1.x - n2.x) ** 2 + (n1.y - n2.y) ** 2 + (n1.z - n2.z) ** 2 + (n1.w - n2.w) ** 2; +} + + +function make_120cell_edges(nodes) { + const seen = {}; + const nnodes = nodes.length; + const links = []; + let id = 1; + for( const n1 of nodes ) { + console.log(n1); + const d2 = []; + for( const n2 of nodes ) { + d2.push({ d2: dist2(n1, n2), id: n2.id }); + } + d2.sort((a, b) => a.d2 - b.d2); + const closest = d2.slice(1, 5); + for( const e of closest ) { + const ids = [ n1.id, e.id ]; + ids.sort(); + const fp = ids.join(','); + if( !seen[fp] ) { + seen[fp] = true; + links.push({ id: id, label: 0, source: n1.id, target: e.id }); + id++; + } + } + } + return links; +} + export const cell120 = () => { const nodes = make_120cell_vertices(); - + const links = make_120cell_edges(nodes); return { nodes: nodes, - links: [] + links: links } } + +const nodes = cell120(); \ No newline at end of file