From 3ea2b93895e80d98211402dd8be4b9808863f052 Mon Sep 17 00:00:00 2001 From: Seb James Date: Tue, 13 Oct 2026 15:27:26 +0100 Subject: [PATCH 1/5] Example to show transformations of a hexgrid --- examples/CMakeLists.txt | 3 + examples/hexgrid_transforms.cpp | 129 ++++++++++++++++++++++++++++++++ maths | 2 +- 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 examples/hexgrid_transforms.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 1cb2ddfa..46fb1621 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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) diff --git a/examples/hexgrid_transforms.cpp b/examples/hexgrid_transforms.cpp new file mode 100644 index 00000000..5d6debff --- /dev/null +++ b/examples/hexgrid_transforms.cpp @@ -0,0 +1,129 @@ +/* + * Test/demo what hexgrids do when transforms are introduced. + */ + +#include +#include +#include +#include + +import sm.vec; +import sm.hexgrid; + +import mplot.visual; +import mplot.visualdatamodel; +import mplot.hexgridvisual; +import mplot.vectorvisual; + +template +void draw (mplot::Visual<>& v, sm::vec<> offset); + +int main() +{ + mplot::Visual<> v(1600, 1000, "mplot::HexGridVisual transforms"); + draw (v, sm::vec<>{}); + draw (v, sm::vec<>{ 0.0f, -0.3f, 0.0f }); + v.keepOpen(); +} + +// Draw a single grid and its neighbour vectors +template +void draw_one (mplot::Visual<>& v, sm::hexgrid& hg, + const std::vector>* 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>(&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>(offset); + vvm->set_parent (v.get_id()); + vvm->thevec = hg.dirn_r(); + 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>(offset); + vvm->set_parent (v.get_id()); + vvm->thevec = hg.dirn_g(); + 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>(offset); + vvm->set_parent (v.get_id()); + vvm->thevec = hg.dirn_b(); + 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 +void draw (mplot::Visual& v, sm::vec<> offset) +{ + sm::hexgrid hg1(0.01f, 0.205f, 0.0f); + hg1.set_boundary_on_outer_edge(); + auto ch = hg1.hexen.begin(); + + std::vector> 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 + sm::mat rotn; + rotn.rotate (sm::vec<>::uz(), sm::mathconst::pi_over_2); + + // A reflection + sm::mat mirror = sm::mat::reflection (sm::vec<>{}, sm::vec<>::ux()); + + sm::hexgrid hg1_mirror = hg1; + hg1_mirror.transform (mirror); + offset[0] += 0.3f; + draw_one (v, hg1_mirror, &data1, offset, "Mirror (y-z)"); + + sm::hexgrid hg1_rotate = hg1; + hg1_rotate.transform (rotn); + offset[0] += 0.3f; + draw_one (v, hg1_rotate, &data1, offset, "Rotate (pi/2)"); + + sm::hexgrid 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 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"); +} diff --git a/maths b/maths index 8057b8a5..29563a2d 160000 --- a/maths +++ b/maths @@ -1 +1 @@ -Subproject commit 8057b8a52d7fde1948006bd9f611d23df2d0ef99 +Subproject commit 29563a2d005a5839100573f43a8f8555f7e993cc From a89951dddf5ec4b26bb551027d481400a433cb47 Mon Sep 17 00:00:00 2001 From: Seb James Date: Tue, 13 Oct 2026 16:58:52 +0100 Subject: [PATCH 2/5] Transform in hexgrid is now 2x2 (not 4x4) so we scale up to a 3x3 instead of down from a 4x4 --- mplot/HexGridVisual.cppm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mplot/HexGridVisual.cppm b/mplot/HexGridVisual.cppm index 1049dde5..8f20dfb8 100644 --- a/mplot/HexGridVisual.cppm +++ b/mplot/HexGridVisual.cppm @@ -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 lt = this->hg->tfm.linear().template as(); + sm::mat lt (this->hg->tfm.template as()); // A hexagon corner/vertex position sm::vec crnr = {}; From 5650d68d635c84998fd4ce1eeaa6212412421f0c Mon Sep 17 00:00:00 2001 From: Seb James Date: Tue, 13 Oct 2026 16:59:12 +0100 Subject: [PATCH 3/5] New maths and transform example --- examples/hexgrid_transforms.cpp | 27 ++++++++++++++++++++------- maths | 2 +- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/examples/hexgrid_transforms.cpp b/examples/hexgrid_transforms.cpp index 5d6debff..bfbe59aa 100644 --- a/examples/hexgrid_transforms.cpp +++ b/examples/hexgrid_transforms.cpp @@ -21,6 +21,7 @@ void draw (mplot::Visual<>& v, sm::vec<> offset); int main() { mplot::Visual<> v(1600, 1000, "mplot::HexGridVisual transforms"); + v.rotateAboutNearest (true); draw (v, sm::vec<>{}); draw (v, sm::vec<>{ 0.0f, -0.3f, 0.0f }); v.keepOpen(); @@ -47,7 +48,7 @@ void draw_one (mplot::Visual<>& v, sm::hexgrid& hg, auto vvm = std::make_unique>(offset); vvm->set_parent (v.get_id()); - vvm->thevec = hg.dirn_r(); + vvm->thevec = hg.dirn_r().plus_one_dim(0.0f); vvm->vgoes = mplot::VectorGoes::FromOrigin; vvm->thickness = hg.d / 10; vvm->fixed_colour = true; @@ -57,7 +58,7 @@ void draw_one (mplot::Visual<>& v, sm::hexgrid& hg, vvm = std::make_unique>(offset); vvm->set_parent (v.get_id()); - vvm->thevec = hg.dirn_g(); + vvm->thevec = hg.dirn_g().plus_one_dim(0.0f);; vvm->vgoes = mplot::VectorGoes::FromOrigin; vvm->thickness = hg.d / 10; vvm->fixed_colour = true; @@ -67,7 +68,7 @@ void draw_one (mplot::Visual<>& v, sm::hexgrid& hg, vvm = std::make_unique>(offset); vvm->set_parent (v.get_id()); - vvm->thevec = hg.dirn_b(); + vvm->thevec = hg.dirn_b().plus_one_dim(0.0f);; vvm->vgoes = mplot::VectorGoes::FromOrigin; vvm->thickness = hg.d / 10; vvm->fixed_colour = true; @@ -100,12 +101,17 @@ void draw (mplot::Visual& v, sm::vec<> offset) // Draw original grid draw_one (v, hg1, &data1, offset, "Original grid"); - // A rotational transform - sm::mat rotn; + // A rotational transform as 4x4 + //sm::mat rotn; + //rotn.rotate (sm::vec<>::uz(), sm::mathconst::pi_over_2); + // or 3x3 + sm::mat rotn; rotn.rotate (sm::vec<>::uz(), sm::mathconst::pi_over_2); - // A reflection - sm::mat mirror = sm::mat::reflection (sm::vec<>{}, sm::vec<>::ux()); + // A reflection as a 4x4 + // sm::mat mirror = sm::mat::reflection (sm::vec<>{}, sm::vec<>::ux()); + // or as a 3x3 + sm::mat mirror = sm::mat::reflection (sm::vec<>::ux()); sm::hexgrid hg1_mirror = hg1; hg1_mirror.transform (mirror); @@ -126,4 +132,11 @@ void draw (mplot::Visual& v, sm::vec<> offset) hg1_rot_mirr.transform (rotn * mirror); offset[0] += 0.3f; draw_one (v, hg1_rot_mirr, &data1, offset, "Mirr then rotn"); + + sm::hexgrid hg1_3d = hg1; + rotn.set_identity(); + rotn.rotate (sm::vec<>::uy(), sm::mathconst::pi_over_4); + hg1_3d.transform (rotn); + offset[0] += 0.3f; + draw_one (v, hg1_3d, &data1, offset, "3D rotns act up"); } diff --git a/maths b/maths index 29563a2d..e12737cc 160000 --- a/maths +++ b/maths @@ -1 +1 @@ -Subproject commit 29563a2d005a5839100573f43a8f8555f7e993cc +Subproject commit e12737ccd49c788eacd6c0ec64830c18895e3625 From 69ed15fea1b6c78014f4295b447c170a958b264b Mon Sep 17 00:00:00 2001 From: Seb James Date: Tue, 13 Oct 2026 17:03:43 +0100 Subject: [PATCH 4/5] Updates in maths --- maths | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths b/maths index e12737cc..c7a65c57 160000 --- a/maths +++ b/maths @@ -1 +1 @@ -Subproject commit e12737ccd49c788eacd6c0ec64830c18895e3625 +Subproject commit c7a65c57d2af152b401e486b1d17480056386242 From 85f6dfa6bcb8766736a8f3571f3e6d917a285582 Mon Sep 17 00:00:00 2001 From: Seb James Date: Wed, 14 Oct 2026 08:49:25 +0100 Subject: [PATCH 5/5] maths main --- maths | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths b/maths index c7a65c57..e1422e73 160000 --- a/maths +++ b/maths @@ -1 +1 @@ -Subproject commit c7a65c57d2af152b401e486b1d17480056386242 +Subproject commit e1422e735c8c83b141b767e5c3a3717302af038c