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