aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmath/raytrace.h
blob: b433e1769cc201c6c64e05c232c2d5b9cf2a2ad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/** 
 * @file raytrace.h
 * @brief Ray intersection tests for primitives.
 *
 * $LicenseInfo:firstyear=2001&license=viewergpl$
 * 
 * Copyright (c) 2001-2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#ifndef LL_RAYTRACE_H
#define LL_RAYTRACE_H

class LLVector3;
class LLQuaternion;

// All functions produce results in the same reference frame as the arguments.
//
// Any arguments of the form "foo_direction" or "foo_normal" are assumed to
// be normalized, or normalized vectors are stored in them.
//
// Vector arguments of the form "shape_scale" represent the scale of the
// object along the three axes.
//
// All functions return the expected TRUE or FALSE, unless otherwise noted.
// When FALSE is returned, any resulting values that might have been stored 
// are undefined.
//
// Rays are defined by a "ray_point" and a "ray_direction" (unit).
//
// Lines are defined by a "line_point" and a "line_direction" (unit).
//
// Line segements are defined by "point_a" and "point_b", and for intersection
// purposes are assumed to point from "point_a" to "point_b".
//
// A ray is different from a line in that it starts at a point and extends
// in only one direction.
//
// Intersection normals always point outside the object, normal to the object's
// surface at the point of intersection.
//
// Object rotations passed as quaternions are expected to rotate from the 
// object's local frame to the absolute frame.  So, if "foo" is a vector in
// the object's local frame, then "foo * object_rotation" is in the absolute
// frame.


// returns TRUE iff line is not parallel to plane.
BOOL line_plane(const LLVector3 &line_point, const LLVector3 &line_direction, 
				const LLVector3 &plane_point, const LLVector3 plane_normal, 
				LLVector3 &intersection);


// returns TRUE iff line is not parallel to plane.
BOOL ray_plane(const LLVector3 &ray_point, const LLVector3 &ray_direction, 
			   const LLVector3 &plane_point, const LLVector3 plane_normal, 
			   LLVector3 &intersection);


BOOL ray_circle(const LLVector3 &ray_point, const LLVector3 &ray_direction, 
				const LLVector3 &circle_center, const LLVector3 plane_normal, F32 circle_radius,
				LLVector3 &intersection);

// point_0 through point_2 define the plane_normal via the right-hand rule:
// circle from point_0 to point_2 with fingers ==> thumb points in direction of normal
BOOL ray_triangle(const LLVector3 &ray_point, const LLVector3 &ray_direction, 
				  const LLVector3 &point_0, const LLVector3 &point_1, const LLVector3 &point_2, 
				  LLVector3 &intersection, LLVector3 &intersection_normal);


// point_0 is the lower-left corner, point_1 is the lower-right, point_2 is the upper-right
// right-hand-rule... curl fingers from lower-left toward lower-right then toward upper-right
// ==> thumb points in direction of normal
// assumes a parallelogram, so point_3 is determined by the other points
BOOL ray_quadrangle(const LLVector3 &ray_point, const LLVector3 &ray_direction, 
					const LLVector3 &point_0, const LLVector3 &point_1, const LLVector3 &point_2, 
					LLVector3 &intersection, LLVector3 &intersection_normal);


BOOL ray_sphere(const LLVector3 &ray_point, const LLVector3 &ray_direction,
				const LLVector3 &sphere_center, F32 sphere_radius,
				LLVector3 &intersection, LLVector3 &intersection_normal);


// finite right cylinder is defined by end centers: "cyl_top", "cyl_bottom", 
// and by the cylinder radius "cyl_radius"
BOOL ray_cylinder(const LLVector3 &ray_point, const LLVector3 &ray_direction,
		          const LLVector3 &cyl_center, const LLVector3 &cyl_scale, const LLQuaternion &cyl_rotation,
				  LLVector3 &intersection, LLVector3 &intersection_normal);


// this function doesn't just return a BOOL because the return is currently
// used to decide how to break up boxes that have been hit by shots... 
// a hack that will probably be changed later
//
// returns a number representing the side of the box that was hit by the ray,
// or NO_SIDE if intersection test failed.
U32 ray_box(const LLVector3 &ray_point, const LLVector3 &ray_direction,
		    const LLVector3 &box_center, const LLVector3 &box_scale, const LLQuaternion &box_rotation,
			LLVector3 &intersection, LLVector3 &intersection_normal);


/* TODO
BOOL ray_ellipsoid(const LLVector3 &ray_point, const LLVector3 &ray_direction,
				   const LLVector3 &e_center, const LLVector3 &e_scale, const LLQuaternion &e_rotation,
				   LLVector3 &intersection, LLVector3 &intersection_normal);


BOOL ray_cone(const LLVector3 &ray_point, const LLVector3 &ray_direction,
			  const LLVector3 &cone_tip, const LLVector3 &cone_bottom, 
			  const LLVector3 &cone_scale, const LLQuaternion &cone_rotation,
			  LLVector3 &intersection, LLVector3 &intersection_normal);
*/


BOOL ray_prism(const LLVector3 &ray_point, const LLVector3 &ray_direction,
			   const LLVector3 &prism_center, const LLVector3 &prism_scale, const LLQuaternion &prism_rotation,
			   LLVector3 &intersection, LLVector3 &intersection_normal);


BOOL ray_tetrahedron(const LLVector3 &ray_point, const LLVector3 &ray_direction,
					 const LLVector3 &t_center, const LLVector3 &t_scale, const LLQuaternion &t_rotation,
					 LLVector3 &intersection, LLVector3 &intersection_normal);


BOOL ray_pyramid(const LLVector3 &ray_point, const LLVector3 &ray_direction,
				 const LLVector3 &p_center, const LLVector3 &p_scale, const LLQuaternion &p_rotation,
				 LLVector3 &intersection, LLVector3 &intersection_normal);



/* TODO
BOOL ray_hemiellipsoid(const LLVector3 &ray_point, const LLVector3 &ray_direction,
					   const LLVector3 &e_center, const LLVector3 &e_scale, const LLQuaternion &e_rotation,
					   const LLVector3 &e_cut_normal,
					   LLVector3 &intersection, LLVector3 &intersection_normal);


BOOL ray_hemisphere(const LLVector3 &ray_point, const LLVector3 &ray_direction,
					const LLVector3 &sphere_center, F32 sphere_radius, const LLVector3 &sphere_cut_normal, 
					LLVector3 &intersection, LLVector3 &intersection_normal);


BOOL ray_hemicylinder(const LLVector3 &ray_point, const LLVector3 &ray_direction,
					  const LLVector3 &cyl_top, const LLVector3 &cyl_bottom, F32 cyl_radius, 
					  const LLVector3 &cyl_cut_normal,
					  LLVector3 &intersection, LLVector3 &intersection_normal);


BOOL ray_hemicone(const LLVector3 &ray_point, const LLVector3 &ray_direction,
				  const LLVector3 &cone_tip, const LLVector3 &cone_bottom, 
				  const LLVector3 &cone_scale, const LLVector3 &cyl_cut_normal,
				  LLVector3 &intersection, LLVector3 &intersection_normal);
*/


BOOL linesegment_circle(const LLVector3 &point_a, const LLVector3 &point_b, 
						const LLVector3 &circle_center, const LLVector3 plane_normal, F32 circle_radius,
						LLVector3 &intersection);

// point_0 through point_2 define the plane_normal via the right-hand rule:
// circle from point_0 to point_2 with fingers ==> thumb points in direction of normal
BOOL linesegment_triangle(const LLVector3 &point_a, const LLVector3 &point_b, 
						  const LLVector3 &point_0, const LLVector3 &point_1, const LLVector3 &point_2, 
						  LLVector3 &intersection, LLVector3 &intersection_normal);


// point_0 is the lower-left corner, point_1 is the lower-right, point_2 is the upper-right
// right-hand-rule... curl fingers from lower-left toward lower-right then toward upper-right
// ==> thumb points in direction of normal
// assumes a parallelogram, so point_3 is determined by the other points
BOOL linesegment_quadrangle(const LLVector3 &point_a, const LLVector3 &point_b, 
							const LLVector3 &point_0, const LLVector3 &point_1, const LLVector3 &point_2, 
							LLVector3 &intersection, LLVector3 &intersection_normal);


BOOL linesegment_sphere(const LLVector3 &point_a, const LLVector3 &point_b,
				const LLVector3 &sphere_center, F32 sphere_radius,
				LLVector3 &intersection, LLVector3 &intersection_normal);


// finite right cylinder is defined by end centers: "cyl_top", "cyl_bottom", 
// and by the cylinder radius "cyl_radius"
BOOL linesegment_cylinder(const LLVector3 &point_a, const LLVector3 &point_b,
						  const LLVector3 &cyl_center, const LLVector3 &cyl_scale, const LLQuaternion &cyl_rotation,
						  LLVector3 &intersection, LLVector3 &intersection_normal);


// this function doesn't just return a BOOL because the return is currently
// used to decide how to break up boxes that have been hit by shots... 
// a hack that will probably be changed later
//
// returns a number representing the side of the box that was hit by the ray,
// or NO_SIDE if intersection test failed.
U32 linesegment_box(const LLVector3 &point_a, const LLVector3 &point_b, 
					const LLVector3 &box_center, const LLVector3 &box_scale, const LLQuaternion &box_rotation,
					LLVector3 &intersection, LLVector3 &intersection_normal);


BOOL linesegment_prism(const LLVector3 &point_a, const LLVector3 &point_b,
					   const LLVector3 &prism_center, const LLVector3 &prism_scale, const LLQuaternion &prism_rotation,
					   LLVector3 &intersection, LLVector3 &intersection_normal);


BOOL linesegment_tetrahedron(const LLVector3 &point_a, const LLVector3 &point_b,
							 const LLVector3 &t_center, const LLVector3 &t_scale, const LLQuaternion &t_rotation,
							 LLVector3 &intersection, LLVector3 &intersection_normal);


BOOL linesegment_pyramid(const LLVector3 &point_a, const LLVector3 &point_b,
						 const LLVector3 &p_center, const LLVector3 &p_scale, const LLQuaternion &p_rotation,
						 LLVector3 &intersection, LLVector3 &intersection_normal);


#endif