12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331 |
- /**
- * @file llraytrace.cpp
- * @brief Functions called by box object scripts.
- *
- * $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$
- */
- #include "linden_common.h"
- #include "llraytrace.h"
- #include "llmath.h"
- #include "llquaternion.h"
- #include "llmatrix3.h"
- #include "llvector3.h"
- bool line_plane(const LLVector3& line_point, const LLVector3& line_direction,
- const LLVector3& plane_point, const LLVector3 plane_normal,
- LLVector3& intersection)
- {
- F32 N = line_direction * plane_normal;
- if (0.0f == N)
- {
- // line is perpendicular to plane normal
- // so it is either entirely on plane, or not on plane at all
- return false;
- }
- // Ax + By, + Cz + D = 0
- // D = - (plane_point * plane_normal)
- // N = line_direction * plane_normal
- // intersection = line_point - ((D + plane_normal * line_point) / N) * line_direction
- intersection = line_point -
- ((plane_normal * line_point - plane_point * plane_normal) / N) *
- line_direction;
- return true;
- }
- bool ray_plane(const LLVector3& ray_point, const LLVector3& ray_direction,
- const LLVector3& plane_point, const LLVector3 plane_normal,
- LLVector3& intersection)
- {
- F32 N = ray_direction * plane_normal;
- if (N == 0.f)
- {
- // Ray is perpendicular to plane normal so it is either entirely on
- // plane, or not on plane at all.
- return false;
- }
- // Ax + By, + Cz + D = 0
- // D = - (plane_point * plane_normal)
- // N = ray_direction * plane_normal
- // intersection = ray_point - ((D + plane_normal * ray_point) / N) * ray_direction
- F32 alpha = -(plane_normal * ray_point - plane_point * plane_normal) / N;
- if (alpha < 0.0f)
- {
- // ray points away from plane
- return false;
- }
- intersection = ray_point + alpha * ray_direction;
- return true;
- }
- bool ray_circle(const LLVector3& ray_point, const LLVector3& ray_direction,
- const LLVector3& circle_center, const LLVector3 plane_normal,
- F32 circle_radius, LLVector3& intersection)
- {
- if (ray_plane(ray_point, ray_direction, circle_center, plane_normal,
- intersection))
- {
- if (circle_radius >= (intersection - circle_center).length())
- {
- return true;
- }
- }
- return false;
- }
- 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)
- {
- LLVector3 side_01 = point_1 - point_0;
- LLVector3 side_12 = point_2 - point_1;
- intersection_normal = side_01 % side_12;
- intersection_normal.normalize();
- if (ray_plane(ray_point, ray_direction, point_0, intersection_normal,
- intersection))
- {
- LLVector3 side_20 = point_0 - point_2;
- if (intersection_normal * (side_01 % (intersection - point_0)) >= 0.0f &&
- intersection_normal * (side_12 % (intersection - point_1)) >= 0.0f &&
- intersection_normal * (side_20 % (intersection - point_2)) >= 0.0f)
- {
- return true;
- }
- }
- return false;
- }
- // assumes a parallelogram
- 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)
- {
- LLVector3 side_01 = point_1 - point_0;
- LLVector3 side_12 = point_2 - point_1;
- intersection_normal = side_01 % side_12;
- intersection_normal.normalize();
- if (ray_plane(ray_point, ray_direction, point_0, intersection_normal,
- intersection))
- {
- LLVector3 point_3 = point_0 + (side_12);
- LLVector3 side_23 = point_3 - point_2;
- LLVector3 side_30 = point_0 - point_3;
- if (intersection_normal * (side_01 % (intersection - point_0)) >= 0.0f &&
- intersection_normal * (side_12 % (intersection - point_1)) >= 0.0f &&
- intersection_normal * (side_23 % (intersection - point_2)) >= 0.0f &&
- intersection_normal * (side_30 % (intersection - point_3)) >= 0.0f)
- {
- return true;
- }
- }
- return false;
- }
- bool ray_sphere(const LLVector3& ray_point, const LLVector3& ray_direction,
- const LLVector3& sphere_center, F32 sphere_radius,
- LLVector3& intersection, LLVector3& intersection_normal)
- {
- LLVector3 ray_to_sphere = sphere_center - ray_point;
- F32 dot = ray_to_sphere * ray_direction;
- LLVector3 closest_approach = dot * ray_direction - ray_to_sphere;
- F32 shortest_distance = closest_approach.lengthSquared();
- F32 radius_squared = sphere_radius * sphere_radius;
- if (shortest_distance > radius_squared)
- {
- return false;
- }
- F32 half_chord = sqrtf(radius_squared - shortest_distance);
- // closest_approach in absolute coordinates:
- closest_approach = sphere_center + closest_approach;
- intersection = closest_approach + half_chord * ray_direction;
- dot = ray_direction * (intersection - ray_point);
- if (dot < 0.0f)
- {
- // ray shoots away from sphere and is not inside it
- return false;
- }
- shortest_distance = ray_direction *
- ((closest_approach - half_chord * ray_direction) -
- ray_point);
- if (shortest_distance > 0.0f)
- {
- // ray enters sphere
- intersection = intersection - (2.0f * half_chord) * ray_direction;
- }
- else
- {
- // do nothing
- // ray starts inside sphere and intersects as it leaves the sphere
- }
- intersection_normal = intersection - sphere_center;
- if (sphere_radius > 0.0f)
- {
- intersection_normal *= 1.0f / sphere_radius;
- }
- else
- {
- intersection_normal.set(0.0f, 0.0f, 0.0f);
- }
- return true;
- }
- 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)
- {
- // calculate the centers of the cylinder caps in the absolute frame
- LLVector3 cyl_top(0.0f, 0.0f, 0.5f * cyl_scale.mV[VZ]);
- LLVector3 cyl_bottom(0.0f, 0.0f, -cyl_top.mV[VZ]);
- cyl_top = (cyl_top * cyl_rotation) + cyl_center;
- cyl_bottom = (cyl_bottom * cyl_rotation) + cyl_center;
- // We only handle cylinders with circular cross-sections at the moment.
- // HACK until scaled cylinders are supported.
- F32 cyl_radius = 0.5f * llmax(cyl_scale.mV[VX], cyl_scale.mV[VY]);
- // This implementation is based on the intcyl() function from
- // Graphics_Gems_IV, page 361
- LLVector3 cyl_axis; // axis direction (bottom toward top)
- LLVector3 ray_to_cyl; // ray_point to cyl_top
- F32 shortest_distance; // shortest distance from ray to axis
- F32 cyl_length;
- LLVector3 shortest_direction;
- LLVector3 temp_vector;
- cyl_axis = cyl_bottom - cyl_top;
- cyl_length = cyl_axis.normalize();
- ray_to_cyl = ray_point - cyl_bottom;
- shortest_direction = ray_direction % cyl_axis;
- // recycle shortest_distance
- shortest_distance = shortest_direction.normalize();
- // check for ray parallel to cylinder axis
- if (0.0f == shortest_distance)
- {
- // ray is parallel to cylinder axis
- temp_vector = ray_to_cyl - (ray_to_cyl * cyl_axis) * cyl_axis;
- shortest_distance = temp_vector.length();
- if (shortest_distance <= cyl_radius)
- {
- shortest_distance = ray_to_cyl * cyl_axis;
- F32 dot = ray_direction * cyl_axis;
- if (shortest_distance > 0.0)
- {
- if (dot > 0.0f)
- {
- // ray points away from cylinder bottom
- return false;
- }
- // ray hit bottom of cylinder from outside
- intersection = ray_point - shortest_distance * cyl_axis;
- intersection_normal = cyl_axis;
- }
- else if (shortest_distance > -cyl_length)
- {
- // ray starts inside cylinder
- if (dot < 0.0f)
- {
- // ray hit top from inside
- intersection = ray_point -
- (cyl_length + shortest_distance) * cyl_axis;
- intersection_normal = -cyl_axis;
- }
- else
- {
- // ray hit bottom from inside
- intersection = ray_point - shortest_distance * cyl_axis;
- intersection_normal = cyl_axis;
- }
- }
- else
- {
- if (dot < 0.0f)
- {
- // ray points away from cylinder bottom
- return false;
- }
- // ray hit top from outside
- intersection = ray_point -
- (shortest_distance + cyl_length) * cyl_axis;
- intersection_normal = -cyl_axis;
- }
- return true;
- }
- return false;
- }
- // check for intersection with infinite cylinder
- shortest_distance = (F32) fabs(ray_to_cyl * shortest_direction);
- if (shortest_distance <= cyl_radius)
- {
- temp_vector = ray_to_cyl % cyl_axis;
- temp_vector = shortest_direction % cyl_axis;
- temp_vector.normalize();
- // Half length of intersection chord
- F32 half_chord_length = fabsf(sqrtf(cyl_radius * cyl_radius -
- shortest_distance * shortest_distance) /
- (ray_direction * temp_vector));
- // Distance from ray_point to closest_point
- F32 dist_to_closest_point = -(temp_vector * shortest_direction);
- // Distance to exiting point
- F32 out = dist_to_closest_point + half_chord_length;
- if (out < 0.f)
- {
- // cylinder is behind the ray, so we return false
- return false;
- }
- // Distance to entering point:
- F32 in = dist_to_closest_point - half_chord_length;
- if (in < 0.f)
- {
- // ray_point is inside the cylinder
- // so we store the exiting intersection
- intersection = ray_point + out * ray_direction;
- shortest_distance = out;
- }
- else
- {
- // ray hit cylinder from outside
- // so we store the entering intersection
- intersection = ray_point + in * ray_direction;
- shortest_distance = in;
- }
- // calculate the normal at intersection
- if (0.0f == cyl_radius)
- {
- intersection_normal.set(0.0f, 0.0f, 0.0f);
- }
- else
- {
- temp_vector = intersection - cyl_bottom;
- intersection_normal = temp_vector -
- (temp_vector * cyl_axis) * cyl_axis;
- intersection_normal.normalize();
- }
- // check for intersection with end caps
- // calculate intersection of ray and top plane
- // NOTE: side-effect: changing temp_vector
- if (line_plane(ray_point, ray_direction, cyl_top, -cyl_axis,
- temp_vector))
- {
- shortest_distance = (temp_vector - ray_point).length();
- if ((ray_direction * cyl_axis) > 0.0f)
- {
- // ray potentially enters the cylinder at top
- if (shortest_distance > out)
- {
- // ray missed the finite cylinder
- return false;
- }
- if (shortest_distance > in)
- {
- // ray intersects cylinder at top plane
- intersection = temp_vector;
- intersection_normal = -cyl_axis;
- return true;
- }
- }
- else
- {
- // ray potentially exits the cylinder at top
- if (shortest_distance < in)
- {
- // missed the finite cylinder
- return false;
- }
- }
- // calculate intersection of ray and bottom plane
- // NOTE: side-effect: changing temp_vector
- line_plane(ray_point, ray_direction, cyl_bottom, cyl_axis,
- temp_vector);
- shortest_distance = (temp_vector - ray_point).length();
- if ((ray_direction * cyl_axis) < 0.0)
- {
- // ray potentially enters the cylinder at bottom
- if (shortest_distance > out)
- {
- // ray missed the finite cylinder
- return false;
- }
- if (shortest_distance > in)
- {
- // ray intersects cylinder at bottom plane
- intersection = temp_vector;
- intersection_normal = cyl_axis;
- return true;
- }
- }
- else
- {
- // ray potentially exits the cylinder at bottom
- if (shortest_distance < in)
- {
- // ray missed the finite cylinder
- return false;
- }
- }
- }
- else
- {
- // ray is parallel to end cap planes
- temp_vector = cyl_bottom - ray_point;
- shortest_distance = temp_vector * cyl_axis;
- if (shortest_distance < 0.0f || shortest_distance > cyl_length)
- {
- // ray missed finite cylinder
- return false;
- }
- }
- return true;
- }
- return false;
- }
- 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)
- {
- // Need to rotate into box frame
- // Rotates things from box frame to absolute:
- LLQuaternion into_box_frame(box_rotation);
- // Now rotates things into box frame:
- into_box_frame.transpose();
- LLVector3 line_point = (ray_point - box_center) * into_box_frame;
- LLVector3 line_direction = ray_direction * into_box_frame;
- // Suppose we have a plane: Ax + By + Cz + D = 0
- // then, assuming [A, B, C] is a unit vector:
- // plane_normal = [A, B, C]
- // D = - (plane_normal * plane_point)
- //
- // Suppose we have a line: X = line_point + alpha * line_direction
- //
- // the intersection of the plane and line determines alpha:
- // alpha = - (D + plane_normal * line_point) / (plane_normal * line_direction)
- LLVector3 line_plane_intersection;
- F32 pointX = line_point.mV[VX];
- F32 pointY = line_point.mV[VY];
- F32 pointZ = line_point.mV[VZ];
- F32 dirX = line_direction.mV[VX];
- F32 dirY = line_direction.mV[VY];
- F32 dirZ = line_direction.mV[VZ];
- // we'll be using the half-scales of the box
- F32 boxX = 0.5f * box_scale.mV[VX];
- F32 boxY = 0.5f * box_scale.mV[VY];
- F32 boxZ = 0.5f * box_scale.mV[VZ];
- // check to see if line_point is OUTSIDE the box
- if (pointX < -boxX || pointX > boxX || pointY < -boxY || pointY > boxY ||
- pointZ < -boxZ || pointZ > boxZ)
- {
- // -------------- point is OUTSIDE the box ----------------
- // front
- if (pointX > 0.0f && dirX < 0.0f)
- {
- // plane_normal = [ 1, 0, 0]
- // plane_normal*line_point = pointX
- // plane_normal*line_direction = dirX
- // D = -boxX
- // alpha = - (-boxX + pointX) / dirX
- line_plane_intersection = line_point -
- ((pointX - boxX) / dirX) * line_direction;
- if (line_plane_intersection.mV[VY] < boxY &&
- line_plane_intersection.mV[VY] > -boxY &&
- line_plane_intersection.mV[VZ] < boxZ &&
- line_plane_intersection.mV[VZ] > -boxZ)
- {
- intersection = line_plane_intersection * box_rotation + box_center;
- intersection_normal = LLVector3(1.0f, 0.0f, 0.0f) * box_rotation;
- return FRONT_SIDE;
- }
- }
- // back
- if (pointX < 0.0f && dirX > 0.0f)
- {
- // plane_normal = [ -1, 0, 0]
- // plane_normal*line_point = -pX
- // plane_normal*line_direction = -direction.mV[VX]
- // D = -bX
- // alpha = - (-bX - pX) / (-dirX)
- line_plane_intersection = line_point -
- ((boxX + pointX)/ dirX) * line_direction;
- if (line_plane_intersection.mV[VY] < boxY &&
- line_plane_intersection.mV[VY] > -boxY &&
- line_plane_intersection.mV[VZ] < boxZ &&
- line_plane_intersection.mV[VZ] > -boxZ)
- {
- intersection = line_plane_intersection * box_rotation + box_center;
- intersection_normal = LLVector3(-1.0f, 0.0f, 0.0f) * box_rotation;
- return BACK_SIDE;
- }
- }
- // left
- if (pointY > 0.0f && dirY < 0.0f)
- {
- // plane_normal = [0, 1, 0]
- // plane_normal*line_point = pointY
- // plane_normal*line_direction = dirY
- // D = -boxY
- // alpha = - (-boxY + pointY) / dirY
- line_plane_intersection = line_point +
- ((boxY - pointY) / dirY) * line_direction;
- if (line_plane_intersection.mV[VX] < boxX &&
- line_plane_intersection.mV[VX] > -boxX &&
- line_plane_intersection.mV[VZ] < boxZ &&
- line_plane_intersection.mV[VZ] > -boxZ)
- {
- intersection = line_plane_intersection * box_rotation + box_center;
- intersection_normal = LLVector3(0.0f, 1.0f, 0.0f) * box_rotation;
- return LEFT_SIDE;
- }
- }
- // right
- if (pointY < 0.0f && dirY > 0.0f)
- {
- // plane_normal = [0, -1, 0]
- // plane_normal*line_point = -pointY
- // plane_normal*line_direction = -dirY
- // D = -boxY
- // alpha = - (-boxY - pointY) / (-dirY)
- line_plane_intersection = line_point - ((boxY + pointY) / dirY) * line_direction;
- if (line_plane_intersection.mV[VX] < boxX &&
- line_plane_intersection.mV[VX] > -boxX &&
- line_plane_intersection.mV[VZ] < boxZ &&
- line_plane_intersection.mV[VZ] > -boxZ)
- {
- intersection = line_plane_intersection * box_rotation + box_center;
- intersection_normal = LLVector3(0.0f, -1.0f, 0.0f) * box_rotation;
- return RIGHT_SIDE;
- }
- }
- // top
- if (pointZ > 0.0f && dirZ < 0.0f)
- {
- // plane_normal = [0, 0, 1]
- // plane_normal*line_point = pointZ
- // plane_normal*line_direction = dirZ
- // D = -boxZ
- // alpha = - (-boxZ + pointZ) / dirZ
- line_plane_intersection = line_point - ((pointZ - boxZ) / dirZ) * line_direction;
- if (line_plane_intersection.mV[VX] < boxX &&
- line_plane_intersection.mV[VX] > -boxX &&
- line_plane_intersection.mV[VY] < boxY &&
- line_plane_intersection.mV[VY] > -boxY)
- {
- intersection = line_plane_intersection * box_rotation + box_center;
- intersection_normal = LLVector3(0.0f, 0.0f, 1.0f) * box_rotation;
- return TOP_SIDE;
- }
- }
- // bottom
- if (pointZ < 0.0f && dirZ > 0.0f)
- {
- // plane_normal = [0, 0, -1]
- // plane_normal*line_point = -pointZ
- // plane_normal*line_direction = -dirZ
- // D = -boxZ
- // alpha = - (-boxZ - pointZ) / (-dirZ)
- line_plane_intersection = line_point -
- ((boxZ + pointZ) / dirZ) * line_direction;
- if (line_plane_intersection.mV[VX] < boxX &&
- line_plane_intersection.mV[VX] > -boxX &&
- line_plane_intersection.mV[VY] < boxY &&
- line_plane_intersection.mV[VY] > -boxY)
- {
- intersection = line_plane_intersection * box_rotation + box_center;
- intersection_normal = LLVector3(0.0f, 0.0f, -1.0f) * box_rotation;
- return BOTTOM_SIDE;
- }
- }
- return NO_SIDE;
- }
- // -------------- point is INSIDE the box ----------------
- // front
- if (dirX > 0.0f)
- {
- // plane_normal = [ 1, 0, 0]
- // plane_normal*line_point = pointX
- // plane_normal*line_direction = dirX
- // D = -boxX
- // alpha = - (-boxX + pointX) / dirX
- line_plane_intersection = line_point - ((pointX - boxX) / dirX) * line_direction;
- if (line_plane_intersection.mV[VY] < boxY &&
- line_plane_intersection.mV[VY] > -boxY &&
- line_plane_intersection.mV[VZ] < boxZ &&
- line_plane_intersection.mV[VZ] > -boxZ)
- {
- intersection = line_plane_intersection * box_rotation + box_center;
- intersection_normal = LLVector3(1.0f, 0.0f, 0.0f) * box_rotation;
- return FRONT_SIDE;
- }
- }
- // back
- if (dirX < 0.0f)
- {
- // plane_normal = [ -1, 0, 0]
- // plane_normal*line_point = -pX
- // plane_normal*line_direction = -direction.mV[VX]
- // D = -bX
- // alpha = - (-bX - pX) / (-dirX)
- line_plane_intersection = line_point -
- ((boxX + pointX) / dirX) * line_direction;
- if (line_plane_intersection.mV[VY] < boxY &&
- line_plane_intersection.mV[VY] > -boxY &&
- line_plane_intersection.mV[VZ] < boxZ &&
- line_plane_intersection.mV[VZ] > -boxZ)
- {
- intersection = line_plane_intersection * box_rotation + box_center;
- intersection_normal = LLVector3(-1.0f, 0.0f, 0.0f) * box_rotation;
- return BACK_SIDE;
- }
- }
- // left
- if (dirY > 0.0f)
- {
- // plane_normal = [0, 1, 0]
- // plane_normal*line_point = pointY
- // plane_normal*line_direction = dirY
- // D = -boxY
- // alpha = - (-boxY + pointY) / dirY
- line_plane_intersection = line_point +
- ((boxY - pointY) / dirY) * line_direction;
- if (line_plane_intersection.mV[VX] < boxX &&
- line_plane_intersection.mV[VX] > -boxX &&
- line_plane_intersection.mV[VZ] < boxZ &&
- line_plane_intersection.mV[VZ] > -boxZ)
- {
- intersection = line_plane_intersection * box_rotation + box_center;
- intersection_normal = LLVector3(0.0f, 1.0f, 0.0f) * box_rotation;
- return LEFT_SIDE;
- }
- }
- // right
- if (dirY < 0.0f)
- {
- // plane_normal = [0, -1, 0]
- // plane_normal*line_point = -pointY
- // plane_normal*line_direction = -dirY
- // D = -boxY
- // alpha = - (-boxY - pointY) / (-dirY)
- line_plane_intersection = line_point -
- ((boxY + pointY) / dirY) * line_direction;
- if (line_plane_intersection.mV[VX] < boxX &&
- line_plane_intersection.mV[VX] > -boxX &&
- line_plane_intersection.mV[VZ] < boxZ &&
- line_plane_intersection.mV[VZ] > -boxZ)
- {
- intersection = line_plane_intersection * box_rotation + box_center;
- intersection_normal = LLVector3(0.0f, -1.0f, 0.0f) * box_rotation;
- return RIGHT_SIDE;
- }
- }
- // top
- if (dirZ > 0.0f)
- {
- // plane_normal = [0, 0, 1]
- // plane_normal*line_point = pointZ
- // plane_normal*line_direction = dirZ
- // D = -boxZ
- // alpha = - (-boxZ + pointZ) / dirZ
- line_plane_intersection = line_point -
- ((pointZ - boxZ) / dirZ) * line_direction;
- if (line_plane_intersection.mV[VX] < boxX &&
- line_plane_intersection.mV[VX] > -boxX &&
- line_plane_intersection.mV[VY] < boxY &&
- line_plane_intersection.mV[VY] > -boxY)
- {
- intersection = line_plane_intersection * box_rotation + box_center;
- intersection_normal = LLVector3(0.0f, 0.0f, 1.0f) * box_rotation;
- return TOP_SIDE;
- }
- }
- // bottom
- if (dirZ < 0.0f)
- {
- // plane_normal = [0, 0, -1]
- // plane_normal*line_point = -pointZ
- // plane_normal*line_direction = -dirZ
- // D = -boxZ
- // alpha = - (-boxZ - pointZ) / (-dirZ)
- line_plane_intersection = line_point -
- ((boxZ + pointZ) / dirZ) * line_direction;
- if (line_plane_intersection.mV[VX] < boxX &&
- line_plane_intersection.mV[VX] > -boxX &&
- line_plane_intersection.mV[VY] < boxY &&
- line_plane_intersection.mV[VY] > -boxY)
- {
- intersection = line_plane_intersection * box_rotation + box_center;
- intersection_normal = LLVector3(0.0f, 0.0f, -1.0f) * box_rotation;
- return BOTTOM_SIDE;
- }
- }
- // Should never get here unless line instersects at tangent point on edge
- // or corner, however such cases will be EXTREMELY rare.
- return NO_SIDE;
- }
- 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)
- {
- // (0) Z
- // /| \ .
- // (1)| \ /|\ _.Y
- // | \ \ | /|
- // | |\ \ | /
- // | | \(0)\ | /
- // | | \ \ |/
- // | | \ \ (*)----> X
- // |(3)---\---(2)
- // |/ \ /
- // (4)-------(5)
- // need to calculate the points of the prism so we can run ray tests with
- // each face
- F32 x = prism_scale.mV[VX];
- F32 y = prism_scale.mV[VY];
- F32 z = prism_scale.mV[VZ];
- F32 tx = x * 2.0f / 3.0f;
- F32 ty = y * 0.5f;
- F32 tz = z * 2.0f / 3.0f;
- LLVector3 point0(tx-x, ty, tz);
- LLVector3 point1(tx-x, -ty, tz);
- LLVector3 point2(tx, ty, tz-z);
- LLVector3 point3(tx-x, ty, tz-z);
- LLVector3 point4(tx-x, -ty, tz-z);
- LLVector3 point5(tx, -ty, tz-z);
- // transform these points into absolute frame
- point0 = (point0 * prism_rotation) + prism_center;
- point1 = (point1 * prism_rotation) + prism_center;
- point2 = (point2 * prism_rotation) + prism_center;
- point3 = (point3 * prism_rotation) + prism_center;
- point4 = (point4 * prism_rotation) + prism_center;
- point5 = (point5 * prism_rotation) + prism_center;
- // test ray intersection for each face
- bool b_hit = false;
- LLVector3 face_intersection, face_normal;
- F32 distance_squared = 0.0f;
- F32 temp;
- // face 0
- if (ray_direction * ((point0 - point2) % (point5 - point2)) < 0.0f &&
- ray_quadrangle(ray_point, ray_direction, point5, point2, point0,
- intersection, intersection_normal))
- {
- distance_squared = (ray_point - intersection).lengthSquared();
- b_hit = true;
- }
- // face 1
- if (ray_direction * ((point0 - point3) % (point2 - point3)) < 0.0f &&
- ray_triangle(ray_point, ray_direction, point2, point3, point0,
- face_intersection, face_normal))
- {
- if (b_hit)
- {
- temp = (ray_point - face_intersection).lengthSquared();
- if (temp < distance_squared)
- {
- distance_squared = temp;
- intersection = face_intersection;
- intersection_normal = face_normal;
- }
- }
- else
- {
- distance_squared = (ray_point - face_intersection).lengthSquared();
- intersection = face_intersection;
- intersection_normal = face_normal;
- b_hit = true;
- }
- }
- // face 2
- if (ray_direction * ((point1 - point4) % (point3 - point4)) < 0.0f &&
- ray_quadrangle(ray_point, ray_direction, point3, point4, point1,
- face_intersection, face_normal))
- {
- if (b_hit)
- {
- temp = (ray_point - face_intersection).lengthSquared();
- if (temp < distance_squared)
- {
- distance_squared = temp;
- intersection = face_intersection;
- intersection_normal = face_normal;
- }
- }
- else
- {
- distance_squared = (ray_point - face_intersection).lengthSquared();
- intersection = face_intersection;
- intersection_normal = face_normal;
- b_hit = true;
- }
- }
- // face 3
- if (ray_direction * ((point5 - point4) % (point1 - point4)) < 0.0f &&
- ray_triangle(ray_point, ray_direction, point1, point4, point5,
- face_intersection, face_normal))
- {
- if (b_hit)
- {
- temp = (ray_point - face_intersection).lengthSquared();
- if (temp < distance_squared)
- {
- distance_squared = temp;
- intersection = face_intersection;
- intersection_normal = face_normal;
- }
- }
- else
- {
- distance_squared = (ray_point - face_intersection).lengthSquared();
- intersection = face_intersection;
- intersection_normal = face_normal;
- b_hit = true;
- }
- }
- // face 4
- if (ray_direction * ((point4 - point5) % (point2 - point5)) < 0.0f &&
- ray_quadrangle(ray_point, ray_direction, point2, point5, point4,
- face_intersection, face_normal))
- {
- if (b_hit)
- {
- temp = (ray_point - face_intersection).lengthSquared();
- if (temp < distance_squared)
- {
- distance_squared = temp;
- intersection = face_intersection;
- intersection_normal = face_normal;
- }
- }
- else
- {
- distance_squared = (ray_point - face_intersection).lengthSquared();
- intersection = face_intersection;
- intersection_normal = face_normal;
- b_hit = true;
- }
- }
- return b_hit;
- }
- 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)
- {
- // height of unit triangle
- F32 a = 0.5f * F_SQRT3;
- // distance of center of unit triangle to each point
- F32 b = 1.0f / F_SQRT3;
- // height of unit tetrahedron
- F32 c = F_SQRT2 / F_SQRT3;
- // distance of center of tetrahedron to each point
- F32 d = 0.5f * F_SQRT3 / F_SQRT2;
- // If we want the tetrahedron to have unit height (c = 1.0) then we need to
- // divide each constant by hieght of a unit tetrahedron:
- F32 oo_c = 1.0f / c;
- a = a * oo_c;
- b = b * oo_c;
- c = 1.0f;
- d = d * oo_c;
- F32 e = 0.5f * oo_c;
- LLVector3 point0(0.0f, 0.0f, t_scale.mV[VZ] * d);
- LLVector3 point1(t_scale.mV[VX] * b, 0.0f, t_scale.mV[VZ] * (d - c));
- LLVector3 point2(t_scale.mV[VX] * (b - a), e * t_scale.mV[VY],
- t_scale.mV[VZ] * (d - c));
- LLVector3 point3(t_scale.mV[VX] * (b - a), -e * t_scale.mV[VY],
- t_scale.mV[VZ] * (d - c));
- // Transform these points into absolute frame
- point0 = (point0 * t_rotation) + t_center;
- point1 = (point1 * t_rotation) + t_center;
- point2 = (point2 * t_rotation) + t_center;
- point3 = (point3 * t_rotation) + t_center;
- // Test ray intersection for each face
- bool b_hit = false;
- LLVector3 face_intersection, face_normal;
- F32 distance_squared = 1.0e12f;
- F32 temp;
- // face 0
- if (ray_direction * ((point2 - point1) % (point0 - point1)) < 0.0f &&
- ray_triangle(ray_point, ray_direction, point1, point2, point0,
- intersection, intersection_normal))
- {
- distance_squared = (ray_point - intersection).lengthSquared();
- b_hit = true;
- }
- // face 1
- if (ray_direction * ((point3 - point2) % (point0 - point2)) < 0.0f &&
- ray_triangle(ray_point, ray_direction, point2, point3, point0,
- face_intersection, face_normal))
- {
- if (b_hit)
- {
- temp = (ray_point - face_intersection).lengthSquared();
- if (temp < distance_squared)
- {
- distance_squared = temp;
- intersection = face_intersection;
- intersection_normal = face_normal;
- }
- }
- else
- {
- distance_squared = (ray_point - face_intersection).lengthSquared();
- intersection = face_intersection;
- intersection_normal = face_normal;
- b_hit = true;
- }
- }
- // face 2
- if (ray_direction * ((point1 - point3) % (point0 - point3)) < 0.0f &&
- ray_triangle(ray_point, ray_direction, point3, point1, point0,
- face_intersection, face_normal))
- {
- if (b_hit)
- {
- temp = (ray_point - face_intersection).lengthSquared();
- if (temp < distance_squared)
- {
- distance_squared = temp;
- intersection = face_intersection;
- intersection_normal = face_normal;
- }
- }
- else
- {
- distance_squared = (ray_point - face_intersection).lengthSquared();
- intersection = face_intersection;
- intersection_normal = face_normal;
- b_hit = true;
- }
- }
- // face 3
- if (ray_direction * ((point2 - point3) % (point1 - point3)) < 0.0f &&
- ray_triangle(ray_point, ray_direction, point3, point2, point1,
- face_intersection, face_normal))
- {
- if (b_hit)
- {
- temp = (ray_point - face_intersection).lengthSquared();
- if (temp < distance_squared)
- {
- intersection = face_intersection;
- intersection_normal = face_normal;
- }
- }
- else
- {
- intersection = face_intersection;
- intersection_normal = face_normal;
- b_hit = true;
- }
- }
- return b_hit;
- }
- 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)
- {
- // center of mass of pyramid is located 1/4 its height from the base
- F32 x = 0.5f * p_scale.mV[VX];
- F32 y = 0.5f * p_scale.mV[VY];
- F32 z = 0.25f * p_scale.mV[VZ];
- LLVector3 point0(0.0f, 0.0f, p_scale.mV[VZ] - z);
- LLVector3 point1(x, y, -z);
- LLVector3 point2(-x, y, -z);
- LLVector3 point3(-x, -y, -z);
- LLVector3 point4(x, -y, -z);
- // transform these points into absolute frame
- point0 = (point0 * p_rotation) + p_center;
- point1 = (point1 * p_rotation) + p_center;
- point2 = (point2 * p_rotation) + p_center;
- point3 = (point3 * p_rotation) + p_center;
- point4 = (point4 * p_rotation) + p_center;
- // test ray intersection for each face
- bool b_hit = false;
- LLVector3 face_intersection, face_normal;
- F32 distance_squared = 1.0e12f;
- F32 temp;
- // face 0
- if (ray_direction * ((point1 - point4) % (point0 - point4)) < 0.0f &&
- ray_triangle(ray_point, ray_direction, point4, point1, point0,
- intersection, intersection_normal))
- {
- distance_squared = (ray_point - intersection).lengthSquared();
- b_hit = true;
- }
- // face 1
- if (ray_direction * ((point2 - point1) % (point0 - point1)) < 0.0f &&
- ray_triangle(ray_point, ray_direction, point1, point2, point0,
- face_intersection, face_normal))
- {
- if (b_hit)
- {
- temp = (ray_point - face_intersection).lengthSquared();
- if (temp < distance_squared)
- {
- distance_squared = temp;
- intersection = face_intersection;
- intersection_normal = face_normal;
- }
- }
- else
- {
- distance_squared = (ray_point - face_intersection).lengthSquared();
- intersection = face_intersection;
- intersection_normal = face_normal;
- b_hit = true;
- }
- }
- // face 2
- if (ray_direction * ((point3 - point2) % (point0 - point2)) < 0.0f &&
- ray_triangle(ray_point, ray_direction, point2, point3, point0,
- face_intersection, face_normal))
- {
- if (b_hit)
- {
- temp = (ray_point - face_intersection).lengthSquared();
- if (temp < distance_squared)
- {
- distance_squared = temp;
- intersection = face_intersection;
- intersection_normal = face_normal;
- }
- }
- else
- {
- distance_squared = (ray_point - face_intersection).lengthSquared();
- intersection = face_intersection;
- intersection_normal = face_normal;
- b_hit = true;
- }
- }
- // face 3
- if (ray_direction * ((point4 - point3) % (point0 - point3)) < 0.0f &&
- ray_triangle(ray_point, ray_direction, point3, point4, point0,
- face_intersection, face_normal))
- {
- if (b_hit)
- {
- temp = (ray_point - face_intersection).lengthSquared();
- if (temp < distance_squared)
- {
- distance_squared = temp;
- intersection = face_intersection;
- intersection_normal = face_normal;
- }
- }
- else
- {
- distance_squared = (ray_point - face_intersection).lengthSquared();
- intersection = face_intersection;
- intersection_normal = face_normal;
- b_hit = true;
- }
- }
- // face 4
- if (ray_direction * ((point3 - point4) % (point2 - point4)) < 0.0f &&
- ray_quadrangle(ray_point, ray_direction, point4, point3, point2,
- face_intersection, face_normal))
- {
- if (b_hit)
- {
- temp = (ray_point - face_intersection).lengthSquared();
- if (temp < distance_squared)
- {
- intersection = face_intersection;
- intersection_normal = face_normal;
- }
- }
- else
- {
- intersection = face_intersection;
- intersection_normal = face_normal;
- b_hit = true;
- }
- }
- return b_hit;
- }
- bool linesegment_circle(const LLVector3& point_a, const LLVector3& point_b,
- const LLVector3& circle_center,
- const LLVector3 plane_normal, F32 circle_radius,
- LLVector3& intersection)
- {
- LLVector3 ray_direction = point_b - point_a;
- F32 segment_length = ray_direction.normalize();
- if (ray_circle(point_a, ray_direction, circle_center, plane_normal,
- circle_radius, intersection))
- {
- if (segment_length >= (point_a - intersection).length())
- {
- return true;
- }
- }
- return false;
- }
- 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)
- {
- LLVector3 ray_direction = point_b - point_a;
- F32 segment_length = ray_direction.normalize();
- if (ray_triangle(point_a, ray_direction, point_0, point_1, point_2,
- intersection, intersection_normal))
- {
- if (segment_length >= (point_a - intersection).length())
- {
- return true;
- }
- }
- return false;
- }
- 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)
- {
- LLVector3 ray_direction = point_b - point_a;
- F32 segment_length = ray_direction.normalize();
- if (ray_quadrangle(point_a, ray_direction, point_0, point_1, point_2,
- intersection, intersection_normal))
- {
- if (segment_length >= (point_a - intersection).length())
- {
- return true;
- }
- }
- return false;
- }
- bool linesegment_sphere(const LLVector3& point_a, const LLVector3& point_b,
- const LLVector3& sphere_center, F32 sphere_radius,
- LLVector3& intersection, LLVector3& intersection_normal)
- {
- LLVector3 ray_direction = point_b - point_a;
- F32 segment_length = ray_direction.normalize();
- if (ray_sphere(point_a, ray_direction, sphere_center, sphere_radius,
- intersection, intersection_normal))
- {
- if (segment_length >= (point_a - intersection).length())
- {
- return true;
- }
- }
- return false;
- }
- 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)
- {
- LLVector3 ray_direction = point_b - point_a;
- F32 segment_length = ray_direction.normalize();
- if (ray_cylinder(point_a, ray_direction, cyl_center, cyl_scale,
- cyl_rotation, intersection, intersection_normal))
- {
- if (segment_length >= (point_a - intersection).length())
- {
- return true;
- }
- }
- return false;
- }
- 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)
- {
- LLVector3 direction = point_b - point_a;
- if (direction.isNull())
- {
- return NO_SIDE;
- }
- F32 segment_length = direction.normalize();
- U32 box_side = ray_box(point_a, direction, box_center, box_scale,
- box_rotation, intersection, intersection_normal);
- if (NO_SIDE == box_side ||
- segment_length < (intersection - point_a).length())
- {
- return NO_SIDE;
- }
- return box_side;
- }
- 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)
- {
- LLVector3 ray_direction = point_b - point_a;
- F32 segment_length = ray_direction.normalize();
- if (ray_prism(point_a, ray_direction, prism_center, prism_scale,
- prism_rotation, intersection, intersection_normal))
- {
- if (segment_length >= (point_a - intersection).length())
- {
- return true;
- }
- }
- return false;
- }
- 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)
- {
- LLVector3 ray_direction = point_b - point_a;
- F32 segment_length = ray_direction.normalize();
- if (ray_tetrahedron(point_a, ray_direction, t_center, t_scale, t_rotation,
- intersection, intersection_normal))
- {
- if (segment_length >= (point_a - intersection).length())
- {
- return true;
- }
- }
- return false;
- }
- 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)
- {
- LLVector3 ray_direction = point_b - point_a;
- F32 segment_length = ray_direction.normalize();
- if (ray_pyramid(point_a, ray_direction, p_center, p_scale, p_rotation,
- intersection, intersection_normal))
- {
- if (segment_length >= (point_a - intersection).length())
- {
- return true;
- }
- }
- return false;
- }
|