Apollo
10.0
自动驾驶开放平台
aligned_matrix.h
浏览该文件的文档.
1
2
/******************************************************************************
3
* Copyright 2019 The Apollo Authors. All Rights Reserved.
4
*
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
8
*
9
* http://www.apache.org/licenses/LICENSE-2.0
10
*
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
16
*****************************************************************************/
17
#pragma once
18
19
#include <cstddef>
20
#include <iostream>
21
22
namespace
apollo
{
23
namespace
localization {
24
namespace
msf {
25
namespace
pyramid_map {
26
27
template
<
typename
Scalar,
int
aligned_len = alignof(max_align_t)>
28
class
AlignedMatrix
{
29
public
:
30
AlignedMatrix
();
31
explicit
AlignedMatrix
(
const
AlignedMatrix<Scalar, aligned_len>
& matrix);
32
~AlignedMatrix
();
33
34
void
Init
(
int
rows,
int
cols);
35
void
MakeEmpty
();
36
void
MakeEmpty
(
int
start_id,
int
end_id);
37
int
GetRow
()
const
;
38
int
GetCol
()
const
;
39
Scalar*
GetData
();
40
void
SetData
(
const
Scalar* data,
unsigned
int
data_size,
41
unsigned
int
start_id);
42
43
AlignedMatrix
&
operator=
(
const
AlignedMatrix<Scalar, aligned_len>
& matrix);
44
45
inline
Scalar*
operator[]
(
int
row) {
return
row_data_
[row]; }
46
47
inline
const
Scalar*
operator[]
(
int
row)
const
{
return
row_data_
[row]; }
48
49
protected
:
50
Scalar*
data_
=
nullptr
;
51
Scalar**
row_data_
=
nullptr
;
52
int
rows_
= 0;
53
int
cols_
= 0;
54
55
private
:
56
void
* raw_ptr_ =
nullptr
;
57
int
raw_size_ = 0;
58
};
59
60
template
<
typename
Scalar,
int
aligned_len>
61
AlignedMatrix<Scalar, aligned_len>::AlignedMatrix
()
62
: data_(nullptr),
63
row_data_(nullptr),
64
rows_(0),
65
cols_(0),
66
raw_ptr_(nullptr),
67
raw_size_(0) {}
68
69
template
<
typename
Scalar,
int
aligned_len>
70
AlignedMatrix<Scalar, aligned_len>::AlignedMatrix
(
71
const
AlignedMatrix<Scalar, aligned_len>
& matrix) {
72
rows_ = matrix.
rows_
;
73
cols_ = matrix.
cols_
;
74
75
raw_size_ = matrix.raw_size_;
76
raw_ptr_ = malloc(raw_size_);
77
78
row_data_ =
reinterpret_cast<
Scalar**
>
(malloc(
sizeof
(Scalar*) * rows_));
79
80
unsigned
char
* ptr =
reinterpret_cast<
unsigned
char
*
>
(raw_ptr_);
81
int
idx = 0;
82
while
(idx < aligned_len) {
83
if
((uint64_t)(ptr) % aligned_len == 0) {
84
break
;
85
}
86
++ptr;
87
++idx;
88
}
89
data_ =
reinterpret_cast<
Scalar*
>
(ptr);
90
91
for
(
int
k = 0; k < rows_; k++) {
92
row_data_[k] = data_ + k * cols_;
93
}
94
95
memcpy(data_, matrix.
data_
,
sizeof
(Scalar) * rows_ * cols_);
96
}
97
98
template
<
typename
Scalar,
int
aligned_len>
99
AlignedMatrix<Scalar, aligned_len>::~AlignedMatrix
() {
100
if
(raw_ptr_) {
101
free(raw_ptr_);
102
raw_size_ = 0;
103
raw_ptr_ =
nullptr
;
104
}
105
106
if
(row_data_) {
107
free(row_data_);
108
row_data_ =
nullptr
;
109
}
110
111
data_ =
nullptr
;
112
raw_size_ = 0;
113
}
114
115
template
<
typename
Scalar,
int
aligned_len>
116
void
AlignedMatrix<Scalar, aligned_len>::Init
(
int
rows,
int
cols) {
117
if
(raw_ptr_) {
118
free(raw_ptr_);
119
raw_size_ = 0;
120
raw_ptr_ =
nullptr
;
121
}
122
123
if
(row_data_) {
124
free(row_data_);
125
row_data_ =
nullptr
;
126
}
127
128
rows_ = rows;
129
cols_ = cols;
130
131
raw_size_ =
static_cast<
int
>
(
sizeof
(Scalar)) * (rows * cols) + aligned_len;
132
raw_ptr_ = malloc(raw_size_);
133
134
row_data_ =
reinterpret_cast<
Scalar**
>
(malloc(
sizeof
(Scalar*) * rows_));
135
136
unsigned
char
* ptr =
reinterpret_cast<
unsigned
char
*
>
(raw_ptr_);
137
int
idx = 0;
138
while
(idx < aligned_len) {
139
if
((uint64_t)(ptr) % aligned_len == 0) {
140
break
;
141
}
142
++ptr;
143
++idx;
144
}
145
data_ =
reinterpret_cast<
Scalar*
>
(ptr);
146
147
for
(
int
k = 0; k < rows_; k++) {
148
row_data_[k] = data_ + k * cols_;
149
}
150
151
MakeEmpty();
152
153
// printf("aligned addr: %p\n", (void*)data_);
154
}
155
156
template
<
typename
Scalar,
int
aligned_len>
157
void
AlignedMatrix<Scalar, aligned_len>::MakeEmpty
() {
158
memset(data_, 0,
sizeof
(Scalar) * rows_ * cols_);
159
}
160
161
template
<
typename
Scalar,
int
aligned_len>
162
void
AlignedMatrix<Scalar, aligned_len>::MakeEmpty
(
int
start_id,
int
end_id) {
163
memset(data_ + start_id, 0,
sizeof
(Scalar) * (end_id - start_id + 1));
164
}
165
166
template
<
typename
Scalar,
int
aligned_len>
167
int
AlignedMatrix<Scalar, aligned_len>::GetRow
()
const
{
168
return
rows_;
169
}
170
171
template
<
typename
Scalar,
int
aligned_len>
172
int
AlignedMatrix<Scalar, aligned_len>::GetCol
()
const
{
173
return
cols_;
174
}
175
176
template
<
typename
Scalar,
int
aligned_len>
177
Scalar*
AlignedMatrix<Scalar, aligned_len>::GetData
() {
178
return
data_;
179
}
180
181
template
<
typename
Scalar,
int
aligned_len>
182
void
AlignedMatrix<Scalar, aligned_len>::SetData
(
const
Scalar* data,
183
unsigned
int
data_size,
184
unsigned
int
start_id) {
185
memcpy(data_ + start_id, data,
sizeof
(Scalar) * data_size);
186
}
187
188
template
<
typename
Scalar,
int
aligned_len>
189
AlignedMatrix<Scalar, aligned_len>
&
AlignedMatrix<Scalar, aligned_len>::
190
operator=
(
const
AlignedMatrix<Scalar, aligned_len>
& matrix) {
191
if
(raw_ptr_) {
192
free(raw_ptr_);
193
raw_size_ = 0;
194
raw_ptr_ =
nullptr
;
195
}
196
197
if
(row_data_) {
198
free(row_data_);
199
row_data_ =
nullptr
;
200
}
201
202
rows_ = matrix.
rows_
;
203
cols_ = matrix.
cols_
;
204
205
raw_size_ = matrix.raw_size_;
206
raw_ptr_ = malloc(raw_size_);
207
208
row_data_ =
reinterpret_cast<
Scalar**
>
(malloc(
sizeof
(Scalar*) * rows_));
209
210
unsigned
char
* ptr =
reinterpret_cast<
unsigned
char
*
>
(raw_ptr_);
211
int
idx = 0;
212
while
(idx < aligned_len) {
213
if
((uint64_t)(ptr) % aligned_len == 0) {
214
break
;
215
}
216
++ptr;
217
++idx;
218
}
219
data_ =
reinterpret_cast<
Scalar*
>
(ptr);
220
221
for
(
int
k = 0; k < rows_; k++) {
222
row_data_[k] = data_ + k * cols_;
223
}
224
225
memcpy(data_, matrix.
data_
,
sizeof
(Scalar) * rows_ * cols_);
226
return
*
this
;
227
}
228
229
}
// namespace pyramid_map
230
}
// namespace msf
231
}
// namespace localization
232
}
// namespace apollo
apollo::localization::msf::pyramid_map::AlignedMatrix
Definition
aligned_matrix.h:28
apollo::localization::msf::pyramid_map::AlignedMatrix::SetData
void SetData(const Scalar *data, unsigned int data_size, unsigned int start_id)
Definition
aligned_matrix.h:182
apollo::localization::msf::pyramid_map::AlignedMatrix::GetRow
int GetRow() const
Definition
aligned_matrix.h:167
apollo::localization::msf::pyramid_map::AlignedMatrix::data_
Scalar * data_
Definition
aligned_matrix.h:50
apollo::localization::msf::pyramid_map::AlignedMatrix::GetData
Scalar * GetData()
Definition
aligned_matrix.h:177
apollo::localization::msf::pyramid_map::AlignedMatrix::~AlignedMatrix
~AlignedMatrix()
Definition
aligned_matrix.h:99
apollo::localization::msf::pyramid_map::AlignedMatrix::Init
void Init(int rows, int cols)
Definition
aligned_matrix.h:116
apollo::localization::msf::pyramid_map::AlignedMatrix::MakeEmpty
void MakeEmpty()
Definition
aligned_matrix.h:157
apollo::localization::msf::pyramid_map::AlignedMatrix::AlignedMatrix
AlignedMatrix()
Definition
aligned_matrix.h:61
apollo::localization::msf::pyramid_map::AlignedMatrix::cols_
int cols_
Definition
aligned_matrix.h:53
apollo::localization::msf::pyramid_map::AlignedMatrix::operator[]
const Scalar * operator[](int row) const
Definition
aligned_matrix.h:47
apollo::localization::msf::pyramid_map::AlignedMatrix::row_data_
Scalar ** row_data_
Definition
aligned_matrix.h:51
apollo::localization::msf::pyramid_map::AlignedMatrix::operator[]
Scalar * operator[](int row)
Definition
aligned_matrix.h:45
apollo::localization::msf::pyramid_map::AlignedMatrix::GetCol
int GetCol() const
Definition
aligned_matrix.h:172
apollo::localization::msf::pyramid_map::AlignedMatrix::rows_
int rows_
Definition
aligned_matrix.h:52
apollo::localization::msf::pyramid_map::AlignedMatrix::operator=
AlignedMatrix & operator=(const AlignedMatrix< Scalar, aligned_len > &matrix)
Definition
aligned_matrix.h:190
apollo
class register implement
Definition
arena_queue.h:37
modules
localization
msf
local_pyramid_map
pyramid_map
aligned_matrix.h