MATLAB Answers

Problem with Patch, trying to fill a surface based on points.

4 views (last 30 days)
luc
luc on 29 Apr 2015
Answered: luc on 1 May 2015
I got an output of a few X Y and Z points, which I want to create into a patch object.
The output however, is more like a sliced potato.
Copy the code below, note that only the last bit about XXX, YYY and ZZZ give the coordinates. any idea how to create a nice consistent shape?
Thanks in advance guys!
PS; Related question; How can I plot a patch object? Say I created a patch object A=patch(x,y,z,1)
Now, after editing it (A.Vertices=[..,..,..]) I want to plot the object in figure(X). What would be the command to get it there?
clc;clear all;close all
r=20;
x=4;
y=6;
z=8;
x0=3;
y0=8;
z0=8;
FOV=pi;
x1=[ x/(x^2 + y^2 + z^2)^(1/2), y/(x^2 + y^2 + z^2)^(1/2), z/(x^2 + y^2 + z^2)^(1/2)]; %<--klopt.
yz=null(x1).'; %find the null spaces of normalised V
xyz=[x1;yz]; %The rows of this matrix are the axes of a normalised
U=xyz(2,:)';
W=xyz(3,:)';%U and W are a ortogonal normal basis for normalised A
a=U(1);b=U(2);c=U(3);d=W(1);e=W(2);f=W(3);
n=1;
tic
for FOV=0:0.1*pi:FOV
for alpha=0:0.2*pi:2*pi
XXX(n)=x0 - ((x/(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*(d*cos(alpha) + a*sin(alpha)))*(a*d*x*x0 + a*d*y*y0 + a*d*z*z0 - a*d*(abs(y + tan(FOV/2)*cos(conj(alpha))*conj(e)*(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*sin(conj(alpha))*conj(b)*(x^2 + y^2 + z^2)^(1/2))^2/(x^2 + y^2 + z^2) + abs(z + tan(FOV/2)*cos(conj(alpha))*conj(f)*(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*sin(conj(alpha))*conj(c)*(x^2 + y^2 + z^2)^(1/2))^2/(x^2 + y^2 + z^2) + abs(a*d*x + a*tan(FOV/2)*cos(conj(alpha))*abs(d)^2*(x^2 + y^2 + z^2)^(1/2) + d*tan(FOV/2)*sin(conj(alpha))*abs(a)^2*(x^2 + y^2 + z^2)^(1/2))^2/(abs(a)^2*abs(d)^2*(x^2 + y^2 + z^2)))^(1/2)*(x^2 + y^2 + z^2)^(1/2)*(r^2 - x0^2 - y0^2 - z0^2 + (abs(a)^2*abs(d)^2*(a*d*x*x0*cos(FOV/2) + a*d*y*y0*cos(FOV/2) + a*d*z*z0*cos(FOV/2) + a*x0*sin(FOV/2)*cos(conj(alpha))*abs(d)^2*(x^2 + y^2 + z^2)^(1/2) + d*x0*sin(FOV/2)*sin(conj(alpha))*abs(a)^2*(x^2 + y^2 + z^2)^(1/2) + a*d*y0*sin(FOV/2)*cos(conj(alpha))*conj(e)*(x^2 + y^2 + z^2)^(1/2) + a*d*z0*sin(FOV/2)*cos(conj(alpha))*conj(f)*(x^2 + y^2 + z^2)^(1/2) + a*d*y0*sin(FOV/2)*sin(conj(alpha))*conj(b)*(x^2 + y^2 + z^2)^(1/2) + a*d*z0*sin(FOV/2)*sin(conj(alpha))*conj(c)*(x^2 + y^2 + z^2)^(1/2))^2)/(a^2*d^2*(abs(a*d*x*cos(FOV/2) + a*sin(FOV/2)*cos(conj(alpha))*abs(d)^2*(x^2 + y^2 + z^2)^(1/2) + d*sin(FOV/2)*sin(conj(alpha))*abs(a)^2*(x^2 + y^2 + z^2)^(1/2))^2 + abs(y*cos(FOV/2) + sin(FOV/2)*cos(conj(alpha))*conj(e)*(x^2 + y^2 + z^2)^(1/2) + sin(FOV/2)*sin(conj(alpha))*conj(b)*(x^2 + y^2 + z^2)^(1/2))^2*abs(a)^2*abs(d)^2 + abs(z*cos(FOV/2) + sin(FOV/2)*cos(conj(alpha))*conj(f)*(x^2 + y^2 + z^2)^(1/2) + sin(FOV/2)*sin(conj(alpha))*conj(c)*(x^2 + y^2 + z^2)^(1/2))^2*abs(a)^2*abs(d)^2)))^(1/2) + a*x0*tan(FOV/2)*cos(conj(alpha))*abs(d)^2*(x^2 + y^2 + z^2)^(1/2) + d*x0*tan(FOV/2)*sin(conj(alpha))*abs(a)^2*(x^2 + y^2 + z^2)^(1/2) + a*d*y0*tan(FOV/2)*cos(conj(alpha))*conj(e)*(x^2 + y^2 + z^2)^(1/2) + a*d*z0*tan(FOV/2)*cos(conj(alpha))*conj(f)*(x^2 + y^2 + z^2)^(1/2) + a*d*y0*tan(FOV/2)*sin(conj(alpha))*conj(b)*(x^2 + y^2 + z^2)^(1/2) + a*d*z0*tan(FOV/2)*sin(conj(alpha))*conj(c)*(x^2 + y^2 + z^2)^(1/2)))/(a*d*(abs(y + tan(FOV/2)*cos(conj(alpha))*conj(e)*(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*sin(conj(alpha))*conj(b)*(x^2 + y^2 + z^2)^(1/2))^2/(x^2 + y^2 + z^2) + abs(z + tan(FOV/2)*cos(conj(alpha))*conj(f)*(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*sin(conj(alpha))*conj(c)*(x^2 + y^2 + z^2)^(1/2))^2/(x^2 + y^2 + z^2) + abs(a*d*x + a*tan(FOV/2)*cos(conj(alpha))*abs(d)^2*(x^2 + y^2 + z^2)^(1/2) + d*tan(FOV/2)*sin(conj(alpha))*abs(a)^2*(x^2 + y^2 + z^2)^(1/2))^2/(abs(a)^2*abs(d)^2*(x^2 + y^2 + z^2)))*(x^2 + y^2 + z^2)^(1/2));
YYY(n)= y0 - ((y/(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*(e*cos(alpha) + b*sin(alpha)))*(b*e*x*x0 + b*e*y*y0 + b*e*z*z0 - b*e*(abs(x + tan(FOV/2)*cos(conj(alpha))*conj(d)*(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*sin(conj(alpha))*conj(a)*(x^2 + y^2 + z^2)^(1/2))^2/(x^2 + y^2 + z^2) + abs(z + tan(FOV/2)*cos(conj(alpha))*conj(f)*(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*sin(conj(alpha))*conj(c)*(x^2 + y^2 + z^2)^(1/2))^2/(x^2 + y^2 + z^2) + abs(b*e*y + b*tan(FOV/2)*cos(conj(alpha))*abs(e)^2*(x^2 + y^2 + z^2)^(1/2) + e*tan(FOV/2)*sin(conj(alpha))*abs(b)^2*(x^2 + y^2 + z^2)^(1/2))^2/(abs(b)^2*abs(e)^2*(x^2 + y^2 + z^2)))^(1/2)*(x^2 + y^2 + z^2)^(1/2)*(r^2 - x0^2 - y0^2 - z0^2 + (abs(b)^2*abs(e)^2*(b*e*x*x0*cos(FOV/2) + b*e*y*y0*cos(FOV/2) + b*e*z*z0*cos(FOV/2) + b*y0*sin(FOV/2)*cos(conj(alpha))*abs(e)^2*(x^2 + y^2 + z^2)^(1/2) + e*y0*sin(FOV/2)*sin(conj(alpha))*abs(b)^2*(x^2 + y^2 + z^2)^(1/2) + b*e*z0*sin(FOV/2)*sin(conj(alpha))*conj(c)*(x^2 + y^2 + z^2)^(1/2) + b*e*x0*sin(FOV/2)*cos(conj(alpha))*conj(d)*(x^2 + y^2 + z^2)^(1/2) + b*e*z0*sin(FOV/2)*cos(conj(alpha))*conj(f)*(x^2 + y^2 + z^2)^(1/2) + b*e*x0*sin(FOV/2)*sin(conj(alpha))*conj(a)*(x^2 + y^2 + z^2)^(1/2))^2)/(b^2*e^2*(abs(b*e*y*cos(FOV/2) + b*sin(FOV/2)*cos(conj(alpha))*abs(e)^2*(x^2 + y^2 + z^2)^(1/2) + e*sin(FOV/2)*sin(conj(alpha))*abs(b)^2*(x^2 + y^2 + z^2)^(1/2))^2 + abs(x*cos(FOV/2) + sin(FOV/2)*cos(conj(alpha))*conj(d)*(x^2 + y^2 + z^2)^(1/2) + sin(FOV/2)*sin(conj(alpha))*conj(a)*(x^2 + y^2 + z^2)^(1/2))^2*abs(b)^2*abs(e)^2 + abs(z*cos(FOV/2) + sin(FOV/2)*cos(conj(alpha))*conj(f)*(x^2 + y^2 + z^2)^(1/2) + sin(FOV/2)*sin(conj(alpha))*conj(c)*(x^2 + y^2 + z^2)^(1/2))^2*abs(b)^2*abs(e)^2)))^(1/2) + b*y0*tan(FOV/2)*cos(conj(alpha))*abs(e)^2*(x^2 + y^2 + z^2)^(1/2) + e*y0*tan(FOV/2)*sin(conj(alpha))*abs(b)^2*(x^2 + y^2 + z^2)^(1/2) + b*e*z0*tan(FOV/2)*sin(conj(alpha))*conj(c)*(x^2 + y^2 + z^2)^(1/2) + b*e*x0*tan(FOV/2)*cos(conj(alpha))*conj(d)*(x^2 + y^2 + z^2)^(1/2) + b*e*z0*tan(FOV/2)*cos(conj(alpha))*conj(f)*(x^2 + y^2 + z^2)^(1/2) + b*e*x0*tan(FOV/2)*sin(conj(alpha))*conj(a)*(x^2 + y^2 + z^2)^(1/2)))/(b*e*(abs(x + tan(FOV/2)*cos(conj(alpha))*conj(d)*(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*sin(conj(alpha))*conj(a)*(x^2 + y^2 + z^2)^(1/2))^2/(x^2 + y^2 + z^2) + abs(z + tan(FOV/2)*cos(conj(alpha))*conj(f)*(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*sin(conj(alpha))*conj(c)*(x^2 + y^2 + z^2)^(1/2))^2/(x^2 + y^2 + z^2) + abs(b*e*y + b*tan(FOV/2)*cos(conj(alpha))*abs(e)^2*(x^2 + y^2 + z^2)^(1/2) + e*tan(FOV/2)*sin(conj(alpha))*abs(b)^2*(x^2 + y^2 + z^2)^(1/2))^2/(abs(b)^2*abs(e)^2*(x^2 + y^2 + z^2)))*(x^2 + y^2 + z^2)^(1/2));
ZZZ(n)= z0 - ((z/(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*(f*cos(alpha) + c*sin(alpha)))*(c*f*x*x0 + c*f*y*y0 + c*f*z*z0 - c*f*(abs(x + tan(FOV/2)*cos(conj(alpha))*conj(d)*(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*sin(conj(alpha))*conj(a)*(x^2 + y^2 + z^2)^(1/2))^2/(x^2 + y^2 + z^2) + abs(y + tan(FOV/2)*cos(conj(alpha))*conj(e)*(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*sin(conj(alpha))*conj(b)*(x^2 + y^2 + z^2)^(1/2))^2/(x^2 + y^2 + z^2) + abs(c*f*z + c*tan(FOV/2)*cos(conj(alpha))*abs(f)^2*(x^2 + y^2 + z^2)^(1/2) + f*tan(FOV/2)*sin(conj(alpha))*abs(c)^2*(x^2 + y^2 + z^2)^(1/2))^2/(abs(c)^2*abs(f)^2*(x^2 + y^2 + z^2)))^(1/2)*(x^2 + y^2 + z^2)^(1/2)*(r^2 - x0^2 - y0^2 - z0^2 + (abs(c)^2*abs(f)^2*(c*f*x*x0*cos(FOV/2) + c*f*y*y0*cos(FOV/2) + c*f*z*z0*cos(FOV/2) + c*z0*sin(FOV/2)*cos(conj(alpha))*abs(f)^2*(x^2 + y^2 + z^2)^(1/2) + f*z0*sin(FOV/2)*sin(conj(alpha))*abs(c)^2*(x^2 + y^2 + z^2)^(1/2) + c*f*y0*sin(FOV/2)*sin(conj(alpha))*conj(b)*(x^2 + y^2 + z^2)^(1/2) + c*f*x0*sin(FOV/2)*cos(conj(alpha))*conj(d)*(x^2 + y^2 + z^2)^(1/2) + c*f*y0*sin(FOV/2)*cos(conj(alpha))*conj(e)*(x^2 + y^2 + z^2)^(1/2) + c*f*x0*sin(FOV/2)*sin(conj(alpha))*conj(a)*(x^2 + y^2 + z^2)^(1/2))^2)/(c^2*f^2*(abs(c*f*z*cos(FOV/2) + c*sin(FOV/2)*cos(conj(alpha))*abs(f)^2*(x^2 + y^2 + z^2)^(1/2) + f*sin(FOV/2)*sin(conj(alpha))*abs(c)^2*(x^2 + y^2 + z^2)^(1/2))^2 + abs(x*cos(FOV/2) + sin(FOV/2)*cos(conj(alpha))*conj(d)*(x^2 + y^2 + z^2)^(1/2) + sin(FOV/2)*sin(conj(alpha))*conj(a)*(x^2 + y^2 + z^2)^(1/2))^2*abs(c)^2*abs(f)^2 + abs(y*cos(FOV/2) + sin(FOV/2)*cos(conj(alpha))*conj(e)*(x^2 + y^2 + z^2)^(1/2) + sin(FOV/2)*sin(conj(alpha))*conj(b)*(x^2 + y^2 + z^2)^(1/2))^2*abs(c)^2*abs(f)^2)))^(1/2) + c*z0*tan(FOV/2)*cos(conj(alpha))*abs(f)^2*(x^2 + y^2 + z^2)^(1/2) + f*z0*tan(FOV/2)*sin(conj(alpha))*abs(c)^2*(x^2 + y^2 + z^2)^(1/2) + c*f*y0*tan(FOV/2)*sin(conj(alpha))*conj(b)*(x^2 + y^2 + z^2)^(1/2) + c*f*x0*tan(FOV/2)*cos(conj(alpha))*conj(d)*(x^2 + y^2 + z^2)^(1/2) + c*f*y0*tan(FOV/2)*cos(conj(alpha))*conj(e)*(x^2 + y^2 + z^2)^(1/2) + c*f*x0*tan(FOV/2)*sin(conj(alpha))*conj(a)*(x^2 + y^2 + z^2)^(1/2)))/(c*f*(abs(x + tan(FOV/2)*cos(conj(alpha))*conj(d)*(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*sin(conj(alpha))*conj(a)*(x^2 + y^2 + z^2)^(1/2))^2/(x^2 + y^2 + z^2) + abs(y + tan(FOV/2)*cos(conj(alpha))*conj(e)*(x^2 + y^2 + z^2)^(1/2) + tan(FOV/2)*sin(conj(alpha))*conj(b)*(x^2 + y^2 + z^2)^(1/2))^2/(x^2 + y^2 + z^2) + abs(c*f*z + c*tan(FOV/2)*cos(conj(alpha))*abs(f)^2*(x^2 + y^2 + z^2)^(1/2) + f*tan(FOV/2)*sin(conj(alpha))*abs(c)^2*(x^2 + y^2 + z^2)^(1/2))^2/(abs(c)^2*abs(f)^2*(x^2 + y^2 + z^2)))*(x^2 + y^2 + z^2)^(1/2));
n=n+1;
end
end
Patch=patch(XXX,YYY,ZZZ,1)

  4 Comments

Show 1 older comment
Stephen Cobeldick
Stephen Cobeldick on 29 Apr 2015
Please use the paperclip button to attach long code like this. You will find this button above the textbox. Note that using it requires pushing both the Choose file and Attach file buttons.
luc
luc on 29 Apr 2015
Okay, thanks for the advice :) I'll post the XXX YYY and ZZZ code then.
To answer pfd's questions: 1) Yes it is supposed to look like a dome
2) Patch draws a filled 2D polygon, but it draws the 2D polygon in 2D. If I would add enough points the shape will eventually look just like a dome. So I want to draw this complex 3D outer shell. out of multiple triangles.
Maybe a more general question would be; I have a set of points in X Y and Z, I want to use patch to draw a "terrain plot" using those points.
3) Thanks, I will.
luc
luc on 29 Apr 2015
Added the matrices, so now the question boils down to:
I want to create a dome shape from these points using the patch command, what would be the best way to start?
Data attached.
Patch(XXX,YYY,ZZZ,1)

Sign in to comment.

Accepted Answer

luc
luc on 1 May 2015
I figured it out, I use a loop to get some double points in my patch object. So each ring gets a double ring. I then add all the ring points.
Here's the code.
for FOV=0:0.1*pi:FOV
for alpha=0:0.1*pi:2*pi
XXX=code
YYY=code
ZZZ=code
n=n+1;
end
end
scatter3(XXX,YYY,ZZZ)
hold on
[nietnodig,sizefov]=size([0:0.1*pi:FOV]);
[nietnodig,sizealpha]=size(0:0.1*pi:2*pi);
m=1;
for m=1:sizefov-1
XXX_s=XXX(((m-1)*sizealpha)+1:(m+1)*sizealpha);
YYY_s=YYY(((m-1)*sizealpha)+1:(m+1)*sizealpha);
ZZZ_s=ZZZ(((m-1)*sizealpha)+1:(m+1)*sizealpha);
Patch_s=patch(XXX_s,YYY_s,ZZZ_s,1);
if m==1
fv_combined1=Patch_s
end
nv1=length(fv_combined1.Vertices);
fv_combined1.Vertices=[fv_combined1.Vertices;Patch_s.Vertices];
fv_combined1.Faces=[fv_combined1.Faces; Patch_s.Faces+nv1];
end

  0 Comments

Sign in to comment.

More Answers (1)

pfb
pfb on 29 Apr 2015
Not sure you can do that with the patch command "as it is".
You have to create some sort of mesh.
Something nicer is obtained with trisurf or trimesh.
tri = delaunay(XXX,YYY);
trisurf(tri,XXX,YYY,ZZZ);
It's not optimal yet, but I guess it is closer to what you actually want. I think it's also a matter of ordering of the points.

  1 Comment

luc
luc on 1 May 2015
Hmm.. any idea on how to order the points? I was thinking of doing a mesh with a few double points...

Sign in to comment.


Translated by