Mercurial > hg > octave-image
annotate src/connectivity.cc @ 907:5e256614c376
Make more use of exceptions and don't set error_state ourselves.
* connectivity.cc, connectivity.h: define new invalid_conversion exception
class so we don't have to set and reset error_state ourselves. This was
causing weird bugs where some errors were printed but could not caught in the
Octave interpreter.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Thu, 30 Oct 2014 22:10:00 +0000 |
parents | 475a5a2a08cb |
children | f53cc3aaa88e |
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 |
907
5e256614c376
Make more use of exceptions and don't set error_state ourselves.
Carnë Draug <carandraug@octave.org>
parents:
906
diff
changeset
|
29 {ctor (double_value (val));} |
5e256614c376
Make more use of exceptions and don't set error_state ourselves.
Carnë Draug <carandraug@octave.org>
parents:
906
diff
changeset
|
30 catch (invalid_conversion& e) |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
31 { |
907
5e256614c376
Make more use of exceptions and don't set error_state ourselves.
Carnë Draug <carandraug@octave.org>
parents:
906
diff
changeset
|
32 try |
5e256614c376
Make more use of exceptions and don't set error_state ourselves.
Carnë Draug <carandraug@octave.org>
parents:
906
diff
changeset
|
33 {ctor (bool_array_value (val));} |
5e256614c376
Make more use of exceptions and don't set error_state ourselves.
Carnë Draug <carandraug@octave.org>
parents:
906
diff
changeset
|
34 catch (invalid_connectivity& e) |
5e256614c376
Make more use of exceptions and don't set error_state ourselves.
Carnë Draug <carandraug@octave.org>
parents:
906
diff
changeset
|
35 {throw;} // so it does not get caught by the parent invalid_conversion |
5e256614c376
Make more use of exceptions and don't set error_state ourselves.
Carnë Draug <carandraug@octave.org>
parents:
906
diff
changeset
|
36 catch (invalid_conversion& e) |
5e256614c376
Make more use of exceptions and don't set error_state ourselves.
Carnë Draug <carandraug@octave.org>
parents:
906
diff
changeset
|
37 {throw invalid_connectivity ("must be logical or in [4 6 8 18 26]");} |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
38 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
39 return; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
40 } |
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 connectivity::connectivity (const boolNDArray& mask) |
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 ctor (mask); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
45 return; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
46 } |
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 void |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
49 connectivity::ctor (const boolNDArray& mask) |
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 // 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
|
52 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
|
53 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
|
54 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
|
55 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
56 if (ndims == 2) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
57 { |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
58 // 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
|
59 // have at least 2d |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
60 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
|
61 && (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
|
62 && (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
|
63 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
|
64 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
65 else |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
66 { |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
67 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
|
68 if (dims(i) != 3) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
69 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
|
70 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
71 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
72 // Center must be true |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
73 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
|
74 if (! mask(center)) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
75 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
|
76 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
77 // 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
|
78 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
|
79 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
|
80 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
|
81 if (start[i] != end[-i]) |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
82 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
|
83 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
84 this->mask = mask; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
85 return; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
86 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
87 |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
88 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
|
89 { |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
90 ctor (conn); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
91 return; |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
92 } |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
93 |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
94 void |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
95 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
|
96 { |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
97 if (conn == 4) |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
98 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
99 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
|
100 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
|
101 md[ 0] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
102 md[ 2] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
103 md[ 6] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
104 md[ 8] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
105 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
106 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
|
107 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
108 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
|
109 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
|
110 md[ 4] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
111 md[10] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
112 md[12] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
113 md[13] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
114 md[14] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
115 md[16] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
116 md[22] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
117 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
118 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
|
119 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
|
120 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
|
121 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
122 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
|
123 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
|
124 md[ 0] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
125 md[ 2] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
126 md[ 6] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
127 md[ 8] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
128 md[18] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
129 md[20] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
130 md[24] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
131 md[26] = false; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
132 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
133 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
|
134 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
|
135 else |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
136 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
|
137 |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
138 return; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
139 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
140 |
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 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
|
143 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
|
144 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
145 dim_vector size; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
146 if (ndims == 1) |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
147 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
|
148 else |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
149 { |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
150 size = dim_vector (3, 3); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
151 size.resize (ndims, 3); |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
152 } |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
153 |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
154 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
|
155 { |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
156 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
|
157 } |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
158 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
|
159 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
160 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
|
161 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
|
162 |
892
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
163 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
|
164 md[0] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
165 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
|
166 { |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
167 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
|
168 md[ stride] = true; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
169 md[-stride] = true; |
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 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
172 else |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
173 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
|
174 |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
175 return; |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
176 } |
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
177 |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
178 |
891
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
179 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
|
180 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
|
181 { |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
182 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
|
183 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
|
184 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
|
185 |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
186 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
|
187 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
|
188 |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
189 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
|
190 |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
191 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
|
192 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
|
193 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
|
194 { |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
195 if (mask(ind)) |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
196 { |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
197 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
|
198 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
|
199 |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
200 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
|
201 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
|
202 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
|
203 |
891
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
204 offsets(found) = off; |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
205 found++; |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
206 } |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
207 } |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
208 |
a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
Carnë Draug <carandraug@octave.org>
parents:
890
diff
changeset
|
209 return offsets; |
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 |
890
3d1d76c830c6
conndef: rewrite as C++ class to be easily used by C++ classes.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
212 |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
213 double |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
214 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
|
215 { |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
216 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
|
217 // 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
|
218 // 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
|
219 if (error_state || ! val.is_scalar_type ()) |
907
5e256614c376
Make more use of exceptions and don't set error_state ourselves.
Carnë Draug <carandraug@octave.org>
parents:
906
diff
changeset
|
220 throw invalid_conversion ("no conversion to double value"); |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
221 return conn; |
892
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
222 } |
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
223 |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
224 boolNDArray |
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
225 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
|
226 { |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
227 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
|
228 // 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
|
229 // 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
|
230 if (val.array_value ().any_element_not_one_or_zero ()) |
907
5e256614c376
Make more use of exceptions and don't set error_state ourselves.
Carnë Draug <carandraug@octave.org>
parents:
906
diff
changeset
|
231 throw invalid_conversion ("no conversion to bool array value"); |
893
f34897bc944f
connectivity: move the validate() static methods into the constructor.
Carnë Draug <carandraug@octave.org>
parents:
892
diff
changeset
|
232 return mask; |
892
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
233 } |
a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
Carnë Draug <carandraug@octave.org>
parents:
891
diff
changeset
|
234 |