unweightedSampling static method

List unweightedSampling(
  1. DynamicPath path1,
  2. DynamicPath path2, {
  3. required int maxControlPoints,
  4. required Offset origin,
})

Implementation

static List<dynamic> unweightedSampling(DynamicPath path1, DynamicPath path2,
    {required int maxControlPoints, required Offset origin}) {
  int totalPoints = lcm(path1.nodes.length, path2.nodes.length);

  ///cap at maxControlPoints, but it is possible that the minimum required points
  ///(max(points1, points2)) is larger than maxControlPoints.
  if (totalPoints > maxControlPoints) {
    totalPoints =
        max(maxControlPoints, max(path1.nodes.length, path2.nodes.length));
  }

  double tempMinWeight = double.infinity;
  List<int>? tempCounts1, tempCounts2;
  DynamicPath tempPath1, tempPath2;

  DynamicPath optimalPath1 = DynamicPath(size: Size.zero, nodes: []),
      optimalPath2 = DynamicPath(size: Size.zero, nodes: []);
  List<int> optimalCount1 = [], optimalCount2 = [];

  int tempTotalPoints = totalPoints;
  int stepPoints = max(
      ((maxControlPoints - totalPoints).abs() / (4 * totalPoints)).round() *
          totalPoints,
      totalPoints);

  do {
    tempCounts1 =
        sampleSupplyCounts(path1, tempTotalPoints, weightBased: false);
    tempCounts2 =
        sampleSupplyCounts(path2, tempTotalPoints, weightBased: false);

    tempPath1 = supplyPoints(path1, tempCounts1);
    tempPath2 = supplyPoints(path2, tempCounts2);

    int tempShift = computeMinimumOffsetIndex(
        tempPath1.nodes.map((e) => e.position).toList(),
        tempPath2.nodes.map((e) => e.position).toList());

    tempPath1.nodes =
        rotateList(tempPath1.nodes, tempShift) as List<DynamicNode>;

    List<Offset> path1Nodes = tempPath1.nodes.map((e) => e.position).toList(),
        path2Nodes = tempPath2.nodes.map((e) => e.position).toList();

    double tempWeight =
        computeTotalMorphWeight(path1Nodes, path2Nodes, origin: origin);

    tempPath1.nodes =
        rotateList(tempPath1.nodes, -tempShift) as List<DynamicNode>;
    if (tempWeight < tempMinWeight) {
      tempMinWeight = tempWeight;
      optimalPath1 = tempPath1;
      optimalPath2 = tempPath2;
      optimalCount1 = tempCounts1;
      optimalCount2 = tempCounts2;
    }
    tempTotalPoints += stepPoints;
  } while (tempTotalPoints < maxControlPoints);

  int shift = computeMinimumOffsetIndex(
      optimalPath1.nodes.map((e) => e.position).toList(),
      optimalPath2.nodes.map((e) => e.position).toList());

  optimalPath1.nodes =
      rotateList(optimalPath1.nodes, shift) as List<DynamicNode>;

  List<Offset> path1Nodes =
          optimalPath1.nodes.map((e) => e.position).toList(),
      path2Nodes = optimalPath2.nodes.map((e) => e.position).toList();

  return [
    optimalCount1,
    optimalCount2,
    optimalPath1,
    optimalPath2,
    shift,
    computeTotalMorphWeight(path1Nodes, path2Nodes, origin: origin),
  ];
}