Getting polygon location (position) under mouse cursor
in_obj - object to raycast
in_raysample - mouse screen position e.g. CVector3(200,300,0)
polyInd - polygon index to raycast
out_pos - output 3d position
performance times:
1,5 mil tri sphere: 6 ms
5 mil tri sphere: 19 ms
Code: Select all
CStatus GetSinglePolyLocationSample3(ToolContext& in_ctxt, X3DObject& in_obj, CVector3 in_raysample, LONG polyInd, CVector3 &out_pos)
{
PolygonMesh pmesh( in_obj.GetActivePrimitive().GetGeometry() );
CTransformation localToGlobal = in_obj.GetKinematics().GetGlobal().GetTransform();
PolygonFace face = pmesh.GetPolygons().GetItem( polyInd );
CLongArray pfaceInd = face.GetNeighborPolygons().GetIndexArray();
pfaceInd.Add(polyInd);
if ( pmesh.SetupPointLocatorQueries( siClosestSurfaceRaycastIntersection,&localToGlobal, pfaceInd.GetCount(),pfaceInd.GetArray(), 10 ) != CStatus::OK)
return CStatus::False;
CVector3 l_rayview;
if( in_ctxt.WorldToView(in_raysample, l_rayview) != CStatus::OK )
return CStatus::Fail;
CLine l_ray;
in_ctxt.GetWorldRay((LONG)l_rayview[0],(LONG)l_rayview[1],l_ray);
CVector3 rayt = l_ray.GetTangent();
CVector3 rayp = l_ray.GetOrigin();
PointLocatorData locators;
locators = pmesh.GetRaycastIntersections( 1, (double*)&rayp, (double*)&rayt, siSemiLineIntersection );
if ( !locators.IsPtLocatorValid(0) )
return CStatus::Fail;
if (pmesh.EvaluatePositions(locators, -1, 0, (double*)&out_pos) != CStatus::OK)
return CStatus::Fail;
out_pos.MulByTransformationInPlace(localToGlobal);
return CStatus::OK;
}