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