TubeHullBands function

List<List<Vector3>> TubeHullBands(
  1. List<Vector3> pointsAndTangents,
  2. int segments,
  3. double radius
)

Given the points and corresponding tangents on a curve. Compute the hull (points and normals) at radius around those points divided into segment sections.

Implementation

List<List<VM.Vector3>> TubeHullBands(List<VM.Vector3> pointsAndTangents,
    int segments, double radius) {
  final List<List<VM.Vector3>> out = [];
  // Avoid unnecessary allocations:

  final VM.Vector3 v1 = VM.Vector3.zero();
  final VM.Vector3 v2 = VM.Vector3.zero();

  final List<double> sinList = Float32List(segments);
  final List<double> cosList = Float32List(segments);

  for (int j = 0; j < segments; ++j) {
    double v = j / segments * 2 * Math.pi;
    cosList[j] = radius * Math.cos(v);
    sinList[j] = radius * Math.sin(v);
  }

  for (int i = 0; i < pointsAndTangents.length; i += 2) {
    VM.Vector3 c = pointsAndTangents[i + 0];
    VM.Vector3 d = pointsAndTangents[i + 1];
    List<VM.Vector3> band = [];
    out.add(band);

    buildPlaneVectors(d, v1, v2);
    v1.normalize();
    v2.normalize();

    for (int j = 0; j < segments; ++j) {
      // normal
      final VM.Vector3 p2 = VM.Vector3.zero();
      p2.addScaled(v1, cosList[j]);
      p2.addScaled(v2, sinList[j]);

      // point on hull
      final VM.Vector3 p1 = c + p2;
      band.add(p1);

      p2.normalize();
      band.add(p2);
    }
  }
  return out;
}