home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / gemsiii / ndline.c < prev    next >
Text File  |  1992-03-30  |  3KB  |  88 lines

  1. /* Badouel / Wuthrich "Face Connected Line Segment Generation in an 
  2.    N-dimensional Space" 
  3. */
  4. /******************************************************************************
  5. Data structure for n-dimensional line segment generation. Initialized by the 
  6. procedure Init() and used by the procedure Incr().
  7. ******************************************************************************/
  8.  
  9. #define DIM   4                  /* number of dimensions                     */
  10.  
  11. typedef struct {
  12.   int  D[DIM];                   /* counter for each of the N dimensions     */
  13.   int  N[DIM];                   /* increment for each of the N dimensions   */
  14.   int  S[DIM];                   /* orientation for each of the N dimensions */
  15.   int  cm;                       /* common multiple                          */
  16. } Nline;
  17.  
  18. Nline   line;
  19.  
  20. /******************************************************************************
  21. Init(): initializes the data structure in order to generate the discrete
  22. path between the point P and the point Q in an n-dimensional space.
  23.  
  24. This procedure should be called once before using Incr().
  25.  
  26. Entry:    P    - origin point
  27.         Q    - destination point
  28.         line - line segment data structure
  29. ******************************************************************************/
  30.  
  31. void Init (P, Q, line)
  32.      int   *P, *Q;
  33.      Nline *line;
  34. {
  35.   int i, v;
  36.  
  37.   line->cm = 1;
  38.   for (i=0; i<DIM; i++) {
  39.     v = Q[i] - P[i];
  40.     if (v < 0) {
  41.       line->S[i] = -1; line->N[i] = -v;
  42.     } else {
  43.       line->S[i] =  1; line->N[i] =  v;
  44.     }
  45.     if (line->N[i] != 0) line->cm *= line->N[i];
  46.   }
  47.   for (i=0; i<DIM; i++) {
  48.     if (line->N[i] == 0) 
  49.       line->D[i] = 2*line->cm;
  50.     else {
  51.       line->D[i] = line->cm/line->N[i];
  52.       line->N[i] = 2*line->D[i];
  53.     }
  54.   }
  55. }
  56.  
  57. /******************************************************************************
  58. Incr(): generates one step of a discrete segment line in an n-dimensional 
  59. space. Indicate the end of the generation with the returned value -1 for 
  60. the direction.
  61.  
  62. The procedure Init() must be called once before using Incr(). 
  63.  
  64. Entry:    line - line segment data structure
  65. Exit:   d    - current step direction
  66.         s    - current step orientation
  67. ******************************************************************************/
  68.  
  69. int Incr (line, d, s)
  70.      Nline *line;
  71.      int   *d, *s;
  72. {
  73.   int i, v = 2*line->cm;
  74.  
  75.   *d = -1;
  76.   for (i=0; i<DIM; i++) {
  77.     if (line->D[i] < v) {
  78.       v = line->D[i];
  79.       *d = i;
  80.     }
  81.   }
  82.   line->D[*d] += line->N[*d];
  83.   *s = line->S[*d];
  84.   return *d;
  85. }
  86.  
  87.  
  88.