Apollo  6.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 <algorithm>
20 #include <string>
21 
22 #include "Eigen/Dense"
23 
24 namespace apollo {
25 namespace perception {
26 namespace common {
27 
28 template <typename T>
29 class SecureMat {
30  public:
31  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
32 
33  public:
34  SecureMat() { Reserve(max_height_, max_width_); }
35 
36  size_t height() { return height_; }
37  size_t width() { return width_; }
38 
39  /* @brief: reserve memory of SecureMat
40  * @params[IN] reserve_height: height of reserve memory
41  * @params[IN] reserve_width: width of reserve memory
42  */
43  void Reserve(const size_t reserve_height, const size_t reserve_width) {
44  max_height_ = std::max(reserve_height, max_height_);
45  max_width_ = std::max(reserve_width, max_width_);
46  mat_.resize(max_height_, max_width_);
47  }
48 
49  /* @brief: resize memory of SecureMat
50  * @params[IN] resize_height: height of resize memory
51  * @params[IN] resize_width: width of resize memory
52  * @return nothing */
53  void Resize(const size_t resize_height, const size_t resize_width) {
54  height_ = resize_height;
55  width_ = resize_width;
56  if (resize_height <= max_height_ && resize_width <= max_width_) {
57  return;
58  }
59  max_height_ = std::max(resize_height, max_height_);
60  max_width_ = std::max(resize_width, max_width_);
61  mat_.resize(max_height_, max_width_);
62  }
63 
64  std::string ToString() const {
65  std::ostringstream oss;
66  for (size_t row = 0; row < height_; ++row) {
67  for (size_t col = 0; col < width_; ++col) {
68  oss << mat_(row, col) << "\t";
69  }
70  oss << "\n";
71  }
72  return oss.str();
73  }
74 
75  inline const T& operator()(const size_t row, const size_t col) const {
76  return mat_(row, col);
77  }
78 
79  inline T& operator()(const size_t row, const size_t col) {
80  return mat_(row, col);
81  }
82 
83  private:
84  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> mat_;
85  size_t max_height_ = 1000;
86  size_t max_width_ = 1000;
87  size_t height_ = 0;
88  size_t width_ = 0;
89 }; // class SecureMat
90 
91 } // namespace common
92 } // namespace perception
93 } // namespace apollo
Definition: secure_matrix.h:29
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void Reserve(const size_t reserve_height, const size_t reserve_width)
Definition: secure_matrix.h:43
const T & operator()(const size_t row, const size_t col) const
Definition: secure_matrix.h:75
SecureMat()
Definition: secure_matrix.h:34
size_t width()
Definition: secure_matrix.h:37
T & operator()(const size_t row, const size_t col)
Definition: secure_matrix.h:79
void Resize(const size_t resize_height, const size_t resize_width)
Definition: secure_matrix.h:53
size_t height()
Definition: secure_matrix.h:36
std::string ToString() const
Definition: secure_matrix.h:64