How the Orientations of the ImageCPRMapper example are calculated?

I wish to known how can i insted of using the pre computaded ones from the json , how can i programmatically create them

It is a matter of creating an orthogonal matrix composed of a tangent, bitangent and normal and assigned at each point of the centerline.

1 Like

Thanks for your advice, I’ve tried to follow it but I’m facing some problems with my calculations, as the final result is different from the json and the image itself look a bit weird, I don’t know if I am doing something wrong. I would be grateful if you could provide me with some guidance on my code.

function computeTangent(points, i, n) {
  let tangent = vec3.create();

  if (i === 0) {
    // forward difference
    vec3.subtract(tangent,points[1], points[0]);
  } else if (i === n - 1) {
    // backward difference
    vec3.subtract(tangent,points[n - 1], points[n - 2]);
  } else {
    // central difference
    vec3.subtract(tangent,points[i + 1], points[i - 1]);
  }

  vec3.normalize(tangent, tangent);

  console.log("Tangent:", tangent);

  return tangent;
}

function computeBitangent(tangent) {
  // Choose an up vector that is not nearly parallel to tangent
  let up = vec3.fromValues(0, 1, 0);
  if (Math.abs(vec3.dot(tangent, up)) > 0.99) {
    up = vec3.fromValues(1, 0, 0);
  }
  // Compute bitangent 
  let bitangent = vec3.create();
  vec3.cross(bitangent, tangent, up);
  vec3.normalize(bitangent, bitangent);

  console.log("Bitangent:", bitangent);

  return bitangent;
}

function computeNormal(tangent, bitangent) {
  const normal = vec3.create();
  vec3.cross(normal, bitangent, tangent);
  vec3.normalize(normal, normal);
  console.log("Normal:", normal);
  return normal;
}

function computeOrientationsFromPositions(positionsFlat) {
  const { nPoints, positions } = getPositionsAsVec3(positionsFlat); 
  const orientations = new Float32Array(nPoints * 16);
  for (let i = 0; i < nPoints; i++) {
    const tangent = computeTangent(positions, i, nPoints);
    const bitangent = computeBitangent(tangent);
    const normal = computeNormal(tangent, bitangent);
    const matrix = buildOrientationMatrix(tangent, bitangent, normal, positions[i]);
    orientations.set(matrix, i * 16);
  }
  return orientations;
}`

what you call tangent is what we call normal.

1 Like