Source code of plot #060 back to plot

Download full working sketch as 060.tar.gz.
Unzip, then start a local web server and load the page in a browser.

import {Vec3} from "./lib/own/geo2.js";

export class WallPlane {
  constructor(norm, displacement) {
    this.norm = norm;
    this.displacement = displacement;
  }
}

export class CellData {
  constructor(id, particle, volume, vertices, faceVertIxs) {
    this.id = id;
    this.particle = particle;
    this.volume = volume;
    this.vertices = vertices;
    this.faceVertIxs = faceVertIxs;
  }
}

export function genVoro(mod, volume, wallPlanes, particles) {

  const cellDataArr = [];

  const szInput =
    6 + // Space bounds
    1 + wallPlanes.length * 4 + // Wall planes
    1 + particles.length * 4; // Particles

  const pInput = mod._malloc(szInput * 8);
  const input = new Float64Array(mod.HEAPU8.buffer, pInput, szInput);
  let ip = 0;
  for (let i = 0; i < 6; ++i)
    input[ip++] = volume[i];

  input[ip++] = wallPlanes.length;
  for (let i = 0; i < wallPlanes.length; ++i) {
    input[ip++] = wallPlanes[i].norm.x;
    input[ip++] = wallPlanes[i].norm.y;
    input[ip++] = wallPlanes[i].norm.z;
    input[ip++] = wallPlanes[i].displacement;
  }

  input[ip++] = particles.length;
  for (let i = 0; i < particles.length; ++i) {
    input[ip++] = i;
    input[ip++] = particles[i].x;
    input[ip++] = particles[i].y;
    input[ip++] = particles[i].z;
  }

  const pRes = mod._calculate_voronoi(pInput, 0);
  const dummyArr = new Float64Array(mod.HEAPU8.buffer, pRes, 1);
  const resSize = dummyArr[0];
  const resArr = new Float64Array(mod.HEAPU8.buffer, pRes, resSize);
  let nCells = resArr[1];
  let pos = 2;

  for (let cix = 0; cix < nCells; ++cix) {

    const id = resArr[pos++];
    const particle = new Vec3(resArr[pos++], resArr[pos++], resArr[pos++]);
    const volume = resArr[pos++];

    // Vertices of cell
    const vertices = [];
    const nVerts = resArr[pos++];
    for (let i = 0; i < nVerts; ++i) {
      vertices.push(new Vec3(resArr[pos++], resArr[pos++], resArr[pos++]));
    }

    // Vertex indices in each face
    const faceVertIxs = [];
    let nFaces = resArr[pos++];
    while (nFaces > 0) {
      --nFaces;
      let nVertsInFace = resArr[pos++];
      const vertIxs = [];
      while (nVertsInFace > 0) {
        --nVertsInFace;
        vertIxs.push(resArr[pos++]);
      }
      faceVertIxs.push(vertIxs);
    }

    // Inset version of cell?
    let nInsetVerts = resArr[pos++];
    if (nInsetVerts != 0) {
      // Vertices of inset cell
      const insetVertices = [];
      for (let i = 0; i < nInsetVerts; ++i) {
        insetVertices.push(new Vec3(resArr[pos++], resArr[pos++], resArr[pos++]));
      }
      // Vertex indices in each face
      const insetFaceVertIxs = [];
      let nInsetFaces = resArr[pos++];
      while (nInsetFaces > 0) {
        --nInsetFaces;
        let nVertsInFace = resArr[pos++];
        const vertIxs = [];
        while (nVertsInFace > 0) {
          --nVertsInFace;
          vertIxs.push(resArr[pos++]);
        }
        insetFaceVertIxs.push(vertIxs);
      }
    }

    cellDataArr.push(new CellData(id, particle, volume, vertices, faceVertIxs));
  }

  mod._free(pRes);
  mod._free(pInput);

  return cellDataArr;
}