Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom frame axes options: scale, dashed lines and no pan&zoom frame #208

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 64 additions & 16 deletions build/ros3d.js
Original file line number Diff line number Diff line change
Expand Up @@ -2473,16 +2473,28 @@ ROS3D.Arrow2.prototype.setLength = function ( length, headLength, headWidth ) {
* * shaftRadius (optional) - the radius of the shaft to render
* * headRadius (optional) - the radius of the head to render
* * headLength (optional) - the length of the head to render
* * scale (optional) - the scale of the frame (defaults to 1.0)
* * lineType (optional) - the line type for the axes. Supported line types:
* 'dashed' and 'full'.
* * lineDashLength (optional) - the length of the dashes, relative to the length of the axis.
* Maximum value is 1, which means the dash length is
* equal to the length of the axis. Parameter only applies when
* lineType is set to dashed.
*/
ROS3D.Axes = function(options) {
var that = this;
options = options || {};
var shaftRadius = options.shaftRadius || 0.008;
var headRadius = options.headRadius || 0.023;
var headLength = options.headLength || 0.1;
var scaleArg = options.scale || 1.0;
var lineType = options.lineType || 'full';
var lineDashLength = options.lineDashLength || 0.1;

THREE.Object3D.call(this);

this.scale = new THREE.Vector3(scaleArg, scaleArg, scaleArg);

// create the cylinders for the objects
this.lineGeom = new THREE.CylinderGeometry(shaftRadius, shaftRadius, 1.0 - headLength);
this.headGeom = new THREE.CylinderGeometry(0, headRadius, headLength);
Expand Down Expand Up @@ -2515,12 +2527,29 @@ ROS3D.Axes = function(options) {
that.add(arrow);

// create the line
var line = new THREE.Mesh(that.lineGeom, material);
line.position.copy(axis);
line.position.multiplyScalar(0.45);
line.quaternion.copy(rot);
line.updateMatrix();
that.add(line);
var line;
if (lineType === 'dashed') {
var l = lineDashLength;
for (var i = 0; (l / 2 + 3 * l * i + l / 2) <= 1; ++i) {
var geom = new THREE.CylinderGeometry(shaftRadius, shaftRadius, l);
line = new THREE.Mesh(geom, material);
line.position.copy(axis);
// Make spacing between dashes equal to 1.5 times the dash length.
line.position.multiplyScalar(l / 2 + 3 * l * i);
line.quaternion.copy(rot);
line.updateMatrix();
that.add(line);
}
} else if (lineType === 'full') {
line = new THREE.Mesh(that.lineGeom, material);
line.position.copy(axis);
line.position.multiplyScalar(0.45);
line.quaternion.copy(rot);
line.updateMatrix();
that.add(line);
} else {
console.warn('[ROS3D.Axes]: Unsupported line type. Not drawing any axes.');
}
}

// add the three markers to the axes
Expand Down Expand Up @@ -4367,7 +4396,12 @@ Object.assign(ROS3D.MouseHandler.prototype, THREE.EventDispatcher.prototype);
* @param userZoomSpeed (optional) - the speed for zooming
* @param userRotateSpeed (optional) - the speed for rotating
* @param autoRotate (optional) - if the orbit should auto rotate
* @param autoRotate (optional) - the speed for auto rotating
* @param autoRotateSpeed (optional) - the speed for auto rotating
* @param displayPanAndZoomFrame - whether to display a frame when panning/zooming
* (defaults to true)
* @param lineTypePanAndZoomFrame - line type for the frame that is displayed when
* panning/zooming. Only has effect when
* displayPanAndZoomFrame is set to true.
*/
ROS3D.OrbitControls = function(options) {
THREE.EventDispatcher.call(this);
Expand All @@ -4382,7 +4416,10 @@ ROS3D.OrbitControls = function(options) {
this.userRotateSpeed = options.userRotateSpeed || 1.0;
this.autoRotate = options.autoRotate;
this.autoRotateSpeed = options.autoRotateSpeed || 2.0;

this.displayPanAndZoomFrame = (options.displayPanAndZoomFrame === undefined) ?
true :
!!options.displayPanAndZoomFrame;
this.lineTypePanAndZoomFrame = options.dashedPanAndZoomFrame || 'full';
// In ROS, z is pointing upwards
this.camera.up = new THREE.Vector3(0, 0, 1);

Expand Down Expand Up @@ -4414,17 +4451,19 @@ ROS3D.OrbitControls = function(options) {
};
var state = STATE.NONE;

// add the axes for the main coordinate frame
this.axes = new ROS3D.Axes({
shaftRadius : 0.025,
headRadius : 0.07,
headLength : 0.2
});
// initially not visible
scene.add(this.axes);
this.axes.traverse(function(obj) {
obj.visible = false;
headLength : 0.2,
lineType: this.lineTypePanAndZoomFrame
});
if (this.displayPanAndZoomFrame) {
// initially not visible
scene.add(this.axes);
this.axes.traverse(function(obj) {
obj.visible = false;
});
}

/**
* Handle the mousedown 3D event.
Expand Down Expand Up @@ -4946,6 +4985,11 @@ ROS3D.SceneNode.prototype.unsubscribeTf = function() {
* * antialias (optional) - if antialiasing should be used
* * intensity (optional) - the lighting intensity setting to use
* * cameraPosition (optional) - the starting position of the camera
* * displayPanAndZoomFrame (optional) - whether to display a frame when
* * panning/zooming. Defaults to true.
* * lineTypePanAndZoomFrame - line type for the frame that is displayed when
* * panning/zooming. Only has effect when
* * displayPanAndZoomFrame is set to true.
*/
ROS3D.Viewer = function(options) {
options = options || {};
Expand All @@ -4964,6 +5008,8 @@ ROS3D.Viewer = function(options) {
z : 3
};
var cameraZoomSpeed = options.cameraZoomSpeed || 0.5;
var displayPanAndZoomFrame = (options.displayPanAndZoomFrame === undefined) ? true : !!options.displayPanAndZoomFrame;
var lineTypePanAndZoomFrame = options.lineTypePanAndZoomFrame || 'full';

// create the canvas to render to
this.renderer = new THREE.WebGLRenderer({
Expand All @@ -4987,7 +5033,9 @@ ROS3D.Viewer = function(options) {
// add controls to the camera
this.cameraControls = new ROS3D.OrbitControls({
scene : this.scene,
camera : this.camera
camera : this.camera,
displayPanAndZoomFrame : displayPanAndZoomFrame,
lineTypePanAndZoomFrame: lineTypePanAndZoomFrame
});
this.cameraControls.userZoomSpeed = cameraZoomSpeed;

Expand Down
6 changes: 3 additions & 3 deletions build/ros3d.min.js

Large diffs are not rendered by default.

41 changes: 35 additions & 6 deletions src/models/Axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,28 @@
* * shaftRadius (optional) - the radius of the shaft to render
* * headRadius (optional) - the radius of the head to render
* * headLength (optional) - the length of the head to render
* * scale (optional) - the scale of the frame (defaults to 1.0)
* * lineType (optional) - the line type for the axes. Supported line types:
* 'dashed' and 'full'.
* * lineDashLength (optional) - the length of the dashes, relative to the length of the axis.
* Maximum value is 1, which means the dash length is
* equal to the length of the axis. Parameter only applies when
* lineType is set to dashed.
*/
ROS3D.Axes = function(options) {
var that = this;
options = options || {};
var shaftRadius = options.shaftRadius || 0.008;
var headRadius = options.headRadius || 0.023;
var headLength = options.headLength || 0.1;
var scaleArg = options.scale || 1.0;
var lineType = options.lineType || 'full';
var lineDashLength = options.lineDashLength || 0.1;

THREE.Object3D.call(this);

this.scale = new THREE.Vector3(scaleArg, scaleArg, scaleArg);

// create the cylinders for the objects
this.lineGeom = new THREE.CylinderGeometry(shaftRadius, shaftRadius, 1.0 - headLength);
this.headGeom = new THREE.CylinderGeometry(0, headRadius, headLength);
Expand Down Expand Up @@ -53,12 +65,29 @@ ROS3D.Axes = function(options) {
that.add(arrow);

// create the line
var line = new THREE.Mesh(that.lineGeom, material);
line.position.copy(axis);
line.position.multiplyScalar(0.45);
line.quaternion.copy(rot);
line.updateMatrix();
that.add(line);
var line;
if (lineType === 'dashed') {
var l = lineDashLength;
for (var i = 0; (l / 2 + 3 * l * i + l / 2) <= 1; ++i) {
var geom = new THREE.CylinderGeometry(shaftRadius, shaftRadius, l);
line = new THREE.Mesh(geom, material);
line.position.copy(axis);
// Make spacing between dashes equal to 1.5 times the dash length.
line.position.multiplyScalar(l / 2 + 3 * l * i);
line.quaternion.copy(rot);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a few duplicated lines here - can you refactor the code to move them outside the if-else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not duplicated, but inside a for loop. The de-duplication would make reasoning about this harder.

line.updateMatrix();
that.add(line);
}
} else if (lineType === 'full') {
line = new THREE.Mesh(that.lineGeom, material);
line.position.copy(axis);
line.position.multiplyScalar(0.45);
line.quaternion.copy(rot);
line.updateMatrix();
that.add(line);
} else {
console.warn('[ROS3D.Axes]: Unsupported line type. Not drawing any axes.');
}
}

// add the three markers to the axes
Expand Down
11 changes: 10 additions & 1 deletion src/visualization/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
* * antialias (optional) - if antialiasing should be used
* * intensity (optional) - the lighting intensity setting to use
* * cameraPosition (optional) - the starting position of the camera
* * displayPanAndZoomFrame (optional) - whether to display a frame when
* * panning/zooming. Defaults to true.
* * lineTypePanAndZoomFrame - line type for the frame that is displayed when
* * panning/zooming. Only has effect when
* * displayPanAndZoomFrame is set to true.
*/
ROS3D.Viewer = function(options) {
options = options || {};
Expand All @@ -36,6 +41,8 @@ ROS3D.Viewer = function(options) {
z : 3
};
var cameraZoomSpeed = options.cameraZoomSpeed || 0.5;
var displayPanAndZoomFrame = (options.displayPanAndZoomFrame === undefined) ? true : !!options.displayPanAndZoomFrame;
var lineTypePanAndZoomFrame = options.lineTypePanAndZoomFrame || 'full';

// create the canvas to render to
this.renderer = new THREE.WebGLRenderer({
Expand All @@ -59,7 +66,9 @@ ROS3D.Viewer = function(options) {
// add controls to the camera
this.cameraControls = new ROS3D.OrbitControls({
scene : this.scene,
camera : this.camera
camera : this.camera,
displayPanAndZoomFrame : displayPanAndZoomFrame,
lineTypePanAndZoomFrame: lineTypePanAndZoomFrame
});
this.cameraControls.userZoomSpeed = cameraZoomSpeed;

Expand Down
28 changes: 19 additions & 9 deletions src/visualization/interaction/OrbitControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
* @param userZoomSpeed (optional) - the speed for zooming
* @param userRotateSpeed (optional) - the speed for rotating
* @param autoRotate (optional) - if the orbit should auto rotate
* @param autoRotate (optional) - the speed for auto rotating
* @param autoRotateSpeed (optional) - the speed for auto rotating
* @param displayPanAndZoomFrame - whether to display a frame when panning/zooming
* (defaults to true)
* @param lineTypePanAndZoomFrame - line type for the frame that is displayed when
* panning/zooming. Only has effect when
* displayPanAndZoomFrame is set to true.
*/
ROS3D.OrbitControls = function(options) {
THREE.EventDispatcher.call(this);
Expand All @@ -29,7 +34,10 @@ ROS3D.OrbitControls = function(options) {
this.userRotateSpeed = options.userRotateSpeed || 1.0;
this.autoRotate = options.autoRotate;
this.autoRotateSpeed = options.autoRotateSpeed || 2.0;

this.displayPanAndZoomFrame = (options.displayPanAndZoomFrame === undefined) ?
true :
!!options.displayPanAndZoomFrame;
this.lineTypePanAndZoomFrame = options.dashedPanAndZoomFrame || 'full';
// In ROS, z is pointing upwards
this.camera.up = new THREE.Vector3(0, 0, 1);

Expand Down Expand Up @@ -61,17 +69,19 @@ ROS3D.OrbitControls = function(options) {
};
var state = STATE.NONE;

// add the axes for the main coordinate frame
this.axes = new ROS3D.Axes({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.axes isn't used if displayPanAndZoomFrame is false. Could you move this under if statement? And update showAxes() something like

 if(this.displayPanAndZoomFrame){
 var that = this;                                                                                                                                                                                                                                                           
  this.axes.traverse(function(obj) {                                                                                                   
    obj.visible = true;                                                                                                                
  });                                                                                                                                  
  if (this.hideTimeout) {                                                                                                              
    clearTimeout(this.hideTimeout);                                                                                                    
  }                                                                                                                                    
  this.hideTimeout = setTimeout(function() {                                                                                           
    that.axes.traverse(function(obj) {                                                                                                 
      obj.visible = false;                                                                                                             
    });                                                                                                                                
    that.hideTimeout = false;                                                                                                          
  }, 1000);                                                                                                                            
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shaftRadius : 0.025,
headRadius : 0.07,
headLength : 0.2
});
// initially not visible
scene.add(this.axes);
this.axes.traverse(function(obj) {
obj.visible = false;
headLength : 0.2,
lineType: this.lineTypePanAndZoomFrame
});
if (this.displayPanAndZoomFrame) {
// initially not visible
scene.add(this.axes);
this.axes.traverse(function(obj) {
obj.visible = false;
});
}

/**
* Handle the mousedown 3D event.
Expand Down