plcopen 0.22.0
载入中...
搜索中...
未找到
frame.h
浏览该文件的文档.
1#pragma once
2
3// Rigid coordinate frame for the B1 v1 coordinate stack (approved matrix:
4// doc/compliance/part4-coordinate-semantics.md, decisions #4/#10): a
5// translation plus one rotation about Z ("2.5D" workpiece placement). The
6// rotation is stored as a cached cosine/sine pair, so applying the frame is
7// a handful of multiplies; rigidity holds by construction — there is no
8// arbitrary-matrix input to validate for orthogonality.
9//
10// RT constraints: plain value math, no allocation, no exceptions. Frame
11// application happens at submit time (planning domain) only.
12
13#include <cmath>
14
15#include "geom/geometry.h"
16#include "rt/error.h"
17
19{
20
22{
23 double x = 0.0;
24 double y = 0.0;
25 double z = 0.0;
26 double cos_rz = 1.0;
27 double sin_rz = 0.0;
28};
29
30inline rt::Result<RigidFrame> make_frame(double x, double y, double z, double rot_z)
31{
32 if(!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(z) || !std::isfinite(rot_z)) {
34 }
35 RigidFrame frame{};
36 frame.x = x;
37 frame.y = y;
38 frame.z = z;
39 frame.cos_rz = std::cos(rot_z);
40 frame.sin_rz = std::sin(rot_z);
42}
43
44// Rotation only: for direction vectors (relative distances).
45inline Vec3 frame_rotate(const RigidFrame &frame, Vec3 direction)
46{
47 return Vec3{frame.cos_rz * direction.x - frame.sin_rz * direction.y,
48 frame.sin_rz * direction.x + frame.cos_rz * direction.y,
49 direction.z};
50}
51
52// Frame-local point to base coordinates: rotate about Z, then translate.
53inline Vec3 frame_to_base(const RigidFrame &frame, Vec3 local)
54{
55 const Vec3 rotated = frame_rotate(frame, local);
56 return Vec3{rotated.x + frame.x, rotated.y + frame.y, rotated.z + frame.z};
57}
58
59// Full rigid transform (orientation batch, approved matrix decision #4/#5):
60// a rotation matrix plus a translation. Built from RPY angles only
61// (R = Rz(yaw) * Ry(pitch) * Rx(roll)), so rigidity holds by construction —
62// no arbitrary-matrix orthogonality validation. RPY is the input direction
63// exclusively; the matrix-to-RPY direction (and its gimbal ambiguity) is
64// deliberately absent.
66{
67 double rotation[3][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}};
69};
70
72 double y,
73 double z,
74 double roll,
75 double pitch,
76 double yaw)
77{
78 const double cr = std::cos(roll);
79 const double sr = std::sin(roll);
80 const double cp = std::cos(pitch);
81 const double sp = std::sin(pitch);
82 const double cy = std::cos(yaw);
83 const double sy = std::sin(yaw);
84
85 RigidTransform transform{};
86 transform.rotation[0][0] = cy * cp;
87 transform.rotation[0][1] = cy * sp * sr - sy * cr;
88 transform.rotation[0][2] = cy * sp * cr + sy * sr;
89 transform.rotation[1][0] = sy * cp;
90 transform.rotation[1][1] = sy * sp * sr + cy * cr;
91 transform.rotation[1][2] = sy * sp * cr - cy * sr;
92 transform.rotation[2][0] = -sp;
93 transform.rotation[2][1] = cp * sr;
94 transform.rotation[2][2] = cp * cr;
95 transform.translation = Vec3{x, y, z};
96 return transform;
97}
98
99inline Vec3 transform_rotate(const RigidTransform &transform, Vec3 v)
100{
101 return Vec3{transform.rotation[0][0] * v.x + transform.rotation[0][1] * v.y +
102 transform.rotation[0][2] * v.z,
103 transform.rotation[1][0] * v.x + transform.rotation[1][1] * v.y +
104 transform.rotation[1][2] * v.z,
105 transform.rotation[2][0] * v.x + transform.rotation[2][1] * v.y +
106 transform.rotation[2][2] * v.z};
107}
108
109inline Vec3 transform_point(const RigidTransform &transform, Vec3 p)
110{
111 return transform_rotate(transform, p) + transform.translation;
112}
113
114// a then b in a's frame: (a ∘ b)(p) = a(b(p)).
116{
117 RigidTransform out{};
118 for(int i = 0; i < 3; ++i) {
119 for(int j = 0; j < 3; ++j) {
120 out.rotation[i][j] = a.rotation[i][0] * b.rotation[0][j] +
121 a.rotation[i][1] * b.rotation[1][j] +
122 a.rotation[i][2] * b.rotation[2][j];
123 }
124 }
126 return out;
127}
128
129// Cartesian-interpolation batch (approved matrix decision #4): rotation
130// primitives for the per-cycle geodesic. relative_axis_angle extracts the
131// axis-angle of b relative to a (a-transpose times b); angles at or beyond
132// pi are rejected by the caller (the geodesic is not unique there), and
133// close to pi the axis comes from the symmetric form with skew-part signs
134// for stability.
135inline void rotation_multiply(const double a[3][3],
136 const double b[3][3],
137 double out[3][3])
138{
139 for(int i = 0; i < 3; ++i) {
140 for(int j = 0; j < 3; ++j) {
141 out[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j];
142 }
143 }
144}
145
146inline void relative_axis_angle(const double a[3][3],
147 const double b[3][3],
148 double axis[3],
149 double &angle)
150{
151 double rel[3][3];
152 for(int i = 0; i < 3; ++i) {
153 for(int j = 0; j < 3; ++j) {
154 rel[i][j] = a[0][i] * b[0][j] + a[1][i] * b[1][j] + a[2][i] * b[2][j];
155 }
156 }
157 const double trace = rel[0][0] + rel[1][1] + rel[2][2];
158 double cosine = (trace - 1.0) * 0.5;
159 cosine = cosine > 1.0 ? 1.0 : (cosine < -1.0 ? -1.0 : cosine);
160 angle = std::acos(cosine);
161 axis[0] = 0.0;
162 axis[1] = 0.0;
163 axis[2] = 1.0;
164 if(angle < 1e-12) {
165 angle = 0.0;
166 return;
167 }
168 const double skew[3] = {rel[2][1] - rel[1][2], rel[0][2] - rel[2][0],
169 rel[1][0] - rel[0][1]};
170 if(angle < 3.0) {
171 const double scale = 1.0 / (2.0 * std::sin(angle));
172 axis[0] = skew[0] * scale;
173 axis[1] = skew[1] * scale;
174 axis[2] = skew[2] * scale;
175 } else {
176 for(int i = 0; i < 3; ++i) {
177 double squared = (rel[i][i] - cosine) / (1.0 - cosine);
178 squared = squared < 0.0 ? 0.0 : squared;
179 axis[i] = std::sqrt(squared);
180 if(skew[i] < 0.0) {
181 axis[i] = -axis[i];
182 }
183 }
184 }
185 const double norm =
186 std::sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);
187 if(norm > 0.0) {
188 axis[0] /= norm;
189 axis[1] /= norm;
190 axis[2] /= norm;
191 }
192}
193
194inline void rodrigues(const double axis[3], double angle, double out[3][3])
195{
196 const double c = std::cos(angle);
197 const double s = std::sin(angle);
198 const double t = 1.0 - c;
199 const double x = axis[0];
200 const double y = axis[1];
201 const double z = axis[2];
202 out[0][0] = t * x * x + c;
203 out[0][1] = t * x * y - s * z;
204 out[0][2] = t * x * z + s * y;
205 out[1][0] = t * x * y + s * z;
206 out[1][1] = t * y * y + c;
207 out[1][2] = t * y * z - s * x;
208 out[2][0] = t * x * z - s * y;
209 out[2][1] = t * y * z + s * x;
210 out[2][2] = t * z * z + c;
211}
212
213// Readback batch (approved matrix decision #4): the declared inverse of
214// make_rpy_transform. pitch = atan2(-r20, hypot(r00, r10)) in [-pi/2, pi/2],
215// roll/yaw from the cosine-pitch element pairs, all in (-pi, pi]. Inside the
216// gimbal band (hypot(r00, r10) < 1e-9) the rotational freedom folds into yaw
217// with roll = 0 by convention and the return value flags it; the rebuilt
218// matrix stays exact either way. This is the only place the matrix-to-RPY
219// direction exists — command inputs stay RPY-only (orientation matrix
220// decision #10).
221inline bool extract_rpy(const double rotation[3][3],
222 double &roll,
223 double &pitch,
224 double &yaw)
225{
226 const double cos_pitch = std::hypot(rotation[0][0], rotation[1][0]);
227 pitch = std::atan2(-rotation[2][0], cos_pitch);
228 if(cos_pitch < 1e-9) {
229 roll = 0.0;
230 yaw = std::atan2(-rotation[0][1], rotation[1][1]);
231 return true;
232 }
233 roll = std::atan2(rotation[2][1], rotation[2][2]);
234 yaw = std::atan2(rotation[1][0], rotation[0][0]);
235 return false;
236}
237
238inline RigidTransform invert(const RigidTransform &transform)
239{
240 RigidTransform out{};
241 for(int i = 0; i < 3; ++i) {
242 for(int j = 0; j < 3; ++j) {
243 out.rotation[i][j] = transform.rotation[j][i];
244 }
245 }
246 out.translation = Vec3{0.0, 0.0, 0.0} - transform_rotate(out, transform.translation);
247 return out;
248}
249
250} // namespace plcopen::core::geom
static constexpr Result success(const T &value)
定义 error.h:24
static constexpr Result failure(ErrorCode error)
定义 error.h:29
Vec3 frame_to_base(const RigidFrame &frame, Vec3 local)
定义 frame.h:53
Vec3 transform_rotate(const RigidTransform &transform, Vec3 v)
定义 frame.h:99
void rotation_multiply(const double a[3][3], const double b[3][3], double out[3][3])
Vec3 transform_point(const RigidTransform &transform, Vec3 p)
RigidTransform invert(const RigidTransform &transform)
double norm(Vec3 value)
void rodrigues(const double axis[3], double angle, double out[3][3])
bool extract_rpy(const double rotation[3][3], double &roll, double &pitch, double &yaw)
RigidTransform make_rpy_transform(double x, double y, double z, double roll, double pitch, double yaw)
定义 frame.h:71
rt::Result< RigidFrame > make_frame(double x, double y, double z, double rot_z)
定义 frame.h:30
void relative_axis_angle(const double a[3][3], const double b[3][3], double axis[3], double &angle)
RigidTransform compose(const RigidTransform &a, const RigidTransform &b)
Vec3 frame_rotate(const RigidFrame &frame, Vec3 direction)
定义 frame.h:45