Mercurial > hg > octave-nkf
annotate scripts/plot/tetramesh.m @ 16800:56102c33122d
Fix problems with changeset 286904321282.
legend.m: Do not delete invisible legend text object. Fix typo.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Sat, 22 Jun 2013 13:38:03 +0800 |
parents | c2dbdeaa25df |
children | ddac88d32d6a |
rev | line source |
---|---|
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 -*- | |
14623
fd3acd55dd42
tetramesh.m: Fix unbalanced parentheses in @deftypefn macro
Rik <octave@nomad.inbox5.com>
parents:
14541
diff
changeset
|
20 ## @deftypefn {Function File} {} tetramesh (@var{T}, @var{X}) |
fd3acd55dd42
tetramesh.m: Fix unbalanced parentheses in @deftypefn macro
Rik <octave@nomad.inbox5.com>
parents:
14541
diff
changeset
|
21 ## @deftypefnx {Function File} {} tetramesh (@var{T}, @var{X}, @var{C}) |
fd3acd55dd42
tetramesh.m: Fix unbalanced parentheses in @deftypefn macro
Rik <octave@nomad.inbox5.com>
parents:
14541
diff
changeset
|
22 ## @deftypefnx {Function File} {} tetramesh (@dots{}, @var{property}, @var{val}, @dots{}) |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
23 ## @deftypefnx {Function File} {@var{h} =} tetramesh (@dots{}) |
14514 | 24 ## |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
25 ## Display the tetrahedrons defined in the m-by-4 matrix @var{T} |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
26 ## as 3-D patches. @var{T} is typically the output of a Delaunay triangulation |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
27 ## of a 3-D set of points. Every row of @var{T} contains four indices into |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
28 ## the n-by-3 matrix @var{X} of the vertices of a tetrahedron. Every row in |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
29 ## @var{X} represents one point in 3-D space. |
14514 | 30 ## |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
31 ## The vector @var{C} specifies the color of each tetrahedron as an index |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
32 ## into the current colormap. The default value is 1:m where m is the number |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
33 ## of tetrahedrons; the indices are scaled to map to the full range of the |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
34 ## colormap. If there are more tetrahedrons than colors in the colormap then |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
35 ## the values in @var{C} are cyclically repeated. |
14514 | 36 ## |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
37 ## Calling @code{tetramesh (@dots{}, "property", "value", @dots{})} passes all |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
38 ## property/value pairs directly to the patch function as additional arguments. |
14514 | 39 ## |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
40 ## The optional return value @var{h} is a vector of patch handles where each |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
41 ## handle represents one tetrahedron in the order given by @var{T}. |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
42 ## A typical use case for @var{h} is to turn the respective patch "visible" |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
43 ## property "on" or "off". |
14514 | 44 ## |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
45 ## Type @code{demo tetramesh} to see examples on using @code{tetramesh}. |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
46 ## @seealso{delaunay3, delaunayn, trimesh, patch} |
14514 | 47 ## @end deftypefn |
48 | |
49 ## Author: Martin Helm <martin@mhelm.de> | |
50 | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
51 function h = tetramesh (varargin) |
14514 | 52 |
53 [reg, prop] = parseparams (varargin); | |
54 | |
55 if (length (reg) < 2 || length (reg) > 3) | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
56 print_usage (); |
14514 | 57 endif |
58 | |
59 T = reg{1}; | |
60 X = reg{2}; | |
61 | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
62 if (! ismatrix (T) || columns (T) != 4) |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
63 error ("tetramesh: T must be a n-by-4 matrix"); |
14514 | 64 endif |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
65 if (! ismatrix (X) || columns (X) != 3) |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
66 error ("tetramesh: X must be a n-by-3 matrix"); |
14514 | 67 endif |
68 | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
69 size_T = rows (T); |
14514 | 70 colmap = colormap (); |
71 | |
72 if (length (reg) < 3) | |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14623
diff
changeset
|
73 size_colmap = rows (colmap); |
14514 | 74 C = mod ((1:size_T)' - 1, size_colmap) + 1; |
75 if (size_T < size_colmap && size_T > 1) | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
76 ## expand to the available range of colors |
14514 | 77 C = floor ((C - 1) * (size_colmap - 1) / (size_T - 1)) + 1; |
78 endif | |
79 else | |
80 C = reg{3}; | |
81 if (! isvector (C) || size_T != length (C)) | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
82 error ("tetramesh: C must be a vector of the same length as T"); |
14514 | 83 endif |
84 endif | |
85 | |
86 h = zeros (1, size_T); | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
87 if (strcmp (graphics_toolkit (), "gnuplot")) |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
88 ## tiny reduction of the tetrahedron size to help gnuplot by |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
89 ## avoiding identical faces with different colors |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
90 for i = 1:size_T |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
91 [th, p] = __shrink__ ([1 2 3 4], X(T(i, :), :), 1 - 1e-7); |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
92 hvec(i) = patch ("Faces", th, "Vertices", p, |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
93 "FaceColor", colmap(C(i), :), prop{:}); |
14514 | 94 endfor |
95 else | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
96 for i = 1:size_T |
14514 | 97 th = [1 2 3; 2 3 4; 3 4 1; 4 1 2]; |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
98 hvec(i) = patch ("Faces", th, "Vertices", X(T(i, :), :), |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
99 "FaceColor", colmap(C(i), :), prop{:}); |
14514 | 100 endfor |
101 endif | |
102 | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
103 if (nargout > 0) |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
104 h = hvec; |
14514 | 105 endif |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
106 |
14514 | 107 endfunction |
108 | |
109 ## shrink the tetrahedron relative to its center of gravity | |
110 function [tri, p] = __shrink__ (T, X, sf) | |
111 midpoint = repmat (sum (X(T, :), 1) / 4, 12, 1); | |
112 p = [X([1 2 3], :); X([2 3 4], :); X([3 4 1], :); X([4 1 2], :)]; | |
113 p = sf * (p - midpoint) + midpoint; | |
114 tri = reshape (1:12, 3, 4)'; | |
115 endfunction | |
116 | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
117 |
14514 | 118 %!demo |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
119 %! clf; |
14514 | 120 %! d = [-1 1]; |
121 %! [x,y,z] = meshgrid (d, d, d); | |
122 %! x = [x(:); 0]; | |
123 %! y = [y(:); 0]; | |
124 %! z = [z(:); 0]; | |
125 %! tetra = delaunay3 (x, y, z); | |
126 %! X = [x(:) y(:) z(:)]; | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
127 %! colormap (jet (64)); |
14514 | 128 %! h = tetramesh (tetra, X); |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
129 %! set (h(1:2:end), "Visible", "off"); |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
130 %! axis equal; |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
131 %! view (30, 20); |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
132 %! title ("Using jet (64), every other tetrahedron invisible"); |
14514 | 133 |
134 %!demo | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
135 %! clf; |
14514 | 136 %! d = [-1 1]; |
137 %! [x,y,z] = meshgrid (d, d, d); | |
138 %! x = [x(:); 0]; | |
139 %! y = [y(:); 0]; | |
140 %! z = [z(:); 0]; | |
141 %! tetra = delaunay3 (x, y, z); | |
142 %! X = [x(:) y(:) z(:)]; | |
143 %! colormap (gray (256)); | |
14541
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
144 %! tetramesh (tetra, X, 21:20:241, "EdgeColor", "w"); |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
145 %! axis equal; |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
146 %! view (30, 20); |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
147 %! title ("Using gray (256) and white edges"); |
759944521fd6
Improve tetramesh docstring and add function to manual.
Rik <octave@nomad.inbox5.com>
parents:
14514
diff
changeset
|
148 |