简体   繁体   English

从C ++中的3D矩阵中提取2D矩阵

[英]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. 我正在尝试编写一个函数,该函数以给定索引作为输入参数,从3D矩阵中提取相应的图层,并返回2D矩阵。

My default 3D matrix constructor looks something like this: 我的默认3D矩阵构造函数如下所示:

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). try/catch仍然是一个小窍门,我知道这不是要做的事情)。

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. 我想用嵌套完成的任务背后的逻辑for周期是正确的,但我真的不知道如何返回新的矩阵。 From the main , used to test the code, I'm calling main用于测试代码,我在打电话

 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(). 创建一个新的类Matrice2D并在slice()中返回它。

The reason why your get total garbage in your code is that you destroy the _depth of the 3D matrix. 之所以在代码中得到全部垃圾,是因为您破坏了3D矩阵的_depth。 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 . newdelete应该出现的唯一时间是在名为something_ptr类中。 You don't need raw pointers here, and you should be returning a 2DMatrix from slice . 您这里不需要原始指针,应该从slice 返回 2DMatrix

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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