Skip to content

Commit

Permalink
performance optimized for findSimilar
Browse files Browse the repository at this point in the history
  • Loading branch information
1Ridav committed Aug 22, 2018
1 parent 9bac02b commit 62143e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/main/java/bot/penguee/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,15 @@ public boolean waitForHide(String fragName, int time, int delay) throws Exceptio
return waitForHide(fragName, time, delay, 0, 0, (int) rect.getWidth(), (int) rect.getHeight());
}

public boolean findSimilar(String fragName, int rate) throws FragmentNotLoadedException, ScreenNotGrabbedException {
public boolean findSimilar(String fragName, double rate) throws FragmentNotLoadedException, ScreenNotGrabbedException {
return findSimilarPos(fragName, rate, fragName) != null;
}

public MatrixPosition findSimilarPos(String fragName, int rate) throws FragmentNotLoadedException, ScreenNotGrabbedException {
public MatrixPosition findSimilarPos(String fragName, double rate) throws FragmentNotLoadedException, ScreenNotGrabbedException {
return findSimilarPos(fragName, rate, fragName);
}

public MatrixPosition findSimilarPos(String fragName, int rate, String customName) throws FragmentNotLoadedException, ScreenNotGrabbedException {
public MatrixPosition findSimilarPos(String fragName, double rate, String customName) throws FragmentNotLoadedException, ScreenNotGrabbedException {
MatrixPosition mp = screen.findSimilar(fragName, rate, customName);
if (mp != null)
lastCoord = mp;
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/bot/penguee/Frag.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,36 +144,37 @@ public void makeFile(String name) throws Exception {
* return null; }
*/

public MatrixPosition findSimilarIn(Frag b, int rate, int x_start, int y_start, int x_stop, int y_stop) {
public MatrixPosition findSimilarIn(Frag b, double rate, int x_start, int y_start, int x_stop, int y_stop) {
// precalculate all frequently used data
final int[][] small = this.rgbData;
final int[][] big = b.rgbData;
final int small_height = small.length;
final int small_width = small[0].length;
rate = 100 - rate;// similarity rate 95% is qual to 5% difference rate.
final long maxDiff = 3 * 255 * small_height * small_width;
double leastDifference = 100;
final long maxBreakDiff = (long) ((rate / 100) * maxDiff);
long leastDifference = Long.MAX_VALUE;
MatrixPosition bestResultMatrixPosition = null;

int[] row_cache_big = null;
int[] row_cache_small = null;
for (int y = y_start; y < y_stop; y++) {
__columnscan: for (int x = x_start; x < x_stop; x++) {
long diff = 0;//sum difference values
long diff = 0;// sum difference values
for (int yy = 0; yy < small_height; yy++) {
row_cache_small = small[yy];
row_cache_big = big[y + yy];
for (int xx = 0; xx < small_width; xx++)
for (int xx = 0; xx < small_width; xx++) {
diff += pixelDiffARGB(row_cache_big[x + xx], row_cache_small[xx]);
if (diff > maxBreakDiff)
continue __columnscan; // no match
}
}
double diffResult = (100.0 * diff / maxDiff);

if (diffResult > rate)//this should be true much more frequent
continue __columnscan; // no match
else if (diffResult == 0.0)
if (diff == 0)
return new MatrixPosition(x, y); // full match
else if (diffResult < leastDifference) { // found better match
leastDifference = diffResult;
else if (diff < leastDifference) { // found better match
leastDifference = diff;
bestResultMatrixPosition = new MatrixPosition(x, y);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/bot/penguee/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public MatrixPosition find(Frag smallFragment) throws ScreenNotGrabbedException
return null;
}

public MatrixPosition findSimilar(Frag smallFragment, int rate) throws ScreenNotGrabbedException {
public MatrixPosition findSimilar(Frag smallFragment, double rate) throws ScreenNotGrabbedException {
MatrixPosition cachedPos = null;

if (screenFrag == null)
Expand Down Expand Up @@ -220,11 +220,11 @@ public MatrixPosition[] find_all(Frag smallFragment) throws ScreenNotGrabbedExce
return mp;
}

MatrixPosition findSimilar(String name, int rate) throws FragmentNotLoadedException, ScreenNotGrabbedException {
MatrixPosition findSimilar(String name, double rate) throws FragmentNotLoadedException, ScreenNotGrabbedException {
return findSimilar(name, rate, name);
}

MatrixPosition findSimilar(String name, int rate, String customName) throws FragmentNotLoadedException, ScreenNotGrabbedException {
MatrixPosition findSimilar(String name, double rate, String customName) throws FragmentNotLoadedException, ScreenNotGrabbedException {
Frag f = Data.fragments().get(name);
if (f != null) {
MatrixPosition mp = findSimilar(f, rate);
Expand Down

0 comments on commit 62143e2

Please sign in to comment.