Circle plotting on different Planes

Hey,
I am trying to plot a series of 2D circle in a 3D plot, which are all on slightly different planes (orientated at different angles).
The information I have is;
Centers of the circles;
X = [2 4 5 7];
Y = [0 2 1 0];
Z = [1 3 5 7];
The radii for the circles are the same at 0.5 units.
When I plot these circles I want to see them orientated so that the normal from each circle is pointing in the direction of the next circle. In other words, the plane the circle is drawn on will be perpendicular to the line joining the centers.
the final picture would look like a small section of a pipe.
How could I achieve this?
All help is greatly appreciated.

 Réponse acceptée

Attila
Attila le 10 Sep 2013
Modifié(e) : Attila le 10 Sep 2013
Hi,
so your problem is more like math-related right?
If you already have the coordinates of the unoriented circle points, you should multiply them with the result of this, like
Rotated = Rx * Ry * Rz * original
(first circle with the first 3 matrices, etc):
clear all, close all, clc
X = [2 4 5 7];
Y = [0 2 1 0];
Z = [1 3 5 7];
r = 0.5
Coords = [X;Y;Z]
Coords_inc = Coords;
Coords_inc(:,3) = Coords(:,4) - Coords(:,3);
Coords_inc(:,2) = Coords(:,3) - Coords(:,2);
Coords_inc(:,1) = Coords(:,2) - Coords(:,1)
x = 1;
y = 2;
z = 3;
hold on
color = ['r' 'g' 'b' 'k'];
theta = -atan(Coords_inc(y,:)./Coords_inc(z,:)); % angle around x
phi = atan(Coords_inc(x,:)./Coords_inc(z,:));% angle 2 (y)
psi = atan(Coords_inc(y,:)./Coords_inc(x,:));% angle 3 (z)
for i=1:4
Rx = [1 0 0; 0 cos(theta(i)) -sin(theta(i)); 0 sin(theta(i)) cos(theta(i))]
Ry = [cos(phi(i)) 0 sin(phi(i)); 0 1 0; -sin(phi(i)) 0 cos(phi(i))]
Rz = [cos(psi(i)) -sin(psi(i)) 0; sin(psi(i)) cos(psi(i)) 0; 0 0 1]
t = 0:0.1:2*pi
x = cos(t)
y = sin(t)
z = zeros(1,length(t))
tol = [X(i); Y(i); Z(i)]
base = [x;y;z]
rotated = Rx*Ry*Rz*base
plot3(rotated(1,:)+X(i),rotated(2,:)+Y(i),rotated(3,:)+Z(i), color(i))
axis equal
end
This code snippet calculates the required orientations and then builds the required rotation matrices.

7 commentaires

Attila
Attila le 10 Sep 2013
Modifié(e) : Attila le 10 Sep 2013
Ok, it works now.
Mazhar
Mazhar le 10 Sep 2013
Ahh... this looks like the stuff!
Going to play around with it and get to understand it correctly.
Now for the radii of the circles not being a single value.
What could we do then?
for example I have this:
clear all
close all
clc
X = [2 4 5 7];
Y = [0 2 1 0];
Z = [1 3 5 7];
radii = [0.45 0.46 0.56 0.50 0.48 0.40 0.54 0.50 0.48 0.52;
0.46 0.48 0.50 0.51 0.48 0.45 0.40 0.45 0.52 0.42;
0.48 0.46 0.40 0.45 0.50 0.52 0.52 0.48 0.45 0.42;
0.45 0.46 0.56 0.50 0.48 0.40 0.54 0.50 0.48 0.52];
angel=(linspace(360/size(radii,2),360,size(radii,2)));
for k=1:size(radii,1)
raw_depth = radii(k,:);
x = ((raw_depth).*(cosd(angel)));
y = ((raw_depth).*(sind(angel)));
z = zeros(4,10);
x = x + X(k);
y = y + Y(k);
z = z + Z(k);
hold all
h = plot3(x,y,z,'-b');
end
grid on
How and where could we add in the rotate command to rotate these "circles"?
Attila
Attila le 10 Sep 2013
I'll look at the problem tomorrow afternoon, if I can find the time.
Mazhar
Mazhar le 11 Sep 2013
Please, and thanks again.
I'm almost there, just stuck on this last part.
Mazhar
Mazhar le 11 Sep 2013
Ok so I think I kind of have it...almost.
Just finding it hard to understand one part of your code.
The rotation matrices you used (Rx, Ry, Rz), in your code, you have changed the sign (-/+) on the sin, to that given in other online sources: http://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/3drota.htm
The graph produced by your code is the correct one, and if you replace the sign (-/+) on the sin, then the graph is not what I am looking for.
You have it correct, but I am wondering how you knew to change that?
Just want to understand your thinking behind it, so I can understand this better :D
Mazhar
Mazhar le 11 Sep 2013
Got it to work :D
Thanks for your help!

Connectez-vous pour commenter.

Plus de réponses (2)

Matt J
Matt J le 10 Sep 2013
You could start by plotting a prototype circle in the xy plane. Then roto-translate them in 3D using a transformation
R*points + t
where R is a 3x3 rotation matrix and t is a translation vector. This FEX file might help with that
Finally, use scatter3() to plot the transformed points.

4 commentaires

Mazhar
Mazhar le 10 Sep 2013
Iv'e been trying to read through the code and understand it.
How exactly will I be able to implement this in to my file?
I'm not sure I have all the inputs it requires.
Matt J
Matt J le 10 Sep 2013
Modifié(e) : Matt J le 10 Sep 2013
For example, in the following, I plot circles of radius 5 in a 45 degree tilted plane. Just adjust the transformation parameters to your liking.
r=5;
theta=linspace(0,2*pi,1000);
t=reshape(0:4,1,1,[]); %translations
xyz=[r*cos(theta);r*sin(theta); zeros(1,1000)]; %prototype circles
xyz=bsxfun(@plus,xyz,t);
xyz=reshape(xyz,3,[]);
XYZ=num2cell(AxelRot(xyz,45,[1,0,0],[]),2); %transformed circles
scatter3(XYZ{:});
Mazhar
Mazhar le 10 Sep 2013
OK yeah, I see how it works now.
Is it possible for to create a code that will calculate the angel (rotation) required from the center points data??
Mazhar
Mazhar le 10 Sep 2013
Also, If my circle does not have a set radius, but I have several radii data for each point of the data. How can I add in a loop to be able to plot such a circle (or shape, as it might not be completely circular)

Connectez-vous pour commenter.

Grzegorz Knor
Grzegorz Knor le 10 Sep 2013
Modifié(e) : Grzegorz Knor le 10 Sep 2013
Try this code:
X = [2 4 5 7];
Y = [0 2 1 0];
Z = [1 3 5 7];
r = 0.5;
[x,y,z] = cylinder(r*ones(size(X)),100);
hold on
for k=length(X):-1:1
x(k,:) = x(k,:)+X(k);
y(k,:) = y(k,:)+Y(k);
z(k,:) = z(k,:)+Z(k);
h(k) = plot3(x(k,:),y(k,:),z(k,:),'r-');
direction = rand(1,3);
alpha = randi(90);
rotate(h(k),direction,alpha)
end
hold off
view(3)
axis equal
I've used random rotation directions and angles. Just calculate the proper values and replace in the code above.

3 commentaires

Mazhar
Mazhar le 10 Sep 2013
Its the calculating the direction and rotation that I am struggling to achieve.
How exactly does your code work? It looks pretty close.
If you add the center line to the end of that;
plot3(X,Y,Z,'-k')
you are almost there.
Are you able to do this without random rotation, but the correct amount to have it sit correctly on the center line?
First of all there is a small mistake, should be:
z(k,:) = Z(k);
instead of:
z(k,:) = z(k,:)+Z(k);
Each circle should be perpendicular to the line connecting its center point with the center point of the next circle, right? And what about last circle and its orientation?
Mazhar
Mazhar le 10 Sep 2013
Almost, just the other way round.
The first circle should be horizontal (flat) and the corresponding circle should be perpendicular to the line joining its center and the one before.

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by