Often a user interaction with a model is needed in a configurator, for example for interactive positioning. In trCAD this can be done by ray-probing an object.
A configurator using the trcad script explained in this tutorial could look like this. The control for placing the ray is used by clicking on the small "+" button on the very right side.
A ray consists of a start point, from which the ray is “shot” and a direction in which it is shot. In code a ray can be defined by
ray myRay = ray(vOrigin, vDir)
where vOrigin and vDir are the vectors for start point and direction.
For checking if and where such a ray hits a solid, the trCAD function probe can be used. The code for the function call looks like this:
bool bHit = probe(myRay, oSolid, vHit)
where the info if the ray hit the solid oSolid is stored in the bool variable bHit and the info where it hits is written in the passed vector vHit.
If in addition the information of the solid’s normal vector at the hit position is needed, then the function call looks like this:
bool bHit = probe(myRay, oSolid, vHit, vHitNormal)
storing the normal vector information in the passed vector vHitNormal.
Now let’s use these features in a small example configurator illustrating the usage. In the example the ray-probing will be used to let a user position a cylinder on the surface of a sphere as a visual feedback, with having the cylinder sitting on the surface pointing outwards. This script is used for the example configurator at the beginning of this tutorial as well.
// The test solid
solid oSphere = sphere(20)
// Solid for visual feedback
solid oHitMark
// Variables for the ray
vector vRayOrigin_start = <[-25.0, 0.0, 0.0 ]>
vector vRayDir_start = <[ -1.0, 0.0, 0.0 ]>
// Variables for the probe hit
bool bHit
vector vHit
vector vHitNormal
// Setting the ray (origin and direction) by user interaction
open ray rRay
{
name = "Click on object"
descr = "Click to get info about hit on object."
value = ray( vRayOrigin_start, vRayDir_start )
}
// Probe, this sets vHit and vHitNormal if it hit.
bHit = probe(rRay, oSphere, vHit, vHitNormal)
// Check if it hit, setting the hit marker if hit.
if( bHit )
{
echo( "Hit at " + vHit +" with normal " + vHitNormal)
oHitMark = translation(vHit) >> cylinder(vHitNormal, 1.0)
}
else
{
echo( "No hit." )
}
// Making the solids, hit mark in green
make oSphere
make rgb(50,200,50) >> oHitMark
Please note that there the clicking-on input is triggered by activating the little square button under the ray vector inputs. Click that button and after that click in the viewport on the solid. The ray vector inputs will be auto-filled. The vectors themselves can be edited as well, but it is not recommended. In an end user frontend those would not be made visible.