Skip to content

Commit

Permalink
rename the file, do more exhaustive tests, all seem good
Browse files Browse the repository at this point in the history
  • Loading branch information
vberthiaume committed Dec 16, 2024
1 parent 2d13db4 commit f337d01
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 65 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_library(puara_gestures
include/puara/descriptors/tilt.h
include/puara/descriptors/touch.h

include/puara/utils/blobDetector.h
include/puara/utils/calibration.h
include/puara/utils/circularbuffer.h
include/puara/utils/leakyintegrator.h
Expand Down
12 changes: 4 additions & 8 deletions include/puara/descriptors/touch.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <puara/utils.h>
#include <puara/utils/blobDetection.h>
#include <puara/utils/blobDetector.h>
#include <puara/utils/leakyintegrator.h>

#include <cmath>
Expand All @@ -12,7 +12,7 @@ namespace puara_gestures
class Touch
{
public:
static const int maxNumBlobs = BlobDetection::maxNumBlobs;
static const int maxNumBlobs = BlobDetector::maxNumBlobs;
float touchAll = 0.0f; // f, 0--1
float touchTop = 0.0f; // f, 0--1
float touchMiddle = 0.0f; // f, 0--1
Expand All @@ -25,7 +25,7 @@ class Touch
// touch array
int touchSizeEdge = 4; // amount of touch stripes for top and bottom portions (arbitrary)

BlobDetection blob;
BlobDetector blob;
int brushCounter[maxNumBlobs]{};

// Arrays of LeakyIntegrator instances
Expand Down Expand Up @@ -64,12 +64,8 @@ class Touch
// normalized between 0 and 1
touchBottom = touchAverage(discrete_touch, (touchSize - touchSizeEdge), touchSize);

//NOW HERE -- not too sure what the heck that loop is, but that i is probably just == 4 lol
//I need to continue moving the blobPos logic to the blobDetector
//I should add a getBlobPos to it so this client doesn't have to store it itself

// 1D blob detection: used for brush
const auto movement = blob.blobDetection1D(discrete_touch, touchSize);
const auto movement = blob.detect1D(discrete_touch, touchSize);

// brush: direction and intensity of capsense brush motion
// rub: intensity of rub motion
Expand Down
57 changes: 0 additions & 57 deletions include/puara/utils/blobDetection.h

This file was deleted.

94 changes: 94 additions & 0 deletions include/puara/utils/blobDetector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#pragma once
namespace puara_gestures
{

struct BlobDetector
{
/** The maximum number of blobs that the algorithm should detect. */
static const int maxNumBlobs = 4;

/** The start index of detected blobs. */
int blobStartPos[maxNumBlobs]{};

/** The cached start index of detected blobs, from the previous time detect1D
* was called.
*/
int lastState_blobPos [maxNumBlobs]{};

/** "size" (amount of stripes) of each blob */
int blobSize[maxNumBlobs]{};

/** shows the "center"(index)of each blob */
float blobCenter[maxNumBlobs]{};

/** amount of detected blobs */
int blobAmount{};

/**
* @brief Detects contiguous regions (blobs) of `1`s in a 1D binary array and computes their movement.
*
* This function identifies blobs in the input binary array `touchArray`, calculates their start
* positions, sizes, and centers, and returns the movement of the blobs compared to their positions
* from the previous function call.
*
* @param touchArray Pointer to the 1D binary array representing touch data. Each element is expected to be 0 or 1.
* @param size The size of the `touchArray`.
* @return A vector of integers representing the movement of each blob's start position since the
* last invocation of `detect1D`. The size of the returned vector is `maxNumBlobs`.
*
* @note
* - The function updates the global variables `blobStartPos`, `blobSize`, `blobCenter`,
* and `lastState_blobPos`.
* - The number of blobs detected is limited by `maxNumBlobs`.
* - If the number of blobs exceeds `maxNumBlobs`, additional blobs are ignored.
*
* @warning
* - Ensure that `touchArray` has at least `size` elements to avoid out-of-bounds access.
* - The function relies on external global variables (`blobStartPos`, `blobSize`, `blobCenter`,
* `lastState_blobPos`, `maxNumBlobs`, `blobAmount`). Ensure they are initialized appropriately
* before calling the function.
*/
std::vector<int> detect1D(const int* const touchArray, const int size)
{

for(int i = 0; i < maxNumBlobs; i++)
{
//cache the last blobStartPos before clearing it
lastState_blobPos[i] = blobStartPos[i];
blobStartPos[i] = 0;
}

for(int stripe = 0; stripe < size;)
{
if(touchArray[stripe] == 1)
{
//start the blob
blobStartPos[blobAmount] = stripe;

//continue the blob until we no longer have 1s
int sizeCounter = 1;
while(touchArray[stripe + sizeCounter] == 1 && (stripe + sizeCounter) <= size)
sizeCounter++;

blobSize[blobAmount] = sizeCounter;
blobCenter[blobAmount] = stripe + (sizeCounter - 1.f) / 2.f;
stripe += sizeCounter;

if(++blobAmount >= maxNumBlobs)
break;
}
else
{
++stripe;
}
}

//return the movement since the last time detect1D was called
std::vector<int> movement(maxNumBlobs, 0);
for(int i = 0; i < maxNumBlobs; ++i)
movement[i] = blobStartPos[i] - lastState_blobPos[i];

return movement;
}
};
}
15 changes: 15 additions & 0 deletions tests/testing_touch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,25 @@ int main()
const int touchSize = 16;
int discrete_touch[touchSize] = {0};

// simulate a blob of size 1 starting at position 0
discrete_touch[0] = 1;

// simulate a blob of size 2 starting at position 5
discrete_touch[5] = 1;
discrete_touch[6] = 1;

// simulate a blob of size 3 starting at position 8
discrete_touch[8] = 1;
discrete_touch[9] = 1;
discrete_touch[10] = 1;

// simulate a blob of size 1 at position 12 -- commented out to test the end of the array in the next blob
//discrete_touch[12] = 1;

// simulate a blob of size 2 starting at position 14
discrete_touch[14] = 1;
discrete_touch[15] = 1;

// Update the touch data
touch.updateTouchArray(discrete_touch, touchSize);

Expand Down

0 comments on commit f337d01

Please sign in to comment.