POV-Ray : Newsgroups : povray.general : Minimum Distance Function : Re: Minimum Distance Function Server Time 8 Aug 2022 22:34:08 EDT (-0400)
 Re: Minimum Distance Function
 From: jceddy Date: 22 Jul 2022 09:55:00 Message:
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#web.62daab4b6fb4e448864166f75d51d79c%40news.povray.org",
"dateCreated": "2022-07-22T13:55:00+00:00",
"datePublished": "2022-07-22T13:55:00+00:00",
"author": {
"@type": "Person",
"name": "jceddy"
}
}
>  if (py < Data->min_y || py > Data->max_y) {
>   diff = nearest - transformedPoint;
>  }

After I posted this, I realized that this is not correct and might be causing
the issue, since it will be incorrect for sample points "above" or "below" flat
triangles at min_y or max_y.

I think I can still optimize for points above or below the y bounding planes. I
should only have to check triangles within one "step" of the nearest vertex.

So I modified the code thusly:

if (py < Data->min_y || py > Data->max_y) {
minX = int(nearest[X]) - 1;
maxX = int(nearest[X]) + 2;
minZ = int(nearest[Z]) - 1;
maxZ = int(nearest[Z]) + 2;
}

Here is the new version of the method I am currently testing:

DBL HField::Proximity(Vector3d &pointOnObject, const Vector3d &samplePoint,
if (Data->kdTree == nullptr) {
return MAX_PROXIMITY_DISTANCE;
}

Vector3d transformedPoint = samplePoint;
Vector3d diff;
if (Trans != nullptr) {
MInvTransPoint(transformedPoint, transformedPoint, Trans);
}
DBL px = transformedPoint[X];
DBL py = transformedPoint[Y];
DBL pz = transformedPoint[Z];

Vector3d nearest = Data->kdTree->nearest_point(transformedPoint);

DBL dist = (nearest - transformedPoint).length();
DBL xmin = px - dist;
DBL zmin = pz - dist;
DBL xmax = px + dist;
DBL zmax = pz + dist;
int minX = max(0, int(xmin) - 1);
int maxX = min(Data->max_x, int(xmax) + 2);
int minZ = max(0, int(zmin) - 1);
int maxZ = min(Data->max_z, int(zmax) + 2);

if (py < Data->min_y || py > Data->max_y) {
minX = int(nearest[X]) - 1;
maxX = int(nearest[X]) + 2;
minZ = int(nearest[Z]) - 1;
maxZ = int(nearest[Z]) + 2;
}

dist = MAX_PROXIMITY_DISTANCE;
for (int z = minZ; z < maxZ - 1; z++)
{
for (int x = minX; x < maxX - 1; x++)
{
Vector3d p1(x, Data->Map[z][x], z);
Vector3d p2(x + 1, Data->Map[z][x + 1], z);
Vector3d p3(x + 1, Data->Map[z + 1][x + 1], z + 1);
Vector3d p4(x, Data->Map[z + 1][x], z + 1);

Vector3d trianglePt;
DBL test = NearestOnTriangle(trianglePt, transformedPoint, p1, p2, p3);
if (test < dist) {
dist = test;
nearest[X] = trianglePt[X];
nearest[Y] = trianglePt[Y];
nearest[Z] = trianglePt[Z];
}

test = NearestOnTriangle(trianglePt, transformedPoint, p1, p3, p4);
if (test < dist) {
dist = test;
nearest[X] = trianglePt[X];
nearest[Y] = trianglePt[Y];
nearest[Z] = trianglePt[Z];
}
}
}

diff = nearest - transformedPoint;

if (Trans != nullptr) {
MTransDirection(diff, diff, Trans);
}

pointOnObject = samplePoint + diff;
return diff.length();
}

Sorry for the SPAM...sometimes just putting my code in front of other people
forces me to thing more deeply about it and I can figure out issues that I