@wandelbots/wandelbots-js-react-components 2.26.0-pr.feature-update-to-react-19.361.c04839a → 2.26.0-pr.feature-update-to-react-19.361.fd043dc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wandelbots/wandelbots-js-react-components",
3
- "version": "2.26.0-pr.feature-update-to-react-19.361.c04839a",
3
+ "version": "2.26.0-pr.feature-update-to-react-19.361.fd043dc",
4
4
  "description": "React UI toolkit for building applications on top of the Wandelbots platform",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -1,5 +1,6 @@
1
1
  import { Line } from "@react-three/drei"
2
2
  import type { DHParameter } from "@wandelbots/nova-api/v1"
3
+ import React, { useRef } from "react"
3
4
  import type * as THREE from "three"
4
5
  import { Matrix4, Quaternion, Vector3 } from "three"
5
6
  import type { LineGeometry } from "three/examples/jsm/lines/LineGeometry.js"
@@ -17,6 +18,16 @@ export function DHRobot({
17
18
  // reused in every update
18
19
  const accumulatedMatrix = new Matrix4()
19
20
 
21
+ // Store direct references to avoid searching by name
22
+ const lineRefs = useRef<any[]>([])
23
+ const meshRefs = useRef<(THREE.Mesh | null)[]>([])
24
+
25
+ // Initialize refs array when dhParameters change
26
+ React.useEffect(() => {
27
+ lineRefs.current = new Array(dhParameters.length).fill(null)
28
+ meshRefs.current = new Array(dhParameters.length).fill(null)
29
+ }, [dhParameters.length])
30
+
20
31
  // Updates accumulatedMatrix with every execution
21
32
  // Reset the matrix to identity if you start a new position update
22
33
  function getLinePoints(
@@ -49,7 +60,7 @@ export function DHRobot({
49
60
 
50
61
  function setJointLineRotation(
51
62
  jointIndex: number,
52
- line: THREE.Line,
63
+ line: any, // Use any for drei Line component
53
64
  mesh: THREE.Mesh,
54
65
  jointValue: number,
55
66
  ) {
@@ -71,14 +82,20 @@ export function DHRobot({
71
82
 
72
83
  function setRotation(joints: THREE.Object3D[], jointValues: number[]) {
73
84
  accumulatedMatrix.identity()
74
- joints.forEach((joint, jointIndex) => {
75
- setJointLineRotation(
76
- jointIndex,
77
- joint.getObjectByName(CHILD_LINE) as THREE.Line,
78
- joint.getObjectByName(CHILD_MESH) as THREE.Mesh,
79
- jointValues[jointIndex]!,
80
- )
81
- })
85
+
86
+ // Use direct refs instead of searching by name
87
+ for (
88
+ let jointIndex = 0;
89
+ jointIndex < Math.min(joints.length, jointValues.length);
90
+ jointIndex++
91
+ ) {
92
+ const line = lineRefs.current[jointIndex]
93
+ const mesh = meshRefs.current[jointIndex]
94
+
95
+ if (line && mesh) {
96
+ setJointLineRotation(jointIndex, line, mesh, jointValues[jointIndex]!)
97
+ }
98
+ }
82
99
  }
83
100
 
84
101
  return (
@@ -103,12 +120,22 @@ export function DHRobot({
103
120
  return (
104
121
  <group name={jointName} key={jointName}>
105
122
  <Line
123
+ ref={(ref) => {
124
+ lineRefs.current[index] = ref
125
+ }}
106
126
  name={CHILD_LINE}
107
127
  points={[a, b]}
108
128
  color={"white"}
109
129
  lineWidth={5}
110
130
  />
111
- <mesh name={CHILD_MESH} key={"mesh_" + index} position={b}>
131
+ <mesh
132
+ ref={(ref) => {
133
+ meshRefs.current[index] = ref
134
+ }}
135
+ name={CHILD_MESH}
136
+ key={"mesh_" + index}
137
+ position={b}
138
+ >
112
139
  <sphereGeometry args={[0.01, 32, 32]} />
113
140
  <meshStandardMaterial color={"black"} depthTest={true} />
114
141
  </mesh>
@@ -27,18 +27,6 @@ export default function RobotAnimator({
27
27
  const jointObjects = useRef<Object3D[]>([])
28
28
  const { invalidate } = useThree()
29
29
 
30
- function isRobotFullyInitialized(): boolean {
31
- return (
32
- jointObjects.current.length > 0 &&
33
- jointObjects.current.every(
34
- (joint): joint is Object3D => joint !== undefined && joint !== null,
35
- ) &&
36
- typedAxisValues !== undefined &&
37
- dhParameters.length === jointObjects.current.length &&
38
- dhParameters.every((param): param is DHParameter => param !== undefined)
39
- )
40
- }
41
-
42
30
  function setGroupRef(group: Group | null) {
43
31
  if (!group) return
44
32
 
@@ -67,14 +55,20 @@ export default function RobotAnimator({
67
55
  )
68
56
 
69
57
  if (onRotationChanged) {
70
- // Filter out undefined/null joints before passing to callback
71
- const validJoints = jointObjects.current.filter((joint) => joint != null)
72
- const validJointValues = validJoints.map(
73
- (_, index) => updatedJointValues[index] || 0,
58
+ // Since we've validated everything, all joints should be valid
59
+ // But let's still be defensive and only pass valid joints
60
+ const validJoints = jointObjects.current.filter(
61
+ (joint): joint is Object3D => joint != null,
74
62
  )
75
63
 
76
- if (validJoints.length > 0) {
77
- onRotationChanged(validJoints, validJointValues)
64
+ if (validJoints.length === jointObjects.current.length) {
65
+ // All joints are valid, pass them with corresponding values
66
+ onRotationChanged(validJoints, updatedJointValues)
67
+ } else {
68
+ // Some joints are invalid, don't call the callback
69
+ console.warn(
70
+ "RobotAnimator: Some joints are invalid, skipping onRotationChanged callback",
71
+ )
78
72
  }
79
73
  } else {
80
74
  for (const [index, object] of jointObjects.current.entries()) {
@@ -117,5 +111,17 @@ export default function RobotAnimator({
117
111
  // Type the spring values properly
118
112
  const typedAxisValues = axisValues as SpringValues<Record<string, number>>
119
113
 
114
+ function isRobotFullyInitialized(): boolean {
115
+ return (
116
+ jointObjects.current.length > 0 &&
117
+ jointObjects.current.every(
118
+ (joint): joint is Object3D => joint !== undefined && joint !== null,
119
+ ) &&
120
+ typedAxisValues !== undefined &&
121
+ dhParameters.length === jointObjects.current.length &&
122
+ dhParameters.every((param): param is DHParameter => param !== undefined)
123
+ )
124
+ }
125
+
120
126
  return <group ref={setGroupRef}>{children}</group>
121
127
  }