home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / vifs / edprob.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-20  |  6.0 KB  |  284 lines

  1. /*
  2.  *    edprob.c  --  edit probabilities.
  3.  *
  4.  *    11 july 1989  Olle Olsson.
  5.  */
  6.  
  7. #include <stdlib.h>        /* for atof() prototype */
  8. #include <string.h>        /* for strlen() prototype */
  9. #include "edtrans.h"
  10.  
  11. /* factor for increase/decrease value */
  12. #define INCDEC_FACT 1.1
  13.  
  14. /* local variables */
  15.  
  16. static menudescr pmenu =        /* the menu */
  17.     {
  18.     "quit",
  19.     "density",
  20.     "equal", "=area",
  21.     "Grp#+", "grp#-",
  22.     "Prgrp+", "prgrp- ",
  23.     "0.1", ".5",
  24.     "+ ", "- "
  25.     };
  26.  
  27. void edprob( ifsdes *dp )
  28. {
  29. int i;
  30. struct gm_status gs;        /* mouse status */
  31. transform *tpc;            /* current transform */
  32. char *fv = " Pr: %.4f ";    /* value formatting */
  33. char *fgv = " Grp %d: %.3f ";    /* group value formatting */
  34. int dvx;            /* x position for value display */
  35. int dgvx;            /* x position for group value */
  36. int dgvy;            /* y position for group value */
  37. double gpr;            /* group prob. */
  38. transform *tp;            /* tmp */
  39. char buf[100];            /* tmp */
  40.  
  41. /*
  42.  * The sum of the probability values in the ifsdes struct (tp -> prob) is 1.
  43.  * The value shown for a transform is relative to the group sum.
  44.  */
  45.  
  46. /* normalize */
  47. normprob( dp -> tp, dp -> size );
  48.  
  49. /* hide the mouse pointer */
  50. gm_hide();
  51.  
  52. /* show the menu (the caller has removed its menu) */
  53. setcolor( dp -> textcolor );
  54. showmenu( &pmenu );
  55.  
  56. /* assume that the menu doesn't cover all of the top line */
  57. sprintf( buf, fv, 0.0 );
  58. dvx = MAX_COL - textwidth( buf );
  59.  
  60. /* the group value is shown below the menu line */
  61. dgvy = textheight( buf );
  62. sprintf( buf, fgv, dp -> maxgroup, 0.0 );
  63. dgvx = MAX_COL - textwidth( buf );
  64.  
  65.  
  66. gm_show();
  67.  
  68. for (tpc = 0;;)
  69.     {
  70.     switch (wbup(), getkey( &gs ))
  71.         {
  72.         case 'q':    /* quit */
  73.             /* remove the menu */
  74.             gm_hide();
  75.             nomenu();
  76.             gm_show();
  77.             return;
  78.  
  79.         case 'd':    /* edit density */
  80.             /* remove the menu and ask for a new value */
  81.             gm_hide();
  82.             nomenu();
  83.             setcolor( dp -> textcolor );
  84.             moveto( 0, 0 );
  85.             sprintf( buf,
  86.              "Density %g (%ld iter; prev %.3g), new: ",
  87.                 dp -> density,
  88.                 DENS2ITER( dp -> density ),
  89.                 dp -> prrdens );
  90.  
  91.             gputs( buf );
  92.             ggets( buf );
  93.  
  94.             /* there could be more checking here... */
  95.             if (strlen( buf ) > 0)
  96.                 dp -> density = atof( buf );
  97.  
  98.             /* show the menu again */
  99.             /* cheat a bit to avoid redrawing */
  100.             showmenu( &pmenu );
  101.             nomenu();        /* (clears the menu area) */
  102.             showmenu( &pmenu );
  103.  
  104.             gm_show();
  105.             break;
  106.  
  107.         case 'G':    /* increase group number */
  108.             if (!tpc)
  109.                 break;
  110.  
  111.             /* wrap to zero if necessary */
  112.             if (++tpc -> group > dp -> maxgroup)
  113.                 tpc -> group = 0;
  114.  
  115.             break;
  116.  
  117.         case 'g':    /* decrease group number */
  118.             if (!tpc)
  119.                 break;
  120.  
  121.             /* wrap backwards from zero if necessary */
  122.             if (--tpc -> group < 0)
  123.                 tpc -> group = dp -> maxgroup;
  124.  
  125.             break;
  126.  
  127.         case 'P':    /* increase group probability */
  128.             if (!tpc)
  129.                 break;
  130.  
  131.             /* increase all members of the group */
  132.             for (tp = dp -> tp, i = dp -> size; i > 0; ++tp, --i)
  133.                 if (tp -> group == tpc -> group)
  134.                     tp -> prob *= INCDEC_FACT;
  135.  
  136.             /* normalize */
  137.             normprob( dp -> tp, dp -> size );
  138.  
  139.             break;
  140.  
  141.         case 'p':    /* decrease group probability */
  142.             if (!tpc)
  143.                 break;
  144.  
  145.             /* decrease all members of the group */
  146.             for (tp = dp -> tp, i = dp -> size; i > 0; ++tp, --i)
  147.                 if (tp -> group == tpc -> group)
  148.                     tp -> prob /= INCDEC_FACT;
  149.  
  150.             /* normalize */
  151.             normprob( dp -> tp, dp -> size );
  152.  
  153.             break;
  154.  
  155.         case '+':    /* increase */
  156.             if (!tpc)
  157.                 break;
  158.  
  159.             /* get group prob. */
  160.             gpr = grprob( dp -> tp, dp -> size, tpc -> group );
  161.  
  162.             tpc -> prob *= INCDEC_FACT;
  163.  
  164.             /* if it was zero it still is zero, check! */
  165.             if (tpc -> prob < MINPROB)
  166.                 tpc -> prob = MINPROB;
  167.  
  168.             /* normalize group */
  169.             normgprob( dp -> tp, dp -> size, tpc -> group, gpr );
  170.  
  171.             break;
  172.  
  173.         case '-':    /* decrease */
  174.             if (!tpc)
  175.                 break;
  176.  
  177.             /* get group prob. */
  178.             gpr = grprob( dp -> tp, dp -> size, tpc -> group );
  179.  
  180.             tpc -> prob /= INCDEC_FACT;
  181.  
  182.             /* not too small */
  183.             if (tpc -> prob < MINPROB)
  184.                 tpc -> prob = MINPROB;
  185.  
  186.             /* normalize group */
  187.             normgprob( dp -> tp, dp -> size, tpc -> group, gpr );
  188.  
  189.             break;
  190.  
  191.         case '0':    /* 0.1 */
  192.             if (!tpc)
  193.                 break;
  194.  
  195.             /* get group prob. */
  196.             gpr = grprob( dp -> tp, dp -> size, tpc -> group );
  197.  
  198.             /* make it .1 (within group) after normalization */
  199.             tpc -> prob = (gpr - tpc -> prob) / 9;
  200.  
  201.             /* normalize group */
  202.             normgprob( dp -> tp, dp -> size, tpc -> group, gpr );
  203.  
  204.             break;
  205.  
  206.         case '.':    /* .5 */
  207.             if (!tpc)
  208.                 break;
  209.  
  210.             /* get group prob. */
  211.             gpr = grprob( dp -> tp, dp -> size, tpc -> group );
  212.  
  213.             /* make it .5 (within group) after normalization */
  214.             tpc -> prob = gpr - tpc -> prob;
  215.  
  216.             /* normalize group */
  217.             normgprob( dp -> tp, dp -> size, tpc -> group, gpr );
  218.  
  219.             break;
  220.  
  221.         case 'e':    /* equal values */
  222.             for (tp = dp -> tp, i = dp -> size; i > 0; ++tp, --i)
  223.                 tp -> prob = 1.0 / dp -> size;
  224.  
  225.             break;
  226.  
  227.         case '=':    /* normalize (area proportional) */
  228.             for (tp = dp -> tp, i = dp -> size; i > 0; ++tp, --i)
  229.                 {
  230.                 tp -> prob = dett( tp );
  231.  
  232.                 /* not too small */
  233.                 if (tp -> prob < MINPROB)
  234.                     tp -> prob = MINPROB;
  235.                 }
  236.  
  237.             /* normalize */
  238.             normprob( dp -> tp, dp -> size );
  239.  
  240.             break;
  241.  
  242.         case 0:        /* a mouse button */
  243.  
  244.             /* a transform is selected, find out which one! */
  245.             i = findtrans( dp -> tp, dp -> size, &dp -> area,
  246.                          gs.gm_xpos, gs.gm_ypos );
  247.  
  248.             /* found it? */
  249.             if (i < 0)
  250.                 break;    /* no */
  251.  
  252.             /* set the pointer for current transform */
  253.             tpc = dp -> tp + i;
  254.  
  255.             break;
  256.  
  257.         default:    /* ?? */
  258.             break;
  259.         }
  260.  
  261.     /* show the value */
  262.     if (tpc)
  263.         {
  264.         gm_hide();
  265.  
  266.         /* get group prob. */
  267.         gpr = grprob( dp -> tp, dp -> size, tpc -> group );
  268.  
  269.         /* show the probability within the group */
  270.         sprintf( buf, fv, (gpr > 0) ? tpc -> prob / gpr : 0 );
  271.         moveto( dvx, 0 );
  272.         gputs( buf );
  273.  
  274.         /* show the group number and probability */
  275.         sprintf( buf, fgv, tpc -> group, gpr );
  276.         moveto( dgvx, dgvy );
  277.         gputs( buf );
  278.  
  279.         gm_show();
  280.         }
  281.     }
  282. }
  283.  
  284.