Apollo  v5.5.0
Open source self driving car software
secure_matrix.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #pragma once
18 
19 #include "Eigen/Dense"
20 
21 namespace apollo {
22 namespace perception {
23 namespace common {
24 
25 template <typename T>
26 class SecureMat {
27  public:
28  SecureMat() : height_(0), width_(0) { Reserve(max_height_, max_width_); }
29 
30  size_t height() { return height_; }
31  size_t width() { return width_; }
32 
33  /* @brief: reserve memory of SecureMat
34  * @params[IN] reserve_height: height of reserve memory
35  * @params[IN] reserve_width: width of reserve memory
36  * @return nothing */
37  void Reserve(const size_t reserve_height, const size_t reserve_width) {
38  max_height_ = (reserve_height > max_height_) ? reserve_height : max_height_;
39  max_width_ = (reserve_width > max_width_) ? reserve_width : max_width_;
40  mat_.resize(max_height_, max_width_);
41  }
42 
43  /* @brief: resize memory of SecureMat
44  * @params[IN] resize_height: height of resize memory
45  * @params[IN] resize_width: width of resize memory
46  * @return nothing */
47  void Resize(const size_t resize_height, const size_t resize_width) {
48  height_ = resize_height;
49  width_ = resize_width;
50  if (resize_height <= max_height_ && resize_width <= max_width_) {
51  return;
52  }
53  max_height_ = (resize_height > max_height_) ? resize_height : max_height_;
54  max_width_ = (resize_width > max_width_) ? resize_width : max_width_;
55  mat_.resize(max_height_, max_width_);
56  }
57 
58  void ToString(std::ostream* out_stream) {
59  std::ostream& stream = *out_stream;
60  for (size_t row = 0; row < height_; ++row) {
61  for (size_t col = 0; col < width_; ++col) {
62  stream << mat_(row, col) << "\t";
63  }
64  stream << "\n";
65  }
66  }
67 
68  inline const T& operator()(const size_t row, const size_t col) const {
69  return mat_(row, col);
70  }
71 
72  inline T& operator()(const size_t row, const size_t col) {
73  return mat_(row, col);
74  }
75 
76  private:
77  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> mat_;
78  size_t max_height_ = 1000;
79  size_t max_width_ = 1000;
80  size_t height_ = 0;
81  size_t width_ = 0;
82 }; // class SecureMat
83 
84 } // namespace common
85 } // namespace perception
86 } // namespace apollo
Definition: secure_matrix.h:26
Definition: blob.h:72
void Reserve(const size_t reserve_height, const size_t reserve_width)
Definition: secure_matrix.h:37
const T & operator()(const size_t row, const size_t col) const
Definition: secure_matrix.h:68
SecureMat()
Definition: secure_matrix.h:28
size_t width()
Definition: secure_matrix.h:31
void ToString(std::ostream *out_stream)
Definition: secure_matrix.h:58
T & operator()(const size_t row, const size_t col)
Definition: secure_matrix.h:72
void Resize(const size_t resize_height, const size_t resize_width)
Definition: secure_matrix.h:47
size_t height()
Definition: secure_matrix.h:30