Skip to content

Commit

Permalink
Fixes #7 by manually normalising values
Browse files Browse the repository at this point in the history
Avoids using the normalise() Leap function and manually normalises values before OSC forwarding.
  • Loading branch information
NiccoloGranieri committed Sep 13, 2019
1 parent 8215fc0 commit e85d18a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
46 changes: 30 additions & 16 deletions Source/MainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@

#include "MainComponent.h"

//==============================================================================
const float grabAndJNormalise(float value, const NormalisableRange<float>& range)
{
const auto normalisedValue = range.convertTo0to1(value);
return jmap(normalisedValue, -1.0f, 1.0f);
}

const float grabAndNormalise(float value, const NormalisableRange<float>& range)
{
const auto normalisedValue = range.convertTo0to1(value);
return normalisedValue;
}

//==============================================================================
MainContentComponent::MainContentComponent ()
{
Expand Down Expand Up @@ -213,28 +226,29 @@ void MainContentComponent::sendDenormalisedValues(Leap::Hand hand, StringRef han

void MainContentComponent::sendNormalisedValues(Leap::Hand hand, StringRef handedness)
{
auto normPalmPos = hand.stabilizedPalmPosition().normalized();
auto palmPosition = hand.stabilizedPalmPosition();

OSCMessage oscPalm = OSCMessage(handedness + "/palm");
oscPalm.addFloat32(normPalmPos.x);
oscPalm.addFloat32(normPalmPos.y);
oscPalm.addFloat32(normPalmPos.z);
oscPalm.addFloat32(grabAndJNormalise(palmPosition.x, xnzAxisRange));
oscPalm.addFloat32(grabAndNormalise(palmPosition.y, yAxisRange));
oscPalm.addFloat32(grabAndJNormalise(palmPosition.z, xnzAxisRange));
sender.send(oscPalm);

auto normWristPos = hand.wristPosition().normalized();
auto wristPosition = hand.wristPosition();

OSCMessage oscWrist = OSCMessage(handedness + "/wrist");
oscWrist.addFloat32(normWristPos.x);
oscWrist.addFloat32(normWristPos.y);
oscWrist.addFloat32(normWristPos.z);
oscWrist.addFloat32(grabAndJNormalise(wristPosition.x, xnzAxisRange));
oscWrist.addFloat32(grabAndNormalise(wristPosition.y, yAxisRange));
oscWrist.addFloat32(grabAndJNormalise(wristPosition.z, xnzAxisRange));
sender.send(oscWrist);

auto normRotation = hand.palmNormal().normalized();
auto palmRotation = hand.palmNormal();

OSCMessage oscRotation = OSCMessage(handedness + "/rotation");
oscRotation.addFloat32(normRotation.x);
oscRotation.addFloat32(normRotation.y);
oscRotation.addFloat32(normRotation.z);

oscRotation.addFloat32(palmPosition.x);
oscRotation.addFloat32(palmRotation.y);
oscRotation.addFloat32(palmRotation.z);
sender.send(oscRotation);

for (auto& finger : hand.fingers())
Expand All @@ -246,12 +260,12 @@ void MainContentComponent::sendNormalisedValues(Leap::Hand hand, StringRef hande
auto boneType = static_cast<Leap::Bone::Type>(i);
auto bone = finger.bone(boneType);

auto normJointPos = bone.nextJoint().normalized();
auto jointPosition = bone.nextJoint();

OSCMessage oscJoint = OSCMessage(String(handedness + jointTypes[finger.type()] + joints[i]));
oscJoint.addFloat32(normJointPos.x);
oscJoint.addFloat32(normJointPos.y);
oscJoint.addFloat32(normJointPos.z);
oscJoint.addFloat32(grabAndJNormalise(jointPosition.x, xnzAxisRange));
oscJoint.addFloat32(grabAndNormalise(jointPosition.y, yAxisRange));
oscJoint.addFloat32(grabAndJNormalise(jointPosition.z, xnzAxisRange));
sender.send(oscJoint);
}
}
Expand Down
4 changes: 4 additions & 0 deletions Source/MainComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class MainContentComponent : public Component,
void sendNormalisedValues(Leap::Hand hand, StringRef handedness);
void sendDenormalisedValues(Leap::Hand hand, StringRef handedness);

//==============================================================================
NormalisableRange<float> xnzAxisRange{ -750.0f, 750.0f, 0.00001f };
NormalisableRange<float> yAxisRange{ 0.0f, 750.0f, 0.00001f };

//==============================================================================
Label ipAddress;
Label port;
Expand Down

0 comments on commit e85d18a

Please sign in to comment.