I need to generate equidistant sample points inside an arbitrary triangle in 3d. By "equidistant" I mean that the points within the plane containing the triangle form a grid with uniformly spaced coordinates in two perpendicular directions.
Approach 1:
There is a really nice algorithm to generate uniformly distributed random sample points in a triangle. I tried to adapt it to my needs, but I only got the following:
tri = [-10, 0, -5; 0, 10, 0; 10, 0, 5];
[X,Y] = meshgrid(linspace(0,1,100));
t = sqrt(X(:));
s = Y(:);
points = (1 - t) * tri(1, :) + bsxfun(@times, ((1 - s) * tri(2, :) + s * tri(3, :)), t);
scatter3(points(:, 1), points(:, 2), points(:,3), '.')
The result is not uniformly gridded.
Approach 2:
The more "classical" way is to get a grid in the xy plane and project the sampling points onto the triangle plane. I used barycentric coordinates to decide whether the points are inside the triangle or not.
tri = [-10, 0, -5; 0, 10, 0; 10, 0, 5];
coeffPlane = cross(tri(3,:)-tri(1,:), tri(2,:)-tri(1,:));
offset = dot(coeffPlane, tri(3,:));
[X,Y] = meshgrid(linspace(-10,10,20));
X = X(:);
Y = Y(:);
Z = (offset-coeffPlane(1)*X-coeffPlane(2)*Y)/coeffPlane(3);
points = [X Y Z];
baryCoord = cartesianToBarycentric(triangulation([1,2,3], tri), ones(size(X)), points);
TFinTri = all(baryCoord>=0 & baryCoord<=1,2);
points(~TFinTri,:) = [];
scatter3(points(:, 1), points(:, 2), points(:,3), '.')
Thats almost the result I want, but the grid space varies depending on the angle between plane normal and z axis. The most critical problem is that this algorithm won't work for triangles whose normal vectors are perpendicular to the z axis.
Do you have some ideas to improve my approaches to get an uniformly spaced grid of 3d points inside a triangle? Or is there a better way to solve the problem?