Ver código fonte

Initial commit

yeswecan 9 anos atrás
commit
17ee072d84
3 arquivos alterados com 173 adições e 0 exclusões
  1. 18 0
      readme.md
  2. 10 0
      src/ofxPlotter.cpp
  3. 145 0
      src/ofxPlotter.h

+ 18 - 0
readme.md

@@ -0,0 +1,18 @@
+ofxPlotter
+==========
+
+Introduction
+------------
+A simple function plotter class for openFrameworks with built-in variable filtering and a ascetic syntax.
+
+Usage
+-----
+ofxPlotter is designed to be as simple and as minimal as possible for plotting functions in realtime.
+
+setWindow(int windowSize); // set window (a.k.a. plot length)
+
+plotter["variable name"] << variable; // add a new data to the plot "variable name"
+
+plotter.draw(100, 100, 300, 300); // draw the plot
+
+Every _variable name_'s plot will be drawn separately in a Rectangle that's position on screen is x = 100, y = 100 and size is width = 300, height = 300.

+ 10 - 0
src/ofxPlotter.cpp

@@ -0,0 +1,10 @@
+//
+//  ofxPlotter.cpp
+//
+//  Created by zebra on 06/05/16.
+//
+//
+
+#include "ofxPlotter.h"
+
+int ofxPlotter::windowSize;

+ 145 - 0
src/ofxPlotter.h

@@ -0,0 +1,145 @@
+//
+//  ofxPlotter.h
+//  Plotter example
+//
+//  Created by zebra on 06/05/16.
+//
+//
+
+#ifndef ofxPlotter_h
+#define ofxPlotter_h
+
+#include <map>
+#include <string>
+#include <vector>
+
+#include "ofMain.h"
+
+class ofxPlotter {
+    
+    class Value {
+    public:
+        float inner_value;
+        float inner_value_filtered;
+        
+        Value() { }
+        
+        // Float righthand
+        void operator<<(float righthand) {
+            inner_value = righthand;
+        }
+
+        // Int righthand
+        void operator<<(int righthand) {
+            inner_value = righthand;
+        }
+        
+        
+        int getI() {
+            return inner_value;
+        }
+
+        float getF() {
+            return (float)inner_value;
+        }
+        
+        float getFiltered(float filter_amount = 0.2) {
+            inner_value_filtered = inner_value_filtered * filter_amount + inner_value * filter_amount;
+            return inner_value_filtered;
+        }
+        
+    };
+
+    
+    static int windowSize;
+    static float windowLength;
+    
+public:
+    ofxPlotter() {
+        windowSize = 300;
+    }
+    
+    void updateHistory() {
+        for (std::map<std::string, ofxPlotter::Value>::iterator i = values.begin(); i != values.end(); i++) {
+            history[i->first].push_back(i->second);
+            if (history[i->first].size() > windowSize) {
+                history[i->first].erase(history[i->first].begin());
+            }
+//            ofLog() << i->first << " : " << i->second.getF();
+        }
+    }
+    
+    Value& operator[](std::string righthand) {
+        updateHistory();
+        return (Value&)values[righthand];
+        
+    }
+    
+    void draw(float x, float y, float width, float height, int verticalLines = 16) {
+        int graphCounts = values.size();
+        int yspace = (float)height / graphCounts;
+        int index = 0;
+        for (std::map<std::string, ofxPlotter::Value>::iterator i = values.begin(); i != values.end(); i++) {
+            std::vector<ofxPlotter::Value>* historyValues = &history[i->first];
+            
+            // Measuring the function scale
+            float sum;
+            float max = -999999999;
+            float min = 999999999;
+            for (int i = 0; i < historyValues->size(); i++) {
+                float value = (*historyValues).operator[](i).getF();
+                sum += value;
+                if (value > max) max = value;
+                if (value < min) min = value;
+            }
+            float median = sum / historyValues->size();
+            
+            float multiplier = 1 / (max - min);
+            
+            float stepWidth = width / historyValues->size();
+            ofPushMatrix();
+            ofTranslate(0, yspace * index);
+            ofSetColor(ofColor::fromHsb((index * 75), 255, 50, 20));
+            ofDrawRectangle(0, 0, width, yspace);
+            
+            ofSetColor(ofColor::fromHsb((index * 75), 255, 50, 25));
+            ofSetLineWidth(2);
+            if (verticalLines > 0 ) {
+                float verticalLineW = width / verticalLines;
+                for (int j = 0; j < verticalLines; j++) {
+                    ofLine(j * verticalLineW, 0, j * verticalLineW, height);
+                }
+            }
+            
+            
+            ofSetColor(ofColor::fromHsb((index * 75), 255, 200, 255));
+            ofPoint p, p2;
+            for (int i = 0; i < (*historyValues).size(); i++) {
+                p = ofPoint(i * stepWidth, yspace / 2 + (*historyValues)[i].getF() * yspace * multiplier);
+                ofSetLineWidth(3);
+                ofLine(p2, p);
+                p2 = p;
+            }
+            ofDrawBitmapStringHighlight(i->first + " ; current: " + ofToString(values[i->first].getFiltered(0.5)) + " ; min: " + ofToString(min) + " ; max: " + ofToString(max), 25, 25);
+            ofPopMatrix();
+            
+            index++;
+        }
+    }
+    
+    std::map<std::string, ofxPlotter::Value> values;
+    std::map<std::string, std::vector<ofxPlotter::Value>> history;
+    
+    
+    // Parameters
+    
+    void setWindowSize(int size) {
+        windowSize = size;
+    }
+    
+    int getWindowSize() {
+        return windowSize;
+    }
+};
+
+#endif /* ofxPlotter_h */