Debian Codegraph @

src/Base/Placement.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2006 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25 #include <fmt/format.h>
26
27 #include "Placement.h"
28 #include "Matrix.h"
29 #include "Rotation.h"
30 #include "DualQuaternion.h"
31
32 using namespace Base;
33
34 Placement::Placement() = default;
35
36 Placement::Placement(const Base::Matrix4D& matrix)
37 {
38 fromMatrix(matrix);
39 }
40
41 Placement::Placement(const Vector3d& Pos, const Rotation& Rot)
42 : _pos(Pos)
43 , _rot(Rot)
44 {}
45
46 Placement::Placement(const Vector3d& Pos, const Rotation& Rot, const Vector3d& Cnt)
47 : _rot(Rot)
48 {
49 Vector3d RotC = Cnt;
50 Rot.multVec(RotC, RotC);
51 this->_pos = Pos + Cnt - RotC;
52 }
53
54 Placement Placement::fromDualQuaternion(DualQuat qq)
55 {
56 Rotation rot(qq.x.re, qq.y.re, qq.z.re, qq.w.re);
57 DualQuat mvq = 2 * qq.dual() * qq.real().conj();
58 return {Vector3d(mvq.x.re, mvq.y.re, mvq.z.re), rot};
59 }
60
61 Base::Matrix4D Placement::toMatrix() const
62 {
63 Base::Matrix4D matrix;
64 _rot.getValue(matrix);
65 matrix[0][3] = this->_pos.x;
66 matrix[1][3] = this->_pos.y;
67 matrix[2][3] = this->_pos.z;
68 return matrix;
69 }
70
71 void Placement::fromMatrix(const Base::Matrix4D& matrix)
72 {
73 _rot.setValue(matrix);
74 this->_pos.x = matrix[0][3];
75 this->_pos.y = matrix[1][3];
76 this->_pos.z = matrix[2][3];
77 }
78
79 DualQuat Placement::toDualQuaternion() const
80 {
81 DualQuat posqq(_pos.x, _pos.y, _pos.z, 0.0);
82 DualQuat rotqq;
83 _rot.getValue(rotqq.x.re, rotqq.y.re, rotqq.z.re, rotqq.w.re);
84 DualQuat ret(rotqq, 0.5 * posqq * rotqq);
85 return ret;
86 }
87
88 bool Placement::isIdentity() const
89 {
90 Base::Vector3d nullvec(0, 0, 0);
91 bool none = (this->_pos == nullvec) && (this->_rot.isIdentity());
92 return none;
93 }
94
95 bool Placement::isIdentity(double tol) const
96 {
97 return isSame(Placement(), tol);
98 }
99
100 bool Placement::isSame(const Placement& p) const
101 {
102 return this->_rot.isSame(p._rot) && this->_pos.IsEqual(p._pos, 0);
103 }
104
105 bool Placement::isSame(const Placement& p, double tol) const
106 {
107 return this->_rot.isSame(p._rot, tol) && this->_pos.IsEqual(p._pos, tol);
108 }
109
110 void Placement::invert()
111 {
112 this->_rot = this->_rot.inverse();
113 this->_rot.multVec(this->_pos, this->_pos);
114 this->_pos = -this->_pos;
115 }
116
117 Placement Placement::inverse() const
118 {
119 Placement p(*this);
120 p.invert();
121 return p;
122 }
123
124 void Placement::move(const Vector3d& MovVec)
125 {
126 _pos += MovVec;
127 }
128
129 bool Placement::operator==(const Placement& that) const
130 {
131 return (this->_pos == that._pos) && (this->_rot == that._rot);
132 }
133
134 bool Placement::operator!=(const Placement& that) const
135 {
136 return !(*this == that);
137 }
138
139 /*!
140 Let this placement be right-multiplied by \a p. Returns reference to
141 self.
142
143 \sa multRight()
144 */
145 Placement& Placement::operator*=(const Placement& p)
146 {
147 return multRight(p);
148 }
149
150 Placement Placement::operator*(const Placement& p) const
151 {
152 Placement plm(*this);
153 plm *= p;
154 return plm;
155 }
156
157 Placement Placement::pow(double t, bool shorten) const
158 {
159 return Placement::fromDualQuaternion(this->toDualQuaternion().pow(t, shorten));
160 }
161
162 /*!
163 Let this placement be right-multiplied by \a p. Returns reference to
164 self.
165
166 \sa multLeft()
167 */
168 Placement& Placement::multRight(const Base::Placement& p)
169 {
170 Base::Vector3d tmp(p._pos);
171 this->_rot.multVec(tmp, tmp);
172 this->_pos += tmp;
173 this->_rot.multRight(p._rot);
174 return *this;
175 }
176
177 /*!
178 Let this placement be left-multiplied by \a p. Returns reference to
179 self.
180
181 \sa multRight()
182 */
183 Placement& Placement::multLeft(const Base::Placement& p)
184 {
185 p.multVec(this->_pos, this->_pos);
186 this->_rot.multLeft(p._rot);
187 return *this;
188 }
189
190 void Placement::multVec(const Vector3d& src, Vector3d& dst) const
191 {
192 this->_rot.multVec(src, dst);
193 dst += this->_pos;
194 }
195
196 void Placement::multVec(const Vector3f& src, Vector3f& dst) const
197 {
198 this->_rot.multVec(src, dst);
199 dst += Base::toVector<float>(this->_pos);
200 }
201
202 Placement Placement::slerp(const Placement& p0, const Placement& p1, double t)
203 {
204 Rotation rot = Rotation::slerp(p0.getRotation(), p1.getRotation(), t);
205 Vector3d pos = p0.getPosition() * (1.0 - t) + p1.getPosition() * t;
206 return {pos, rot};
207 }
208
209 Placement Placement::sclerp(const Placement& p0, const Placement& p1, double t, bool shorten)
210 {
211 Placement trf = p0.inverse() * p1;
212 return p0 * trf.pow(t, shorten);
213 }
214
215 std::string Placement::toString() const
216 {
217 Base::Vector3d pos = getPosition();
218 Base::Rotation rot = getRotation();
219
220 Base::Vector3d axis;
221 double angle {};
222 rot.getRawValue(axis, angle);
223
224 // clang-format off
225 return fmt::format("position ({:.1f}, {:.1f}, {:.1f}), axis ({:.1f}, {:.1f}, {:.1f}), angle {:.1f}\n",
226 pos.x, pos.y, pos.z, axis.x, axis.y, axis.z, angle);
227 // clang-format on
228 }