|
|
> 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,
TraceThreadData *threaddata) {
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
hadn't previously.
Cheers,
Joe
Post a reply to this message
|
|