# TraceGeometry

• On 11/05/2015 at 12:08, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   15
Platform:   Windows  ;   Mac OSX  ;
Language(s) :     C++  ;

---------
Working on a shader that calculates the distance from front surface of object to back surface of object.

basic code is:

if (sd->vd != nullptr)
{
SurfaceIntersection si = SurfaceIntersection();
Vector64 point = sd->vd->p;

while (sd->vd->TraceGeometry(sd->vd->ray, 999999, sd->vd->lhit, &si) == true)
{
if (sd->vd->op == si.op)
{
distance += (si.p - point).GetLength();
point = si.p;
}
}
}

In theory this should intersect the first point, trace to the next unique object intersection incrementing the distance.  This will continue until the ray exits ( which should occur because the material has no refraction, internal reflections ).  Removed the normal checks to simplify.

The trace is bouncing between the traced intersection and the original intersection forever.

Ama

• On 11/05/2015 at 14:44, xxxxxxxx wrote:

try to add ray "epsilon" , for floating point errors, this may be the problem.
so if you trace from point (px,py,pz) to a direction (dx,dy,dz).
make the point = (px + dx*epsilon, py + dy*epsilon, pz + dz*epsilon) , epsilon should be small "1e-3f for example"

• On 11/05/2015 at 15:06, xxxxxxxx wrote:

I thought that is why you pass sd->vd->lhit?  Isn't that supposed to be the intersection to ignore?

Ama

• On 11/05/2015 at 15:32, xxxxxxxx wrote:

your code needs a little logic change, you don't modify sd->vd->ray , modify the point with epsilon, and the ray, and it should work!!

• On 11/05/2015 at 16:21, xxxxxxxx wrote:

the following?

sd->vd->p + ( !sd->vd->ray->v * 0.001 )

It does not seem necessary to alter the ray using this approach?

Ama

• On 11/05/2015 at 17:19, xxxxxxxx wrote:

this code should work if the problem is numerical precision error.

``````
if(sd->vd)
{
SurfaceIntersection si = SurfaceIntersection();
const float eps = 1e-3f;
Vector64 point = sd->vd->p + sd->vd->ray->v * eps;
Ray r;
r.p = point;
r.v = sd->vd->ray->v;
while(sd->vd->TraceGeometry(r, 999999, sd->vd->lhit, &si) == true)
{
if(sd->vd->op == si.op)
{
distance += (si.p - point).GetLength();
point = si.p;
r.p = point;
}
}
}
``````