[4de1c7]: / app / resources / LeapSDK / v3_python27 / src / InteractionBox.cs

Download this file

202 lines (189 with data), 6.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/******************************************************************************\
* Copyright (C) 2012-2016 Leap Motion, Inc. All rights reserved. *
* Leap Motion proprietary and confidential. Not for distribution. *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
\******************************************************************************/
namespace Leap
{
using System;
using System.Runtime.InteropServices;
/**
* 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.
*
* \image html images/Leap_InteractionBox.png
*
* The InteractionBox region is defined by a center and dimensions along the x, y,
* and z axes.
*
* Get an InteractionBox object from a Frame object.
* @since 1.0
*/
public struct InteractionBox
{
/**
* Create an interaction box with a specxific size and center position.
*
* @param center The midpoint of the box.
* @param size The dimensions of the box along each axis.
*/
public InteractionBox(Vector center, Vector size)
{
Size = size;
Center = center;
}
/**
* Normalizes the coordinates of a point using the interaction box.
*
* \include InteractionBox_normalizePoint.txt
*
* 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.
*
* @param position The input position in device coordinates.
* @param 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 The normalized position.
* @since 1.0
*/
public Vector NormalizePoint(Vector position, bool clamp = true)
{
if (!this.IsValid)
{
return Vector.Zero;
}
float x = (position.x - Center.x + Size.x / 2.0f) / Size.x;
float y = (position.y - Center.y + Size.y / 2.0f) / Size.y;
float z = (position.z - Center.z + Size.z / 2.0f) / Size.z;
if (clamp)
{
x = Math.Min(1.0f, Math.Max(0.0f, x));
y = Math.Min(1.0f, Math.Max(0.0f, y));
z = Math.Min(1.0f, Math.Max(0.0f, z));
}
return new Vector(x, y, z);
}
/**
* Converts a position defined by normalized InteractionBox coordinates into device
* coordinates in millimeters.
*
* \include InteractionBox_denormalizePoint.txt
*
* This function performs the inverse of normalizePoint().
*
* @param normalizedPosition The input position in InteractionBox coordinates.
* @returns The corresponding denormalized position in device coordinates.
* @since 1.0
*/
public Vector DenormalizePoint(Vector normalizedPosition)
{
if (!IsValid)
{
return Vector.Zero;
}
float x = normalizedPosition.x * Size.x + (Center.x - Size.x / 2.0f);
float y = normalizedPosition.y * Size.y + (Center.y - Size.y / 2.0f);
float z = normalizedPosition.z * Size.z + (Center.z - Size.z / 2.0f);
return new Vector(x, y, z);
}
/**
* Compare InteractionBox object equality.
*
* \include InteractionBox_operator_equals.txt
*
* Two InteractionBox objects are equal if and only if both InteractionBox objects
* are the same size, in the same position and both InteractionBoxes are valid.
* @since 1.0
*/
public bool Equals(InteractionBox other)
{
return this.IsValid && other.IsValid && (this.Center == other.Center) && (this.Size == other.Size);
}
/**
* A string containing a brief, human readable description of the InteractionBox object.
*
* @returns A description of the InteractionBox as a string.
* @since 1.0
*/
public override string ToString()
{
return "InteractionBox Center: " + Center + ", Size: " + Size;
}
/**
* The center of the InteractionBox in device coordinates (millimeters). This point
* is equidistant from all sides of the box.
*
* \include InteractionBox_center.txt
*
* @returns The InteractionBox center in device coordinates.
* @since 1.0
*/
public Vector Center;
/**
* The dimensions of the interaction box along each axis.
*/
public Vector Size;
/**
* The width of the InteractionBox in millimeters, measured along the x-axis.
*
* \include InteractionBox_width.txt
*
* @returns The InteractionBox width in millimeters.
* @since 1.0
*/
public float Width
{
get { return Size.x; }
}
/**
* The height of the InteractionBox in millimeters, measured along the y-axis.
*
* \include InteractionBox_height.txt
*
* @returns The InteractionBox height in millimeters.
* @since 1.0
*/
public float Height
{
get { return Size.y; }
}
/**
* The depth of the InteractionBox in millimeters, measured along the z-axis.
*
* \include InteractionBox_depth.txt
*
* @returns The InteractionBox depth in millimeters.
* @since 1.0
*/
public float Depth
{
get { return Size.z; }
}
/**
* Reports whether this is a valid InteractionBox object.
*
* \include InteractionBox_isValid.txt
*
* @returns True, if this InteractionBox object contains valid data.
* @since 1.0
*/
public bool IsValid
{
get
{
return Size != Vector.Zero
&& !float.IsNaN(Size.x)
&& !float.IsNaN(Size.y)
&& !float.IsNaN(Size.z);
}
}
}
}