UNPKG

4.56 kBJavaScriptView Raw
1var glMatrix = require("gl-matrix")
2 , vec3 = glMatrix.vec3;
3
4/**
5 * Constructs a InteractionBox object.
6 *
7 * @class InteractionBox
8 * @memberof Leap
9 * @classdesc
10 * The InteractionBox class represents a box-shaped region completely within
11 * the field of view of the Leap Motion controller.
12 *
13 * The interaction box is an axis-aligned rectangular prism and provides
14 * normalized coordinates for hands, fingers, and tools within this box.
15 * The InteractionBox class can make it easier to map positions in the
16 * Leap Motion coordinate system to 2D or 3D coordinate systems used
17 * for application drawing.
18 *
19 * ![Interaction Box](images/Leap_InteractionBox.png)
20 *
21 * The InteractionBox region is defined by a center and dimensions along the x, y, and z axes.
22 */
23var InteractionBox = module.exports = function(data) {
24 /**
25 * Indicates whether this is a valid InteractionBox object.
26 *
27 * @member valid
28 * @type {Boolean}
29 * @memberof Leap.InteractionBox.prototype
30 */
31 this.valid = true;
32 /**
33 * The center of the InteractionBox in device coordinates (millimeters).
34 * This point is equidistant from all sides of the box.
35 *
36 * @member center
37 * @type {number[]}
38 * @memberof Leap.InteractionBox.prototype
39 */
40 this.center = data.center;
41
42 this.size = data.size;
43 /**
44 * The width of the InteractionBox in millimeters, measured along the x-axis.
45 *
46 * @member width
47 * @type {number}
48 * @memberof Leap.InteractionBox.prototype
49 */
50 this.width = data.size[0];
51 /**
52 * The height of the InteractionBox in millimeters, measured along the y-axis.
53 *
54 * @member height
55 * @type {number}
56 * @memberof Leap.InteractionBox.prototype
57 */
58 this.height = data.size[1];
59 /**
60 * The depth of the InteractionBox in millimeters, measured along the z-axis.
61 *
62 * @member depth
63 * @type {number}
64 * @memberof Leap.InteractionBox.prototype
65 */
66 this.depth = data.size[2];
67}
68
69/**
70 * Converts a position defined by normalized InteractionBox coordinates
71 * into device coordinates in millimeters.
72 *
73 * This function performs the inverse of normalizePoint().
74 *
75 * @method denormalizePoint
76 * @memberof Leap.InteractionBox.prototype
77 * @param {number[]} normalizedPosition The input position in InteractionBox coordinates.
78 * @returns {number[]} The corresponding denormalized position in device coordinates.
79 */
80InteractionBox.prototype.denormalizePoint = function(normalizedPosition) {
81 return vec3.fromValues(
82 (normalizedPosition[0] - 0.5) * this.size[0] + this.center[0],
83 (normalizedPosition[1] - 0.5) * this.size[1] + this.center[1],
84 (normalizedPosition[2] - 0.5) * this.size[2] + this.center[2]
85 );
86}
87
88/**
89 * Normalizes the coordinates of a point using the interaction box.
90 *
91 * Coordinates from the Leap Motion frame of reference (millimeters) are
92 * converted to a range of [0..1] such that the minimum value of the
93 * InteractionBox maps to 0 and the maximum value of the InteractionBox maps to 1.
94 *
95 * @method normalizePoint
96 * @memberof Leap.InteractionBox.prototype
97 * @param {number[]} position The input position in device coordinates.
98 * @param {Boolean} clamp Whether or not to limit the output value to the range [0,1]
99 * when the input position is outside the InteractionBox. Defaults to true.
100 * @returns {number[]} The normalized position.
101 */
102InteractionBox.prototype.normalizePoint = function(position, clamp) {
103 var vec = vec3.fromValues(
104 ((position[0] - this.center[0]) / this.size[0]) + 0.5,
105 ((position[1] - this.center[1]) / this.size[1]) + 0.5,
106 ((position[2] - this.center[2]) / this.size[2]) + 0.5
107 );
108
109 if (clamp) {
110 vec[0] = Math.min(Math.max(vec[0], 0), 1);
111 vec[1] = Math.min(Math.max(vec[1], 0), 1);
112 vec[2] = Math.min(Math.max(vec[2], 0), 1);
113 }
114 return vec;
115}
116
117/**
118 * Writes a brief, human readable description of the InteractionBox object.
119 *
120 * @method toString
121 * @memberof Leap.InteractionBox.prototype
122 * @returns {String} A description of the InteractionBox object as a string.
123 */
124InteractionBox.prototype.toString = function() {
125 return "InteractionBox [ width:" + this.width + " | height:" + this.height + " | depth:" + this.depth + " ]";
126}
127
128/**
129 * An invalid InteractionBox object.
130 *
131 * You can use this InteractionBox instance in comparisons testing
132 * whether a given InteractionBox instance is valid or invalid. (You can also use the
133 * InteractionBox.valid property.)
134 *
135 * @static
136 * @type {Leap.InteractionBox}
137 * @name Invalid
138 * @memberof Leap.InteractionBox
139 */
140InteractionBox.Invalid = { valid: false };