Marc
Marc

Reputation: 51

How to find minimum distance from points to a plane

I have a plane which intersects a point cloud and want to extract the points at a certain distance around the intersection point. In my script I can already determine points around the plane, but unfortunately points that are not intersected by my plane are also determined, as you can see in the following figure. (The red circles show the determined points)

enter image description here

That is the part of my script that detects the points.

p0 = [0 0 0];
p1 = [-74 -76.968 0];
p2 = [0 0 690.18];

n = cross(p0-p1,p0-p2);
n = n/norm(n);

FDIST = @(X,Y,Z) sum(n.*(p0-[X Y Z]));
D = arrayfun(FDIST,x,y,z,'uni',false);
D1 = cell2mat(D);
mindist = 0.65;                           
ind = abs(D1) < mindist;

x,y and z are 114964x1 vectors, respectively. Since the intersections are all in the third quadrant, I tried to consider only the negative values in the x and y vectors. But I got an error that the input arguments don't have the same length anymore.

idx = x1<0;
cx = x1(idx);

idy = y1<0;
cy = y1(idy);

Error using arrayfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 57966 in dimension 1. Input #3 has size
58204

I thought another way might be to adjust FDIST, however I'm not very good at MATLAB's function handle and therefore wasn't successful with it. Thanks in advance for your help.

Upvotes: 0

Views: 231

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 102880

I think you should use the following to subset the points within the third quadrant

idx_neg = (x <=0 & y<=0);
x = x(idx_neg);
y = y(idx_neg);
z = z(idx_neg);

such that the dimension of x, y, and z are equal.

I used the following code with dummy data, which runs without any error.

clc;
clear;

% dummy points data for x, y, and z
x = randn(100,1);
y = randn(100,1);
z = randn(100,1);

p0 = [0 0 0];
p1 = [-74 -76.968 0];
p2 = [0 0 690.18];

n = cross(p0-p1,p0-p2);
n = n/norm(n);

% restrict to the third quadrant
idx_neg = (x <=0 & y<=0);
x = x(idx_neg);
y = y(idx_neg);
z = z(idx_neg);

FDIST = @(X,Y,Z) sum(n.*(p0-[X Y Z]));
D = arrayfun(FDIST,x,y,z,'uni',false);
D1 = cell2mat(D);
mindist = 0.65;                           
ind = abs(D1) < mindist;

Upvotes: 1

Related Questions