Skip to content

Commit 3e5c15a

Browse files
committed
Add comments for documentation
1 parent 13d2189 commit 3e5c15a

File tree

8 files changed

+180
-6
lines changed

8 files changed

+180
-6
lines changed

src/controls.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { GUI } from 'dat.gui';
2+
23
import { Color } from 'three';
4+
35
import { URDFJoint } from 'urdf-loader';
46

57
interface IJoints {
68
[name: string]: URDFJoint;
79
}
810

11+
/**
12+
* URDFControls: a GUI panel for controlling the viewer settings and
13+
* the robot joints
14+
*/
915
export class URDFControls extends GUI {
1016
private _workspaceFolder: any;
1117
private _sceneFolder: any;
@@ -22,6 +28,9 @@ export class URDFControls extends GUI {
2228
joints: {}
2329
};
2430

31+
/**
32+
* Creates a controls panel with the default folders
33+
*/
2534
constructor() {
2635
super({ autoPlace: false });
2736

@@ -44,22 +53,43 @@ export class URDFControls extends GUI {
4453
this._jointsFolder.domElement.setAttribute('class', 'dg joints-folder');
4554
}
4655

56+
/**
57+
* Retrieves the folder with workspace settings
58+
*/
4759
get workspaceFolder() {
4860
return this._workspaceFolder;
4961
}
5062

63+
/**
64+
* Retrieves the folder with scene settings
65+
*/
5166
get sceneFolder() {
5267
return this._sceneFolder;
5368
}
5469

70+
/**
71+
* Retrieves the folder with joints settings
72+
*/
5573
get jointsFolder() {
5674
return this._jointsFolder;
5775
}
5876

77+
/**
78+
* Checks if a given object is empty {}
79+
*
80+
* @param obj - The object to check
81+
* @returns - True when the object is empty, or false when it is not empty
82+
*/
5983
private _isEmpty(obj: object): boolean {
6084
return Object.keys(obj).length === 0;
6185
}
6286

87+
/**
88+
* Creates an input box and a button to modify the working path
89+
*
90+
* @param workingPath - The path where the loader looks for mesh files
91+
* @returns - The controls to trigger callbacks when the path is changed
92+
*/
6393
createWorkspaceControls(workingPath = '') {
6494
if (this._isEmpty(this.controls.path)) {
6595
this._workingPath = workingPath;
@@ -79,6 +109,13 @@ export class URDFControls extends GUI {
79109
return this.controls.path;
80110
}
81111

112+
/**
113+
* Creates color selectors to modify the scene background and grid
114+
*
115+
* @param bgColor - The background color as a three.js Color
116+
* @param gridColor - The grid color as a three.js Color
117+
* @returns - The controls to trigger callbacks when the colors are changed
118+
*/
82119
createSceneControls(
83120
bgColor = new Color(0x263238),
84121
gridColor = new Color(0x263238)
@@ -115,7 +152,13 @@ export class URDFControls extends GUI {
115152
return this.controls.scene;
116153
}
117154

118-
private _convertColor2Array(color: THREE.Color): number[] {
155+
/**
156+
* Converts a three.js Color to an RGB Array
157+
*
158+
* @param color - The three.js Color to convert
159+
* @returns - The [R, G, B] Array with range [0, 255]
160+
*/
161+
private _convertColor2Array(color: Color): number[] {
119162
// Note: using hex value instead of the RGB values in Color because
120163
// those are dependant on the color space
121164
const hexColor = color.getHexString();
@@ -127,6 +170,12 @@ export class URDFControls extends GUI {
127170
return colorArray;
128171
}
129172

173+
/**
174+
* Creates number sliders for each movable robot joint
175+
*
176+
* @param joints - An object containing all of the robot's joints
177+
* @returns - The controls to trigger callbacks when any joint value changes
178+
*/
130179
createJointControls(joints: IJoints) {
131180
if (this._isEmpty(this.controls.joints)) {
132181
Object.keys(joints).forEach((name: string) => {

src/factory.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { URDFWidget, URDFPanel } from './widget';
88

99
/**
10-
* A widget factory to create new instances of URDFWidgets
10+
* URDFWidgetFactory: a widget factory to create new instances of URDFWidgets
1111
*/
1212
export class URDFWidgetFactory extends ABCWidgetFactory<
1313
URDFWidget,
@@ -17,7 +17,9 @@ export class URDFWidgetFactory extends ABCWidgetFactory<
1717
super(options);
1818
}
1919

20-
// Create new URDFWidget given a context (file info)
20+
/**
21+
* Create new URDFWidget given a context (file info)
22+
*/
2123
protected createNewWidget(
2224
context: DocumentRegistry.IContext<DocumentModel>
2325
): URDFWidget {

src/icons.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { LabIcon } from '@jupyterlab/ui-components';
22

33
import urdf_logo from '/style/icons/urdf_logo.svg';
44

5+
/**
6+
* Creates an icon for the URDF extension from a custom SVG
7+
*/
58
export const urdf_icon = new LabIcon({
69
name: 'urdf:icon/logo',
710
svgstr: urdf_logo

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ import { IEditorLanguageRegistry } from '@jupyterlab/codemirror';
3232
// Name of the factory that creates the URDF widgets
3333
const FACTORY = 'URDF Widget Factory';
3434

35-
// Export token so other extensions can require it
35+
/**
36+
* Export token so other extensions can require it
37+
*/
3638
export const IURDFTracker = new Token<IWidgetTracker<URDFWidget>>(
3739
'urdf-tracker'
3840
);

src/layout.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { Message } from '@lumino/messaging';
2+
23
import { PanelLayout, Widget } from '@lumino/widgets';
4+
35
import { DocumentRegistry, DocumentModel } from '@jupyterlab/docregistry';
46

57
import { Vector2, Color } from 'three';
8+
69
import { URDFControls } from './controls';
10+
711
import { URDFRenderer } from './renderer';
12+
813
import { URDFLoadingManager } from './robot';
914

1015
interface IURDFColors {
@@ -13,7 +18,7 @@ interface IURDFColors {
1318
}
1419

1520
/**
16-
* A URDF layout to host the URDF viewer
21+
* URDFLayout: A layout panel to host the URDF viewer
1722
*/
1823
export class URDFLayout extends PanelLayout {
1924
private _host: HTMLElement;
@@ -47,6 +52,7 @@ export class URDFLayout extends PanelLayout {
4752
*/
4853
dispose(): void {
4954
this._renderer.dispose();
55+
this._loader.dispose();
5056
super.dispose();
5157
}
5258

@@ -76,11 +82,21 @@ export class URDFLayout extends PanelLayout {
7682
return;
7783
}
7884

85+
/**
86+
* Updates the viewer with any new changes on the file
87+
*
88+
* @param urdfString - The contents of the file with the new changes
89+
*/
7990
updateURDF(urdfString: string): void {
8091
this._loader.setRobot(urdfString);
8192
this._renderer.setRobot(this._loader.robotModel);
8293
}
8394

95+
/**
96+
* Sets the robot model initially and configures loader with default values
97+
*
98+
* @param context - Contains the URDF file and its parameters
99+
*/
84100
setURDF(context: DocumentRegistry.IContext<DocumentModel>): void {
85101
// Default to parent directory of URDF file
86102
const filePath = context.path;
@@ -100,13 +116,25 @@ export class URDFLayout extends PanelLayout {
100116
}
101117
}
102118

119+
/**
120+
* Retrieves the values for any color variable declared in CSS
121+
*
122+
* @param colorName - The variable name of the color. Ex: '--jp-layout-color1'
123+
* @returns - The values of the color as a three.js Color
124+
*/
103125
private _getThemeColor(colorName: string): Color | void {
104126
const colorString = window
105127
.getComputedStyle(document.documentElement)
106128
.getPropertyValue(colorName);
107129
return this._parseColor(colorString);
108130
}
109131

132+
/**
133+
* Converts keyword or hex color definitions into three.js Color
134+
*
135+
* @param color - A color string such as: 'red', '#aaa', or '#232323'
136+
* @returns - The same color transformed to a three.js Color
137+
*/
110138
private _parseColor(color: string): Color | void {
111139
let parsedColor;
112140
if (color[0] !== '#') {
@@ -230,6 +258,9 @@ export class URDFLayout extends PanelLayout {
230258
this._host.appendChild(this._controlsPanel.domElement);
231259
}
232260

261+
/**
262+
* Sets the size of the rendered view to match the host window size
263+
*/
233264
private _resizeWorkspace(): void {
234265
const rect = this.parent?.node.getBoundingClientRect();
235266
this._host.style.height = rect?.height + 'px';

src/renderer.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as THREE from 'three';
2+
23
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
4+
35
import { URDFRobot } from 'urdf-loader';
46

57
/**
@@ -12,6 +14,9 @@ import { URDFRobot } from 'urdf-loader';
1214
* Z
1315
*/
1416

17+
/**
18+
* URDFRenderer: a renderer to manage the view of a scene with a robot
19+
*/
1520
export class URDFRenderer extends THREE.WebGLRenderer {
1621
private _scene: THREE.Scene;
1722
private _camera: THREE.PerspectiveCamera;
@@ -20,6 +25,12 @@ export class URDFRenderer extends THREE.WebGLRenderer {
2025
private _colorGround = new THREE.Color();
2126
private _robotIndex = -1;
2227

28+
/**
29+
* Creates a renderer to manage the scene elements
30+
*
31+
* @param colorSky - The color of the scene background
32+
* @param colorGround - The color of the ground (grid)
33+
*/
2334
constructor(
2435
colorSky = new THREE.Color(0x263238),
2536
colorGround = new THREE.Color(0x263238)
@@ -45,11 +56,17 @@ export class URDFRenderer extends THREE.WebGLRenderer {
4556
this._initControls();
4657
}
4758

59+
/**
60+
* Initializes the camera
61+
*/
4862
private _initCamera(): void {
4963
this._camera.position.set(4, 4, 4);
5064
this._camera.lookAt(0, 0, 0);
5165
}
5266

67+
/**
68+
* Initializes the orbital controls
69+
*/
5370
private _initControls(): void {
5471
this._controls.rotateSpeed = 2.0;
5572
this._controls.zoomSpeed = 5;
@@ -61,6 +78,9 @@ export class URDFRenderer extends THREE.WebGLRenderer {
6178
this._controls.addEventListener('change', () => this.redraw());
6279
}
6380

81+
/**
82+
* Initializes the scene
83+
*/
6484
private _initScene(): void {
6585
this._scene.background = this._colorSky;
6686
this._scene.up = new THREE.Vector3(0, 0, 1); // Z is up
@@ -69,7 +89,11 @@ export class URDFRenderer extends THREE.WebGLRenderer {
6989
this._addLights();
7090
}
7191

92+
/**
93+
* Adds a plane representing the ground to the scene
94+
*/
7295
private _addGround(): void {
96+
// TODO: fix shadows
7397
const ground = new THREE.Mesh(
7498
new THREE.PlaneGeometry(40, 40),
7599
new THREE.ShadowMaterial({ opacity: 0.5 })
@@ -80,6 +104,9 @@ export class URDFRenderer extends THREE.WebGLRenderer {
80104
this._scene.add(ground);
81105
}
82106

107+
/**
108+
* Adds a grid to the scene with ground color given to the constructor()
109+
*/
83110
private _addGrid(): void {
84111
const grid = new THREE.GridHelper(
85112
50,
@@ -91,6 +118,9 @@ export class URDFRenderer extends THREE.WebGLRenderer {
91118
this._scene.add(grid);
92119
}
93120

121+
/**
122+
* Adds three lights to the scene
123+
*/
94124
private _addLights(): void {
95125
const directionalLight = new THREE.DirectionalLight(0xffffff, 2.0);
96126
directionalLight.castShadow = true;
@@ -116,6 +146,9 @@ export class URDFRenderer extends THREE.WebGLRenderer {
116146
this._scene.add(hemisphereLight);
117147
}
118148

149+
/**
150+
* Updates the hemisphere light to reflect the sky and ground colors
151+
*/
119152
private _updateLights(): void {
120153
const hemisphereLight = new THREE.HemisphereLight(
121154
this._colorSky,
@@ -160,6 +193,11 @@ export class URDFRenderer extends THREE.WebGLRenderer {
160193
this.redraw();
161194
}
162195

196+
/**
197+
* Changes the height of the grid in the vertical axis (y-axis for three.js)
198+
*
199+
* @param height - The height to shift the grid to
200+
*/
163201
setGridHeight(height = 0): void {
164202
const gridIndex = this._scene.children
165203
.map(i => i.type)
@@ -168,6 +206,11 @@ export class URDFRenderer extends THREE.WebGLRenderer {
168206
this.redraw();
169207
}
170208

209+
/**
210+
* Adds a robot to the scene or updates the existing robot
211+
*
212+
* @param robot
213+
*/
171214
setRobot(robot: URDFRobot): void {
172215
if (this._robotIndex < 0) {
173216
this._scene.add(robot);
@@ -180,6 +223,9 @@ export class URDFRenderer extends THREE.WebGLRenderer {
180223
this.redraw();
181224
}
182225

226+
/**
227+
* Refreshes the viewer by re-rendering the scene and its elements
228+
*/
183229
redraw(): void {
184230
this.render(this._scene, this._camera);
185231
}

0 commit comments

Comments
 (0)