Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //sender ofApp.h
- #pragma once
- #include "ofxNetwork.h"
- #include "ofMain.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;
- };
- /*struct udpMsg
- {
- int nBlobs;
- blob_t* blobs;
- int mMarks;
- mark_t* marks;
- };*/
- 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);
- ofPoint randomPoint();
- blob_t genBlob();
- mark_t genMark();
- string ptToStr(const ofPoint& aPt);
- string edgesToStr(const edges_t& edges);
- string blobMsgAssembly(const blob_t& blob, const string& startMsg);
- string markMsgAssembly(const mark_t& mark, const string& startMsg);
- blob_t bl;
- mark_t mk;
- string msg;
- ofPolyline aPoly;
- bool latch;
- ofxUDPManager udpConnection;
- };
- //sender ofApp.cpp
- #include "ofApp.h"
- #define RECONNECT_TIME 400
- #define CONTENT_SEPARATOR "[/b]"
- //--------------------------------------------------------------
- void ofApp::setup(){
- // we don't want t 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);o be running to fast
- ofSetVerticalSync(true);
- ofSetFrameRate(60);
- msg="";
- bl = genBlob();
- mk=genMark();
- latch = false;
- //create the socket and set to send to 127.0.0.1:11999
- udpConnection.Create();
- udpConnection.Connect("127.0.0.1",11999);
- udpConnection.SetNonBlocking(true);
- }
- //--------------------------------------------------------------
- void ofApp::update()
- {
- if (latch)
- {
- msg = "msgLen = " + ofToString(msg.length()) + " || " + msg;
- latch = false;
- }
- }
- //--------------------------------------------------------------
- void ofApp::draw(){
- ofSetColor(20, 20, 20);
- ofDrawBitmapString("openFrameworks UDP Send Example (left button sends blob, right button sends mark", 15, 30);
- ofDrawBitmapString(msg.c_str(), 10, 10);
- stringstream ss;
- ss << "\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)
- {
- msg="";
- switch (button)
- {
- case 0: //left button
- for (unsigned int i = 0 ; i < 10 ; i++)
- {
- bl = genBlob();
- cout << "bl.id[" << i << "]=" << bl.id << endl;
- msg = blobMsgAssembly(bl,"blob1");
- udpConnection.Send(msg.c_str(),msg.length());
- }
- break;
- case 2: //right button
- for (unsigned int i = 0 ; i < 10 ; i++)
- {
- mk = genMark();
- cout << "mk.id[" << i << "]=" << mk.id << endl;
- msg = markMsgAssembly(mk,"mark1");
- udpConnection.Send(msg.c_str(),msg.length());
- }
- break;
- default: break;
- }
- latch = true;
- }
- //--------------------------------------------------------------
- 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){
- }
- ofPoint ofApp::randomPoint()
- {
- ofPoint aPt;
- aPt.x = ofRandom(0, 1280);
- aPt.y = ofRandom(0, 1024);
- aPt.z = ofRandom(0, 1024);
- return aPt;
- }
- ofApp::blob_t ofApp::genBlob()
- {
- struct blob_t aBlob;
- unsigned int nPts = round(ofRandom(100,1500));
- aBlob.id = round(ofRandom(0,1000));
- aBlob.centroid = randomPoint();
- aBlob.edges.leftMost = randomPoint();
- aBlob.edges.rightMost = randomPoint();
- aBlob.edges.topMost = randomPoint();
- aBlob.edges.bottomLeftMost = randomPoint();
- aBlob.edges.bottomRightMost = randomPoint();
- aBlob.blobOutline = &aPoly;
- aBlob.blobOutline->clear();
- for (unsigned int i = 0 ; i < nPts ; i++ )
- {
- aBlob.blobOutline->addVertex(randomPoint());
- }
- return aBlob;
- }
- ofApp::mark_t ofApp::genMark()
- {
- mark_t aMark;
- aMark.id = round(ofRandom(0,1000));
- aMark.centroid = randomPoint();
- return aMark;
- }
- string ofApp::ptToStr(const ofPoint& aPt)
- {
- string str;
- str = ofToString(aPt.x) + "|" \
- + ofToString(aPt.y) + "|" \
- + ofToString(aPt.z) + "[/p]" ;
- return str;
- }
- string ofApp::edgesToStr(const edges_t& edges)
- {
- string str;
- str = \
- ptToStr(edges.leftMost) + \
- ptToStr(edges.rightMost) + \
- ptToStr(edges.topMost) + \
- ptToStr(edges.bottomLeftMost) + \
- ptToStr(edges.bottomRightMost);
- return str;
- }
- string ofApp::blobMsgAssembly(const ofApp::blob_t& blob, const string& startMsg)
- {
- string str;
- str += startMsg ;
- str += CONTENT_SEPARATOR ;
- str += ofToString(blob.id) ;
- str += CONTENT_SEPARATOR ;
- str += ptToStr(blob.centroid) ;
- str += edgesToStr(blob.edges) ;
- str += CONTENT_SEPARATOR ;
- for (unsigned int i = 0 ; i < blob.blobOutline->size() ; i++)
- {
- str += ptToStr( (*blob.blobOutline)[i] );
- }
- str += "[/EOM]" ;
- return str;
- }
- string ofApp::markMsgAssembly(const mark_t& mark, const string& startMsg)
- {
- string str;
- str += startMsg ;
- str += CONTENT_SEPARATOR ;
- str += ofToString(mark.id) ;
- str += CONTENT_SEPARATOR ;
- str += ptToStr(mark.centroid) ;
- str += "[/EOM]" ;
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement