Skip to content

Commit

Permalink
Merge pull request #126 from optseb/optcontribs
Browse files Browse the repository at this point in the history
Adds ability to set data in GraphVisual with vvec of coords
  • Loading branch information
sebjameswml authored Aug 16, 2023
2 parents c62a35a + fd6431e commit e5dc9f7
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ if(USE_GLEW)
target_link_libraries(graph_logist2 GLEW::GLEW)
endif()

add_executable(graph_coords graph_coords.cpp)
target_link_libraries(graph_coords OpenGL::GL glfw Freetype::Freetype)
if(USE_GLEW)
target_link_libraries(graph_coords GLEW::GLEW)
endif()

add_executable(linregr linregr.cpp)
target_link_libraries(linregr OpenGL::GL glfw Freetype::Freetype)
if(USE_GLEW)
Expand Down
52 changes: 52 additions & 0 deletions examples/graph_coords.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// This version uses containers of coordinates to set the graph data.

#include <morph/Visual.h>
#include <morph/GraphVisual.h>
#include <morph/vvec.h>
#include <morph/mathconst.h>
#include <morph/Random.h>

int main()
{
morph::Visual v(1024, 768, "Coordinates in GraphVisual");
v.setSceneTrans (morph::vec<float,3>({-0.458656f, -0.428112f, -2.5f}));

auto gv = std::make_unique<morph::GraphVisual<double>> (v.shaders, morph::vec<float>({0,0,0}));

// Choose marker policy for this graph
gv->policy = morph::stylepolicy::markers;
// And set the graph limits suitably
gv->setlimits (0, 1, 0, 1);

// Two random number generators
morph::RandNormal<double> rn1 (0.2, 0.07);
morph::RandNormal<double> rn2 (0.6, 0.04);

// Three coordinates, which will be randomly sampled using rngs
morph::vec<double, 2> x1 = {rn1.get(), rn1.get()};
morph::vec<double, 2> x2 = {rn2.get(), rn1.get()};
morph::vec<double, 2> x3 = {rn2.get(), rn2.get()};

morph::vvec<morph::vec<double, 2>> coords = { x1, x2, x3 };

gv->setdata (coords);
gv->finalize();

auto gvp = v.addVisualModel (gv);

while (v.readyToFinish == false) {
glfwWaitEventsTimeout (0.01667); // 16.67 ms ~ 60 Hz

// Re-draw numbers for the coordinates
x1 = {rn1.get(), rn1.get()};
x2 = {rn2.get(), rn1.get()};
x3 = {rn2.get(), rn2.get()};
coords = { x1, x2, x3 };

// Update the graph
gvp->update (coords, 0);
v.render();
}

return 0;
}
39 changes: 39 additions & 0 deletions morph/GraphVisual.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,18 @@ namespace morph {
this->reinit();
}

//! update() overload that accepts vvec of coords
void update (const morph::vvec<morph::vec<Flt, 2>>& _coords, const size_t data_idx)
{
std::vector<Flt> absc (_coords.size(), Flt{0});
std::vector<Flt> ord (_coords.size(), Flt{0});
for (unsigned int i = 0; i < _coords.size(); ++i) {
absc[i] = _coords[i][0];
ord[i] = _coords[i][1];
}
this->update (absc, ord, data_idx);
}

//! Set marker and colours in ds, according the 'style policy'
void setstyle (morph::DatasetStyle& ds, std::array<float, 3> col, morph::markerstyle ms)
{
Expand Down Expand Up @@ -447,6 +459,20 @@ namespace morph {
this->setdata (_abscissae, _data, ds);
}

//! setdata overload that accepts vvec of coords (as morph::vec<Flt, 2>)
void setdata (const morph::vvec<morph::vec<Flt, 2>>& _coords,
const std::string name = "", const morph::axisside axisside = morph::axisside::left)
{
// Split coords into two vectors then call setdata() overload
std::vector<Flt> absc (_coords.size(), Flt{0});
std::vector<Flt> ord (_coords.size(), Flt{0});
for (unsigned int i = 0; i < _coords.size(); ++i) {
absc[i] = _coords[i][0];
ord[i] = _coords[i][1];
}
this->setdata (absc, ord, name, axisside);
}

//! Set a dataset into the graph. Provide abscissa and ordinate and a dataset
//! style. The locations of the markers for each dataset are computed and stored
//! in this->graohDataCoords, one vector for each dataset.
Expand Down Expand Up @@ -511,6 +537,19 @@ namespace morph {
}
}

//! setdata overload that accepts vvec of coords (as morph::vec<Flt, 2>)
void setdata (const morph::vvec<morph::vec<Flt, 2>>& _coords, const DatasetStyle& ds)
{
// Split coords into two vectors then call setdata() overload
std::vector<Flt> absc (_coords.size(), Flt{0});
std::vector<Flt> ord (_coords.size(), Flt{0});
for (unsigned int i = 0; i < _coords.size(); ++i) {
absc[i] = _coords[i][0];
ord[i] = _coords[i][1];
}
this->setdata (absc, ord, ds);
}

//! Special setdata for a morph::histo object
void setdata (const morph::histo<Flt>& h, const std::string name = "")
{
Expand Down

0 comments on commit e5dc9f7

Please sign in to comment.