1
1
import { Message } from '@lumino/messaging' ;
2
2
import { PanelLayout , Widget } from '@lumino/widgets' ;
3
+ import { DocumentRegistry , DocumentModel } from '@jupyterlab/docregistry' ;
3
4
4
- import {
5
- DocumentRegistry ,
6
- DocumentModel
7
- } from '@jupyterlab/docregistry' ;
8
-
9
- import { PageConfig } from '@jupyterlab/coreutils' ;
10
-
11
- import {
12
- LoadingManager ,
13
- Vector2 ,
14
- Color
15
- } from 'three' ;
16
-
17
- import URDFLoader from 'urdf-loader' ;
18
- import { XacroLoader } from 'xacro-parser' ;
19
-
5
+ import { Vector2 , Color } from 'three' ;
20
6
import { URDFControls } from './controls' ;
21
7
import { URDFRenderer } from './renderer' ;
8
+ import { URDFLoadingManager } from './robot' ;
22
9
23
10
interface URDFColors {
24
11
sky : Color ,
@@ -30,14 +17,10 @@ interface URDFColors {
30
17
*/
31
18
export class URDFLayout extends PanelLayout {
32
19
private _host : HTMLElement ;
33
- private _robotModel : any = null ;
34
20
private _controlsPanel : URDFControls ;
35
21
private _renderer : URDFRenderer ;
36
22
private _colors : URDFColors ;
37
- private _manager : LoadingManager ;
38
- private _loader : URDFLoader ;
39
- private _workingPath : string ;
40
- private _urdfString : string ;
23
+ private _loader : URDFLoadingManager ;
41
24
42
25
/**
43
26
* Construct a `URDFLayout`
@@ -56,11 +39,7 @@ export class URDFLayout extends PanelLayout {
56
39
57
40
this . _renderer = new URDFRenderer ( this . _colors . sky , this . _colors . ground ) ;
58
41
this . _controlsPanel = new URDFControls ( ) ;
59
-
60
- this . _urdfString = '' ;
61
- this . _workingPath = '' ;
62
- this . _manager = new LoadingManager ;
63
- this . _loader = new URDFLoader ( this . _manager ) ;
42
+ this . _loader = new URDFLoadingManager ( ) ;
64
43
}
65
44
66
45
/**
@@ -98,62 +77,25 @@ export class URDFLayout extends PanelLayout {
98
77
}
99
78
100
79
updateURDF ( urdfString : string ) : void {
101
- this . _robotModel = this . _loader . parse ( urdfString ) ;
102
- this . _robotModel . rotation . x = - Math . PI / 2 ;
103
- this . _renderer . setRobot ( this . _robotModel ) ;
80
+ this . _loader . setRobot ( urdfString ) ;
81
+ this . _renderer . setRobot ( this . _loader . robotModel ) ;
104
82
}
105
83
106
84
setURDF ( context : DocumentRegistry . IContext < DocumentModel > ) : void {
107
85
// Default to parent directory of URDF file
108
- if ( ! this . _workingPath ) {
109
- const filePath = context . path ;
110
- const parentDir = filePath . substring ( 0 , filePath . lastIndexOf ( '/' ) ) ;
111
- this . changeWorkingPath ( parentDir ) ;
112
- }
113
-
114
- this . _urdfString = context . model . toString ( ) ;
115
-
116
- let robotXML ;
117
-
118
-
119
- if ( context . path . endsWith ( 'xacro' ) ) {
120
- const xacroLoader : any = new XacroLoader ( ) ;
121
- xacroLoader . workingPath = PageConfig . getBaseUrl ( ) + 'files/' ;
122
-
123
- xacroLoader . parse (
124
- context . model . toString ( ) ,
125
- ( xml : XMLDocument ) => {
126
- robotXML = xml ;
127
- console . log ( "XML" , xml ) ;
128
- this . _robotModel = this . _loader . parse ( robotXML ) ;
129
- this . _robotModel . rotation . x = - Math . PI / 2 ;
130
-
131
- this . _renderer . setRobot ( this . _robotModel ) ;
132
- this . _setControls ( ) ;
133
-
134
- } ,
135
- ( error : Error ) => console . log ( error )
136
- ) ;
137
- } else {
138
-
139
- // Load robot model
140
- this . _robotModel = this . _loader . parse ( context . model . toString ( ) ) ;
141
-
142
-
143
- this . _robotModel . rotation . x = - Math . PI / 2 ;
86
+ const filePath = context . path ;
87
+ const parentDir = filePath . substring ( 0 , filePath . lastIndexOf ( '/' ) ) ;
88
+ this . _loader . setWorkingPath ( parentDir ) ;
144
89
145
- // TODO: redundant but necessary for files without any meshes
146
- this . _renderer . setRobot ( this . _robotModel ) ;
147
-
148
- this . _manager . onLoad = ( ) => {
149
- this . _renderer . setRobot ( this . _robotModel ) ;
150
- } ;
151
-
152
- this . _renderer . setSize (
153
- this . _renderer . domElement . clientWidth ,
154
- this . _renderer . domElement . clientHeight
155
- ) ;
90
+ this . _loader . onLoad = ( ) => {
91
+ this . _renderer . setRobot ( this . _loader . robotModel ) ;
92
+ this . _setControls ( ) ;
93
+ } ;
156
94
95
+ this . _loader . setRobot ( context . model . toString ( ) ) ;
96
+
97
+ if ( this . _loader . isReady ) {
98
+ this . _renderer . setRobot ( this . _loader . robotModel ) ;
157
99
this . _setControls ( ) ;
158
100
}
159
101
}
@@ -188,7 +130,7 @@ export class URDFLayout extends PanelLayout {
188
130
* Set the callback functions for each of item in the controls panel
189
131
*/
190
132
private _setControls ( ) : void {
191
- if ( ! this . _robotModel ) return ;
133
+ if ( ! this . _loader . isReady ) return ;
192
134
193
135
this . _setPathControls ( ) ;
194
136
this . _setSceneControls ( ) ;
@@ -200,12 +142,12 @@ export class URDFLayout extends PanelLayout {
200
142
* render again.
201
143
*/
202
144
private _setPathControls ( ) : void {
203
- const pathControl = this . _controlsPanel . createWorkspaceControls ( this . _workingPath ) ;
145
+ const pathControl = this . _controlsPanel
146
+ . createWorkspaceControls ( this . _loader . workingPath ) ;
204
147
pathControl . onChange (
205
148
( newPath : string = pathControl . object [ 'Path' ] ) => {
206
- this . changeWorkingPath ( newPath ) ;
207
- this . updateURDF ( this . _urdfString ) ;
208
- this . _renderer . redraw ( ) ;
149
+ this . _loader . setWorkingPath ( newPath ) ;
150
+ this . updateURDF ( '' ) ;
209
151
}
210
152
) ;
211
153
}
@@ -228,7 +170,8 @@ export class URDFLayout extends PanelLayout {
228
170
* Set callback for each joint when the value changes in the controls panel.
229
171
*/
230
172
private _setJointControls ( ) : void {
231
- const jointControl = this . _controlsPanel . createJointControls ( this . _robotModel . joints ) ;
173
+ const jointControl = this . _controlsPanel
174
+ . createJointControls ( this . _loader . robotModel . joints ) ;
232
175
Object . keys ( jointControl ) . forEach (
233
176
( jointName : string ) => {
234
177
jointControl [ jointName ] . onChange (
@@ -244,36 +187,8 @@ export class URDFLayout extends PanelLayout {
244
187
* @param jointName - The name of the joint to be set
245
188
*/
246
189
private _setJointValue ( jointName : string , newValue : number ) : void {
247
- this . _robotModel . setJointValue ( jointName , newValue ) ;
248
- this . _renderer . redraw ( ) ;
249
- }
250
-
251
- /**
252
- * Changes the path to find mesh files described in the URDF
253
- *
254
- * @param workingPath Directory path containing robot description folders
255
- */
256
- changeWorkingPath ( workingPath : string ) : void {
257
- if ( ! workingPath ) return ;
258
-
259
- // To match '/this/format/path'
260
- workingPath = ( workingPath [ 0 ] !== '/' ) ? ( '/' + workingPath ) : workingPath ;
261
- workingPath = ( workingPath [ workingPath . length - 1 ] === '/' ) ?
262
- workingPath . slice ( 0 , - 1 ) : workingPath ;
263
-
264
- console . debug ( '[Manager]: Modify URL with prefix ' , workingPath ) ;
265
- this . _workingPath = workingPath ;
266
-
267
- this . _manager . setURLModifier ( ( url : string ) => {
268
- if ( url . startsWith ( workingPath ) ) {
269
- console . debug ( '[Loader]:' , url ) ;
270
- return '/files' + url ;
271
- } else {
272
- const modifiedURL = '/files' + workingPath + url ;
273
- console . debug ( '[Loader]:' , modifiedURL ) ;
274
- return modifiedURL ;
275
- }
276
- } ) ;
190
+ this . _loader . robotModel . setJointValue ( jointName , newValue ) ;
191
+ this . _renderer . setRobot ( this . _loader . robotModel ) ;
277
192
}
278
193
279
194
/**
@@ -303,6 +218,10 @@ export class URDFLayout extends PanelLayout {
303
218
protected onAfterAttach ( msg : Message ) : void {
304
219
this . _renderer . redraw ( ) ;
305
220
this . _host . appendChild ( this . _renderer . domElement ) ;
221
+ this . _renderer . setSize (
222
+ this . _renderer . domElement . clientWidth ,
223
+ this . _renderer . domElement . clientHeight
224
+ ) ;
306
225
this . _host . appendChild ( this . _controlsPanel . domElement ) ;
307
226
}
308
227
0 commit comments