Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //receiver ofApp.h
- #pragma once
- #include "ofMain.h"
- #include "ofxNetwork.h"
- class ofApp : public ofBaseApp{
- public:
- struct edges_t
- {
- ofPoint leftMost;
- ofPoint rightMost;
- ofPoint topMost;
- ofPoint bottomLeftMost;
- ofPoint bottomRightMost;
- };
- struct blob_t
- {
- int id;
- ofPoint centroid;
- edges_t edges;
- ofPolyline blobOutline;
- };
- struct mark_t
- {
- int id;
- ofPoint centroid;
- };
- void setup();
- void update();
- void draw();
- void keyPressed(int key);
- void keyReleased(int key);
- void mouseMoved(int x, int y );
- void mouseDragged(int x, int y, int button);
- void mousePressed(int x, int y, int button);
- void mouseReleased(int x, int y, int button);
- void windowResized(int w, int h);
- void dragEvent(ofDragInfo dragInfo);
- void gotMessage(ofMessage msg);
- ofxUDPManager udpConnection;
- int extractBlobParts(const vector<string>& vecOfPartStrings, blob_t& aBlob);
- int extractMarkParts(const vector<string>& vecOfPartStrings, mark_t& aMark);
- int extractPoints(const string& pointsString, vector<ofPoint>& pts);
- int extractPoint(const string pointString , ofPoint& aPt);
- blob_t bl;
- mark_t mk;
- int msgLen;
- };
- //receiver ofApp.cpp
- #include "ofApp.h"
- #define CONTENT_SEPARATOR "[/b]"
- //--------------------------------------------------------------
- void ofApp::setup(){
- //we run at 60 fps!
- ofSetVerticalSync(true);
- ofSetFrameRate(60);
- ofSetLogLevel(OF_LOG_NOTICE);
- msgLen = 0 ;
- //create the socket and bind to port 11999
- udpConnection.Create();
- udpConnection.Bind(11999);
- udpConnection.SetNonBlocking(true);
- //ofSetBackgroundAuto(false);
- ofBackground(255, 255, 255);
- }
- //--------------------------------------------------------------
- void ofApp::update(){
- char udpMessage[1000000];
- int receivedBytesNum = udpConnection.Receive(udpMessage,1000000);
- if (receivedBytesNum >= 1000000)
- ofLog(OF_LOG_NOTICE, "maxed out udp buffer %d!",receivedBytesNum);
- if (receivedBytesNum == SOCKET_TIMEOUT)
- ofLog(OF_LOG_ERROR, "socket_timeout");
- string message=udpMessage;
- if(message!="")
- {
- msgLen = message.length();
- //cout << "msgLen=" << msgLen << " receivedBytes=" << receivedBytesNum << endl;
- //I may receive many messages in one read. splitting them...
- vector<string> msgs = ofSplitString(message,"[/EOM]");
- if (msgs.size()-1 > 1)
- ofLog(OF_LOG_NOTICE, "received %d messages!",msgs.size()-1);
- //the -1 just above and just below are used because of the way ofSplitString works
- //it creates vector entries for both sides of a "separator" even if there are no
- //chars after the separator (ie if it is a terminator)
- for (unsigned int i = 0 ; i < msgs.size()-1 ; i++ )
- {
- cout << "-------------------pass[" << i << "]---------------------" << endl;
- vector<string> msgParts = ofSplitString(msgs[i],CONTENT_SEPARATOR);
- if ( msgParts[0] == "blob1" )
- {
- blob_t aBlob;
- if ( extractBlobParts(msgParts, aBlob) >= 0 )
- {
- bl = aBlob ;
- cout << "bl.id[" << i << "]=" << bl.id << endl;
- }
- else
- {
- ofLog(OF_LOG_ERROR, "blob extraction error. did not alter values %d!",i);
- }
- }
- else if ( msgParts[0] == "mark1" )
- {
- mark_t aMark;
- if ( extractMarkParts(msgParts, aMark) >= 0 )
- {
- mk = aMark;
- cout << "mk.id[" << i << "]=" << mk.id << endl;
- }
- else
- {
- ofLog(OF_LOG_ERROR, "mark extraction error. did not alter values %d!",i);
- }
- }
- else
- {
- ofLog(OF_LOG_ERROR, "shouldn't be here");
- }
- }//end for
- } //end if not empty message
- }
- //--------------------------------------------------------------
- void ofApp::draw(){
- ofFill();
- ofSetHexColor(0xFFFFFF);
- ofRect(0,0,200,30);
- ofSetHexColor(0x101010);
- ofDrawBitmapString("UDP Receiver Example ", 10, 20);
- stringstream ss;
- ss << "msgLen " << msgLen << "\n";
- ss << "blob id " << bl.id << "\n";
- ss << "blob centroid " << bl.centroid << "\n";
- ss << "leftmost " << bl.edges.leftMost << "\n";
- ss << "rightMost " << bl.edges.rightMost << "\n";
- ss << "topMost " << bl.edges.topMost << "\n";
- ss << "bottomLeftMost " << bl.edges.bottomLeftMost << "\n";
- ss << "bottomRightMost " << bl.edges.bottomRightMost << "\n";
- ss << "outline size = " << bl.blobOutline.size() << "\n";
- for (unsigned int i = 0 ; i < bl.blobOutline.size() ; i++ )
- ss << bl.blobOutline[i] ;
- ss << "\n";
- ss << "mark id " << mk.id << "\n";
- ss << "mark centroid " << mk.centroid << "\n";
- ofDrawBitmapString(ss.str(), 10, 40);
- }
- //--------------------------------------------------------------
- void ofApp::keyPressed(int key){
- }
- //--------------------------------------------------------------
- void ofApp::keyReleased(int key){
- }
- //--------------------------------------------------------------
- void ofApp::mouseMoved(int x, int y ){
- }
- //--------------------------------------------------------------
- void ofApp::mouseDragged(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::mousePressed(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::mouseReleased(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::windowResized(int w, int h){
- }
- //--------------------------------------------------------------
- void ofApp::gotMessage(ofMessage msg){
- }
- //--------------------------------------------------------------
- void ofApp::dragEvent(ofDragInfo dragInfo){
- }
- //==============================================================
- int ofApp::extractBlobParts(const vector<string>& vecOfPartStrings, blob_t& aBlob)
- {
- int returnValue = 0 ;
- if (vecOfPartStrings.size() < 4)
- {
- ofLog(OF_LOG_ERROR, "less than 4 parts in blob msg!");
- returnValue = -1;
- }
- else if (vecOfPartStrings.size() >= 4)
- {
- if (vecOfPartStrings.size() > 4)
- ofLog(OF_LOG_WARNING, "more than 4 parts in blob msg!");
- aBlob.id = atoi(vecOfPartStrings[1].c_str());
- //the 10 * is just to make clear which of the 2 return values that follow
- //cuased the
- vector<ofPoint> featuresPts ;
- returnValue = 10 * extractPoints(vecOfPartStrings[2], featuresPts);
- vector<ofPoint> polylinePts ;
- returnValue += extractPoints(vecOfPartStrings[3], polylinePts);
- if ( returnValue < 0 )
- {
- }
- else
- {
- if (featuresPts.size() > 6 )
- {
- ofLog(OF_LOG_WARNING, "edges points vector has more than 6 points!");
- cout << vecOfPartStrings[2] << "!" << endl;
- for (unsigned int i = 0 ; i < featuresPts.size() ; i++ )
- cout << featuresPts[i] <<endl;
- }
- aBlob.centroid = featuresPts[0];
- aBlob.edges.leftMost = featuresPts[1];
- aBlob.edges.rightMost = featuresPts[2];
- aBlob.edges.topMost = featuresPts[3];
- aBlob.edges.bottomLeftMost = featuresPts[4];
- aBlob.edges.bottomRightMost = featuresPts[5];
- if (aBlob.blobOutline.size() > 0)
- aBlob.blobOutline.clear();
- aBlob.blobOutline.addVertices( polylinePts );
- }
- }
- return returnValue;
- }
- int ofApp::extractMarkParts(const vector<string>& vecOfPartStrings, mark_t& aMark)
- {
- int returnValue = 0 ;
- if (vecOfPartStrings.size() < 3)
- {
- ofLog(OF_LOG_ERROR, "less than 3 parts in mark msg!");
- returnValue = -1;
- }
- else if (vecOfPartStrings.size() >= 3)
- {
- if (vecOfPartStrings.size() > 3)
- ofLog(OF_LOG_WARNING, "more than 3 parts in mark msg!");
- aMark.id = atoi(vecOfPartStrings[1].c_str());
- vector<ofPoint> markPts ;
- returnValue = extractPoints(vecOfPartStrings[2] , markPts);
- if ( returnValue < 0 )
- {
- }
- else
- {
- if ( markPts.size() >= 1 )
- {
- if ( markPts.size() > 1 )
- ofLog(OF_LOG_WARNING, "mark points vector has more than 1 points!");
- aMark.centroid = markPts[0];
- }
- else
- {
- }
- }
- }
- return returnValue;
- }
- int ofApp::extractPoints(const string& pointsString, vector<ofPoint>& pts)
- {
- vector<string> points = ofSplitString(pointsString,"[/p]");
- //the -1 is here because the last point has a "[/p]" terminator as all the previous points
- //this leads ofSplitString() to create an extra entry in the vector that has 0,0,0 coordinates.
- //it can be safely ignored
- for (unsigned int i = 0 ; i < points.size()-1 ; i++ )
- {
- ofPoint pt ;
- if ( extractPoint(points[i],pt) > 0 )
- {
- pts.push_back( pt );
- }
- else
- {
- break;
- }
- }
- int vecPointsSize = pts.size() - ( points.size()-1 );
- return vecPointsSize;
- }
- int ofApp::extractPoint(const string pointString , ofPoint& aPt)
- {
- vector<string> coordinate = ofSplitString(pointString,"|");
- int pointSize = coordinate.size();
- if (coordinate.size() >= 3 )
- {
- if (coordinate.size() > 3 )
- {
- ofLog(OF_LOG_WARNING, "point string has more than 3 points!");
- cout << pointString << "!" << endl;
- }
- aPt.x = atof(coordinate[0].c_str());
- aPt.y = atof(coordinate[1].c_str());
- aPt.z = atof(coordinate[2].c_str());
- }
- else if (coordinate.size() < 3 )
- {
- ofLog(OF_LOG_ERROR, "point string has less than 3 points!");
- aPt = ofPoint(-1,-1,-1) ;
- pointSize = coordinate.size() - 3 ;
- }
- return pointSize;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement