]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/isocolors.m
update packages
[CreaPhase.git] / octave_packages / m / plot / isocolors.m
1 ## Copyright (C) 2009-2012 Martin Helm
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {[@var{cd}] =} isocolors (@var{c}, @var{v})
21 ## @deftypefnx {Function File} {[@var{cd}] =} isocolors (@var{x}, @var{y}, @var{z}, @var{c}, @var{v})
22 ## @deftypefnx {Function File} {[@var{cd}] =} isocolors (@var{x}, @var{y}, @var{z}, @var{r}, @var{g}, @var{b}, @var{v})
23 ## @deftypefnx {Function File} {[@var{cd}] =} isocolors (@var{r}, @var{g}, @var{b}, @var{v})
24 ## @deftypefnx {Function File} {[@var{cd}] =} isocolors (@dots{}, @var{p})
25 ## @deftypefnx {Function File} {} isocolors (@dots{})
26 ##
27 ## If called with one output argument and the first input argument
28 ## @var{c} is a three-dimensional array that contains color values and
29 ## the second input argument @var{v} keeps the vertices of a geometry
30 ## then return a matrix @var{cd} with color data information for the
31 ## geometry at computed points
32 ## @command{[x, y, z] = meshgrid (1:l, 1:m, 1:n)}.  The output argument
33 ## @var{cd} can be taken to manually set FaceVertexCData of a patch.
34 ##
35 ## If called with further input arguments @var{x}, @var{y} and @var{z}
36 ## which are three--dimensional arrays of the same size than @var{c}
37 ## then the color data is taken at those given points.  Instead of the
38 ## color data @var{c} this function can also be called with RGB values
39 ## @var{r}, @var{g}, @var{b}.  If input argumnets @var{x}, @var{y},
40 ## @var{z} are not given then again @command{meshgrid} computed values
41 ## are taken.
42 ##
43 ## Optionally, the patch handle @var{p} can be given as the last input
44 ## argument to all variations of function calls instead of the vertices
45 ## data @var{v}.  Finally, if no output argument is given then directly
46 ## change the colors of a patch that is given by the patch handle
47 ## @var{p}.
48 ##
49 ## For example:
50 ##
51 ## @example
52 ## function [] = isofinish (p)
53 ##   set (gca, "PlotBoxAspectRatioMode", "manual", ...
54 ##             "PlotBoxAspectRatio", [1 1 1]);
55 ##   set (p, "FaceColor", "interp");
56 ##   ## set (p, "FaceLighting", "flat");
57 ##   ## light ("Position", [1 1 5]); ## Available with JHandles
58 ## endfunction
59 ##
60 ## N = 15;    # Increase number of vertices in each direction
61 ## iso = .4;  # Change isovalue to .1 to display a sphere
62 ## lin = linspace (0, 2, N);
63 ## [x, y, z] = meshgrid (lin, lin, lin);
64 ## c = abs ((x-.5).^2 + (y-.5).^2 + (z-.5).^2);
65 ## figure (); # Open another figure window
66 ##
67 ## subplot (2,2,1); view (-38, 20);
68 ## [f, v] = isosurface (x, y, z, c, iso);
69 ## p = patch ("Faces", f, "Vertices", v, "EdgeColor", "none");
70 ## cdat = rand (size (c));       # Compute random patch color data
71 ## isocolors (x, y, z, cdat, p); # Directly set colors of patch
72 ## isofinish (p);                # Call user function isofinish
73 ##
74 ## subplot (2,2,2); view (-38, 20);
75 ## p = patch ("Faces", f, "Vertices", v, "EdgeColor", "none");
76 ## [r, g, b] = meshgrid (lin, 2-lin, 2-lin);
77 ## cdat = isocolors (x, y, z, c, v); # Compute color data vertices
78 ## set (p, "FaceVertexCData", cdat); # Set color data manually
79 ## isofinish (p);
80 ##
81 ## subplot (2,2,3); view (-38, 20);
82 ## p = patch ("Faces", f, "Vertices", v, "EdgeColor", "none");
83 ## cdat = isocolors (r, g, b, c, p); # Compute color data patch
84 ## set (p, "FaceVertexCData", cdat); # Set color data manually
85 ## isofinish (p);
86 ##
87 ## subplot (2,2,4); view (-38, 20);
88 ## p = patch ("Faces", f, "Vertices", v, "EdgeColor", "none");
89 ## r = g = b = repmat ([1:N] / N, [N, 1, N]); # Black to white
90 ## cdat = isocolors (x, y, z, r, g, b, v);
91 ## set (p, "FaceVertexCData", cdat);
92 ## isofinish (p);
93 ## @end example
94 ##
95 ## @seealso{isosurface, isonormals}
96 ##
97 ## @end deftypefn
98
99 ## Author: Martin Helm <martin@mhelm.de>
100
101 function varargout = isocolors(varargin)
102   calc_rgb = false;
103   switch (nargin)
104     case 2
105       c = varargin{1};
106       vp = varargin{2};
107       x = 1:size (c, 2);
108       y = 1:size (c, 1);
109       z = 1:size (c, 3);
110     case 4
111       calc_rgb = true;
112       R = varargin{1};
113       G = varargin{2};
114       B = varargin{3};
115       vp = varargin{4};
116       x = 1:size (R, 1);
117       y = 1:size (R, 2);
118       z = 1:size (R, 3);
119     case 5
120       x = varargin{1};
121       y = varargin{2};
122       z = varargin{3};
123       c = varargin{4};
124       vp = varargin{5};
125     case 7
126       calc_rgb = true;
127       x = varargin{1};
128       y = varargin{2};
129       z = varargin{3};
130       R = varargin{4};
131       G = varargin{5};
132       B = varargin{6};
133       vp = varargin{7};
134     otherwise
135       print_usage ();
136   endswitch
137   if (ismatrix (vp) && size (vp,2) == 3)
138     pa = [];
139     v = vp;
140   elseif ( ishandle (vp) )
141     pa = vp;
142     v = get (pa, "Vertices");
143   else
144     error ("isocolors: last argument is not a vertex list or patch handle");
145   endif
146   if (calc_rgb)
147     new_col = zeros (size (v, 1), 3);
148     new_col(:,1) = __interp_cube__ (x, y, z, R, v, "values" );
149     new_col(:,2) = __interp_cube__ (x, y, z, G, v, "values" );
150     new_col(:,3) = __interp_cube__ (x, y, z, B, v, "values" );
151   else
152     new_col = __interp_cube__ (x, y, z, c, v, "values" );
153   endif
154   switch (nargout)
155     case 0
156       if (!isempty (pa))
157         set (pa, "FaceVertexCData", new_col);
158       endif
159     case 1
160       varargout = {new_col};
161     otherwise
162       print_usage ();
163   endswitch
164 endfunction
165
166 %!test
167 %!  [x, y, z] = meshgrid (0:.5:2, 0:.5:2, 0:.5:2);
168 %!  c = (x-.5).^2 + (y-.5).^2 + (z-.5).^2;
169 %!  [f, v] = isosurface (x, y, z, c, .4);
170 %!  cdat = isocolors (x, y, z, c, v);
171 %!  assert (size (cdat, 1) == size (v, 1));
172 ## Can't create a patch handle for tests without a figure