Skip to content

ofRandom tweaks #8182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 27, 2025
122 changes: 62 additions & 60 deletions apps/devApps/RandomExplorer/src/Dist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct Dist {
virtual auto gen() -> void = 0;
virtual auto clear() -> void = 0;
virtual auto compile() -> void = 0;
virtual auto draw(float x, float y, float w, float h) -> void = 0;
virtual auto draw(float x, float y, float w, float h, bool graph = true) -> void = 0;
virtual ~Dist() = default;

Dist() {};
Expand Down Expand Up @@ -94,7 +94,7 @@ struct ConcreteDist : public Dist {
}
}

auto draw(float x, float y, float w, float h) -> void override {
auto draw(float x, float y, float w, float h, bool graph = true) -> void override {
ofPushStyle();
ofPushMatrix();
{
Expand All @@ -108,66 +108,68 @@ struct ConcreteDist : public Dist {
ofSetColor(255, 255, 255, 255);
ofDrawBitmapStringHighlight(parameters_.getName() + " " + ofToString(cost_ * 1000, 2, 5) + "ms", w + 5, 12);

if constexpr (std::is_arithmetic_v<T>) {

auto p = 0.0f;
double incr = w / bins_.size();
auto fact = h / max_;
if (discrete_) {

// line bars for discrete
ofTranslate(incr / 2, 0);
for (auto y : bins_) {
ofDrawLine(0, h, 0, h - float(y) * fact);
if (y == 0) {
ofNoFill();
ofDrawCircle(0, h - float(y) * fact, 2.5);
ofFill();
} else {
ofDrawCircle(0, h - float(y) * fact, 3);
if (graph) {
if constexpr (std::is_arithmetic_v<T>) {

auto p = 0.0f;
double incr = w / bins_.size();
auto fact = h / max_;
if (discrete_) {

// line bars for discrete
ofTranslate(incr / 2, 0);
for (auto ypos : bins_) {
ofDrawLine(0, h, 0, h - float(ypos) * fact);
if (y == 0) {
ofNoFill();
ofDrawCircle(0, h - float(ypos) * fact, 2.5);
ofFill();
} else {
ofDrawCircle(0, h - float(ypos) * fact, 3);
}
ofTranslate(int(incr), 0);
}
ofTranslate(int(incr), 0);
} else {

// integral for reals
ofPolyline line;
line.addVertex(0, h - bins_[0] * fact);
for (auto ypos : bins_)
line.lineTo(p += incr, h - float(ypos) * fact);
line.draw();
}

} else if constexpr (std::is_same_v<T, glm::vec2>) {

ofSetColor(255, 255, 255, 96);
for (const auto & d : data_) {
if (d.x > w || d.y > h) underflow_++;
if (d.x < 0 || d.y < 0) overflow_++;
ofDrawCircle(d, 0.5);
}

} else if constexpr (std::is_same_v<T, glm::vec3>) {

ofSetColor(255, 255, 255, 32);
of3dPrimitive prim;
for (const auto & d : data_) {
if (d.x > w || d.y > h) underflow_++;
if (d.x < 0 || d.y < 0) overflow_++;
}
prim.getMesh().getVertices() = data_;
prim.getMesh().setMode(OF_PRIMITIVE_POINTS);
prim.rotateDeg(70, { 0.2, 0.3, 0.5 }); // just some perspective

ofPushMatrix();
{
ofTranslate(w * 0.2, h * 0.2);
prim.drawWireframe();
prim.drawAxes(w * 0.5);
}
ofPopMatrix();
} else {

// integral for reals
ofPolyline line;
line.addVertex(0, h - bins_[0] * fact);
for (auto y : bins_)
line.lineTo(p += incr, h - float(y) * fact);
line.draw();
}

} else if constexpr (std::is_same_v<T, glm::vec2>) {

ofSetColor(255, 255, 255, 96);
for (const auto & d : data_) {
if (d.x > w || d.y > h) underflow_++;
if (d.x < 0 || d.y < 0) overflow_++;
ofDrawCircle(d, 0.5);
}

} else if constexpr (std::is_same_v<T, glm::vec3>) {

ofSetColor(255, 255, 255, 32);
of3dPrimitive prim;
for (const auto & d : data_) {
if (d.x > w || d.y > h) underflow_++;
if (d.x < 0 || d.y < 0) overflow_++;
}
prim.getMesh().getVertices() = data_;
prim.getMesh().setMode(OF_PRIMITIVE_POINTS);
prim.rotateDeg(70, { 0.2, 0.3, 0.5 }); // just some perspective

ofPushMatrix();
{
ofTranslate(w * 0.2, h * 0.2);
prim.drawWireframe();
prim.drawAxes(w * 0.5);
ofDrawBitmapString("unsupported visualisation", 10, 10);
}
ofPopMatrix();
} else {
ofDrawBitmapString("unsupported visualisation", 10, 10);
}
ofSetColor(ofColor::deepPink);
if (underflow_) ofDrawBitmapString("undershoot: " + ofToString(underflow_), w + 5, 70);
Expand All @@ -185,15 +187,15 @@ struct DistGroup {
DistGroup(std::vector<std::shared_ptr<Dist>> dists)
: dists_(dists) { }

auto draw(std::string label, int square, int gap) {
auto draw(std::string label, int square, int gap, bool graph = true) {
panel_.draw();
ofPushMatrix();
{
ofTranslate(panel_.getPosition());
ofDrawBitmapString(label, 0, -10);
ofTranslate(panel_.getWidth() + 20, 0);
for (const auto & dist : dists_) {
dist->draw(0, 0, square, square);
dist->draw(0, 0, square, square, graph);
ofTranslate(0, square + gap);
}
}
Expand Down
12 changes: 7 additions & 5 deletions apps/devApps/RandomExplorer/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void ofApp::setup() {
panel_.add(size_);
panel_.add(seed_);
panel_.add(reinit_);
panel_.add(draw_graphs_);

// panel_.add(ok_color_);
// panel_.add(saturation_);
Expand Down Expand Up @@ -79,12 +80,13 @@ void ofApp::draw() {
} else {
ofDrawBitmapStringHighlight("engine is non-deterministic", x, 20, ofColor::black, ofColor::white);
}

panel_.draw();
dists_["core"]->draw("C++ fundamental distributions", square_, gap_);
dists_["special"]->draw("more specialized distributions", square_, gap_);
dists_["of"]->draw("OF/art-centric wrappers/utils", square_, gap_);
dists_["old"]->draw("Previous implementation (reference)", square_, gap_);
dists_["core"]->draw("C++ fundamental distributions", square_, gap_, draw_graphs_);
dists_["special"]->draw("more specialized distributions", square_, gap_, draw_graphs_);
dists_["of"]->draw("OF/art-centric wrappers/utils", square_, gap_, draw_graphs_);
dists_["old"]->draw("Previous implementation (reference)", square_, gap_, draw_graphs_);


ofDrawBitmapStringHighlight("Performance: on M1/M2 processors, the old srand is faster\nthan uniform<float> in Debug, but slower in Release...\nPlease make sure to evaluate performance in Release!",
dists_["old"]->panel_.getPosition() + glm::vec2(0, square_ + gap_), ofColor(50, 0, 0), ofColor(ofColor::white));
Expand Down
7 changes: 4 additions & 3 deletions apps/devApps/RandomExplorer/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ class ofApp : public ofBaseApp {
ofParameter<size_t> size_ { "size (cube root)", 25, 1, 50 };
ofParameter<unsigned long> seed_ { "seed", 0, 0, 1000 };
ofParameter<void> reinit_ { "re-init engine" };
ofParameter<bool> draw_graphs_ { "draw graphs", true };
ofParameter<bool> ok_color_ { "ok_color", true };
ofParameter<float> saturation_ { "saturation", 0.95 };
ofParameter<float> value_ { "value", .45 };
ofParameter<float> offset_ { "offset", 0 };
ofParameter<float> saturation_ { "saturation", 0.95f };
ofParameter<float> value_ { "value", .45f };
ofParameter<float> offset_ { "offset", 0.0f };

size_t col_w_ = 640;
size_t square_ = 110;
Expand Down
3 changes: 3 additions & 0 deletions examples/math/randomExample/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ void ofApp::perform() {
ofLogNotice("ofRandomBoundNormal<glm::vec4>(10, 20)") << ofRandomBoundNormal<glm::vec4>(10, 20);
ofLogNotice("ofRandomBoundNormal<glm::vec4>({100 ,200, 300, 400},{110, 210, 310, 410})") << ofRandomBoundNormal<glm::vec4>({100 ,200, 300, 400},{110, 210, 310, 410});

ofLogNotice("ofRandomBoundNormal<glm::vec2>(10, 20, 1)") << ofRandomBoundNormal<glm::vec2>(10, 20, 1);
ofLogNotice("ofRandomBoundNormal<glm::vec4>(10, 20, 1)") << ofRandomBoundNormal<glm::vec2>({10, 10}, {20,20}, {1,1});

ofLogNotice("======= alternate engines test =====");
std::random_device rd {};
std::seed_seq seq { rd(), rd(), rd(), rd() };
Expand Down
Loading