|
| 1 | +#ifndef DIST_HPP |
| 2 | +#define DIST_HPP |
| 3 | + |
| 4 | +#include "ofMain.h" |
| 5 | + |
| 6 | + |
| 7 | +struct Dist { |
| 8 | + |
| 9 | + inline static string base_url_{ "https://en.cppreference.com/w/cpp/numeric/random/" }; |
| 10 | + string url_; |
| 11 | + ofParameterGroup parameters_; |
| 12 | + ofColor color_ {128,128,128}; |
| 13 | + std::string info_; |
| 14 | + glm::vec2 range_ { }; |
| 15 | + bool discrete_ { false }; |
| 16 | + size_t underflow_; |
| 17 | + size_t overflow_; |
| 18 | + std::size_t max_ { 0 }; |
| 19 | + float cost_ { 0.0f }; |
| 20 | + |
| 21 | + virtual auto gen() -> void = 0; |
| 22 | + virtual auto clear() -> void = 0; |
| 23 | + virtual auto compile() -> void = 0; |
| 24 | + virtual auto draw(float x, float y, float w, float h) -> void = 0; |
| 25 | + virtual ~Dist() = default; |
| 26 | + |
| 27 | + Dist() {}; |
| 28 | +}; |
| 29 | + |
| 30 | +template <typename T> |
| 31 | +struct ConcreteDist: public Dist { |
| 32 | + |
| 33 | + std::vector<T> data_; |
| 34 | + std::function<T()> gen_; |
| 35 | + std::vector<T> bins_; |
| 36 | + int autorot_ { 0 }; |
| 37 | + |
| 38 | + ofParameter<void> url_button_; |
| 39 | + |
| 40 | + ConcreteDist(std::string label, |
| 41 | + std::string info, |
| 42 | + std::string url, |
| 43 | + std::vector<ofAbstractParameter *> params, |
| 44 | + std::function<T()> gen, |
| 45 | + std::size_t num_bins = 101, |
| 46 | + glm::vec2 range = {0, 100}, |
| 47 | + bool discrete = false ) |
| 48 | + : gen_(gen) |
| 49 | + { |
| 50 | + url_ = base_url_+url; |
| 51 | + info_ = info; |
| 52 | + range_ = range; |
| 53 | + discrete_ = discrete; |
| 54 | + bins_.resize(num_bins); |
| 55 | + parameters_.setName(label); |
| 56 | + for (auto & p: params) parameters_.add(*p); |
| 57 | + url_button_.set("click for reference"); |
| 58 | + url_button_.addListener(this, &ConcreteDist::open_url); |
| 59 | + parameters_.add(url_button_); |
| 60 | + |
| 61 | + //panel_.getControl("seed")->setTextColor(ofColor::green); |
| 62 | + |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + auto open_url() { |
| 67 | + ofLaunchBrowser(url_); |
| 68 | + } |
| 69 | + |
| 70 | + auto gen() -> void override { |
| 71 | + data_.push_back(gen_()); |
| 72 | + } |
| 73 | + |
| 74 | + auto clear() -> void override { |
| 75 | + overflow_ = 0; |
| 76 | + underflow_ = 0; |
| 77 | + data_.clear(); |
| 78 | + std::fill(bins_.begin(), bins_.end(), T{0}); |
| 79 | + } |
| 80 | + |
| 81 | + auto compile() -> void override { |
| 82 | + // histograms non-vecs only |
| 83 | + if constexpr (std::is_arithmetic_v<T>) { |
| 84 | + float divisor = (range_.y-range_.x)/float(bins_.size()-1); |
| 85 | + for (auto & v: data_) { |
| 86 | + v /= divisor; |
| 87 | + if (v < range_.x) { |
| 88 | + underflow_++; |
| 89 | + } else if (v >= bins_.size()) { |
| 90 | + overflow_++; |
| 91 | + } else { |
| 92 | + bins_.at(v) = bins_.at(v)+1; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + max_ = 0.0; |
| 97 | + for (size_t i=0;i<bins_.size(); i++) { |
| 98 | + if (bins_.at(i) > max_) max_ = bins_.at(i); |
| 99 | + } |
| 100 | + } else { |
| 101 | + // no histograms for vecN |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + auto draw(float x, float y, float w, float h) -> void override { |
| 106 | + ofPushStyle(); |
| 107 | + ofPushMatrix(); |
| 108 | + { |
| 109 | + ofTranslate(x,y); |
| 110 | + ofSetColor(color_); |
| 111 | + ofDrawRectangle(0,0,w,h); |
| 112 | + |
| 113 | + ofSetColor(ofColor::darkRed); |
| 114 | + if (underflow_) ofDrawBitmapString("undershoot: " + ofToString(underflow_), w+5, 58); |
| 115 | + if (overflow_) ofDrawBitmapString("overshoot: " + ofToString(overflow_), w+5, 74); |
| 116 | + |
| 117 | + ofSetColor(192,192,192,255); |
| 118 | + ofDrawBitmapString(info_, w+5, 35); |
| 119 | + ofSetColor(255,255,255,255); |
| 120 | + ofDrawBitmapStringHighlight(parameters_.getName() + " " + ofToString(cost_*1000, 2, 5)+"ms", w+5, 12); |
| 121 | + |
| 122 | + if constexpr (std::is_arithmetic_v<T>) { |
| 123 | + |
| 124 | + auto p = 0.0f; |
| 125 | + double incr = w/bins_.size(); |
| 126 | + auto fact = h/max_; |
| 127 | + if (discrete_) { |
| 128 | + |
| 129 | + // line bars for discrete |
| 130 | + ofTranslate(incr/2, 0); |
| 131 | + for (auto y: bins_) { |
| 132 | + ofDrawLine(0, h, 0, h-float(y)*fact); |
| 133 | + if (y==0) { |
| 134 | + ofNoFill(); |
| 135 | + ofDrawCircle(0, h-float(y)*fact, 2.5); |
| 136 | + ofFill(); |
| 137 | + } else { |
| 138 | + ofDrawCircle(0, h-float(y)*fact, 3); |
| 139 | + } |
| 140 | + ofTranslate(int(incr), 0); |
| 141 | + } |
| 142 | + } else { |
| 143 | + |
| 144 | + // integral for reals |
| 145 | + ofPolyline line; |
| 146 | + line.addVertex(0,h-bins_[0]*fact); |
| 147 | + for (auto y: bins_) line.lineTo(p+=incr, h-float(y)*fact); |
| 148 | + line.draw(); |
| 149 | + } |
| 150 | + |
| 151 | + } else if constexpr (std::is_same_v<T, glm::vec2>) { |
| 152 | + |
| 153 | + ofSetColor(255,255,255,96); |
| 154 | + for (const auto & d: data_) ofDrawCircle(d, .5); |
| 155 | + |
| 156 | + } else if constexpr (std::is_same_v<T, glm::vec3>) { |
| 157 | + |
| 158 | + ofSetColor(255,255,255,32); |
| 159 | + of3dPrimitive prim; |
| 160 | + prim.getMesh().getVertices() = data_; |
| 161 | + prim.getMesh().setMode(OF_PRIMITIVE_POINTS); |
| 162 | + prim.rotateDeg(70,{ 0.2, 0.3, 0.5 }); // just some perspective |
| 163 | + |
| 164 | + ofPushMatrix(); |
| 165 | + { |
| 166 | + ofTranslate(w * 0.2, h * 0.2); |
| 167 | + prim.drawWireframe(); |
| 168 | + prim.drawAxes(w * 0.5); |
| 169 | + } |
| 170 | + ofPopMatrix(); |
| 171 | + } else { |
| 172 | + ofDrawBitmapString("unsupported visualisation", 10,10); |
| 173 | + } |
| 174 | + } |
| 175 | + ofPopMatrix(); |
| 176 | + ofPopStyle(); |
| 177 | + } |
| 178 | +}; |
| 179 | + |
| 180 | +struct DistGroup { |
| 181 | + std::vector<std::shared_ptr<Dist>> dists_; |
| 182 | + ofxPanel panel_; |
| 183 | + DistGroup(std::vector<std::shared_ptr<Dist>> dists): dists_(dists) { } |
| 184 | + |
| 185 | + auto draw(std::string label, int square, int gap) { |
| 186 | + panel_.draw(); |
| 187 | + ofPushMatrix(); |
| 188 | + { |
| 189 | + ofTranslate(panel_.getPosition()); |
| 190 | + ofDrawBitmapString(label,0,-10); |
| 191 | + ofTranslate(panel_.getWidth()+20,0); |
| 192 | + for (const auto & dist: dists_) { |
| 193 | + dist->draw(0,0,square,square); |
| 194 | + ofTranslate(0,square + gap); |
| 195 | + } |
| 196 | + } |
| 197 | + ofPopMatrix(); |
| 198 | + } |
| 199 | +}; |
| 200 | + |
| 201 | + |
| 202 | +#endif /* DIST_HPP */ |
0 commit comments