🛰️航天仿真算法库 SpaceAST 0.4.0
载入中...
搜索中...
未找到
ParamAdapter.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstMath/Matrix.hpp"
25
26AST_NAMESPACE_BEGIN
27
33#ifndef SWIG
34
36template <typename T>
37class In;
38
40template <typename T>
41class Out;
42
43template <typename T>
44class InOut;
45
46// -------------------------
47// --- double* 参数适配器
48// -------------------------
49
51template <>
52class In<const double*>
53{
54public:
55 explicit In(const double* value) : value_(value) {}
56 operator const Vector3d& () const { return *reinterpret_cast<const Vector3d*>(value_); }
57 operator const Quaternion& () const { return *reinterpret_cast<const Quaternion*>(value_); }
58private:
59 const double* value_{};
60};
61
63template <>
64class Out<double*>
65{
66public:
67 explicit Out(double* value) : value_(value) {}
68 operator Vector3d& () { return *reinterpret_cast<Vector3d*>(value_); }
69 operator Quaternion& () { return *reinterpret_cast<Quaternion*>(value_); }
70private:
71 double* value_{};
72};
73
74
75
76// -------------------------
77// --- double[3][3] 参数适配器
78// -------------------------
79
80#if _AST_MATRIX_IS_ROW_MAJOR
81
83template <>
84class In<const double (*)[3]>
85{
86public:
87 explicit In(const double (*value)[3]) : value_(value) {}
88 operator const Matrix3d& () const { return *reinterpret_cast<const Matrix3d*>(value_); }
89private:
90 const double (*value_)[3]{};
91};
92
93
94
96template <>
97class Out<double (*)[3]>
98{
99public:
100 explicit Out(double (*value)[3]) : value_(value) {}
101 operator Matrix3d& () { return *reinterpret_cast<Matrix3d*>(value_); }
102private:
103 double (*value_)[3]{};
104};
105
106#else
107
109template <>
110class In<const double (*)[3]>
111{
112public:
113 explicit In(const double (*value)[3])
114 : value_(value)
115 {
116 if (value_ == nullptr)
117 return;
118 for(int i = 0; i < 3; i++)
119 {
120 for(int j = 0; j < 3; j++)
121 {
122 matrix_(i, j) = value_[i][j];
123 }
124 }
125 }
126 operator const Matrix3d& () const { return matrix_; }
127private:
128 Matrix3d matrix_{};
129 const double (*value_)[3]{};
130};
131
132
133
135template <>
136class Out<double (*)[3]>
137{
138public:
139 explicit Out(double (*value)[3]) : matrix_(Matrix3d::Zero()), value_(value) {}
141 Out(Out&& other) : matrix_(other.matrix_), value_(other.value_) { other.value_ = nullptr; }
142 operator Matrix3d& () { return matrix_; }
143 ~Out() {
144 if (value_ == nullptr)
145 return;
146 for(int i = 0; i < 3; i++)
147 {
148 for(int j = 0; j < 3; j++)
149 {
150 value_[i][j] = matrix_(i, j);
151 }
152 }
153 }
154private:
155 Matrix3d matrix_{};
156 double (*value_)[3]{};
157};
158
159#endif
160
161// -------------------------
162// --- double[6][6] 参数适配器
163// -------------------------
164
165// Eigen 对定长矩阵可能带有对齐要求(例如大小为 16 字节整数倍的矩阵)
166// 故不能把裸 double 缓冲区直接当作 Eigen::Matrix6d 对象使用,
167#if _AST_MATRIX_IS_ROW_MAJOR && !defined(AST_USE_EIGEN_MATRIX)
168
170template <>
171class Out<double (*)[6]>
172{
173public:
174 explicit Out(double (*value)[6]) : value_(value) {}
175 operator Matrix6d& () { return *reinterpret_cast<Matrix6d*>(value_); }
176private:
177 double (*value_)[6]{};
178};
179
180#else
181
183template <>
184class Out<double (*)[6]>
185{
186public:
187 explicit Out(double (*value)[6]) : matrix_(Matrix6d::Zero()), value_(value) {}
189 Out(Out&& other) : matrix_(other.matrix_), value_(other.value_) { other.value_ = nullptr; }
190 operator Matrix6d& () { return matrix_; }
191 ~Out() {
192 if (value_ == nullptr)
193 return;
194 for(int i = 0; i < 6; i++)
195 {
196 for(int j = 0; j < 6; j++)
197 {
198 value_[i][j] = matrix_(i, j);
199 }
200 }
201 }
202private:
203 Matrix6d matrix_{};
204 double (*value_)[6]{};
205};
206
207#endif
208
209
210// -------------------------
211// --- Matrix3d 参数适配器
212// -------------------------
213
214#if _AST_MATRIX_IS_ROW_MAJOR
215
217template <>
218class In<Matrix3d>
219{
220public:
221 explicit In(const Matrix3d& value) : value_(value) {}
222 typedef const double(*CMatrixType)[3];
223 operator CMatrixType () const { return reinterpret_cast<CMatrixType>(value_.data()); }
224private:
225 const Matrix3d& value_;
226};
227
228#else
229
231template <>
233{
234public:
235 explicit In(const Matrix3d& value)
236 : value_(value)
237 {
238 for(int i = 0; i < 3; i++)
239 {
240 for(int j = 0; j < 3; j++)
241 {
242 matrix_[i][j] = value_(i, j);
243 }
244 }
245 }
246 typedef const double(*CMatrixType)[3];
247 operator CMatrixType () const { return matrix_; }
248private:
249 double matrix_[3][3]{};
250 const Matrix3d& value_{};
251};
252
253#endif
254
255
256
261
262
263A_ALWAYS_INLINE In<const double*> in(const double* value)
264{
265 return In<const double*>(value);
266}
267
268A_ALWAYS_INLINE In<const double (*)[3]> in(const double (*value)[3])
269{
270 return In<const double (*)[3]>(value);
271}
272
273A_ALWAYS_INLINE In<Matrix3d> in(const Matrix3d& value)
274{
275 return In<Matrix3d>(value);
276}
277
278A_ALWAYS_INLINE Out<double*> out(double* value)
279{
280 return Out<double*>(value);
281}
282
283A_ALWAYS_INLINE Out<double (*)[3]> out(double (*value)[3])
284{
285 return Out<double (*)[3]>(value);
286}
287
288A_ALWAYS_INLINE Out<double (*)[6]> out(double (*value)[6])
289{
290 return Out<double (*)[6]>(value);
291}
292
293#endif
294
297AST_NAMESPACE_END
定义 ParamAdapter.hpp:44
输入适配器:把外部数据转换为函数的期望参数类型
定义 ParamAdapter.hpp:37
Out(Out &&other)
转移回写责任,源对象指针置空以免重复回写
定义 ParamAdapter.hpp:141
Out(Out &&other)
转移回写责任,源对象指针置空以免重复回写
定义 ParamAdapter.hpp:189
输出适配器:析构时把结果回写到外部缓冲区,或者直接将外部缓冲区绑定到函数的输出参数
定义 ParamAdapter.hpp:41
四元数
定义 Quaternion.hpp:48
Unit in
英寸
定义 Unit.cpp:459