Ensure sprite positions never go out of image bounds
@@ -45,6 +45,8 @@ public class SpriteCutter
final Solution solution = spriteCutter.getSolution(grid, optimizationType);
// fast optimization
solution.fastOptimize();
+ // fix positions
+ solution.fixPos();
// add the solution
if (!solution.cells.isEmpty())
@@ -486,6 +488,16 @@ public class SpriteCutter
while ((newScore != score) && (Math.abs(conv) > 0.0005d));
}
+ public void fixPos()
+ {
+ final List<SpriteCell> cellsCopy = new ArrayList<>(cells);
+
+ // rebuild solution while fixing cell position (no negative position)
+ reset();
+ for (SpriteCell cell : cellsCopy)
+ addCell(SpriteCell.fixPosition(dim, cell));
+ }
+
public void fastOptimize()
{
optimizeMerge();
@@ -1037,7 +1049,7 @@ public class SpriteCutter
* The method returns immediately, you should retrieve the result using
* {@link #getOptimizedSolution()}
*
- * @param solution
+ * @param solutions
* input solution< to optimize (should be valid)
* @param numIteration
* Number of iteration for the genetic algorithm (0 = no limit, about 100000 it/s on core
@@ -1099,6 +1111,8 @@ public class SpriteCutter
// get best solution from optimizer
final Solution result = optimizer.getBestSolution();
+ // fix positions
+ result.fixPos();
// done
optimizer = null;
