Advertisement
Guest User

Untitled

a guest
Jan 8th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.57 KB | None | 0 0
  1. #include "ofMain.h"
  2. #include "ofxBox2d.h"
  3. #include "ofxGui.h"
  4. #include "ofxOscParameterSync.h"
  5. #include "ofxAChaosLib.h"
  6. #include "ofxMSATensorFlow.h"
  7. #include "ofxOpenCv.h"
  8. #include <map>
  9. #include <set>
  10. #include "PdBase.hpp"
  11. #include "AChaosBase.h"
  12.  
  13. #define PLANETS 512
  14.  
  15. class AChaosChannel {
  16. public:
  17.     REAL min, max;
  18.     vector<REAL> mystory;
  19.     AChaosChannel(){
  20.         min = 1e10;
  21.         max = -1e10;
  22.         mystory.clear();
  23.     }
  24.     void update(REAL val){
  25.         if(val < min) min = val;
  26.         if(val > max) max = val;
  27.         mystory.push_back(val);
  28.         if(mystory.size()>PLANETS){
  29.             mystory.erase(mystory.begin());
  30.         }
  31.     }
  32. };
  33.  
  34. //--------------------------------------------------------------
  35. class AChaosViz {
  36. public:
  37.    
  38.     AChaosBase *obj;
  39.     vector<AChaosChannel> channels;
  40.     int n;
  41.    
  42.     void setup(AChaosBase *ptr){
  43.         obj = ptr;
  44.         n=obj->ov.size();
  45.         for(int i=0; i<n;i++){
  46.             AChaosChannel ch;
  47.             channels.push_back(ch);
  48.         }
  49.     }
  50.    
  51.     void update(float * data){
  52.         for(int i=0; i<n;i++){
  53.             channels[i].update(data[i]);
  54.         }
  55.     }
  56.    
  57.     void draw(){
  58.         float w = ofGetWidth();
  59.         float h = ofGetHeight();
  60.         float chh = h / n;
  61.         int s = channels[0].mystory.size();
  62.         if(s<1){
  63.             return;
  64.         }
  65.         float xg = w / s;
  66.        
  67.         for(int i=0; i<n;i++){
  68.            
  69.             float ya = i*chh;
  70.             float yc = (i+1)*chh;
  71.             float yb = yc - chh*0.5f;
  72.             ofLine(0, yb, w, yb);
  73.             ofDrawBitmapString(ofToString(channels[i].max), 5, ya + 0.1*chh);
  74.             ofDrawBitmapString(ofToString(channels[i].min), 5, yc);
  75.             ofDrawBitmapString(ofToString(channels[i].mystory[s-1]), 5, yb);
  76.            
  77.             for(int k=0; k<s-1; k++){
  78.                 float y0 = ofMap(channels[i].mystory[k], channels[i].min, channels[i].max, ya, yc);
  79.                 float y1 = ofMap(channels[i].mystory[k+1], channels[i].min, channels[i].max, ya, yc);
  80.                
  81.                 ofLine(xg*k, y0, (xg+1)*k, y1);
  82.             }
  83.            
  84.         }
  85.     }
  86. };
  87.  
  88. class ofApp : public ofBaseApp{
  89.    
  90. public:
  91.     void setup();
  92.     void update();
  93.     void draw();
  94.    
  95.     void exit();
  96.    
  97.     void keyPressed(int key);
  98.     void keyReleased(int key);
  99.     void mouseMoved(int x, int y );
  100.     void mouseDragged(int x, int y, int button);
  101.     void mousePressed(int x, int y, int button);
  102.     void mouseReleased(int x, int y, int button);
  103.     void mouseEntered(int x, int y);
  104.     void mouseExited(int x, int y);
  105.     void windowResized(int w, int h);
  106.     void dragEvent(ofDragInfo dragInfo);
  107.     void gotMessage(ofMessage msg);
  108.    
  109.     void drawGravity(ofPoint p, ofPoint gravity);
  110.    
  111.    
  112.     void circleResolutionChanged(int & circleResolution);
  113.     void ringButtonPressed();
  114.    
  115.     void vSyncChanged(bool & vSync);
  116.    
  117.     bool bHide;
  118.    
  119.     ofxOscParameterSync sync;
  120.    
  121.     ofParameter<float> size;
  122.     ofParameter<int> number;
  123.     ofParameter<float> radius;
  124.     ofParameter<bool> check;
  125.     ofParameterGroup parameters;
  126.     ofParameter<ofColor> color;
  127.     ofParameter<ofVec2f> center;
  128.     ofParameter<int> circleResolution;
  129.     ofParameter<bool> filled;
  130.     ofxButton twoCircles;
  131.     ofxButton ringButton;
  132.     ofParameter<string> screenSize;
  133.    
  134.     ofxPanel gui;
  135.     ofXml settings;
  136.    
  137.     ofParameter<bool> vSync;
  138.     ofTrueTypeFont font;
  139.     ofSoundPlayer ring;
  140.    
  141.     AChaosNavierStokes  chaos;
  142.     AChaosViz       viz;
  143.    
  144.     ofxBox2d                 box2dA;
  145.     ofxBox2d                 box2dB;
  146.    
  147.     vector <shared_ptr<ofxBox2dCircle> >  circlesA;
  148.     vector <shared_ptr<ofxBox2dCircle> >  circlesB;
  149.     vector <shared_ptr<ofxBox2dRect> >    sharedRects;
  150.    
  151.     ofRectangle boundsA;
  152.     ofRectangle boundsB;
  153.    
  154.     // shared pointer to tensorflow::Session
  155.     msa::tf::Session_ptr session;
  156.    
  157.     // shared pointer to tensorflow::GraphDef
  158.     msa::tf::GraphDef_ptr graph_def;
  159.    
  160.     // input tensors
  161.     tensorflow::Tensor a, b;
  162.    
  163.     // output tensors
  164.     vector<tensorflow::Tensor> outputs;
  165.    
  166. };
  167.  
  168. //--------------------------------------------------------------
  169. void ofApp::setup(){
  170.     parameters.setName("parameters");
  171.     parameters.add(size.set("size",10,0,100));
  172.     parameters.add(number.set("number",10,0,100));
  173.     parameters.add(check.set("check",false));
  174.     parameters.add(color.set("color",ofColor(127),ofColor(0,0),ofColor(255)));
  175.     gui.setup(parameters);
  176.     // by now needs to pass the gui parameter groups since the panel internally creates it's own group
  177.     sync.setup((ofParameterGroup&)gui.getParameter(),6666,"localhost",6667);
  178.     ofSetVerticalSync(true);
  179.     chaos.setup(); viz.setup(&chaos);
  180.    
  181.     ofSetFrameRate(60);
  182.    
  183.     // the world bounds
  184.     boundsA.set(0, 0, 500, 500);
  185.     boundsB.set(500, 0, 500, 500);
  186.    
  187.     // setup world A
  188.     box2dA.init();
  189.     box2dA.setFPS(60);
  190.     box2dA.setGravity(0, -10);
  191.     box2dA.createBounds(boundsA);
  192.     box2dA.registerGrabbing();
  193.    
  194.     // setup world B
  195.     box2dB.init();
  196.     box2dB.setFPS(60);
  197.     box2dB.setGravity(0, 10);
  198.     box2dB.createBounds(boundsB);
  199.     box2dB.registerGrabbing();
  200.    
  201.     // add some cirlces to world A
  202.     for(int i=0; i<10; i++) {
  203.         shared_ptr<ofxBox2dCircle> c = shared_ptr<ofxBox2dCircle>(new ofxBox2dCircle);
  204.         c.get()->setPhysics(1, 0.5, 1);
  205.         c.get()->setup(box2dA.getWorld(), 250+ofRandom(-50, 50), 10, ofRandom(10,30));
  206.         circlesA.push_back(c);
  207.     }
  208.    
  209.     // add some cirlces to world B
  210.     for(int i=0; i<10; i++) {
  211.         shared_ptr<ofxBox2dCircle> c = shared_ptr<ofxBox2dCircle>(new ofxBox2dCircle);
  212.         c.get()->setPhysics(1, 0.5, 1);
  213.         c.get()->setup(box2dB.getWorld(), 750+ofRandom(-50, 50), 10, ofRandom(10,30));
  214.         circlesB.push_back(c);
  215.     }
  216.    
  217.     // we can also have a vector of any shape that is
  218.     // shared between both worlds
  219.     for(int i=0; i<20; i++) {
  220.        
  221.         shared_ptr<ofxBox2dRect> r = shared_ptr<ofxBox2dRect>(new ofxBox2dRect);
  222.         r.get()->setPhysics(1, 0.7, 0.9);
  223.        
  224.         // add to world A
  225.         if(i <= 9) {
  226.             r.get()->setup(box2dA.getWorld(), 250+ofRandom(-50, 50), 10, ofRandom(10,30), ofRandom(10,30));
  227.         }
  228.        
  229.         // add to world B
  230.         else {
  231.             r.get()->setup(box2dB.getWorld(), 750+ofRandom(-50, 50), 10, ofRandom(10,30), ofRandom(10,30));
  232.         }
  233.        
  234.         // add to one vector
  235.         sharedRects.push_back(r);
  236.     }
  237.    
  238.     ofSetColor(255);
  239.     ofBackground(0);
  240.     ofSetVerticalSync(true);
  241.    
  242.     // Load graph (i.e. trained model) we exported from python, add to session, return if error
  243.     graph_def = msa::tf::load_graph_def("models/model.pb");
  244.     if(!graph_def) return;
  245.    
  246.     // initialize session with graph
  247.     session = msa::tf::create_session_with_graph(graph_def);
  248.    
  249.     // initialize input tensor dimensions
  250.     // (not sure what the best way to do this was as there isn't an 'init' method, just a constructor)
  251.     a = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape());
  252.     b = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape());
  253. }
  254.  
  255. //--------------------------------------------------------------
  256. void ofApp::update(){
  257.     sync.update();
  258.     box2dA.update();
  259.     box2dB.update();
  260. }
  261.  
  262. //--------------------------------------------------------------
  263. void ofApp::draw(){
  264.     gui.draw();
  265.     ofSetColor(color);
  266.     for(int i=0;i<number;i++){
  267.         ofDrawCircle(ofGetWidth()*.5-size*((number-1)*0.5-i), ofGetHeight()*.5, size);
  268.     }
  269.     viz. draw();
  270.  
  271.     float angle = (atan2(gravity.y, gravity.x) * 180 / PI) - 90;
  272.     float len   = MIN(200, gravity.length()*10); // scale it up a bit
  273.    
  274.     ofPushMatrix();
  275.     ofTranslate(p.x, p.y);
  276.     ofRotate(angle);
  277.     ofDrawLine(0, 0, 0, len);
  278.     ofDrawTriangle(0, len,
  279.                    -5, len-10,
  280.                    5, len-10);
  281.     ofPopMatrix();
  282.    
  283.     // world A
  284.     boundsA.inside(ofGetMouseX(), ofGetMouseY()) ? ofSetColor(80) : ofSetColor(100);
  285.     ofFill();
  286.     ofDrawRectangle(boundsA);
  287.    
  288.     // world A
  289.     boundsB.inside(ofGetMouseX(), ofGetMouseY()) ? ofSetColor(180) : ofSetColor(200);
  290.     ofFill();
  291.     ofDrawRectangle(boundsB);
  292.    
  293.     ofFill();
  294.    
  295.     // A World Circles
  296.     for (int i=0; i<circlesA.size(); i++) {
  297.         ofSetHexColor(0xBFE364);
  298.         circlesA[i].get()->draw();
  299.     }
  300.    
  301.     // B World Circles
  302.     for (int i=0; i<circlesB.size(); i++) {
  303.         ofSetHexColor(0xE83AAB);
  304.         circlesB[i].get()->draw();
  305.     }
  306.    
  307.     // Shared Rects
  308.     for (int i=0; i<sharedRects.size(); i++) {
  309.         ofSetHexColor(0x2F9BA1);
  310.         ofFill();
  311.         sharedRects[i].get()->draw();
  312.     }
  313.    
  314.     ofSetColor(255); ofDrawBitmapString("World A\nGravity "+ofToString(box2dA.getGravity().x, 1)+","+ofToString(box2dA.getGravity().y, 1), 250, ofGetHeight()/2);
  315.     ofSetColor(90);  ofDrawBitmapString("World B\nGravity "+ofToString(box2dB.getGravity().x, 1)+","+ofToString(box2dB.getGravity().y, 1), 750, ofGetHeight()/2);
  316.    
  317.     ofPoint centerA(250, 250);
  318.     ofPoint centerB(750, 250);
  319.     ofSetColor(255);
  320.     drawGravity(centerA, box2dA.getGravity());
  321.     drawGravity(centerB, box2dB.getGravity());
  322.    
  323.     stringstream message;
  324.    
  325.     if(session) {
  326.         // inputs are linked to mouse position, normalized to 0..10
  327.         a.scalar<float>()() = round(ofMap(ofGetMouseX(), 0, ofGetWidth(), 0, 10));
  328.         b.scalar<float>()() = round(ofMap(ofGetMouseY(), 0, ofGetHeight(), 0, 10));
  329.        
  330.         // Collect inputs into a vector
  331.         // IMPORTANT: the string must match the name of the variable/node in the graph
  332.         vector<pair<string, tensorflow::Tensor>> inputs = {
  333.             { "a", a },
  334.             { "b", b },
  335.         };
  336.        
  337.         // desired outputs which we want processed and returned from the graph
  338.         // IMPORTANT: the string must match the name of the variable/node in the graph
  339.         vector<string> output_names = { "c" };
  340.        
  341.         // Run the graph, pass in our inputs and desired outputs, evaluate operation and return
  342.         session->Run(inputs, output_names, {}, &outputs);
  343.        
  344.         // outputs is a vector of tensors, we're interested in only the first tensor
  345.         auto &c = outputs[0];
  346.        
  347.         // get scalar values of each tensor (since they're 1D and single element it's easy)
  348.         float val_a = a.scalar<float>()();
  349.         float val_b = b.scalar<float>()();
  350.         float val_c = c.scalar<float>()();
  351.        
  352.         // Print the results
  353.         message << "MOVE MOUSE!" << endl << endl;
  354.         message << val_a << " (" << a.DebugString() << ")" << endl;
  355.         message << " * " << endl;
  356.         message << val_b << " (" << b.DebugString() << ")" << endl;
  357.         message << " = " << endl;
  358.         message << val_c << " (" << c.DebugString() << ")" << endl;
  359.         message << endl;
  360.         message << "all this madness, just to calculate a * b" << endl;
  361.        
  362.     } else {
  363.         message << "Error during initialization, check console for details.";
  364.        
  365.     }
  366.    
  367.     ofDrawBitmapString(message.str(), 30, 30);
  368. }
  369.  
  370. }
  371.  
  372. //--------------------------------------------------------------
  373. void ofApp::drawGravity(ofPoint p, ofPoint gravity) {
  374.    
  375.     float angle = (atan2(gravity.y, gravity.x) * 180 / PI) - 90;
  376.     float len   = MIN(200, gravity.length()*10); // scale it up a bit
  377.    
  378.     ofPushMatrix();
  379.     ofTranslate(p.x, p.y);
  380.     ofRotate(angle);
  381.     ofDrawLine(0, 0, 0, len);
  382.     ofDrawTriangle(0, len,
  383.                    -5, len-10,
  384.                    5, len-10);
  385.     ofPopMatrix();
  386. }
  387.  
  388.  
  389. //--------------------------------------------------------------
  390. void ofApp::mouseDragged(int x, int y, int button){
  391.     float maxGravity = 100;
  392.     if(boundsA.inside(x, y)) {
  393.         float gx = ofMap(x, 0, 500, -maxGravity, maxGravity);
  394.         float gy = ofMap(y, 0, ofGetHeight(), -maxGravity, maxGravity);
  395.         box2dA.setGravity(gx, gy);
  396.     }
  397.     else if(boundsB.inside(x, y)) {
  398.         float gx = ofMap(x, 500, ofGetWidth(), -maxGravity, maxGravity);
  399.         float gy = ofMap(y, 0, ofGetHeight(), -maxGravity, maxGravity);
  400.         box2dB.setGravity(gx, gy);
  401.     }
  402. }
  403.  
  404. int main( ){
  405.     ofSetupOpenGL(600,600,OF_WINDOW);           // <-------- setup the GL context
  406.    
  407.     // this kicks off the running of my app
  408.     // can be OF_WINDOW or OF_FULLSCREEN
  409.     // pass in width and height too:
  410.     ofRunApp(new ofApp());
  411.    
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement