How to draw a sphere in matlab and patch it in a 3D plot? - matlab

I have a 3D plot and I want to put a sphere in a designed position. How can I create a sphere? I expected to use patch? Can anyone help me to do this please?

You can follow this:
Consider the center is at [c1,c2,c3]. The number of faces as r.
[x,y,z] = sphere(r);
surf(x+c1, y+c2, z+c3)
These two lines of code are enough to plot a sphere using the surf command.
For instance, if C=[2,2,2] and r=30 the result is as follows:
This is a sphere of radius 1 centered at [2,2,2]. To have a sphere with an arbitrary radius R, you should multiply the [x,y,z] values by R before adding the center.

Related

How to determine the points of intersection between a 3d circle and polyhedron in matlab

I have a workspace containing a polyhedral shape so I sliced the workspace into different 3D circles. For each circle, it is either there occurs an intersection with the polyhedral or not. Is it possible to determine if the circle intersects with the polyhedron in MATLAB and if yes, how can I determine the polygon formed by the intersection of a 3d circle with a polyhedral shape?
NB: 3D circle is a circular shaped object in the 3D space. For ease of understanding, I have attached a MATLAB plot:
There are 3 circles and four polyhedral shapes in the plot.
Any help will be appreciated

Patching Delaunay Triangles

I have a number of 2D points x and y, and I need to color delaunay triangles, which have edges that exceed a certain limit. However I can't figure out how to write it down so that it would compare each edge of each triangle and add it to a vector of triangles that need to be patched. The end result is supposed to be a 3D surface, where color depends on the Z coordinate, except for the triangles that have been patched white. Any help would be great!
You can try a contour plot. With your triangles and z values find the average of each triangle.

I need to fit a best circle to the 3D data in matlab

Basically, I have a many irregular circle on the ground in the form of x,y,z coordinates (of 200*3 matrix). but I want to fix a best circle in to the data of x,y,z coordinates (of 200*3 matrix).
Any help will be greatly appreciated.
I would try using the RANSAC algorithm which finds the parameters of your model (in your case a circle) given noisy data. The algorithm is quite easy to understand and robust against outliers.
The wikipedia article has a Matlab example for fitting a line but it shouldn't be too hard to adapt it to fit a circle.
These slides give a good introduction to the RANSAC algorithm (starting from page 42). They even show examples for fitting a circle.
Though this answer is late, I hope this helps others
To fit a circle to 3d points
Find the centroid of the 3d points (nx3 matrix)
Subtract the centroid from the 3D points.
Using RANSAC, fit a plane to the 3D points. You can refer here for the function to fit plane using RANSAC
Apply SVD to the 3d points (nx3 matrix) and get the v matrix
Generate the axes along the RANSAC plane using the axes from SVD. For example, if the plane norm is along the z-direction, then cross product between the 1st column of v matrix and the plane norm will generate the vector along the y-direction, then the cross product between the generated y-vector and plane norm will generate a vector along the x-direction. Using the generated vectors, form a Rotation matrix [x_vector y_vector z_vector]
Multiply the Rotation matrix with the centroid subtracted 3d points so that the points will be parallel to the XY plane.
Project the points to XY plane by simply removing the Z-axes from the 3d points
fit a circle using Least squares circle fit
Rotate the center of the circle using the inverse of the rotation matrix obtained from step 5
Translate back the center to the original location using the centroid
The circle in 3D will have the center, the radius will be the same as the 2D circle we obtained from step 8, the circle plane will be the RANSAC plane we obtained from step 3

Contour triangulation

I write my study and is stuck when i try triangulate the contour of surface. When it is in 2D its ok. When it in 3D a have trouble with triangle angle detection, i tried with:
Triange have 3 Vertices v1,v2,v3
I create 2 vectors(vec21, vec23) from v2v1 and v2v3
then vec21 x vec23 and obtain a det of matrix
on the stand which I define Span angle
I also check if edges do not crossing and if any point isnt in area of triangle.
But when it in 3D i choose point around polygon then this metod didn't work
Points of contour i want triangulate to flat polygon: https://docs.google.com/open?id=0Bw5-VXnqutXBckRJMGNJMW9JaXc
Bad resoult: https://docs.google.com/open?id=0Bw5-VXnqutXBMzV5elIxX1FaeDQ
In 2d:
Points on 2D :https://docs.google.com/open?id=0Bw5-VXnqutXBWVE4bWJsZ09mOVk
Good resoults:https://docs.google.com/open?id=0Bw5-VXnqutXBdGFKM2Z4UnFRdXc
Where i made mistake? Can u explain me this?
Greetings!
PS. Im interested in algoithm at 2 last case:http://www.cosy.sbg.ac.at/~held/projects/triang/triang.html
Typically one would use a Delaunay Triangulation for the 2D case. For the 3D case you can project the points to 2D, triangulate and project the triangles back to 3D. This will of course only work if the patch to be triangulated can be projected to 2D (without selfintersections).

Creating a cylinder with axis centered differently

I know Matlab has a function called cylinder to create the points for a cylinder when number of points along the circumference, and the radius length. What if I don't want a unit cylinder, and also don't want it to center at the default axis (for example along z-axis)? What would be the easiest approach to create such a cylinder? Thanks in advance.
The previous answer is fine, but you can get matlab to do more of the work for you (because the results of cylinder separate x,y,z components you need to work a little to do the matrix multiplication for the rotation). To have the center of base of the cylinder at [x0 y0 z0], scaled by [xf yf xf] (use xf=yf unless you want an elliptic cylinder), use:
[x y z] = cylinder;
h=mesh(x*xf+x0,y*yf+y0,z*zf+z0)
If you also want to rotate it so it isn't aligned along the z-axis, use rotate. For example, to rotate about the x-axis by 90 degrees, so it's aligned along the y-axis, use:
rotate(h,[1 0 0],90)
Multiply the points by your favourite combination of a scaling matrix, a translation matrix, and a rotation matrix.