1 %% Copyright (c) 2011, INRA
2 %% 2010-2011, David Legland <david.legland@grignon.inra.fr>
3 %% 2011 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
5 %% All rights reserved.
6 %% (simplified BSD License)
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %% list of conditions and the following disclaimer.
14 %% 2. Redistributions in binary form must reproduce the above copyright notice,
15 %% this list of conditions and the following disclaimer in the documentation
16 %% and/or other materials provided with the distribution.
18 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 %% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 %% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 %% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 %% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 %% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 %% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 %% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 %% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 %% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 %% POSSIBILITY OF SUCH DAMAGE.
30 %% The views and conclusions contained in the software and documentation are
31 %% those of the authors and should not be interpreted as representing official
32 %% policies, either expressed or implied, of copyright holder.
35 %% @deftypefn {Function File} {[@var{edge} @var{inside}] =} clipRay (@var{ray}, @var{box})
36 %% Clip a ray with a box.
38 %% @var{ray} is a straight ray given as a 4 element row vector: [x0 y0 dx dy],
39 %% with (x0 y0) being the origin of the ray and (dx dy) its direction
40 %% vector, @var{box} is the clipping box, given by its extreme coordinates:
41 %% [xmin xmax ymin ymax].
42 %% The result is given as an edge, defined by the coordinates of its 2
43 %% extreme points: [x1 y1 x2 y2].
44 %% If the ray does not intersect the box, [NaN NaN NaN NaN] is returned.
46 %% Function works also if @var{ray} is a Nx4 array, if @var{box} is a Nx4 array, or
47 %% if both @var{ray} and @var{box} are Nx4 arrays. In these cases, @var{edge} is a Nx4
50 %% @seealso{rays2d, boxes2d, edges2d, clipLine, drawRay}
53 function [edge isInside] = clipRay(ray, bb)
55 % adjust size of two input arguments
57 ray = repmat(ray, size(bb, 1), 1);
59 bb = repmat(bb, size(ray, 1), 1);
60 elseif size(ray, 1) != size(bb, 1)
61 error('bad sizes for input');
64 % first compute clipping of supporting line
65 edge = clipLine(ray, bb);
67 % detectes valid edges (edges outside box are all NaN)
68 inds = find(isfinite(edge(:, 1)));
70 % compute position of edge extremities relative to the ray
71 pos1 = linePosition(edge(inds,1:2), ray(inds,:));
72 pos2 = linePosition(edge(inds,3:4), ray(inds,:));
74 % if first point is before ray origin, replace by origin
75 edge(inds(pos1<0), 1:2) = ray(inds(pos1<0), 1:2);
77 % if last point of edge is before origin, set all edge to NaN
78 edge(inds(pos2<0), :) = NaN;
80 % eventually returns result about inside or outside
82 isInside = isfinite(edge(:,1));
88 %! bb = [0 100 0 100];
92 %! direction = [10 0];
93 %! ray = [origin direction];
94 %! expected = [30 40 100 40];
95 %! assert (expected, clipRay(ray, bb), 1e-6);
99 %! direction = [10 0];
100 %! ray = [origin direction];
101 %! assert (sum(isnan(clipRay(ray, bb)))==4);
103 %!test % line inside, but ray outside
104 %! origin = [130 40];
105 %! direction = [10 0];
106 %! ray = [origin direction];
107 %! assert (sum(isnan(clipRay(ray, bb)))==4);
111 %! direction = [-10 0];
112 %! ray = [origin direction];
113 %! expected = [30 40 0 40];
114 %! assert (expected, clipRay(ray, bb), 1e-6);
117 %! origin = [30 140];
118 %! direction = [-10 0];
119 %! ray = [origin direction];
120 %! assert (sum(isnan(clipRay(ray, bb)))==4);
122 %!test % line inside, but ray outside
123 %! origin = [-30 40];
124 %! direction = [-10 0];
125 %! ray = [origin direction];
126 %! assert (sum(isnan(clipRay(ray, bb)))==4);
130 %! direction = [0 10];
131 %! ray = [origin direction];
132 %! expected = [30 40 30 100];
133 %! assert (expected, clipRay(ray, bb), 1e-6);
136 %! origin = [130 40];
137 %! direction = [0 10];
138 %! ray = [origin direction];
139 %! assert (sum(isnan(clipRay(ray, bb)))==4);
141 %!test % line inside, but ray outside
142 %! origin = [30 140];
143 %! direction = [0 10];
144 %! ray = [origin direction];
145 %! assert (sum(isnan(clipRay(ray, bb)))==4);
149 %! direction = [0 -10];
150 %! ray = [origin direction];
151 %! expected = [30 40 30 0];
152 %! assert (expected, clipRay(ray, bb), 1e-6);
155 %! origin = [130 40];
156 %! direction = [0 -10];
157 %! ray = [origin direction];
158 %! assert (sum(isnan(clipRay(ray, bb)))==4);
160 %!test % line inside, but ray outside
161 %! origin = [30 -40];
162 %! direction = [0 -10];
163 %! ray = [origin direction];
164 %! assert (sum(isnan(clipRay(ray, bb)))==4);
167 %! origins = [30 40;30 40;30 140;130 40];
168 %! directions = [10 0;0 10;10 0;0 10];
169 %! rays = [origins directions];
170 %! expected = [30 40 100 40;30 40 30 100;NaN NaN NaN NaN;NaN NaN NaN NaN];
171 %! clipped = clipRay(rays, bb);
172 %! assert (expected, clipped, 1e-6);