Mercurial > hg > octave-image
annotate src/connectivity.cc @ 906:475a5a2a08cb
Fix make rules and handling of "shared" libraries.
* Makefile: we wanted to have shared libraries for strel.so and
connectivity.so but that's not really possible (Octave's pkg does not
have a system to handle this). So we create object files and statically
link them. This the closest we can do at the moment. Also change setting
CXXFLAGS to only add '-std=c++0x' rather than defining all the other options
again.
* conndef.h, connectivity.h: renamed the first as the later.
* conndef.cc: moved the definition of the connectivity class into its own
connectivity.cc file and include that. The idea is to then create a shared
library without Fconndef and Fiptcheckconn but that did not reallt happen yet.
* connectivity.cc: definition of the connectivity class, from conndef.cc.
* bwlabeln.cc: change the include for the name connectivity header file.
* COPYING: update license for the new files.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Thu, 30 Oct 2014 21:34:55 +0000 |
parents | src/conndef.cc@f34897bc944f |
children | 5e256614c376 |
rev | line source |
---|---|
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
1 // Copyright (C) 2014 Carnë Draug <carandraug@octave.org> |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
2 // |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
3 // This program is free software; you can redistribute it and/or |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
4 // modify it under the terms of the GNU General Public License as |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
5 // published by the Free Software Foundation; either version 3 of the |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
6 // License, or (at your option) any later version. |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
7 // |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
8 // This program is distributed in the hope that it will be useful, but |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
9 // WITHOUT ANY WARRANTY; without even the implied warranty of |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
11 // General Public License for more details. |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
12 // |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
13 // You should have received a copy of the GNU General Public License |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
14 // along with this program; if not, see |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
15 // <http://www.gnu.org/licenses/>. |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
16 |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
17 #include <octave/oct.h> |
906
475a5a2a08cb
Fix make rules and handling of "shared" libraries.
Carnë Draug <carandraug@octave.org>
parents:
893
diff
changeset
|
18 #include "connectivity.h" |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
19 |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
20 using namespace octave::image; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
21 |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
22 connectivity::connectivity () |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
23 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
24 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
25 |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
26 connectivity::connectivity (const octave_value& val) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
27 { |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
28 try |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
29 { |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
30 const double conn = double_value (val); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
31 if (error_state) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
32 throw invalid_connectivity ("must be in [4 6 8 18 26]"); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
33 ctor (conn); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
34 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
35 catch (invalid_connectivity& e) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
36 { |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
37 const boolNDArray mask = bool_array_value (val); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
38 if (error_state) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
39 throw invalid_connectivity ("must be logical or in [4 6 8 18 26]"); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
40 ctor (mask); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
41 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
42 return; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
43 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
44 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
45 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
46 connectivity::connectivity (const boolNDArray& mask) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
47 { |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
48 ctor (mask); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
49 return; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
50 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
51 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
52 void |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
53 connectivity::ctor (const boolNDArray& mask) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
54 { |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
55 // Must be 1x1, 3x1, or 3x3x3x...x3 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
56 const octave_idx_type numel = mask.numel (); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
57 const octave_idx_type ndims = mask.ndims (); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
58 const dim_vector dims = mask.dims (); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
59 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
60 if (ndims == 2) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
61 { |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
62 // Don't forget 1x1, and 3x1 which are valid but arrays always |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
63 // have at least 2d |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
64 if ( (dims(1) != 3 && dims(2) != 3) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
65 && (dims(1) != 3 && dims(2) != 1) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
66 && (dims(1) != 1 && dims(2) != 1)) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
67 throw invalid_connectivity ("is not 1x1, 3x1, 3x3, or 3x3x...x3"); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
68 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
69 else |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
70 { |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
71 for (octave_idx_type i = 0; i < ndims; i++) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
72 if (dims(i) != 3) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
73 throw invalid_connectivity ("is not 3x3x...x3"); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
74 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
75 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
76 // Center must be true |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
77 const octave_idx_type center = floor (numel /2); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
78 if (! mask(center)) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
79 throw invalid_connectivity ("center is not true"); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
80 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
81 // Must be symmetric relative to its center |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
82 const bool* start = mask.fortran_vec (); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
83 const bool* end = mask.fortran_vec () + (numel -1); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
84 for (octave_idx_type i = 0; i < center; i++) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
85 if (start[i] != end[-i]) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
86 throw invalid_connectivity ("is not symmetric relative to its center"); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
87 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
88 this->mask = mask; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
89 return; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
90 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
91 |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
92 connectivity::connectivity (const octave_idx_type& conn) |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
93 { |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
94 ctor (conn); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
95 return; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
96 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
97 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
98 void |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
99 connectivity::ctor (const octave_idx_type& conn) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
100 { |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
101 if (conn == 4) |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
102 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
103 mask = boolNDArray (dim_vector (3, 3), true); |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
104 bool* md = mask.fortran_vec (); |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
105 md[ 0] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
106 md[ 2] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
107 md[ 6] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
108 md[ 8] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
109 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
110 else if (conn == 6) |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
111 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
112 mask = boolNDArray (dim_vector (3, 3, 3), false); |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
113 bool* md = mask.fortran_vec (); |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
114 md[ 4] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
115 md[10] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
116 md[12] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
117 md[13] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
118 md[14] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
119 md[16] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
120 md[22] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
121 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
122 else if (conn == 8) |
891
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
123 mask = boolNDArray (dim_vector (3, 3), true); |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
124 else if (conn == 18) |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
125 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
126 mask = boolNDArray (dim_vector (3, 3, 3), true); |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
127 bool* md = mask.fortran_vec (); |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
128 md[ 0] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
129 md[ 2] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
130 md[ 6] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
131 md[ 8] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
132 md[18] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
133 md[20] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
134 md[24] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
135 md[26] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
136 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
137 else if (conn == 26) |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
138 mask = boolNDArray (dim_vector (3, 3, 3), true); |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
139 else |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
140 throw invalid_connectivity ("must be in the set [4 6 8 18 26]"); |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
141 |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
142 return; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
143 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
144 |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
145 |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
146 connectivity::connectivity (const octave_idx_type& ndims, |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
147 const std::string& type) |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
148 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
149 dim_vector size; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
150 if (ndims == 1) |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
151 size = dim_vector (3, 1); |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
152 else |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
153 { |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
154 size = dim_vector (3, 3); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
155 size.resize (ndims, 3); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
156 } |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
157 |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
158 if (type == "maximal") |
891
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
159 { |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
160 mask = boolNDArray (size, true); |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
161 } |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
162 else if (type == "minimal") |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
163 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
164 mask = boolNDArray (size, false); |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
165 bool* md = mask.fortran_vec (); |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
166 |
892
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
167 md += int (floor (pow (3, ndims) /2)); // move to center |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
168 md[0] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
169 for (octave_idx_type dim = 0; dim < ndims; dim++) |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
170 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
171 const octave_idx_type stride = pow (3, dim); |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
172 md[ stride] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
173 md[-stride] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
174 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
175 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
176 else |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
177 throw invalid_connectivity ("must be \"maximal\" or \"minimal\""); |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
178 |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
179 return; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
180 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
181 |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
182 |
891
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
183 Array<octave_idx_type> |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
184 connectivity::offsets (const dim_vector& size) const |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
185 { |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
186 const octave_idx_type nnz = mask.nnz (); |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
187 const octave_idx_type ndims = mask.ndims (); |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
188 const dim_vector dims = mask.dims (); |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
189 |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
190 Array<octave_idx_type> offsets (dim_vector (nnz, 1)); // retval |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
191 const dim_vector cum_size = size.cumulative (); |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
192 |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
193 Array<octave_idx_type> diff (dim_vector (ndims, 1)); |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
194 |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
195 Array<octave_idx_type> sub (dim_vector (ndims, 1), 0); |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
196 for (octave_idx_type ind = 0, found = 0; found < nnz; |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
197 ind++, boolNDArray::increment_index (sub, dims)) |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
198 { |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
199 if (mask(ind)) |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
200 { |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
201 for (octave_idx_type i = 0; i < ndims; i++) |
892
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
202 diff(i) = 1 - sub(i); // 1 is center since conn is 3x3x...x3 |
891
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
203 |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
204 octave_idx_type off = diff(0); |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
205 for (octave_idx_type dim = 1; dim < ndims; dim++) |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
206 off += (diff(dim) * cum_size(dim-1)); |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
207 |
891
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
208 offsets(found) = off; |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
209 found++; |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
210 } |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
211 } |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
212 |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
213 return offsets; |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
214 } |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
215 |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
216 |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
217 double |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
218 connectivity::double_value (const octave_value& val) |
892
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
219 { |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
220 error_state = 0; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
221 const double conn = val.double_value (); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
222 // Check is_scalar_type because the warning Octave:array-to-scalar |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
223 // is off by default and we will get the first element only. |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
224 if (error_state || ! val.is_scalar_type ()) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
225 error_state = 1; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
226 return conn; |
892
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
227 } |
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
228 |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
229 boolNDArray |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
230 connectivity::bool_array_value (const octave_value& val) |
892
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
231 { |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
232 error_state = 0; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
233 const boolNDArray mask = val.bool_array_value (); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
234 // bool_array_value converts anything other than 0 to true, which will |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
235 // then validate as conn array, hence any_element_not_one_or_zero() |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
236 if (val.array_value ().any_element_not_one_or_zero ()) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
237 error_state = 1; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
238 return mask; |
892
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
239 } |
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
240 |