gnugo-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnugo-devel] AFFINE_TRANSFORM()


From: Gunnar Farneback
Subject: [gnugo-devel] AFFINE_TRANSFORM()
Date: Fri, 01 Feb 2002 17:31:08 +0100
User-agent: EMH/1.14.1 SEMI/1.14.3 (Ushinoya) FLIM/1.14.2 (Yagi-Nishiguchi) APEL/10.3 Emacs/20.7 (sparc-sun-solaris2.7) (with unibyte mode)

At several places in the code this or a similar construction can be
found:

  int ti, tj;
  int move;
  TRANSFORM(pattern->movei, pattern->movej, &ti, &tj, ll);
  ti += m;
  tj += n;
  move = POS(ti, tj);

This adds a translation to the rotation/reflection performed by the
TRANSFORM() macro. The patch below introduces the AFFINE_TRANSFORM()
macro, doing all this in one operation so that the code above is
replaced by

  int move = AFFINE_TRANSFORM(pattern->movei, pattern->movej, ll, m, n);

/Gunnar

Index: engine/combination.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/combination.c,v
retrieving revision 1.23
diff -u -r1.23 combination.c
--- engine/combination.c        31 Jan 2002 08:30:24 -0000      1.23
+++ engine/combination.c        1 Feb 2002 16:20:29 -0000
@@ -887,15 +887,11 @@
 atari_atari_attack_callback(int m, int n, int color,
                            struct pattern *pattern, int ll, void *data)
 {
-  int ti, tj;
   int move;
   int k;
   UNUSED(data);
 
-  TRANSFORM(pattern->movei, pattern->movej, &ti, &tj, ll);
-  ti += m;
-  tj += n;
-  move = POS(ti, tj);
+  move = AFFINE_TRANSFORM(pattern->movei, pattern->movej, ll, m, n);
 
   if (forbidden[move])
     return;
@@ -920,13 +916,8 @@
   for (k = 0; k < pattern->patlen; ++k) { /* match each point */
     if (pattern->patn[k].att == ATT_X) {
       /* transform pattern real coordinate */
-      int x, y;
-      int str;
-      TRANSFORM(pattern->patn[k].x, pattern->patn[k].y, &x, &y, ll);
-      x += m;
-      y += n;
-
-      str = find_origin(POS(x, y));
+      int str = find_origin(AFFINE_TRANSFORM(pattern->patn[k].x,
+                                            pattern->patn[k].y, ll, m, n));
 
       if (current_minsize > 0
          && get_aa_value(str) < current_minsize
Index: engine/dragon.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/dragon.c,v
retrieving revision 1.49
diff -u -r1.49 dragon.c
--- engine/dragon.c     20 Jan 2002 15:08:06 -0000      1.49
+++ engine/dragon.c     1 Feb 2002 16:20:30 -0000
@@ -46,7 +46,6 @@
 #include <ctype.h>
 
 #include "liberty.h"
-#include "patterns.h"
 
 static void initialize_supplementary_dragon_data(void);
 static void find_neighbor_dragons(void);
Index: engine/influence.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/influence.c,v
retrieving revision 1.32
diff -u -r1.32 influence.c
--- engine/influence.c  29 Jan 2002 16:20:53 -0000      1.32
+++ engine/influence.c  1 Feb 2002 16:20:30 -0000
@@ -599,9 +599,9 @@
 influence_callback(int m, int n, int color, struct pattern *pattern, int ll,
                   void *data)
 {
-  int ti, tj;
+  int pos;
   int k;
-  int saved_pos;
+  int saved_pos = NO_MOVE;
   struct influence_data *q = data;
   
   /* Patterns marked FY get ignored in experimental influence,
@@ -710,15 +710,13 @@
     }
   }
 
-  TRANSFORM(pattern->movei, pattern->movej, &ti, &tj, ll);
-  ti += m;
-  tj += n;
+  pos = AFFINE_TRANSFORM(pattern->movei, pattern->movej, ll, m, n);
 
   /* If the pattern has a constraint, call the autohelper to see
    * if the pattern must be rejected.
    */
   if (pattern->autohelper_flag & HAVE_CONSTRAINT) {
-    if (!pattern->autohelper(pattern, ll, POS(ti, tj), color, 0))
+    if (!pattern->autohelper(pattern, ll, pos, color, 0))
       return;
   }
 
@@ -726,10 +724,10 @@
    * be rejected.
    */
   if (pattern->helper) {
-    if (!pattern->helper(pattern, ll, POS(ti, tj), color)) {
+    if (!pattern->helper(pattern, ll, pos, color)) {
       DEBUG(DEBUG_INFLUENCE,
            "Influence pattern %s+%d rejected by helper at %1m\n",
-           pattern->name, ll, POS(ti, tj));
+           pattern->name, ll, pos);
       return;
     }
   }
@@ -740,7 +738,7 @@
   /* For t patterns, everything happens in the action. */
   if ((pattern->class & CLASS_t)
       && (pattern->autohelper_flag & HAVE_ACTION)) {
-    pattern->autohelper(pattern, ll, POS(ti, tj), color, 1);
+    pattern->autohelper(pattern, ll, pos, color, 1);
   }
   
   
@@ -756,14 +754,14 @@
 
     /* Increase strength if we're computing escape influence. */
     if (q == &escape_influence && (pattern->class & CLASS_e))
-      add_influence_source(POS(ti, tj), this_color,
+      add_influence_source(pos, this_color,
                           20 * pattern->value, 1.5, q);
     else
-      add_influence_source(POS(ti, tj), this_color, pattern->value, 1.5, q);
+      add_influence_source(pos, this_color, pattern->value, 1.5, q);
 
     DEBUG(DEBUG_INFLUENCE,
-         "  low intensity influence source at %m, strength %f, color %C\n",
-         ti, tj, pattern->value, this_color);
+         "  low intensity influence source at %1m, strength %f, color %C\n",
+         pos, pattern->value, this_color);
     return;
   }
   
@@ -771,11 +769,11 @@
    * pattern defined strength at *.
    */
   if (pattern->class & CLASS_E) {
-    add_influence_source(POS(ti, tj), color,
+    add_influence_source(pos, color,
                         pattern->value, DEFAULT_ATTENUATION, q);
     DEBUG(DEBUG_INFLUENCE,
-         "  extra %C source at %m, strength %f\n", color,
-         ti, tj, pattern->value);
+         "  extra %C source at %1m, strength %f\n", color,
+         pos, pattern->value);
     return;
   }
   
@@ -856,16 +854,15 @@
   for (k = 0; k < pattern->patlen; ++k)  /* match each point */
     if (pattern->patn[k].att == ATT_not) {
       /* transform pattern real coordinate */
-      int x, y;
-      TRANSFORM(pattern->patn[k].x, pattern->patn[k].y, &x, &y, ll);
-      x += m;
-      y += n;
+      int pos;
+      pos = AFFINE_TRANSFORM(pattern->patn[k].x, pattern->patn[k].y,
+                            ll, m, n);
 
       /* Low intensity influence source for the color in turn to move. */
-      enter_intrusion_source(saved_stone, POS(x, y), pattern->value,
+      enter_intrusion_source(saved_stone, pos, pattern->value,
                             EXP_DEFAULT_ATTENUATION, &followup_influence);
-      DEBUG(DEBUG_INFLUENCE, "  followup for %m: intrusion at %m\n",
-            m, n, x, y);
+      DEBUG(DEBUG_INFLUENCE, "  followup for %m: intrusion at %1m\n",
+            m, n, pos);
     }
 }
 
Index: engine/owl.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/owl.c,v
retrieving revision 1.54
diff -u -r1.54 owl.c
--- engine/owl.c        31 Jan 2002 16:19:36 -0000      1.54
+++ engine/owl.c        1 Feb 2002 16:20:31 -0000
@@ -2589,16 +2589,13 @@
 owl_shapes_callback(int m, int n, int color, struct pattern *pattern,
                    int ll, void *data)
 {
-  int ti, tj, tval;  /* trial move and its value */
+  int tval;  /* trial move and its value */
   int move;
   struct owl_move_data *moves = data; /* considered moves passed as data */
   int same_dragon;
 
   /* Pick up the location of the move */
-  TRANSFORM(pattern->movei, pattern->movej, &ti, &tj, ll);
-  ti += m;
-  tj += n;
-  move = POS(ti, tj);
+  move = AFFINE_TRANSFORM(pattern->movei, pattern->movej, ll, m, n);
 
   /* Before we do any expensive reading, check whether this move
    * already is known with a higher value or if there are too many
Index: engine/reading.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/reading.c,v
retrieving revision 1.46
diff -u -r1.46 reading.c
--- engine/reading.c    30 Jan 2002 23:30:01 -0000      1.46
+++ engine/reading.c    1 Feb 2002 16:20:32 -0000
@@ -2692,16 +2692,12 @@
                            struct pattern *pattern, int ll, void *data)
 {
   int k;
-  int ti, tj;
   int move;
   int value = pattern->value;
   struct reading_move_data *moves = data;
   UNUSED(data);
 
-  TRANSFORM(pattern->movei, pattern->movej, &ti, &tj, ll);
-  ti += m;
-  tj += n;
-  move = POS(ti, tj);
+  move = AFFINE_TRANSFORM(pattern->movei, pattern->movej, ll, m, n);
 
   for (k = 0; k < MAX_READING_MOVES; k++) {
     if (moves[k].pos) {
@@ -2761,13 +2757,8 @@
     for (k = 0; k < pattern->patlen; ++k) { /* match each point */
     if (pattern->patn[k].att == ATT_X) {
       /* transform pattern real coordinate */
-      int x, y;
-      int str;
-      TRANSFORM(pattern->patn[k].x, pattern->patn[k].y, &x, &y, ll);
-      x += m;
-      y += n;
-
-      str = find_origin(POS(x, y));
+      int str = find_origin(AFFINE_TRANSFORM(pattern->patn[k].x,
+                                            pattern->patn[k].y, ll, m, n));
       readpat_tried[str] = 1;
       /*gprintf("Matched at %1m\n", str);*/
     }
Index: engine/shapes.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/shapes.c,v
retrieving revision 1.21
diff -u -r1.21 shapes.c
--- engine/shapes.c     19 Jan 2002 04:31:16 -0000      1.21
+++ engine/shapes.c     1 Feb 2002 16:20:33 -0000
@@ -41,7 +41,6 @@
                void *data)
 {
   int k, l;
-  int ti, tj;  /* trial move and its value */
   int move;
   
   /* Dragons of our color. */
@@ -59,10 +58,7 @@
   UNUSED(data);
   
   /* Pick up the location of the move */
-  TRANSFORM(pattern->movei, pattern->movej, &ti, &tj, ll);
-  ti += m;
-  tj += n;
-  move = POS(ti, tj);
+  move = AFFINE_TRANSFORM(pattern->movei, pattern->movej, ll, m, n);
   
   /* For some classes of patterns we need to find all dragons present
    * in the pattern.
@@ -74,7 +70,7 @@
 
     /* Match each point. */
     for (k = 0; k < pattern->patlen; ++k) { 
-      int x, y; /* absolute (board) co-ords of (transformed) pattern element */
+      int pos; /* absolute (board) co-ord of (transformed) pattern element */
       int origin; /* dragon origin */
       
       /* all the following stuff (currently) applies only at occupied cells */
@@ -82,9 +78,7 @@
        continue;
 
       /* transform pattern real coordinate */
-      TRANSFORM(pattern->patn[k].x, pattern->patn[k].y, &x, &y, ll);
-      x += m;
-      y += n;
+      pos = AFFINE_TRANSFORM(pattern->patn[k].x, pattern->patn[k].y, ll, m, n);
 
       /* Already, matchpat rejects O patterns containing a friendly stone with
        * DEAD or CRITICAL matcher_status. If the stone is tactically
@@ -95,13 +89,13 @@
        * known by matchpat().
        */
       if ((class & CLASS_O)
-         && BOARD(x, y) == color
-         && worm[POS(x, y)].attack_points[0] != 0
-         && !does_defend(move, POS(x, y)))
+         && board[pos] == color
+         && worm[pos].attack_points[0] != 0
+         && !does_defend(move, pos))
        return;
 
-      origin = dragon[POS(x, y)].origin;
-      if (BOARD(x, y) == color && my_ndragons < MAX_DRAGONS_PER_PATTERN) {
+      origin = dragon[pos].origin;
+      if (board[pos] == color && my_ndragons < MAX_DRAGONS_PER_PATTERN) {
        for (l = 0; l < my_ndragons; l++) {
          if (my_dragons[l] == origin)
            break;
@@ -111,8 +105,8 @@
            * rather the underlying worm) cannot be tactically
            * captured before adding it to the list of my_dragons.  
           */
-         if (worm[POS(x, y)].attack_codes[0] == 0
-             || does_defend(move, POS(x, y))) {
+         if (worm[pos].attack_codes[0] == 0
+             || does_defend(move, pos)) {
            /* Ok, add the dragon to the list. */
            my_dragons[l] = origin;
            my_ndragons++;
@@ -120,7 +114,7 @@
        }
       }
 
-      if (BOARD(x, y) == other && your_ndragons < MAX_DRAGONS_PER_PATTERN) {
+      if (board[pos] == other && your_ndragons < MAX_DRAGONS_PER_PATTERN) {
        for (l = 0; l < your_ndragons; l++) {
          if (your_dragons[l] == origin)
            break;
Index: engine/worm.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/worm.c,v
retrieving revision 1.29
diff -u -r1.29 worm.c
--- engine/worm.c       31 Jan 2002 16:19:36 -0000      1.29
+++ engine/worm.c       1 Feb 2002 16:20:33 -0000
@@ -1636,15 +1636,11 @@
 attack_callback(int m, int n, int color, struct pattern *pattern, int ll,
                void *data)
 {
-  int ti, tj;
   int move;
   int k;
   UNUSED(data);
 
-  TRANSFORM(pattern->movei, pattern->movej, &ti, &tj, ll);
-  ti += m;
-  tj += n;
-  move = POS(ti, tj);
+  move = AFFINE_TRANSFORM(pattern->movei, pattern->movej, ll, m, n);
 
   /* If the pattern has a constraint, call the autohelper to see
    * if the pattern must be rejected.
@@ -1670,13 +1666,10 @@
   for (k = 0; k < pattern->patlen; ++k) { /* match each point */
     if (pattern->patn[k].att == ATT_X) {
       /* transform pattern real coordinate */
-      int x, y;
-      int str;
-      TRANSFORM(pattern->patn[k].x, pattern->patn[k].y, &x, &y, ll);
-      x += m;
-      y += n;
+      int pos = AFFINE_TRANSFORM(pattern->patn[k].x, pattern->patn[k].y,
+                                ll, m, n);
 
-      str = worm[POS(x, y)].origin;
+      int str = worm[pos].origin;
 
       /* A string with 5 liberties or more is considered tactically alive. */
       if (countlib(str) > 4)
@@ -1732,15 +1725,11 @@
 defense_callback(int m, int n, int color, struct pattern *pattern, int ll,
                 void *data)
 {
-  int ti, tj;
   int move;
   int k;
   UNUSED(data);
 
-  TRANSFORM(pattern->movei, pattern->movej, &ti, &tj, ll);
-  ti += m;
-  tj += n;
-  move = POS(ti, tj);
+  move = AFFINE_TRANSFORM(pattern->movei, pattern->movej, ll, m, n);
   
   /* If the pattern has a constraint, call the autohelper to see
    * if the pattern must be rejected.
@@ -1766,13 +1755,9 @@
   for (k = 0; k < pattern->patlen; ++k) { /* match each point */
     if (pattern->patn[k].att == ATT_O) {
       /* transform pattern real coordinate */
-      int x, y;
-      int str;
-      TRANSFORM(pattern->patn[k].x, pattern->patn[k].y, &x, &y, ll);
-      x += m;
-      y += n;
-
-      str = worm[POS(x, y)].origin;
+      int pos = AFFINE_TRANSFORM(pattern->patn[k].x, pattern->patn[k].y,
+                                ll, m, n);
+      int str = worm[pos].origin;
 
       if (worm[str].attack_codes[0] == 0
          || defense_move_known(move, str))
Index: patterns/connections.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/patterns/connections.c,v
retrieving revision 1.19
diff -u -r1.19 connections.c
--- patterns/connections.c      10 Nov 2001 10:45:57 -0000      1.19
+++ patterns/connections.c      1 Feb 2002 16:20:33 -0000
@@ -41,19 +41,15 @@
                     int ll, void *data)
 {
   int anchor = POS(m, n);
-  int stari, starj;
   int move;
   int k;
   int first_dragon  = NO_MOVE;
   int second_dragon = NO_MOVE;
 
-  int other=OTHER_COLOR(color);
+  int other = OTHER_COLOR(color);
   UNUSED(data);
   
-  TRANSFORM(pattern->movei, pattern->movej, &stari, &starj, ll);
-  stari += m;
-  starj += n;
-  move = POS(stari, starj);
+  move = AFFINE_TRANSFORM(pattern->movei, pattern->movej, ll, m, n);
   
   if ((pattern->class & CLASS_B) && !safe_move(move, other))
     return;
@@ -64,14 +60,9 @@
    */
   if (pattern->class & CLASS_C) {
     for (k = 0; k < pattern->patlen; ++k) { /* match each point */
-      int x, y; /* absolute (board) co-ords of (transformed) pattern element */
-      int pos;
-      
       /* transform pattern real coordinate */
-      TRANSFORM(pattern->patn[k].x, pattern->patn[k].y, &x, &y, ll);
-      x += m;
-      y += n;
-      pos = POS(x, y);
+      int pos = AFFINE_TRANSFORM(pattern->patn[k].x, pattern->patn[k].y,
+                                ll, m, n);
       
       /* Look for distinct dragons. */
       if (pattern->patn[k].att == ATT_O) {
@@ -111,17 +102,12 @@
     for (k = 0; k < pattern->patlen; ++k) { /* match each point */
       if (pattern->patn[k].att == ATT_X) {
        /* transform pattern real coordinate */
-       int x, y;
-       int pos;
-       
-       TRANSFORM(pattern->patn[k].x, pattern->patn[k].y, &x, &y, ll);
-       x += m;
-       y += n;
-       pos = POS(x, y);
+       int pos = AFFINE_TRANSFORM(pattern->patn[k].x, pattern->patn[k].y,
+                                  ll, m, n);
 
        if (worm[pos].attack_codes[0] == WIN
-         && (pattern->movei == -1
-             || !does_defend(move, pos)))
+           && (pattern->movei == -1
+               || !does_defend(move, pos)))
          return; /* Match failed */
       }
     }
@@ -177,19 +163,14 @@
   first_dragon  = NO_MOVE;
   second_dragon = NO_MOVE;
   for (k = 0; k < pattern->patlen; ++k) { /* match each point */
-    int x, y; /* absolute (board) co-ords of (transformed) pattern element */
-    int pos;
-    
     /* transform pattern real coordinate */
-    TRANSFORM(pattern->patn[k].x, pattern->patn[k].y, &x, &y, ll);
-    x += m;
-    y += n;
-    pos = POS(x, y);
+    int pos = AFFINE_TRANSFORM(pattern->patn[k].x, pattern->patn[k].y,
+                              ll, m, n);
 
     /* Look for dragons to amalgamate. Never amalgamate stones which
      * can be attacked.
      */
-    if ((pattern->class & CLASS_C) && (BOARD(x, y) == color)
+    if ((pattern->class & CLASS_C) && (board[pos] == color)
        && (worm[pos].attack_codes[0] == 0)) {
       if (first_dragon == NO_MOVE)
        first_dragon = dragon[pos].origin;
Index: patterns/patterns.h
===================================================================
RCS file: /cvsroot/gnugo/gnugo/patterns/patterns.h,v
retrieving revision 1.19
diff -u -r1.19 patterns.h
--- patterns/patterns.h 30 Jan 2002 23:30:02 -0000      1.19
+++ patterns/patterns.h 1 Feb 2002 16:20:33 -0000
@@ -83,6 +83,16 @@
   *tj = transformations[trans][1][0] * (i) + transformations[trans][1][1] * 
(j); \
 } while (0)
 
+/* The ordinary TRANSFORM plus translation with (m, n), returned as a
+ * 1D coordinate.
+ */
+#define AFFINE_TRANSFORM(i, j, trans, m, n) \
+  POS((m) + transformations[trans][0][0] * (i) \
+          + transformations[trans][0][1] * (j), \
+      (n) + transformations[trans][1][0] * (i) \
+          + transformations[trans][1][1] * (j))
+
+
 #define ATTACK_MACRO(pos) ((stackp==0) ? (worm[pos].attack_codes[0]) : 
attack(pos, NULL))
 #define DEFEND_MACRO(pos) ((stackp==0) ? (worm[pos].defend_codes[0]) : 
find_defense(pos, NULL))
 #define DRAGON_WEAK(pos) (DRAGON2(pos).safety != ALIVE \



reply via email to

[Prev in Thread] Current Thread [Next in Thread]