fourdjs/polytopes.js

280 lines
5.7 KiB
JavaScript
Raw Normal View History

import * as PERMUTE from './permute.js';
2023-07-27 09:23:26 +00:00
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 dist2(n1, n2) {
return (n1.x - n2.x) ** 2 + (n1.y - n2.y) ** 2 + (n1.z - n2.z) ** 2 + (n1.w - n2.w) ** 2;
}
2023-07-31 07:23:57 +00:00
function auto_detect_edges(nodes, neighbours, debug=false) {
const seen = {};
const nnodes = nodes.length;
const links = [];
let id = 1;
for( const n1 of nodes ) {
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, neighbours + 1);
2023-07-31 07:23:57 +00:00
if( debug ) {
console.log(`closest = ${closest.length}`);
console.log(closest);
}
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++;
}
}
}
2023-07-31 07:23:57 +00:00
if( debug ) {
console.log(`Found ${links.length} edges`)
}
return links;
}
// too small and simple to calculate
2023-07-27 09:23:26 +00:00
export const cell5 = () => {
const r5 = Math.sqrt(5);
const r2 = Math.sqrt(2) / 2;
return {
nodes: [
{id:1, x: r2, y: r2, z: r2, w: -r2 / r5 },
{id:2, x: r2, y: -r2, z: -r2, w: -r2 / r5 },
{id:3, x: -r2, y: r2, z: -r2, w: -r2 / r5 },
{id:4, x: -r2, y: -r2, z: r2, w: -r2 / r5 },
{id:5, x: 0, y: 0, z: 0, w: 4 * r2 / r5 },
],
links: [
{ id:1, source:1, target: 2},
{ id:2, source:1, target: 3},
{ id:3, source:1, target: 4},
{ id:4, source:1, target: 5},
{ id:5, source:2, target: 3},
{ id:6, source:2, target: 4},
{ id:7, source:2, target: 5},
{ id:8, source:3, target: 4},
{ id:9, source:3, target: 5},
{ id:10, source:4, target: 5},
2023-07-29 06:23:08 +00:00
],
geometry: {
node_size: 0.02,
link_size: 0.02
2023-07-29 06:23:08 +00:00
}
};
};
export const cell16 = () => {
let nodes = PERMUTE.coordinates([1, 1, 1, 1], 0);
nodes = nodes.filter((n) => n.x * n.y * n.z * n.w > 0);
scale_and_index(nodes, 0.75);
const links = auto_detect_edges(nodes, 6);
return {
nodes: nodes,
2023-07-29 06:23:08 +00:00
links: links,
geometry: {
node_size: 0.02,
link_size: 0.02
2023-07-29 06:23:08 +00:00
}
};
};
export const tesseract = () => {
const nodes = scale_and_index(PERMUTE.coordinates([1, 1, 1, 1], 0), Math.sqrt(2) / 2);
const links = auto_detect_edges(nodes, 4);
2023-07-24 23:34:53 +00:00
return {
nodes: nodes,
2023-07-29 06:23:08 +00:00
links: links,
geometry: {
node_size: 0.02,
link_size: 0.02
2023-07-29 06:23:08 +00:00
}
};
}
export const cell24 = () => {
const nodes = scale_and_index(PERMUTE.coordinates([0, 0, 1, 1], 0), 1);
const links = auto_detect_edges(nodes, 6);
2023-07-24 23:34:53 +00:00
return {
nodes: nodes,
2023-07-29 06:23:08 +00:00
links: links,
geometry: {
node_size: 0.02,
link_size: 0.02
}
2023-07-24 23:34:53 +00:00
};
2023-07-24 00:39:11 +00:00
}
2023-07-28 07:06:42 +00:00
// face detection for the 120-cell
// NOTE: all of these return node ids, not nodes
// return all the links which connect to a node
function nodes_links(links, nodeid) {
return links.filter((l) => l.source === nodeid || l.target === nodeid);
}
// filter to remove a link to a given id from a set of links
function not_to_this(link, nodeid) {
return !(link.source === nodeid || link.target === nodeid);
}
// given nodes n1, n2, return all neighbours of n2 which are not n1
function unmutuals(links, n1id, n2id) {
const nlinks = nodes_links(links, n2id).filter((l) => not_to_this(l, n1id));
return nlinks.map((l) => {
if( l.source === n2id ) {
return l.target;
} else {
return l.source;
}
})
}
function fingerprint(ids) {
const sids = [...ids];
sids.sort();
return sids.join(',');
}
function auto_120cell_faces(links) {
const faces = [];
const seen = {};
let id = 1;
for( const edge of links ) {
const v1 = edge.source;
const v2 = edge.target;
const n1 = unmutuals(links, v2, v1);
const n2 = unmutuals(links, v1, v2);
const shared = [];
for( const a of n1 ) {
const an = unmutuals(links, v1, a);
for( const d of n2 ) {
const dn = unmutuals(links, v2, d);
for( const x of an ) {
for( const y of dn ) {
if( x == y ) {
shared.push([v1, a, x, d, v2])
}
}
}
}
}
if( shared.length !== 3 ) {
console.log(`Bad shared faces for ${edge.id} ${v1} ${v2}`);
}
for( const face of shared ) {
const fp = fingerprint(face);
if( !seen[fp] ) {
faces.push({ id: id, nodes: face });
id++;
seen[fp] = true;
}
}
}
return faces;
}
2023-07-27 09:23:26 +00:00
2023-07-27 21:18:27 +00:00
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 = [
PERMUTE.coordinates([0, 0, 2, 2], 0),
PERMUTE.coordinates([1, 1, 1, r5], 0),
PERMUTE.coordinates([phi, phi, phi, phi2inv], 0),
2023-07-27 21:18:27 +00:00
PERMUTE.coordinates([phiinv, phiinv, phiinv, phi2], 0),
PERMUTE.coordinates([phi2, phi2inv, 1, 0], 0, true),
PERMUTE.coordinates([r5, phiinv, phi, 0], 0, true),
2023-07-27 21:18:27 +00:00
PERMUTE.coordinates([2, 1, phi, phiinv], 0, true),
].flat();
return scale_and_index(nodes, 0.5);
}
export const cell120 = () => {
const nodes = make_120cell_vertices();
2023-07-27 21:18:27 +00:00
const links = auto_detect_edges(nodes, 4);
2023-07-28 07:06:42 +00:00
const faces = auto_120cell_faces(links);
return {
nodes: nodes,
2023-07-28 07:06:42 +00:00
links: links,
2023-07-29 06:23:08 +00:00
geometry: {
node_size: 0.02,
link_size: 0.02
2023-07-31 07:33:40 +00:00
},
2023-07-28 07:06:42 +00:00
faces: faces
}
}
2023-07-27 09:23:26 +00:00
2023-07-27 21:18:27 +00:00
function make_600cell_vertices() {
const phi = 0.5 * (1 + Math.sqrt(5));
const nodes = [
PERMUTE.coordinates([0, 0, 0, 2], 0),
PERMUTE.coordinates([1, 1, 1, 1], 0),
2023-07-27 21:18:27 +00:00
PERMUTE.coordinates([phi, 1, 1 / phi, 0], 0, true)
2023-07-27 21:18:27 +00:00
].flat();
return scale_and_index(nodes, 0.75);
}
export const cell600 = () => {
const nodes = make_600cell_vertices();
2023-07-31 07:24:26 +00:00
const links = auto_detect_edges(nodes, 12);
2023-07-27 21:18:27 +00:00
return {
nodes: nodes,
2023-07-29 06:23:08 +00:00
links: links,
geometry: {
node_size: 0.02,
link_size: 0.02
2023-07-29 06:23:08 +00:00
}
2023-07-27 21:18:27 +00:00
}
}