1## Copyright (C) 2021 David Legland 2## All rights reserved. 3## 4## Redistribution and use in source and binary forms, with or without 5## modification, are permitted provided that the following conditions are met: 6## 7## 1 Redistributions of source code must retain the above copyright notice, 8## this list of conditions and the following disclaimer. 9## 2 Redistributions in binary form must reproduce the above copyright 10## notice, this list of conditions and the following disclaimer in the 11## documentation and/or other materials provided with the distribution. 12## 13## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' 14## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 17## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 20## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23## 24## The views and conclusions contained in the software and documentation are 25## those of the authors and should not be interpreted as representing official 26## policies, either expressed or implied, of the copyright holders. 27 28function clipped = clipEdge3d(edge, box) 29%CLIPEDGE3D Clip a 3D edge with a cuboid box. 30% 31% CLIPPED = clipEdge3d(EDGE, BOX) 32% 33% Example 34% clipEdge3d 35% 36% See also 37% lines3d, edges3d, clipLine3d 38 39% ------ 40% Author: David Legland 41% e-mail: david.legland@inra.fr 42% Created: 2018-04-12, using Matlab 9.3.0.713579 (R2017b) 43% Copyright 2018 INRA - Cepia Software Platform. 44 45% compute supporting line of edge 46line = [edge(:, 1:3) edge(:,4:6)-edge(:,1:3)]; 47 48% clip supporting line 49clipped = clipLine3d(line, box); 50 51% for each clipped line, check that extremities are contained in edge 52nEdges = size(edge, 1); 53for i = 1:nEdges 54 % if supporting line does not intersect the box, the edge is totally 55 % clipped. 56 if isnan(clipped(i,1)) 57 continue; 58 end 59 60 % position of intersection points on the current supporting line 61 pos1 = linePosition3d(clipped(i,1:3), line(i,:)); 62 pos2 = linePosition3d(clipped(i,4:6), line(i,:)); 63 64 if pos1 > 1 || pos2 < 0 65 % case of an edge totally clipped 66 clipped(i,:) = NaN; 67 elseif pos1 > 0 && pos2 < 1 68 % case of an edge already contained within the bounding box 69 % -> nothin to do... 70 continue; 71 else 72 % otherwise, need to adjust bounds of the clipped edge 73 pos1 = max(pos1, 0); 74 pos2 = min(pos2, 1); 75 p1 = line(i,1:3) + pos1 * line(i,4:6); 76 p2 = line(i,1:3) + pos2 * line(i,4:6); 77 clipped(i,:) = [p1 p2]; 78 end 79end 80