llraytrace.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * @file llraytrace.h
  3. * @brief Ray intersection tests for primitives.
  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. #ifndef LL_RAYTRACE_H
  33. #define LL_RAYTRACE_H
  34. #include "llpreprocessor.h"
  35. #include "stdtypes.h"
  36. class LLVector3;
  37. class LLQuaternion;
  38. // Sides of a box...
  39. // . Z __.Y
  40. // /|\ /| 0 = NO_SIDE
  41. // | / 1 = FRONT_SIDE = +x
  42. // +------|-----------+ 2 = BACK_SIDE = -x
  43. // /| |/ / /| 3 = LEFT_SIDE = +y
  44. // / | -5- |/ / | 4 = RIGHT_SIDE = -y
  45. // / | /| -3- / | 5 = TOP_SIDE = +z
  46. // +------------------+ | 6 = BOTTOM_SIDE = -z
  47. // | | | / | |
  48. // | |/| | / | |/|
  49. // | 2 | | *-------|-1--------> X
  50. // |/| | -4- |/| |
  51. // | +----|---------|---+
  52. // | / / | /
  53. // | / -6- | /
  54. // |/ / |/
  55. // +------------------+
  56. constexpr U32 NO_SIDE = 0;
  57. constexpr U32 FRONT_SIDE = 1;
  58. constexpr U32 BACK_SIDE = 2;
  59. constexpr U32 LEFT_SIDE = 3;
  60. constexpr U32 RIGHT_SIDE = 4;
  61. constexpr U32 TOP_SIDE = 5;
  62. constexpr U32 BOTTOM_SIDE = 6;
  63. // All functions produce results in the same reference frame as the arguments.
  64. //
  65. // Any arguments of the form "foo_direction" or "foo_normal" are assumed to
  66. // be normalized, or normalized vectors are stored in them.
  67. //
  68. // Vector arguments of the form "shape_scale" represent the scale of the
  69. // object along the three axes.
  70. //
  71. // All functions return the expected true or false, unless otherwise noted.
  72. // When false is returned, any resulting values that might have been stored
  73. // are undefined.
  74. //
  75. // Rays are defined by a "ray_point" and a "ray_direction" (unit).
  76. //
  77. // Lines are defined by a "line_point" and a "line_direction" (unit).
  78. //
  79. // Line segements are defined by "point_a" and "point_b", and for intersection
  80. // purposes are assumed to point from "point_a" to "point_b".
  81. //
  82. // A ray is different from a line in that it starts at a point and extends
  83. // in only one direction.
  84. //
  85. // Intersection normals always point outside the object, normal to the object's
  86. // surface at the point of intersection.
  87. //
  88. // Object rotations passed as quaternions are expected to rotate from the
  89. // object's local frame to the absolute frame. So, if "foo" is a vector in
  90. // the object's local frame, then "foo * object_rotation" is in the absolute
  91. // frame.
  92. // Returns true if line is not parallel to plane.
  93. bool line_plane(const LLVector3& line_point, const LLVector3& line_direction,
  94. const LLVector3& plane_point, const LLVector3 plane_normal,
  95. LLVector3& intersection);
  96. // Returns true if line is not parallel to plane.
  97. bool ray_plane(const LLVector3& ray_point, const LLVector3& ray_direction,
  98. const LLVector3& plane_point, const LLVector3 plane_normal,
  99. LLVector3& intersection);
  100. bool ray_circle(const LLVector3& ray_point, const LLVector3& ray_direction,
  101. const LLVector3& circle_center, const LLVector3 plane_normal,
  102. F32 circle_radius, LLVector3& intersection);
  103. // point_0 through point_2 define the plane_normal via the right-hand rule:
  104. // circle from point_0 to point_2 with fingers ==> thumb points in direction of
  105. // normal
  106. bool ray_triangle(const LLVector3& ray_point, const LLVector3& ray_direction,
  107. const LLVector3& point_0, const LLVector3& point_1,
  108. const LLVector3& point_2, LLVector3& intersection,
  109. LLVector3& intersection_normal);
  110. // point_0 is the lower-left corner, point_1 is the lower-right, point_2 is the
  111. // upper-right right-hand-rule... curl fingers from lower-left toward lower-
  112. // right then toward upper-right ==> thumb points in direction of normal
  113. // Assumes a parallelogram, so point_3 is determined by the other points.
  114. bool ray_quadrangle(const LLVector3& ray_point, const LLVector3& ray_direction,
  115. const LLVector3& point_0, const LLVector3& point_1,
  116. const LLVector3& point_2, LLVector3& intersection,
  117. LLVector3& intersection_normal);
  118. bool ray_sphere(const LLVector3& ray_point, const LLVector3& ray_direction,
  119. const LLVector3& sphere_center, F32 sphere_radius,
  120. LLVector3& intersection, LLVector3& intersection_normal);
  121. // Finite right cylinder is defined by end centers: "cyl_top", "cyl_bottom",
  122. // and by the cylinder radius "cyl_radius"
  123. bool ray_cylinder(const LLVector3& ray_point, const LLVector3& ray_direction,
  124. const LLVector3& cyl_center, const LLVector3& cyl_scale,
  125. const LLQuaternion& cyl_rotation,
  126. LLVector3& intersection, LLVector3& intersection_normal);
  127. // This function doesn't just return a bool because the return is currently
  128. // used to decide how to break up boxes that have been hit by shots...
  129. // a hack that will probably be changed later
  130. //
  131. // returns a number representing the side of the box that was hit by the ray,
  132. // or NO_SIDE if intersection test failed.
  133. U32 ray_box(const LLVector3& ray_point, const LLVector3& ray_direction,
  134. const LLVector3& box_center, const LLVector3& box_scale,
  135. const LLQuaternion& box_rotation,
  136. LLVector3& intersection, LLVector3& intersection_normal);
  137. bool ray_prism(const LLVector3& ray_point, const LLVector3& ray_direction,
  138. const LLVector3& prism_center, const LLVector3& prism_scale,
  139. const LLQuaternion& prism_rotation,
  140. LLVector3& intersection, LLVector3& intersection_normal);
  141. bool ray_tetrahedron(const LLVector3& ray_point,
  142. const LLVector3& ray_direction,
  143. const LLVector3& t_center, const LLVector3& t_scale,
  144. const LLQuaternion& t_rotation,
  145. LLVector3& intersection, LLVector3& intersection_normal);
  146. bool ray_pyramid(const LLVector3& ray_point, const LLVector3& ray_direction,
  147. const LLVector3& p_center, const LLVector3& p_scale,
  148. const LLQuaternion& p_rotation,
  149. LLVector3& intersection, LLVector3& intersection_normal);
  150. bool linesegment_circle(const LLVector3& point_a, const LLVector3& point_b,
  151. const LLVector3& circle_center,
  152. const LLVector3 plane_normal, F32 circle_radius,
  153. LLVector3& intersection);
  154. // point_0 through point_2 define the plane_normal via the right-hand rule:
  155. // circle from point_0 to point_2 with fingers ==> thumb points in direction of
  156. // normal.
  157. bool linesegment_triangle(const LLVector3& point_a, const LLVector3& point_b,
  158. const LLVector3& point_0, const LLVector3& point_1,
  159. const LLVector3& point_2,
  160. LLVector3& intersection,
  161. LLVector3& intersection_normal);
  162. // point_0 is the lower-left corner, point_1 is the lower-right, point_2 is the
  163. // upper-right right-hand-rule... curl fingers from lower-left toward lower-
  164. // right then toward upper-right ==> thumb points in direction of normal
  165. // Assumes a parallelogram, so point_3 is determined by the other points.
  166. bool linesegment_quadrangle(const LLVector3& point_a, const LLVector3& point_b,
  167. const LLVector3& point_0, const LLVector3& point_1,
  168. const LLVector3& point_2,
  169. LLVector3& intersection,
  170. LLVector3& intersection_normal);
  171. bool linesegment_sphere(const LLVector3& point_a, const LLVector3& point_b,
  172. const LLVector3& sphere_center, F32 sphere_radius,
  173. LLVector3& intersection, LLVector3& intersection_normal);
  174. // Finite right cylinder is defined by end centers: "cyl_top", "cyl_bottom",
  175. // and by the cylinder radius "cyl_radius"
  176. bool linesegment_cylinder(const LLVector3& point_a, const LLVector3& point_b,
  177. const LLVector3& cyl_center,
  178. const LLVector3& cyl_scale,
  179. const LLQuaternion& cyl_rotation,
  180. LLVector3& intersection,
  181. LLVector3& intersection_normal);
  182. // This function doesn't just return a bool because the return is currently
  183. // used to decide how to break up boxes that have been hit by shots...
  184. // A hack that will probably be changed later.
  185. //
  186. // Returns a number representing the side of the box that was hit by the ray,
  187. // or NO_SIDE if intersection test failed.
  188. U32 linesegment_box(const LLVector3& point_a, const LLVector3& point_b,
  189. const LLVector3& box_center, const LLVector3& box_scale,
  190. const LLQuaternion& box_rotation,
  191. LLVector3& intersection, LLVector3& intersection_normal);
  192. bool linesegment_prism(const LLVector3& point_a, const LLVector3& point_b,
  193. const LLVector3& prism_center,
  194. const LLVector3& prism_scale,
  195. const LLQuaternion& prism_rotation,
  196. LLVector3& intersection, LLVector3& intersection_normal);
  197. bool linesegment_tetrahedron(const LLVector3& point_a,
  198. const LLVector3& point_b,
  199. const LLVector3& t_center,
  200. const LLVector3& t_scale,
  201. const LLQuaternion& t_rotation,
  202. LLVector3& intersection,
  203. LLVector3& intersection_normal);
  204. bool linesegment_pyramid(const LLVector3& point_a, const LLVector3& point_b,
  205. const LLVector3& p_center, const LLVector3& p_scale,
  206. const LLQuaternion& p_rotation,
  207. LLVector3& intersection,
  208. LLVector3& intersection_normal);
  209. #endif // LL_RAYTRACE_H