File size: 3,978 Bytes
3aea7c6
18b0fa5
 
 
 
3aea7c6
 
 
 
 
 
18b0fa5
3aea7c6
18b0fa5
 
 
3aea7c6
18b0fa5
3aea7c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18b0fa5
 
 
3aea7c6
 
 
 
 
 
 
18b0fa5
3aea7c6
 
 
 
 
 
 
 
18b0fa5
 
 
 
 
 
 
 
 
 
 
 
 
 
3aea7c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca4340e
 
3aea7c6
 
 
 
18b0fa5
3aea7c6
 
 
 
 
 
 
 
 
 
 
 
 
 
ca4340e
3aea7c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca4340e
 
 
 
 
 
 
 
 
 
 
 
 
18b0fa5
ca4340e
 
 
 
 
3aea7c6
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<script lang="ts">
	import type IUrdfJoint from "../interfaces/IUrdfJoint";
	import { T } from "@threlte/core";
	import UrdfLink from "./UrdfLink.svelte";
	import { Vector3 } from "three";
	import {
		Billboard,
		interactivity,
		MeshLineGeometry,
		Text,
		type InteractivityProps
	} from "@threlte/extras";

	import type IUrdfLink from "../interfaces/IUrdfLink";
	import type IUrdfRobot from "../interfaces/IUrdfRobot";
	import { radiansToDegrees } from "@/utils";

	const defaultOnClick: InteractivityProps["onclick"] = (event) => {
		event.stopPropagation();
		selectedLink = undefined;
		selectedJoint = joint;
	};

	type Props = InteractivityProps & {
		robot: IUrdfRobot;
		joint: IUrdfJoint;
		selectedJoint?: IUrdfJoint;
		selectedLink?: IUrdfLink;
		showName?: boolean;
		nameHeight?: number;
		highlightColor?: string;
		jointColor?: string;
		jointIndicatorColor?: string;
		showLine?: boolean;
		opacity?: number;
		isInteractive?: boolean;
		showVisual?: boolean;
		showCollision?: boolean;
		visualOpacity?: number;
		collisionOpacity?: number;
		collisionColor?: string;
		jointNames?: boolean;
		joints?: boolean;
	};

	let {
		robot,
		joint,
		selectedJoint = $bindable(),
		selectedLink = $bindable(),
		showLine = false,
		nameHeight = 0.02,
		highlightColor = "#ff0000",
		jointColor = "#000000",
		jointIndicatorColor = "#000000",
		opacity = 0.7,
		isInteractive = false,
		showName = false,
		showVisual = true,
		showCollision = true,
		visualOpacity = 1,
		collisionOpacity = 1,
		collisionColor = "#000000",
		jointNames = true,
		onclick = defaultOnClick,
		...restProps
	}: Props = $props();

	if (isInteractive) {
		interactivity();
	}

	// Helper function to get current joint rotation value in degrees
	function getJointRotationValue(joint: any): number {
		const axis = joint.axis_xyz || [0, 0, 1];
		const rotation = joint.rotation || [0, 0, 0];

		// Find the primary axis and get the rotation value
		for (let i = 0; i < 3; i++) {
			if (Math.abs(axis[i]) > 0.001) {
				return radiansToDegrees(rotation[i] / axis[i]);
			}
		}
		return 0;
	}
</script>

{@html `<!-- Joint ${joint.name} (${joint.type}) -->`}
<!-- draw line from parent-frame to joint origin -->
{#if showLine}
	<T.Line>
		<MeshLineGeometry
			points={[
				new Vector3(0, 0, 0),
				new Vector3(joint.origin_xyz[0], joint.origin_xyz[1], joint.origin_xyz[2])
			]}
		/>
		<T.LineBasicMaterial color={jointColor} />
	</T.Line>
{/if}

<T.Group position={joint.origin_xyz} rotation={joint.origin_rpy}>
	<T.Group rotation={joint.rotation}>
		{#if joint.child}
			<UrdfLink
				{robot}
				link={joint.child}
				textScale={0.2}
				{showName}
				{showVisual}
				{showCollision}
				{visualOpacity}
				{collisionOpacity}
				{collisionColor}
				jointNames={true}
				joints={true}
				{jointColor}
				{jointIndicatorColor}
				{nameHeight}
				{selectedLink}
				{selectedJoint}
				{highlightColor}
				{showLine}
				opacity={1}
				isInteractive={true}
			/>
		{/if}

		{#if showLine}
			<T.Line>
				<MeshLineGeometry points={[new Vector3(0, 0, 0), new Vector3(0, -0.02, 0)]} />
				<T.LineBasicMaterial color={jointIndicatorColor} />
			</T.Line>

			<T.Mesh rotation={[Math.PI / 2, 0, 0]} {...restProps}>
				<T.CylinderGeometry args={[0.004, 0.004, 0.03]} />
				<T.MeshBasicMaterial
					color={selectedJoint == joint ? highlightColor : jointColor}
					{opacity}
					transparent={opacity < 1.0}
				/>
			</T.Mesh>
		{/if}
	</T.Group>
</T.Group>

{#if showName}
	<Billboard
		position.x={joint.origin_xyz[0] + 0.04}
		position.y={joint.origin_xyz[1] + 0.01}
		position.z={joint.origin_xyz[2] + 0.03}
		renderOrder={999}
		frustumCulled={false}
	>
		<Text
			scale={nameHeight}
			color={selectedJoint == joint ? highlightColor : jointColor}
			text={joint.name + " " + getJointRotationValue(joint).toFixed(0) + "°"}
			characters="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
			renderOrder={999}
		></Text>
	</Billboard>
{/if}

<!-- From https://github.com/brean/urdf-viewer -->