简体   繁体   中英

Extracting a 2D matrix from a 3D matrix in c++

I'm trying to code a function that, given an index as the input argument, extract the corresponding layer from a 3D matrix, returning a 2D matrix.

My default 3D matrix constructor looks something like this:

Matrice3D(unsigned int depth, unsigned int height, unsigned int width, const T &value) : _3D_matrix(0), _height(0), _width(0), _depth(0) { 

    try {
       _3D_matrix = new T[height * width * depth];
            for (int z = 0; z < depth; z++) {
                for (int y = 0; y < height; y++) {
                    for (int x = 0; x < width; x++) {
                        _3D_matrix[y * width * depth + x * depth + z] = value; 
                    }
                }
            }
   }
    catch(...) {
        delete[] _3D_matrix;
        throw;
    }


    _height = height;
    _width = width;
    _depth = depth;

    }

(the try/catch is still a wip, I know it's not a thing to do).

So far, I've coded this:

void slice(int index) {

        try{
            _2D_matrix = new T[_height * _width]; 
        }catch(...){
            delete[] _2D_matrix;
            _height = 0;
            _width = 0;
            _depth = 0;
            throw;
        }

        _depth = 1;

        for (int k = 0; k< _depth; k++) {
            for (int j = 0; j< _height; j++) {
                for (int i = 0; i< _width; i++) {
                    _2D_matrix[j * _width + i] = 
                    _3D_matrix[j * _width * _depth + i * _depth + index];
                }
            }
        }
    }

I guess the logic behind the assignment done with the nested for cycles is right, but I don't really know how to return the new matrix. From the main , used to test the code, I'm calling

 std::cout << "--------TEST SLICE------------" << std::endl;
 Matrice3D<int> m1(3,3,3,17);
 std::cout << "Setter su (0,0,2,99)" << std::endl; 
 m1(0,0,2,91); //setting a value

 m1.slice(2);
 std::cout << "Matrix obtained from slicing the layer 2: " <<std::endl;
 std::cout << m1;

but I keep getting the first layer of the matrix, whatever index I choose for the input.

Create a new class Matrice2D and return that in slice().

The reason why your get total garbage in your code is that you destroy the _depth of the 3D matrix. It's not even the first layer but really just garbage.

The only time new and delete should appear is in classes named something_ptr . You don't need raw pointers here, and you should be returning a 2DMatrix from slice .

template <typename T>
class 2DMatrix;

template <typename T>
class 3DMatrix {
    std::vector<T> data;
    std::size_t height, width, depth;
public:
    3DMatrix(std::size_t height, std::size_t width, std::size_t depth, T value = {})
      : data(height * width * depth, value), 
        height(height), 
        width(width), 
        depth(depth) 
    {}

    2DMatrix<T> slice(std::size_t index) {
        2DMatrix<T> result(height, width);
        for (std::size_t i = index; i < data.size(); i += depth) {
            result.data[i / depth] = data[i];
        }
        return result;
    }

    // other members
}

template <typename T>
class 2DMatrix {
    std::vector<T> data;
    std::size_t height, width;
    friend class 3DMatrix<T>;
public:
    2DMatrix(std::size_t height, std::size_t width, T value = {})
      : data(height * width, value), 
        height(height), 
        width(width) 
    {}

    // other members
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM