Skip to content

Commit

Permalink
started implementing clustering algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
bgstaal committed Jul 15, 2014
1 parent c735d3b commit bb22656
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 7 deletions.
92 changes: 86 additions & 6 deletions src/ofxSensfloor.cpp
Expand Up @@ -19,9 +19,8 @@ const ofVec2f ofxSensfloor::TILE_SIZE_LARGE = ofVec2f(50, 100);
const int ofxSensfloor::BAUD_RATE_DEFAULT = 115200;

ofxSensfloor::ofxSensfloor ()
: _highlightColor(255, 0, 0, 255), _numCycles(0), threshold(0.145)
{
threshold = 0.145f;
_numCycles = 0;
_font.loadFont("Courier New", 40);
}

Expand Down Expand Up @@ -208,9 +207,13 @@ void ofxSensfloor::_parseMessage(vector<unsigned char> m)
for (int i = 9; i < 17; i++)
{
int value = (int)m[i] - 0x80;
t->second->fields[j].val = value / (float)0x80;
float val = value / (float)0x80;
t->second->fields[j].val = val;
t->second->hasActiveField = val >= threshold;
j++;
}

_updateActivePolygons();
}
else
{
Expand All @@ -223,6 +226,56 @@ void ofxSensfloor::_parseMessage(vector<unsigned char> m)
}
}

void ofxSensfloor::_updateActivePolygons()
{
_activeFields.clear();
_activeFieldsNotClustered.clear();
_clusters.clear();

// get active fields
for (vector<TilePtr>::iterator t = _tiles.begin(); t != _tiles.end(); t++)
{
TilePtr tile = *t;

// implement when not testing
/*
if (tile->hasActiveField)
{*/
for (vector<Field>::iterator f = tile->fields.begin(); f != tile->fields.end(); f++)
{
if (f->val > threshold)
{
_activeFields.push_back(*f);
_activeFieldsNotClustered.push_back(*f);
}
}
//}
}

while (_activeFieldsNotClustered.size())
{
vector<Field> cluster;

Field &f =_activeFieldsNotClustered[0];
vector<Field> neighbours = _findNeighbouringFields(f);
cluster.push_back(f);
_clusters.push_back(cluster);

_activeFieldsNotClustered.pop_front();
}

//cout << "num active fields: " << _activeFields.size() << endl;
}

vector<ofxSensfloor::Field> ofxSensfloor::_findNeighbouringFields(Field &field)
{
vector<Field> fields;



return fields;
}

ofMatrix4x4 ofxSensfloor::getTransform()
{
return _transform;
Expand Down Expand Up @@ -371,8 +424,16 @@ void ofxSensfloor::setup(unsigned char roomID1, unsigned char roomID2, int numCo
_updateTransform();
}

void ofxSensfloor::setHighlightColor(const ofColor &c)
{
_highlightColor = c;
}

void ofxSensfloor::draw(bool drawIDs)
{
//TODO: remove this call when not developing this functionality anymore
_updateActivePolygons();

ofPushStyle();
ofSetLineWidth(1.0f);
glPointSize(10);
Expand All @@ -389,7 +450,7 @@ void ofxSensfloor::draw(bool drawIDs)

if (val > threshold)
{
ofSetColor(255, 0, 0, 255);
//ofSetColor(_highlightColor);
}
else
{
Expand All @@ -405,16 +466,35 @@ void ofxSensfloor::draw(bool drawIDs)
ofVec3f offset = ofVec3f(0, 0, 40 * val);
//ofTriangle(v0+offset, v1+offset, v2+offset);
}

ofNoFill();
ofSetLineWidth(3.0f);

for (int i = 0; i < _clusters.size(); i++)
{
ofFloatColor f;
f.setBrightness(1.0f);
f.setSaturation(1.0f);
f.setHue(abs(sin(i*10)));

ofSetColor(f);
for (int j = 0; j < _clusters[i].size(); j++)
{
Field &f = _clusters[i][j];
ofTriangle(_verticesTransformed[f.index0], _verticesTransformed[f.index1], _verticesTransformed[f.index2]);
}
}

stringstream ss;
ss << (int)tile->tileID1 << "," << (int)tile->tileID2 << endl;

ofSetColor(255, 255, 255);
ofVec3f c = _verticesTransformed[tile->fields[0].index0];
//ofDrawBitmapString(ss.str(), c);

if (drawIDs)
{
stringstream ss;
ss << (int)tile->tileID1 << "," << (int)tile->tileID2 << endl;

ofPushMatrix();
ofTranslate(c);
ofScale(.25f, .25f);
Expand Down
13 changes: 12 additions & 1 deletion src/ofxSensfloor.h
Expand Up @@ -23,6 +23,7 @@ class ofxSensfloor : public ofThread
void start(string portName, int baudRate = BAUD_RATE_DEFAULT);
void start(int deviceNumber, int baudRate = BAUD_RATE_DEFAULT);
void stop();
void setHighlightColor(const ofColor &c);
//void addTile(unsigned char tileID1, unsigned char tileID2, ofVec3f pos);

void draw(bool drawIDs = false);
Expand All @@ -33,34 +34,44 @@ class ofxSensfloor : public ofThread

struct Field
{
Field(int index0, int index1, int index2, unsigned char val = 0) : index0(index0), index1(index1), index2(index2), val() {};
Field(int index0, int index1, int index2, unsigned char val = 0) : index0(index0), index1(index1), index2(index2), val(ofNoise(index0/10.0f, index1/10.0f, index2/10.0f) * .2f) {};
int index0, index1, index2;
float val;
};

struct Tile
{
Tile() : hasActiveField(false) {};
char tileID1, tileID2;
vector<Field> fields;
float latestUpdateTime;
bool hasActiveField;
};

typedef ofPtr<Tile> TilePtr;

ofColor _highlightColor;
unsigned int _numCycles;
char _roomID1, _roomID2;
ofSerial _serial;
vector<ofVec3f> _vertices;
vector<ofVec3f> _verticesTransformed;
vector<TilePtr> _tiles;
vector<Field> _activeFields;
deque<Field> _activeFieldsNotClustered;
vector<vector<Field> > _clusters;
vector<ofPolyline> _activePolygons;
map<pair<unsigned char, unsigned char>, TilePtr> _tileByIDs;
ofVboMesh _mesh;
vector<unsigned char> _latestMessage;
ofTrueTypeFont _font;
ofMatrix4x4 _transform;

vector<Field> _findNeighbouringFields(Field &field);

void threadedFunction();
void _readSensorData();
void _updateActivePolygons();
void _parseMessage(vector<unsigned char> msg);
void _sendMessage(vector<unsigned char> msg);
void _sendPollMessage(unsigned char tileID1, unsigned char tileID2);
Expand Down

0 comments on commit bb22656

Please sign in to comment.