Skip to content

Commit 9a14281

Browse files
committed
实现并测试二维和三维位姿
1 parent c54d1d9 commit 9a14281

6 files changed

Lines changed: 14 additions & 53 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
}
99

1010
group = "org.mechdancer"
11-
version = "0.2.6-dev-2"
11+
version = "0.2.7-dev-1"
1212

1313
repositories {
1414
mavenCentral()

src/main/kotlin/org/mechdancer/geometry/rotation3d/Angle3D.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,5 +461,4 @@ data class Angle3D(val first: Angle, val second: Angle, val third: Angle, val ax
461461

462462
override fun equals(other: Any?): Boolean =
463463
other is Angle3D && other.matrix == matrix
464-
465-
}
464+
}

src/main/kotlin/org/mechdancer/geometry/rotation3d/AxesOrder.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,4 @@ enum class AxesOrder(val first: Int, val second: Int, val third: Int) {
2727
values().find { it.first == f && it.second == s && it.third == t }
2828
?: throw IllegalArgumentException()
2929
}
30-
31-
}
30+
}
Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.mechdancer.geometry.transformation
22

3-
import org.mechdancer.algebra.function.vector.times
43
import 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
78
import org.mechdancer.geometry.angle.toRad
89
import org.mechdancer.geometry.angle.toVector
9-
import kotlin.math.cos
10-
import kotlin.math.sin
10+
import kotlin.math.atan2
1111

1212
fun 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

2727
fun 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

3433
fun 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-
7744
fun Pose2D.to3D(): Pose3D =
7845
Pose3D(p = vector3DOf(p.x, p.y, 0),
7946
d = vector3DOf(0, 0, d.asRadian() / 2))

src/main/kotlin/org/mechdancer/geometry/transformation/Pose3D.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ data class Pose3D(
5757
q1 * q0.conjugate)
5858
}
5959

60-
override fun toString() = "p = ${p.simpleString()}, u = ${u.simpleString()}, θ = $theta"
60+
override fun toString() = "p = ${p.simpleView()}, u = ${u.simpleView()}, θ = $theta"
6161

6262
private companion object {
63-
fun Vector3D.simpleString() = "($x, $y, $z)"
63+
fun Vector3D.simpleView() = "($x, $y, $z)"
6464

6565
fun Pose3D.toQuaternions(): Pair<Quaternion, Quaternion> {
6666
val (x, y, z) = p
@@ -71,9 +71,7 @@ data class Pose3D(
7171

7272
fun pose3D(p: Quaternion, d: Quaternion): Pose3D {
7373
assert(doubleEquals(p.r, .0))
74-
val u = d.v
75-
val half = atan2(u.length, d.r)
76-
return Pose3D(p.v, u.normalize() * half)
74+
return Pose3D(p.v, d.v.run { normalize() * atan2(length, d.r) })
7775
}
7876
}
7977
}

src/test/kotlin/org/mechdancer/geometry/transformation/TestPose3D.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import org.mechdancer.geometry.angle.toRad
77
import kotlin.math.PI
88

99
class TestPose3D {
10-
/**
11-
* 测试里程计算法
12-
*/
10+
/** 测试三维里程算法 */
1311
@Test
1412
fun test() {
1513
val step0 = pose3D()

0 commit comments

Comments
 (0)