@wandelbots/wandelbots-js-react-components 2.26.0-pr.feature-update-to-react-19.361.edd4b6b → 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.edd4b6b",
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>
@@ -1,4 +1,4 @@
1
- import { Globals, useSpring } from "@react-spring/three"
1
+ import { Globals, useSpring, type SpringValues } from "@react-spring/three"
2
2
  import { useThree } from "@react-three/fiber"
3
3
  import type {
4
4
  DHParameter,
@@ -32,16 +32,10 @@ export default function RobotAnimator({
32
32
 
33
33
  jointObjects.current = collectJoints(group)
34
34
 
35
- // Set initial position only if we have valid joint objects
36
- if (
37
- jointObjects.current.length > 0 &&
38
- jointObjects.current.every((obj) => obj != null)
39
- ) {
40
- // Use requestAnimationFrame to ensure axisValues is ready
41
- requestAnimationFrame(() => {
42
- setRotation()
43
- invalidate()
44
- })
35
+ // Only set initial position if robot is fully initialized
36
+ if (isRobotFullyInitialized()) {
37
+ setRotation()
38
+ invalidate()
45
39
  }
46
40
  }
47
41
 
@@ -50,47 +44,43 @@ export default function RobotAnimator({
50
44
  setSpring.start(Object.assign({}, jointValues.current) as any)
51
45
  }
52
46
 
53
- const [axisValues, setSpring] = useSpring(() => {
54
- const currentJointValues =
55
- rapidlyChangingMotionState.state.joint_position.joints.filter(
56
- (item) => item !== undefined,
57
- )
58
-
59
- return {
60
- ...Object.assign({}, currentJointValues),
61
- onChange: () => {
62
- setRotation()
63
- invalidate()
64
- },
65
- onResolve: () => {
66
- setRotation()
67
- },
68
- }
69
- })
70
-
71
47
  function setRotation() {
72
- // Safety check to prevent race condition
73
- if (
74
- jointObjects.current.length === 0 ||
75
- !jointObjects.current.every((obj) => obj != null)
76
- ) {
48
+ // Use the comprehensive validation function
49
+ if (!isRobotFullyInitialized()) {
77
50
  return
78
51
  }
79
52
 
80
- const updatedJointValues = jointObjects.current.map((_, objectIndex) =>
81
- (axisValues as any)[objectIndex].get(),
53
+ const updatedJointValues = jointObjects.current.map(
54
+ (_, objectIndex) => typedAxisValues[objectIndex.toString()]?.get() || 0,
82
55
  )
83
56
 
84
57
  if (onRotationChanged) {
85
- onRotationChanged(jointObjects.current, updatedJointValues)
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,
62
+ )
63
+
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
+ )
72
+ }
86
73
  } else {
87
74
  for (const [index, object] of jointObjects.current.entries()) {
75
+ // Skip if object or dhParameter is undefined
76
+ if (!object || !dhParameters[index]) continue
77
+
88
78
  const dhParam = dhParameters[index]
89
79
  const rotationOffset = dhParam.theta || 0
90
80
  const rotationSign = dhParam.reverse_rotation_direction ? -1 : 1
91
81
 
92
82
  object.rotation.y =
93
- rotationSign * updatedJointValues[index]! + rotationOffset
83
+ rotationSign * (updatedJointValues[index] || 0) + rotationOffset
94
84
  }
95
85
  }
96
86
  }
@@ -101,33 +91,37 @@ export default function RobotAnimator({
101
91
  (item) => item !== undefined,
102
92
  )
103
93
 
104
- requestAnimationFrame(() => {
105
- // Check if spring values are significantly different from target values
106
- const currentSpringValues = jointObjects.current.map(
107
- (_, index) => (axisValues as any)[index]?.get() || 0,
108
- )
109
-
110
- const maxDifference = Math.max(
111
- ...newJointValues.map((newValue, index) =>
112
- Math.abs(newValue - (currentSpringValues[index] || 0)),
113
- ),
114
- )
115
-
116
- // Be more aggressive about detecting "snap back" situations
117
- // If difference is large (> 0.05 radians ~= 2.9 degrees), reset immediately
118
- // This handles focus restoration and other cases where animation would be jarring
119
- if (maxDifference > 0.05) {
120
- jointValues.current = newJointValues
121
- setSpring.set(Object.assign({}, jointValues.current) as any)
122
- // Force immediate rotation update
123
- setRotation()
124
- invalidate()
125
- } else {
126
- // Normal smooth animation for small changes
127
- updateJoints(newJointValues)
128
- }
129
- })
94
+ requestAnimationFrame(() => updateJoints(newJointValues))
130
95
  })
131
96
 
97
+ const [axisValues, setSpring] = useSpring(() => ({
98
+ ...Object.assign(
99
+ {},
100
+ rapidlyChangingMotionState.state.joint_position.joints,
101
+ ),
102
+ onChange: () => {
103
+ setRotation()
104
+ invalidate()
105
+ },
106
+ onResolve: () => {
107
+ setRotation()
108
+ },
109
+ }))
110
+
111
+ // Type the spring values properly
112
+ const typedAxisValues = axisValues as SpringValues<Record<string, number>>
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
+
132
126
  return <group ref={setGroupRef}>{children}</group>
133
127
  }