Back to Body Journal
SOURCE / PINNED RELEASE

Made of little things.

Body Journal

Release
e8049f8da4e2…
Author-recorded commit
61c04bf0e8aa…
License
LICENSE
Author’s source reference
nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-6c4554844c7

Archive hash verified: 4369d778da046a8b…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

tests/skin.test.mjs
import assert from 'node:assert/strict';
import test from 'node:test';
import { readFileSync } from 'node:fs';
import { createHash } from 'node:crypto';
import * as three from 'three';
import { resolve,dirname } from 'node:path';
import { runInNewContext } from 'node:vm';
import ts from 'typescript';
const cache=new Map();
function load(file) {
 const path=resolve(file); if(cache.has(path))return cache.get(path).exports;
 const module={exports:{}};cache.set(path,module);
 runInNewContext(ts.transpileModule(readFileSync(path,'utf8'),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText,{module,exports:module.exports,TextEncoder,TextDecoder,require: name=>name==='three'?three:load(resolve(dirname(path),name.replace(/\.js$/,'.ts')))});
 return module.exports;
}
const {SkinModel,decodeSkin}=load('src/skin-model.ts');
const {shapeWeights}=load('src/skin-data.ts');
const {skinRegion}=load('src/body-regions.ts');
const {draftString,parseDraft}=load('src/case-record.ts');
const {profile,makeConcern}=load('src/case.ts');
const buffer=readFileSync('assets/skin/body-skin.bin');
const data=decodeSkin(buffer.buffer.slice(buffer.byteOffset,buffer.byteOffset+buffer.byteLength));
const {Vector3,Raycaster,MeshStandardMaterial,Group}=three;
function model() {return new SkinModel(data,new MeshStandardMaterial());}
function rayMark(m,x,y,back=false) {
 m.update(); const start=m.mesh.localToWorld(new Vector3(x,y,back?-2:2));
 const end=m.mesh.localToWorld(new Vector3(x,y,back?2:-2));
 const hit=new Raycaster(start,end.sub(start).normalize()).intersectObject(m.mesh,false)[0];
 assert.ok(hit,'Ray hits actual skin geometry'); return m.mark(hit,.022,1);
}
test('pinned asset has finite normalized skin weights, stable topology, UVs and nine complete shape samples',()=>{
 const provenance=JSON.parse(readFileSync('assets/skin/provenance.json'));
 assert.equal(createHash('sha256').update(buffer).digest('hex'),provenance.sha256);
 assert.equal(data.meta.vertices,13380);assert.equal(data.meta.triangles,26756);assert.equal(data.meta.bones.length,53);
 assert.equal(data.indices.length,data.meta.triangles*3);
 assert.ok(data.indices.every(i=>i<data.meta.vertices));
 assert.equal(data.uvCorners.length,data.indices.length*2);assert.ok(data.uvCorners.every(v=>v>=0&&v<=1));
 for(let i=0;i<data.meta.vertices;i++) {
  assert.ok(Math.abs(data.skinWeights.slice(i*4,i*4+4).reduce((a,b)=>a+b,0)-1)<1e-6);
  assert.ok(data.skinIndices.slice(i*4,i*4+4).every(j=>j<data.meta.bones.length));
 }
 for(const sample of data.positions){assert.equal(sample.length,data.meta.vertices*3);assert.ok(sample.every(Number.isFinite));}
 for(const [g,w] of [[0,0],[1,1],[.4,.2],[.6,.9]]) assert.ok(Math.abs(shapeWeights(g,w).reduce((a,b)=>a+b,0)-1)<1e-6);
});
test('actual front/back knee rays preserve patient side and yield valid triangle/UV anchors',()=>{
 const m=model();
 for(const [x,left] of [[.148,true],[-.148,false]]) for(const back of [false,true]){
  const result=rayMark(m,x,.492,back);
  assert.match(result.label,new RegExp(`${left?'Left':'Right'} knee`));
  assert.match(result.label,new RegExp(back?'back':'front'));
  const {surface}=result.mark;assert.ok(surface.triangle>=0&&surface.triangle<data.meta.triangles);
  assert.ok(Math.abs(surface.barycentric.reduce((a,b)=>a+b,0)-1)<1e-6);
 }
 m.dispose();
});
test('paint stays on its skinned triangle through body shape, size, sitting and limb manipulation',()=>{
 const m=model(),parent=new Group();parent.add(m.group);
 const {mark,label}=rayMark(m,.148,.492),serialized=JSON.stringify(mark);
 const before=m.mesh.localToWorld(m.anchor(mark).point).clone();
 m.joints.get('left-hip').rotation.x=-1.48;m.joints.get('left-knee').rotation.x=1.48;
 parent.scale.setScalar(190/172); m.shape(190,110,'Female');m.update();
 const a=m.anchor(mark),world=m.mesh.localToWorld(a.point.clone());
 assert.ok(world.distanceTo(before)>.2,'Paint moves with changed body');
 // A short normal ray must hit the same real deformed surface at the anchor.
 const normal=a.normal.clone().transformDirection(m.mesh.matrixWorld);
 const hit=new Raycaster(world.clone().addScaledVector(normal,.01),normal.clone().negate(),0,.03).intersectObject(m.mesh,false)[0];
 assert.ok(hit);assert.ok(hit.point.distanceTo(world)<1e-5,'Paint lies on the surface after deformation');
 const target=m.endpoint('left-hip').add(new Vector3(.05,.10,.06));
 m.solveLimb('left-hip',target);assert.ok(m.anchor(mark).point.toArray().every(Number.isFinite));
 assert.ok(m.joints.get('left-knee').rotation.x>=0&&m.joints.get('left-knee').rotation.x<=2.3);
 assert.equal(JSON.stringify(mark),serialized,'Pose and morph never mutate canonical anchor');assert.match(label,/Left knee/);
 const c=makeConcern(1,label,'sitting');c.marks=[mark];c.joints['left-knee']=[1.48,0,0];
 const restored=parseDraft(draftString(profile,[c])).concerns[0].marks[0];
 assert.ok(m.anchor(restored).point.distanceTo(m.anchor(mark).point)<1e-9);
 m.dispose();
});
test('draft anchors reject wrong models, out-of-range triangles and invalid barycentric coordinates; old text is retained',()=>{
 const m=model(),c=makeConcern(1,'Left knee','standing');c.marks=[rayMark(m,.148,.492).mark];
 const valid=JSON.parse(draftString(profile,[c]));
 for(const mutate of [s=>s.model='another-mesh',s=>s.triangle=26756,s=>s.barycentric=[.9,.9,.9],s=>s.barycentric=[-1,1,1]]) {
  const bad=structuredClone(valid);mutate(bad.concerns[0].marks[0].surface);assert.throws(()=>parseDraft(JSON.stringify(bad)));
 }
 const old=structuredClone(valid);old.version=1;delete old.concerns[0].marks[0].surface;
 const migrated=parseDraft(JSON.stringify(old));assert.equal(migrated.version,3);assert.equal(migrated.concerns[0].location,'Left knee');assert.equal(migrated.concerns[0].marks[0].surface,undefined);
 m.dispose();
});
test('surface atlas separates limb levels, inner/outer sides and individual fingers',()=>{
 const lower=skinRegion([.18,.22,.09],'calf_l');const upper=skinRegion([.16,.38,.09],'calf_l');
 assert.notEqual(lower.zone,upper.zone);assert.match(lower.label,/Left lower leg/);
 assert.match(skinRegion([.21,.492,.04],'calf_l').label,/outer side/);
 assert.match(skinRegion([.09,.492,.04],'calf_l').label,/inner side/);
 assert.match(skinRegion([.35,.8,.03],'index_03_l').label,/Left index finger · tip/);
});

test('external anatomy is paintable, independently selectable, and keeps stable surface anchors through shape and pose changes',()=>{
  const m=model();
  for(const [sex,choice,zone] of [['Male','auto','shaft'],['Female','auto','labium-majus-left'],['Female','penis','scrotum-left']]){
    m.shape(172,70,sex,choice);m.update();
    const mesh=m.surfaces.find(s=>s.name===zone);assert.ok(mesh,'Selected anatomy has a raycast surface');
    const p=mesh.getWorldPosition(new Vector3());const ray=new Raycaster(p.clone().add(new Vector3(0,0,1)),new Vector3(0,0,-1));
    const hit=ray.intersectObject(mesh,false)[0];assert.ok(hit);const {mark,label}=m.mark(hit,.006,1);
    assert.equal(mark.surface.model,'external-anatomy-1');assert.equal(mark.surface.zone,zone);
    const anchor=m.anchor(mark),world=m.group.localToWorld(anchor.point.clone());assert.ok(world.distanceTo(hit.point)<1e-6);
    const initial=JSON.stringify(mark);m.shape(190,130,sex,choice);m.group.rotation.x=-Math.PI/2;m.update();
    assert.ok(m.anchor(mark).point.toArray().every(Number.isFinite));assert.equal(JSON.stringify(mark),initial);
    const c=makeConcern(1,label,'lying');c.marks=[mark];const saved=parseDraft(draftString({...profile,anatomy:choice},[c]));
    assert.equal(saved.concerns[0].marks[0].surface.zone,zone);assert.ok(m.anchor(saved.concerns[0].marks[0]));
    m.shape(172,70,sex,'none');assert.equal(m.anchor(mark),undefined,'Hidden anatomy never reattaches to another surface');m.group.rotation.set(0,0,0);
  }
  m.dispose();
});

test('anatomy roots are embedded in the pelvis across the nine body shapes and sitting pose',()=>{
 const m=model();
 for(const sex of ['Female','','Male']) for(const bmi of [16,26,36]) for(const choice of ['penis','vulva']) {
  m.shape(172,bmi*1.72**2,sex,choice);
  for(const sitting of [false,true]) {
   for(const side of ['left','right']) {m.joints.get(`${side}-hip`).rotation.x=sitting?-1.48:0;m.joints.get(`${side}-knee`).rotation.x=sitting?1.48:0;}
   m.update();
   const roots=choice==='penis'?['shaft','scrotum-left','scrotum-right']:['labium-majus-left','labium-majus-right'];
   for(const id of roots) {
    const mesh=m.surfaces.find(s=>s.name===id),root=mesh.localToWorld(new Vector3().fromBufferAttribute(mesh.geometry.getAttribute('position'),0));
    const hit=new Raycaster(new Vector3(root.x,root.y,2),new Vector3(0,0,-1)).intersectObject(m.mesh,false)[0];
    assert.ok(hit,`${sex}/${bmi}/${sitting}/${id}: root is over the pelvis`);
    assert.ok(root.z<=hit.point.z+.001,`${sex}/${bmi}/${sitting}/${id}: root must join skin, gap ${root.z-hit.point.z}`);
   }
  }
 }
 m.dispose();
});