main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /****************************************************************************
  2. *
  3. * MODULE: v.kernel
  4. *
  5. * AUTHOR(S): Stefano Menegon, ITC-irst, Trento, Italy
  6. * PURPOSE: Generates a raster density map from vector points data using
  7. * a moving 2D isotropic Gaussian kernel or
  8. * optionally generates a vector density map on vector network
  9. * with a 1D kernel
  10. * COPYRIGHT: (C) 2004 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <math.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <float.h>
  21. #include <grass/gis.h>
  22. #include <grass/glocale.h>
  23. #include <grass/gmath.h>
  24. #include <grass/Vect.h>
  25. #include "global.h"
  26. static int ndists; /* number of distances in dists */
  27. static double *dists; /* array of all distances < dmax */
  28. static int npoints;
  29. int net = 0;
  30. static double dimension = 2.;
  31. /* define score function L(window size) */
  32. double L(double smooth)
  33. {
  34. int ii;
  35. double resL, n, term;
  36. n = npoints;
  37. resL = 0.;
  38. term = 1. / pow((2. * M_PI), dimension / 2.);
  39. for (ii = 0; ii < ndists; ii++) {
  40. /* resL+= gaussianFunction(dists[ii]/smooth,2.,dimension) - 2. * gaussianKernel(dists[ii]/smooth,term); */
  41. resL +=
  42. gaussianFunction(dists[ii] / smooth, 2.,
  43. dimension) -
  44. 2. * gaussianFunction(dists[ii] / smooth, 1., dimension);
  45. }
  46. if (!net)
  47. resL *= 2.;
  48. resL =
  49. (1. / (pow(n, 2.) * pow(smooth, dimension))) * (resL +
  50. n *
  51. (gaussianFunction
  52. (0., 2.,
  53. dimension) -
  54. 2. *
  55. gaussianFunction(0.,
  56. 1.,
  57. dimension)))
  58. + (2. / (n * pow(smooth, dimension))) * gaussianFunction(0., 1.,
  59. dimension);
  60. /* resL = (1./(pow(n,2.)*pow(smooth,dimension))) * (resL + n*( gaussianFunction(0.,2.,dimension) - 2. * gaussianKernel(0.,term)) ) + (2./(n*pow(smooth,dimension)))*gaussianKernel(0.,term); */
  61. G_debug(3, "smooth = %e resL = %e", smooth, resL);
  62. G_message(_("\tScore Value=%f\tsmoothing parameter (standard deviation)=%f"),
  63. resL, smooth);
  64. return (resL);
  65. }
  66. int main(int argc, char **argv)
  67. {
  68. struct Option *in_opt, *net_opt, *out_opt;
  69. struct Option *stddev_opt, *dsize_opt, *segmax_opt, *netmax_opt,
  70. *multip_opt;
  71. struct Flag *flag_o, *flag_q;
  72. char *mapset;
  73. struct Map_info In, Net, Out;
  74. int fdout = 0, maskfd = 0;
  75. int row, col;
  76. struct Cell_head window;
  77. double gaussian;
  78. double N, E;
  79. CELL *mask = NULL;
  80. DCELL *output_cell = NULL;
  81. double sigma, dmax, segmax, netmax, multip;
  82. double **coordinate;
  83. double sigmaOptimal;
  84. struct GModule *module;
  85. double dsize;
  86. double term;
  87. double gausmax = 0;
  88. /* Initialize the GIS calls */
  89. G_gisinit(argv[0]);
  90. module = G_define_module();
  91. module->keywords = _("vector, kernel density");
  92. module->description =
  93. _("Generates a raster density map from vector points data using a moving 2D isotropic Gaussian kernel or "
  94. "optionally generates a vector density map on vector network with a 1D kernel.");
  95. in_opt = G_define_standard_option(G_OPT_V_INPUT);
  96. in_opt->description = _("Input vector with training points");
  97. net_opt = G_define_standard_option(G_OPT_V_INPUT);
  98. net_opt->key = "net";
  99. net_opt->description = _("Input network vector map");
  100. net_opt->required = NO;
  101. out_opt = G_define_option();
  102. out_opt->key = "output";
  103. out_opt->type = TYPE_STRING;
  104. out_opt->key_desc = "name";
  105. out_opt->required = YES;
  106. out_opt->description = _("Output raster/vector map");
  107. stddev_opt = G_define_option();
  108. stddev_opt->key = "stddeviation";
  109. stddev_opt->type = TYPE_DOUBLE;
  110. stddev_opt->required = YES;
  111. stddev_opt->description = _("Standard deviation in map units");
  112. dsize_opt = G_define_option();
  113. dsize_opt->key = "dsize";
  114. dsize_opt->type = TYPE_DOUBLE;
  115. dsize_opt->required = NO;
  116. dsize_opt->description = _("Discretization error in map units");
  117. dsize_opt->answer = "0.";
  118. segmax_opt = G_define_option();
  119. segmax_opt->key = "segmax";
  120. segmax_opt->type = TYPE_DOUBLE;
  121. segmax_opt->required = NO;
  122. segmax_opt->description = _("Maximum length of segment on network");
  123. segmax_opt->answer = "100.";
  124. netmax_opt = G_define_option();
  125. netmax_opt->key = "distmax";
  126. netmax_opt->type = TYPE_DOUBLE;
  127. netmax_opt->required = NO;
  128. netmax_opt->description = _("Maximum distance from point to network");
  129. netmax_opt->answer = "100.";
  130. multip_opt = G_define_option();
  131. multip_opt->key = "mult";
  132. multip_opt->type = TYPE_DOUBLE;
  133. multip_opt->required = NO;
  134. multip_opt->description = _("Multiply the density result by this number");
  135. multip_opt->answer = "1.";
  136. flag_o = G_define_flag();
  137. flag_o->key = 'o';
  138. flag_o->description =
  139. _("Try to calculate an optimal standard deviation with 'stddeviation' taken as maximum (experimental)");
  140. flag_q = G_define_flag();
  141. flag_q->key = 'q';
  142. flag_q->description =
  143. _("Only calculate optimal standard deviation and exit (no map is written)");
  144. if (G_parser(argc, argv))
  145. exit(EXIT_FAILURE);
  146. /*read options */
  147. sigma = atof(stddev_opt->answer);
  148. dsize = atof(dsize_opt->answer);
  149. segmax = atof(segmax_opt->answer);
  150. netmax = atof(netmax_opt->answer);
  151. multip = atof(multip_opt->answer);
  152. if (flag_q->answer) {
  153. flag_o->answer = 1;
  154. }
  155. if (net_opt->answer) {
  156. Vect_check_input_output_name(in_opt->answer, out_opt->answer,
  157. GV_FATAL_EXIT);
  158. Vect_check_input_output_name(net_opt->answer, out_opt->answer,
  159. GV_FATAL_EXIT);
  160. }
  161. G_get_window(&window);
  162. G_message("STDDEV: %f\nRES: %f\tROWS: %d\tCOLS: %d",
  163. sigma, window.ew_res, window.rows, window.cols);
  164. /* Open input vector */
  165. if ((mapset = G_find_vector2(in_opt->answer, "")) == NULL)
  166. G_fatal_error(_("Vector map <%s> not found"), in_opt->answer);
  167. Vect_set_open_level(2);
  168. Vect_open_old(&In, in_opt->answer, mapset);
  169. if (net_opt->answer) {
  170. int nlines, line;
  171. int notreachable = 0;
  172. struct line_pnts *Points;
  173. Points = Vect_new_line_struct();
  174. net = 1;
  175. dimension = 1.;
  176. /* Open input network */
  177. if ((mapset = G_find_vector2(net_opt->answer, "")) == NULL)
  178. G_fatal_error(_("Network input map <%s> not found"),
  179. net_opt->answer);
  180. Vect_set_open_level(2);
  181. Vect_open_old(&Net, net_opt->answer, mapset);
  182. Vect_net_build_graph(&Net, GV_LINES, 0, 0, NULL, NULL, NULL, 0, 0);
  183. if (!flag_q->answer) {
  184. Vect_open_new(&Out, out_opt->answer, 0);
  185. Vect_hist_command(&Out);
  186. }
  187. /* verify not reachable points */
  188. nlines = Vect_get_num_lines(&In);
  189. for (line = 1; line <= nlines; line++) {
  190. int ltype;
  191. ltype = Vect_read_line(&In, Points, NULL, line);
  192. if (!(ltype & GV_POINTS))
  193. continue;
  194. if (Vect_find_line
  195. (&Net, Points->x[0], Points->y[0], 0.0, GV_LINES, netmax, 0,
  196. 0) == 0)
  197. notreachable++;
  198. }
  199. if (notreachable > 0)
  200. G_warning(_("%d points outside threshold"), notreachable);
  201. }
  202. else {
  203. /* check and open the name of output map */
  204. if (!flag_q->answer) {
  205. if (G_legal_filename(out_opt->answer) < 0)
  206. G_fatal_error(_("<%s> is an illegal file name"),
  207. out_opt->answer);
  208. G_set_fp_type(DCELL_TYPE);
  209. if ((fdout = G_open_raster_new(out_opt->answer, DCELL_TYPE)) < 0)
  210. G_fatal_error(_("Unable to create raster map <%s>"),
  211. out_opt->answer);
  212. /* open mask file */
  213. if ((maskfd = G_maskfd()) >= 0)
  214. mask = G_allocate_cell_buf();
  215. else
  216. mask = NULL;
  217. /* allocate output raster */
  218. output_cell = G_allocate_raster_buf(DCELL_TYPE);
  219. }
  220. }
  221. /* valutazione distanza ottimale */
  222. if (flag_o->answer) {
  223. /* Note: sigmaOptimal calculates using ALL points (also those outside the region) */
  224. G_message(_("Automatic choose of smoothing parameter (standard deviation), maximum possible "
  225. "value of standard deviation is was set to %f"), sigma);
  226. /* maximum distance 4*sigma (3.9*sigma ~ 1.0000), keep it small, otherwise it takes
  227. * too much points and calculation on network becomes slow */
  228. dmax = 4 * sigma; /* used as maximum value */
  229. G_message(_("Using maximum distance between points: %f"), dmax);
  230. if (net_opt->answer) {
  231. npoints = Vect_get_num_primitives(&In, GV_POINTS);
  232. /* Warning: each distance is registered twice (both directions) */
  233. ndists =
  234. compute_all_net_distances(&In, &Net, netmax, &dists, dmax);
  235. }
  236. else {
  237. /* Read points */
  238. npoints = read_points(&In, &coordinate, dsize);
  239. ndists = compute_all_distances(coordinate, &dists, npoints, dmax);
  240. }
  241. G_message(_("Number of input points: %d."), npoints);
  242. G_message(_("%d distances read from the map."), ndists);
  243. if (ndists == 0)
  244. G_fatal_error(_("Distances between all points are beyond %e (4 * "
  245. "standard deviation), unable to calculate optimal value."),
  246. dmax);
  247. /* double iii;
  248. for ( iii = 1.; iii <= 10000; iii++){
  249. fprintf(stderr,"i=%f v=%.16f \n",iii,R(iii));
  250. } */
  251. /* sigma is used in brent as maximum possible value for sigmaOptimal */
  252. sigmaOptimal = brent_iterate(L, 0.0, sigma, 1000);
  253. G_message(_("Optimal smoothing parameter (standard deviation): %f."),
  254. sigmaOptimal);
  255. /* Reset sigma to calculated optimal value */
  256. sigma = sigmaOptimal;
  257. if (flag_q->answer) {
  258. Vect_close(&In);
  259. if (net_opt->answer)
  260. Vect_close(&Net);
  261. exit(EXIT_SUCCESS);
  262. }
  263. }
  264. term = 1. / (pow(sigma, dimension) * pow((2. * M_PI), dimension / 2.));
  265. dmax = sigma * 4.;
  266. if (net) {
  267. int line, nlines;
  268. struct line_pnts *Points, *SPoints;
  269. struct line_cats *SCats;
  270. G_message(_("\nWriting output vector map using smooth parameter=%f."),
  271. sigma);
  272. G_message(_("\nNormalising factor=%f."),
  273. 1. / gaussianFunction(sigma / 4., sigma, dimension));
  274. /* Divide lines to segments and calculate gaussian for center of each segment */
  275. Points = Vect_new_line_struct();
  276. SPoints = Vect_new_line_struct();
  277. SCats = Vect_new_cats_struct();
  278. nlines = Vect_get_num_lines(&Net);
  279. G_debug(3, "net nlines = %d", nlines);
  280. for (line = 1; line <= nlines; line++) {
  281. int seg, nseg, ltype;
  282. double llength, length, x, y;
  283. ltype = Vect_read_line(&Net, Points, NULL, line);
  284. if (!(ltype & GV_LINES))
  285. continue;
  286. llength = Vect_line_length(Points);
  287. nseg = (int)(1 + llength / segmax);
  288. length = llength / nseg;
  289. G_debug(3, "net line = %d, nseg = %d, seg length = %f", line,
  290. nseg, length);
  291. for (seg = 0; seg < nseg; seg++) {
  292. double offset1, offset2;
  293. offset1 = (seg + 0.5) * length;
  294. Vect_point_on_line(Points, offset1, &x, &y, NULL, NULL, NULL);
  295. G_debug(3, " segment = %d, offset = %f, xy = %f %f", seg,
  296. offset1, x, y);
  297. compute_net_distance(x, y, &In, &Net, netmax, sigma, term,
  298. &gaussian, dmax);
  299. gaussian *= multip;
  300. if (gaussian > gausmax)
  301. gausmax = gaussian;
  302. G_debug(3, " gaussian = %f", gaussian);
  303. /* Write segment */
  304. if (gaussian > 0) {
  305. offset1 = seg * length;
  306. offset2 = (seg + 1) * length;
  307. if (offset2 > llength)
  308. offset2 = llength;
  309. Vect_line_segment(Points, offset1, offset2, SPoints);
  310. /* TODO!!! remove later
  311. if ( SPoints->n_points > 0 )
  312. Vect_append_point( SPoints, SPoints->x[SPoints->n_points-1],
  313. SPoints->y[SPoints->n_points-1], 0 );
  314. */
  315. Vect_reset_cats(SCats);
  316. Vect_cat_set(SCats, 1, (int)gaussian);
  317. Vect_write_line(&Out, GV_LINE, SPoints, SCats);
  318. }
  319. }
  320. G_percent(line, nlines, 1);
  321. }
  322. Vect_close(&Net);
  323. Vect_build(&Out, stderr);
  324. Vect_close(&Out);
  325. }
  326. else {
  327. G_message(_("\nWriting output raster map using smooth parameter=%f."),
  328. sigma);
  329. G_message(_("\nNormalising factor=%f."),
  330. 1. / gaussianFunction(sigma / 4., sigma, dimension));
  331. for (row = 0; row < window.rows; row++) {
  332. G_percent(row, window.rows, 2);
  333. if (mask) {
  334. if (G_get_map_row(maskfd, mask, row) < 0)
  335. G_fatal_error(_("Unable to read MASK"));
  336. }
  337. for (col = 0; col < window.cols; col++) {
  338. /* don't interpolate outside of the mask */
  339. if (mask && mask[col] == 0) {
  340. G_set_d_null_value(&output_cell[col], 1);
  341. continue;
  342. }
  343. N = G_row_to_northing(row + 0.5, &window);
  344. E = G_col_to_easting(col + 0.5, &window);
  345. compute_distance(N, E, &In, sigma, term, &gaussian, dmax);
  346. output_cell[col] = multip * gaussian;
  347. if (gaussian > gausmax)
  348. gausmax = gaussian;
  349. }
  350. G_put_raster_row(fdout, output_cell, DCELL_TYPE);
  351. }
  352. G_close_cell(fdout);
  353. }
  354. G_message(_("Maximum value in output: %e."), gausmax);
  355. Vect_close(&In);
  356. exit(EXIT_SUCCESS);
  357. }
  358. /* Read points to array return number of points */
  359. int read_points(struct Map_info *In, double ***coordinate, double dsize)
  360. {
  361. int line, nlines, npoints, ltype, i = 0;
  362. double **xySites;
  363. static struct line_pnts *Points = NULL;
  364. if (!Points)
  365. Points = Vect_new_line_struct();
  366. /* Allocate array of pointers */
  367. npoints = Vect_get_num_primitives(In, GV_POINT);
  368. xySites = (double **)G_calloc(npoints, sizeof(double *));
  369. nlines = Vect_get_num_lines(In);
  370. for (line = 1; line <= nlines; line++) {
  371. ltype = Vect_read_line(In, Points, NULL, line);
  372. if (!(ltype & GV_POINT))
  373. continue;
  374. xySites[i] = (double *)G_calloc((size_t) 2, sizeof(double));
  375. xySites[i][0] = Points->x[0];
  376. xySites[i][1] = Points->y[0];
  377. i++;
  378. }
  379. *coordinate = xySites;
  380. return (npoints);
  381. }
  382. /* Calculate distances < dmax between all sites in coordinate
  383. * Return: number of distances in dists */
  384. double compute_all_distances(double **coordinate, double **dists, int n,
  385. double dmax)
  386. {
  387. int ii, jj, kk;
  388. size_t nn;
  389. nn = n * (n - 1) / 2;
  390. *dists = (double *)G_calloc(nn, sizeof(double));
  391. kk = 0;
  392. for (ii = 0; ii < n - 1; ii++) {
  393. for (jj = ii + 1; jj < n; jj++) {
  394. double dist;
  395. dist = euclidean_distance(coordinate[ii], coordinate[jj], 2);
  396. G_debug(3, "dist = %f", dist);
  397. if (dist <= dmax) {
  398. (*dists)[kk] = dist;
  399. kk++;
  400. }
  401. }
  402. }
  403. return (kk);
  404. }
  405. /* Calculate distances < dmax between all sites in coordinate
  406. * Return: number of distances in dists */
  407. double compute_all_net_distances(struct Map_info *In, struct Map_info *Net,
  408. double netmax, double **dists, double dmax)
  409. {
  410. int nn, kk, nalines, aline;
  411. double dist;
  412. struct line_pnts *APoints, *BPoints;
  413. BOUND_BOX box;
  414. struct ilist *List;
  415. APoints = Vect_new_line_struct();
  416. BPoints = Vect_new_line_struct();
  417. List = Vect_new_list();
  418. nn = Vect_get_num_primitives(In, GV_POINTS);
  419. nn = nn * (nn - 1);
  420. *dists = (double *)G_calloc(nn, sizeof(double));
  421. kk = 0;
  422. nalines = Vect_get_num_lines(In);
  423. for (aline = 1; aline <= nalines; aline++) {
  424. int i, altype;
  425. G_debug(3, " aline = %d", aline);
  426. altype = Vect_read_line(In, APoints, NULL, aline);
  427. if (!(altype & GV_POINTS))
  428. continue;
  429. box.E = APoints->x[0] + dmax;
  430. box.W = APoints->x[0] - dmax;
  431. box.N = APoints->y[0] + dmax;
  432. box.S = APoints->y[0] - dmax;
  433. box.T = PORT_DOUBLE_MAX;
  434. box.B = -PORT_DOUBLE_MAX;
  435. Vect_select_lines_by_box(In, &box, GV_POINT, List);
  436. G_debug(3, " %d points in box", List->n_values);
  437. for (i = 0; i < List->n_values; i++) {
  438. int bline, ret;
  439. bline = List->value[i];
  440. if (bline == aline)
  441. continue;
  442. G_debug(3, " bline = %d", bline);
  443. Vect_read_line(In, BPoints, NULL, bline);
  444. ret =
  445. Vect_net_shortest_path_coor(Net, APoints->x[0], APoints->y[0],
  446. 0.0, BPoints->x[0], BPoints->y[0],
  447. 0.0, netmax, netmax, &dist, NULL,
  448. NULL, NULL, NULL, NULL, NULL);
  449. G_debug(3, " SP: %f %f -> %f %f", APoints->x[0], APoints->y[0],
  450. BPoints->x[0], BPoints->y[0]);
  451. if (ret == 0) {
  452. G_debug(3, "not reachable");
  453. continue; /* Not reachable */
  454. }
  455. G_debug(3, " dist = %f", dist);
  456. if (dist <= dmax) {
  457. (*dists)[kk] = dist;
  458. kk++;
  459. }
  460. G_debug(3, " kk = %d", kk);
  461. }
  462. }
  463. return (kk);
  464. }
  465. /* Compute gausian for x, y along Net, using all points in In */
  466. void compute_net_distance(double x, double y, struct Map_info *In,
  467. struct Map_info *Net, double netmax, double sigma,
  468. double term, double *gaussian, double dmax)
  469. {
  470. int i;
  471. double dist;
  472. static struct line_pnts *Points = NULL;
  473. BOUND_BOX box;
  474. static struct ilist *List = NULL;
  475. if (!Points)
  476. Points = Vect_new_line_struct();
  477. if (!List)
  478. List = Vect_new_list();
  479. *gaussian = .0;
  480. /* The network is usually much bigger than dmax and to calculate shortest path is slow
  481. * -> use spatial index to select points */
  482. box.E = x + dmax;
  483. box.W = x - dmax;
  484. box.N = y + dmax;
  485. box.S = y - dmax;
  486. box.T = PORT_DOUBLE_MAX;
  487. box.B = -PORT_DOUBLE_MAX;
  488. Vect_select_lines_by_box(In, &box, GV_POINT, List);
  489. G_debug(3, " %d points in box", List->n_values);
  490. for (i = 0; i < List->n_values; i++) {
  491. int line, ret;
  492. line = List->value[i];
  493. Vect_read_line(In, Points, NULL, line);
  494. G_debug(3, " SP: %f %f -> %f %f", x, y, Points->x[0], Points->y[0]);
  495. ret =
  496. Vect_net_shortest_path_coor(Net, x, y, 0.0, Points->x[0],
  497. Points->y[0], 0.0, netmax, netmax,
  498. &dist, NULL, NULL, NULL, NULL, NULL,
  499. NULL);
  500. if (ret == 0) {
  501. G_debug(3, "not reachable");
  502. continue; /* Not reachable */
  503. }
  504. if (dist <= dmax)
  505. *gaussian += gaussianKernel(dist / sigma, term);
  506. G_debug(3, " dist = %f gaussian = %f", dist, *gaussian);
  507. }
  508. }
  509. void compute_distance(double N, double E, struct Map_info *In,
  510. double sigma, double term, double *gaussian,
  511. double dmax)
  512. {
  513. int line, nlines;
  514. double a[2], b[2];
  515. double dist;
  516. /* spatial index handling, borrowed from lib/vector/Vlib/find.c */
  517. BOUND_BOX box;
  518. static struct ilist *NList = NULL;
  519. static struct line_pnts *Points = NULL;
  520. a[0] = E;
  521. a[1] = N;
  522. if (!NList)
  523. NList = Vect_new_list();
  524. if (!Points)
  525. Points = Vect_new_line_struct();
  526. /* create bounding box 2x2*dmax size from the current cell center */
  527. box.N = N + dmax;
  528. box.S = N - dmax;
  529. box.E = E + dmax;
  530. box.W = E - dmax;
  531. box.T = HUGE_VAL;
  532. box.B = -HUGE_VAL;
  533. /* number of lines within dmax box */
  534. nlines = Vect_select_lines_by_box(In, &box, GV_POINT, NList);
  535. *gaussian = .0;
  536. for (line = 0; line < nlines; line++) {
  537. Vect_read_line(In, Points, NULL, NList->value[line]);
  538. b[0] = Points->x[0];
  539. b[1] = Points->y[0];
  540. dist = euclidean_distance(a, b, 2);
  541. if (dist <= dmax)
  542. *gaussian += gaussianKernel(dist / sigma, term);
  543. }
  544. }