diff --git a/src/ofxSensfloor.cpp b/src/ofxSensfloor.cpp index 91bf60c..dfa293f 100644 --- a/src/ofxSensfloor.cpp +++ b/src/ofxSensfloor.cpp @@ -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); } @@ -208,9 +207,13 @@ void ofxSensfloor::_parseMessage(vector 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 { @@ -223,6 +226,56 @@ void ofxSensfloor::_parseMessage(vector m) } } +void ofxSensfloor::_updateActivePolygons() +{ + _activeFields.clear(); + _activeFieldsNotClustered.clear(); + _clusters.clear(); + + // get active fields + for (vector::iterator t = _tiles.begin(); t != _tiles.end(); t++) + { + TilePtr tile = *t; + + // implement when not testing + /* + if (tile->hasActiveField) + {*/ + for (vector::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 cluster; + + Field &f =_activeFieldsNotClustered[0]; + vector neighbours = _findNeighbouringFields(f); + cluster.push_back(f); + _clusters.push_back(cluster); + + _activeFieldsNotClustered.pop_front(); + } + + //cout << "num active fields: " << _activeFields.size() << endl; +} + +vector ofxSensfloor::_findNeighbouringFields(Field &field) +{ + vector fields; + + + + return fields; +} + ofMatrix4x4 ofxSensfloor::getTransform() { return _transform; @@ -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); @@ -389,7 +450,7 @@ void ofxSensfloor::draw(bool drawIDs) if (val > threshold) { - ofSetColor(255, 0, 0, 255); + //ofSetColor(_highlightColor); } else { @@ -405,9 +466,25 @@ 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]; @@ -415,6 +492,9 @@ void ofxSensfloor::draw(bool drawIDs) if (drawIDs) { + stringstream ss; + ss << (int)tile->tileID1 << "," << (int)tile->tileID2 << endl; + ofPushMatrix(); ofTranslate(c); ofScale(.25f, .25f); diff --git a/src/ofxSensfloor.h b/src/ofxSensfloor.h index 19ad71f..5b9d42d 100644 --- a/src/ofxSensfloor.h +++ b/src/ofxSensfloor.h @@ -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); @@ -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 fields; float latestUpdateTime; + bool hasActiveField; }; typedef ofPtr TilePtr; + ofColor _highlightColor; unsigned int _numCycles; char _roomID1, _roomID2; ofSerial _serial; vector _vertices; vector _verticesTransformed; vector _tiles; + vector _activeFields; + deque _activeFieldsNotClustered; + vector > _clusters; + vector _activePolygons; map, TilePtr> _tileByIDs; ofVboMesh _mesh; vector _latestMessage; ofTrueTypeFont _font; ofMatrix4x4 _transform; + vector _findNeighbouringFields(Field &field); + void threadedFunction(); void _readSensorData(); + void _updateActivePolygons(); void _parseMessage(vector msg); void _sendMessage(vector msg); void _sendPollMessage(unsigned char tileID1, unsigned char tileID2);