|
1 |
| -import { GUI } from "dat.gui"; |
2 |
| -import { Color } from "three"; |
3 |
| -import { URDFJoint } from "urdf-loader"; |
| 1 | +import { GUI } from 'dat.gui'; |
| 2 | +import { Color } from 'three'; |
| 3 | +import { URDFJoint } from 'urdf-loader'; |
4 | 4 |
|
5 | 5 | interface Joints {
|
6 |
| - [name: string]: URDFJoint |
| 6 | + [name: string]: URDFJoint; |
7 | 7 | }
|
8 | 8 |
|
9 | 9 | export class URDFControls extends GUI {
|
10 |
| - |
11 |
| - private _workspaceFolder: any; |
12 |
| - private _sceneFolder: any; |
13 |
| - private _jointsFolder: any; |
14 |
| - private _workingPath: string = ''; |
15 |
| - |
16 |
| - controls: any = { |
17 |
| - path: {}, |
18 |
| - scene: { |
19 |
| - background: {}, |
20 |
| - grid: {}, |
21 |
| - height: {} |
22 |
| - }, |
23 |
| - joints: {} |
24 |
| - }; |
25 |
| - |
26 |
| - constructor() { |
27 |
| - super({autoPlace: false}); |
28 |
| - |
29 |
| - this.width = 310; |
30 |
| - this.domElement.style.position = 'absolute'; |
31 |
| - this.domElement.style.top = '0'; |
32 |
| - this.domElement.style.right = '0'; |
33 |
| - |
34 |
| - this._workspaceFolder = this.addFolder('Workspace'); |
35 |
| - this._sceneFolder = this.addFolder('Scene'); |
36 |
| - this._jointsFolder = this.addFolder('Joints'); |
37 |
| - } |
38 |
| - |
39 |
| - get workspaceFolder() { |
40 |
| - return this._workspaceFolder; |
41 |
| - } |
42 |
| - |
43 |
| - get sceneFolder() { |
44 |
| - return this._sceneFolder; |
45 |
| - } |
46 |
| - |
47 |
| - get jointsFolder() { |
48 |
| - return this._jointsFolder; |
| 10 | + private _workspaceFolder: any; |
| 11 | + private _sceneFolder: any; |
| 12 | + private _jointsFolder: any; |
| 13 | + private _workingPath: string = ''; |
| 14 | + |
| 15 | + controls: any = { |
| 16 | + path: {}, |
| 17 | + scene: { |
| 18 | + background: {}, |
| 19 | + grid: {}, |
| 20 | + height: {} |
| 21 | + }, |
| 22 | + joints: {} |
| 23 | + }; |
| 24 | + |
| 25 | + constructor() { |
| 26 | + super({ autoPlace: false }); |
| 27 | + |
| 28 | + this.width = 310; |
| 29 | + this.domElement.style.position = 'absolute'; |
| 30 | + this.domElement.style.top = '0'; |
| 31 | + this.domElement.style.right = '0'; |
| 32 | + |
| 33 | + this._workspaceFolder = this.addFolder('Workspace'); |
| 34 | + this._sceneFolder = this.addFolder('Scene'); |
| 35 | + this._jointsFolder = this.addFolder('Joints'); |
| 36 | + } |
| 37 | + |
| 38 | + get workspaceFolder() { |
| 39 | + return this._workspaceFolder; |
| 40 | + } |
| 41 | + |
| 42 | + get sceneFolder() { |
| 43 | + return this._sceneFolder; |
| 44 | + } |
| 45 | + |
| 46 | + get jointsFolder() { |
| 47 | + return this._jointsFolder; |
| 48 | + } |
| 49 | + |
| 50 | + private _isEmpty(obj: Object): Boolean { |
| 51 | + return Object.keys(obj).length === 0; |
| 52 | + } |
| 53 | + |
| 54 | + createWorkspaceControls(workingPath: string = '') { |
| 55 | + if (this._isEmpty(this.controls.path)) { |
| 56 | + this._workingPath = workingPath; |
| 57 | + const workspaceSettings = { |
| 58 | + Path: this._workingPath, |
| 59 | + 'set path': () => {} |
| 60 | + }; |
| 61 | + this._workspaceFolder.add(workspaceSettings, 'Path'); |
| 62 | + this.controls.path = this._workspaceFolder.add( |
| 63 | + workspaceSettings, |
| 64 | + 'set path' |
| 65 | + ); |
| 66 | + this._workspaceFolder.open(); |
49 | 67 | }
|
50 |
| - |
51 |
| - private _isEmpty(obj: Object): Boolean { |
52 |
| - return Object.keys(obj).length === 0; |
| 68 | + return this.controls.path; |
| 69 | + } |
| 70 | + |
| 71 | + createSceneControls( |
| 72 | + bgColor = new Color(0x263238), |
| 73 | + gridColor = new Color(0x263238) |
| 74 | + ) { |
| 75 | + if (this._isEmpty(this.controls.scene.background)) { |
| 76 | + const sceneSettings = { |
| 77 | + Background: this._convertColor2Array(bgColor), |
| 78 | + Grid: this._convertColor2Array(gridColor), |
| 79 | + Height: 0 |
| 80 | + }; |
| 81 | + |
| 82 | + this.controls.scene.background = this._sceneFolder.addColor( |
| 83 | + sceneSettings, |
| 84 | + 'Background' |
| 85 | + ); |
| 86 | + this.controls.scene.grid = this._sceneFolder.addColor( |
| 87 | + sceneSettings, |
| 88 | + 'Grid' |
| 89 | + ); |
| 90 | + |
| 91 | + const minHeight = -2; |
| 92 | + const maxHeight = 5; |
| 93 | + const stepSize = 0.1; |
| 94 | + this.controls.scene.height = this._sceneFolder.add( |
| 95 | + sceneSettings, |
| 96 | + 'Height', |
| 97 | + minHeight, |
| 98 | + maxHeight, |
| 99 | + stepSize |
| 100 | + ); |
| 101 | + |
| 102 | + this._sceneFolder.open(); |
53 | 103 | }
|
54 |
| - |
55 |
| - createWorkspaceControls(workingPath: string = '') { |
56 |
| - if (this._isEmpty(this.controls.path)) { |
57 |
| - this._workingPath = workingPath; |
58 |
| - const workspaceSettings = { |
59 |
| - 'Path': this._workingPath, |
60 |
| - 'set path': () => {} |
61 |
| - } |
62 |
| - this._workspaceFolder.add(workspaceSettings, 'Path'); |
63 |
| - this.controls.path = this._workspaceFolder.add(workspaceSettings, 'set path'); |
64 |
| - this._workspaceFolder.open(); |
| 104 | + return this.controls.scene; |
| 105 | + } |
| 106 | + |
| 107 | + private _convertColor2Array(color: THREE.Color): number[] { |
| 108 | + // Note: using hex value instead of the RGB values in Color because |
| 109 | + // those are dependant on the color space |
| 110 | + const hexColor = color.getHexString(); |
| 111 | + const colorArray = [ |
| 112 | + parseInt(hexColor.slice(0, 2), 16), |
| 113 | + parseInt(hexColor.slice(2, 4), 16), |
| 114 | + parseInt(hexColor.slice(4, 6), 16) |
| 115 | + ]; |
| 116 | + return colorArray; |
| 117 | + } |
| 118 | + |
| 119 | + createJointControls(joints: Joints) { |
| 120 | + if (this._isEmpty(this.controls.joints)) { |
| 121 | + Object.keys(joints).forEach((name: string) => { |
| 122 | + // Skip joints which should not be moved |
| 123 | + if (joints[name].jointType === 'fixed') { |
| 124 | + return; |
65 | 125 | }
|
66 |
| - return this.controls.path; |
67 |
| - } |
68 | 126 |
|
69 |
| - createSceneControls( |
70 |
| - bgColor = new Color(0x263238), |
71 |
| - gridColor = new Color(0x263238) |
72 |
| - ) { |
73 |
| - if (this._isEmpty(this.controls.scene.background)) { |
74 |
| - const sceneSettings = { |
75 |
| - 'Background': this._convertColor2Array(bgColor), |
76 |
| - 'Grid': this._convertColor2Array(gridColor), |
77 |
| - 'Height': 0 |
78 |
| - }; |
79 |
| - |
80 |
| - this.controls.scene.background = this._sceneFolder.addColor(sceneSettings, 'Background'); |
81 |
| - this.controls.scene.grid = this._sceneFolder.addColor(sceneSettings, 'Grid'); |
82 |
| - |
83 |
| - const minHeight = -2; |
84 |
| - const maxHeight = 5; |
85 |
| - const stepSize = 0.1; |
86 |
| - this.controls.scene.height = this._sceneFolder.add( |
87 |
| - sceneSettings, 'Height', minHeight, maxHeight, stepSize); |
88 |
| - |
89 |
| - this._sceneFolder.open(); |
90 |
| - } |
91 |
| - return this.controls.scene; |
92 |
| - } |
| 127 | + const limitMin = Number(joints[name].limit.lower); |
| 128 | + const limitMax = Number(joints[name].limit.upper); |
93 | 129 |
|
94 |
| - private _convertColor2Array(color: THREE.Color): number[] { |
95 |
| - // Note: using hex value instead of the RGB values in Color because |
96 |
| - // those are dependant on the color space |
97 |
| - const hexColor = color.getHexString(); |
98 |
| - const colorArray = [ |
99 |
| - parseInt(hexColor.slice(0,2), 16), |
100 |
| - parseInt(hexColor.slice(2,4), 16), |
101 |
| - parseInt(hexColor.slice(4,6), 16) |
102 |
| - ] |
103 |
| - return colorArray; |
104 |
| - } |
105 |
| - |
106 |
| - createJointControls(joints: Joints) { |
107 |
| - if (this._isEmpty(this.controls.joints)) { |
108 |
| - Object.keys(joints).forEach((name: string) => { |
109 |
| - // Skip joints which should not be moved |
110 |
| - if (joints[name].jointType === 'fixed') { |
111 |
| - return; |
112 |
| - } |
113 |
| - |
114 |
| - const limitMin = Number(joints[name].limit.lower); |
115 |
| - const limitMax = Number(joints[name].limit.upper); |
116 |
| - |
117 |
| - // Skip joint if the limits are not defined |
118 |
| - if ( limitMin === 0 && limitMax === 0 ) { |
119 |
| - return; |
120 |
| - } |
121 |
| - |
122 |
| - const stepSize = ( limitMax - limitMin ) / 20; |
123 |
| - const initValue = joints[name].jointValue[0]; |
124 |
| - |
125 |
| - this.controls.joints[name] = this._jointsFolder.add( |
126 |
| - {[name]: initValue}, name, limitMin, limitMax, stepSize); |
127 |
| - }); |
128 |
| - this._jointsFolder.open(); |
| 130 | + // Skip joint if the limits are not defined |
| 131 | + if (limitMin === 0 && limitMax === 0) { |
| 132 | + return; |
129 | 133 | }
|
130 |
| - return this.controls.joints; |
| 134 | + |
| 135 | + const stepSize = (limitMax - limitMin) / 20; |
| 136 | + const initValue = joints[name].jointValue[0]; |
| 137 | + |
| 138 | + this.controls.joints[name] = this._jointsFolder.add( |
| 139 | + { [name]: initValue }, |
| 140 | + name, |
| 141 | + limitMin, |
| 142 | + limitMax, |
| 143 | + stepSize |
| 144 | + ); |
| 145 | + }); |
| 146 | + this._jointsFolder.open(); |
131 | 147 | }
|
| 148 | + return this.controls.joints; |
| 149 | + } |
132 | 150 | }
|
0 commit comments