Advertisement
synthnassizer

customUdpSender

Nov 29th, 2014
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.75 KB | None | 0 0
  1. //sender ofApp.h
  2. #pragma once
  3.  
  4. #include "ofxNetwork.h"
  5. #include "ofMain.h"
  6.  
  7. class ofApp : public ofBaseApp{
  8.  
  9.     public:
  10.         struct edges_t
  11.         {
  12.             ofPoint leftMost;
  13.             ofPoint rightMost;
  14.             ofPoint topMost;
  15.             ofPoint bottomLeftMost;
  16.             ofPoint bottomRightMost;
  17.         };
  18.         struct blob_t
  19.         {
  20.             int id;
  21.             ofPoint centroid;
  22.             edges_t edges;
  23.             ofPolyline* blobOutline;
  24.         };
  25.         struct mark_t
  26.         {
  27.             int id;
  28.             ofPoint centroid;
  29.         };
  30.         /*struct udpMsg
  31.         {
  32.             int nBlobs;
  33.             blob_t* blobs;
  34.             int mMarks;
  35.             mark_t* marks;
  36.         };*/
  37.         void setup();
  38.         void update();
  39.         void draw();
  40.  
  41.         void keyPressed(int key);
  42.         void keyReleased(int key);
  43.         void mouseMoved(int x, int y );
  44.         void mouseDragged(int x, int y, int button);
  45.         void mousePressed(int x, int y, int button);
  46.         void mouseReleased(int x, int y, int button);
  47.         void windowResized(int w, int h);
  48.         void dragEvent(ofDragInfo dragInfo);
  49.         void gotMessage(ofMessage msg);
  50.  
  51.  
  52.         ofPoint randomPoint();
  53.         blob_t genBlob();
  54.         mark_t genMark();
  55.         string ptToStr(const ofPoint& aPt);
  56.         string edgesToStr(const edges_t& edges);
  57.         string blobMsgAssembly(const blob_t& blob, const string& startMsg);
  58.         string markMsgAssembly(const mark_t& mark, const string& startMsg);
  59.  
  60.         blob_t bl;
  61.         mark_t mk;
  62.         string msg;
  63.         ofPolyline aPoly;
  64.         bool latch;
  65.  
  66.         ofxUDPManager udpConnection;
  67. };
  68.  
  69. //sender ofApp.cpp
  70. #include "ofApp.h"
  71.  
  72. #define RECONNECT_TIME 400
  73. #define CONTENT_SEPARATOR   "[/b]"
  74.  
  75. //--------------------------------------------------------------
  76. void ofApp::setup(){
  77.     // we don't want t      void update();
  78.         void draw();
  79.  
  80.         void keyPressed(int key);
  81.         void keyReleased(int key);
  82.         void mouseMoved(int x, int y );
  83.         void mouseDragged(int x, int y, int button);
  84.         void mousePressed(int x, int y, int button);
  85.         void mouseReleased(int x, int y, int button);
  86.         void windowResized(int w, int h);
  87.         void dragEvent(ofDragInfo dragInfo);
  88.         void gotMessage(ofMessage msg);o be running to fast
  89.     ofSetVerticalSync(true);
  90.     ofSetFrameRate(60);
  91.  
  92.     msg="";
  93.     bl = genBlob();
  94.     mk=genMark();
  95.  
  96.     latch = false;
  97.  
  98.     //create the socket and set to send to 127.0.0.1:11999
  99.     udpConnection.Create();
  100.     udpConnection.Connect("127.0.0.1",11999);
  101.     udpConnection.SetNonBlocking(true);
  102.  
  103.  
  104. }
  105.  
  106. //--------------------------------------------------------------
  107. void ofApp::update()
  108. {
  109.     if (latch)
  110.     {
  111.         msg = "msgLen = " + ofToString(msg.length()) + " || " + msg;
  112.         latch = false;
  113.     }
  114. }
  115.  
  116. //--------------------------------------------------------------
  117. void ofApp::draw(){
  118.  
  119.     ofSetColor(20, 20, 20);
  120.     ofDrawBitmapString("openFrameworks UDP Send Example (left button sends blob, right button sends mark", 15, 30);
  121.     ofDrawBitmapString(msg.c_str(), 10, 10);
  122.  
  123.     stringstream ss;
  124.     ss << "\n";
  125.     ss << "blob id " << bl.id << "\n";
  126.     ss << "blob centroid " << bl.centroid << "\n";
  127.     ss << "leftmost " << bl.edges.leftMost << "\n";
  128.     ss << "rightMost " << bl.edges.rightMost << "\n";
  129.     ss << "topMost " << bl.edges.topMost << "\n";
  130.     ss << "bottomLeftMost " << bl.edges.bottomLeftMost << "\n";
  131.     ss << "bottomRightMost " << bl.edges.bottomRightMost << "\n";
  132.     ss << "outline size = " << bl.blobOutline->size() << "\n";
  133.     for (unsigned int i = 0 ; i < bl.blobOutline->size() ; i++ )
  134.         ss << (*bl.blobOutline)[i] ;
  135.     ss << "\n";
  136.  
  137.     ss << "mark id " << mk.id << "\n";
  138.     ss << "mark centroid " << mk.centroid << "\n";
  139.     ofDrawBitmapString(ss.str(), 10, 40);
  140. }
  141.  
  142. //--------------------------------------------------------------
  143. void ofApp::keyPressed(int key){
  144.  
  145. }
  146.  
  147. //--------------------------------------------------------------
  148. void ofApp::keyReleased(int key){
  149.  
  150. }
  151.  
  152. //--------------------------------------------------------------
  153. void ofApp::mouseMoved(int x, int y ){
  154.  
  155. }
  156.  
  157. //--------------------------------------------------------------
  158. void ofApp::mouseDragged(int x, int y, int button){
  159.  
  160. }
  161.  
  162. //--------------------------------------------------------------
  163. void ofApp::mousePressed(int x, int y, int button)
  164. {
  165.     msg="";
  166.     switch (button)
  167.     {
  168.         case 0: //left button
  169.             for (unsigned int i = 0 ; i < 10 ; i++)
  170.             {
  171.                 bl = genBlob();
  172.                 cout << "bl.id[" << i << "]=" << bl.id << endl;
  173.                 msg = blobMsgAssembly(bl,"blob1");
  174.                 udpConnection.Send(msg.c_str(),msg.length());
  175.             }
  176.         break;
  177.         case 2: //right button
  178.             for (unsigned int i = 0 ; i < 10 ; i++)
  179.             {
  180.                 mk = genMark();
  181.                 cout << "mk.id[" << i << "]=" << mk.id << endl;
  182.                 msg = markMsgAssembly(mk,"mark1");
  183.                 udpConnection.Send(msg.c_str(),msg.length());
  184.             }
  185.         break;
  186.         default: break;
  187.     }
  188.     latch = true;
  189. }
  190.  
  191. //--------------------------------------------------------------
  192. void ofApp::mouseReleased(int x, int y, int button){
  193.  
  194. }
  195.  
  196. //--------------------------------------------------------------
  197. void ofApp::windowResized(int w, int h){
  198.  
  199. }
  200.  
  201. //--------------------------------------------------------------
  202. void ofApp::gotMessage(ofMessage msg){
  203.  
  204. }
  205.  
  206. //--------------------------------------------------------------
  207. void ofApp::dragEvent(ofDragInfo dragInfo){
  208.  
  209. }
  210.  
  211. ofPoint ofApp::randomPoint()
  212. {
  213.     ofPoint aPt;
  214.     aPt.x = ofRandom(0, 1280);
  215.     aPt.y = ofRandom(0, 1024);
  216.     aPt.z = ofRandom(0, 1024);
  217.     return aPt;
  218. }
  219.  
  220. ofApp::blob_t ofApp::genBlob()
  221. {
  222.     struct blob_t aBlob;
  223.     unsigned int nPts = round(ofRandom(100,1500));
  224.  
  225.     aBlob.id = round(ofRandom(0,1000));
  226.     aBlob.centroid = randomPoint();
  227.     aBlob.edges.leftMost = randomPoint();
  228.     aBlob.edges.rightMost = randomPoint();
  229.     aBlob.edges.topMost = randomPoint();
  230.     aBlob.edges.bottomLeftMost = randomPoint();
  231.     aBlob.edges.bottomRightMost = randomPoint();
  232.     aBlob.blobOutline = &aPoly;
  233.     aBlob.blobOutline->clear();
  234.     for (unsigned int i = 0 ; i < nPts ; i++ )
  235.     {
  236.         aBlob.blobOutline->addVertex(randomPoint());
  237.     }
  238.  
  239.     return aBlob;
  240. }
  241.  
  242. ofApp::mark_t ofApp::genMark()
  243. {
  244.     mark_t aMark;
  245.  
  246.     aMark.id = round(ofRandom(0,1000));
  247.     aMark.centroid = randomPoint();
  248.  
  249.     return aMark;
  250. }
  251.  
  252. string ofApp::ptToStr(const ofPoint& aPt)
  253. {
  254.     string str;
  255.     str = ofToString(aPt.x) + "|" \
  256.         + ofToString(aPt.y) + "|" \
  257.         + ofToString(aPt.z) + "[/p]" ;
  258.     return str;
  259. }
  260.  
  261. string ofApp::edgesToStr(const edges_t& edges)
  262. {
  263.     string str;
  264.     str =  \
  265.           ptToStr(edges.leftMost) + \
  266.           ptToStr(edges.rightMost) + \
  267.           ptToStr(edges.topMost) + \
  268.           ptToStr(edges.bottomLeftMost) + \
  269.           ptToStr(edges.bottomRightMost);
  270.     return str;
  271. }
  272.  
  273. string ofApp::blobMsgAssembly(const ofApp::blob_t& blob, const string& startMsg)
  274. {
  275.     string str;
  276.  
  277.     str += startMsg ;
  278.     str += CONTENT_SEPARATOR ;
  279.     str += ofToString(blob.id) ;
  280.     str += CONTENT_SEPARATOR ;
  281.     str += ptToStr(blob.centroid) ;
  282.     str += edgesToStr(blob.edges) ;
  283.     str += CONTENT_SEPARATOR ;
  284.     for (unsigned int i = 0 ; i < blob.blobOutline->size() ; i++)
  285.     {
  286.         str += ptToStr( (*blob.blobOutline)[i] );
  287.     }
  288.     str += "[/EOM]" ;
  289.  
  290.     return str;
  291. }
  292.  
  293. string ofApp::markMsgAssembly(const mark_t& mark, const string& startMsg)
  294. {
  295.     string str;
  296.  
  297.     str += startMsg ;
  298.     str += CONTENT_SEPARATOR ;
  299.     str += ofToString(mark.id) ;
  300.     str += CONTENT_SEPARATOR ;
  301.     str += ptToStr(mark.centroid) ;
  302.     str += "[/EOM]" ;
  303.  
  304.     return str;
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement