llraytrace.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. /**
  2. * @file llraytrace.cpp
  3. * @brief Functions called by box object scripts.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llraytrace.h"
  34. #include "llmath.h"
  35. #include "llquaternion.h"
  36. #include "llmatrix3.h"
  37. #include "llvector3.h"
  38. bool line_plane(const LLVector3& line_point, const LLVector3& line_direction,
  39. const LLVector3& plane_point, const LLVector3 plane_normal,
  40. LLVector3& intersection)
  41. {
  42. F32 N = line_direction * plane_normal;
  43. if (0.0f == N)
  44. {
  45. // line is perpendicular to plane normal
  46. // so it is either entirely on plane, or not on plane at all
  47. return false;
  48. }
  49. // Ax + By, + Cz + D = 0
  50. // D = - (plane_point * plane_normal)
  51. // N = line_direction * plane_normal
  52. // intersection = line_point - ((D + plane_normal * line_point) / N) * line_direction
  53. intersection = line_point -
  54. ((plane_normal * line_point - plane_point * plane_normal) / N) *
  55. line_direction;
  56. return true;
  57. }
  58. bool ray_plane(const LLVector3& ray_point, const LLVector3& ray_direction,
  59. const LLVector3& plane_point, const LLVector3 plane_normal,
  60. LLVector3& intersection)
  61. {
  62. F32 N = ray_direction * plane_normal;
  63. if (N == 0.f)
  64. {
  65. // Ray is perpendicular to plane normal so it is either entirely on
  66. // plane, or not on plane at all.
  67. return false;
  68. }
  69. // Ax + By, + Cz + D = 0
  70. // D = - (plane_point * plane_normal)
  71. // N = ray_direction * plane_normal
  72. // intersection = ray_point - ((D + plane_normal * ray_point) / N) * ray_direction
  73. F32 alpha = -(plane_normal * ray_point - plane_point * plane_normal) / N;
  74. if (alpha < 0.0f)
  75. {
  76. // ray points away from plane
  77. return false;
  78. }
  79. intersection = ray_point + alpha * ray_direction;
  80. return true;
  81. }
  82. bool ray_circle(const LLVector3& ray_point, const LLVector3& ray_direction,
  83. const LLVector3& circle_center, const LLVector3 plane_normal,
  84. F32 circle_radius, LLVector3& intersection)
  85. {
  86. if (ray_plane(ray_point, ray_direction, circle_center, plane_normal,
  87. intersection))
  88. {
  89. if (circle_radius >= (intersection - circle_center).length())
  90. {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. bool ray_triangle(const LLVector3& ray_point, const LLVector3& ray_direction,
  97. const LLVector3& point_0, const LLVector3& point_1,
  98. const LLVector3& point_2, LLVector3& intersection,
  99. LLVector3& intersection_normal)
  100. {
  101. LLVector3 side_01 = point_1 - point_0;
  102. LLVector3 side_12 = point_2 - point_1;
  103. intersection_normal = side_01 % side_12;
  104. intersection_normal.normalize();
  105. if (ray_plane(ray_point, ray_direction, point_0, intersection_normal,
  106. intersection))
  107. {
  108. LLVector3 side_20 = point_0 - point_2;
  109. if (intersection_normal * (side_01 % (intersection - point_0)) >= 0.0f &&
  110. intersection_normal * (side_12 % (intersection - point_1)) >= 0.0f &&
  111. intersection_normal * (side_20 % (intersection - point_2)) >= 0.0f)
  112. {
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. // assumes a parallelogram
  119. bool ray_quadrangle(const LLVector3& ray_point, const LLVector3& ray_direction,
  120. const LLVector3& point_0, const LLVector3& point_1,
  121. const LLVector3& point_2, LLVector3& intersection,
  122. LLVector3& intersection_normal)
  123. {
  124. LLVector3 side_01 = point_1 - point_0;
  125. LLVector3 side_12 = point_2 - point_1;
  126. intersection_normal = side_01 % side_12;
  127. intersection_normal.normalize();
  128. if (ray_plane(ray_point, ray_direction, point_0, intersection_normal,
  129. intersection))
  130. {
  131. LLVector3 point_3 = point_0 + (side_12);
  132. LLVector3 side_23 = point_3 - point_2;
  133. LLVector3 side_30 = point_0 - point_3;
  134. if (intersection_normal * (side_01 % (intersection - point_0)) >= 0.0f &&
  135. intersection_normal * (side_12 % (intersection - point_1)) >= 0.0f &&
  136. intersection_normal * (side_23 % (intersection - point_2)) >= 0.0f &&
  137. intersection_normal * (side_30 % (intersection - point_3)) >= 0.0f)
  138. {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. bool ray_sphere(const LLVector3& ray_point, const LLVector3& ray_direction,
  145. const LLVector3& sphere_center, F32 sphere_radius,
  146. LLVector3& intersection, LLVector3& intersection_normal)
  147. {
  148. LLVector3 ray_to_sphere = sphere_center - ray_point;
  149. F32 dot = ray_to_sphere * ray_direction;
  150. LLVector3 closest_approach = dot * ray_direction - ray_to_sphere;
  151. F32 shortest_distance = closest_approach.lengthSquared();
  152. F32 radius_squared = sphere_radius * sphere_radius;
  153. if (shortest_distance > radius_squared)
  154. {
  155. return false;
  156. }
  157. F32 half_chord = sqrtf(radius_squared - shortest_distance);
  158. // closest_approach in absolute coordinates:
  159. closest_approach = sphere_center + closest_approach;
  160. intersection = closest_approach + half_chord * ray_direction;
  161. dot = ray_direction * (intersection - ray_point);
  162. if (dot < 0.0f)
  163. {
  164. // ray shoots away from sphere and is not inside it
  165. return false;
  166. }
  167. shortest_distance = ray_direction *
  168. ((closest_approach - half_chord * ray_direction) -
  169. ray_point);
  170. if (shortest_distance > 0.0f)
  171. {
  172. // ray enters sphere
  173. intersection = intersection - (2.0f * half_chord) * ray_direction;
  174. }
  175. else
  176. {
  177. // do nothing
  178. // ray starts inside sphere and intersects as it leaves the sphere
  179. }
  180. intersection_normal = intersection - sphere_center;
  181. if (sphere_radius > 0.0f)
  182. {
  183. intersection_normal *= 1.0f / sphere_radius;
  184. }
  185. else
  186. {
  187. intersection_normal.set(0.0f, 0.0f, 0.0f);
  188. }
  189. return true;
  190. }
  191. bool ray_cylinder(const LLVector3& ray_point, const LLVector3& ray_direction,
  192. const LLVector3& cyl_center, const LLVector3& cyl_scale,
  193. const LLQuaternion& cyl_rotation, LLVector3& intersection,
  194. LLVector3& intersection_normal)
  195. {
  196. // calculate the centers of the cylinder caps in the absolute frame
  197. LLVector3 cyl_top(0.0f, 0.0f, 0.5f * cyl_scale.mV[VZ]);
  198. LLVector3 cyl_bottom(0.0f, 0.0f, -cyl_top.mV[VZ]);
  199. cyl_top = (cyl_top * cyl_rotation) + cyl_center;
  200. cyl_bottom = (cyl_bottom * cyl_rotation) + cyl_center;
  201. // We only handle cylinders with circular cross-sections at the moment.
  202. // HACK until scaled cylinders are supported.
  203. F32 cyl_radius = 0.5f * llmax(cyl_scale.mV[VX], cyl_scale.mV[VY]);
  204. // This implementation is based on the intcyl() function from
  205. // Graphics_Gems_IV, page 361
  206. LLVector3 cyl_axis; // axis direction (bottom toward top)
  207. LLVector3 ray_to_cyl; // ray_point to cyl_top
  208. F32 shortest_distance; // shortest distance from ray to axis
  209. F32 cyl_length;
  210. LLVector3 shortest_direction;
  211. LLVector3 temp_vector;
  212. cyl_axis = cyl_bottom - cyl_top;
  213. cyl_length = cyl_axis.normalize();
  214. ray_to_cyl = ray_point - cyl_bottom;
  215. shortest_direction = ray_direction % cyl_axis;
  216. // recycle shortest_distance
  217. shortest_distance = shortest_direction.normalize();
  218. // check for ray parallel to cylinder axis
  219. if (0.0f == shortest_distance)
  220. {
  221. // ray is parallel to cylinder axis
  222. temp_vector = ray_to_cyl - (ray_to_cyl * cyl_axis) * cyl_axis;
  223. shortest_distance = temp_vector.length();
  224. if (shortest_distance <= cyl_radius)
  225. {
  226. shortest_distance = ray_to_cyl * cyl_axis;
  227. F32 dot = ray_direction * cyl_axis;
  228. if (shortest_distance > 0.0)
  229. {
  230. if (dot > 0.0f)
  231. {
  232. // ray points away from cylinder bottom
  233. return false;
  234. }
  235. // ray hit bottom of cylinder from outside
  236. intersection = ray_point - shortest_distance * cyl_axis;
  237. intersection_normal = cyl_axis;
  238. }
  239. else if (shortest_distance > -cyl_length)
  240. {
  241. // ray starts inside cylinder
  242. if (dot < 0.0f)
  243. {
  244. // ray hit top from inside
  245. intersection = ray_point -
  246. (cyl_length + shortest_distance) * cyl_axis;
  247. intersection_normal = -cyl_axis;
  248. }
  249. else
  250. {
  251. // ray hit bottom from inside
  252. intersection = ray_point - shortest_distance * cyl_axis;
  253. intersection_normal = cyl_axis;
  254. }
  255. }
  256. else
  257. {
  258. if (dot < 0.0f)
  259. {
  260. // ray points away from cylinder bottom
  261. return false;
  262. }
  263. // ray hit top from outside
  264. intersection = ray_point -
  265. (shortest_distance + cyl_length) * cyl_axis;
  266. intersection_normal = -cyl_axis;
  267. }
  268. return true;
  269. }
  270. return false;
  271. }
  272. // check for intersection with infinite cylinder
  273. shortest_distance = (F32) fabs(ray_to_cyl * shortest_direction);
  274. if (shortest_distance <= cyl_radius)
  275. {
  276. temp_vector = ray_to_cyl % cyl_axis;
  277. temp_vector = shortest_direction % cyl_axis;
  278. temp_vector.normalize();
  279. // Half length of intersection chord
  280. F32 half_chord_length = fabsf(sqrtf(cyl_radius * cyl_radius -
  281. shortest_distance * shortest_distance) /
  282. (ray_direction * temp_vector));
  283. // Distance from ray_point to closest_point
  284. F32 dist_to_closest_point = -(temp_vector * shortest_direction);
  285. // Distance to exiting point
  286. F32 out = dist_to_closest_point + half_chord_length;
  287. if (out < 0.f)
  288. {
  289. // cylinder is behind the ray, so we return false
  290. return false;
  291. }
  292. // Distance to entering point:
  293. F32 in = dist_to_closest_point - half_chord_length;
  294. if (in < 0.f)
  295. {
  296. // ray_point is inside the cylinder
  297. // so we store the exiting intersection
  298. intersection = ray_point + out * ray_direction;
  299. shortest_distance = out;
  300. }
  301. else
  302. {
  303. // ray hit cylinder from outside
  304. // so we store the entering intersection
  305. intersection = ray_point + in * ray_direction;
  306. shortest_distance = in;
  307. }
  308. // calculate the normal at intersection
  309. if (0.0f == cyl_radius)
  310. {
  311. intersection_normal.set(0.0f, 0.0f, 0.0f);
  312. }
  313. else
  314. {
  315. temp_vector = intersection - cyl_bottom;
  316. intersection_normal = temp_vector -
  317. (temp_vector * cyl_axis) * cyl_axis;
  318. intersection_normal.normalize();
  319. }
  320. // check for intersection with end caps
  321. // calculate intersection of ray and top plane
  322. // NOTE: side-effect: changing temp_vector
  323. if (line_plane(ray_point, ray_direction, cyl_top, -cyl_axis,
  324. temp_vector))
  325. {
  326. shortest_distance = (temp_vector - ray_point).length();
  327. if ((ray_direction * cyl_axis) > 0.0f)
  328. {
  329. // ray potentially enters the cylinder at top
  330. if (shortest_distance > out)
  331. {
  332. // ray missed the finite cylinder
  333. return false;
  334. }
  335. if (shortest_distance > in)
  336. {
  337. // ray intersects cylinder at top plane
  338. intersection = temp_vector;
  339. intersection_normal = -cyl_axis;
  340. return true;
  341. }
  342. }
  343. else
  344. {
  345. // ray potentially exits the cylinder at top
  346. if (shortest_distance < in)
  347. {
  348. // missed the finite cylinder
  349. return false;
  350. }
  351. }
  352. // calculate intersection of ray and bottom plane
  353. // NOTE: side-effect: changing temp_vector
  354. line_plane(ray_point, ray_direction, cyl_bottom, cyl_axis,
  355. temp_vector);
  356. shortest_distance = (temp_vector - ray_point).length();
  357. if ((ray_direction * cyl_axis) < 0.0)
  358. {
  359. // ray potentially enters the cylinder at bottom
  360. if (shortest_distance > out)
  361. {
  362. // ray missed the finite cylinder
  363. return false;
  364. }
  365. if (shortest_distance > in)
  366. {
  367. // ray intersects cylinder at bottom plane
  368. intersection = temp_vector;
  369. intersection_normal = cyl_axis;
  370. return true;
  371. }
  372. }
  373. else
  374. {
  375. // ray potentially exits the cylinder at bottom
  376. if (shortest_distance < in)
  377. {
  378. // ray missed the finite cylinder
  379. return false;
  380. }
  381. }
  382. }
  383. else
  384. {
  385. // ray is parallel to end cap planes
  386. temp_vector = cyl_bottom - ray_point;
  387. shortest_distance = temp_vector * cyl_axis;
  388. if (shortest_distance < 0.0f || shortest_distance > cyl_length)
  389. {
  390. // ray missed finite cylinder
  391. return false;
  392. }
  393. }
  394. return true;
  395. }
  396. return false;
  397. }
  398. U32 ray_box(const LLVector3& ray_point, const LLVector3& ray_direction,
  399. const LLVector3& box_center, const LLVector3& box_scale,
  400. const LLQuaternion& box_rotation, LLVector3& intersection,
  401. LLVector3& intersection_normal)
  402. {
  403. // Need to rotate into box frame
  404. // Rotates things from box frame to absolute:
  405. LLQuaternion into_box_frame(box_rotation);
  406. // Now rotates things into box frame:
  407. into_box_frame.transpose();
  408. LLVector3 line_point = (ray_point - box_center) * into_box_frame;
  409. LLVector3 line_direction = ray_direction * into_box_frame;
  410. // Suppose we have a plane: Ax + By + Cz + D = 0
  411. // then, assuming [A, B, C] is a unit vector:
  412. // plane_normal = [A, B, C]
  413. // D = - (plane_normal * plane_point)
  414. //
  415. // Suppose we have a line: X = line_point + alpha * line_direction
  416. //
  417. // the intersection of the plane and line determines alpha:
  418. // alpha = - (D + plane_normal * line_point) / (plane_normal * line_direction)
  419. LLVector3 line_plane_intersection;
  420. F32 pointX = line_point.mV[VX];
  421. F32 pointY = line_point.mV[VY];
  422. F32 pointZ = line_point.mV[VZ];
  423. F32 dirX = line_direction.mV[VX];
  424. F32 dirY = line_direction.mV[VY];
  425. F32 dirZ = line_direction.mV[VZ];
  426. // we'll be using the half-scales of the box
  427. F32 boxX = 0.5f * box_scale.mV[VX];
  428. F32 boxY = 0.5f * box_scale.mV[VY];
  429. F32 boxZ = 0.5f * box_scale.mV[VZ];
  430. // check to see if line_point is OUTSIDE the box
  431. if (pointX < -boxX || pointX > boxX || pointY < -boxY || pointY > boxY ||
  432. pointZ < -boxZ || pointZ > boxZ)
  433. {
  434. // -------------- point is OUTSIDE the box ----------------
  435. // front
  436. if (pointX > 0.0f && dirX < 0.0f)
  437. {
  438. // plane_normal = [ 1, 0, 0]
  439. // plane_normal*line_point = pointX
  440. // plane_normal*line_direction = dirX
  441. // D = -boxX
  442. // alpha = - (-boxX + pointX) / dirX
  443. line_plane_intersection = line_point -
  444. ((pointX - boxX) / dirX) * line_direction;
  445. if (line_plane_intersection.mV[VY] < boxY &&
  446. line_plane_intersection.mV[VY] > -boxY &&
  447. line_plane_intersection.mV[VZ] < boxZ &&
  448. line_plane_intersection.mV[VZ] > -boxZ)
  449. {
  450. intersection = line_plane_intersection * box_rotation + box_center;
  451. intersection_normal = LLVector3(1.0f, 0.0f, 0.0f) * box_rotation;
  452. return FRONT_SIDE;
  453. }
  454. }
  455. // back
  456. if (pointX < 0.0f && dirX > 0.0f)
  457. {
  458. // plane_normal = [ -1, 0, 0]
  459. // plane_normal*line_point = -pX
  460. // plane_normal*line_direction = -direction.mV[VX]
  461. // D = -bX
  462. // alpha = - (-bX - pX) / (-dirX)
  463. line_plane_intersection = line_point -
  464. ((boxX + pointX)/ dirX) * line_direction;
  465. if (line_plane_intersection.mV[VY] < boxY &&
  466. line_plane_intersection.mV[VY] > -boxY &&
  467. line_plane_intersection.mV[VZ] < boxZ &&
  468. line_plane_intersection.mV[VZ] > -boxZ)
  469. {
  470. intersection = line_plane_intersection * box_rotation + box_center;
  471. intersection_normal = LLVector3(-1.0f, 0.0f, 0.0f) * box_rotation;
  472. return BACK_SIDE;
  473. }
  474. }
  475. // left
  476. if (pointY > 0.0f && dirY < 0.0f)
  477. {
  478. // plane_normal = [0, 1, 0]
  479. // plane_normal*line_point = pointY
  480. // plane_normal*line_direction = dirY
  481. // D = -boxY
  482. // alpha = - (-boxY + pointY) / dirY
  483. line_plane_intersection = line_point +
  484. ((boxY - pointY) / dirY) * line_direction;
  485. if (line_plane_intersection.mV[VX] < boxX &&
  486. line_plane_intersection.mV[VX] > -boxX &&
  487. line_plane_intersection.mV[VZ] < boxZ &&
  488. line_plane_intersection.mV[VZ] > -boxZ)
  489. {
  490. intersection = line_plane_intersection * box_rotation + box_center;
  491. intersection_normal = LLVector3(0.0f, 1.0f, 0.0f) * box_rotation;
  492. return LEFT_SIDE;
  493. }
  494. }
  495. // right
  496. if (pointY < 0.0f && dirY > 0.0f)
  497. {
  498. // plane_normal = [0, -1, 0]
  499. // plane_normal*line_point = -pointY
  500. // plane_normal*line_direction = -dirY
  501. // D = -boxY
  502. // alpha = - (-boxY - pointY) / (-dirY)
  503. line_plane_intersection = line_point - ((boxY + pointY) / dirY) * line_direction;
  504. if (line_plane_intersection.mV[VX] < boxX &&
  505. line_plane_intersection.mV[VX] > -boxX &&
  506. line_plane_intersection.mV[VZ] < boxZ &&
  507. line_plane_intersection.mV[VZ] > -boxZ)
  508. {
  509. intersection = line_plane_intersection * box_rotation + box_center;
  510. intersection_normal = LLVector3(0.0f, -1.0f, 0.0f) * box_rotation;
  511. return RIGHT_SIDE;
  512. }
  513. }
  514. // top
  515. if (pointZ > 0.0f && dirZ < 0.0f)
  516. {
  517. // plane_normal = [0, 0, 1]
  518. // plane_normal*line_point = pointZ
  519. // plane_normal*line_direction = dirZ
  520. // D = -boxZ
  521. // alpha = - (-boxZ + pointZ) / dirZ
  522. line_plane_intersection = line_point - ((pointZ - boxZ) / dirZ) * line_direction;
  523. if (line_plane_intersection.mV[VX] < boxX &&
  524. line_plane_intersection.mV[VX] > -boxX &&
  525. line_plane_intersection.mV[VY] < boxY &&
  526. line_plane_intersection.mV[VY] > -boxY)
  527. {
  528. intersection = line_plane_intersection * box_rotation + box_center;
  529. intersection_normal = LLVector3(0.0f, 0.0f, 1.0f) * box_rotation;
  530. return TOP_SIDE;
  531. }
  532. }
  533. // bottom
  534. if (pointZ < 0.0f && dirZ > 0.0f)
  535. {
  536. // plane_normal = [0, 0, -1]
  537. // plane_normal*line_point = -pointZ
  538. // plane_normal*line_direction = -dirZ
  539. // D = -boxZ
  540. // alpha = - (-boxZ - pointZ) / (-dirZ)
  541. line_plane_intersection = line_point -
  542. ((boxZ + pointZ) / dirZ) * line_direction;
  543. if (line_plane_intersection.mV[VX] < boxX &&
  544. line_plane_intersection.mV[VX] > -boxX &&
  545. line_plane_intersection.mV[VY] < boxY &&
  546. line_plane_intersection.mV[VY] > -boxY)
  547. {
  548. intersection = line_plane_intersection * box_rotation + box_center;
  549. intersection_normal = LLVector3(0.0f, 0.0f, -1.0f) * box_rotation;
  550. return BOTTOM_SIDE;
  551. }
  552. }
  553. return NO_SIDE;
  554. }
  555. // -------------- point is INSIDE the box ----------------
  556. // front
  557. if (dirX > 0.0f)
  558. {
  559. // plane_normal = [ 1, 0, 0]
  560. // plane_normal*line_point = pointX
  561. // plane_normal*line_direction = dirX
  562. // D = -boxX
  563. // alpha = - (-boxX + pointX) / dirX
  564. line_plane_intersection = line_point - ((pointX - boxX) / dirX) * line_direction;
  565. if (line_plane_intersection.mV[VY] < boxY &&
  566. line_plane_intersection.mV[VY] > -boxY &&
  567. line_plane_intersection.mV[VZ] < boxZ &&
  568. line_plane_intersection.mV[VZ] > -boxZ)
  569. {
  570. intersection = line_plane_intersection * box_rotation + box_center;
  571. intersection_normal = LLVector3(1.0f, 0.0f, 0.0f) * box_rotation;
  572. return FRONT_SIDE;
  573. }
  574. }
  575. // back
  576. if (dirX < 0.0f)
  577. {
  578. // plane_normal = [ -1, 0, 0]
  579. // plane_normal*line_point = -pX
  580. // plane_normal*line_direction = -direction.mV[VX]
  581. // D = -bX
  582. // alpha = - (-bX - pX) / (-dirX)
  583. line_plane_intersection = line_point -
  584. ((boxX + pointX) / dirX) * line_direction;
  585. if (line_plane_intersection.mV[VY] < boxY &&
  586. line_plane_intersection.mV[VY] > -boxY &&
  587. line_plane_intersection.mV[VZ] < boxZ &&
  588. line_plane_intersection.mV[VZ] > -boxZ)
  589. {
  590. intersection = line_plane_intersection * box_rotation + box_center;
  591. intersection_normal = LLVector3(-1.0f, 0.0f, 0.0f) * box_rotation;
  592. return BACK_SIDE;
  593. }
  594. }
  595. // left
  596. if (dirY > 0.0f)
  597. {
  598. // plane_normal = [0, 1, 0]
  599. // plane_normal*line_point = pointY
  600. // plane_normal*line_direction = dirY
  601. // D = -boxY
  602. // alpha = - (-boxY + pointY) / dirY
  603. line_plane_intersection = line_point +
  604. ((boxY - pointY) / dirY) * line_direction;
  605. if (line_plane_intersection.mV[VX] < boxX &&
  606. line_plane_intersection.mV[VX] > -boxX &&
  607. line_plane_intersection.mV[VZ] < boxZ &&
  608. line_plane_intersection.mV[VZ] > -boxZ)
  609. {
  610. intersection = line_plane_intersection * box_rotation + box_center;
  611. intersection_normal = LLVector3(0.0f, 1.0f, 0.0f) * box_rotation;
  612. return LEFT_SIDE;
  613. }
  614. }
  615. // right
  616. if (dirY < 0.0f)
  617. {
  618. // plane_normal = [0, -1, 0]
  619. // plane_normal*line_point = -pointY
  620. // plane_normal*line_direction = -dirY
  621. // D = -boxY
  622. // alpha = - (-boxY - pointY) / (-dirY)
  623. line_plane_intersection = line_point -
  624. ((boxY + pointY) / dirY) * line_direction;
  625. if (line_plane_intersection.mV[VX] < boxX &&
  626. line_plane_intersection.mV[VX] > -boxX &&
  627. line_plane_intersection.mV[VZ] < boxZ &&
  628. line_plane_intersection.mV[VZ] > -boxZ)
  629. {
  630. intersection = line_plane_intersection * box_rotation + box_center;
  631. intersection_normal = LLVector3(0.0f, -1.0f, 0.0f) * box_rotation;
  632. return RIGHT_SIDE;
  633. }
  634. }
  635. // top
  636. if (dirZ > 0.0f)
  637. {
  638. // plane_normal = [0, 0, 1]
  639. // plane_normal*line_point = pointZ
  640. // plane_normal*line_direction = dirZ
  641. // D = -boxZ
  642. // alpha = - (-boxZ + pointZ) / dirZ
  643. line_plane_intersection = line_point -
  644. ((pointZ - boxZ) / dirZ) * line_direction;
  645. if (line_plane_intersection.mV[VX] < boxX &&
  646. line_plane_intersection.mV[VX] > -boxX &&
  647. line_plane_intersection.mV[VY] < boxY &&
  648. line_plane_intersection.mV[VY] > -boxY)
  649. {
  650. intersection = line_plane_intersection * box_rotation + box_center;
  651. intersection_normal = LLVector3(0.0f, 0.0f, 1.0f) * box_rotation;
  652. return TOP_SIDE;
  653. }
  654. }
  655. // bottom
  656. if (dirZ < 0.0f)
  657. {
  658. // plane_normal = [0, 0, -1]
  659. // plane_normal*line_point = -pointZ
  660. // plane_normal*line_direction = -dirZ
  661. // D = -boxZ
  662. // alpha = - (-boxZ - pointZ) / (-dirZ)
  663. line_plane_intersection = line_point -
  664. ((boxZ + pointZ) / dirZ) * line_direction;
  665. if (line_plane_intersection.mV[VX] < boxX &&
  666. line_plane_intersection.mV[VX] > -boxX &&
  667. line_plane_intersection.mV[VY] < boxY &&
  668. line_plane_intersection.mV[VY] > -boxY)
  669. {
  670. intersection = line_plane_intersection * box_rotation + box_center;
  671. intersection_normal = LLVector3(0.0f, 0.0f, -1.0f) * box_rotation;
  672. return BOTTOM_SIDE;
  673. }
  674. }
  675. // Should never get here unless line instersects at tangent point on edge
  676. // or corner, however such cases will be EXTREMELY rare.
  677. return NO_SIDE;
  678. }
  679. bool ray_prism(const LLVector3& ray_point, const LLVector3& ray_direction,
  680. const LLVector3& prism_center, const LLVector3& prism_scale,
  681. const LLQuaternion& prism_rotation, LLVector3& intersection,
  682. LLVector3& intersection_normal)
  683. {
  684. // (0) Z
  685. // /| \ .
  686. // (1)| \ /|\ _.Y
  687. // | \ \ | /|
  688. // | |\ \ | /
  689. // | | \(0)\ | /
  690. // | | \ \ |/
  691. // | | \ \ (*)----> X
  692. // |(3)---\---(2)
  693. // |/ \ /
  694. // (4)-------(5)
  695. // need to calculate the points of the prism so we can run ray tests with
  696. // each face
  697. F32 x = prism_scale.mV[VX];
  698. F32 y = prism_scale.mV[VY];
  699. F32 z = prism_scale.mV[VZ];
  700. F32 tx = x * 2.0f / 3.0f;
  701. F32 ty = y * 0.5f;
  702. F32 tz = z * 2.0f / 3.0f;
  703. LLVector3 point0(tx-x, ty, tz);
  704. LLVector3 point1(tx-x, -ty, tz);
  705. LLVector3 point2(tx, ty, tz-z);
  706. LLVector3 point3(tx-x, ty, tz-z);
  707. LLVector3 point4(tx-x, -ty, tz-z);
  708. LLVector3 point5(tx, -ty, tz-z);
  709. // transform these points into absolute frame
  710. point0 = (point0 * prism_rotation) + prism_center;
  711. point1 = (point1 * prism_rotation) + prism_center;
  712. point2 = (point2 * prism_rotation) + prism_center;
  713. point3 = (point3 * prism_rotation) + prism_center;
  714. point4 = (point4 * prism_rotation) + prism_center;
  715. point5 = (point5 * prism_rotation) + prism_center;
  716. // test ray intersection for each face
  717. bool b_hit = false;
  718. LLVector3 face_intersection, face_normal;
  719. F32 distance_squared = 0.0f;
  720. F32 temp;
  721. // face 0
  722. if (ray_direction * ((point0 - point2) % (point5 - point2)) < 0.0f &&
  723. ray_quadrangle(ray_point, ray_direction, point5, point2, point0,
  724. intersection, intersection_normal))
  725. {
  726. distance_squared = (ray_point - intersection).lengthSquared();
  727. b_hit = true;
  728. }
  729. // face 1
  730. if (ray_direction * ((point0 - point3) % (point2 - point3)) < 0.0f &&
  731. ray_triangle(ray_point, ray_direction, point2, point3, point0,
  732. face_intersection, face_normal))
  733. {
  734. if (b_hit)
  735. {
  736. temp = (ray_point - face_intersection).lengthSquared();
  737. if (temp < distance_squared)
  738. {
  739. distance_squared = temp;
  740. intersection = face_intersection;
  741. intersection_normal = face_normal;
  742. }
  743. }
  744. else
  745. {
  746. distance_squared = (ray_point - face_intersection).lengthSquared();
  747. intersection = face_intersection;
  748. intersection_normal = face_normal;
  749. b_hit = true;
  750. }
  751. }
  752. // face 2
  753. if (ray_direction * ((point1 - point4) % (point3 - point4)) < 0.0f &&
  754. ray_quadrangle(ray_point, ray_direction, point3, point4, point1,
  755. face_intersection, face_normal))
  756. {
  757. if (b_hit)
  758. {
  759. temp = (ray_point - face_intersection).lengthSquared();
  760. if (temp < distance_squared)
  761. {
  762. distance_squared = temp;
  763. intersection = face_intersection;
  764. intersection_normal = face_normal;
  765. }
  766. }
  767. else
  768. {
  769. distance_squared = (ray_point - face_intersection).lengthSquared();
  770. intersection = face_intersection;
  771. intersection_normal = face_normal;
  772. b_hit = true;
  773. }
  774. }
  775. // face 3
  776. if (ray_direction * ((point5 - point4) % (point1 - point4)) < 0.0f &&
  777. ray_triangle(ray_point, ray_direction, point1, point4, point5,
  778. face_intersection, face_normal))
  779. {
  780. if (b_hit)
  781. {
  782. temp = (ray_point - face_intersection).lengthSquared();
  783. if (temp < distance_squared)
  784. {
  785. distance_squared = temp;
  786. intersection = face_intersection;
  787. intersection_normal = face_normal;
  788. }
  789. }
  790. else
  791. {
  792. distance_squared = (ray_point - face_intersection).lengthSquared();
  793. intersection = face_intersection;
  794. intersection_normal = face_normal;
  795. b_hit = true;
  796. }
  797. }
  798. // face 4
  799. if (ray_direction * ((point4 - point5) % (point2 - point5)) < 0.0f &&
  800. ray_quadrangle(ray_point, ray_direction, point2, point5, point4,
  801. face_intersection, face_normal))
  802. {
  803. if (b_hit)
  804. {
  805. temp = (ray_point - face_intersection).lengthSquared();
  806. if (temp < distance_squared)
  807. {
  808. distance_squared = temp;
  809. intersection = face_intersection;
  810. intersection_normal = face_normal;
  811. }
  812. }
  813. else
  814. {
  815. distance_squared = (ray_point - face_intersection).lengthSquared();
  816. intersection = face_intersection;
  817. intersection_normal = face_normal;
  818. b_hit = true;
  819. }
  820. }
  821. return b_hit;
  822. }
  823. bool ray_tetrahedron(const LLVector3& ray_point, const LLVector3& ray_direction,
  824. const LLVector3& t_center, const LLVector3& t_scale,
  825. const LLQuaternion& t_rotation, LLVector3& intersection,
  826. LLVector3& intersection_normal)
  827. {
  828. // height of unit triangle
  829. F32 a = 0.5f * F_SQRT3;
  830. // distance of center of unit triangle to each point
  831. F32 b = 1.0f / F_SQRT3;
  832. // height of unit tetrahedron
  833. F32 c = F_SQRT2 / F_SQRT3;
  834. // distance of center of tetrahedron to each point
  835. F32 d = 0.5f * F_SQRT3 / F_SQRT2;
  836. // If we want the tetrahedron to have unit height (c = 1.0) then we need to
  837. // divide each constant by hieght of a unit tetrahedron:
  838. F32 oo_c = 1.0f / c;
  839. a = a * oo_c;
  840. b = b * oo_c;
  841. c = 1.0f;
  842. d = d * oo_c;
  843. F32 e = 0.5f * oo_c;
  844. LLVector3 point0(0.0f, 0.0f, t_scale.mV[VZ] * d);
  845. LLVector3 point1(t_scale.mV[VX] * b, 0.0f, t_scale.mV[VZ] * (d - c));
  846. LLVector3 point2(t_scale.mV[VX] * (b - a), e * t_scale.mV[VY],
  847. t_scale.mV[VZ] * (d - c));
  848. LLVector3 point3(t_scale.mV[VX] * (b - a), -e * t_scale.mV[VY],
  849. t_scale.mV[VZ] * (d - c));
  850. // Transform these points into absolute frame
  851. point0 = (point0 * t_rotation) + t_center;
  852. point1 = (point1 * t_rotation) + t_center;
  853. point2 = (point2 * t_rotation) + t_center;
  854. point3 = (point3 * t_rotation) + t_center;
  855. // Test ray intersection for each face
  856. bool b_hit = false;
  857. LLVector3 face_intersection, face_normal;
  858. F32 distance_squared = 1.0e12f;
  859. F32 temp;
  860. // face 0
  861. if (ray_direction * ((point2 - point1) % (point0 - point1)) < 0.0f &&
  862. ray_triangle(ray_point, ray_direction, point1, point2, point0,
  863. intersection, intersection_normal))
  864. {
  865. distance_squared = (ray_point - intersection).lengthSquared();
  866. b_hit = true;
  867. }
  868. // face 1
  869. if (ray_direction * ((point3 - point2) % (point0 - point2)) < 0.0f &&
  870. ray_triangle(ray_point, ray_direction, point2, point3, point0,
  871. face_intersection, face_normal))
  872. {
  873. if (b_hit)
  874. {
  875. temp = (ray_point - face_intersection).lengthSquared();
  876. if (temp < distance_squared)
  877. {
  878. distance_squared = temp;
  879. intersection = face_intersection;
  880. intersection_normal = face_normal;
  881. }
  882. }
  883. else
  884. {
  885. distance_squared = (ray_point - face_intersection).lengthSquared();
  886. intersection = face_intersection;
  887. intersection_normal = face_normal;
  888. b_hit = true;
  889. }
  890. }
  891. // face 2
  892. if (ray_direction * ((point1 - point3) % (point0 - point3)) < 0.0f &&
  893. ray_triangle(ray_point, ray_direction, point3, point1, point0,
  894. face_intersection, face_normal))
  895. {
  896. if (b_hit)
  897. {
  898. temp = (ray_point - face_intersection).lengthSquared();
  899. if (temp < distance_squared)
  900. {
  901. distance_squared = temp;
  902. intersection = face_intersection;
  903. intersection_normal = face_normal;
  904. }
  905. }
  906. else
  907. {
  908. distance_squared = (ray_point - face_intersection).lengthSquared();
  909. intersection = face_intersection;
  910. intersection_normal = face_normal;
  911. b_hit = true;
  912. }
  913. }
  914. // face 3
  915. if (ray_direction * ((point2 - point3) % (point1 - point3)) < 0.0f &&
  916. ray_triangle(ray_point, ray_direction, point3, point2, point1,
  917. face_intersection, face_normal))
  918. {
  919. if (b_hit)
  920. {
  921. temp = (ray_point - face_intersection).lengthSquared();
  922. if (temp < distance_squared)
  923. {
  924. intersection = face_intersection;
  925. intersection_normal = face_normal;
  926. }
  927. }
  928. else
  929. {
  930. intersection = face_intersection;
  931. intersection_normal = face_normal;
  932. b_hit = true;
  933. }
  934. }
  935. return b_hit;
  936. }
  937. bool ray_pyramid(const LLVector3& ray_point, const LLVector3& ray_direction,
  938. const LLVector3& p_center, const LLVector3& p_scale,
  939. const LLQuaternion& p_rotation, LLVector3& intersection,
  940. LLVector3& intersection_normal)
  941. {
  942. // center of mass of pyramid is located 1/4 its height from the base
  943. F32 x = 0.5f * p_scale.mV[VX];
  944. F32 y = 0.5f * p_scale.mV[VY];
  945. F32 z = 0.25f * p_scale.mV[VZ];
  946. LLVector3 point0(0.0f, 0.0f, p_scale.mV[VZ] - z);
  947. LLVector3 point1(x, y, -z);
  948. LLVector3 point2(-x, y, -z);
  949. LLVector3 point3(-x, -y, -z);
  950. LLVector3 point4(x, -y, -z);
  951. // transform these points into absolute frame
  952. point0 = (point0 * p_rotation) + p_center;
  953. point1 = (point1 * p_rotation) + p_center;
  954. point2 = (point2 * p_rotation) + p_center;
  955. point3 = (point3 * p_rotation) + p_center;
  956. point4 = (point4 * p_rotation) + p_center;
  957. // test ray intersection for each face
  958. bool b_hit = false;
  959. LLVector3 face_intersection, face_normal;
  960. F32 distance_squared = 1.0e12f;
  961. F32 temp;
  962. // face 0
  963. if (ray_direction * ((point1 - point4) % (point0 - point4)) < 0.0f &&
  964. ray_triangle(ray_point, ray_direction, point4, point1, point0,
  965. intersection, intersection_normal))
  966. {
  967. distance_squared = (ray_point - intersection).lengthSquared();
  968. b_hit = true;
  969. }
  970. // face 1
  971. if (ray_direction * ((point2 - point1) % (point0 - point1)) < 0.0f &&
  972. ray_triangle(ray_point, ray_direction, point1, point2, point0,
  973. face_intersection, face_normal))
  974. {
  975. if (b_hit)
  976. {
  977. temp = (ray_point - face_intersection).lengthSquared();
  978. if (temp < distance_squared)
  979. {
  980. distance_squared = temp;
  981. intersection = face_intersection;
  982. intersection_normal = face_normal;
  983. }
  984. }
  985. else
  986. {
  987. distance_squared = (ray_point - face_intersection).lengthSquared();
  988. intersection = face_intersection;
  989. intersection_normal = face_normal;
  990. b_hit = true;
  991. }
  992. }
  993. // face 2
  994. if (ray_direction * ((point3 - point2) % (point0 - point2)) < 0.0f &&
  995. ray_triangle(ray_point, ray_direction, point2, point3, point0,
  996. face_intersection, face_normal))
  997. {
  998. if (b_hit)
  999. {
  1000. temp = (ray_point - face_intersection).lengthSquared();
  1001. if (temp < distance_squared)
  1002. {
  1003. distance_squared = temp;
  1004. intersection = face_intersection;
  1005. intersection_normal = face_normal;
  1006. }
  1007. }
  1008. else
  1009. {
  1010. distance_squared = (ray_point - face_intersection).lengthSquared();
  1011. intersection = face_intersection;
  1012. intersection_normal = face_normal;
  1013. b_hit = true;
  1014. }
  1015. }
  1016. // face 3
  1017. if (ray_direction * ((point4 - point3) % (point0 - point3)) < 0.0f &&
  1018. ray_triangle(ray_point, ray_direction, point3, point4, point0,
  1019. face_intersection, face_normal))
  1020. {
  1021. if (b_hit)
  1022. {
  1023. temp = (ray_point - face_intersection).lengthSquared();
  1024. if (temp < distance_squared)
  1025. {
  1026. distance_squared = temp;
  1027. intersection = face_intersection;
  1028. intersection_normal = face_normal;
  1029. }
  1030. }
  1031. else
  1032. {
  1033. distance_squared = (ray_point - face_intersection).lengthSquared();
  1034. intersection = face_intersection;
  1035. intersection_normal = face_normal;
  1036. b_hit = true;
  1037. }
  1038. }
  1039. // face 4
  1040. if (ray_direction * ((point3 - point4) % (point2 - point4)) < 0.0f &&
  1041. ray_quadrangle(ray_point, ray_direction, point4, point3, point2,
  1042. face_intersection, face_normal))
  1043. {
  1044. if (b_hit)
  1045. {
  1046. temp = (ray_point - face_intersection).lengthSquared();
  1047. if (temp < distance_squared)
  1048. {
  1049. intersection = face_intersection;
  1050. intersection_normal = face_normal;
  1051. }
  1052. }
  1053. else
  1054. {
  1055. intersection = face_intersection;
  1056. intersection_normal = face_normal;
  1057. b_hit = true;
  1058. }
  1059. }
  1060. return b_hit;
  1061. }
  1062. bool linesegment_circle(const LLVector3& point_a, const LLVector3& point_b,
  1063. const LLVector3& circle_center,
  1064. const LLVector3 plane_normal, F32 circle_radius,
  1065. LLVector3& intersection)
  1066. {
  1067. LLVector3 ray_direction = point_b - point_a;
  1068. F32 segment_length = ray_direction.normalize();
  1069. if (ray_circle(point_a, ray_direction, circle_center, plane_normal,
  1070. circle_radius, intersection))
  1071. {
  1072. if (segment_length >= (point_a - intersection).length())
  1073. {
  1074. return true;
  1075. }
  1076. }
  1077. return false;
  1078. }
  1079. bool linesegment_triangle(const LLVector3& point_a, const LLVector3& point_b,
  1080. const LLVector3& point_0, const LLVector3& point_1,
  1081. const LLVector3& point_2, LLVector3& intersection,
  1082. LLVector3& intersection_normal)
  1083. {
  1084. LLVector3 ray_direction = point_b - point_a;
  1085. F32 segment_length = ray_direction.normalize();
  1086. if (ray_triangle(point_a, ray_direction, point_0, point_1, point_2,
  1087. intersection, intersection_normal))
  1088. {
  1089. if (segment_length >= (point_a - intersection).length())
  1090. {
  1091. return true;
  1092. }
  1093. }
  1094. return false;
  1095. }
  1096. bool linesegment_quadrangle(const LLVector3& point_a, const LLVector3& point_b,
  1097. const LLVector3& point_0, const LLVector3& point_1,
  1098. const LLVector3& point_2, LLVector3& intersection,
  1099. LLVector3& intersection_normal)
  1100. {
  1101. LLVector3 ray_direction = point_b - point_a;
  1102. F32 segment_length = ray_direction.normalize();
  1103. if (ray_quadrangle(point_a, ray_direction, point_0, point_1, point_2,
  1104. intersection, intersection_normal))
  1105. {
  1106. if (segment_length >= (point_a - intersection).length())
  1107. {
  1108. return true;
  1109. }
  1110. }
  1111. return false;
  1112. }
  1113. bool linesegment_sphere(const LLVector3& point_a, const LLVector3& point_b,
  1114. const LLVector3& sphere_center, F32 sphere_radius,
  1115. LLVector3& intersection, LLVector3& intersection_normal)
  1116. {
  1117. LLVector3 ray_direction = point_b - point_a;
  1118. F32 segment_length = ray_direction.normalize();
  1119. if (ray_sphere(point_a, ray_direction, sphere_center, sphere_radius,
  1120. intersection, intersection_normal))
  1121. {
  1122. if (segment_length >= (point_a - intersection).length())
  1123. {
  1124. return true;
  1125. }
  1126. }
  1127. return false;
  1128. }
  1129. bool linesegment_cylinder(const LLVector3& point_a, const LLVector3& point_b,
  1130. const LLVector3& cyl_center, const LLVector3& cyl_scale,
  1131. const LLQuaternion& cyl_rotation, LLVector3& intersection,
  1132. LLVector3& intersection_normal)
  1133. {
  1134. LLVector3 ray_direction = point_b - point_a;
  1135. F32 segment_length = ray_direction.normalize();
  1136. if (ray_cylinder(point_a, ray_direction, cyl_center, cyl_scale,
  1137. cyl_rotation, intersection, intersection_normal))
  1138. {
  1139. if (segment_length >= (point_a - intersection).length())
  1140. {
  1141. return true;
  1142. }
  1143. }
  1144. return false;
  1145. }
  1146. U32 linesegment_box(const LLVector3& point_a, const LLVector3& point_b,
  1147. const LLVector3& box_center, const LLVector3& box_scale,
  1148. const LLQuaternion& box_rotation,
  1149. LLVector3& intersection, LLVector3& intersection_normal)
  1150. {
  1151. LLVector3 direction = point_b - point_a;
  1152. if (direction.isNull())
  1153. {
  1154. return NO_SIDE;
  1155. }
  1156. F32 segment_length = direction.normalize();
  1157. U32 box_side = ray_box(point_a, direction, box_center, box_scale,
  1158. box_rotation, intersection, intersection_normal);
  1159. if (NO_SIDE == box_side ||
  1160. segment_length < (intersection - point_a).length())
  1161. {
  1162. return NO_SIDE;
  1163. }
  1164. return box_side;
  1165. }
  1166. bool linesegment_prism(const LLVector3& point_a, const LLVector3& point_b,
  1167. const LLVector3& prism_center,
  1168. const LLVector3& prism_scale,
  1169. const LLQuaternion& prism_rotation,
  1170. LLVector3& intersection, LLVector3& intersection_normal)
  1171. {
  1172. LLVector3 ray_direction = point_b - point_a;
  1173. F32 segment_length = ray_direction.normalize();
  1174. if (ray_prism(point_a, ray_direction, prism_center, prism_scale,
  1175. prism_rotation, intersection, intersection_normal))
  1176. {
  1177. if (segment_length >= (point_a - intersection).length())
  1178. {
  1179. return true;
  1180. }
  1181. }
  1182. return false;
  1183. }
  1184. bool linesegment_tetrahedron(const LLVector3& point_a,
  1185. const LLVector3& point_b,
  1186. const LLVector3& t_center,
  1187. const LLVector3& t_scale,
  1188. const LLQuaternion& t_rotation,
  1189. LLVector3& intersection,
  1190. LLVector3& intersection_normal)
  1191. {
  1192. LLVector3 ray_direction = point_b - point_a;
  1193. F32 segment_length = ray_direction.normalize();
  1194. if (ray_tetrahedron(point_a, ray_direction, t_center, t_scale, t_rotation,
  1195. intersection, intersection_normal))
  1196. {
  1197. if (segment_length >= (point_a - intersection).length())
  1198. {
  1199. return true;
  1200. }
  1201. }
  1202. return false;
  1203. }
  1204. bool linesegment_pyramid(const LLVector3& point_a, const LLVector3& point_b,
  1205. const LLVector3& p_center, const LLVector3& p_scale,
  1206. const LLQuaternion& p_rotation,
  1207. LLVector3& intersection,
  1208. LLVector3& intersection_normal)
  1209. {
  1210. LLVector3 ray_direction = point_b - point_a;
  1211. F32 segment_length = ray_direction.normalize();
  1212. if (ray_pyramid(point_a, ray_direction, p_center, p_scale, p_rotation,
  1213. intersection, intersection_normal))
  1214. {
  1215. if (segment_length >= (point_a - intersection).length())
  1216. {
  1217. return true;
  1218. }
  1219. }
  1220. return false;
  1221. }