Distance between 2 surfaces along surface normal direction

18 views (last 30 days)
Hello. I have 2 surfaces and I would like to calculate the distance between them along surface normal. I created an example below that illustrates what I am trying to do. It shows 2 surfaces that contain the localized surface normal directions as red lines for each surface. This example uses spheres, but in general the surfaces I would like to evaluate not easily described in functional form. For each of these surface points I would like to calculate the distance along it's surface normal (red line) until it intersects with the other surface. If it does not intersect with the other surface along this direction I would like to report a NaN.
The other section in the Matlab code below isan attempt to see if I understand the surfnorm output correctly. I was trying to derive a line emitting from the 1st surface that is normal and passes through the 2nd surface. The distance for this blue line should be However, I don't think the red and blue lines overlap, so I am not sure if my understanding is correct?
The final output should be 2 sets of distances, since each surface will have a different normal direction and will intersect with the other surface differently. Any suggestions on how I can try to derive these distances for each surface would be greatly appreciated. I have the optimization and curve fitting toolboxes if those functions would help solve this problem. Thank you.
%%Derive distance between 2 surfaces along the surface normal
%create a unit sphere for testing purposes
[XSphere1,YSphere1,ZSphere1] = sphere;
%specify the shift and scale paramters for the 2nd sphere
XShift = 1.5;
YShift = -1.5;
ZShift = 0.5;
XScale = 0.7;
YScale = 1.4;
ZScale = 1;
%scale and shift another sphere
XSphere2 = XSphere1*XScale + XShift;
YSphere2 = YSphere1*YScale + YShift ;
ZSphere2 = ZSphere1*ZScale + ZShift;
%create plots to ensure it makes sense and to visulize the surface normals
figure, surfnorm(XSphere1, YSphere1, ZSphere1)
hold on
surfnorm(XSphere2, YSphere2, ZSphere2)
%%Use the derived scaled surface normal data to show intersection
%not sure if I am intepreting the output from the surfnorm function
%correctly
[Xnorm1, Ynorm1, Znorm1] = surfnorm(XSphere1, YSphere1, ZSphere1);
[Xnorm2, Ynorm2, Znorm2] = surfnorm(XSphere2, YSphere2, ZSphere2);
%scale the normalized normal vector
V2x = Xnorm1*5;
V2y = Ynorm1*5;
V2z = Znorm1*5;
V2x2 = Xnorm1*1;
V2y2 = Ynorm1*1;
V2z2 = Znorm1*1;
%for testing one of the points on the first surface
TestPoint = 180;
%plot one of the surface normals to verify that it makes sense; It looks
%like the red line does not overlap with blue line. Am I using the
%surfnorm output correctly?
figure, plot3([XSphere1(TestPoint), XSphere1(TestPoint) + Xnorm1(TestPoint), V2x(TestPoint), V2x2(TestPoint)],...
[YSphere1(TestPoint), YSphere1(TestPoint) + Ynorm1(TestPoint), V2y(TestPoint), V2y2(TestPoint)],...
[ZSphere1(TestPoint), ZSphere1(TestPoint) + Znorm1(TestPoint), V2z(TestPoint), V2z2(TestPoint)])
hold on
surfnorm(XSphere1, YSphere1, ZSphere1)
surf(XSphere2, YSphere2, ZSphere2)

Answers (1)

Alan Weiss
Alan Weiss on 16 Aug 2017
You could just perform a constrained minimization. Have one endpoint constrained to be in one region, the other endpoint on the other region, and when you get a minimum length of the line between the two points, the line will be normal to the constraining surfaces.
Then again, you need fmincon from Optimization Toolbox™ for this minimization.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Kevin Baker
Kevin Baker on 16 Aug 2017
Hello Alan. I don't mind using fmincon, since I have Optimization toolbox but I do need to understand how to use it correctly... :)
I don't think this is what I want to do if I understand what you are suggesting. For example, you can imagine a normal line emitting from one surface and if you follow this direction to the other surface it might have a longer length than the min length you mentioned that uses a different path (i.e., the derived distance must be along the normal direction).
Does the objective function require only one independent variable or can it handle more since the distance is in 3D space?
I also don't know if the normal line that I indicated in the Matlab code is correct. I have somewhat confused as the output from the surfnorm Matlab function. I think the origin of the normal vector is at the specific surface location (x,y,z) but not sure based on the visualization I created in the Matlab code example.
Would it be possible to create a Matlab code (or links to some relevant examples) illustrating what you describing using the example I provided? Even if it derives the distance like you outlined I think that would help me understand the basics, so I can modify it to fit my needs.
Thank you for your help.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!