Home > matGeom > geom3d > clipLine3d.m

clipLine3d

PURPOSE ^

CLIPLINE3D Clip a line with a box and return an edge.

SYNOPSIS ^

function edge = clipLine3d(line, box)

DESCRIPTION ^

CLIPLINE3D Clip a line with a box and return an edge.

   EDGE = clipLine3d(LINE, BOX);
   Clips the line LINE with the bounds given in BOX, and returns the
   corresponding edge. 

   If the line lies totally outside of the box, returns a 1-by-6 row array
   containing only NaN's.

   If LINE is a N-by-6 array, with one line by row, returns the clipped
   edge coresponding to each line in a N-by-6 array.

   See also
   lines3d, edges3d, createLine3d, clipRay3d

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function edge = clipLine3d(line, box)
0002 %CLIPLINE3D Clip a line with a box and return an edge.
0003 %
0004 %   EDGE = clipLine3d(LINE, BOX);
0005 %   Clips the line LINE with the bounds given in BOX, and returns the
0006 %   corresponding edge.
0007 %
0008 %   If the line lies totally outside of the box, returns a 1-by-6 row array
0009 %   containing only NaN's.
0010 %
0011 %   If LINE is a N-by-6 array, with one line by row, returns the clipped
0012 %   edge coresponding to each line in a N-by-6 array.
0013 %
0014 %   See also
0015 %   lines3d, edges3d, createLine3d, clipRay3d
0016 %
0017 
0018 % ------
0019 % Author: David Legland
0020 % E-mail: david.legland@inrae.fr
0021 % Created: 2008-10-30, from drawLine3d
0022 % Copyright 2008-2024 INRA - TPV URPOI - BIA IMASTE
0023 
0024 % get box limits
0025 xmin = box(1); xmax = box(2);
0026 ymin = box(3); ymax = box(4);
0027 zmin = box(5); zmax = box(6);
0028 
0029 % extreme corners of the box
0030 p000 = [xmin ymin zmin];
0031 p111 = [xmax ymax zmax];
0032 
0033 % main vectors
0034 ex   = [1 0 0];
0035 ey   = [0 1 0];
0036 ez   = [0 0 1];
0037 
0038 % box faces parallel to Oxy
0039 planeZ0 = [p000 ex ey];
0040 planeZ1 = [p111 ex ey];
0041 
0042 % box faces parallel to Oxz
0043 planeY0 = [p000 ex ez];
0044 planeY1 = [p111 ex ez];
0045 
0046 % box faces parallel to Oyz
0047 planeX0 = [p000 ey ez];
0048 planeX1 = [p111 ey ez];
0049 
0050 % number of lines
0051 nLines = size(line, 1);
0052 
0053 % allocate memory for result
0054 edge = zeros(nLines, 6);
0055 
0056 % iterate over lines to clip
0057 for i = 1:nLines
0058     
0059     % compute intersection point with each plane
0060     ipZ0 = intersectLinePlane(line(i,:), planeZ0);
0061     ipZ1 = intersectLinePlane(line(i,:), planeZ1);
0062     ipY0 = intersectLinePlane(line(i,:), planeY0);
0063     ipY1 = intersectLinePlane(line(i,:), planeY1);
0064     ipX1 = intersectLinePlane(line(i,:), planeX1);
0065     ipX0 = intersectLinePlane(line(i,:), planeX0);
0066 
0067     % concatenate resulting points
0068     points  = [ipX0;ipX1;ipY0;ipY1;ipZ0;ipZ1];
0069 
0070     % compute position of each point on the line
0071     pos     = line3dPosition(points, line(i,:));
0072 
0073     % keep only defined points
0074     ind     = find(~isnan(pos));
0075     pos     = pos(ind);
0076     points  = points(ind,:);
0077 
0078     % sort points with respect to their position
0079     [pos, ind] = sort(pos); %#ok<ASGLU>
0080     points  = points(ind, :);
0081 
0082     % keep median points wrt to position. These points define the limit of
0083     % the clipped edge.
0084     nv      = length(ind)/2;
0085 
0086     % create resulting edge.
0087     edge(i,:)   = [points(nv, :) points(nv+1, :)];
0088 end
0089 
0090 % check that middle point of the edge is contained in the box
0091 midX = mean(edge(:, [1 4]), 2);
0092 xOk  = xmin <= midX & midX <= xmax;
0093 midY = mean(edge(:, [2 5]), 2);
0094 yOk  = ymin <= midY & midY <= ymax;
0095 midZ = mean(edge(:, [3 6]), 2);
0096 zOk  = zmin <= midZ & midZ <= zmax;
0097 
0098 % if one of the bounding condition is not met, set edge to NaN
0099 edge (~(xOk & yOk & zOk), :) = NaN;

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