]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/conndef.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / conndef.m
1 ## Copyright (C) 2004 Josep Mones i Teixidor
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 2 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/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{conn} = } conndef (@var{num_dims}, @var{type})
18 ## Creates a connectivity array.
19 ##
20 ## @code{conn=conndef(num_dims,type)} creates a connectivity array
21 ## (@var{CONN}) of @var{num_dims} dimensions and which type is defined
22 ## by @var{type} as follows:
23 ## @table @code
24 ## @item minimal
25 ## Neighbours touch the central element on a (@var{num_dims}-1)-dimensional
26 ## surface.
27 ## @item maximal
28 ## Neighbours touch the central element in any way. Equivalent to
29 ## @code{ones(repmat(3,1,@var{num_dims}))}.
30 ## @end table
31 ##
32 ## @end deftypefn
33
34
35
36 ## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
37
38 function conn = conndef(num_dims,conntype)
39   if(nargin!=2)
40     usage("conn=conndef(num_dims, type)");
41   endif
42   if(num_dims<=0)
43     error("conndef: num_dims must be > 0");
44   endif
45     
46   if(strcmp(conntype,"minimal"))
47     if(num_dims==1)
48       conn=[1;1;1];
49     elseif(num_dims==2)
50       conn=[0,1,0;1,1,1;0,1,0];
51     else
52       conn=zeros(repmat(3,1,num_dims));
53       idx={};
54       idx{1}=1:3;
55       for i=2:num_dims
56         idx{i}=2;
57       endfor
58       conn(idx{:})=1;
59       for i=2:num_dims
60         idx{i-1}=2;
61         idx{i}=1:3;
62         conn(idx{:})=1;
63       endfor
64     endif
65     
66   elseif(strcmp(conntype,"maximal"))
67     if(num_dims==1)
68       conn=[1;1;1];
69     else
70       conn=ones(repmat(3,1,num_dims));
71     endif
72   else
73     error("conndef: invalid type parameter.");
74   endif
75   
76 endfunction
77
78 %!demo
79 %! conndef(2,'minimal')
80 %! % Create a 2-D minimal connectivity array
81
82 %!assert(conndef(1,'minimal'), [1;1;1]);
83
84 %!assert(conndef(2,'minimal'), [0,1,0;1,1,1;0,1,0]);
85
86 %!test
87 %! C=zeros(3,3);
88 %! C(2,2,1)=1;
89 %! C(2,2,3)=1;
90 %! C(:,:,2)=[0,1,0;1,1,1;0,1,0];
91 %! assert(conndef(3,'minimal'), C);
92
93 %!assert(conndef(1,'maximal'), ones(3,1));
94 %!assert(conndef(2,'maximal'), ones(3,3));
95 %!assert(conndef(3,'maximal'), ones(3,3,3));
96 %!assert(conndef(4,'maximal'), ones(3,3,3,3));
97
98
99
100 % $Log$
101 % Revision 1.3  2007/03/23 16:14:36  adb014
102 % Update the FSF address
103 %
104 % Revision 1.2  2007/01/04 23:41:47  hauberg
105 % Minor changes in help text
106 %
107 % Revision 1.1  2006/08/20 12:59:32  hauberg
108 % Changed the structure to match the package system
109 %
110 % Revision 1.2  2005/07/03 01:10:19  pkienzle
111 % Try to correct for missing newline at the end of the file
112 %
113 % Revision 1.1  2004/08/15 19:38:44  jmones
114 % conndef added: Creates a connectivity array