Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ target_link_libraries(showcase mathplot_examples)
add_executable(hexgrid hexgrid.cpp)
target_link_libraries(hexgrid mathplot_examples)

add_executable(hexgrid_transforms hexgrid_transforms.cpp)
target_link_libraries(hexgrid_transforms mathplot_examples)

add_executable(hexgrid_fft hexgrid_fft.cpp)
target_link_libraries(hexgrid_fft mathplot_examples)

Expand Down
142 changes: 142 additions & 0 deletions examples/hexgrid_transforms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Test/demo what hexgrids do when transforms are introduced.
*/

#include <iostream>
#include <memory>
#include <vector>
#include <cmath>

import sm.vec;
import sm.hexgrid;

import mplot.visual;
import mplot.visualdatamodel;
import mplot.hexgridvisual;
import mplot.vectorvisual;

template<sm::hexalign halign>
void draw (mplot::Visual<>& v, sm::vec<> offset);

int main()
{
mplot::Visual<> v(1600, 1000, "mplot::HexGridVisual transforms");
v.rotateAboutNearest (true);
draw<sm::hexalign::point_up> (v, sm::vec<>{});
draw<sm::hexalign::flat_up> (v, sm::vec<>{ 0.0f, -0.3f, 0.0f });
v.keepOpen();
}

// Draw a single grid and its neighbour vectors
template<sm::hexalign halign>
void draw_one (mplot::Visual<>& v, sm::hexgrid<float, halign>& hg,
const std::vector<sm::vec<>>* data1, const sm::vec<>& offset, const std::string& label)
{
// sm::HexVisMode::HexInterp to see the hexagons or sm::HexVisMode::Triangles for a smoother surface plot
const mplot::HexVisMode visMode = mplot::HexVisMode::HexInterp;

// Add a HexGridVisual to display the HexGrid within the sm::Visual scene
auto hgv1 = std::make_unique<mplot::HexGridVisual<float, halign>>(&hg, offset);
hgv1->set_parent (v.get_id());
hgv1->cm.setType (mplot::ColourMapType::Duochrome);
hgv1->setVectorData (data1);
hgv1->hexVisMode = visMode;
hgv1->zScale.null_scaling();
hgv1->addLabel (label, sm::vec<>{ -0.1f, -0.15f }, mplot::TextFeatures(0.02f));
hgv1->finalize();
v.addVisualModel (hgv1);

auto vvm = std::make_unique<mplot::VectorVisual<float, 3>>(offset);
vvm->set_parent (v.get_id());
vvm->thevec = hg.dirn_r().plus_one_dim(0.0f);
vvm->vgoes = mplot::VectorGoes::FromOrigin;
vvm->thickness = hg.d / 10;
vvm->fixed_colour = true;
vvm->single_colour = mplot::colour::crimson;
vvm->finalize();
v.addVisualModel (vvm);

vvm = std::make_unique<mplot::VectorVisual<float, 3>>(offset);
vvm->set_parent (v.get_id());
vvm->thevec = hg.dirn_g().plus_one_dim(0.0f);;
vvm->vgoes = mplot::VectorGoes::FromOrigin;
vvm->thickness = hg.d / 10;
vvm->fixed_colour = true;
vvm->single_colour = mplot::colour::springgreen2;
vvm->finalize();
v.addVisualModel (vvm);

vvm = std::make_unique<mplot::VectorVisual<float, 3>>(offset);
vvm->set_parent (v.get_id());
vvm->thevec = hg.dirn_b().plus_one_dim(0.0f);;
vvm->vgoes = mplot::VectorGoes::FromOrigin;
vvm->thickness = hg.d / 10;
vvm->fixed_colour = true;
vvm->single_colour = mplot::colour::royalblue2;
vvm->finalize();
v.addVisualModel (vvm);
}

// Draw the original and all transformed grids in one row.
template<sm::hexalign halign>
void draw (mplot::Visual<mplot::gl::version_4_1>& v, sm::vec<> offset)
{
sm::hexgrid<float, halign> hg1(0.01f, 0.205f, 0.0f);
hg1.set_boundary_on_outer_edge();
auto ch = hg1.hexen.begin();

std::vector<sm::vec<>> data1(hg1.num());
for (unsigned int ri = 0; ri < hg1.num(); ++ri) {
// We visualize the original x/y position on the grid with a Duochrome colourmap
data1[ri][0] = hg1.d_x[ri];
data1[ri][1] = hg1.d_y[ri];
}

// Centre hex is reddish
data1[ch->vi] = { 0.1f, 0, 0 };
// neighbour0 is greenish
auto chn0 = ch->get_neighbour(0);
data1[chn0->vi] = { 0, 0.1f, 0 };

// Draw original grid
draw_one (v, hg1, &data1, offset, "Original grid");

// A rotational transform as 4x4
//sm::mat<float, 4> rotn;
//rotn.rotate (sm::vec<>::uz(), sm::mathconst<float>::pi_over_2);
// or 3x3
sm::mat<float, 3> rotn;
rotn.rotate (sm::vec<>::uz(), sm::mathconst<float>::pi_over_2);

// A reflection as a 4x4
// sm::mat<float, 4> mirror = sm::mat<float, 4>::reflection (sm::vec<>{}, sm::vec<>::ux());
// or as a 3x3
sm::mat<float, 3> mirror = sm::mat<float, 3>::reflection (sm::vec<>::ux());

sm::hexgrid<float, halign> hg1_mirror = hg1;
hg1_mirror.transform (mirror);
offset[0] += 0.3f;
draw_one (v, hg1_mirror, &data1, offset, "Mirror (y-z)");

sm::hexgrid<float, halign> hg1_rotate = hg1;
hg1_rotate.transform (rotn);
offset[0] += 0.3f;
draw_one (v, hg1_rotate, &data1, offset, "Rotate (pi/2)");

sm::hexgrid<float, halign> hg1_mirr_rot = hg1;
hg1_mirr_rot.transform (mirror * rotn);
offset[0] += 0.3f;
draw_one (v, hg1_mirr_rot, &data1, offset, "Rotn then mirr");

sm::hexgrid<float, halign> hg1_rot_mirr = hg1;
hg1_rot_mirr.transform (rotn * mirror);
offset[0] += 0.3f;
draw_one (v, hg1_rot_mirr, &data1, offset, "Mirr then rotn");

sm::hexgrid<float, halign> hg1_3d = hg1;
rotn.set_identity();
rotn.rotate (sm::vec<>::uy(), sm::mathconst<float>::pi_over_4);
hg1_3d.transform (rotn);
offset[0] += 0.3f;
draw_one (v, hg1_3d, &data1, offset, "3D rotns act up");
}
2 changes: 1 addition & 1 deletion maths
2 changes: 1 addition & 1 deletion mplot/HexGridVisual.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export namespace mplot
this->vertex_push (this->zoom * vtx_0, this->vertexPositions);

// The rotation from the transformation in the hexgrid (if any)
sm::mat<float, 3> lt = this->hg->tfm.linear().template as<float>();
sm::mat<float, 3> lt (this->hg->tfm.template as<float>());

// A hexagon corner/vertex position
sm::vec<float> crnr = {};
Expand Down
Loading