Skip to content

Commit 9ebfedf

Browse files
ofRandom tweaks (#8182)
* Random Distributions: typos, docs and tweaks * RandomExplorer: option to disable graphs * ofSIngleton: tweaks/updates * ofRandomExample: more cases * thread-safety for random urn * Urn: explicit edge rules * Urn: better iterator init * Urn: correct end matching * Urn: non-leaky move constructor * feat: removal of Urn --------- Co-authored-by: alexandre burton <[email protected]>
1 parent a15228f commit 9ebfedf

File tree

7 files changed

+123
-324
lines changed

7 files changed

+123
-324
lines changed

apps/devApps/RandomExplorer/src/Dist.hpp

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct Dist {
2121
virtual auto gen() -> void = 0;
2222
virtual auto clear() -> void = 0;
2323
virtual auto compile() -> void = 0;
24-
virtual auto draw(float x, float y, float w, float h) -> void = 0;
24+
virtual auto draw(float x, float y, float w, float h, bool graph = true) -> void = 0;
2525
virtual ~Dist() = default;
2626

2727
Dist() {};
@@ -94,7 +94,7 @@ struct ConcreteDist : public Dist {
9494
}
9595
}
9696

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

111-
if constexpr (std::is_arithmetic_v<T>) {
112-
113-
auto p = 0.0f;
114-
double incr = w / bins_.size();
115-
auto fact = h / max_;
116-
if (discrete_) {
117-
118-
// line bars for discrete
119-
ofTranslate(incr / 2, 0);
120-
for (auto y : bins_) {
121-
ofDrawLine(0, h, 0, h - float(y) * fact);
122-
if (y == 0) {
123-
ofNoFill();
124-
ofDrawCircle(0, h - float(y) * fact, 2.5);
125-
ofFill();
126-
} else {
127-
ofDrawCircle(0, h - float(y) * fact, 3);
111+
if (graph) {
112+
if constexpr (std::is_arithmetic_v<T>) {
113+
114+
auto p = 0.0f;
115+
double incr = w / bins_.size();
116+
auto fact = h / max_;
117+
if (discrete_) {
118+
119+
// line bars for discrete
120+
ofTranslate(incr / 2, 0);
121+
for (auto ypos : bins_) {
122+
ofDrawLine(0, h, 0, h - float(ypos) * fact);
123+
if (y == 0) {
124+
ofNoFill();
125+
ofDrawCircle(0, h - float(ypos) * fact, 2.5);
126+
ofFill();
127+
} else {
128+
ofDrawCircle(0, h - float(ypos) * fact, 3);
129+
}
130+
ofTranslate(int(incr), 0);
128131
}
129-
ofTranslate(int(incr), 0);
132+
} else {
133+
134+
// integral for reals
135+
ofPolyline line;
136+
line.addVertex(0, h - bins_[0] * fact);
137+
for (auto ypos : bins_)
138+
line.lineTo(p += incr, h - float(ypos) * fact);
139+
line.draw();
130140
}
141+
142+
} else if constexpr (std::is_same_v<T, glm::vec2>) {
143+
144+
ofSetColor(255, 255, 255, 96);
145+
for (const auto & d : data_) {
146+
if (d.x > w || d.y > h) underflow_++;
147+
if (d.x < 0 || d.y < 0) overflow_++;
148+
ofDrawCircle(d, 0.5);
149+
}
150+
151+
} else if constexpr (std::is_same_v<T, glm::vec3>) {
152+
153+
ofSetColor(255, 255, 255, 32);
154+
of3dPrimitive prim;
155+
for (const auto & d : data_) {
156+
if (d.x > w || d.y > h) underflow_++;
157+
if (d.x < 0 || d.y < 0) overflow_++;
158+
}
159+
prim.getMesh().getVertices() = data_;
160+
prim.getMesh().setMode(OF_PRIMITIVE_POINTS);
161+
prim.rotateDeg(70, { 0.2, 0.3, 0.5 }); // just some perspective
162+
163+
ofPushMatrix();
164+
{
165+
ofTranslate(w * 0.2, h * 0.2);
166+
prim.drawWireframe();
167+
prim.drawAxes(w * 0.5);
168+
}
169+
ofPopMatrix();
131170
} else {
132-
133-
// integral for reals
134-
ofPolyline line;
135-
line.addVertex(0, h - bins_[0] * fact);
136-
for (auto y : bins_)
137-
line.lineTo(p += incr, h - float(y) * fact);
138-
line.draw();
139-
}
140-
141-
} else if constexpr (std::is_same_v<T, glm::vec2>) {
142-
143-
ofSetColor(255, 255, 255, 96);
144-
for (const auto & d : data_) {
145-
if (d.x > w || d.y > h) underflow_++;
146-
if (d.x < 0 || d.y < 0) overflow_++;
147-
ofDrawCircle(d, 0.5);
148-
}
149-
150-
} else if constexpr (std::is_same_v<T, glm::vec3>) {
151-
152-
ofSetColor(255, 255, 255, 32);
153-
of3dPrimitive prim;
154-
for (const auto & d : data_) {
155-
if (d.x > w || d.y > h) underflow_++;
156-
if (d.x < 0 || d.y < 0) overflow_++;
157-
}
158-
prim.getMesh().getVertices() = data_;
159-
prim.getMesh().setMode(OF_PRIMITIVE_POINTS);
160-
prim.rotateDeg(70, { 0.2, 0.3, 0.5 }); // just some perspective
161-
162-
ofPushMatrix();
163-
{
164-
ofTranslate(w * 0.2, h * 0.2);
165-
prim.drawWireframe();
166-
prim.drawAxes(w * 0.5);
171+
ofDrawBitmapString("unsupported visualisation", 10, 10);
167172
}
168-
ofPopMatrix();
169-
} else {
170-
ofDrawBitmapString("unsupported visualisation", 10, 10);
171173
}
172174
ofSetColor(ofColor::deepPink);
173175
if (underflow_) ofDrawBitmapString("undershoot: " + ofToString(underflow_), w + 5, 70);
@@ -185,15 +187,15 @@ struct DistGroup {
185187
DistGroup(std::vector<std::shared_ptr<Dist>> dists)
186188
: dists_(dists) { }
187189

188-
auto draw(std::string label, int square, int gap) {
190+
auto draw(std::string label, int square, int gap, bool graph = true) {
189191
panel_.draw();
190192
ofPushMatrix();
191193
{
192194
ofTranslate(panel_.getPosition());
193195
ofDrawBitmapString(label, 0, -10);
194196
ofTranslate(panel_.getWidth() + 20, 0);
195197
for (const auto & dist : dists_) {
196-
dist->draw(0, 0, square, square);
198+
dist->draw(0, 0, square, square, graph);
197199
ofTranslate(0, square + gap);
198200
}
199201
}

apps/devApps/RandomExplorer/src/ofApp.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ void ofApp::setup() {
1212
panel_.add(size_);
1313
panel_.add(seed_);
1414
panel_.add(reinit_);
15+
panel_.add(draw_graphs_);
1516

1617
// panel_.add(ok_color_);
1718
// panel_.add(saturation_);
@@ -79,12 +80,13 @@ void ofApp::draw() {
7980
} else {
8081
ofDrawBitmapStringHighlight("engine is non-deterministic", x, 20, ofColor::black, ofColor::white);
8182
}
82-
83+
8384
panel_.draw();
84-
dists_["core"]->draw("C++ fundamental distributions", square_, gap_);
85-
dists_["special"]->draw("more specialized distributions", square_, gap_);
86-
dists_["of"]->draw("OF/art-centric wrappers/utils", square_, gap_);
87-
dists_["old"]->draw("Previous implementation (reference)", square_, gap_);
85+
dists_["core"]->draw("C++ fundamental distributions", square_, gap_, draw_graphs_);
86+
dists_["special"]->draw("more specialized distributions", square_, gap_, draw_graphs_);
87+
dists_["of"]->draw("OF/art-centric wrappers/utils", square_, gap_, draw_graphs_);
88+
dists_["old"]->draw("Previous implementation (reference)", square_, gap_, draw_graphs_);
89+
8890

8991
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!",
9092
dists_["old"]->panel_.getPosition() + glm::vec2(0, square_ + gap_), ofColor(50, 0, 0), ofColor(ofColor::white));

apps/devApps/RandomExplorer/src/ofApp.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ class ofApp : public ofBaseApp {
2020
ofParameter<size_t> size_ { "size (cube root)", 25, 1, 50 };
2121
ofParameter<unsigned long> seed_ { "seed", 0, 0, 1000 };
2222
ofParameter<void> reinit_ { "re-init engine" };
23+
ofParameter<bool> draw_graphs_ { "draw graphs", true };
2324
ofParameter<bool> ok_color_ { "ok_color", true };
24-
ofParameter<float> saturation_ { "saturation", 0.95 };
25-
ofParameter<float> value_ { "value", .45 };
26-
ofParameter<float> offset_ { "offset", 0 };
25+
ofParameter<float> saturation_ { "saturation", 0.95f };
26+
ofParameter<float> value_ { "value", .45f };
27+
ofParameter<float> offset_ { "offset", 0.0f };
2728

2829
size_t col_w_ = 640;
2930
size_t square_ = 110;

examples/math/randomExample/src/ofApp.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ void ofApp::perform() {
302302
ofLogNotice("ofRandomBoundNormal<glm::vec4>(10, 20)") << ofRandomBoundNormal<glm::vec4>(10, 20);
303303
ofLogNotice("ofRandomBoundNormal<glm::vec4>({100 ,200, 300, 400},{110, 210, 310, 410})") << ofRandomBoundNormal<glm::vec4>({100 ,200, 300, 400},{110, 210, 310, 410});
304304

305+
ofLogNotice("ofRandomBoundNormal<glm::vec2>(10, 20, 1)") << ofRandomBoundNormal<glm::vec2>(10, 20, 1);
306+
ofLogNotice("ofRandomBoundNormal<glm::vec4>(10, 20, 1)") << ofRandomBoundNormal<glm::vec2>({10, 10}, {20,20}, {1,1});
307+
305308
ofLogNotice("======= alternate engines test =====");
306309
std::random_device rd {};
307310
std::seed_seq seq { rd(), rd(), rd(), rd() };

0 commit comments

Comments
 (0)