Mercurial > hg > octave-image
changeset 891:a65390dffc5a
conndef.cc: add new offsets method to the C++ condeff class.
* src/conndef.cc: add new method to calculate offsets for a connectivity
array, in an array of specific size.
* src/conndef.h: add new meethod to header file.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Wed, 01 Oct 2014 04:34:07 +0100 |
parents | 3d1d76c830c6 |
children | a2140b980079 |
files | src/conndef.cc src/conndef.h |
diffstat | 2 files changed, 42 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/src/conndef.cc +++ b/src/conndef.cc @@ -47,7 +47,7 @@ md[22] = true; } else if (conn == 8) - mask = boolNDArray (dim_vector (3, 3), true); + mask = boolNDArray (dim_vector (3, 3), true); else if (conn == 18) { mask = boolNDArray (dim_vector (3, 3, 3), true); @@ -81,16 +81,16 @@ size.resize (ndims, 3); if (type == "maximal") - mask = boolNDArray (size, true); + { + mask = boolNDArray (size, true); + } else if (type == "minimal") { mask = boolNDArray (size, false); bool* md = mask.fortran_vec (); - const octave_idx_type center = ceil (pow (3, ndims) /2); - md += (center -1); + md += int (ceil (pow (3, ndims) /2) -1); // move to center md[0] = true; - for (octave_idx_type dim = 0; dim < ndims; dim++) { const octave_idx_type stride = pow (3, dim); @@ -104,6 +104,39 @@ return; } +Array<octave_idx_type> +connectivity::offsets (const dim_vector& size) const +{ + const octave_idx_type nnz = mask.nnz (); + const octave_idx_type ndims = mask.ndims (); + const dim_vector dims = mask.dims (); + + Array<octave_idx_type> offsets (dim_vector (nnz, 1)); // retval + const dim_vector cum_size = size.cumulative (); + + + Array<octave_idx_type> diff (dim_vector (ndims, 1)); + + Array<octave_idx_type> sub (dim_vector (ndims, 1), 0); + for (octave_idx_type ind = 0, found = 0; found < nnz; + ind++, boolNDArray::increment_index (sub, dims)) + { + if (mask(ind)) + { + for (octave_idx_type i = 0; i < ndims; i++) + diff(i) = 1 - sub(i); + + octave_idx_type off = diff(0); + for (octave_idx_type dim = 1; dim < ndims; dim++) + off += (diff(dim) * cum_size(dim-1)); + offsets(found) = off; + found++; + } + } + + return offsets; +} + // The conndef() function is really really simple and could have easily // been a m file (actually it once was, check the hg log if it ever needs
--- a/src/conndef.h +++ b/src/conndef.h @@ -31,6 +31,10 @@ connectivity (const octave_idx_type& ndims, const std::string& type); boolNDArray mask; + + // For a matrix of size `size', what are the offsets for all of its + // connected elements (will have negative and positive values). + Array<octave_idx_type> offsets (const dim_vector& size) const; }; } }