Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
find distance along complicated geometric surface

Subject: find distance along complicated geometric surface

From: Meagan

Date: 17 May, 2010 20:45:22

Message: 1 of 27

hi all,

I am working on a project and I have hit a wall and could really use some help.

I am trying to find the distance between two points on a shape that is defined by vertices and faces. I can't just use the distance formula because I need to make sure that the entire distance between the two points is in contact with the shape surface. I have no idea how to start doing this or if this task is even possible so any input would be greatly appreciated.

Thanks,
Meagan

Subject: find distance along complicated geometric surface

From: Walter Roberson

Date: 17 May, 2010 21:00:41

Message: 2 of 27

Meagan wrote:

> I am trying to find the distance between two points on a shape that is
> defined by vertices and faces. I can't just use the distance formula
> because I need to make sure that the entire distance between the two
> points is in contact with the shape surface. I have no idea how to start
> doing this or if this task is even possible so any input would be
> greatly appreciated.

Sounds tricky.

To cross-check: is it permitted for the line to cross a face? For example, if
you had two triangles joined at the base, then the shortest distance between
the far corners would probably go over the faces.

Also, is the shape convex or could it be concave? Concave would, I think, be a
much harder problem.

Subject: find distance along complicated geometric surface

From: Walter Roberson

Date: 17 May, 2010 21:16:31

Message: 3 of 27

Walter Roberson wrote:
> Meagan wrote:
>
>> I am trying to find the distance between two points on a shape that is
>> defined by vertices and faces. I can't just use the distance formula
>> because I need to make sure that the entire distance between the two
>> points is in contact with the shape surface. I have no idea how to
>> start doing this or if this task is even possible so any input would
>> be greatly appreciated.
>
> Sounds tricky.
>
> To cross-check

Another question to cross-check: is this a 2D or 3D problem (or more?)

When you speak of "distance" and needing contact with the shape surface, would
I be correct in figuring that what you want is the "path length" ? Just to be
sure we have disambiguited "distance" correctly.

For example, if we had a discretized helix, then the distance between two
points would be the same no matter how tightly or loosely the helix was coiled
(provided that the coils did not touch) ?

Subject: find distance along complicated geometric surface

From: Meagan

Date: 17 May, 2010 21:30:22

Message: 4 of 27

Walter Roberson <roberson@hushmail.com> wrote in message <hssboe$avg$1@canopus.cc.umanitoba.ca>...
> Walter Roberson wrote:
> > Meagan wrote:
> >
> >> I am trying to find the distance between two points on a shape that is
> >> defined by vertices and faces. I can't just use the distance formula
> >> because I need to make sure that the entire distance between the two
> >> points is in contact with the shape surface. I have no idea how to
> >> start doing this or if this task is even possible so any input would
> >> be greatly appreciated.
> >
> > Sounds tricky.
> >
> > To cross-check
>
> Another question to cross-check: is this a 2D or 3D problem (or more?)
>
> When you speak of "distance" and needing contact with the shape surface, would
> I be correct in figuring that what you want is the "path length" ? Just to be
> sure we have disambiguited "distance" correctly.
>
> For example, if we had a discretized helix, then the distance between two
> points would be the same no matter how tightly or loosely the helix was coiled
> (provided that the coils did not touch) ?


Thank you for clarifying. I had a feeling that I did not include enough detail in the original message. To answer your questions,

-the line is allowed to cross a face
-the shape in convex
-it is a 3D problem
-it is correct to say that I am looking for the "path length" as in your example of a helix.

Thank you so much for your help,
Meagan

Subject: find distance along complicated geometric surface

From: Matt J

Date: 17 May, 2010 21:48:03

Message: 5 of 27

"Meagan " <meagan.musselman@l-3com.com> wrote in message <hssche$2o9$1@fred.mathworks.com>...

>
> -the line is allowed to cross a face
> -the shape in convex
> -it is a 3D problem
> -it is correct to say that I am looking for the "path length" as in your example of a helix.
==============

I'm going to hypothesize that the shortest distance across a 3D convex polyhedral surface is a piecewise linear path confined to a single plane (don't know how to prove it off the top of my head, though).

Under this hypothesis, a helpful tool for this problem might be the vert2con function in the FEX:

http://www.mathworks.com/matlabcentral/fileexchange/7895-vert2con-vertices-to-constraints

It will allow you to input the vertices and obtain the representation of the polyhedron in the form

A*x<=b

The intersection of the polyhedron with a plane passing through your 2 given surface points is a 2D polygon. Using the above inequalities, you can find this 2D polygon and represent it in the x-y plane. Then, you can use this tool

http://www.mathworks.com/matlabcentral/fileexchange/7894-con2vert-constraints-to-vertices

to find the vertices of the 2D polygon and trace the distance between the successive vertices.

The above will give you total distance between the two points for one particular planar cut through the 2 given surface points (and the polyhedron's surface). Using fminsearch, you can search over all such cuts for the minimum distance.

There are a lot of details to fill in here, but at least this might be a direction to go...

Subject: find distance along complicated geometric surface

From: Walter Roberson

Date: 17 May, 2010 22:09:19

Message: 6 of 27

Meagan wrote:
> To answer your questions,
> -the line is allowed to cross a face
> -the shape in convex
> -it is a 3D problem
> -it is correct to say that I am looking for the "path length" as in your
> example of a helix.

Not so easy. Perhaps we should try a simple example and see if we can come up
with a strategy.

Let us take a 2 x 2 rectangle with a slit from the middle to the right side,
and let us lift up the upper-right 1 x 1 flap to an angle of 45 degrees
relative to the plane, and let us calculate the distance between the two right
hand corners.

In this example, because of the discontinuity not providing a valid path, the
answer would clearly be to go from the top right corner to the center and
there to the bottom right corner, for a total path length of 2 * sqrt(2).

Now, let us fasten join two edges of the slight with some stretchable elastic,
so as to provide a triangular surface between the two halves. If my mental
imaging is correct, there is a path that goes along the right side of the top
flap, then drops a perpendicular to the top of the bottom flap, then goes
diagonally over to the bottom right corner, and I believe that path length
would be 1 + 1/sqrt(2) + sqrt(3 - 2*sqrt(2)), which would be 3/sqrt(2), and
thus would be less than the 2 * sqrt(2) for the other path. Is there a shorter
route? If not, how do we prove that?

Let us take this further by taking that elastic joining the two halves of the
slit, and stretching the center of it up or down. possibly to a peak above or
valley below. As the distance along that perpendicular increases, it might be
worth heading some amount further to the left to avoid descending the valley
or climbing the peak, until eventually we get end up with the shortest
distance again being to go to the middle and back -- but how do we optimize
the path length?

I believe that this example is solvable in theory (and practice), but it would
take me some more thinking to set up the path length equations.

Subject: find distance along complicated geometric surface

From: Walter Roberson

Date: 17 May, 2010 22:21:29

Message: 7 of 27

Walter Roberson wrote:
> Meagan wrote:
>> To answer your questions,
>> -the line is allowed to cross a face
>> -the shape in convex
>> -it is a 3D problem
>> -it is correct to say that I am looking for the "path length" as in
>> your example of a helix.
>
> Not so easy. Perhaps we should try a simple example and see if we can
> come up with a strategy.

After all of that, I re-read and realized I had confused "concave" and
"convex". The example I gave is concave.

For convex... ummm...

Suppose you have a square pyramid and the two points are opposite corners of
the square. If the pyramid is 0 height then the shortest path would go
directly over the center to the opposite corner, and if the pyramid is
infinitely high (and thus has infinite slope) then the shortest path is going
to go along the two edges. There is plausibly a maximum slope that makes it
worth traveling at least part-way over the surface, but at first glance it is
also plausible that for any finite slope the shortest path might go over the
surface at least a little bit; I would have to explore that further. Clearly,
though, as the center rises, the path would get pushed further and further
towards following the edges. I haven't figured out yet what the balance is.

Subject: find distance along complicated geometric surface

From: ImageAnalyst

Date: 17 May, 2010 22:23:39

Message: 8 of 27

Couldn't you use dynamic programming? I used that once to track blood
vessels but it's a pretty general concept that I think should be able
to find the shortest distance numerically.
http://en.wikipedia.org/wiki/Dynamic_programming

Subject: find distance along complicated geometric surface

From: Matt J

Date: 17 May, 2010 22:31:05

Message: 9 of 27

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <9cf53832-31d4-4c7e-b5a2-4994719d03a4@u7g2000vbq.googlegroups.com>...
> Couldn't you use dynamic programming? I used that once to track blood
> vessels but it's a pretty general concept that I think should be able
> to find the shortest distance numerically.
> http://en.wikipedia.org/wiki/Dynamic_programming
=================

Perhaps if the linear pieces of the path were requireded to run between vertices of the polyhedron. Otherwise, I don't see what sort of backward induction rule you could set up.

Subject: find distance along complicated geometric surface

From: Roger Stafford

Date: 17 May, 2010 22:38:04

Message: 10 of 27

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hssdij$7pp$1@fred.mathworks.com>...
> "Meagan " <meagan.musselman@l-3com.com> wrote in message <hssche$2o9$1@fred.mathworks.com>...
>
> >
> > -the line is allowed to cross a face
> > -the shape in convex
> > -it is a 3D problem
> > -it is correct to say that I am looking for the "path length" as in your example of a helix.
> ==============
>
> I'm going to hypothesize that the shortest distance across a 3D convex polyhedral surface is a piecewise linear path confined to a single plane (don't know how to prove it off the top of my head, though).
>
> Under this hypothesis, a helpful tool for this problem might be the vert2con function in the FEX:
>
> http://www.mathworks.com/matlabcentral/fileexchange/7895-vert2con-vertices-to-constraints
>
> It will allow you to input the vertices and obtain the representation of the polyhedron in the form
>
> A*x<=b
>
> The intersection of the polyhedron with a plane passing through your 2 given surface points is a 2D polygon. Using the above inequalities, you can find this 2D polygon and represent it in the x-y plane. Then, you can use this tool
>
> http://www.mathworks.com/matlabcentral/fileexchange/7894-con2vert-constraints-to-vertices
>
> to find the vertices of the 2D polygon and trace the distance between the successive vertices.
>
> The above will give you total distance between the two points for one particular planar cut through the 2 given surface points (and the polyhedron's surface). Using fminsearch, you can search over all such cuts for the minimum distance.
>
> There are a lot of details to fill in here, but at least this might be a direction to go...

  Matt, geodesics, even over surfaces consisting of plane faces, do not have to follow a planar path. They can wander back and forth in complicated ways in seeking a shortest path. For a smooth surface a geodesic curve must always have the curve's normal parallel to the surface's normal, and as the surface normal tilts to the right or left along the path, the curve must corresponingly adjust its own normal to match. (A curve's normal is defined as being in the direction of the change of the tangent unit vector - in other words it must lie in the best-fitting plane to the curve at a given point and be orthogonal to the tangent vector.)

Roger Stafford

Subject: find distance along complicated geometric surface

From: Matt J

Date: 17 May, 2010 23:05:06

Message: 11 of 27

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hssggc$gr8$1@fred.mathworks.com>...


> Matt, geodesics, even over surfaces consisting of plane faces, do not have to follow a planar path.
=========

OK. Well, clearly, they must follow a linear path across each face. I'm starting to wonder if this couldn't be used to set up a dynamic programming formulation, along the lines of what ImageAnalyst was proposing... However, it still seems like it would lead to a brutal continuous state space dynamic program.

Subject: find distance along complicated geometric surface

From: ImageAnalyst

Date: 18 May, 2010 01:00:53

Message: 12 of 27

On May 17, 6:31 pm, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> Perhaps if the linear pieces of the path were required to run between vertices of the polyhedron. Otherwise, I don't see what sort of backward induction rule you could set up.
--------------------------------------------------------------
Well, you're right Matt. I was thinking that if she needed to find
the distance along the faces/facets that she'd just resample it fine
enough to create extra vertices so that it could traverse the face.
For example, it might be shorter to travel AROUND the mountain than
across it's peak but if all you had was the peak and the base
locations, you couldn't go around, but if you interpolated more
locations in between (which would be on the sides of the mountain),
then you could go around.

And, by the way, I realize it would be a numerical approximation, not
an exact analytical one but I was assuming that she didn't really have
an analytical function to begin with anyway - that it was starting out
as a numerical array (which may or may not be a sampling of an
analytical function but that's immaterial).

Subject: find distance along complicated geometric surface

From: Walter Roberson

Date: 18 May, 2010 04:44:14

Message: 13 of 27

Matt J wrote:
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
> message <hssggc$gr8$1@fred.mathworks.com>...

>> Matt, geodesics, even over surfaces consisting of plane faces, do
>> not have to follow a planar path.

> OK. Well, clearly, they must follow a linear path across each face. I'm
> starting to wonder if this couldn't be used to set up a dynamic
> programming formulation, along the lines of what ImageAnalyst was
> proposing... However, it still seems like it would lead to a brutal
> continuous state space dynamic program.

I am not _certain_ that it would lead to a continuous state space
dynamic program. When you get down to a simple enough geometry, I think
you should be able to solve the problem absolutely (except for
symmetries), and it seems to me that the assumption of convex shape
should allow one to reason about which faces and edges could be involved
in the shortest path.

If the problem involved con*cave* objects, then you would run into
situations where you headed "down" and have to switch to "up" (or vice
versa), which could lead you to taking zig-zags around the outskirts of
a face to avoid going too far up or down on the face. That cannot happen
on a con*vex* object: once you are on a face, all the other faces
attached to it curl "down" away from that plane.

I think it would help if we had an example from the original poster to
look at.

Subject: find distance along complicated geometric surface

From: Bruno Luong

Date: 18 May, 2010 06:11:06

Message: 14 of 27

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hssggc$gr8$1@fred.mathworks.com>...

>
> Matt, geodesics, even over surfaces consisting of plane faces, do not have to follow a planar path. They can wander back and forth in complicated ways in seeking a shortest path.

Agree with Roger.

The geodesic on polytope is a straight line when the polytope is flattenen (making a cuts on the cell edges, excepted those crossed by the geoderic, then change the dihedral angle between to neighboring faces to pi, i.e., 180 degree). As we disccus in earlier thread, given a local azimuth direction from the start point, is possible to follow the geodesic from cell to cell by the above characteristic. The problem become 1D solve: find the azimuth that leads to the geodesic crossing the end point. Easy to tell the theory than to code it.

Bruno

Subject: find distance along complicated geometric surface

From: Roger Stafford

Date: 18 May, 2010 09:52:03

Message: 15 of 27

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hstb1q$fl7$1@fred.mathworks.com>...
> Agree with Roger.
>
> The geodesic on polytope is a straight line when the polytope is flattenen (making a cuts on the cell edges, excepted those crossed by the geoderic, then change the dihedral angle between to neighboring faces to pi, i.e., 180 degree). As we disccus in earlier thread, given a local azimuth direction from the start point, is possible to follow the geodesic from cell to cell by the above characteristic. The problem become 1D solve: find the azimuth that leads to the geodesic crossing the end point. Easy to tell the theory than to code it.
>
> Bruno

  Following those geodesics from various azimuths is not as one-dimnsional an affair as one would like it to be. For example, if two geodesics proceeding as you correctly describe, Bruno, and in nearly parallel paths, go short distances on opposite sides of a vertex joining three facets, then after they pass that vertex their paths may cross each other at very sizable angles on the other side. This means that choosing an azimuth for one geodesic just to the left of another geodesic is no guarantee that that geodesic will remain to the left. It can go far to the right instead. Therefore there can remain still much of a combinatorial nature about seeking a shortest path across a surface composed of numerous plane facets and therefore exceedingly difficult to determine.

Roger Stafford

Subject: find distance along complicated geometric surface

From: Bruno Luong

Date: 18 May, 2010 10:20:06

Message: 16 of 27

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hsto03$f28$1@fred.mathworks.com>...

>
> Following those geodesics from various azimuths is not as one-dimnsional an affair as one would like it to be. For example, if two geodesics proceeding as you correctly describe, Bruno, and in nearly parallel paths, go short distances on opposite sides of a vertex joining three facets, then after they pass that vertex their paths may cross each other at very sizable angles on the other side. This means that choosing an azimuth for one geodesic just to the left of another geodesic is no guarantee that that geodesic will remain to the left. It can go far to the right instead. Therefore there can remain still much of a combinatorial nature about seeking a shortest path across a surface composed of numerous plane facets and therefore exceedingly difficult to determine.

You are right Roger, but I believe for the surface with positive curvature (convex polytope in particular in the case here), the geodesic ode is locally Lipschitz and stable. We must also decide to stop the search when the length of the geodesic get too long to avoid wrap around. There is no explosion or bifurcation of the solution. That makes the problem somewhat nicer.

However I also think minimizing the path length for all the paths that go through 2 points is an even better approach. It must be fun to code such thing, unfortunately I don't have spare time to do so.

Bruno

Subject: find distance along complicated geometric surface

From: Meagan

Date: 18 May, 2010 14:04:04

Message: 17 of 27

I have posted my code below. It is very long and not cleaned up at all...sorry. When the code is run, you should get an airplane. Now I have my shape and would like to find the distance from say one wing to the nose of the plane but the path needs to stay on the surface of the plane. The shape can change based on the input array p1 thru p7 and the other problem is that when the airplane is made, most of the shapes intersect so a lot of the vertices for a particular component are not necessarily on the outside of the plane. I am not required to make my shape this way but eventually, I will have to convert my code to C so the functions must be embedded matlab compatible. Any suggestions or comments are greatly appreciated. Thanks,
Meagan



p1=[1 1 3 1 1 5 0 90 0 0 0 1.01];
p2=[2 2 1 3 1 1 0 0 0 -4.5 0 1.01];
p3=[3 2 1 1 10 0.2 0 0 0 -2 0 1];
p4=[4 2 1 1.5 0.5 0.5 0 0 0 -2 3 1];
p5=[5 2 1 2 1 1 0 0 0 4.5 0 1.01];
p6=[6 2 1 1.5 0.5 0.5 0 0 0 -2 -3 1];
p7=[7 2 1 1 .2 2 0 30 0 5 0 2];

cube.vertices=[ 0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1;] ;
cube.faces=[ 1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8; ] ;
cube.vertices = cube.vertices * 2 - 1;

cylinder.vertices=[-1,0,-1;-1,0,0;-0.500000000000000,-0.856380903262898,-1;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,-1;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,-1;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,-1;-0.500000000000000,0.856380903262898,0;0,-1,-1;0,-1,0;0,1,-1;0,1,0;0.500000000000000,-0.856380903262898,-1;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,-1;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,-1;0.856380903262898,-0.500000000000000,0;1,0,-1;1,0,0;0.856380903262898,0.500000000000000,-1;0.856380903262898,0.500000000000000,0;-1,0,1;-0.500000000000000,-0.856380903262898,1;-0.856380903262898,-0.500000000000000,1;-0.856380903262898,0.500000000000000,1;-0.500000000000000,0.856380903262898,1;0,-1,1;0,1,1;0.5000000000000
00,-0.856380903262898,1;0.500000000000000,0.856380903262898,1;0.856380903262898,-0.500000000000000,1;1,0,1;0.856380903262898,0.500000000000000,1;];
cylinder.faces=[3,4,5;5,4,6;5,6,1;1,6,2;1,2,7;7,2,8;7,8,9;9,8,10;11,12,3;3,12,4;9,10,13;13,10,14;11,15,12;12,15,16;17,13,18;18,13,14;15,19,16;16,19,20;19,21,20;20,21,22;21,23,22;22,23,24;23,17,24;24,17,18;4,26,6;6,26,27;6,27,2;2,27,25;2,25,8;8,25,28;8,28,10;10,28,29;12,30,4;4,30,26;10,29,14;14,29,31;12,16,30;30,16,32;18,14,33;33,14,31;16,20,32;32,20,34;20,22,34;34,22,35;22,24,35;35,24,36;24,18,36;36,18,33;];

sphere.vertices=[0,0,-1;-0.500000000000000,-0.500000000000000,-0.686740068930804;-0.500000000000000,-0.686740068930804,-0.500000000000000;-0.686740068930804,-0.500000000000000,-0.500000000000000;-0.500000000000000,0,-0.856380903262898;-0.856380903262898,0,-0.500000000000000;-0.500000000000000,0.500000000000000,-0.686740068930804;-0.686740068930804,0.500000000000000,-0.500000000000000;-0.500000000000000,0.686740068930805,-0.500000000000000;0,-0.500000000000000,-0.856380903262898;0,-0.856380903262898,-0.500000000000000;0,0.500000000000000,-0.856380903262898;0,0.856380903262898,-0.500000000000000;0.500000000000000,-0.500000000000000,-0.686740068930804;0.500000000000000,-0.686740068930804,-0.500000000000000;0.500000000000000,0,-0.856380903262898;0.500000000000000,0.500000000000000,-0.686740068930804;0.500000000000000,0.686740068930805,-0.500000000000000;0.686740068930805,-0.500000000000000,-
0.500000000000000;0.856380903262898,0,-0.500000000000000;0.686740068930805,0.500000000000000,-0.500000000000000;-1,0,0;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,0;0,-1,0;0,1,0;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,0;1,0,0;0.856380903262898,0.500000000000000,0;-0.500000000000000,-0.686740068930804,0.500000000000000;-0.686740068930804,-0.500000000000000,0.500000000000000;-0.856380903262898,0,0.500000000000000;-0.686740068930804,0.500000000000000,0.500000000000000;-0.500000000000000,0.686740068930805,0.500000000000000;0,-0.856380903262898,0.500000000000000;0,0.856380903262898,0.500000000000000;0.500000000000000,-0.686740068930804,0.500000000000000;0.500000000000000,0.686740068930805,0.500000000000000;0.6
86740068930805,-0.500000000000000,0.500000000000000;0.856380903262898,0,0.500000000000000;0.686740068930805,0.500000000000000,0.500000000000000;-0.500000000000000,-0.500000000000000,0.686740068930805;-0.500000000000000,0,0.856380903262898;-0.500000000000000,0.500000000000000,0.686740068930805;0,-0.500000000000000,0.856380903262898;0,0,1;0,0.500000000000000,0.856380903262898;0.500000000000000,-0.500000000000000,0.686740068930805;0.500000000000000,0,0.856380903262898;0.500000000000000,0.500000000000000,0.686740068930805;];
sphere.faces=[2,3,4;2,4,5;5,4,6;5,6,7;7,6,8;7,8,9;2,10,3;3,10,11;1,10,2;1,2,5;1,5,7;1,7,12;7,9,12;12,9,13;10,14,11;11,14,15;10,1,16;10,16,14;16,1,12;16,12,17;12,13,17;17,13,18;14,19,15;14,16,19;19,16,20;16,17,20;20,17,21;17,18,21;3,23,4;4,23,24;4,24,6;6,24,22;6,22,8;8,22,25;8,25,9;9,25,26;11,27,3;3,27,23;13,9,28;28,9,26;15,29,11;11,29,27;18,13,30;30,13,28;15,19,29;29,19,31;19,20,31;31,20,32;20,21,32;32,21,33;21,18,33;33,18,30;23,34,24;24,34,35;24,35,22;22,35,36;25,22,36;25,36,37;25,37,26;26,37,38;23,27,39;23,39,34;26,38,28;28,38,40;27,29,41;27,41,39;30,28,42;42,28,40;29,31,41;41,31,43;31,32,43;43,32,44;32,33,45;32,45,44;33,30,45;45,30,42;34,46,35;35,46,36;36,46,47;36,47,37;37,47,48;37,48,38;39,49,34;34,49,46;46,49,47;47,49,50;47,50,48;48,50,51;40,38,51;51,38,48;41,52,39;39,52,49;49,52,50;50,52,53;53,54,50;50,54,51;42,40,54;54,40,51;41,43,52;43,44,52;52,44,53;44,45,53;53,45,54;45,42,54;];

if (p1(2)==1) %part ID 1=UnitCylinder
    res=p1(3); %resolution
    scalex=p1(4); %diameter of cylinder in x direction
    scaley=p1(5); %diameter of cylinder in y direction
    scalez=p1(6); %length of cylinder
    rotx=p1(7); %rotation about x axis
    roty=p1(8); %rotation about y axis
    rotz=p1(9); %rotation about z axis
    transx=p1(10); %translation in x direction
    transy=p1(11); %translation in y direction
    transz=p1(12); %translation in z direction
    
    
   V1=cylinder.vertices;
   V1=[V1(:,1)*scalex, V1(:,2)*scaley, V1(:,3)*scalez];
   cylinder.vertices=V1;

   a=rotz/57.29; %degrees to radians
   V1=cylinder.vertices;
   V1=[cos(a)*V1(:,1)-sin(a)*V1(:,2),sin(a)*V1(:,1)+cos(a)*V1(:,2),V1(:,3)];
   cylinder.vertices=V1;

   a=roty/57.29; %degrees to radians
   V1=cylinder.vertices;
   V1=[cos(a)*V1(:,1)+sin(a)*V1(:,3), V1(:,2), -sin(a)*V1(:,1)+cos(a)*V1(:,3)];
   cylinder.vertices=V1;
  
   a=rotx/57.29; %degrees to radians
   V1=cylinder.vertices;
   V1=[V1(:,1), cos(a)*V1(:,2)-sin(a)*V1(:,3), sin(a)*V1(:,2)+cos(a)*V1(:,3)];
   cylinder.vertices=V1;

   V1=cylinder.vertices;
   V1=[V1(:,1)+transx, V1(:,2)+transy, V1(:,3)+transz];
   cylinder.vertices=V1;
   
   part1=cylinder;
    
elseif (p1(2)==2) %part ID 2=UnitSphere
    res=p1(3); %resolution
    scalex=p1(4); %diameter of sphere in x direction
    scaley=p1(5); %diameter of sphere in y direction
    scalez=p1(6); %diameter of sphere in z direction
    rotx=p1(7); %rotation about x axis
    roty=p1(8); %rotation about y axis
    rotz=p1(9); %rotation about z axis
    transx=p1(10); %translation in x direction
    transy=p1(11); %translation in y direction
    transz=p1(12); %translation in z direction
    
    
   M1=sphere.vertices;
   M1=[M1(:,1)*scalex, M1(:,2)*scaley, M1(:,3)*scalez];
   sphere.vertices=M1;

   a=rotz/57.29; %degrees to radians
   M1=sphere.vertices;
   M1=[cos(a)*M1(:,1)-sin(a)*M1(:,2),sin(a)*M1(:,1)+cos(a)*M1(:,2), M1(:,3)];
   sphere.vertices=M1;

    a=roty/57.29; %degrees to radians
    M1=sphere.vertices;
    M1=[cos(a)*M1(:,1)+sin(a)*M1(:,3), M1(:,2), -sin(a)*M1(:,1)+cos(a)*M1(:,3)];
    sphere.vertices=M1;
  
    a=rotx/57.29; %degrees to radians
    M1=sphere.vertices;
    M1=[M1(:,1), cos(a)*M1(:,2)-sin(a)*M1(:,3), sin(a)*M1(:,2)+cos(a)*M1(:,3)];
    sphere.vertices=M1;

    M1=sphere.vertices;
    M1=[M1(:,1)+transx, M1(:,2)+transy, M1(:,3)+transz];
    sphere.vertices=M1;
    
    part1=sphere;

elseif (p1(2)==3) %part ID 3=UnitCube
    res=p1(3); %resolution
    scalex=p1(4); %length in x direction
    scaley=p1(5); %length in y direction
    scalez=p1(6); %length in z direction
    rotx=p1(7); %rotation about x axis
    roty=p1(8); %rotation about y axis
    rotz=p1(9); %rotation about z axis
    transx=p1(10); %translation in x direction
    transy=p1(11); %translation in y direction
    transz=p1(12); %translation in z direction
  
    F1=cube.vertices;
    F1=[F1(:,1)*scalex, F1(:,2)*scaley, F1(:,3)*scalez];
    cube.vertices=F1;

    a=rotz/57.29; %degrees to radians
    F1=cube.vertices;
    F1=[cos(a)*F1(:,1)-sin(a)*F1(:,2), sin(a)*F1(:,1)+cos(a)*F1(:,2), F1(:,3)];
    cube.vertices=F1;

    a=roty/57.29; %degrees to radians
    F1=cube.vertices;
    F1=[cos(a)*F1(:,1)+sin(a)*F1(:,3),F1(:,2), -sin(a)*F1(:,1)+cos(a)*F1(:,3)];
    cube.vertices=F1;
  
    a=rotx/57.29; %degrees to radians
    F1=cube.vertices;
    F1=[F(:,1),cos(a)*F1(:,2)-sin(a)*F1(:,3), sin(a)*F1(:,2)+cos(a)*F1(:,3)];
    cube.vertices=F1;

    F1=cube.vertices;
    F1=[F1(:,1)+transx, F1(:,2)+transy, F1(:,3)+transz];
    cube.vertices=F1;
  
    part1=cube;

else
    p1(2)=0;
 
end

cube.vertices=[ 0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1;] ;
cube.faces=[ 1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8; ] ;
cube.vertices = cube.vertices * 2 - 1;

cylinder.vertices=[-1,0,-1;-1,0,0;-0.500000000000000,-0.856380903262898,-1;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,-1;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,-1;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,-1;-0.500000000000000,0.856380903262898,0;0,-1,-1;0,-1,0;0,1,-1;0,1,0;0.500000000000000,-0.856380903262898,-1;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,-1;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,-1;0.856380903262898,-0.500000000000000,0;1,0,-1;1,0,0;0.856380903262898,0.500000000000000,-1;0.856380903262898,0.500000000000000,0;-1,0,1;-0.500000000000000,-0.856380903262898,1;-0.856380903262898,-0.500000000000000,1;-0.856380903262898,0.500000000000000,1;-0.500000000000000,0.856380903262898,1;0,-1,1;0,1,1;0.5000000000000
00,-0.856380903262898,1;0.500000000000000,0.856380903262898,1;0.856380903262898,-0.500000000000000,1;1,0,1;0.856380903262898,0.500000000000000,1;];
cylinder.faces=[3,4,5;5,4,6;5,6,1;1,6,2;1,2,7;7,2,8;7,8,9;9,8,10;11,12,3;3,12,4;9,10,13;13,10,14;11,15,12;12,15,16;17,13,18;18,13,14;15,19,16;16,19,20;19,21,20;20,21,22;21,23,22;22,23,24;23,17,24;24,17,18;4,26,6;6,26,27;6,27,2;2,27,25;2,25,8;8,25,28;8,28,10;10,28,29;12,30,4;4,30,26;10,29,14;14,29,31;12,16,30;30,16,32;18,14,33;33,14,31;16,20,32;32,20,34;20,22,34;34,22,35;22,24,35;35,24,36;24,18,36;36,18,33;];

sphere.vertices=[0,0,-1;-0.500000000000000,-0.500000000000000,-0.686740068930804;-0.500000000000000,-0.686740068930804,-0.500000000000000;-0.686740068930804,-0.500000000000000,-0.500000000000000;-0.500000000000000,0,-0.856380903262898;-0.856380903262898,0,-0.500000000000000;-0.500000000000000,0.500000000000000,-0.686740068930804;-0.686740068930804,0.500000000000000,-0.500000000000000;-0.500000000000000,0.686740068930805,-0.500000000000000;0,-0.500000000000000,-0.856380903262898;0,-0.856380903262898,-0.500000000000000;0,0.500000000000000,-0.856380903262898;0,0.856380903262898,-0.500000000000000;0.500000000000000,-0.500000000000000,-0.686740068930804;0.500000000000000,-0.686740068930804,-0.500000000000000;0.500000000000000,0,-0.856380903262898;0.500000000000000,0.500000000000000,-0.686740068930804;0.500000000000000,0.686740068930805,-0.500000000000000;0.686740068930805,-0.500000000000000,-
0.500000000000000;0.856380903262898,0,-0.500000000000000;0.686740068930805,0.500000000000000,-0.500000000000000;-1,0,0;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,0;0,-1,0;0,1,0;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,0;1,0,0;0.856380903262898,0.500000000000000,0;-0.500000000000000,-0.686740068930804,0.500000000000000;-0.686740068930804,-0.500000000000000,0.500000000000000;-0.856380903262898,0,0.500000000000000;-0.686740068930804,0.500000000000000,0.500000000000000;-0.500000000000000,0.686740068930805,0.500000000000000;0,-0.856380903262898,0.500000000000000;0,0.856380903262898,0.500000000000000;0.500000000000000,-0.686740068930804,0.500000000000000;0.500000000000000,0.686740068930805,0.500000000000000;0.6
86740068930805,-0.500000000000000,0.500000000000000;0.856380903262898,0,0.500000000000000;0.686740068930805,0.500000000000000,0.500000000000000;-0.500000000000000,-0.500000000000000,0.686740068930805;-0.500000000000000,0,0.856380903262898;-0.500000000000000,0.500000000000000,0.686740068930805;0,-0.500000000000000,0.856380903262898;0,0,1;0,0.500000000000000,0.856380903262898;0.500000000000000,-0.500000000000000,0.686740068930805;0.500000000000000,0,0.856380903262898;0.500000000000000,0.500000000000000,0.686740068930805;];
sphere.faces=[2,3,4;2,4,5;5,4,6;5,6,7;7,6,8;7,8,9;2,10,3;3,10,11;1,10,2;1,2,5;1,5,7;1,7,12;7,9,12;12,9,13;10,14,11;11,14,15;10,1,16;10,16,14;16,1,12;16,12,17;12,13,17;17,13,18;14,19,15;14,16,19;19,16,20;16,17,20;20,17,21;17,18,21;3,23,4;4,23,24;4,24,6;6,24,22;6,22,8;8,22,25;8,25,9;9,25,26;11,27,3;3,27,23;13,9,28;28,9,26;15,29,11;11,29,27;18,13,30;30,13,28;15,19,29;29,19,31;19,20,31;31,20,32;20,21,32;32,21,33;21,18,33;33,18,30;23,34,24;24,34,35;24,35,22;22,35,36;25,22,36;25,36,37;25,37,26;26,37,38;23,27,39;23,39,34;26,38,28;28,38,40;27,29,41;27,41,39;30,28,42;42,28,40;29,31,41;41,31,43;31,32,43;43,32,44;32,33,45;32,45,44;33,30,45;45,30,42;34,46,35;35,46,36;36,46,47;36,47,37;37,47,48;37,48,38;39,49,34;34,49,46;46,49,47;47,49,50;47,50,48;48,50,51;40,38,51;51,38,48;41,52,39;39,52,49;49,52,50;50,52,53;53,54,50;50,54,51;42,40,54;54,40,51;41,43,52;43,44,52;52,44,53;44,45,53;53,45,54;45,42,54;];


if (p2(2)==1) %part ID 1=UnitCylinder
    res=p2(3); %resolution
    scalex=p2(4); %diameter of cylinder in x direction
    scaley=p2(5); %diameter of cylinder in y direction
    scalez=p2(6); %length of cylinder
    rotx=p2(7); %rotation about x axis
    roty=p2(8); %rotation about y axis
    rotz=p2(9); %rotation about z axis
    transx=p2(10); %translation in x direction
    transy=p2(11); %translation in y direction
    transz=p2(12); %translation in z direction
    
    
    V2=cylinder.vertices;
    V2=[V2(:,1)*scalex, V2(:,2)*scaley, V2(:,3)*scalez];
    cylinder.vertices=V2;

     a=rotz/57.29; %degrees to radians
     V2=cylinder.vertices;
     V2=[cos(a)*V2(:,1)-sin(a)*V2(:,2), sin(a)*V(:,1)+cos(a)*V2(:,2), V2(:,3)];
     cylinder.vertices=V2;

    a=roty/57.29; %degrees to radians
    V2=cylinder.vertices;
    V2=[cos(a)*V2(:,1)+sin(a)*V2(:,3), V2(:,2), -sin(a)*V2(:,1)+cos(a)*V2(:,3)];
    cylinder.vertices=V2;
  
    a=rotx/57.29; %degrees to radians
    V2=cylinder.vertices;
    V2=[V(:,1), cos(a)*V2(:,2)-sin(a)*V2(:,3), sin(a)*V2(:,2)+cos(a)*V2(:,3)];
    cylinder.vertices=V2;

    V2=cylinder.vertices;
    V2=[V2(:,1)+transx, V2(:,2)+transy, V2(:,3)+transz];
    cylinder.vertices=V2;
    
    part2=cylinder;
    
elseif (p2(2)==2) %part ID 2=UnitSphere
    res=p2(3); %resolution
    scalex=p2(4); %diameter of sphere in x direction
    scaley=p2(5); %diameter of sphere in y direction
    scalez=p2(6); %diameter of sphere in z direction
    rotx=p2(7); %rotation about x axis
    roty=p2(8); %rotation about y axis
    rotz=p2(9); %rotation about z axis
    transx=p2(10); %translation in x direction
    transy=p2(11); %translation in y direction
    transz=p2(12); %translation in z direction
    
  
    M2=sphere.vertices;
    M2=[M2(:,1)*scalex, M2(:,2)*scaley, M2(:,3)*scalez];
    sphere.vertices=M2;
 
    a=rotz/57.29; %degrees to radians
    M2=sphere.vertices;
    M2=[cos(a)*M2(:,1)-sin(a)*M2(:,2), sin(a)*M2(:,1)+cos(a)*M2(:,2), M2(:,3)];
    sphere.vertices=M2;

    a=roty/57.29; %degrees to radians
    M2=sphere.vertices;
    M2=[cos(a)*M2(:,1)+sin(a)*M2(:,3), M2(:,2), -sin(a)*M2(:,1)+cos(a)*M2(:,3)];
    sphere.vertices=M2;
  
    a=rotx/57.29; %degrees to radians
    M2=sphere.vertices;
    M2=[M2(:,1), cos(a)*M2(:,2)-sin(a)*M2(:,3), sin(a)*M2(:,2)+cos(a)*M2(:,3)];
    sphere.vertices=M2;

    M2=sphere.vertices;
    M2=[M2(:,1)+transx, M2(:,2)+transy, M2(:,3)+transz];
    sphere.vertices=M2;
    
    part2=sphere;

elseif (p2(2)==3) %part ID 3=UnitCube
    res=p2(3); %resolution
    scalex=p2(4); %length in x direction
    scaley=p2(5); %length in y direction
    scalez=p2(6); %length in z direction
    rotx=p2(7); %rotation about x axis
    roty=p2(8); %rotation about y axis
    rotz=p2(9); %rotation about z axis
    transx=p2(10); %translation in x direction
    transy=p2(11); %translation in y direction
    transz=p2(12); %translation in z direction
   
    F2=cube.vertices;
    F2=[F2(:,1)*scalex, F2(:,2)*scaley, F2(:,3)*scalez];
    cube.vertices=F2;

    a=rotz/57.29; %degrees to radians
    F2=cube.vertices;
    F2=[cos(a)*F2(:,1)-sin(a)*F2(:,2), sin(a)*F2(:,1)+cos(a)*F2(:,2), F2(:,3)];
    cube.vertices=F2;

    a=roty/57.29; %degrees to radians
    F2=cube.vertices;
    F2=[cos(a)*F2(:,1)+sin(a)*F2(:,3), F2(:,2), -sin(a)*F2(:,1)+cos(a)*F2(:,3)];
    cube.vertices=F2;
  
    a=rotx/57.29; %degrees to radians
    F2=cube.vertices;
    F2=[F(:,1), cos(a)*F2(:,2)-sin(a)*F2(:,3),sin(a)*F2(:,2)+cos(a)*F2(:,3)];
    cube.vertices=F2;

    F2=cube.vertices;
    F2=[F2(:,1)+transx, F2(:,2)+transy, F2(:,3)+transz];
    cube.vertices=F2;
    
    part2=cube;

else
    p2(2)=0;
end

cube.vertices=[ 0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1;] ;
cube.faces=[ 1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8; ] ;
cube.vertices = cube.vertices * 2 - 1;

cylinder.vertices=[-1,0,-1;-1,0,0;-0.500000000000000,-0.856380903262898,-1;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,-1;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,-1;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,-1;-0.500000000000000,0.856380903262898,0;0,-1,-1;0,-1,0;0,1,-1;0,1,0;0.500000000000000,-0.856380903262898,-1;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,-1;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,-1;0.856380903262898,-0.500000000000000,0;1,0,-1;1,0,0;0.856380903262898,0.500000000000000,-1;0.856380903262898,0.500000000000000,0;-1,0,1;-0.500000000000000,-0.856380903262898,1;-0.856380903262898,-0.500000000000000,1;-0.856380903262898,0.500000000000000,1;-0.500000000000000,0.856380903262898,1;0,-1,1;0,1,1;0.5000000000000
00,-0.856380903262898,1;0.500000000000000,0.856380903262898,1;0.856380903262898,-0.500000000000000,1;1,0,1;0.856380903262898,0.500000000000000,1;];
cylinder.faces=[3,4,5;5,4,6;5,6,1;1,6,2;1,2,7;7,2,8;7,8,9;9,8,10;11,12,3;3,12,4;9,10,13;13,10,14;11,15,12;12,15,16;17,13,18;18,13,14;15,19,16;16,19,20;19,21,20;20,21,22;21,23,22;22,23,24;23,17,24;24,17,18;4,26,6;6,26,27;6,27,2;2,27,25;2,25,8;8,25,28;8,28,10;10,28,29;12,30,4;4,30,26;10,29,14;14,29,31;12,16,30;30,16,32;18,14,33;33,14,31;16,20,32;32,20,34;20,22,34;34,22,35;22,24,35;35,24,36;24,18,36;36,18,33;];

sphere.vertices=[0,0,-1;-0.500000000000000,-0.500000000000000,-0.686740068930804;-0.500000000000000,-0.686740068930804,-0.500000000000000;-0.686740068930804,-0.500000000000000,-0.500000000000000;-0.500000000000000,0,-0.856380903262898;-0.856380903262898,0,-0.500000000000000;-0.500000000000000,0.500000000000000,-0.686740068930804;-0.686740068930804,0.500000000000000,-0.500000000000000;-0.500000000000000,0.686740068930805,-0.500000000000000;0,-0.500000000000000,-0.856380903262898;0,-0.856380903262898,-0.500000000000000;0,0.500000000000000,-0.856380903262898;0,0.856380903262898,-0.500000000000000;0.500000000000000,-0.500000000000000,-0.686740068930804;0.500000000000000,-0.686740068930804,-0.500000000000000;0.500000000000000,0,-0.856380903262898;0.500000000000000,0.500000000000000,-0.686740068930804;0.500000000000000,0.686740068930805,-0.500000000000000;0.686740068930805,-0.500000000000000,-
0.500000000000000;0.856380903262898,0,-0.500000000000000;0.686740068930805,0.500000000000000,-0.500000000000000;-1,0,0;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,0;0,-1,0;0,1,0;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,0;1,0,0;0.856380903262898,0.500000000000000,0;-0.500000000000000,-0.686740068930804,0.500000000000000;-0.686740068930804,-0.500000000000000,0.500000000000000;-0.856380903262898,0,0.500000000000000;-0.686740068930804,0.500000000000000,0.500000000000000;-0.500000000000000,0.686740068930805,0.500000000000000;0,-0.856380903262898,0.500000000000000;0,0.856380903262898,0.500000000000000;0.500000000000000,-0.686740068930804,0.500000000000000;0.500000000000000,0.686740068930805,0.500000000000000;0.6
86740068930805,-0.500000000000000,0.500000000000000;0.856380903262898,0,0.500000000000000;0.686740068930805,0.500000000000000,0.500000000000000;-0.500000000000000,-0.500000000000000,0.686740068930805;-0.500000000000000,0,0.856380903262898;-0.500000000000000,0.500000000000000,0.686740068930805;0,-0.500000000000000,0.856380903262898;0,0,1;0,0.500000000000000,0.856380903262898;0.500000000000000,-0.500000000000000,0.686740068930805;0.500000000000000,0,0.856380903262898;0.500000000000000,0.500000000000000,0.686740068930805;];
sphere.faces=[2,3,4;2,4,5;5,4,6;5,6,7;7,6,8;7,8,9;2,10,3;3,10,11;1,10,2;1,2,5;1,5,7;1,7,12;7,9,12;12,9,13;10,14,11;11,14,15;10,1,16;10,16,14;16,1,12;16,12,17;12,13,17;17,13,18;14,19,15;14,16,19;19,16,20;16,17,20;20,17,21;17,18,21;3,23,4;4,23,24;4,24,6;6,24,22;6,22,8;8,22,25;8,25,9;9,25,26;11,27,3;3,27,23;13,9,28;28,9,26;15,29,11;11,29,27;18,13,30;30,13,28;15,19,29;29,19,31;19,20,31;31,20,32;20,21,32;32,21,33;21,18,33;33,18,30;23,34,24;24,34,35;24,35,22;22,35,36;25,22,36;25,36,37;25,37,26;26,37,38;23,27,39;23,39,34;26,38,28;28,38,40;27,29,41;27,41,39;30,28,42;42,28,40;29,31,41;41,31,43;31,32,43;43,32,44;32,33,45;32,45,44;33,30,45;45,30,42;34,46,35;35,46,36;36,46,47;36,47,37;37,47,48;37,48,38;39,49,34;34,49,46;46,49,47;47,49,50;47,50,48;48,50,51;40,38,51;51,38,48;41,52,39;39,52,49;49,52,50;50,52,53;53,54,50;50,54,51;42,40,54;54,40,51;41,43,52;43,44,52;52,44,53;44,45,53;53,45,54;45,42,54;];

if (p3(2)==1) %part ID 1=UnitCylinder
    res=p3(3); %resolution
    scalex=p3(4); %diameter of cylinder in x direction
    scaley=p3(5); %diameter of cylinder in y direction
    scalez=p3(6); %length of cylinder
    rotx=p3(7); %rotation about x axis
    roty=p3(8); %rotation about y axis
    rotz=p3(9); %rotation about z axis
    transx=p3(10); %translation in x direction
    transy=p3(11); %translation in y direction
    transz=p3(12); %translation in z direction
    
    V3=cylinder.vertices;
    V3=[V3(:,1)*scalex, V3(:,2)*scaley, V3(:,3)*scalez];
    cylinder.vertices=V3;

    a=rotz/57.29; %degrees to radians
    V3=cylinder.vertices;
    V3=[cos(a)*V3(:,1)-sin(a)*V3(:,2), sin(a)*V3(:,1)+cos(a)*V3(:,2),V3(:,3)];
    cylinder.vertices=V3;

    a=roty/57.29; %degrees to radians
    V3=cylinder.vertices;
    V3=[cos(a)*V3(:,1)+sin(a)*V3(:,3),V3(:,2), -sin(a)*V3(:,1)+cos(a)*V3(:,3)];
    cylinder.vertices=V3;
  
    a=rotx/57.29; %degrees to radians
    V3=cylinder.vertices;
    V3=[V3(:,1), cos(a)*V3(:,2)-sin(a)*V3(:,3), sin(a)*V3(:,2)+cos(a)*V3(:,3)];
    cylinder.vertices=V3;

    V3=cylinder.vertices;
    V3=[V3(:,1)+transx, V3(:,2)+transy, V3(:,3)+transz];
    cylinder.vertices=V3;
    
    part3=cylinder;
    
elseif (p3(2)==2) %part ID 2=UnitSphere
    res=p3(3); %resolution
    scalex=p3(4); %diameter of sphere in x direction
    scaley=p3(5); %diameter of sphere in y direction
    scalez=p3(6); %diameter of sphere in z direction
    rotx=p3(7); %rotation about x axis
    roty=p3(8); %rotation about y axis
    rotz=p3(9); %rotation about z axis
    transx=p3(10); %translation in x direction
    transy=p3(11); %translation in y direction
    transz=p3(12); %translation in z direction
    
    M3=sphere.vertices;
    M3=[M3(:,1)*scalex, M3(:,2)*scaley, M3(:,3)*scalez];
    sphere.vertices=M3;

    a=rotz/57.29; %degrees to radians
    M3=sphere.vertices;
    M3=[cos(a)*M3(:,1)-sin(a)*M3(:,2),sin(a)*M3(:,1)+cos(a)*M3(:,2),M3(:,3)];
    sphere.vertices=M3;

    a=roty/57.29; %degrees to radians
    M3=sphere.vertices;
    M3=[cos(a)*M3(:,1)+sin(a)*M3(:,3),M3(:,2),-sin(a)*M3(:,1)+cos(a)*M3(:,3)];
    sphere.vertices=M3;
  
    a=rotx/57.29; %degrees to radians
    M3=sphere.vertices;
    M3=[M3(:,1), cos(a)*M3(:,2)-sin(a)*M3(:,3),sin(a)*M3(:,2)+cos(a)*M3(:,3)];
    sphere.vertices=M3;

    M3=sphere.vertices;
    M3=[M3(:,1)+transx, M3(:,2)+transy, M3(:,3)+transz];
    sphere.vertices=M3;
    
    part3=sphere;

elseif (p3(2)==3) %part ID 3=UnitCube
    res=p3(3); %resolution
    scalex=p3(4); %length in x direction
    scaley=p3(5); %length in y direction
    scalez=p3(6); %length in z direction
    rotx=p3(7); %rotation about x axis
    roty=p3(8); %rotation about y axis
    rotz=p3(9); %rotation about z axis
    transx=p3(10); %translation in x direction
    transy=p3(11); %translation in y direction
    transz=p3(12); %translation in z direction
   
    F3=cube.vertices;
    F3=[F3(:,1)*scalex, F3(:,2)*scaley, F3(:,3)*scalez];
    cube.vertices=F3;

    a=rotz/57.29; %degrees to radians
    F3=cube.vertices;
    F3=[cos(a)*F3(:,1)-sin(a)*F3(:,2),sin(a)*F3(:,1)+cos(a)*F3(:,2),F3(:,3)];
    cube.vertices=F3;

    a=roty/57.29; %degrees to radians
    F3=cube.vertices;
    F3=[cos(a)*F3(:,1)+sin(a)*F3(:,3),F3(:,2), -sin(a)*F3(:,1)+cos(a)*F3(:,3)];
    cube.vertices=F3;
  
    a=rotx/57.29; %degrees to radians
    F3=cube.vertices;
    F3=[F3(:,1),cos(a)*F3(:,2)-sin(a)*F3(:,3),sin(a)*F3(:,2)+cos(a)*F3(:,3)];
    cube.vertices=F3;

    F3=cube.vertices;
    F3=[F3(:,1)+transx, F3(:,2)+transy, F3(:,3)+transz];
    cube.vertices=F3;
  
    part3=cube;

else
    p3(2)=0;
end
 
cube.vertices=[ 0 0 0; 1 0 0; 1 1 0; 0 1 0;0 0 1; 1 0 1; 1 1 1; 0 1 1;] ;
cube.faces=[ 1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8; ] ;
cube.vertices = cube.vertices * 2 - 1;

cylinder.vertices=[-1,0,-1;-1,0,0;-0.500000000000000,-0.856380903262898,-1;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,-1;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,-1;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,-1;-0.500000000000000,0.856380903262898,0;0,-1,-1;0,-1,0;0,1,-1;0,1,0;0.500000000000000,-0.856380903262898,-1;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,-1;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,-1;0.856380903262898,-0.500000000000000,0;1,0,-1;1,0,0;0.856380903262898,0.500000000000000,-1;0.856380903262898,0.500000000000000,0;-1,0,1;-0.500000000000000,-0.856380903262898,1;-0.856380903262898,-0.500000000000000,1;-0.856380903262898,0.500000000000000,1;-0.500000000000000,0.856380903262898,1;0,-1,1;0,1,1;0.5000000000000
00,-0.856380903262898,1;0.500000000000000,0.856380903262898,1;0.856380903262898,-0.500000000000000,1;1,0,1;0.856380903262898,0.500000000000000,1;];
cylinder.faces=[3,4,5;5,4,6;5,6,1;1,6,2;1,2,7;7,2,8;7,8,9;9,8,10;11,12,3;3,12,4;9,10,13;13,10,14;11,15,12;12,15,16;17,13,18;18,13,14;15,19,16;16,19,20;19,21,20;20,21,22;21,23,22;22,23,24;23,17,24;24,17,18;4,26,6;6,26,27;6,27,2;2,27,25;2,25,8;8,25,28;8,28,10;10,28,29;12,30,4;4,30,26;10,29,14;14,29,31;12,16,30;30,16,32;18,14,33;33,14,31;16,20,32;32,20,34;20,22,34;34,22,35;22,24,35;35,24,36;24,18,36;36,18,33;];

sphere.vertices=[0,0,-1;-0.500000000000000,-0.500000000000000,-0.686740068930804;-0.500000000000000,-0.686740068930804,-0.500000000000000;-0.686740068930804,-0.500000000000000,-0.500000000000000;-0.500000000000000,0,-0.856380903262898;-0.856380903262898,0,-0.500000000000000;-0.500000000000000,0.500000000000000,-0.686740068930804;-0.686740068930804,0.500000000000000,-0.500000000000000;-0.500000000000000,0.686740068930805,-0.500000000000000;0,-0.500000000000000,-0.856380903262898;0,-0.856380903262898,-0.500000000000000;0,0.500000000000000,-0.856380903262898;0,0.856380903262898,-0.500000000000000;0.500000000000000,-0.500000000000000,-0.686740068930804;0.500000000000000,-0.686740068930804,-0.500000000000000;0.500000000000000,0,-0.856380903262898;0.500000000000000,0.500000000000000,-0.686740068930804;0.500000000000000,0.686740068930805,-0.500000000000000;0.686740068930805,-0.500000000000000,-
0.500000000000000;0.856380903262898,0,-0.500000000000000;0.686740068930805,0.500000000000000,-0.500000000000000;-1,0,0;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,0;0,-1,0;0,1,0;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,0;1,0,0;0.856380903262898,0.500000000000000,0;-0.500000000000000,-0.686740068930804,0.500000000000000;-0.686740068930804,-0.500000000000000,0.500000000000000;-0.856380903262898,0,0.500000000000000;-0.686740068930804,0.500000000000000,0.500000000000000;-0.500000000000000,0.686740068930805,0.500000000000000;0,-0.856380903262898,0.500000000000000;0,0.856380903262898,0.500000000000000;0.500000000000000,-0.686740068930804,0.500000000000000;0.500000000000000,0.686740068930805,0.500000000000000;0.6
86740068930805,-0.500000000000000,0.500000000000000;0.856380903262898,0,0.500000000000000;0.686740068930805,0.500000000000000,0.500000000000000;-0.500000000000000,-0.500000000000000,0.686740068930805;-0.500000000000000,0,0.856380903262898;-0.500000000000000,0.500000000000000,0.686740068930805;0,-0.500000000000000,0.856380903262898;0,0,1;0,0.500000000000000,0.856380903262898;0.500000000000000,-0.500000000000000,0.686740068930805;0.500000000000000,0,0.856380903262898;0.500000000000000,0.500000000000000,0.686740068930805;];
sphere.faces=[2,3,4;2,4,5;5,4,6;5,6,7;7,6,8;7,8,9;2,10,3;3,10,11;1,10,2;1,2,5;1,5,7;1,7,12;7,9,12;12,9,13;10,14,11;11,14,15;10,1,16;10,16,14;16,1,12;16,12,17;12,13,17;17,13,18;14,19,15;14,16,19;19,16,20;16,17,20;20,17,21;17,18,21;3,23,4;4,23,24;4,24,6;6,24,22;6,22,8;8,22,25;8,25,9;9,25,26;11,27,3;3,27,23;13,9,28;28,9,26;15,29,11;11,29,27;18,13,30;30,13,28;15,19,29;29,19,31;19,20,31;31,20,32;20,21,32;32,21,33;21,18,33;33,18,30;23,34,24;24,34,35;24,35,22;22,35,36;25,22,36;25,36,37;25,37,26;26,37,38;23,27,39;23,39,34;26,38,28;28,38,40;27,29,41;27,41,39;30,28,42;42,28,40;29,31,41;41,31,43;31,32,43;43,32,44;32,33,45;32,45,44;33,30,45;45,30,42;34,46,35;35,46,36;36,46,47;36,47,37;37,47,48;37,48,38;39,49,34;34,49,46;46,49,47;47,49,50;47,50,48;48,50,51;40,38,51;51,38,48;41,52,39;39,52,49;49,52,50;50,52,53;53,54,50;50,54,51;42,40,54;54,40,51;41,43,52;43,44,52;52,44,53;44,45,53;53,45,54;45,42,54;];

if (p4(2)==1) %part ID 1=UnitCylinder
    res=p4(3); %resolution
    scalex=p4(4); %diameter of cylinder in x direction
    scaley=p4(5); %diameter of cylinder in y direction
    scalez=p4(6); %length of cylinder
    rotx=p4(7); %rotation about x axis
    roty=p4(8); %rotation about y axis
    rotz=p4(9); %rotation about z axis
    transx=p4(10); %translation in x direction
    transy=p4(11); %translation in y direction
    transz=p4(12); %translation in z direction
    
    V4=cylinder.vertices;
    V4=[V4(:,1)*scalex, V4(:,2)*scaley, V4(:,3)*scalez];
    cylinder.vertices=V4;

    a=rotz/57.29; %degrees to radians
    V4=cylinder.vertices;
    V4=[cos(a)*V4(:,1)-sin(a)*V4(:,2),sin(a)*V4(:,1)+cos(a)*V4(:,2),V4(:,3)];
    cylinder.vertices=V4;

    a=roty/57.29; %degrees to radians
    V4=cylinder.vertices;
    V4=[cos(a)*V4(:,1)+sin(a)*V4(:,3),V4(:,2), -sin(a)*V4(:,1)+cos(a)*V4(:,3)];
    cylinder.vertices=V4;
  
    
    a=rotx/57.29; %degrees to radians
    V4=cylinder.vertices;
    V4=[V4(:,1), cos(a)*V4(:,2)-sin(a)*V4(:,3), sin(a)*V4(:,2)+cos(a)*V4(:,3)];
    cylinder.vertices=V4;

    V4=cylinder.vertices;
    V4=[V4(:,1)+transx, V4(:,2)+transy, V4(:,3)+transz];
    cylinder.vertices=V4;
    
    part4=cylinder;
    
elseif (p4(2)==2) %part ID 2=UnitSphere
    res=p4(3); %resolution
    scalex=p4(4); %diameter of sphere in x direction
    scaley=p4(5); %diameter of sphere in y direction
    scalez=p4(6); %diameter of sphere in z direction
    rotx=p4(7); %rotation about x axis
    roty=p4(8); %rotation about y axis
    rotz=p4(9); %rotation about z axis
    transx=p4(10); %translation in x direction
    transy=p4(11); %translation in y direction
    transz=p4(12); %translation in z direction
    
    M4=sphere.vertices;
    M4=[M4(:,1)*scalex, M4(:,2)*scaley, M4(:,3)*scalez];
    sphere.vertices=M4;

    a=rotz/57.29; %degrees to radians
    M4=sphere.vertices;
    M4=[cos(a)*M4(:,1)-sin(a)*M4(:,2),sin(a)*M4(:,1)+cos(a)*M4(:,2),M4(:,3)];
    sphere.vertices=M4;

    a=roty/57.29; %degrees to radians
    M4=sphere.vertices;
    M4=[cos(a)*M4(:,1)+sin(a)*M4(:,3), M4(:,2),-sin(a)*M4(:,1)+cos(a)*M4(:,3)];
    sphere.vertices=M4;
  
    a=rotx/57.29; %degrees to radians
    M4=sphere.vertices;
    M4=[M4(:,1), cos(a)*M4(:,2)-sin(a)*M4(:,3),sin(a)*M4(:,2)+cos(a)*M4(:,3)];
    sphere.vertices=M4;

    M4=sphere.vertices;
    M4=[M4(:,1)+transx, M4(:,2)+transy, M4(:,3)+transz];
    sphere.vertices=M4;
 
    part4=sphere;

elseif (p4(2)==3) %part ID 3=UnitCube
    res=p4(3); %resolution
    scalex=p4(4); %length in x direction
    scaley=p4(5); %length in y direction
    scalez=p4(6); %length in z direction
    rotx=p4(7); %rotation about x axis
    roty=p4(8); %rotation about y axis
    rotz=p4(9); %rotation about z axis
    transx=p4(10); %translation in x direction
    transy=p4(11); %translation in y direction
    transz=p4(12); %translation in z direction
   
    F4=cube.vertices;
    F4=[F4(:,1)*scalex, F4(:,2)*scaley, F4(:,3)*scalez];
    cube.vertices=F4;

    a=rotz/57.29; %degrees to radians
    F4=cube.vertices;
    F4=[cos(a)*F4(:,1)-sin(a)*F4(:,2), sin(a)*F4(:,1)+cos(a)*F4(:,2),F4(:,3)];
    cube.vertices=F4;

    a=roty/57.29; %degrees to radians
    F4=cube.vertices;
    F4=[cos(a)*F4(:,1)+sin(a)*F4(:,3), F4(:,2), -sin(a)*F4(:,1)+cos(a)*F4(:,3)];
    cube.vertices=F4;
  
    a=rotx/57.29; %degrees to radians
    F4=cube.vertices;
    F4=[F4(:,1), cos(a)*F4(:,2)-sin(a)*F4(:,3), sin(a)*F4(:,2)+cos(a)*F4(:,3)];
    cube.vertices=F4;

    F4=cube.vertices;
    F4=[F4(:,1)+transx, F4(:,2)+transy, F4(:,3)+transz];
    cube.vertices=F4;
  
    part4=cube;

else
    p4(2)=0;
end

cube.vertices=[ 0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1;] ;
cube.faces=[ 1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8; ] ;
cube.vertices = cube.vertices * 2 - 1;

cylinder.vertices=[-1,0,-1;-1,0,0;-0.500000000000000,-0.856380903262898,-1;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,-1;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,-1;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,-1;-0.500000000000000,0.856380903262898,0;0,-1,-1;0,-1,0;0,1,-1;0,1,0;0.500000000000000,-0.856380903262898,-1;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,-1;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,-1;0.856380903262898,-0.500000000000000,0;1,0,-1;1,0,0;0.856380903262898,0.500000000000000,-1;0.856380903262898,0.500000000000000,0;-1,0,1;-0.500000000000000,-0.856380903262898,1;-0.856380903262898,-0.500000000000000,1;-0.856380903262898,0.500000000000000,1;-0.500000000000000,0.856380903262898,1;0,-1,1;0,1,1;0.5000000000000
00,-0.856380903262898,1;0.500000000000000,0.856380903262898,1;0.856380903262898,-0.500000000000000,1;1,0,1;0.856380903262898,0.500000000000000,1;];
cylinder.faces=[3,4,5;5,4,6;5,6,1;1,6,2;1,2,7;7,2,8;7,8,9;9,8,10;11,12,3;3,12,4;9,10,13;13,10,14;11,15,12;12,15,16;17,13,18;18,13,14;15,19,16;16,19,20;19,21,20;20,21,22;21,23,22;22,23,24;23,17,24;24,17,18;4,26,6;6,26,27;6,27,2;2,27,25;2,25,8;8,25,28;8,28,10;10,28,29;12,30,4;4,30,26;10,29,14;14,29,31;12,16,30;30,16,32;18,14,33;33,14,31;16,20,32;32,20,34;20,22,34;34,22,35;22,24,35;35,24,36;24,18,36;36,18,33;];

sphere.vertices=[0,0,-1;-0.500000000000000,-0.500000000000000,-0.686740068930804;-0.500000000000000,-0.686740068930804,-0.500000000000000;-0.686740068930804,-0.500000000000000,-0.500000000000000;-0.500000000000000,0,-0.856380903262898;-0.856380903262898,0,-0.500000000000000;-0.500000000000000,0.500000000000000,-0.686740068930804;-0.686740068930804,0.500000000000000,-0.500000000000000;-0.500000000000000,0.686740068930805,-0.500000000000000;0,-0.500000000000000,-0.856380903262898;0,-0.856380903262898,-0.500000000000000;0,0.500000000000000,-0.856380903262898;0,0.856380903262898,-0.500000000000000;0.500000000000000,-0.500000000000000,-0.686740068930804;0.500000000000000,-0.686740068930804,-0.500000000000000;0.500000000000000,0,-0.856380903262898;0.500000000000000,0.500000000000000,-0.686740068930804;0.500000000000000,0.686740068930805,-0.500000000000000;0.686740068930805,-0.500000000000000,-
0.500000000000000;0.856380903262898,0,-0.500000000000000;0.686740068930805,0.500000000000000,-0.500000000000000;-1,0,0;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,0;0,-1,0;0,1,0;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,0;1,0,0;0.856380903262898,0.500000000000000,0;-0.500000000000000,-0.686740068930804,0.500000000000000;-0.686740068930804,-0.500000000000000,0.500000000000000;-0.856380903262898,0,0.500000000000000;-0.686740068930804,0.500000000000000,0.500000000000000;-0.500000000000000,0.686740068930805,0.500000000000000;0,-0.856380903262898,0.500000000000000;0,0.856380903262898,0.500000000000000;0.500000000000000,-0.686740068930804,0.500000000000000;0.500000000000000,0.686740068930805,0.500000000000000;0.6
86740068930805,-0.500000000000000,0.500000000000000;0.856380903262898,0,0.500000000000000;0.686740068930805,0.500000000000000,0.500000000000000;-0.500000000000000,-0.500000000000000,0.686740068930805;-0.500000000000000,0,0.856380903262898;-0.500000000000000,0.500000000000000,0.686740068930805;0,-0.500000000000000,0.856380903262898;0,0,1;0,0.500000000000000,0.856380903262898;0.500000000000000,-0.500000000000000,0.686740068930805;0.500000000000000,0,0.856380903262898;0.500000000000000,0.500000000000000,0.686740068930805;];
sphere.faces=[2,3,4;2,4,5;5,4,6;5,6,7;7,6,8;7,8,9;2,10,3;3,10,11;1,10,2;1,2,5;1,5,7;1,7,12;7,9,12;12,9,13;10,14,11;11,14,15;10,1,16;10,16,14;16,1,12;16,12,17;12,13,17;17,13,18;14,19,15;14,16,19;19,16,20;16,17,20;20,17,21;17,18,21;3,23,4;4,23,24;4,24,6;6,24,22;6,22,8;8,22,25;8,25,9;9,25,26;11,27,3;3,27,23;13,9,28;28,9,26;15,29,11;11,29,27;18,13,30;30,13,28;15,19,29;29,19,31;19,20,31;31,20,32;20,21,32;32,21,33;21,18,33;33,18,30;23,34,24;24,34,35;24,35,22;22,35,36;25,22,36;25,36,37;25,37,26;26,37,38;23,27,39;23,39,34;26,38,28;28,38,40;27,29,41;27,41,39;30,28,42;42,28,40;29,31,41;41,31,43;31,32,43;43,32,44;32,33,45;32,45,44;33,30,45;45,30,42;34,46,35;35,46,36;36,46,47;36,47,37;37,47,48;37,48,38;39,49,34;34,49,46;46,49,47;47,49,50;47,50,48;48,50,51;40,38,51;51,38,48;41,52,39;39,52,49;49,52,50;50,52,53;53,54,50;50,54,51;42,40,54;54,40,51;41,43,52;43,44,52;52,44,53;44,45,53;53,45,54;45,42,54;];

if (p5(2)==1) %part ID 1=UnitCylinder
    res=p5(3); %resolution
    scalex=p5(4); %diameter of cylinder in x direction
    scaley=p5(5); %diameter of cylinder in y direction
    scalez=p5(6); %length of cylinder
    rotx=p5(7); %rotation about x axis
    roty=p5(8); %rotation about y axis
    rotz=p5(9); %rotation about z axis
    transx=p5(10); %translation in x direction
    transy=p5(11); %translation in y direction
    transz=p5(12); %translation in z direction
    
    V5=cylinder.vertices;
    V5=[V5(:,1)*scalex, V5(:,2)*scaley, V5(:,3)*scalez];
    cylinder.vertices=V5;

    a=rotz/57.29; %degrees to radians
    V5=cylinder.vertices;
    V5=[cos(a)*V5(:,1)-sin(a)*V5(:,2), sin(a)*V5(:,1)+cos(a)*V5(:,2),V5(:,3)];
    cylinder.vertices=V5;

    a=roty/57.29; %degrees to radians
    V5=cylinder.vertices;
    V5=[cos(a)*V5(:,1)+sin(a)*V5(:,3), V5(:,2), -sin(a)*V5(:,1)+cos(a)*V5(:,3)];
    cylinder.vertices=V5;
  
    a=rotx/57.29; %degrees to radians
    V5=cylinder.vertices;
    V5=[V5(:,1), cos(a)*V5(:,2)-sin(a)*V5(:,3), sin(a)*V5(:,2)+cos(a)*V5(:,3)];
    cylinder.vertices=V5;

    V5=cylinder.vertices;
    V5=[V5(:,1)+transx, V5(:,2)+transy, V5(:,3)+transz];
    cylinder.vertices=V5;
    
    part5=cylinder;
    
elseif (p5(2)==2) %part ID 2=UnitSphere
    res=p5(3); %resolution
    scalex=p5(4); %diameter of sphere in x direction
    scaley=p5(5); %diameter of sphere in y direction
    scalez=p5(6); %diameter of sphere in z direction
    rotx=p5(7); %rotation about x axis
    roty=p5(8); %rotation about y axis
    rotz=p5(9); %rotation about z axis
    transx=p5(10); %translation in x direction
    transy=p5(11); %translation in y direction
    transz=p5(12); %translation in z direction
    
    M5=sphere.vertices;
    M5=[M5(:,1)*scalex, M5(:,2)*scaley, M5(:,3)*scalez];
    sphere.vertices=M5;

    a=rotz/57.29; %degrees to radians
    M5=sphere.vertices;
    M5=[cos(a)*M5(:,1)-sin(a)*M5(:,2), sin(a)*M5(:,1)+cos(a)*M5(:,2),M5(:,3)];
    sphere.vertices=M5;

    a=roty/57.29; %degrees to radians
    M5=sphere.vertices;
    M5=[cos(a)*M5(:,1)+sin(a)*M5(:,3), M5(:,2), -sin(a)*M5(:,1)+cos(a)*M5(:,3)];
    sphere.vertices=M5;
  
    a=rotx/57.29; %degrees to radians
    M5=sphere.vertices;
    M5=[M5(:,1), cos(a)*M5(:,2)-sin(a)*M5(:,3), sin(a)*M5(:,2)+cos(a)*M5(:,3)];
    sphere.vertices=M5;

    M5=sphere.vertices;
    M5=[M5(:,1)+transx, M5(:,2)+transy, M5(:,3)+transz];
    sphere.vertices=M5;
 
    part5=sphere;

elseif (p5(2)==3) %part ID 3=UnitCube
    res=p5(3); %resolution
    scalex=p5(4); %length in x direction
    scaley=p5(5); %length in y direction
    scalez=p5(6); %length in z direction
    rotx=p5(7); %rotation about x axis
    roty=p5(8); %rotation about y axis
    rotz=p5(9); %rotation about z axis
    transx=p5(10); %translation in x direction
    transy=p5(11); %translation in y direction
    transz=p5(12); %translation in z direction
   
    F5=cube.vertices;
    F5=[F5(:,1)*scalex, F5(:,2)*scaley, F5(:,3)*scalez];
    cube.vertices=F5;

    a=rotz/57.29; %degrees to radians
    F5=cube.vertices;
    F5=[cos(a)*F5(:,1)-sin(a)*F5(:,2), sin(a)*F5(:,1)+cos(a)*F5(:,2), F5(:,3)];
    cube.vertices=F5;

    a=roty/57.29; %degrees to radians
    F5=cube.vertices;
    F5=[cos(a)*F5(:,1)+sin(a)*F5(:,3), F5(:,2), -sin(a)*F5(:,1)+cos(a)*F5(:,3)];
    cube.vertices=F5;
  
    a=rotx/57.29; %degrees to radians
    F5=cube.vertices;
    F5=[F5(:,1), cos(a)*F5(:,2)-sin(a)*F5(:,3), sin(a)*F5(:,2)+cos(a)*F5(:,3)];
    cube.vertices=F5;

    F5=cube.vertices;
    F5=[F5(:,1)+transx, F5(:,2)+transy, F5(:,3)+transz];
    cube.vertices=F5;
  
    part5=cube;

else
    p5(2)=0;
end

cube.vertices=[ 0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1;] ;
cube.faces=[ 1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8; ] ;
cube.vertices = cube.vertices * 2 - 1;

cylinder.vertices=[-1,0,-1;-1,0,0;-0.500000000000000,-0.856380903262898,-1;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,-1;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,-1;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,-1;-0.500000000000000,0.856380903262898,0;0,-1,-1;0,-1,0;0,1,-1;0,1,0;0.500000000000000,-0.856380903262898,-1;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,-1;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,-1;0.856380903262898,-0.500000000000000,0;1,0,-1;1,0,0;0.856380903262898,0.500000000000000,-1;0.856380903262898,0.500000000000000,0;-1,0,1;-0.500000000000000,-0.856380903262898,1;-0.856380903262898,-0.500000000000000,1;-0.856380903262898,0.500000000000000,1;-0.500000000000000,0.856380903262898,1;0,-1,1;0,1,1;0.5000000000000
00,-0.856380903262898,1;0.500000000000000,0.856380903262898,1;0.856380903262898,-0.500000000000000,1;1,0,1;0.856380903262898,0.500000000000000,1;];
cylinder.faces=[3,4,5;5,4,6;5,6,1;1,6,2;1,2,7;7,2,8;7,8,9;9,8,10;11,12,3;3,12,4;9,10,13;13,10,14;11,15,12;12,15,16;17,13,18;18,13,14;15,19,16;16,19,20;19,21,20;20,21,22;21,23,22;22,23,24;23,17,24;24,17,18;4,26,6;6,26,27;6,27,2;2,27,25;2,25,8;8,25,28;8,28,10;10,28,29;12,30,4;4,30,26;10,29,14;14,29,31;12,16,30;30,16,32;18,14,33;33,14,31;16,20,32;32,20,34;20,22,34;34,22,35;22,24,35;35,24,36;24,18,36;36,18,33;];

sphere.vertices=[0,0,-1;-0.500000000000000,-0.500000000000000,-0.686740068930804;-0.500000000000000,-0.686740068930804,-0.500000000000000;-0.686740068930804,-0.500000000000000,-0.500000000000000;-0.500000000000000,0,-0.856380903262898;-0.856380903262898,0,-0.500000000000000;-0.500000000000000,0.500000000000000,-0.686740068930804;-0.686740068930804,0.500000000000000,-0.500000000000000;-0.500000000000000,0.686740068930805,-0.500000000000000;0,-0.500000000000000,-0.856380903262898;0,-0.856380903262898,-0.500000000000000;0,0.500000000000000,-0.856380903262898;0,0.856380903262898,-0.500000000000000;0.500000000000000,-0.500000000000000,-0.686740068930804;0.500000000000000,-0.686740068930804,-0.500000000000000;0.500000000000000,0,-0.856380903262898;0.500000000000000,0.500000000000000,-0.686740068930804;0.500000000000000,0.686740068930805,-0.500000000000000;0.686740068930805,-0.500000000000000,-
0.500000000000000;0.856380903262898,0,-0.500000000000000;0.686740068930805,0.500000000000000,-0.500000000000000;-1,0,0;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,0;0,-1,0;0,1,0;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,0;1,0,0;0.856380903262898,0.500000000000000,0;-0.500000000000000,-0.686740068930804,0.500000000000000;-0.686740068930804,-0.500000000000000,0.500000000000000;-0.856380903262898,0,0.500000000000000;-0.686740068930804,0.500000000000000,0.500000000000000;-0.500000000000000,0.686740068930805,0.500000000000000;0,-0.856380903262898,0.500000000000000;0,0.856380903262898,0.500000000000000;0.500000000000000,-0.686740068930804,0.500000000000000;0.500000000000000,0.686740068930805,0.500000000000000;0.6
86740068930805,-0.500000000000000,0.500000000000000;0.856380903262898,0,0.500000000000000;0.686740068930805,0.500000000000000,0.500000000000000;-0.500000000000000,-0.500000000000000,0.686740068930805;-0.500000000000000,0,0.856380903262898;-0.500000000000000,0.500000000000000,0.686740068930805;0,-0.500000000000000,0.856380903262898;0,0,1;0,0.500000000000000,0.856380903262898;0.500000000000000,-0.500000000000000,0.686740068930805;0.500000000000000,0,0.856380903262898;0.500000000000000,0.500000000000000,0.686740068930805;];
sphere.faces=[2,3,4;2,4,5;5,4,6;5,6,7;7,6,8;7,8,9;2,10,3;3,10,11;1,10,2;1,2,5;1,5,7;1,7,12;7,9,12;12,9,13;10,14,11;11,14,15;10,1,16;10,16,14;16,1,12;16,12,17;12,13,17;17,13,18;14,19,15;14,16,19;19,16,20;16,17,20;20,17,21;17,18,21;3,23,4;4,23,24;4,24,6;6,24,22;6,22,8;8,22,25;8,25,9;9,25,26;11,27,3;3,27,23;13,9,28;28,9,26;15,29,11;11,29,27;18,13,30;30,13,28;15,19,29;29,19,31;19,20,31;31,20,32;20,21,32;32,21,33;21,18,33;33,18,30;23,34,24;24,34,35;24,35,22;22,35,36;25,22,36;25,36,37;25,37,26;26,37,38;23,27,39;23,39,34;26,38,28;28,38,40;27,29,41;27,41,39;30,28,42;42,28,40;29,31,41;41,31,43;31,32,43;43,32,44;32,33,45;32,45,44;33,30,45;45,30,42;34,46,35;35,46,36;36,46,47;36,47,37;37,47,48;37,48,38;39,49,34;34,49,46;46,49,47;47,49,50;47,50,48;48,50,51;40,38,51;51,38,48;41,52,39;39,52,49;49,52,50;50,52,53;53,54,50;50,54,51;42,40,54;54,40,51;41,43,52;43,44,52;52,44,53;44,45,53;53,45,54;45,42,54;];

if (p6(2)==1) %part ID 1=UnitCylinder
    res=p6(3); %resolution
    scalex=p6(4); %diameter of cylinder in x direction
    scaley=p6(5); %diameter of cylinder in y direction
    scalez=p6(6); %length of cylinder
    rotx=p6(7); %rotation about x axis
    roty=p6(8); %rotation about y axis
    rotz=p6(9); %rotation about z axis
    transx=p6(10); %translation in x direction
    transy=p6(11); %translation in y direction
    transz=p6(12); %translation in z direction
    
    V6=cylinder.vertices;
    V6=[V6(:,1)*scalex, V6(:,2)*scaley, V6(:,3)*scalez];
    cylinder.vertices=V6;

    a=rotz/57.29; %degrees to radians
    V6=cylinder.vertices;
    V6=[cos(a)*V6(:,1)-sin(a)*V6(:,2), sin(a)*V6(:,1)+cos(a)*V6(:,2),V6(:,3)];
    cylinder.vertices=V6;

    a=roty/57.29; %degrees to radians
    V6=cylinder.vertices;
    V6=[cos(a)*V6(:,1)+sin(a)*V6(:,3), V6(:,2), -sin(a)*V6(:,1)+cos(a)*V6(:,3)];
    cylinder.vertices=V6;
  
    a=rotx/57.29; %degrees to radians
    V6=cylinder.vertices;
    V6=[V6(:,1), cos(a)*V6(:,2)-sin(a)*V6(:,3), sin(a)*V6(:,2)+cos(a)*V6(:,3)];
    cylinder.vertices=V6;

    V6=cylinder.vertices;
    V6=[V6(:,1)+transx, V6(:,2)+transy, V6(:,3)+transz];
    cylinder.vertices=V6;
    
    part6=cylinder;
    
elseif (p6(2)==2) %part ID 2=UnitSphere
    res=p6(3); %resolution
    scalex=p6(4); %diameter of sphere in x direction
    scaley=p6(5); %diameter of sphere in y direction
    scalez=p6(6); %diameter of sphere in z direction
    rotx=p6(7); %rotation about x axis
    roty=p6(8); %rotation about y axis
    rotz=p6(9); %rotation about z axis
    transx=p6(10); %translation in x direction
    transy=p6(11); %translation in y direction
    transz=p6(12); %translation in z direction
    
    M6=sphere.vertices;
    M6=[M6(:,1)*scalex, M6(:,2)*scaley, M6(:,3)*scalez];
    sphere.vertices=M6;

    a=rotz/57.29; %degrees to radians
    M6=sphere.vertices;
    M6=[cos(a)*M6(:,1)-sin(a)*M6(:,2), sin(a)*M6(:,1)+cos(a)*M6(:,2), M6(:,3)];
    sphere.vertices=M6;

    a=roty/57.29; %degrees to radians
    M6=sphere.vertices;
    M6=[cos(a)*M6(:,1)+sin(a)*M6(:,3), M6(:,2), -sin(a)*M6(:,1)+cos(a)*M6(:,3)];
    sphere.vertices=M6;
  
    a=rotx/57.29; %degrees to radians
    M6=sphere.vertices;
    M6=[M6(:,1), cos(a)*M6(:,2)-sin(a)*M6(:,3), sin(a)*M6(:,2)+cos(a)*M6(:,3)];
    sphere.vertices=M6;

    M6=sphere.vertices;
    M6=[M6(:,1)+transx, M6(:,2)+transy, M6(:,3)+transz];
    sphere.vertices=M6;
 
    part6=sphere;

elseif (p6(2)==3) %part ID 3=UnitCube
    res=p6(3); %resolution
    scalex=p6(4); %length in x direction
    scaley=p6(5); %length in y direction
    scalez=p6(6); %length in z direction
    rotx=p6(7); %rotation about x axis
    roty=p6(8); %rotation about y axis
    rotz=p6(9); %rotation about z axis
    transx=p6(10); %translation in x direction
    transy=p6(11); %translation in y direction
    transz=p6(12); %translation in z direction
   
    F6=cube.vertices;
    F6=[F6(:,1)*scalex, F6(:,2)*scaley, F6(:,3)*scalez];
    cube.vertices=F6;

    a=rotz/57.29; %degrees to radians
    F6=cube.vertices;
    F6=[cos(a)*F6(:,1)-sin(a)*F6(:,2), sin(a)*F6(:,1)+cos(a)*F6(:,2), F6(:,3)];
    cube.vertices=F6;

    a=roty/57.29; %degrees to radians
    F6=cube.vertices;
    F6=[cos(a)*F6(:,1)+sin(a)*F6(:,3), F6(:,2), -sin(a)*F6(:,1)+cos(a)*F6(:,3)];
    cube.vertices=F6;
  
    a=rotx/57.29; %degrees to radians
    F6=cube.vertices;
    F6=[F6(:,1), cos(a)*F6(:,2)-sin(a)*F6(:,3), sin(a)*F6(:,2)+cos(a)*F6(:,3)];
    cube.vertices=F6;

    F6=cube.vertices;
    F6=[F6(:,1)+transx, F6(:,2)+transy, F6(:,3)+transz];
    cube.vertices=F6;
  
    part6=cube;

else
    p6(2)=0;
end

cube.vertices=[ 0 0 0; 1 0 0; 1 1 0; 0 1 0;0 0 1; 1 0 1; 1 1 1; 0 1 1;] ;
cube.faces=[ 1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8; ] ;
cube.vertices = cube.vertices * 2 - 1;

cylinder.vertices=[-1,0,-1;-1,0,0;-0.500000000000000,-0.856380903262898,-1;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,-1;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,-1;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,-1;-0.500000000000000,0.856380903262898,0;0,-1,-1;0,-1,0;0,1,-1;0,1,0;0.500000000000000,-0.856380903262898,-1;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,-1;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,-1;0.856380903262898,-0.500000000000000,0;1,0,-1;1,0,0;0.856380903262898,0.500000000000000,-1;0.856380903262898,0.500000000000000,0;-1,0,1;-0.500000000000000,-0.856380903262898,1;-0.856380903262898,-0.500000000000000,1;-0.856380903262898,0.500000000000000,1;-0.500000000000000,0.856380903262898,1;0,-1,1;0,1,1;0.5000000000000
00,-0.856380903262898,1;0.500000000000000,0.856380903262898,1;0.856380903262898,-0.500000000000000,1;1,0,1;0.856380903262898,0.500000000000000,1;];
cylinder.faces=[3,4,5;5,4,6;5,6,1;1,6,2;1,2,7;7,2,8;7,8,9;9,8,10;11,12,3;3,12,4;9,10,13;13,10,14;11,15,12;12,15,16;17,13,18;18,13,14;15,19,16;16,19,20;19,21,20;20,21,22;21,23,22;22,23,24;23,17,24;24,17,18;4,26,6;6,26,27;6,27,2;2,27,25;2,25,8;8,25,28;8,28,10;10,28,29;12,30,4;4,30,26;10,29,14;14,29,31;12,16,30;30,16,32;18,14,33;33,14,31;16,20,32;32,20,34;20,22,34;34,22,35;22,24,35;35,24,36;24,18,36;36,18,33;];

sphere.vertices=[0,0,-1;-0.500000000000000,-0.500000000000000,-0.686740068930804;-0.500000000000000,-0.686740068930804,-0.500000000000000;-0.686740068930804,-0.500000000000000,-0.500000000000000;-0.500000000000000,0,-0.856380903262898;-0.856380903262898,0,-0.500000000000000;-0.500000000000000,0.500000000000000,-0.686740068930804;-0.686740068930804,0.500000000000000,-0.500000000000000;-0.500000000000000,0.686740068930805,-0.500000000000000;0,-0.500000000000000,-0.856380903262898;0,-0.856380903262898,-0.500000000000000;0,0.500000000000000,-0.856380903262898;0,0.856380903262898,-0.500000000000000;0.500000000000000,-0.500000000000000,-0.686740068930804;0.500000000000000,-0.686740068930804,-0.500000000000000;0.500000000000000,0,-0.856380903262898;0.500000000000000,0.500000000000000,-0.686740068930804;0.500000000000000,0.686740068930805,-0.500000000000000;0.686740068930805,-0.500000000000000,-
0.500000000000000;0.856380903262898,0,-0.500000000000000;0.686740068930805,0.500000000000000,-0.500000000000000;-1,0,0;-0.500000000000000,-0.856380903262898,0;-0.856380903262898,-0.500000000000000,0;-0.856380903262898,0.500000000000000,0;-0.500000000000000,0.856380903262898,0;0,-1,0;0,1,0;0.500000000000000,-0.856380903262898,0;0.500000000000000,0.856380903262898,0;0.856380903262898,-0.500000000000000,0;1,0,0;0.856380903262898,0.500000000000000,0;-0.500000000000000,-0.686740068930804,0.500000000000000;-0.686740068930804,-0.500000000000000,0.500000000000000;-0.856380903262898,0,0.500000000000000;-0.686740068930804,0.500000000000000,0.500000000000000;-0.500000000000000,0.686740068930805,0.500000000000000;0,-0.856380903262898,0.500000000000000;0,0.856380903262898,0.500000000000000;0.500000000000000,-0.686740068930804,0.500000000000000;0.500000000000000,0.686740068930805,0.500000000000000;0.6
86740068930805,-0.500000000000000,0.500000000000000;0.856380903262898,0,0.500000000000000;0.686740068930805,0.500000000000000,0.500000000000000;-0.500000000000000,-0.500000000000000,0.686740068930805;-0.500000000000000,0,0.856380903262898;-0.500000000000000,0.500000000000000,0.686740068930805;0,-0.500000000000000,0.856380903262898;0,0,1;0,0.500000000000000,0.856380903262898;0.500000000000000,-0.500000000000000,0.686740068930805;0.500000000000000,0,0.856380903262898;0.500000000000000,0.500000000000000,0.686740068930805;];
sphere.faces=[2,3,4;2,4,5;5,4,6;5,6,7;7,6,8;7,8,9;2,10,3;3,10,11;1,10,2;1,2,5;1,5,7;1,7,12;7,9,12;12,9,13;10,14,11;11,14,15;10,1,16;10,16,14;16,1,12;16,12,17;12,13,17;17,13,18;14,19,15;14,16,19;19,16,20;16,17,20;20,17,21;17,18,21;3,23,4;4,23,24;4,24,6;6,24,22;6,22,8;8,22,25;8,25,9;9,25,26;11,27,3;3,27,23;13,9,28;28,9,26;15,29,11;11,29,27;18,13,30;30,13,28;15,19,29;29,19,31;19,20,31;31,20,32;20,21,32;32,21,33;21,18,33;33,18,30;23,34,24;24,34,35;24,35,22;22,35,36;25,22,36;25,36,37;25,37,26;26,37,38;23,27,39;23,39,34;26,38,28;28,38,40;27,29,41;27,41,39;30,28,42;42,28,40;29,31,41;41,31,43;31,32,43;43,32,44;32,33,45;32,45,44;33,30,45;45,30,42;34,46,35;35,46,36;36,46,47;36,47,37;37,47,48;37,48,38;39,49,34;34,49,46;46,49,47;47,49,50;47,50,48;48,50,51;40,38,51;51,38,48;41,52,39;39,52,49;49,52,50;50,52,53;53,54,50;50,54,51;42,40,54;54,40,51;41,43,52;43,44,52;52,44,53;44,45,53;53,45,54;45,42,54;];

if (p7(2)==1) %part ID 1=UnitCylinder
    res=p7(3); %resolution
    scalex=p7(4); %diameter of cylinder in x direction
    scaley=p7(5); %diameter of cylinder in y direction
    scalez=p7(6); %length of cylinder
    rotx=p7(7); %rotation about x axis
    roty=p7(8); %rotation about y axis
    rotz=p7(9); %rotation about z axis
    transx=p7(10); %translation in x direction
    transy=p7(11); %translation in y direction
    transz=p7(12); %translation in z direction
    
    V7=cylinder.vertices;
    V7=[V7(:,1)*scalex, V7(:,2)*scaley, V7(:,3)*scalez];
    cylinder.vertices=V7;

    a=rotz/57.29; %degrees to radians
    V7=cylinder.vertices;
    V7=[cos(a)*V7(:,1)-sin(a)*V7(:,2), sin(a)*V7(:,1)+cos(a)*V7(:,2), V7(:,3)];
    cylinder.vertices=V7;

    a=roty/57.29; %degrees to radians
    V7=cylinder.vertices;
    V7=[cos(a)*V7(:,1)+sin(a)*V7(:,3), V7(:,2), -sin(a)*V7(:,1)+cos(a)*V7(:,3)];
    cylinder.vertices=V7;
  
    a=rotx/57.29; %degrees to radians
    V7=cylinder.vertices;
    V7=[V7(:,1),cos(a)*V7(:,2)-sin(a)*V7(:,3),sin(a)*V7(:,2)+cos(a)*V7(:,3)];
    cylinder.vertices=V7;

    V7=cylinder.vertices;
    V7=[V7(:,1)+transx, V7(:,2)+transy, V7(:,3)+transz];
    cylinder.vertices=V7;
    
    part7=cylinder;
    
elseif (p7(2)==2) %part ID 2=UnitSphere
    res=p7(3); %resolution
    scalex=p7(4); %diameter of sphere in x direction
    scaley=p7(5); %diameter of sphere in y direction
    scalez=p7(6); %diameter of sphere in z direction
    rotx=p7(7); %rotation about x axis
    roty=p7(8); %rotation about y axis
    rotz=p7(9); %rotation about z axis
    transx=p7(10); %translation in x direction
    transy=p7(11); %translation in y direction
    transz=p7(12); %translation in z direction
    
    M7=sphere.vertices;
    M7=[M7(:,1)*scalex, M7(:,2)*scaley, M7(:,3)*scalez];
    sphere.vertices=M7;

    a=rotz/57.29; %degrees to radians
    M7=sphere.vertices;
    M7=[cos(a)*M7(:,1)-sin(a)*M7(:,2),sin(a)*M7(:,1)+cos(a)*M7(:,2), M7(:,3)];
    sphere.vertices=M7;

    a=roty/57.29; %degrees to radians
    M7=sphere.vertices;
    M7=[cos(a)*M7(:,1)+sin(a)*M7(:,3), M7(:,2), -sin(a)*M7(:,1)+cos(a)*M7(:,3)];
    sphere.vertices=M7;
  
    a=rotx/57.29; %degrees to radians
    M7=sphere.vertices;
    M7=[M7(:,1), cos(a)*M7(:,2)-sin(a)*M7(:,3), sin(a)*M7(:,2)+cos(a)*M7(:,3)];
    sphere.vertices=M7;

    M7=sphere.vertices;
    M7=[M7(:,1)+transx, M7(:,2)+transy, M7(:,3)+transz];
    sphere.vertices=M7;
 
    part7=sphere;

elseif (p7(2)==3) %part ID 3=UnitCube
    res=p7(3); %resolution
    scalex=p7(4); %length in x direction
    scaley=p7(5); %length in y direction
    scalez=p7(6); %length in z direction
    rotx=p7(7); %rotation about x axis
    roty=p7(8); %rotation about y axis
    rotz=p7(9); %rotation about z axis
    transx=p7(10); %translation in x direction
    transy=p7(11); %translation in y direction
    transz=p7(12); %translation in z direction
   
    F7=cube.vertices;
    F7=[F7(:,1)*scalex, F7(:,2)*scaley, F7(:,3)*scalez];
    cube.vertices=F7;

    a=rotz/57.29; %degrees to radians
    F7=cube.vertices;
    F7=[cos(a)*F7(:,1)-sin(a)*F7(:,2), sin(a)*F7(:,1)+cos(a)*F7(:,2),F7(:,3)];
    cube.vertices=F7;

    a=roty/57.29; %degrees to radians
    F7=cube.vertices;
    F7=[cos(a)*F7(:,1)+sin(a)*F7(:,3),F7(:,2), -sin(a)*F7(:,1)+cos(a)*F7(:,3)];
    cube.vertices=F7;
  
    a=rotx/57.29; %degrees to radians
    F7=cube.vertices;
    F7=[F7(:,1), cos(a)*F7(:,2)-sin(a)*F7(:,3),sin(a)*F7(:,2)+cos(a)*F7(:,3)];
    cube.vertices=F7;

    F7=cube.vertices;
    F7=[F7(:,1)+transx, F7(:,2)+transy, F7(:,3)+transz];
    cube.vertices=F7;
  
    part7=cube;

else
    p7(2)=0;
end
 
if (p7(2)>0)
Out=[part1,part2,part3,part4,part5,part6,part7];
count=renderpatch(part1)+renderpatch(part2)+renderpatch(part3)+renderpatch(part4)+renderpatch(part5)+renderpatch(part6)+renderpatch(part7);
elseif (p6(2)>0)
Out=[part1,part2,part3,part4,part5,part6];
count=renderpatch(part1)+renderpatch(part2)+renderpatch(part3)+renderpatch(part4)+renderpatch(part5)+renderpatch(part6);
elseif (p5(2)>0)
Out=[part1,part2,part3,part4,part5];
count=renderpatch(part1)+renderpatch(part2)+renderpatch(part3)+renderpatch(part4)+renderpatch(part5);
elseif (p4(2)>0)
Out=[part1,part2,part3,part4];
count=renderpatch(part1)+renderpatch(part2)+renderpatch(part3)+renderpatch(part4);
elseif (p3(2)>0)
Out=[part1,part2,part3];
count=renderpatch(part1)+renderpatch(part2)+renderpatch(part3);
elseif (p2(2)>0)
Out=[part1,part2];
count=renderpatch(part1)+renderpatch(part2);
else
Out=part1;
count=renderpatch(part1);
end

   axis off;
   grid on
   daspect([1 1 1])
   light('position',[10,-10,10])
   rotate3d on

Subject: find distance along complicated geometric surface

From: Walter Roberson

Date: 18 May, 2010 14:29:40

Message: 18 of 27

Meagan wrote:

> other problem is that when the airplane is made, most of the shapes
> intersect so a lot of the vertices for a particular component are not
> necessarily on the outside of the plane.

Ick. Finding the "outside" of complicated figures is a pain.

An airplane is NOT a convex shape! For convex figures, every line that
goes directly between any two points on the figure must be contained
entirely within the figure: that is not the case on an airplane, where a
line between the tips of the wings and the nose would go through space
rather than within the figure. Algorithms for finding the shortest path
on a convex shape are not the same as finding the shortest path on a
concave shape.

Subject: find distance along complicated geometric surface

From: Meagan

Date: 18 May, 2010 14:43:04

Message: 19 of 27

Walter Roberson <roberson@hushmail.com> wrote in message <ovxIn.11220$wV.3023@newsfe11.iad>...
> Meagan wrote:
>
> > other problem is that when the airplane is made, most of the shapes
> > intersect so a lot of the vertices for a particular component are not
> > necessarily on the outside of the plane.
>
> Ick. Finding the "outside" of complicated figures is a pain.
>
> An airplane is NOT a convex shape! For convex figures, every line that
> goes directly between any two points on the figure must be contained
> entirely within the figure: that is not the case on an airplane, where a
> line between the tips of the wings and the nose would go through space
> rather than within the figure. Algorithms for finding the shortest path
> on a convex shape are not the same as finding the shortest path on a
> concave shape.

Sorry I guess I was confused as to what you meant by convex and concave.

Subject: find distance along complicated geometric surface

From: Bruno Luong

Date: 18 May, 2010 14:57:04

Message: 20 of 27

Walter Roberson <roberson@hushmail.com> wrote in message <ovxIn.11220$wV.3023@newsfe11.iad>...
>
> Algorithms for finding the shortest path
> on a convex shape are not the same as finding the shortest path on a
> concave shape.

What is a concave shape? Is it a complement of a convex shape? Or do you mean a non-convex shape?

Bruno

Subject: find distance along complicated geometric surface

From: Matt J

Date: 18 May, 2010 15:55:21

Message: 21 of 27

Perhaps the following article can help. I haven't read it, though


R. Kimmel and N. Kiryati. Finding shortest paths on surfaces by fast global approximation and precise local refinement. International Journal of Pattern Recognition and Artificial Intelligence, 10(6):643-656, 1996.

Subject: find distance along complicated geometric surface

From: Rune Allnor

Date: 18 May, 2010 17:34:06

Message: 22 of 27

On 17 Mai, 22:45, "Meagan " <meagan.mussel...@l-3com.com> wrote:
> hi all,
>
> I am working on a project and I have hit a wall and could really use some help.
>
> I am trying to find the distance between two points on a shape that is defined by vertices and faces. I can't just use the distance formula because I need to make sure that the entire distance between the two points is in contact with the shape surface. I have no idea how to start doing this or if this task is even possible so any input would be greatly appreciated.

The *idea* is simple, assuming you already have a surface
consisting of edges and vertices, like in a triangulation:
Track the points where you cross edges, and compute the
distance between consecutive edge crossings (possibly also
accounting for path waypoints). The total distance along the
path will be the cumulant sum of all such edge-to-edge
distances.

*Implementing* this simple idea is not at all simple. If you
want this to be fast, you will need to have access to certain
awkward data structures internal to the triangulated surface.

A naive method might be to check the path for intersections
with all the edges in the triangulation. This method will
find all the edge crossings, and will also allow you to sort
the edge crossing points to the order in which they are traversed
along the path. However, this method will be very slow; maybe
intolerably slow.

Rune

Subject: find distance along complicated geometric surface

From: Matt J

Date: 18 May, 2010 17:51:04

Message: 23 of 27

Rune Allnor <allnor@tele.ntnu.no> wrote in message <99c70f0a-1227-4506-b3a1-ab85568616c8@l6g2000vbo.googlegroups.com>...
 
> The *idea* is simple, assuming you already have a surface
> consisting of edges and vertices, like in a triangulation:
> Track the points where you cross edges, and compute the
> distance between consecutive edge crossings (possibly also
> accounting for path waypoints). The total distance along the
> path will be the cumulant sum of all such edge-to-edge
> distances.
========

The difficulty though, Rune, is that you don't have the shortest-distance path a priori. It's finding the shortest path, not computing its length, which is the challenge.

Subject: find distance along complicated geometric surface

From: Rune Allnor

Date: 18 May, 2010 18:52:48

Message: 24 of 27

On 18 Mai, 19:51, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> Rune Allnor <all...@tele.ntnu.no> wrote in message <99c70f0a-1227-4506-b3a1-ab8556861...@l6g2000vbo.googlegroups.com>...
> > The *idea* is simple, assuming you already have a surface
> > consisting of edges and vertices, like in a triangulation:
> > Track the points where you cross edges, and compute the
> > distance between consecutive edge crossings (possibly also
> > accounting for path waypoints). The total distance along the
> > path will be the cumulant sum of all such edge-to-edge
> > distances.
>
> ========
>
> The difficulty though, Rune, is that you don't have the shortest-distance path a priori. It's finding the shortest path, not computing its length, which is the challenge.

I can't see how being able to compute the distance along
a genrral path isn't helpful:

1) Select an initial path
2) Compute the distance along this path
3) Modify the path in a way that likely
   might shorten the distance
4) Repeat from 2

until no significant improvements in path distance can be made.
Of course, one can only hope to find a local solution to this
problem.

Rune

Subject: find distance along complicated geometric surface

From: ImageAnalyst

Date: 18 May, 2010 19:08:33

Message: 25 of 27

On May 18, 1:51 pm, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> The difficulty though, Rune, is that you don't have the shortest-distance path a priori. It's finding the shortest path, not computing its length, which is the challenge.
----------------------------------------------------------------------------------------------------------
Isn't that were dynamic programming, Djikstra, A*, or your other
favorite search-path/optimization routine comes in? They will
efficiently find the shortest path (given all the possible points
along the way) without searching through an infinite number of
possibilities. For example, if you're tracking a bright ridge (e.g. a
blood vessel in an angiogram) and you know two image columns that it
passes through, you can use dynamic programming to find the path
between the left column and the right column that has the highest
average brightness - without actually searching through all the
bazillion possible paths. (I know because I've done it, although it
was 24 years ago) In Meagan's case she could use the cumulative
length of the path for the criteria rather than the average path
brightness as in my example. However with my limited spare time, I'm
not about to tackle her airplane data, though I think it could be
done.

Subject: find distance along complicated geometric surface

From: Walter Roberson

Date: 18 May, 2010 19:24:39

Message: 26 of 27

Bruno Luong wrote:

> The geodesic on polytope is a straight line when the polytope is
> flattenen (making a cuts on the cell edges, excepted those crossed by
> the geoderic, then change the dihedral angle between to neighboring
> faces to pi, i.e., 180 degree).

Yes, _conceptually_, if we were to take a model of the geometry in which we
replaced all of the faces with small rigid plates, and replaced all of the
edges with hinges, then if we were to pick up the model by the two points and
pull those points apart as far as they go (assuming no elasticity!), then the
geodesic would be the shortest path along the "top" (assuming the other pieces
will be pulled downwards by gravity.)

Subject: find distance along complicated geometric surface

From: Mike Karr

Date: 21 Jun, 2010 14:26:51

Message: 27 of 27

Meagan wrote:
> hi all,
>
> I am working on a project and I have hit a wall and could really use
> some help.
> I am trying to find the distance between two points on a shape that is
> defined by vertices and faces. I can't just use the distance formula
> because I need to make sure that the entire distance between the two
> points is in contact with the shape surface. I have no idea how to start
> doing this or if this task is even possible so any input would be
> greatly appreciated.
> Thanks,
> Meagan

Google "geodesic triangulated surface" and you will find a body of
research done on your problem. Add "MATLAB" to the search and it will
take you implementations in the file exchange of MATLAB central.

I have not investigated either the research or the MATLAB code.

hth,
mike

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us