# Point to bounding box directional distance

One smart guy told me that a manufacturing plan should always be questioned, even when it is composed by a pretty respectable software package, whatever it is, Mastercam or Fusion360, you name it. If what you’re doing is, for example, fabrication cost estimation, it’s always an approximation: coarser or finer, but never absolutely precise.

The ultimate manufacturing price is a function of several variables, many of whose are geometric in nature. For example, to assess manufacturability of a single face or a feature, it is necessary to predict how a tool is going to approach it. To approximate the tool length and diameter with respect to a CAD face, the following simple technique can be used. Let’s cover our face with a “dense enough” point distribution and measure two values at each probe: *clearance* and *depth*. Clearance is the distance from a probe point on a face to any other feature next to it in the face normal direction. This value has direct relation to the max allowed tool diameter. Depth is the distance from a probe point to the stock boundary (e.g., a solid box) measured in the *a priori known machining direction*. This depth value is evidently related to the tool length.

While both clearance and depth values are easily computed with a ray-casting method, there is one essential unknown in this whole approach. *What are the machining directions?* Well, actually, these directions can be guessed heuristically based on feature recognition. But let’s leave this subject aside for now as it is a big topic in itself.

If we manage to associate depth/clearance scalars and the machining direction field with each point on a CAD face, the resulting data structure will already serve as a somewhat approximation medium to the manufacturing plan. The values of depth, clearance, and machining directions can *potentially* tell more about features than, for example, primitive voxel nodes or dexels. It is still a question though, how to utilize this data effectively, because without a clear recipe this whole discussion isn’t worth a penny. Still, the idea looks quite promising. For each CAD face, we extract not only its local properties, such as a surface type, curvature, area, etc., but also its global properties with respect to the other model regions. Here we step aside from the feature recognition as such, and keep ourselves focused on individual faces that become aware of features indirectly, through intensive ray scanning.

Now, to the dirty practice. To measure depth values for a selected face, Analysis Situs 1.2 (the upcoming release) will provide a specific Tcl function:

Checking the distance from a point to the axis-aligned bounding box (AABB) in a predefined direction is a standard algorithm. I borrowed the implementation 5.3.3 from “Real-Time Collision Detection” by Christer Ericson and it worked nicely. Here’s the code:

`int RTCD::IntersectRayAABB(Point p, Vector d, AABB a,`

double &tmin, double &tmax)

{

const double EPSILON = RealEpsilon();

tmin = -DBL_MAX; // set to -DBL_MAX to get first hit on line.

tmax = DBL_MAX; // set to max distance ray can travel.

// For all three slabs

for ( int i = 0; i < 3; ++i )

{

if ( Abs(d[i]) < EPSILON )

{

// Ray is parallel to slab. No hit if origin not within slab.

if ( p[i] < a.min[i] || p[i] > a.max[i] )

return 0;

}

else

{

// Compute intersection of ray with near/far plane of slab.

double ood = 1.0f / d[i];

double t1 = (a.min[i] - p[i]) * ood;

double t2 = (a.max[i] - p[i]) * ood;

// `t1` is intersection with near plane, `t2` -- far plane.

if ( t1 > t2 )

std::swap(t1, t2);

// Compute the intersection of slab intersection intervals.

tmin = Max(tmin, t1);

tmax = Min(tmax, t2);

// Exit (no hit) as soon as slab intersection becomes empty.

if ( tmin > tmax )

return 0;

}

}

// Ray intersects all 3 slabs. Return `tmin` and `tmax`.

return 1;

}

*In contrast to the original version by C. Ericson, the above implementation is slightly adapted to handle rays emitted from the inside of an AABB.*

Once we know the probe point `p`, the machining axis `d`, and the stock box `a`, we can easily compute the “depth” property for the point `p` through the stock. Supplemented with the clearance value, this depth serves as an idealized tool descriptor.

I was thinking how to conclude this small blog post, but nothing actually comes to my mind. That’s likely because what’s written above is more of an open question. At the same time, this crumpled discussion opens the door to another challenging niche: Design for Manufacturing (DfM). Let this post be a teaser for the new “DfM” rubric then.