leapjs
Version:
JavaScript client for the Leap Motion Controller
141 lines (133 loc) • 4.56 kB
JavaScript
var glMatrix = require("gl-matrix")
, vec3 = glMatrix.vec3;
/**
* Constructs a InteractionBox object.
*
* @class InteractionBox
* @memberof Leap
* @classdesc
* The InteractionBox class represents a box-shaped region completely within
* the field of view of the Leap Motion controller.
*
* The interaction box is an axis-aligned rectangular prism and provides
* normalized coordinates for hands, fingers, and tools within this box.
* The InteractionBox class can make it easier to map positions in the
* Leap Motion coordinate system to 2D or 3D coordinate systems used
* for application drawing.
*
* 
*
* The InteractionBox region is defined by a center and dimensions along the x, y, and z axes.
*/
var InteractionBox = module.exports = function(data) {
/**
* Indicates whether this is a valid InteractionBox object.
*
* @member valid
* @type {Boolean}
* @memberof Leap.InteractionBox.prototype
*/
this.valid = true;
/**
* The center of the InteractionBox in device coordinates (millimeters).
* This point is equidistant from all sides of the box.
*
* @member center
* @type {number[]}
* @memberof Leap.InteractionBox.prototype
*/
this.center = data.center;
this.size = data.size;
/**
* The width of the InteractionBox in millimeters, measured along the x-axis.
*
* @member width
* @type {number}
* @memberof Leap.InteractionBox.prototype
*/
this.width = data.size[0];
/**
* The height of the InteractionBox in millimeters, measured along the y-axis.
*
* @member height
* @type {number}
* @memberof Leap.InteractionBox.prototype
*/
this.height = data.size[1];
/**
* The depth of the InteractionBox in millimeters, measured along the z-axis.
*
* @member depth
* @type {number}
* @memberof Leap.InteractionBox.prototype
*/
this.depth = data.size[2];
}
/**
* Converts a position defined by normalized InteractionBox coordinates
* into device coordinates in millimeters.
*
* This function performs the inverse of normalizePoint().
*
* @method denormalizePoint
* @memberof Leap.InteractionBox.prototype
* @param {number[]} normalizedPosition The input position in InteractionBox coordinates.
* @returns {number[]} The corresponding denormalized position in device coordinates.
*/
InteractionBox.prototype.denormalizePoint = function(normalizedPosition) {
return vec3.fromValues(
(normalizedPosition[0] - 0.5) * this.size[0] + this.center[0],
(normalizedPosition[1] - 0.5) * this.size[1] + this.center[1],
(normalizedPosition[2] - 0.5) * this.size[2] + this.center[2]
);
}
/**
* Normalizes the coordinates of a point using the interaction box.
*
* Coordinates from the Leap Motion frame of reference (millimeters) are
* converted to a range of [0..1] such that the minimum value of the
* InteractionBox maps to 0 and the maximum value of the InteractionBox maps to 1.
*
* @method normalizePoint
* @memberof Leap.InteractionBox.prototype
* @param {number[]} position The input position in device coordinates.
* @param {Boolean} clamp Whether or not to limit the output value to the range [0,1]
* when the input position is outside the InteractionBox. Defaults to true.
* @returns {number[]} The normalized position.
*/
InteractionBox.prototype.normalizePoint = function(position, clamp) {
var vec = vec3.fromValues(
((position[0] - this.center[0]) / this.size[0]) + 0.5,
((position[1] - this.center[1]) / this.size[1]) + 0.5,
((position[2] - this.center[2]) / this.size[2]) + 0.5
);
if (clamp) {
vec[0] = Math.min(Math.max(vec[0], 0), 1);
vec[1] = Math.min(Math.max(vec[1], 0), 1);
vec[2] = Math.min(Math.max(vec[2], 0), 1);
}
return vec;
}
/**
* Writes a brief, human readable description of the InteractionBox object.
*
* @method toString
* @memberof Leap.InteractionBox.prototype
* @returns {String} A description of the InteractionBox object as a string.
*/
InteractionBox.prototype.toString = function() {
return "InteractionBox [ width:" + this.width + " | height:" + this.height + " | depth:" + this.depth + " ]";
}
/**
* An invalid InteractionBox object.
*
* You can use this InteractionBox instance in comparisons testing
* whether a given InteractionBox instance is valid or invalid. (You can also use the
* InteractionBox.valid property.)
*
* @static
* @type {Leap.InteractionBox}
* @name Invalid
* @memberof Leap.InteractionBox
*/
InteractionBox.Invalid = { valid: false };