Ejemplo n.º 1
0
void MantaController::update()
{
    if (!connected)
    {
        connected = getConnected();
        return;
    }
    
    // pad velocities
    int idx = 0;
    for (int row=0; row<6; row++)
    {
        for (int col=0; col<8; col++)
        {
            padVelocity[row][col] = ofLerp(padVelocity[row][col], getPad(row, col) - prevPad[row][col], 0.1);
            prevPad[row][col] = getPad(row, col);
            idx++;
        }
    }
    // slider velocities
    for (int i=0; i<2; i++)
    {
        sliderVelocity[i] = ofLerp(sliderVelocity[i], getSlider(i) - prevSlider[i], velocityLerpRate);
        prevSlider[i] = getSlider(i);
    }
    // button velocities
    for (int i=0; i<4; i++)
    {
        buttonVelocity[i] = ofLerp(buttonVelocity[i], getButton(i) - prevButton[i], velocityLerpRate);
        prevButton[i] = getButton(i);
    }
    
    // finger stats
    float _padSum = 0;
    float _padAverage = 0;
    float _width = 0;
    float _height = 0;
    float _whRatio = 0;
    float _numPads = 0;
    
    fingers.clear();
    fingerValues.clear();
    ofPoint fingersMin = ofPoint(1, 1);
    ofPoint fingersMax = ofPoint(0, 0);
    float currentValue;
    for (int row=0; row<6; row++)
    {
        for (int col=0; col<8; col++)
        {
            currentValue = getPad(row, col);
            if (currentValue > 0)
            {
                ofPoint fingerPos = getPositionAtPad(row, col);
                fingers.push_back(fingerPos);
                fingerValues.push_back(currentValue);
                _numPads+=1.0;
                _padSum += currentValue;
                if (fingerPos.x > fingersMax.x)   fingersMax.x = fingerPos.x;
                if (fingerPos.x < fingersMin.x)   fingersMin.x = fingerPos.x;
                if (fingerPos.y > fingersMax.y)   fingersMax.y = fingerPos.y;
                if (fingerPos.y < fingersMin.y)   fingersMin.y = fingerPos.y;
            }
        }
    }
    
    _padAverage = fingers.size() > 0 ? _padSum / _numPads : 0.0;
    
    float _perimeter = 0.0;
    float _averageInterFingerDistance = 0.0;
    
    // stats on finger groups
    if (fingers.size() < 2)
    {
        _width = 0;
        _height = 0;
        _whRatio = 0;
        _perimeter = 0;
        _averageInterFingerDistance = 0;
        fingersHull.resize(0);
        fingersHullNormalized.resize(0);
    }
    else if (fingers.size() == 2)
    {
        _width = fingersMax.x - fingersMin.x;
        _height = fingersMax.y - fingersMin.y;
        _whRatio = _width / (1.0 + _height);
        
        _perimeter = (pow(fingers[0].x - fingers[1].x, 2)+
                      pow(fingers[0].y - fingers[1].y, 2));
        
        _averageInterFingerDistance = _perimeter;
        fingersHull.resize(0);
        fingersHullNormalized.resize(0);
    }
    else
    {
        _width = fingersMax.x - fingersMin.x;
        _height = fingersMax.y - fingersMin.y;
        _whRatio = _width / _height;
        
        fingersHull = convexHull.getConvexHull(fingers);
        fingersHullNormalized.resize(fingersHull.size());
        for (int i=0; i<fingersHull.size(); i++)
        {
            fingersHullNormalized[i].x = (fingersHull[i].x - fingersMin.x) / (fingersMax.x - fingersMin.x);
            fingersHullNormalized[i].y = (fingersHull[i].y - fingersMin.y) / (fingersMax.y - fingersMin.y);
        }
        for (int i=0; i<fingersHull.size()-1; i++)
        {
            _perimeter += (pow(fingersHull[i].x - fingersHull[(i+1)].x, 2)+
                           pow(fingersHull[i].y - fingersHull[(i+1)].y, 2));
        }
        _averageInterFingerDistance = _perimeter / (float) (fingersHull.size()-1);
    }
    
    numPadsVelocity = ofLerp(numPadsVelocity, _numPads-numPads, velocityLerpRate);
    perimeterVelocity = ofLerp(perimeterVelocity, _perimeter-perimeter, velocityLerpRate);
    averageInterFingerDistanceVelocity = ofLerp(averageInterFingerDistanceVelocity, _averageInterFingerDistance-averageInterFingerDistance, velocityLerpRate);
    
    padSumVelocity = ofLerp(padSumVelocity, _padSum-padSum, velocityLerpRate);
    padAverageVelocity = ofLerp(padAverageVelocity, _padAverage-padAverage, velocityLerpRate);
    
    widthVelocity = ofLerp(widthVelocity, _width-padWidth, velocityLerpRate);
    heightVelocity = ofLerp(heightVelocity, _height-padHeight, velocityLerpRate);
    whRatioVelocity = ofLerp(whRatioVelocity, _whRatio-whRatio, velocityLerpRate);
    
    padWidth = _width;
    padHeight = _height;
    whRatio = _whRatio;
    perimeter = _perimeter;
    averageInterFingerDistance = _averageInterFingerDistance;
    padSum = _padSum;
    padAverage = _padAverage;
    numPads = _numPads;
    
    // centroid and weighted centroid
    ofPoint _centroid, _weightedCentroid;
    for (int i=0; i<fingers.size(); i++)
    {
        _centroid += fingers[i];
        _weightedCentroid += (fingers[i] * fingerValues[i] / padSum);
    }
    _centroid /= _numPads;
    
    centroidVelocityX = ofLerp(centroidVelocityX, _centroid.x-centroidX, velocityLerpRate);
    centroidVelocityY = ofLerp(centroidVelocityY, _centroid.y-centroidY, velocityLerpRate);
    weightedCentroidVelocityX = ofLerp(weightedCentroidVelocityX, _weightedCentroid.x-weightedCentroidX, velocityLerpRate);
    weightedCentroidVelocityY = ofLerp(weightedCentroidVelocityY, _weightedCentroid.y-weightedCentroidY, velocityLerpRate);
    
    centroidX = _centroid.x;
    centroidY = _centroid.y;
    weightedCentroidX = _weightedCentroid.x;
    weightedCentroidY = _weightedCentroid.y;
}
Ejemplo n.º 2
0
void MantaStats::update()
{
    ofxManta::update();
    
    if (!IsConnected()) {
        return;
    }
    
    // pad velocities
    int idx = 0;
    for (int row=0; row<6; row++)
    {
        for (int col=0; col<8; col++)
        {
            padVelocity[row][col] = ofLerp(padVelocity[row][col], getPad(row, col) - prevPad[row][col], 0.1);
            prevPad[row][col] = getPad(row, col);
            idx++;
        }
    }
    
    // slider velocities
    for (int i=0; i<2; i++)
    {
        sliderVelocity[i] = ofLerp(sliderVelocity[i], getSlider(i) - prevSlider[i], velocityLerpRate);
        prevSlider[i] = getSlider(i);
    }
    
    // button velocities
    for (int i=0; i<4; i++)
    {
        buttonVelocity[i] = ofLerp(buttonVelocity[i], getButton(i) - prevButton[i], velocityLerpRate);
        prevButton[i] = getButton(i);
    }
    
    // finger stats
    float _padSum = 0;
    float _padAverage = 0;
    float _width = 0;
    float _height = 0;
    float _whRatio = 0;
    float _numPads = 0;
    
    fingers.clear();
    fingerValues.clear();
    ofPoint fingersMin = ofPoint(1, 1);
    ofPoint fingersMax = ofPoint(0, 0);
    float currentValue;
    for (int row=0; row<6; row++)
    {
        for (int col=0; col<8; col++)
        {
            currentValue = getPad(row, col);
            if (currentValue > 0)
            {
                ofPoint fingerPos = getPositionAtPad(row, col);
                fingers.push_back(fingerPos);
                fingerValues.push_back(currentValue);
                _numPads+=1.0;
                _padSum += currentValue;
                if (fingerPos.x > fingersMax.x)   fingersMax.x = fingerPos.x;
                if (fingerPos.x < fingersMin.x)   fingersMin.x = fingerPos.x;
                if (fingerPos.y > fingersMax.y)   fingersMax.y = fingerPos.y;
                if (fingerPos.y < fingersMin.y)   fingersMin.y = fingerPos.y;
            }
        }
    }
    
    _padAverage = fingers.size() > 0 ? _padSum / _numPads : 0.0;
    
    float _perimeter = 0.0;
    float _area = 0.0;
    float _averageInterFingerDistance = 0.0;
    
    // stats on finger groups
    if (fingers.size() < 2)
    {
        _width = 0;
        _height = 0;
        _whRatio = 0;
        _perimeter = 0;
        _area = 0;
        _averageInterFingerDistance = 0;
        fingersHull.resize(0);
    }
    else if (fingers.size() == 2)
    {
        _width = fingersMax.x - fingersMin.x;
        _height = fingersMax.y - fingersMin.y;
        _whRatio = _width / (1.0 + _height);
        
        _perimeter = (pow(fingers[0].x - fingers[1].x, 2)+
                      pow(fingers[0].y - fingers[1].y, 2));
        _area = 0;
        
        _averageInterFingerDistance = _perimeter;
        fingersHull.resize(0);
    }
    else
    {
        _width = fingersMax.x - fingersMin.x;
        _height = fingersMax.y - fingersMin.y;
        _whRatio = _width / (1.0 + _height);
        
        fingersHull = convexHull.getConvexHull(fingers);
        for (int i=0; i<fingersHull.size()-1; i++) {
            _perimeter += (pow(fingersHull[i].x - fingersHull[(i+1)].x, 2)+
                           pow(fingersHull[i].y - fingersHull[(i+1)].y, 2));
        }
        _area = convexHull.getArea(fingersHull);
        _averageInterFingerDistance = _perimeter / (float) (fingersHull.size()-1);
    }
    
    numPadsVelocity = ofLerp(numPadsVelocity, _numPads-numPads, velocityLerpRate);
    perimeterVelocity = ofLerp(perimeterVelocity, _perimeter-perimeter, velocityLerpRate);
    areaVelocity = ofLerp(areaVelocity, _area-area, velocityLerpRate);
    averageInterFingerDistanceVelocity = ofLerp(averageInterFingerDistanceVelocity, _averageInterFingerDistance-averageInterFingerDistance, velocityLerpRate);
    
    padSumVelocity = ofLerp(padSumVelocity, _padSum-padSum, velocityLerpRate);
    padAverageVelocity = ofLerp(padAverageVelocity, _padAverage-padAverage, velocityLerpRate);
    
    widthVelocity = ofLerp(widthVelocity, _width-padWidth, velocityLerpRate);
    heightVelocity = ofLerp(heightVelocity, _height-padHeight, velocityLerpRate);
    whRatioVelocity = ofLerp(whRatioVelocity, _whRatio-whRatio, velocityLerpRate);
    
    if (abs(numPadsVelocity) < EPSILON)    numPadsVelocity = 0;
    if (abs(padSumVelocity) < EPSILON)    padSumVelocity = 0;
    if (abs(padAverageVelocity) < EPSILON)    padAverageVelocity = 0;
    if (abs(centroidVelocityX) < EPSILON)    centroidVelocityX = 0;
    if (abs(centroidVelocityY) < EPSILON)    centroidVelocityY = 0;
    if (abs(weightedCentroidVelocityX) < EPSILON)    weightedCentroidVelocityX = 0;
    if (abs(weightedCentroidVelocityY) < EPSILON)    weightedCentroidVelocityY = 0;
    if (abs(averageInterFingerDistanceVelocity) < EPSILON)    averageInterFingerDistanceVelocity = 0;
    if (abs(perimeterVelocity) < EPSILON)    perimeterVelocity = 0;
    if (abs(areaVelocity) < EPSILON)    areaVelocity = 0;
    if (abs(widthVelocity) < EPSILON)    widthVelocity = 0;
    if (abs(heightVelocity) < EPSILON)    heightVelocity = 0;
    if (abs(whRatioVelocity) < EPSILON)    whRatioVelocity = 0;
    
    // centroid and weighted centroid
    ofPoint _centroid, _weightedCentroid;
    for (int i=0; i<fingers.size(); i++)
    {
        _centroid += fingers[i];
        _weightedCentroid += (fingers[i] * fingerValues[i] / _padSum);
    }
    _centroid /= _numPads;
    
    centroidVelocityX = ofLerp(centroidVelocityX, _centroid.x-centroidX, velocityLerpRate);
    centroidVelocityY = ofLerp(centroidVelocityY, _centroid.y-centroidY, velocityLerpRate);
    weightedCentroidVelocityX = ofLerp(weightedCentroidVelocityX, _weightedCentroid.x-weightedCentroidX, velocityLerpRate);
    weightedCentroidVelocityY = ofLerp(weightedCentroidVelocityY, _weightedCentroid.y-weightedCentroidY, velocityLerpRate);
    
    // update stats
    compareStats(9, &padWidth, _width);
    compareStats(11, &padHeight, _height);
    compareStats(12, &whRatio, _whRatio);
    compareStats(6, &perimeter, _perimeter);
    compareStats(8, &area, _area);
    compareStats(10, &averageInterFingerDistance, _averageInterFingerDistance);
    compareStats(2, &padSum, _padSum);
    compareStats(4, &padAverage, _padAverage);
    compareStats(0, &numPads, _numPads);
    compareStats(1, &centroidX, _centroid.x);
    compareStats(3, &centroidY, _centroid.y);
    compareStats(5, &weightedCentroidX, _weightedCentroid.x);
    compareStats(7, &weightedCentroidY, _weightedCentroid.y);
}