@wandelbots/wandelbots-js-react-components 2.26.0-pr.feature-update-to-react-19.361.e3316bb → 2.26.0-pr.feature-update-to-react-19.361.0d1640d

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.e3316bb",
3
+ "version": "2.26.0-pr.feature-update-to-react-19.361.0d1640d",
4
4
  "description": "React UI toolkit for building applications on top of the Wandelbots platform",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -52,7 +52,6 @@
52
52
  "@emotion/styled": "^11.14.0",
53
53
  "@mui/icons-material": "^7.1.1",
54
54
  "@mui/material": "^7.1.1",
55
- "@react-spring/three": "10.0.0",
56
55
  "@react-three/drei": "^10.2.0",
57
56
  "@react-three/fiber": "^9.1.2",
58
57
  "@rollup/plugin-commonjs": "^28.0.2",
@@ -2,9 +2,22 @@ import type {
2
2
  DHParameter,
3
3
  MotionGroupStateResponse,
4
4
  } from "@wandelbots/nova-api/v1"
5
- import { describe, expect, it } from "vitest"
5
+ import { describe, expect, it, vi } from "vitest"
6
6
  import RobotAnimator from "./RobotAnimator"
7
7
 
8
+ // Mock the dependencies
9
+ vi.mock("./robotModelLogic", () => ({
10
+ collectJoints: vi.fn(),
11
+ }))
12
+
13
+ vi.mock("../utils/interpolation", () => ({
14
+ ValueInterpolator: vi.fn().mockImplementation(() => ({
15
+ setTarget: vi.fn(),
16
+ getCurrentValues: vi.fn(() => []),
17
+ destroy: vi.fn(),
18
+ })),
19
+ }))
20
+
8
21
  describe("RobotAnimator", () => {
9
22
  it("should export the component correctly", () => {
10
23
  expect(RobotAnimator).toBeDefined()
@@ -68,66 +81,33 @@ describe("RobotAnimator", () => {
68
81
  }).not.toThrow()
69
82
  })
70
83
 
71
- it("should verify dynamic joint configuration logic", () => {
72
- // Test the logic that would create dynamic spring configs
73
- const testConfigs = [
74
- { jointCount: 3, expectedJoints: ["joint0", "joint1", "joint2"] },
75
- {
76
- jointCount: 6,
77
- expectedJoints: [
78
- "joint0",
79
- "joint1",
80
- "joint2",
81
- "joint3",
82
- "joint4",
83
- "joint5",
84
- ],
85
- },
86
- {
87
- jointCount: 8,
88
- expectedJoints: [
89
- "joint0",
90
- "joint1",
91
- "joint2",
92
- "joint3",
93
- "joint4",
94
- "joint5",
95
- "joint6",
96
- "joint7",
97
- ],
98
- },
99
- ]
84
+ it("should use custom interpolation for smooth value transitions", () => {
85
+ // Test the rotation calculation logic
86
+ const dhParam1 = { theta: 0.1, reverse_rotation_direction: false }
87
+ const dhParam2 = { theta: -0.2, reverse_rotation_direction: true }
88
+ const jointValue1 = 0.5
89
+ const jointValue2 = 1.0
100
90
 
101
- testConfigs.forEach(({ jointCount, expectedJoints }) => {
102
- // Simulate the logic from the component
103
- const dhParameters = Array(jointCount).fill({
104
- theta: 0,
105
- reverse_rotation_direction: false,
106
- })
107
- const jointValues = Array(jointCount)
108
- .fill(0)
109
- .map((_, i) => i * 0.1)
110
-
111
- const config: any = {
112
- config: { tension: 120, friction: 20 },
113
- onChange: () => {},
114
- }
91
+ // Calculate expected rotations
92
+ const expectedRotation1 = 1 * jointValue1 + 0.1 // 0.6
93
+ const expectedRotation2 = -1 * jointValue2 + -0.2 // -1.2
115
94
 
116
- dhParameters.forEach((_, index) => {
117
- config[`joint${index}`] = jointValues[index] || 0
118
- })
119
-
120
- // Verify the configuration has the expected joint properties
121
- expectedJoints.forEach((jointKey) => {
122
- expect(config[jointKey]).toBeDefined()
123
- expect(typeof config[jointKey]).toBe("number")
124
- })
125
-
126
- // Verify no extra joints were added
127
- const actualJointKeys = Object.keys(config).filter((key) =>
128
- key.startsWith("joint"),
129
- )
130
- expect(actualJointKeys).toHaveLength(jointCount)
131
- })
95
+ expect(expectedRotation1).toBe(0.6)
96
+ expect(expectedRotation2).toBe(-1.2)
97
+
98
+ // Test edge cases
99
+ const dhParamReverse = { theta: 0, reverse_rotation_direction: true }
100
+ const jointValueZero = 0
101
+ const expectedRotationReverse = -1 * jointValueZero + 0 // 0
102
+
103
+ expect(expectedRotationReverse).toBe(0)
104
+
105
+ // Test rotation direction logic
106
+ const testValue = 1.5
107
+ const normalParam = { theta: 0, reverse_rotation_direction: false }
108
+ const reversedParam = { theta: 0, reverse_rotation_direction: true }
109
+
110
+ expect(1 * testValue + 0).toBe(1.5) // Normal direction
111
+ expect(-1 * testValue + 0).toBe(-1.5) // Reversed direction
132
112
  })
133
113
  })
@@ -1,11 +1,12 @@
1
- import { Globals, useSpring } from "@react-spring/three"
2
1
  import { useThree } from "@react-three/fiber"
3
2
  import type {
4
3
  DHParameter,
5
4
  MotionGroupStateResponse,
6
5
  } from "@wandelbots/nova-api/v1"
7
- import React, { useMemo, useRef } from "react"
6
+ import React, { useEffect, useRef } from "react"
8
7
  import type { Group, Object3D } from "three"
8
+ import { useAutorun } from "../utils/hooks"
9
+ import { ValueInterpolator } from "../utils/interpolation"
9
10
  import { collectJoints } from "./robotModelLogic"
10
11
 
11
12
  type RobotAnimatorProps = {
@@ -21,59 +22,53 @@ export default function RobotAnimator({
21
22
  onRotationChanged,
22
23
  children,
23
24
  }: RobotAnimatorProps) {
24
- Globals.assign({ frameLoop: "always" })
25
+ const jointValues = useRef<number[]>([])
25
26
  const jointObjects = useRef<Object3D[]>([])
27
+ const interpolatorRef = useRef<ValueInterpolator | null>(null)
26
28
  const { invalidate } = useThree()
27
29
 
28
- function setGroupRef(group: Group | null) {
29
- if (!group) return
30
-
31
- jointObjects.current = collectJoints(group)
32
-
33
- // Set initial position
34
- setRotation()
35
- invalidate()
36
- }
37
-
38
- // Extract joint values
39
- const jointValues = useMemo(
40
- () =>
30
+ // Initialize interpolator
31
+ useEffect(() => {
32
+ const initialJointValues =
41
33
  rapidlyChangingMotionState.state.joint_position.joints.filter(
42
34
  (item) => item !== undefined,
43
- ),
44
- [rapidlyChangingMotionState.state.joint_position.joints],
45
- )
35
+ )
46
36
 
47
- // Create dynamic spring configuration based on actual number of joints
48
- const springConfig = useMemo(() => {
49
- const config: any = {
50
- config: {
51
- tension: 120,
52
- friction: 20,
53
- },
37
+ interpolatorRef.current = new ValueInterpolator(initialJointValues, {
38
+ speed: 0.15, // Smooth interpolation speed
39
+ easing: "spring", // Spring-like animation to match the previous feel
40
+ threshold: 0.001,
54
41
  onChange: () => {
55
- // This is critical: trigger setRotation when spring values change
56
42
  setRotation()
57
43
  invalidate()
58
44
  },
45
+ onComplete: () => {
46
+ setRotation()
47
+ },
48
+ })
49
+
50
+ return () => {
51
+ interpolatorRef.current?.destroy()
59
52
  }
53
+ }, [])
60
54
 
61
- // Add joint values dynamically based on dhParameters length
62
- dhParameters.forEach((_, index) => {
63
- config[`joint${index}`] = jointValues[index] || 0
64
- })
55
+ function setGroupRef(group: Group | null) {
56
+ if (!group) return
65
57
 
66
- return config
67
- }, [jointValues, dhParameters.length])
58
+ jointObjects.current = collectJoints(group)
68
59
 
69
- const axisValues = useSpring(springConfig)
60
+ // Set initial position
61
+ setRotation()
62
+ invalidate()
63
+ }
64
+
65
+ function updateJoints(newJointValues: number[]) {
66
+ jointValues.current = newJointValues
67
+ interpolatorRef.current?.setTarget(newJointValues)
68
+ }
70
69
 
71
- // Update setRotation to use the dynamic spring values
72
70
  function setRotation() {
73
- const updatedJointValues = dhParameters.map((_, index) => {
74
- const springValue = (axisValues as any)[`joint${index}`]
75
- return springValue ? springValue.get() : 0
76
- })
71
+ const updatedJointValues = interpolatorRef.current?.getCurrentValues() || []
77
72
 
78
73
  if (onRotationChanged) {
79
74
  onRotationChanged(jointObjects.current, updatedJointValues)
@@ -84,10 +79,19 @@ export default function RobotAnimator({
84
79
  const rotationSign = dhParam.reverse_rotation_direction ? -1 : 1
85
80
 
86
81
  object.rotation.y =
87
- rotationSign * updatedJointValues[index]! + rotationOffset
82
+ rotationSign * (updatedJointValues[index] || 0) + rotationOffset
88
83
  }
89
84
  }
90
85
  }
91
86
 
87
+ useAutorun(() => {
88
+ const newJointValues =
89
+ rapidlyChangingMotionState.state.joint_position.joints.filter(
90
+ (item) => item !== undefined,
91
+ )
92
+
93
+ requestAnimationFrame(() => updateJoints(newJointValues))
94
+ })
95
+
92
96
  return <group ref={setGroupRef}>{children}</group>
93
97
  }
@@ -0,0 +1,189 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
2
+ import { ValueInterpolator } from "./interpolation"
3
+
4
+ describe("ValueInterpolator", () => {
5
+ let interpolator: ValueInterpolator
6
+ let mockOnChange: ReturnType<typeof vi.fn>
7
+ let mockOnComplete: ReturnType<typeof vi.fn>
8
+
9
+ beforeEach(() => {
10
+ mockOnChange = vi.fn()
11
+ mockOnComplete = vi.fn()
12
+
13
+ // Mock requestAnimationFrame for testing
14
+ vi.stubGlobal("requestAnimationFrame", (cb: FrameRequestCallback) => {
15
+ return setTimeout(cb, 16) // Simulate 60fps
16
+ })
17
+
18
+ vi.stubGlobal("cancelAnimationFrame", (id: number) => {
19
+ clearTimeout(id)
20
+ })
21
+ })
22
+
23
+ afterEach(() => {
24
+ interpolator?.destroy()
25
+ vi.restoreAllMocks()
26
+ vi.unstubAllGlobals()
27
+ })
28
+
29
+ it("should handle frequent target updates without losing interpolation quality", async () => {
30
+ interpolator = new ValueInterpolator([0, 0, 0], {
31
+ speed: 0.2,
32
+ easing: "spring",
33
+ threshold: 0.001,
34
+ onChange: mockOnChange,
35
+ onComplete: mockOnComplete,
36
+ })
37
+
38
+ // Simulate rapid updates (like robot motion state changes)
39
+ const rapidUpdates = [
40
+ [1, 2, 3],
41
+ [1.1, 2.1, 3.1],
42
+ [1.2, 2.2, 3.2],
43
+ [1.3, 2.3, 3.3],
44
+ [1.5, 2.5, 3.5], // Final target
45
+ ]
46
+
47
+ // Apply updates rapidly (faster than animation frames)
48
+ rapidUpdates.forEach((values, index) => {
49
+ setTimeout(() => {
50
+ interpolator.setTarget(values)
51
+ }, index * 5) // 5ms intervals (much faster than 16ms frame rate)
52
+ })
53
+
54
+ // Wait for interpolation to settle
55
+ await new Promise((resolve) => setTimeout(resolve, 200))
56
+
57
+ // Verify interpolation handled rapid updates
58
+ expect(mockOnChange).toHaveBeenCalled()
59
+
60
+ // Final values should be close to the last target (spring may not reach exactly)
61
+ const finalValues = interpolator.getCurrentValues()
62
+ expect(finalValues[0]).toBeCloseTo(1.5, 0) // More lenient tolerance for spring
63
+ expect(finalValues[1]).toBeCloseTo(2.5, 0)
64
+ expect(finalValues[2]).toBeCloseTo(3.5, 0)
65
+ })
66
+
67
+ it("should provide smooth spring-like animation characteristics", async () => {
68
+ const changeHistory: number[][] = []
69
+
70
+ interpolator = new ValueInterpolator([0], {
71
+ speed: 0.15,
72
+ easing: "spring",
73
+ threshold: 0.001,
74
+ onChange: (values) => {
75
+ changeHistory.push([...values])
76
+ },
77
+ })
78
+
79
+ interpolator.setTarget([10])
80
+
81
+ // Wait for animation to complete
82
+ await new Promise((resolve) => setTimeout(resolve, 300))
83
+
84
+ // Verify we have smooth progression
85
+ expect(changeHistory.length).toBeGreaterThan(5) // Multiple animation steps
86
+
87
+ // Spring animation should show progression toward target
88
+ const firstValue = changeHistory[0][0]
89
+ const lastValue = changeHistory[changeHistory.length - 1][0]
90
+ const midValue = changeHistory[Math.floor(changeHistory.length / 2)][0]
91
+
92
+ expect(firstValue).toBeLessThan(midValue)
93
+ expect(midValue).toBeLessThan(lastValue)
94
+ expect(lastValue).toBeCloseTo(10, 0) // More lenient for spring animation
95
+ })
96
+
97
+ it("should handle different easing functions correctly", () => {
98
+ const linearInterpolator = new ValueInterpolator([0], {
99
+ speed: 0.5,
100
+ easing: "linear",
101
+ })
102
+
103
+ const springInterpolator = new ValueInterpolator([0], {
104
+ speed: 0.5,
105
+ easing: "spring",
106
+ })
107
+
108
+ const easeOutInterpolator = new ValueInterpolator([0], {
109
+ speed: 0.5,
110
+ easing: "easeOut",
111
+ })
112
+
113
+ // Test that different easing functions produce different intermediate values
114
+ linearInterpolator.setTarget([10])
115
+ springInterpolator.setTarget([10])
116
+ easeOutInterpolator.setTarget([10])
117
+
118
+ // After one step, values should be different due to easing
119
+ const linearValue = linearInterpolator.getCurrentValues()[0]
120
+ const springValue = springInterpolator.getCurrentValues()[0]
121
+ const easeOutValue = easeOutInterpolator.getCurrentValues()[0]
122
+
123
+ // All should be moving toward target but with different characteristics
124
+ expect(linearValue).toBeGreaterThan(0)
125
+ expect(springValue).toBeGreaterThan(0)
126
+ expect(easeOutValue).toBeGreaterThan(0)
127
+
128
+ // Spring should be more aggressive initially (due to 1.5x multiplier)
129
+ expect(springValue).toBeGreaterThanOrEqual(linearValue)
130
+
131
+ linearInterpolator.destroy()
132
+ springInterpolator.destroy()
133
+ easeOutInterpolator.destroy()
134
+ })
135
+
136
+ it("should handle array length changes gracefully", () => {
137
+ interpolator = new ValueInterpolator([1, 2], {
138
+ speed: 0.5,
139
+ onChange: mockOnChange,
140
+ })
141
+
142
+ // Expand array
143
+ interpolator.setTarget([1, 2, 3, 4])
144
+ expect(interpolator.getCurrentValues()).toHaveLength(4)
145
+
146
+ // Shrink array
147
+ interpolator.setTarget([5, 6])
148
+ expect(interpolator.getCurrentValues()).toHaveLength(2)
149
+
150
+ // Values should be updated
151
+ const values = interpolator.getCurrentValues()
152
+ expect(values[0]).not.toBe(1) // Should be interpolating toward 5
153
+ expect(values[1]).not.toBe(2) // Should be interpolating toward 6
154
+ })
155
+
156
+ it("should stop and start interpolation correctly", async () => {
157
+ interpolator = new ValueInterpolator([0], {
158
+ speed: 0.1, // Slow for testing
159
+ onChange: mockOnChange,
160
+ })
161
+
162
+ interpolator.setTarget([10])
163
+ expect(interpolator.isInterpolating()).toBe(true)
164
+
165
+ interpolator.stop()
166
+ expect(interpolator.isInterpolating()).toBe(false)
167
+
168
+ // Give time to ensure no more onChange calls
169
+ const callCountAfterStop = mockOnChange.mock.calls.length
170
+ await new Promise((resolve) => setTimeout(resolve, 50))
171
+ expect(mockOnChange.mock.calls.length).toBe(callCountAfterStop)
172
+
173
+ // Should be able to restart
174
+ interpolator.setTarget([5])
175
+ expect(interpolator.isInterpolating()).toBe(true)
176
+ })
177
+
178
+ it("should handle setImmediate correctly", () => {
179
+ interpolator = new ValueInterpolator([0, 0], {
180
+ onChange: mockOnChange,
181
+ })
182
+
183
+ interpolator.setImmediate([5, 10])
184
+
185
+ expect(interpolator.getCurrentValues()).toEqual([5, 10])
186
+ expect(interpolator.isInterpolating()).toBe(false)
187
+ expect(mockOnChange).toHaveBeenCalledWith([5, 10])
188
+ })
189
+ })