Compare commits

...

4 Commits

Author SHA1 Message Date
Mike Lynch
cc7f77a5a9 Added a shape with a single link to make testing easier 2025-11-08 17:47:47 +11:00
Mike Lynch
a1fff090fc Committing this before trashing it maybe? 2024-06-09 17:21:16 +10:00
Mike Lynch
6728908d18 Link scaling 2024-05-28 17:57:11 +10:00
Mike Lynch
c8b3f1902a Added TaperedLink class, still very broken but I think this approach
to properly-scaled links is viable
2024-05-28 17:40:30 +10:00
5 changed files with 126 additions and 39 deletions

View File

@ -1,9 +1,12 @@
import * as THREE from 'three'; import * as THREE from 'three';
import { TaperedLink } from './taperedLink.js';
const HYPERPLANE = 2.0; const HYPERPLANE = 2.0;
const W_FORESHORTENING = 0.04; const W_FORESHORTENING = 0.04;
class FourDShape extends THREE.Group { class FourDShape extends THREE.Group {
constructor(node_ms, link_ms, face_ms, structure) { constructor(node_ms, link_ms, face_ms, structure) {
@ -42,45 +45,40 @@ class FourDShape extends THREE.Group {
return sphere; return sphere;
} }
makeLink(material, link) { makeLink(basematerial, link) {
const n1 = this.nodes3[link.source]; const n1 = this.nodes3[link.source];
const n2 = this.nodes3[link.target]; const n2 = this.nodes3[link.target];
const s1 = n1.scale; const s1 = this.link_scale * n1.scale;
const s2 = n2.scale; const s2 = this.link_scale * n2.scale;
const length = n1.v3.distanceTo(n2.v3); const edge = new TaperedLink(basematerial, n1, n2, s1, s2);
const centre = new THREE.Vector3(); this.add( edge );
centre.lerpVectors(n1.v3, n2.v3, 0.5);
const geometry = new THREE.CylinderGeometry(
this.link_scale * s2, this.link_scale * s1, 1,
16, 1, true
);
const cyl = new THREE.Mesh(geometry, material);
const edge = new THREE.Group();
edge.add(cyl);
edge.position.copy(centre);
edge.scale.copy(new THREE.Vector3(1, 1, length));
edge.lookAt(n2.v3);
cyl.rotation.x = Math.PI / 2.0;
this.add(edge);
return edge; return edge;
// const length = n1.v3.distanceTo(n2.v3);
// const centre = nfew THREE.Vector3();
// centre.lerpVectors(n1.v3, n2.v3, 0.5);
// const edge = new TaperedLink(basematerial);
// edge.update(s1, s2, length);
// edge.position.copy(centre);
// edge.lookAt(n2.v3);
// edge.children[0].rotation.x = Math.PI / 2.0;
// this.add(edge);
// return edge;
} }
updateLink(link, links_show) { updateLink(link, links_show) {
const n1 = this.nodes3[link.source]; const n1 = this.nodes3[link.source];
const n2 = this.nodes3[link.target]; const n2 = this.nodes3[link.target];
const s1 = n1.scale; const s1 = this.link_scale * n1.scale;
const s2 = n2.scale; const s2 = this.link_scale * n2.scale;
const length = n1.v3.distanceTo(n2.v3); link.object.update(n1, n2, s1, s2);
const centre = new THREE.Vector3(); // const length = n1.v3.distanceTo(n2.v3);
centre.lerpVectors(n1.v3, n2.v3, 0.5); // const centre = new THREE.Vector3();
// take the average of the ends as the thickness - as a workaround, // centre.lerpVectors(n1.v3, n2.v3, 0.5);
// because I haven't worked out how to reshape tapered links without // link.object.update(s1, s2, length);
// having to reassign a new geometry to every link // link.object.position.copy(n1.v3);
const link_mean = this.link_scale * (s1 + s2) * 0.5; // link.object.lookAt(n2.v3);
link.object.scale.copy(new THREE.Vector3(link_mean, link_mean, length)); // link.object.children[0].rotation.x = Math.PI / 2.0;
link.object.position.copy(centre);
link.object.lookAt(n2.v3);
link.object.children[0].rotation.x = Math.PI / 2.0;
link.object.visible = (!links_show || link.label in links_show); link.object.visible = (!links_show || link.label in links_show);
} }

10
gui.js
View File

@ -2,12 +2,12 @@ import { GUI } from 'lil-gui';
const DEFAULTS = { const DEFAULTS = {
nodesize: 0.25, nodesize: 4,
nodeopacity: 1, nodeopacity: 1,
linksize: 0.2, linksize: 1.0,
linkopacity: 0.75, linkopacity: 0.75,
link2opacity: 0.75, link2opacity: 0.75,
shape: '120-cell', shape: 'linky',
option: 'none', option: 'none',
visibility: 5, visibility: 5,
inscribed: false, inscribed: false,
@ -72,9 +72,9 @@ class FourDGUI {
}); });
this.gui.add(this.params, 'hyperplane', 0.5, 1 / 0.8); this.gui.add(this.params, 'hyperplane', 0.5, 1 / 0.8);
this.gui.add(this.params, 'zoom', 0.1, 2.0); this.gui.add(this.params, 'zoom', 0.1, 2.0);
this.gui.add(this.params, 'nodesize', 0, 1); this.gui.add(this.params, 'nodesize', 0, 5);
this.gui.add(this.params, 'nodeopacity', 0, 1).onChange(setNodeOpacity); this.gui.add(this.params, 'nodeopacity', 0, 1).onChange(setNodeOpacity);
this.gui.add(this.params, 'linksize', 0, 1); this.gui.add(this.params, 'linksize', 0, 5);
this.gui.add(this.params, 'linkopacity', 0, 1).onChange( this.gui.add(this.params, 'linkopacity', 0, 1).onChange(
(v) => setLinkOpacity(v, true) (v) => setLinkOpacity(v, true)
); );

View File

@ -31,6 +31,10 @@ camera.lookAt(0, 0, 0);
const renderer = new THREE.WebGLRenderer({antialias: true}); const renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize( window.innerWidth, window.innerHeight ); renderer.setSize( window.innerWidth, window.innerHeight );
renderer.localClippingEnabled = true;
document.body.appendChild( renderer.domElement ); document.body.appendChild( renderer.domElement );
// set up colours and materials for gui callbacks // set up colours and materials for gui callbacks

View File

@ -55,8 +55,28 @@ export function auto_detect_edges(nodes, neighbours, debug=false) {
return links; return links;
} }
export const linkTest = () => {
return {
name: 'linky',
nodes: [
{ id:1, label: 1, x: -1, y: -1, z:-1, w: 0 },
{ id:2, label: 2, x: 1, y: 1, z: 1, w: 0 },
],
links: [
{ id: 1, source: 1, target: 2 }
],
options: [ { name: '--' }],
description: `link`,
}
};
// too small and simple to calculate // too small and simple to calculate
export const cell5 = () => { export const cell5 = () => {
const c1 = Math.sqrt(5) / 4; const c1 = Math.sqrt(5) / 4;
return { return {
@ -844,7 +864,8 @@ export const icosahedron = () => {
export const build_all = () => { export const build_all = () => {
return [ return [
tetrahedron(), linkTest(),
/* tetrahedron(),
octahedron(), octahedron(),
cube(), cube(),
icosahedron(), icosahedron(),
@ -858,9 +879,9 @@ export const build_all = () => {
cell600_layered(), cell600_layered(),
cell120_inscribed(), cell120_inscribed(),
cell120_layered() cell120_layered()
]; */ ];
} }
export const radii = (shape) => { export const radii = (shape) => {
return shape.nodes.map(n => Math.sqrt(n.x * n.x + n.y * n.y + n.z * n.z + n.w * n.w)) return shape.nodes.map(n => Math.sqrt(n.x * n.x + n.y * n.y + n.z * n.z + n.w * n.w))
} }

64
taperedLink.js Normal file
View File

@ -0,0 +1,64 @@
import * as THREE from 'three';
class TaperedLink extends THREE.Group {
constructor(baseMaterial, n1, n2, r1, r2) {
super();
const geometry = new THREE.ConeGeometry( 0.75, 1, 32, true );
const cplane = new THREE.Plane(new THREE.Vector3(0, -1, 0), 0.5);
const material = baseMaterial.clone();
material.clippingPlanes = [ cplane ];
this.cone = new THREE.Mesh( geometry, material );
this.add( this.cone );
this.update(n1, n2, r1, r2);
}
update(n1, n2, r1, r2) {
const kraw = r1 - r2;
let k = ( kraw == 0 ) ? 0.001 : kraw;
let nbase = n1.v3;
let napex = n2.v3;
let rbase = r1;
let rapex = r2;
if( k < 0 ) {
nbase = n2.v3;
napex = n1.v3;
rbase = r2;
rapex = r1;
k = -k;
}
// FIXME - the problem is that when the h_offset > 1, the centroid
// of the cone is on the other side of napex, so it looks the wrong way
const l = nbase.distanceTo(napex);
const h = l * rbase / k;
const h_offset = 0.5 * h / l;
const pos = new THREE.Vector3();
if( l > 0 ) {
pos.lerpVectors(nbase, napex, h_offset);
}
if( h_offset < 1 ) {
this.cone.scale.copy(new THREE.Vector3(rbase, h, rbase));
} else {
// you're on the other side of napex so flip the cone
this.cone.scale.copy(new THREE.Vector3(rbase, -h, rbase));
}
this.lookAt(napex);
this.position.copy(pos);
this.cone.rotation.x = Math.PI / 2.0;
const clipnorm = new THREE.Vector3();
clipnorm.copy(napex);
clipnorm.sub(nbase);
clipnorm.negate();
clipnorm.normalize();
this.cone.material.clippingPlanes[0].setFromNormalAndCoplanarPoint(
clipnorm, napex
);
}
}
export { TaperedLink };