Home > matGeom > geom2d > clipRay.m

clipRay

PURPOSE ^

CLIPRAY Clip a ray with a box.

SYNOPSIS ^

function [edge, isInside] = clipRay(ray, bb)

DESCRIPTION ^

CLIPRAY Clip a ray with a box.

   EDGE = clipRay(RAY, BOX);
   RAY is a straight ray given as a 4 element row vector: [x0 y0 dx dy],
   with (x0 y0) being the origin of the ray and (dx dy) its direction
   vector, BOX is the clipping box, given by its extreme coordinates: 
   [xmin xmax ymin ymax].
   The result is given as an edge, defined by the coordinates of its 2
   extreme points: [x1 y1 x2 y2].
   If the ray does not intersect the box, [NaN NaN NaN NaN] is returned.
   
   Function works also if RAY is a N-by-4 array, if BOX is a Nx4 array, or
   if both RAY and BOX are N-by-4 arrays. In these cases, EDGE is a N-by-4
   array.
      
   See also
     rays2d, boxes2d, edges2d, clipLine, drawRay

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [edge, isInside] = clipRay(ray, bb)
0002 %CLIPRAY Clip a ray with a box.
0003 %
0004 %   EDGE = clipRay(RAY, BOX);
0005 %   RAY is a straight ray given as a 4 element row vector: [x0 y0 dx dy],
0006 %   with (x0 y0) being the origin of the ray and (dx dy) its direction
0007 %   vector, BOX is the clipping box, given by its extreme coordinates:
0008 %   [xmin xmax ymin ymax].
0009 %   The result is given as an edge, defined by the coordinates of its 2
0010 %   extreme points: [x1 y1 x2 y2].
0011 %   If the ray does not intersect the box, [NaN NaN NaN NaN] is returned.
0012 %
0013 %   Function works also if RAY is a N-by-4 array, if BOX is a Nx4 array, or
0014 %   if both RAY and BOX are N-by-4 arrays. In these cases, EDGE is a N-by-4
0015 %   array.
0016 %
0017 %   See also
0018 %     rays2d, boxes2d, edges2d, clipLine, drawRay
0019 %
0020 
0021 % ------
0022 % Author: David Legland
0023 % E-mail: david.legland@inrae.fr
0024 % Created: 2010-05-13, using Matlab 7.4.0.287 (R2007a)
0025 % Copyright 2010-2024 INRA - Cepia Software Platform
0026 
0027 % adjust size of two input arguments
0028 if size(ray, 1) == 1
0029     ray = repmat(ray, size(bb, 1), 1);
0030 elseif size(bb, 1) == 1
0031     bb = repmat(bb, size(ray, 1), 1);
0032 elseif size(ray, 1) ~= size(bb, 1)
0033     error('bad sizes for input');
0034 end
0035 
0036 % first compute clipping of supporting line
0037 edge = clipLine(ray, bb);
0038 
0039 % detects valid edges (edges outside box are all NaN)
0040 inds = find(isfinite(edge(:, 1)));
0041 
0042 % compute position of edge extremities relative to the ray
0043 pos1 = linePosition(edge(inds,1:2), ray(inds,:), 'diag');
0044 pos2 = linePosition(edge(inds,3:4), ray(inds,:), 'diag');
0045 
0046 % if first point is before ray origin, replace by origin
0047 edge(inds(pos1 < 0), 1:2) = ray(inds(pos1 < 0), 1:2);
0048 
0049 % if last point of edge is before origin, set all edge to NaN
0050 edge(inds(pos2 < 0), :) = NaN;
0051 
0052 % eventually returns result about inside or outside
0053 if nargout > 1
0054     isInside = isfinite(edge(:,1));
0055 end

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022