Mercurial > hg > octave-lyh
comparison scripts/plot/tetramesh.m @ 14541:759944521fd6
Improve tetramesh docstring and add function to manual.
* geometry.txi: Add tetramesh to manual.
* delaunay.m, delaunay3.m, delaunayn.m: Update seealso cross-references.
* tetramesh.m: Update docstring. Use Octave coding conventions.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 09 Apr 2012 15:34:57 -0700 |
parents | 34a9fdaa6ea3 |
children | fd3acd55dd42 |
comparison
equal
deleted
inserted
replaced
14540:ac8520c03fc9 | 14541:759944521fd6 |
---|---|
17 ## <http://www.gnu.org/licenses/>. | 17 ## <http://www.gnu.org/licenses/>. |
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} tetramesh (@var{T}, @var{X}) | 20 ## @deftypefn {Function File} tetramesh (@var{T}, @var{X}) |
21 ## @deftypefnx {Function File} tetramesh (@var{T}, @var{X}, @var{C}) | 21 ## @deftypefnx {Function File} tetramesh (@var{T}, @var{X}, @var{C}) |
22 ## @deftypefnx {Function File} {[@var{h}] =} tetramesh (...) | 22 ## @deftypefnx {Function File} tetramesh (@dots{}, @var{property}, @var{val}, @dots{}) |
23 ## @deftypefnx {Function File} {[@var{h}] =} tetramesh (..., @var{PROP}, @var{VAL}) | 23 ## @deftypefnx {Function File} {@var{h} =} tetramesh (@dots{}) |
24 ## | 24 ## |
25 ## The function displays the tetrahedrons defined in the m by 4 matrix @var{T} | 25 ## Display the tetrahedrons defined in the m-by-4 matrix @var{T} |
26 ## as 3D patches. @var{T} is usually the output of a Delaunay triangulation of a | 26 ## as 3-D patches. @var{T} is typically the output of a Delaunay triangulation |
27 ## 3D set of points. | 27 ## of a 3-D set of points. Every row of @var{T} contains four indices into |
28 ## Every row of @var{T} contains four indices into the n by 3 matrix @var{X} | 28 ## the n-by-3 matrix @var{X} of the vertices of a tetrahedron. Every row in |
29 ## of the vertices of a tetrahedron. | 29 ## @var{X} represents one point in 3-D space. |
30 ## Every row in @var{X} represents one point in 3D space. | |
31 ## | 30 ## |
32 ## If the vector @var{C} is supplied it must contain indices into the current | 31 ## The vector @var{C} specifies the color of each tetrahedron as an index |
33 ## colormap. Called without @var{C} it is set to 1:m, where m is the number of | 32 ## into the current colormap. The default value is 1:m where m is the number |
34 ## tetrahedrons, the indices are scaled to map to the full range of the colormap. | 33 ## of tetrahedrons; the indices are scaled to map to the full range of the |
35 ## If more tetrahedrons than entries in the colormap are given the entries of | 34 ## colormap. If there are more tetrahedrons than colors in the colormap then |
36 ## @var{C} are cyclic repeated. | 35 ## the values in @var{C} are cyclically repeated. |
37 ## | 36 ## |
38 ## When called with one output argument @var{H} it returns a vector of patch | 37 ## Calling @code{tetramesh (@dots{}, "property", "value", @dots{})} passes all |
39 ## handles,each representing one tetrahedron in the order given by @var{T}. | 38 ## property/value pairs directly to the patch function as additional arguments. |
40 ## One use case for @var{H} is to turn the respective patch 'Visible' property | |
41 ## 'on' or 'off'. | |
42 ## | 39 ## |
43 ## Calling tetramesh(...,'param','value','param','value'...) passes all | 40 ## The optional return value @var{h} is a vector of patch handles where each |
44 ## option/value pairs directly as additional arguments to the patch function for | 41 ## handle represents one tetrahedron in the order given by @var{T}. |
45 ## every tetrahedron. | 42 ## A typical use case for @var{h} is to turn the respective patch "visible" |
43 ## property "on" or "off". | |
46 ## | 44 ## |
47 ## The command | 45 ## Type @code{demo tetramesh} to see examples on using @code{tetramesh}. |
48 ## | 46 ## @seealso{delaunay3, delaunayn, trimesh, patch} |
49 ##@example | |
50 ## @group | |
51 ## demo tetramesh | |
52 ## @end group | |
53 ## @end example | |
54 ## | |
55 ## @noindent | |
56 ## will show some examples how to use it. | |
57 #### @seealso{patch} | |
58 ## @end deftypefn | 47 ## @end deftypefn |
59 | 48 |
60 ## Author: Martin Helm <martin@mhelm.de> | 49 ## Author: Martin Helm <martin@mhelm.de> |
61 | 50 |
62 function [h] = tetramesh (varargin) | 51 function h = tetramesh (varargin) |
63 | 52 |
64 [reg, prop] = parseparams (varargin); | 53 [reg, prop] = parseparams (varargin); |
65 | 54 |
66 if (length (reg) < 2 || length (reg) > 3) | 55 if (length (reg) < 2 || length (reg) > 3) |
67 print_usage () | 56 print_usage (); |
68 endif | 57 endif |
69 | 58 |
70 T = reg{1}; | 59 T = reg{1}; |
71 X = reg{2}; | 60 X = reg{2}; |
72 | 61 |
73 if (! ismatrix (T) || size (T, 2) != 4) | 62 if (! ismatrix (T) || columns (T) != 4) |
74 error ("tetramesh: T must be a n by 4 matrix") | 63 error ("tetramesh: T must be a n-by-4 matrix"); |
75 endif | 64 endif |
76 if (! ismatrix (X) || size (X, 2) != 3) | 65 if (! ismatrix (X) || columns (X) != 3) |
77 error ("tetramesh: X must be a n by 3 matrix") | 66 error ("tetramesh: X must be a n-by-3 matrix"); |
78 endif | 67 endif |
79 | 68 |
80 size_T = size (T, 1); | 69 size_T = rows (T); |
81 colmap = colormap (); | 70 colmap = colormap (); |
82 | 71 |
83 # do we need to enable gnuplot workaround? | |
84 shrink = strcmp (graphics_toolkit (), "gnuplot"); | |
85 | |
86 if (length (reg) < 3) | 72 if (length (reg) < 3) |
87 size_colmap = size (colmap, 1); | 73 size_colmap = size (colmap, 1); |
88 C = mod ((1:size_T)' - 1, size_colmap) + 1; | 74 C = mod ((1:size_T)' - 1, size_colmap) + 1; |
89 if (size_T < size_colmap && size_T > 1) | 75 if (size_T < size_colmap && size_T > 1) |
90 # expand to the available range of colors | 76 ## expand to the available range of colors |
91 C = floor ((C - 1) * (size_colmap - 1) / (size_T - 1)) + 1; | 77 C = floor ((C - 1) * (size_colmap - 1) / (size_T - 1)) + 1; |
92 endif | 78 endif |
93 else | 79 else |
94 C = reg{3}; | 80 C = reg{3}; |
95 if (! isvector (C) || size_T != length (C)) | 81 if (! isvector (C) || size_T != length (C)) |
96 error ("tetramesh: C must be a vector of the same length as T") | 82 error ("tetramesh: C must be a vector of the same length as T"); |
97 endif | 83 endif |
98 endif | 84 endif |
99 | 85 |
100 h = zeros (1, size_T); | 86 h = zeros (1, size_T); |
101 if (shrink) | 87 if (strcmp (graphics_toolkit (), "gnuplot")) |
102 # tiny reduction of the tetrahedron size to help gnuplot by | 88 ## tiny reduction of the tetrahedron size to help gnuplot by |
103 # avoiding identical faces with different colors | 89 ## avoiding identical faces with different colors |
104 for ii = 1:size_T | 90 for i = 1:size_T |
105 [th, p] = __shrink__ ([1 2 3 4], X(T(ii, :), :), 1 - 1e-7); | 91 [th, p] = __shrink__ ([1 2 3 4], X(T(i, :), :), 1 - 1e-7); |
106 h(ii) = patch ("Faces", th, "Vertices", p, "FaceColor", ... | 92 hvec(i) = patch ("Faces", th, "Vertices", p, |
107 colmap(C(ii), :), prop{:}); | 93 "FaceColor", colmap(C(i), :), prop{:}); |
108 endfor | 94 endfor |
109 else | 95 else |
110 for ii = 1:size_T | 96 for i = 1:size_T |
111 th = [1 2 3; 2 3 4; 3 4 1; 4 1 2]; | 97 th = [1 2 3; 2 3 4; 3 4 1; 4 1 2]; |
112 h(ii) = patch ("Faces", th, "Vertices", X(T(ii, :), :), "FaceColor", ... | 98 hvec(i) = patch ("Faces", th, "Vertices", X(T(i, :), :), |
113 colmap(C(ii), :), prop{:}); | 99 "FaceColor", colmap(C(i), :), prop{:}); |
114 endfor | 100 endfor |
115 endif | 101 endif |
116 | 102 |
117 if (nargout == 0) #return nothing | 103 if (nargout > 0) |
118 clear h; | 104 h = hvec; |
119 endif | 105 endif |
106 | |
120 endfunction | 107 endfunction |
121 | 108 |
122 ## shrink the tetrahedron relative to its center of gravity | 109 ## shrink the tetrahedron relative to its center of gravity |
123 function [tri, p] = __shrink__ (T, X, sf) | 110 function [tri, p] = __shrink__ (T, X, sf) |
124 midpoint = repmat (sum (X(T, :), 1) / 4, 12, 1); | 111 midpoint = repmat (sum (X(T, :), 1) / 4, 12, 1); |
125 p = [X([1 2 3], :); X([2 3 4], :); X([3 4 1], :); X([4 1 2], :)]; | 112 p = [X([1 2 3], :); X([2 3 4], :); X([3 4 1], :); X([4 1 2], :)]; |
126 p = sf * (p - midpoint) + midpoint; | 113 p = sf * (p - midpoint) + midpoint; |
127 tri = reshape (1:12, 3, 4)'; | 114 tri = reshape (1:12, 3, 4)'; |
128 endfunction | 115 endfunction |
129 | 116 |
117 | |
130 %!demo | 118 %!demo |
119 %! clf; | |
131 %! d = [-1 1]; | 120 %! d = [-1 1]; |
132 %! [x,y,z] = meshgrid (d, d, d); | 121 %! [x,y,z] = meshgrid (d, d, d); |
133 %! x = [x(:); 0]; | 122 %! x = [x(:); 0]; |
134 %! y = [y(:); 0]; | 123 %! y = [y(:); 0]; |
135 %! z = [z(:); 0]; | 124 %! z = [z(:); 0]; |
136 %! tetra = delaunay3 (x, y, z); | 125 %! tetra = delaunay3 (x, y, z); |
137 %! X = [x(:) y(:) z(:)]; | 126 %! X = [x(:) y(:) z(:)]; |
138 %! clf () | 127 %! colormap (jet (64)); |
139 %! colormap (jet (64)) | |
140 %! h = tetramesh (tetra, X); | 128 %! h = tetramesh (tetra, X); |
141 %! for ii=1:2:length(h); | 129 %! set (h(1:2:end), "Visible", "off"); |
142 %! set(h(ii), "Visible", "off"); | 130 %! axis equal; |
143 %! endfor | 131 %! view (30, 20); |
144 %! axis equal | 132 %! title ("Using jet (64), every other tetrahedron invisible"); |
145 %! view (30, 20) | |
146 %! title ("Using jet (64), every other tetrahedron invisible") | |
147 | 133 |
148 %!demo | 134 %!demo |
135 %! clf; | |
149 %! d = [-1 1]; | 136 %! d = [-1 1]; |
150 %! [x,y,z] = meshgrid (d, d, d); | 137 %! [x,y,z] = meshgrid (d, d, d); |
151 %! x = [x(:); 0]; | 138 %! x = [x(:); 0]; |
152 %! y = [y(:); 0]; | 139 %! y = [y(:); 0]; |
153 %! z = [z(:); 0]; | 140 %! z = [z(:); 0]; |
154 %! tetra = delaunay3 (x, y, z); | 141 %! tetra = delaunay3 (x, y, z); |
155 %! X = [x(:) y(:) z(:)]; | 142 %! X = [x(:) y(:) z(:)]; |
156 %! clf () | |
157 %! colormap (gray (256)); | 143 %! colormap (gray (256)); |
158 %! tetramesh (tetra, X, 21:20:241, "EdgeColor", "w") | 144 %! tetramesh (tetra, X, 21:20:241, "EdgeColor", "w"); |
159 %! axis equal | 145 %! axis equal; |
160 %! view (30, 20) | 146 %! view (30, 20); |
161 %! title ("Using gray (256) and white edges") | 147 %! title ("Using gray (256) and white edges"); |
148 |