Program: 11685
Project: 11685
Buggy source:
/** 
 * Computes a pre-alignemnt of all non-fixed   {@link Tile}s by propagating the pairwise models. This does not give a correct registration but a very good starting point for the global optimization. This is necessary for models where the global optimization is not guaranteed to converge like the   {@link HomographyModel2D},   {@link RigidModel3D}, ...
 * @return - a list of {@link Tile}s that could not be pre-aligned
 * @throws NotEnoughDataPointsException
 * @throws {@link IllDefinedDataPointsException}
 */
public List<Tile<?>> preAlign() throws NotEnoughDataPointsException, IllDefinedDataPointsException {
  final ArrayList<Tile<?>> unAlignedTiles=new ArrayList<Tile<?>>();
  final ArrayList<Tile<?>> alignedTiles=new ArrayList<Tile<?>>();
  if (getFixedTiles().size() == 0) {
    final Iterator<Tile<?>> it=this.getTiles().iterator();
    alignedTiles.add(it.next());
    while (it.hasNext())     unAlignedTiles.add(it.next());
  }
 else {
    for (    final Tile<?> tile : this.getTiles()) {
      if (this.getFixedTiles().contains(tile))       alignedTiles.add(tile);
 else       unAlignedTiles.add(tile);
    }
  }
  for (final ListIterator<Tile<?>> referenceIterator=alignedTiles.listIterator(); referenceIterator.hasNext(); ) {
    if (unAlignedTiles.size() == 0)     break;
    final Tile<?> referenceTile=referenceIterator.next();
    referenceTile.apply();
    for (final ListIterator<Tile<?>> targetIterator=unAlignedTiles.listIterator(); targetIterator.hasNext(); ) {
      final Tile<?> targetTile=targetIterator.next();
      if (referenceTile.getConnectedTiles().contains(targetTile)) {
        final ArrayList<PointMatch> pm=getConnectingPointMatches(targetTile,referenceTile);
        if (pm.size() > targetTile.getModel().getMinNumMatches()) {
          targetTile.getModel().fit(pm);
          targetIterator.remove();
          int countFwd=0;
          while (referenceIterator.hasNext()) {
            referenceIterator.next();
            ++countFwd;
          }
          referenceIterator.add(targetTile);
          for (int j=0; j < countFwd + 1; ++j)           referenceIterator.previous();
        }
      }
    }
  }
  return unAlignedTiles;
}

Fixed version (ground truth):
/** 
 * Computes a pre-alignemnt of all non-fixed   {@link Tile}s by propagating the pairwise models. This does not give a correct registration but a very good starting point for the global optimization. This is necessary for models where the global optimization is not guaranteed to converge like the   {@link HomographyModel2D},   {@link RigidModel3D}, ...
 * @return - a list of {@link Tile}s that could not be pre-aligned
 * @throws NotEnoughDataPointsException
 * @throws {@link IllDefinedDataPointsException}
 */
public List<Tile<?>> preAlign() throws NotEnoughDataPointsException, IllDefinedDataPointsException {
  final ArrayList<Tile<?>> unAlignedTiles=new ArrayList<Tile<?>>();
  final ArrayList<Tile<?>> alignedTiles=new ArrayList<Tile<?>>();
  if (getFixedTiles().size() == 0) {
    final Iterator<Tile<?>> it=this.getTiles().iterator();
    alignedTiles.add(it.next());
    while (it.hasNext())     unAlignedTiles.add(it.next());
  }
 else {
    for (    final Tile<?> tile : this.getTiles()) {
      if (this.getFixedTiles().contains(tile))       alignedTiles.add(tile);
 else       unAlignedTiles.add(tile);
    }
  }
  for (final ListIterator<Tile<?>> referenceIterator=alignedTiles.listIterator(); referenceIterator.hasNext(); ) {
    if (unAlignedTiles.size() == 0)     break;
    final Tile<?> referenceTile=referenceIterator.next();
    referenceTile.apply();
    for (final ListIterator<Tile<?>> targetIterator=unAlignedTiles.listIterator(); targetIterator.hasNext(); ) {
      final Tile<?> targetTile=targetIterator.next();
      if (referenceTile.getConnectedTiles().contains(targetTile)) {
        final ArrayList<PointMatch> pm=getConnectingPointMatches(targetTile,referenceTile);
        if (pm.size() >= targetTile.getModel().getMinNumMatches()) {
          targetTile.getModel().fit(pm);
          targetIterator.remove();
          int countFwd=0;
          while (referenceIterator.hasNext()) {
            referenceIterator.next();
            ++countFwd;
          }
          referenceIterator.add(targetTile);
          for (int j=0; j < countFwd + 1; ++j)           referenceIterator.previous();
        }
      }
    }
  }
  return unAlignedTiles;
}