Skip to content

Instantly share code, notes, and snippets.

@lukpazera
Last active February 20, 2020 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukpazera/47ff3d3f5cd91c84aeb996eb3c7522b1 to your computer and use it in GitHub Desktop.
Save lukpazera/47ff3d3f5cd91c84aeb996eb3c7522b1 to your computer and use it in GitHub Desktop.
A WIP sample on how to measure the distance from a given object in viewport to mouse - in pixels.
// This is not entirely working as it crashes MODO when an item with this drawing
// is selected and transform tool is enable. So something somewhere has to return bad value at some point
// and this sample is not handling that.
// This code needs to be executed from within item's Draw method.
// Drawing is aborted if mouse pointer is more then 120 pixels from the item to be drawn.
// The idea is to only draw the item when mouse pointer is close enough to it.
#include <lx_vp.hpp>
CLxUser_View view(stroke);
CLxUser_Matrix itemWorldMatrixRaw;
chan.Object(item, "worldMatrix", itemWorldMatrixRaw);
CLxMatrix4 itemWorldMatrix(itemWorldMatrixRaw);
CLxVector item3dpos = itemWorldMatrix.getTranslation();
CLxUser_View3DportService viewService;
int mx, my;
int viewIndex = viewService.Mouse(&mx, &my);
if (viewIndex >= 0)
{
CLxVector mousePos(mx, my, 0.0);
double ix, iy;
int result = view.ToScreen(item3dpos.v, &ix, &iy);
if(result == 0)
{
return; // this 3d location is not on screen, bail out.
}
if (ix < 0.0 || iy < 0.0 || mx < 0 || my < 0)
{
return;
}
CLxVector itemPos(ix, iy, 0.0); // item pos in screen coords
CLxVector screenOffset = mousePos - itemPos;
double distance = screenOffset.length();
if (distance > 120.0)
{
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment