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 h = drawLine3d(lin, varargin)
29%DRAWLINE3D Draw a 3D line clipped by the current axes.
30%
31%   drawLine3d(LINE) draws the line LINE on the current axis, by clipping
32%   with the current axis.
33%
34%   drawLine3d(LINE, PARAM, VALUE) accepts parameter/value pairs, like
35%   for plot function. Color of the line can also be given as a single
36%   parameter.
37%
38%   drawLine3d(AX,...) plots into AX instead of GCA.
39%
40%   H = drawLine3d(...) returns a handle to the created line object.
41%   If the line is not clipped by the axis, function returns -1.
42%
43%   Example
44%     % draw a sphere together with the three main axes
45%     figure; hold on;
46%     drawSphere([40 30 20 30]);
47%     view(3); axis equal; axis([-20 80 -20 80 -20 80])
48%     drawLine3d([0 0 0   1 0 0], 'k');
49%     drawLine3d([0 0 0   0 1 0], 'k');
50%     drawLine3d([0 0 0   0 0 1], 'k');
51%     light;
52%
53%
54%   See also:
55%     lines3d, createLine3d, clipLine3d, drawRay3d, drawEdge3d
56%
57
58% ---------
59% author : David Legland
60% INRA - TPV URPOI - BIA IMASTE
61% created the 17/02/2005.
62
63
64% Parse and check inputs
65isLine3d = @(x) validateattributes(x,{'numeric'},...
66    {'nonempty','nonnan','real','finite','size',[nan,6]});
67defOpts.Color = 'b';
68[hAx, lin, varargin] = ...
69    parseDrawInput(lin, isLine3d, 'line', defOpts, varargin{:});
70
71% extract limits of the bounding box
72box = [get(hAx, 'xlim') get(hAx, 'ylim') get(hAx, 'zlim')];
73
74% clip the line with the limits of the current axis
75edge = clipLine3d(lin, box);
76
77% draw the clipped line
78if sum(isnan(edge)) == 0
79    hh = drawEdge3d(hAx, edge);
80    if ~isempty(varargin)
81        set(hh, varargin{:});
82    end
83else
84    hh = [];
85end
86
87% process output
88if nargout > 0
89    h = hh;
90end
91