11package org.mechdancer.geometry.transformation
22
3- import org.mechdancer.algebra.function.vector.times
43import org.mechdancer.algebra.implement.matrix.builder.matrix
5- import org.mechdancer.algebra.implement.vector.*
6- import org.mechdancer.geometry.angle.toAngle
4+ import org.mechdancer.algebra.implement.vector.Vector3D
5+ import org.mechdancer.algebra.implement.vector.vector2DOf
6+ import org.mechdancer.algebra.implement.vector.vector3DOf
7+ import org.mechdancer.algebra.implement.vector.vector3DOfZero
78import org.mechdancer.geometry.angle.toRad
89import org.mechdancer.geometry.angle.toVector
9- import kotlin.math.cos
10- import kotlin.math.sin
10+ import kotlin.math.atan2
1111
1212fun pose2D (x : Number = 0, y : Number = 0, theta : Number = 0) =
1313 Pose2D (vector2DOf(x, y), theta.toRad())
@@ -26,9 +26,8 @@ fun quaternion(r: Number = 0, v: Vector3D = vector3DOfZero()) =
2626
2727fun Transformation.toPose2D (): Pose2D {
2828 require(dim == 2 ) { " 2d transformation is required" }
29- val p = invoke(vector2DOfZero()).to2D()
30- val d = invokeLinear(.0 .toRad().toVector()).to2D().toAngle()
31- return Pose2D (p, d)
29+ return Pose2D (vector2DOf(matrix[0 , 2 ], matrix[1 , 2 ]),
30+ atan2(matrix[0 , 0 ], matrix[1 , 0 ]).toRad())
3231}
3332
3433fun Pose2D.toTransformation (): Transformation {
@@ -42,38 +41,6 @@ fun Pose2D.toTransformation(): Transformation {
4241 })
4342}
4443
45- fun Transformation.toPose3D (): Pose3D {
46- require(dim == 3 ) { " 3d transformation is required" }
47- val p = invoke(vector3DOfZero()).to3D()
48- TODO ()
49- }
50-
51- fun Pose3D.toTransformation (): Transformation {
52- val (x, y, z) = p
53- val half = theta.asRadian() / 2
54- val a = cos(half)
55- val (b, c, d) = u * sin(half)
56-
57- val ab = a * b
58- val ac = a * c
59- val ad = a * d
60- val bc = b * c
61- val bd = b * d
62- val cd = c * d
63-
64- val b2 = b * b
65- val c2 = c * c
66- val d2 = d * d
67-
68- return Transformation (
69- matrix {
70- row(1 - 2 * c2 - 2 * d2, 2 * bc - 2 * ad, 2 * ac + 2 * bd, x)
71- row(2 * bc + 2 * ad, 1 - 2 * b2 - 2 * d2, 2 * cd - 2 * ab, y)
72- row(2 * bd - 2 * ac, 2 * ab + 2 * cd, 1 - 2 * b2 - 2 * c2, z)
73- row(0 , 0 , 0 , 1 )
74- })
75- }
76-
7744fun Pose2D.to3D (): Pose3D =
7845 Pose3D (p = vector3DOf(p.x, p.y, 0 ),
7946 d = vector3DOf(0 , 0 , d.asRadian() / 2 ))
0 commit comments