Skip to content

Instantly share code, notes, and snippets.

@hachibeeDI
Created August 2, 2013 06:09
Show Gist options
  • Save hachibeeDI/6137813 to your computer and use it in GitHub Desktop.
Save hachibeeDI/6137813 to your computer and use it in GitHub Desktop.
とりあえずleapのsampleの単純な置き換えのおいとく
import java.io.IOException
import com.leapmotion.leap._
import com.leapmotion.leap.Vector
import com.leapmotion.leap.Frame
;
import com.leapmotion.leap.Gesture.State
;
/**
* Created with IntelliJ IDEA.
* User: dogura
* Date: 2013/08/02
* Time: 13:30
* To change this template use File | Settings | File Templates.
*/
object Sample {
def main(args: Array[String]): Unit = {
// Create a sample listener and controller
val listener = new SampleListener()
val controller = new Controller()
// Have the sample listener receive events from the controller
controller.addListener(listener)
// Keep this process running until Enter is pressed
println("Press Enter to quit...")
try {
System.in.read()
} catch {
case e: IOException =>
e.printStackTrace()
}
// Remove the sample listener when done
controller.removeListener(listener)
}
}
class SampleListener extends Listener {
override def onInit(controller: Controller): Unit = {
println("init")
}
override def onConnect(controller: Controller): Unit = {
println("Connected")
//controller.enableGesture(Gesture.Type.TYPE_SWIPE)
controller.enableGesture(Gesture.Type.TYPE_CIRCLE)
//controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP)
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP)
}
/*
public void onDisconnect(Controller controller) {
//Note: not dispatched when running in a debugger.
System.out.println("Disconnected");
}
*/
/*
public void onExit(Controller controller) {
System.out.println("Exited");
}
*/
override def onFrame(controller: Controller): Unit = {
// Get the most recent frame and report some basic information
val frame: Frame = controller.frame()
println("Frame id: " + frame.id()
+ ", timestamp: " + frame.timestamp()
+ ", hands: " + frame.hands().count()
+ ", fingers: " + frame.fingers().count()
+ ", tools: " + frame.tools().count()
+ ", gestures " + frame.gestures().count())
if (!frame.hands().empty()) {
// Get the first hand
val hand: Hand = frame.hands().get(0)
// Check if the hand has any fingers
val fingers: FingerList = hand.fingers()
var avgPos = Vector.zero()
if (!fingers.empty()) {
// Calculate the hand's average finger tip position
for (i <- Range(0, fingers.count())) {
val finger = fingers.get(i)
avgPos = avgPos.plus(finger.tipPosition())
}
avgPos = avgPos.divide(fingers.count())
println("Hand has " + fingers.count()
+ " fingers, average finger tip position: " + avgPos)
}
// Get the hand's sphere radius and palm position
println("Hand sphere radius: " + hand.sphereRadius()
+ " mm, palm position: " + hand.palmPosition())
// Get the hand's normal vector and direction
val normal: Vector = hand.palmNormal()
val direction: Vector = hand.direction()
// Calculate the hand's pitch, roll, and yaw angles
println("Hand pitch: " + Math.toDegrees(direction.pitch()) + " degrees, "
+ "roll: " + Math.toDegrees(normal.roll()) + " degrees, "
+ "yaw: " + Math.toDegrees(direction.yaw()) + " degrees")
}
val gestures: GestureList = frame.gestures()
val list_of_gesture = for (i <- Range(0, gestures.count())) yield {
gestures.get(i)
}
list_of_gesture.foreach(
gesture =>
gesture.`type`() match {
case Gesture.Type.TYPE_CIRCLE =>
val circle: CircleGesture = new CircleGesture(gesture)
val circle_direction = circle.pointable().direction().angleTo(circle.normal()) <= Math.PI/4
// Calculate clock direction using the angle between circle normal and pointable
val clockwiseness: String =
if (circle_direction) {
// Clockwise if angle is less than 90 degrees
"clockwise"
} else {
"counterclockwise"
}
// Calculate angle swept since last frame
val swept_angle: Double =
if (circle.state() != State.STATE_START) {
val previousUpdate: CircleGesture = new CircleGesture(controller.frame(1).gesture(circle.id()))
(circle.progress() - previousUpdate.progress()) * 2 * Math.PI
} else 0
println(s"Circle id: ${circle.id()}, ${circle.state()}, progress: ${circle.progress()}, radius: ${circle.radius()}, angle: ${Math.toDegrees(swept_angle)}, $clockwiseness"
)
case Gesture.Type.TYPE_SWIPE =>
val swipe: SwipeGesture = new SwipeGesture(gesture)
println("Swipe id: , %s, position: %s, direction: %s, speed: %f".format(swipe.state(), swipe.position(), swipe.direction(), swipe.speed()) )
case Gesture.Type.TYPE_SCREEN_TAP =>
val screenTap: ScreenTapGesture = new ScreenTapGesture(gesture)
println(s"Screen Tap id: ${screenTap.id()}, ${screenTap.state()}, position: ${screenTap.position()}, direction: ${screenTap.direction()}")
case Gesture.Type.TYPE_KEY_TAP =>
val keyTap: KeyTapGesture = new KeyTapGesture(gesture)
println(s"Key Tap id: ${keyTap.id()}, ${keyTap.state()}, position: ${keyTap.position()}, direction: ${keyTap.direction()}")
case _ =>
println("Unknown gesture type.")
}
)
}
//if (!frame.hands().empty() || !gestures.empty()) {
// System.out.println()
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment