gs_query.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*!
  2. \file lib/ogsf/gs_query.c
  3. \brief OGSF library - query (lower level functions)
  4. GRASS OpenGL gsurf OGSF Library
  5. (C) 1999-2008 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Bill Brown USACERL (January 1994)
  11. \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
  12. */
  13. #include <math.h>
  14. #include <grass/gis.h>
  15. #include <grass/ogsf.h>
  16. /*!
  17. \brief Values needed for Ray-Convex Polyhedron Intersection Test below
  18. originally by Eric Haines, erich@eye.com
  19. */
  20. #ifndef HUGE_VAL
  21. #define HUGE_VAL 1.7976931348623157e+308
  22. #endif
  23. /* return codes */
  24. #define MISSED 0
  25. #define FRONTFACE 1
  26. #define BACKFACE -1
  27. /* end Ray-Convex Polyhedron Intersection Test values */
  28. /*!
  29. \brief Crude method of intersecting line of sight with closest part of surface.
  30. Uses los vector to determine the point of first intersection
  31. which is returned in point. Returns 0 if los doesn't intersect.
  32. \param surfid surface id
  33. \param los should be in surf-world coordinates
  34. \param[out] point intersect point (real)
  35. \return 0 on failure
  36. \return 1 on success
  37. */
  38. int gs_los_intersect1(int surfid, float (*los)[3], float *point)
  39. {
  40. float dx, dy, dz, u_d[3];
  41. float a[3], incr, min_incr, tlen, len;
  42. int outside, above, below, edge, istep;
  43. float b[3];
  44. geosurf *gs;
  45. typbuff *buf;
  46. G_debug(3, "gs_los_intersect1():");
  47. if (NULL == (gs = gs_get_surf(surfid))) {
  48. return (0);
  49. }
  50. if (0 == GS_v3dir(los[FROM], los[TO], u_d)) {
  51. return (0);
  52. }
  53. buf = gs_get_att_typbuff(gs, ATT_TOPO, 0);
  54. istep = edge = below = 0;
  55. len = 0.0;
  56. tlen = GS_distance(los[FROM], los[TO]);
  57. incr = tlen / 1000.0;
  58. min_incr = incr / 1000.0;
  59. dx = incr * u_d[X];
  60. dy = incr * u_d[Y];
  61. dz = incr * u_d[Z];
  62. a[X] = los[FROM][X];
  63. a[Y] = los[FROM][Y];
  64. a[Z] = los[FROM][Z];
  65. b[X] = a[X] - gs->x_trans;
  66. b[Y] = a[Y] - gs->y_trans;
  67. if (viewcell_tri_interp(gs, buf, b, 0)) {
  68. /* expects surface coords */
  69. b[Z] += gs->z_trans;
  70. if (a[Z] < b[Z]) {
  71. /* viewing from below surface */
  72. /* don't use this method
  73. fprintf(stderr,"view from below\n");
  74. below = 1;
  75. */
  76. return (0);
  77. }
  78. }
  79. while (incr > min_incr) {
  80. outside = 0;
  81. above = 0;
  82. b[X] = a[X] - gs->x_trans;
  83. b[Y] = a[Y] - gs->y_trans;
  84. if (viewcell_tri_interp(gs, buf, b, 0)) {
  85. /* ignores masks */
  86. b[Z] += gs->z_trans;
  87. above = (a[Z] > b[Z]);
  88. }
  89. else {
  90. outside = 1;
  91. if (istep > 10) {
  92. edge = 1;
  93. below = 1;
  94. }
  95. }
  96. while (outside || above) {
  97. a[X] += dx;
  98. a[Y] += dy;
  99. a[Z] += dz;
  100. len += incr;
  101. outside = 0;
  102. above = 0;
  103. b[X] = a[X] - gs->x_trans;
  104. b[Y] = a[Y] - gs->y_trans;
  105. if (viewcell_tri_interp(gs, buf, b, 0)) {
  106. b[Z] += gs->z_trans;
  107. above = (a[Z] > b[Z]);
  108. }
  109. else {
  110. outside = 1;
  111. }
  112. if (len > tlen) {
  113. return 0; /* over surface *//* under surface */
  114. }
  115. }
  116. /* could look for spikes here - see if any data points along
  117. shadow of line on surf go above los */
  118. /* back up several spots? */
  119. a[X] -= (1.0 * dx);
  120. a[Y] -= (1.0 * dy);
  121. a[Z] -= (1.0 * dz);
  122. incr /= 2.0;
  123. ++istep;
  124. dx = incr * u_d[X];
  125. dy = incr * u_d[Y];
  126. dz = incr * u_d[Z];
  127. }
  128. if ((edge) && (b[Z] - (a[Z] + dz * 2.0) > incr * u_d[Z])) {
  129. G_debug(3, " looking under surface");
  130. return 0;
  131. }
  132. point[X] = b[X];
  133. point[Y] = b[Y];
  134. point[Z] = b[Z] - gs->z_trans;
  135. return (1);
  136. }
  137. /*!
  138. \brief Crude method of intersecting line of sight with closest part of surface.
  139. This version uses the shadow of the los projected down to
  140. the surface to generate a line_on_surf, then follows each
  141. point in that line until the los intersects it.
  142. \param surfid surface id
  143. \param los should be in surf-world coordinates
  144. \param[out] point intersect point (real)
  145. \return 0 on failure
  146. \return 1 on success
  147. */
  148. int gs_los_intersect(int surfid, float **los, float *point)
  149. {
  150. double incr;
  151. float p1, p2, u_d[3];
  152. int above, ret, num, i, usedx;
  153. float a[3], b[3];
  154. float bgn[3], end[3], a1[3];
  155. geosurf *gs;
  156. typbuff *buf;
  157. Point3 *points;
  158. G_debug(3, "gs_los_intersect");
  159. if (NULL == (gs = gs_get_surf(surfid))) {
  160. return (0);
  161. }
  162. if (0 == GS_v3dir(los[FROM], los[TO], u_d)) {
  163. return (0);
  164. }
  165. buf = gs_get_att_typbuff(gs, ATT_TOPO, 0);
  166. GS_v3eq(bgn, los[FROM]);
  167. GS_v3eq(end, los[TO]);
  168. bgn[X] -= gs->x_trans;
  169. bgn[Y] -= gs->y_trans;
  170. end[X] -= gs->x_trans;
  171. end[Y] -= gs->y_trans;
  172. /* trans? */
  173. points = gsdrape_get_allsegments(gs, bgn, end, &num);
  174. /* DEBUG
  175. {
  176. float t1[3], t2[3];
  177. t1[X] = los[FROM][X] ;
  178. t1[Y] = los[FROM][Y] ;
  179. t2[X] = los[TO][X] ;
  180. t2[Y] = los[TO][Y] ;
  181. GS_set_draw(GSD_FRONT);
  182. gsd_pushmatrix();
  183. gsd_do_scale(1);
  184. gsd_translate(gs->x_trans, gs->y_trans, gs->z_trans);
  185. gsd_linewidth(1);
  186. gsd_color_func(GS_default_draw_color());
  187. gsd_line_onsurf(gs, t1, t2);
  188. gsd_popmatrix();
  189. GS_set_draw(GSD_BACK);
  190. gsd_flush();
  191. }
  192. fprintf(stderr,"%d points to check\n", num);
  193. fprintf(stderr,"point0 = %.6lf %.6lf %.6lf FT =%.6lf %.6lf %.6lf\n",
  194. points[0][X],points[0][Y],points[0][Z],
  195. los[FROM][X],los[FROM][Y],los[FROM][Z]);
  196. fprintf(stderr,"incr1 = %.6lf: %.6lf %.6lf %.6lf\n",incr,u_d[X],u_d[Y],u_d[Z]);
  197. fprintf(stderr,"first point below surf\n");
  198. fprintf(stderr,"incr2 = %f\n", (float)incr);
  199. fprintf(stderr,"(%d/%d) %f > %f\n", i,num, a[Z], points[i][Z]);
  200. fprintf(stderr,"incr3 = %f\n", (float)incr);
  201. fprintf(stderr,"all points above surf\n");
  202. */
  203. if (num < 2) {
  204. G_debug(3, " %d points to check", num);
  205. return (0);
  206. }
  207. /* use larger of deltas for better precision */
  208. usedx = (fabs(u_d[X]) > fabs(u_d[Y]));
  209. if (usedx) {
  210. incr = ((points[0][X] - (los[FROM][X] - gs->x_trans)) / u_d[X]);
  211. }
  212. else if (u_d[Y]) {
  213. incr = ((points[0][Y] - (los[FROM][Y] - gs->y_trans)) / u_d[Y]);
  214. }
  215. else {
  216. point[X] = los[FROM][X] - gs->x_trans;
  217. point[Y] = los[FROM][Y] - gs->y_trans;
  218. return (viewcell_tri_interp(gs, buf, point, 1));
  219. }
  220. /* DEBUG
  221. fprintf(stderr,"-----------------------------\n");
  222. fprintf(stderr,"%d points to check\n", num);
  223. fprintf(stderr,"incr1 = %.6lf: %.9f %.9f %.9f\n",incr,u_d[X],u_d[Y],u_d[Z]);
  224. fprintf(stderr,
  225. "\tpoint0 = %.6f %.6f %.6f\n\tFT = %.6f %.6f %.6f\n\tpoint%d = %.6f %.6f\n",
  226. points[0][X],points[0][Y],points[0][Z],
  227. los[FROM][X],los[FROM][Y],los[FROM][Z],
  228. num-1, points[num-1][X],points[num-1][Y]);
  229. */
  230. /* This should bring us right above (or below) the first point */
  231. a[X] = los[FROM][X] + incr * u_d[X] - gs->x_trans;
  232. a[Y] = los[FROM][Y] + incr * u_d[Y] - gs->y_trans;
  233. a[Z] = los[FROM][Z] + incr * u_d[Z] - gs->z_trans;
  234. if (a[Z] < points[0][Z]) {
  235. /* viewing from below surface */
  236. /* don't use this method */
  237. /* DEBUG
  238. fprintf(stderr,"first point below surf\n");
  239. fprintf(stderr,"aZ= %.6f point0 = %.6f %.6f %.6f FT =%.6f %.6f %.6f\n",
  240. a[Z], points[0][X],points[0][Y],points[0][Z],
  241. los[FROM][X],los[FROM][Y],los[FROM][Z]);
  242. */
  243. return (0);
  244. }
  245. GS_v3eq(a1, a);
  246. GS_v3eq(b, a);
  247. for (i = 1; i < num; i++) {
  248. if (usedx) {
  249. incr = ((points[i][X] - a1[X]) / u_d[X]);
  250. }
  251. else {
  252. incr = ((points[i][Y] - a1[Y]) / u_d[Y]);
  253. }
  254. a[X] = a1[X] + (incr * u_d[X]);
  255. a[Y] = a1[Y] + (incr * u_d[Y]);
  256. a[Z] = a1[Z] + (incr * u_d[Z]);
  257. above = (a[Z] >= points[i][Z]);
  258. if (above) {
  259. GS_v3eq(b, a);
  260. continue;
  261. }
  262. /*
  263. * Now we know b[Z] is above points[i-1]
  264. * and a[Z] is below points[i]
  265. * Since there should only be one polygon along this seg,
  266. * just interpolate to intersect
  267. */
  268. if (usedx) {
  269. incr = ((a[X] - b[X]) / u_d[X]);
  270. }
  271. else {
  272. incr = ((a[Y] - b[Y]) / u_d[Y]);
  273. }
  274. if (1 == (ret = segs_intersect(1.0, points[i][Z],
  275. 0.0, points[i - 1][Z],
  276. 1.0, a[Z], 0.0, b[Z], &p1, &p2))) {
  277. point[X] = points[i - 1][X] + (u_d[X] * incr * p1);
  278. point[Y] = points[i - 1][Y] + (u_d[Y] * incr * p1);
  279. point[Z] = p2;
  280. return (1);
  281. }
  282. G_debug(3, " line of sight error %d", ret);
  283. return 0;
  284. }
  285. /* over surface */
  286. return 0;
  287. }
  288. /*!
  289. \brief Ray-Convex Polyhedron Intersection Test
  290. Originally by Eric Haines, erich@eye.com
  291. This test checks the ray against each face of a polyhedron, checking whether
  292. the set of intersection points found for each ray-plane intersection
  293. overlaps the previous intersection results. If there is no overlap (i.e.
  294. no line segment along the ray that is inside the polyhedron), then the
  295. ray misses and returns 0; else 1 is returned if the ray is entering the
  296. polyhedron, -1 if the ray originates inside the polyhedron. If there is
  297. an intersection, the distance and the nunber of the face hit is returned.
  298. \param org,dir origin and direction of ray
  299. \param tmax maximum useful distance along ray
  300. \param phdrn list of planes in convex polyhedron
  301. \param ph_num number of planes in convex polyhedron
  302. \param[out] tresult distance of intersection along ray
  303. \param[out] pn number of face hit (0 to ph_num-1)
  304. \return FACE code
  305. */
  306. int RayCvxPolyhedronInt(Point3 org, Point3 dir, double tmax, Point4 * phdrn,
  307. int ph_num, double *tresult, int *pn)
  308. {
  309. double tnear, tfar, t, vn, vd;
  310. int fnorm_num, bnorm_num; /* front/back face # hit */
  311. tnear = -HUGE_VAL;
  312. tfar = tmax;
  313. /* Test each plane in polyhedron */
  314. for (; ph_num--;) {
  315. /* Compute intersection point T and sidedness */
  316. vd = DOT3(dir, phdrn[ph_num]);
  317. vn = DOT3(org, phdrn[ph_num]) + phdrn[ph_num][W];
  318. if (vd == 0.0) {
  319. /* ray is parallel to plane - check if ray origin is inside plane's
  320. half-space */
  321. if (vn > 0.0) {
  322. /* ray origin is outside half-space */
  323. return (MISSED);
  324. }
  325. }
  326. else {
  327. /* ray not parallel - get distance to plane */
  328. t = -vn / vd;
  329. if (vd < 0.0) {
  330. /* front face - T is a near point */
  331. if (t > tfar) {
  332. return (MISSED);
  333. }
  334. if (t > tnear) {
  335. /* hit near face, update normal */
  336. fnorm_num = ph_num;
  337. tnear = t;
  338. }
  339. }
  340. else {
  341. /* back face - T is a far point */
  342. if (t < tnear) {
  343. return (MISSED);
  344. }
  345. if (t < tfar) {
  346. /* hit far face, update normal */
  347. bnorm_num = ph_num;
  348. tfar = t;
  349. }
  350. }
  351. }
  352. }
  353. /* survived all tests */
  354. /* Note: if ray originates on polyhedron, may want to change 0.0 to some
  355. * epsilon to avoid intersecting the originating face.
  356. */
  357. if (tnear >= 0.0) {
  358. /* outside, hitting front face */
  359. *tresult = tnear;
  360. *pn = fnorm_num;
  361. return (FRONTFACE);
  362. }
  363. else {
  364. if (tfar < tmax) {
  365. /* inside, hitting back face */
  366. *tresult = tfar;
  367. *pn = bnorm_num;
  368. return (BACKFACE);
  369. }
  370. else {
  371. /* inside, but back face beyond tmax */
  372. return (MISSED);
  373. }
  374. }
  375. }
  376. /*!
  377. \brief Get data bounds for plane
  378. \param[out] planes
  379. */
  380. void gs_get_databounds_planes(Point4 * planes)
  381. {
  382. float n, s, w, e, b, t;
  383. Point3 tlfront, brback;
  384. GS_get_zrange(&b, &t, 0);
  385. gs_get_xrange(&w, &e);
  386. gs_get_yrange(&s, &n);
  387. tlfront[X] = tlfront[Y] = 0.0;
  388. tlfront[Z] = t;
  389. brback[X] = e - w;
  390. brback[Y] = n - s;
  391. brback[Z] = b;
  392. /* top */
  393. planes[0][X] = planes[0][Y] = 0.0;
  394. planes[0][Z] = 1.0;
  395. planes[0][W] = -(DOT3(planes[0], tlfront));
  396. /* bottom */
  397. planes[1][X] = planes[1][Y] = 0.0;
  398. planes[1][Z] = -1.0;
  399. planes[1][W] = -(DOT3(planes[1], brback));
  400. /* left */
  401. planes[2][Y] = planes[2][Z] = 0.0;
  402. planes[2][X] = -1.0;
  403. planes[2][W] = -(DOT3(planes[2], tlfront));
  404. /* right */
  405. planes[3][Y] = planes[3][Z] = 0.0;
  406. planes[3][X] = 1.0;
  407. planes[3][W] = -(DOT3(planes[3], brback));
  408. /* front */
  409. planes[4][X] = planes[4][Z] = 0.0;
  410. planes[4][Y] = -1.0;
  411. planes[4][W] = -(DOT3(planes[4], tlfront));
  412. /* back */
  413. planes[5][X] = planes[5][Z] = 0.0;
  414. planes[5][Y] = 1.0;
  415. planes[5][W] = -(DOT3(planes[5], brback));
  416. return;
  417. }
  418. /*!
  419. Gets all current cutting planes & data bounding planes
  420. Intersects los with resulting convex polyhedron, then replaces los[FROM] with
  421. first point on ray inside data.
  422. \param[out] los
  423. \return 0 on failure
  424. \return 1 on success
  425. */
  426. int gs_setlos_enterdata(Point3 * los)
  427. {
  428. Point4 planes[12]; /* MAX_CPLANES + 6 - should define this */
  429. Point3 dir;
  430. double dist, maxdist;
  431. int num, ret, retp; /* might want to tell if retp is a clipping plane */
  432. gs_get_databounds_planes(planes);
  433. num = gsd_get_cplanes(planes + 6);
  434. GS_v3dir(los[FROM], los[TO], dir);
  435. maxdist = GS_distance(los[FROM], los[TO]);
  436. ret = RayCvxPolyhedronInt(los[0], dir, maxdist,
  437. planes, num + 6, &dist, &retp);
  438. if (ret == MISSED) {
  439. return (0);
  440. }
  441. if (ret == FRONTFACE) {
  442. GS_v3mult(dir, (float)dist);
  443. GS_v3add(los[FROM], dir);
  444. }
  445. return (1);
  446. }
  447. /***********************************************************************/
  448. /* DEBUG ****
  449. void pr_plane(int pnum)
  450. {
  451. switch(pnum)
  452. {
  453. case 0:
  454. fprintf(stderr,"top plane");
  455. break;
  456. case 1:
  457. fprintf(stderr,"bottom plane");
  458. break;
  459. case 2:
  460. fprintf(stderr,"left plane");
  461. break;
  462. case 3:
  463. fprintf(stderr,"right plane");
  464. break;
  465. case 4:
  466. fprintf(stderr,"front plane");
  467. break;
  468. case 5:
  469. fprintf(stderr,"back plane");
  470. break;
  471. default:
  472. fprintf(stderr,"clipping plane %d", 6 - pnum);
  473. break;
  474. }
  475. return;
  476. }
  477. ******* */