Skip to content

Commit ce86afc

Browse files
authored
Merge pull request #96 from sebsjames/dev/navmesh_tweak
Minor tweak to NavMesh
2 parents 3197fae + 917edca commit ce86afc

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

examples/cray_eye.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ int main (int argc, char** argv)
2727
eyefile = std::string (argv[1]);
2828
}
2929

30+
// Compound eyes are tiny. We make them larger in our scene by this factor
31+
constexpr float eye_scaleup = 1000.0f;
32+
3033
float psrad = 0.5f;
3134
if (argc > 2) { psrad = std::atof (argv[2]); }
3235

33-
auto v = mplot::Visual<>(1024, 768, "mplot::compoundray::EyeVisual");
34-
3536
// We read the information from the eye file into a vector of Ommatidium objects. Ommatidium is
3637
// defined in "cameras/CompoundEyeDataTypes.h" in compound ray, mplot::Ommatidium is a
3738
// mplot/Seb's maths style equivalent. It contains 2 3D float vectors and two scalar floating point
@@ -43,6 +44,18 @@ int main (int argc, char** argv)
4344
// using it, but for this example we instead make use of mplot::compoundray::readEye
4445
if (mplot::compoundray::readEye (ommatidia.get(), eyefile) == nullptr) { std::cout << "Failed to read eye\n"; return -1; }
4546

47+
sm::range<sm::vec<float>> ommspan = sm::range<sm::vec<float>>::search_initialized();
48+
// Use the eye spacing to control the size of the coord arrows
49+
for (auto omm : *ommatidia.get()) {
50+
// omm is an Ommatidium. Want to know the x, y and z spans
51+
ommspan.update (omm.relativePosition);
52+
}
53+
float ca_len = eye_scaleup * ommspan.span().max() / 3.0f;
54+
auto v = mplot::Visual<>(1024, 768, "mplot::compoundray::EyeVisual");
55+
v.showCoordArrows (true);
56+
v.coordArrowsInScene (true);
57+
v.updateCoordLengths ({ ca_len, ca_len, ca_len }, 1.0f);
58+
4659
// Make some dummy data to demo the eye
4760
sm::vvec<float> ommatidiaData;
4861
ommatidiaData.linspace (0, 1, ommatidia->size());
@@ -70,7 +83,7 @@ int main (int argc, char** argv)
7083

7184
[[maybe_unused]] auto ep = v.addVisualModel (eyevm);
7285
// ep->reinitColours();
73-
ep->scaleViewMatrix (1000.0f);
86+
ep->scaleViewMatrix (eye_scaleup);
7487

7588
v.keepOpen();
7689
}

mplot/NavMesh.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -794,11 +794,20 @@ namespace mplot
794794
*
795795
* \param model_to_scene The model to scene transformation for the parent of the navmesh
796796
*
797+
* \param search_dist_mult a multiplier on the search distance. The length of vdir in this
798+
* function should cross the landscape model. By default it's the vector from the camera
799+
* location in the model frame of reference to the middle of the bounding box. If the vector
800+
* is too long when finding the surface of a convex hull, such as a model of a rock, it is
801+
* possible to mis-identify the back side of the model. However, for finding a location on a
802+
* large, flat, one-sided landscape, we want to make vdir long. search_dist_mult is applied
803+
* to vdir.
804+
*
797805
* \return tuple containing: the hit point in scene coordinates; the triangle normal of the
798806
* triangle we hit; and the indices of the triangle we hit.
799807
*/
800808
std::tuple<sm::vec<float>, sm::vec<float>, std::array<uint32_t, 4>>
801-
find_triangle_hit (const sm::mat44<float>& camspace, const sm::mat44<float>& model_to_scene)
809+
find_triangle_hit (const sm::mat44<float>& camspace, const sm::mat44<float>& model_to_scene,
810+
const float search_dist_mult = 1.0f)
802811
{
803812
sm::mat44<float> scene_to_model = model_to_scene.inverse();
804813
// use camera location in gltf to start from, then find model surface.
@@ -807,15 +816,16 @@ namespace mplot
807816
this->tn0 = {};
808817
sm::vec<float> hit = {};
809818
sm::vec<float> vdir = this->bb.mid() - camloc_mf;
810-
std::tie (hit, this->ti0, this->tn0) = this->find_triangle_crossing (camloc_mf , vdir);
819+
vdir *= search_dist_mult;
820+
std::tie (hit, this->ti0, this->tn0) = this->find_triangle_crossing (camloc_mf, vdir);
811821

812822
if (this->ti0[0] == std::numeric_limits<uint32_t>::max()) { std::cout << __func__ << ": No hit\n"; }
813823

814824
sm::vec<float> hp_scene = (model_to_scene * hit).less_one_dim();
815825

816-
constexpr bool debug = false;
826+
constexpr bool debug = true;
817827
if constexpr (debug) {
818-
std::cout << "found hit at " << hit << " (model); " << hp_scene << " (scene)\n";
828+
std::cout << "found hit at " << hit << " (model); " << hp_scene << " (scene) in direction " << vdir << "\n";
819829
// Check we'll get a hit when we compute_mesh_movement:
820830
sm::vec<sm::vec<float>, 3> tv_mf = this->triangle_vertices (this->ti0);
821831
std::cout << "tn0: " << this->tn0 << ", length " << this->tn0.length() << std::endl;

0 commit comments

Comments
 (0)