14514
|
1 ## Copyright (C) 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} tetramesh (@var{T}, @var{X}) |
|
21 ## @deftypefnx {Function File} tetramesh (@var{T}, @var{X}, @var{C}) |
|
22 ## @deftypefnx {Function File} {[@var{h}] =} tetramesh (...) |
|
23 ## @deftypefnx {Function File} {[@var{h}] =} tetramesh (..., @var{PROP}, @var{VAL}) |
|
24 ## |
|
25 ## The function displays 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 |
|
27 ## 3D set of points. |
|
28 ## Every row of @var{T} contains four indices into the n by 3 matrix @var{X} |
|
29 ## of the vertices of a tetrahedron. |
|
30 ## Every row in @var{X} represents one point in 3D space. |
|
31 ## |
|
32 ## If the vector @var{C} is supplied it must contain indices into the current |
|
33 ## colormap. Called without @var{C} it is set to 1:m, where m is the number of |
|
34 ## tetrahedrons, the indices are scaled to map to the full range of the colormap. |
|
35 ## If more tetrahedrons than entries in the colormap are given the entries of |
|
36 ## @var{C} are cyclic repeated. |
|
37 ## |
|
38 ## When called with one output argument @var{H} it returns a vector of patch |
|
39 ## handles,each representing one tetrahedron in the order given by @var{T}. |
|
40 ## One use case for @var{H} is to turn the respective patch 'Visible' property |
|
41 ## 'on' or 'off'. |
|
42 ## |
|
43 ## Calling tetramesh(...,'param','value','param','value'...) passes all |
|
44 ## option/value pairs directly as additional arguments to the patch function for |
|
45 ## every tetrahedron. |
|
46 ## |
|
47 ## The command |
|
48 ## |
|
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 |
|
59 |
|
60 ## Author: Martin Helm <martin@mhelm.de> |
|
61 |
|
62 function [h] = tetramesh (varargin) |
|
63 |
|
64 [reg, prop] = parseparams (varargin); |
|
65 |
|
66 if (length (reg) < 2 || length (reg) > 3) |
|
67 print_usage () |
|
68 endif |
|
69 |
|
70 T = reg{1}; |
|
71 X = reg{2}; |
|
72 |
|
73 if (! ismatrix (T) || size (T, 2) != 4) |
|
74 error ("tetramesh: T must be a n by 4 matrix") |
|
75 endif |
|
76 if (! ismatrix (X) || size (X, 2) != 3) |
|
77 error ("tetramesh: X must be a n by 3 matrix") |
|
78 endif |
|
79 |
|
80 size_T = size (T, 1); |
|
81 colmap = colormap (); |
|
82 |
|
83 # do we need to enable gnuplot workaround? |
|
84 shrink = strcmp (graphics_toolkit (), "gnuplot"); |
|
85 |
|
86 if (length (reg) < 3) |
|
87 size_colmap = size (colmap, 1); |
|
88 C = mod ((1:size_T)' - 1, size_colmap) + 1; |
|
89 if (size_T < size_colmap && size_T > 1) |
|
90 # expand to the available range of colors |
|
91 C = floor ((C - 1) * (size_colmap - 1) / (size_T - 1)) + 1; |
|
92 endif |
|
93 else |
|
94 C = reg{3}; |
|
95 if (! isvector (C) || size_T != length (C)) |
|
96 error ("tetramesh: C must be a vector of the same length as T") |
|
97 endif |
|
98 endif |
|
99 |
|
100 h = zeros (1, size_T); |
|
101 if (shrink) |
|
102 # tiny reduction of the tetrahedron size to help gnuplot by |
|
103 # avoiding identical faces with different colors |
|
104 for ii = 1:size_T |
|
105 [th, p] = __shrink__ ([1 2 3 4], X(T(ii, :), :), 1 - 1e-7); |
|
106 h(ii) = patch ("Faces", th, "Vertices", p, "FaceColor", ... |
|
107 colmap(C(ii), :), prop{:}); |
|
108 endfor |
|
109 else |
|
110 for ii = 1:size_T |
|
111 th = [1 2 3; 2 3 4; 3 4 1; 4 1 2]; |
|
112 h(ii) = patch ("Faces", th, "Vertices", X(T(ii, :), :), "FaceColor", ... |
|
113 colmap(C(ii), :), prop{:}); |
|
114 endfor |
|
115 endif |
|
116 |
|
117 if (nargout == 0) #return nothing |
|
118 clear h; |
|
119 endif |
|
120 endfunction |
|
121 |
|
122 ## shrink the tetrahedron relative to its center of gravity |
|
123 function [tri, p] = __shrink__ (T, X, sf) |
|
124 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], :)]; |
|
126 p = sf * (p - midpoint) + midpoint; |
|
127 tri = reshape (1:12, 3, 4)'; |
|
128 endfunction |
|
129 |
|
130 %!demo |
|
131 %! d = [-1 1]; |
|
132 %! [x,y,z] = meshgrid (d, d, d); |
|
133 %! x = [x(:); 0]; |
|
134 %! y = [y(:); 0]; |
|
135 %! z = [z(:); 0]; |
|
136 %! tetra = delaunay3 (x, y, z); |
|
137 %! X = [x(:) y(:) z(:)]; |
|
138 %! clf () |
|
139 %! colormap (jet (64)) |
|
140 %! h = tetramesh (tetra, X); |
|
141 %! for ii=1:2:length(h); |
|
142 %! set(h(ii), "Visible", "off"); |
|
143 %! endfor |
|
144 %! axis equal |
|
145 %! view (30, 20) |
|
146 %! title ("Using jet (64), every other tetrahedron invisible") |
|
147 |
|
148 %!demo |
|
149 %! d = [-1 1]; |
|
150 %! [x,y,z] = meshgrid (d, d, d); |
|
151 %! x = [x(:); 0]; |
|
152 %! y = [y(:); 0]; |
|
153 %! z = [z(:); 0]; |
|
154 %! tetra = delaunay3 (x, y, z); |
|
155 %! X = [x(:) y(:) z(:)]; |
|
156 %! clf () |
|
157 %! colormap (gray (256)); |
|
158 %! tetramesh (tetra, X, 21:20:241, "EdgeColor", "w") |
|
159 %! axis equal |
|
160 %! view (30, 20) |
|
161 %! title ("Using gray (256) and white edges") |