line.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*******************************************************************************
  2. License:
  3. This software and/or related materials was developed at the National Institute
  4. of Standards and Technology (NIST) by employees of the Federal Government
  5. in the course of their official duties. Pursuant to title 17 Section 105
  6. of the United States Code, this software is not subject to copyright
  7. protection and is in the public domain.
  8. This software and/or related materials have been determined to be not subject
  9. to the EAR (see Part 734.3 of the EAR for exact details) because it is
  10. a publicly available technology and software, and is freely distributed
  11. to any interested party with no licensing requirements. Therefore, it is
  12. permissible to distribute this software as a free download from the internet.
  13. Disclaimer:
  14. This software and/or related materials was developed to promote biometric
  15. standards and biometric technology testing for the Federal Government
  16. in accordance with the USA PATRIOT Act and the Enhanced Border Security
  17. and Visa Entry Reform Act. Specific hardware and software products identified
  18. in this software were used in order to perform the software development.
  19. In no case does such identification imply recommendation or endorsement
  20. by the National Institute of Standards and Technology, nor does it imply that
  21. the products and equipment identified are necessarily the best available
  22. for the purpose.
  23. This software and/or related materials are provided "AS-IS" without warranty
  24. of any kind including NO WARRANTY OF PERFORMANCE, MERCHANTABILITY,
  25. NO WARRANTY OF NON-INFRINGEMENT OF ANY 3RD PARTY INTELLECTUAL PROPERTY
  26. or FITNESS FOR A PARTICULAR PURPOSE or for any purpose whatsoever, for the
  27. licensed product, however used. In no event shall NIST be liable for any
  28. damages and/or costs, including but not limited to incidental or consequential
  29. damages of any kind, including economic damage or injury to property and lost
  30. profits, regardless of whether NIST shall be advised, have reason to know,
  31. or in fact shall know of the possibility.
  32. By using this software, you agree to bear all risk relating to quality,
  33. use and performance of the software and/or related materials. You agree
  34. to hold the Government harmless from any claim arising from your use
  35. of the software.
  36. *******************************************************************************/
  37. /***********************************************************************
  38. LIBRARY: LFS - NIST Latent Fingerprint System
  39. FILE: LINE.C
  40. AUTHOR: Michael D. Garris
  41. DATE: 03/16/1999
  42. Contains routines that compute contiguous linear trajectories
  43. between two coordinate points required by the NIST Latent
  44. Fingerprint System (LFS).
  45. ***********************************************************************
  46. ROUTINES:
  47. line_points()
  48. ***********************************************************************/
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include "lfs.h"
  52. /*************************************************************************
  53. **************************************************************************
  54. #cat: line_points - Returns the contiguous coordinates of a line connecting
  55. #cat: 2 specified points.
  56. Input:
  57. x1 - x-coord of first point
  58. y1 - y-coord of first point
  59. x2 - x-coord of second point
  60. y2 - y-coord of second point
  61. Output:
  62. ox_list - x-coords along line trajectory
  63. oy_list - y-coords along line trajectory
  64. onum - number of points along line trajectory
  65. Return Code:
  66. Zero - successful completion
  67. Negative - system error
  68. **************************************************************************/
  69. int line_points(int **ox_list, int **oy_list, int *onum,
  70. const int x1, const int y1, const int x2, const int y2)
  71. {
  72. int asize;
  73. int dx, dy, adx, ady;
  74. int x_incr, y_incr;
  75. int i, inx, iny, intx, inty;
  76. double x_factor, y_factor;
  77. double rx, ry;
  78. int ix, iy;
  79. int *x_list, *y_list;
  80. /* Compute maximum number of points needed to hold line segment. */
  81. asize = max(abs(x2-x1)+2, abs(y2-y1)+2);
  82. /* Allocate x and y-pixel coordinate lists to length 'asize'. */
  83. x_list = (int *)malloc(asize*sizeof(int));
  84. if(x_list == (int *)NULL){
  85. fprintf(stderr, "ERROR : line_points : malloc : x_list\n");
  86. return(-410);
  87. }
  88. y_list = (int *)malloc(asize*sizeof(int));
  89. if(y_list == (int *)NULL){
  90. free(x_list);
  91. fprintf(stderr, "ERROR : line_points : malloc : y_list\n");
  92. return(-411);
  93. }
  94. /* Compute delta x and y. */
  95. dx = x2 - x1;
  96. dy = y2 - y1;
  97. /* Set x and y increments. */
  98. if(dx >= 0)
  99. x_incr = 1;
  100. else
  101. x_incr = -1;
  102. if(dy >= 0)
  103. y_incr = 1;
  104. else
  105. y_incr = -1;
  106. /* Compute |DX| and |DY|. */
  107. adx = abs(dx);
  108. ady = abs(dy);
  109. /* Set x-orientation. */
  110. if(adx > ady)
  111. inx = 1;
  112. else
  113. inx = 0;
  114. /* Set y-orientation. */
  115. if(ady > adx)
  116. iny = 1;
  117. else
  118. iny = 0;
  119. /* CASE 1: |DX| > |DY| */
  120. /* Increment in X by +-1 */
  121. /* in Y by +-|DY|/|DX| */
  122. /* inx = 1 */
  123. /* iny = 0 */
  124. /* intx = 1 (inx) */
  125. /* inty = 0 (iny) */
  126. /* CASE 2: |DX| < |DY| */
  127. /* Increment in Y by +-1 */
  128. /* in X by +-|DX|/|DY| */
  129. /* inx = 0 */
  130. /* iny = 1 */
  131. /* intx = 0 (inx) */
  132. /* inty = 1 (iny) */
  133. /* CASE 3: |DX| == |DY| */
  134. /* inx = 0 */
  135. /* iny = 0 */
  136. /* intx = 1 */
  137. /* inty = 1 */
  138. intx = 1 - iny;
  139. inty = 1 - inx;
  140. /* DX */
  141. /* x_factor = (inx * +-1) + ( iny * ------------ ) */
  142. /* max(1, |DY|) */
  143. /* */
  144. x_factor = (inx * x_incr) + (iny * ((double)dx/max(1, ady)));
  145. /* DY */
  146. /* y_factor = (iny * +-1) + ( inx * ------------ ) */
  147. /* max(1, |DX|) */
  148. /* */
  149. y_factor = (iny * y_incr) + (inx * ((double)dy/max(1, adx)));
  150. /* Initialize integer coordinates. */
  151. ix = x1;
  152. iy = y1;
  153. /* Set floating point coordinates. */
  154. rx = (double)x1;
  155. ry = (double)y1;
  156. /* Initialize to first point in line segment. */
  157. i = 0;
  158. /* Assign first point into coordinate list. */
  159. x_list[i] = x1;
  160. y_list[i++] = y1;
  161. while((ix != x2) || (iy != y2)){
  162. if(i >= asize){
  163. fprintf(stderr, "ERROR : line_points : coord list overflow\n");
  164. free(x_list);
  165. free(y_list);
  166. return(-412);
  167. }
  168. rx += x_factor;
  169. ry += y_factor;
  170. /* Need to truncate precision so that answers are consistent */
  171. /* on different computer architectures when truncating doubles. */
  172. rx = trunc_dbl_precision(rx, TRUNC_SCALE);
  173. ry = trunc_dbl_precision(ry, TRUNC_SCALE);
  174. /* Compute new x and y-pixel coords in floating point and */
  175. /* then round to the nearest integer. */
  176. ix = (intx * (ix + x_incr)) + (iny * (int)(rx + 0.5));
  177. iy = (inty * (iy + y_incr)) + (inx * (int)(ry + 0.5));
  178. /* Assign first point into coordinate list. */
  179. x_list[i] = ix;
  180. y_list[i++] = iy;
  181. }
  182. /* Set output pointers. */
  183. *ox_list = x_list;
  184. *oy_list = y_list;
  185. *onum = i;
  186. /* Return normally. */
  187. return(0);
  188. }