[MATLAB]: How would I mathematically and visually reproduce the 3D surface of the new King's Cross 'Western Concourse'? - matlab

Anyone have any starting tips for me? I want to learn from this (ie Don't want to be lazy and have someone answer this for me).
I would like to develop my understanding of mathematical 3D surfaces. My own personal project is to produce a 3D surface/graph of the concourse structure in MATLAB.
I found a link with good pictures of its geometry here. I am not expecting to get it 100% perfectly but I'd like to come close!
At the end of this exercise I would like to have a mathematical definition of the geometry as well as a visual representation of the surface. This can involve cartesian equations, parametric equations, matrices, etc.
Any help would be very much appreciated!

To give some specific advice for MATLAB:
I would load in the 'section' image from the web page you have linked, and display this in a MATLAB figure window. You can then try plotting lines over the top until you find one that fits nicely. So you might do something like:
A = imread('~/Desktop/1314019872-1244-n364-1000x707.jpg');
imshow(A)
hold on
axis on
%# my guess at the function - obviously not a good fit
x = [550:900];
plot(x, 0.0001*x.^2 + 300)
Of course, you might want to move the position of the origin or crop the picture and so on.
As an arguably better alternative to this trial-and-error method, you could trace the outline of the section (e.g by clicking points with something like ginput), and then use one of MATLAB's curve-fitting tools (e.g. fit) to fit a function to the data.
The final 3D shape looks to me (at a casual glance) to be a 3D revolution of the section shape around a central axis. Use of a cylindrical coordinate system could therefore be a good idea.
The final plotting of your 3D shape could be done with a function such as surf or mesh.

I would start by defining a function that defines for each x, y coordinate whether there is a point z, and if so with which altitude.
The shape reminds me a bit of a log or a square root.

Related

Constructing voxels of a 3D cube in MATLAB

I want to construct a 3D cube in MATLAB. I know that the units of any 3D shape are voxels not pixels. Here is what I want to do,
First, I want to construct a cube with some given dimensions x, y, and z.
Second, according to what I understand from different image processing tutorials, this cube must consists of voxels (3D pixels). I want to give every voxel an initial color value, say gray.
Third, I want to access every voxel and change its color, but I want to distinguish the voxels that represent the faces of the cube from those that represent the internal region. I want to axis every voxel by its position x,y, z. At the end, we will end up with a cube that have different colors regions.
I've searched a lot but couldn't find a good way to implement that, but the code given here seems very close in regard to constructing the internal region of the cube,
http://www.mathworks.com/matlabcentral/fileexchange/3280-voxel
But it's not clear to me how it performs the process.
Can anyone tell me how to build such a cube in MATLAB?
Thanks.
You want to plot voxels! Good! Lets see how we can do this stuff.
First of all: yeah, the unit of 3D shapes may be voxels, but they don't need to be. You can plot an sphere in 3D without it being "blocky", thus you dont need to describe it in term of voxels, the same way you don't need to describe a sinusoidal wave in term of pixels to be able to plot it on screen. Look at the figure below. (same happens for cubes)
If you are interested in drawing voxels, I generally would recommend you to use vol3D v2 from Matlab's FEX. Why that instead of your own?
Because the best (only?) way of plotting voxels is actually plotting flat square surfaces, 6 for each cube (see answer here for function that does that). This flat surfaces will also create some artifacts for something called z-fighting in computer graphics. vol3D actually only plots 3 surfaces, the ones looking at you, saving half of the computational time, and avoiding ugly plotting artifacts. It is easy to use, you can define colors per voxel and also the alpha (transparency) of each of them, allowing you to see inside.
Example of use:
% numbers are arbitrary
cube=zeros(11,11,11);
cube(3:9,3:9,3:9)=5; % Create a cube inside the region
% Boring: faces of the cube are a different color.
cube(3:9,3:9,3)=2;
cube(3:9,3:9,9)=2;
cube(3:9,3,3:9)=2;
cube(3:9,9,3:9)=2;
cube(3,3:9,3:9)=2;
cube(9,3:9,3:9)=2;
vold3d('Cdata',cube,'alpha',cube/5)
But yeah, that still looks bad. Because if you want to see the inside, voxel plotting is not the best option. Alphas of different faces stack one on top of the other and the only way of solving this is writing advanced computer graphics ray tracing algorithms, and trust me, that's a long and tough road to take.
Very often one has 4D data, thus data that contains 3D location and a single data for each of the locations. One may think that in this case, you really want voxels, as each of them have a 3D +color, 4D data. Indeed! you can do it with voxels, but sometimes its better to describe it in some other ways. As an example, lets see this person who wanted to highlight a region in his/hers 4D space (link). To see a bigger list I suggest you look at my answer in here about 4D visualization techniques.
Lets try wits a different approach than the voxel one. Lets use the previous cube and create isosurfaces whenever the 4D data changes of value.
iso1=isosurface(cube,1);
iso2=isosurface(cube,4);
p1=patch(iso1,'facecolor','r','facealpha',0.3,'linestyle','none');
p2=patch(iso2,'facecolor','g','facealpha',1,'linestyle','none');
% below here is code for it to look "fancy"
isonormals(cube,p1)
view(3);
axis tight
axis equal
axis off
camlight
lighting gouraud
And this one looks way better, in my opinion.
Choose freely and good plotting!

Matlab computing wrong surface normals?

I have a big FEM model from where I can get the "surface" of the model, say the elements and vertex that define the surface of that FEM model. For plotting purposes (nice plots are always a win!) I want to plot it nicely. My approach is just to use
lungs.Vertex=vtx;
lungs.Faces=fcs;
patch(lungs,'facecolor','r','edgecolor','none')
NOTE: I need edgecolor none, as this is 4D data and different FEM have different triangulation, if edges are plotted user will be dizzy.
However this will output everything in a really plain red color, which is not nice (as it can not show the complexity of the figure, which are lungs, for the attentive to details).
therefore I decided to use ligthing:
camlight; camlight(-80,-10); lighting phong;
But again, this is not entirely correct. Actually it seems that the patch nromals are not computed correctly by Matlab.
My supposition is that maybe the patches are not always defined counter-clockwise and therefore some normals go to the wrong direction. However that is something its not straightforward to check.
Anyone has a similar problem, or ahint of how should I aproach this problem in order to have a nice surface ploted here?
EDIT
Just for the shake of plotting, here is the result obtained with #magnetometer answer:
If your model gives you outward oriented normals, you can re-sort the faces of your model so that Matlab can properly calculate it's own normals. The following function works if you have triangular faces and outward oriented normals:
function [FaceCor,nnew]=SortFaces(Faces,Normals,Vertices)
FaceCor=Faces;
nnew=Normals*0;
for jj=1:size(Faces,1)
v1=Vertices(Faces(jj,3),:)-Vertices(Faces(jj,2),:);
v2=Vertices(Faces(jj,2),:)-Vertices(Faces(jj,1),:);
nvek=cross(v2,v1); %calculate normal vectors
nvek=nvek/norm(nvek);
nnew(jj,:)=nvek;
if dot(nvek,Normals(jj,:))<0
FaceCor(jj,:)=[Faces(jj,3) Faces(jj,2) Faces(jj,1)];
nnew(jj,:)=-nvek;
end
end
If your FEM model doesn't give you outward directed normals, one way could be to reconstruct the surface using e.g. a crust algorithm that gives you outward directed normals or correctly oriented patches.
EDIT:
As you don't have normals, the only solution that comes to my mind is to reconstruct the surface. This implementation of the crust algorithm has worked well for me in the past. All you need to do is:
[FacesNew,NormalsNew]=MyRobustCrust(Vertices);
If I remember correctly, FacesNew are not yet oriented counterclockwise, but you can use the SortFaces algorithm I posted above to correct for this, as you now have correctly oriented face normals, i.e. run:
[FaceCor,~]=SortFaces(FacesNew,NormalsNew,Vertices)
If you use Matlab's reducepatch (e.g. reducedmodel=reducepatch(fullmodel,reduction); ) to reduce the number of vertices, you will have to reconstruct the surface again, as reducepatch doesn't seem to keep the correct orientation of the patches.

List of point into smooth curve (airfoil shape)

I have a list of 200 points I garnered from a graph digitization software I would like to transform into a smooth curve and then into Solidworks.
My points form an ellipse (airfoil shape to be more precise), so the commands I've tried in Matlab didn't have a circular curve.
My issues are:
* Obtaining a smooth curve that doesn't necessarily pass through all points, smooth being motus operandi.
* Being able to have a elliptical curve
* Somehow being able to export this curve into Solidwords
If anyone knows the right software, command line or anything that could get me started, I would be extremely thankful.
imacube
I've used Solid Works before. It's a very powerful tool. There should be some way to draw a curved spline through these points, such as a cubic spline.
If you are using a standard(ish) airfoil, then you can use a variety of tools to plot the points without having to use a graph digitization software.
Javafoil, for instance, is one of those. Even if you know the characteristics of your airfoil, you can use this to give you a smooth set of points.
Again, if your airfoil is a naca 4-series, then these are governed by a set of equations.
But I take it that the airfoil you want a more complicated one. Let me know if I can help anymore.

How can I interpolate points in 3D space?

I have 3D space. And I know for example N points in this space (x1,y1,z1), (x2,y2,z2)..., (xn,yn,zn). I want to interplolate points, that is different from this. How can I do this in Matlab?
interp3 may help you. Here is the documentation.
As always, there are questions left unanswered by your one line query.
If the data is of the form where there is a functional relationship z(x,y), (or y(x,z) or x(y,z)) then you might potentially be able to use one of the interpolation tools. Thus, suppose you have data that lies on a lattice in the (x,y) plane, thus some value of z at each point in that lattice. In this case, you can use interp2.
Alternatively, if the data is scattered, but there is some single valued functional relationship z(x,y) that you don't have, but it is some continuous function. Infinite first derivatives are a problem too here. In this case, assuming that you have data that at least fills some convex domain in the (x,y) plane, you can still interpolate a value of z. For this, use griddata, or TriScatteredInterp. Or you might use my own gridfit tool, as found on the file exchange.
Next, the way you describe the data, I'm not at all positive that you have something in one of the above forms. For example, if your data lies along some curved path in this 3D domain, and you wish to interpolate points along that curved arc can be done using my interparc tool, also found on the file exchange.
One last case that people often seem to have when they talk about interpolation of a spatial set like this, is just a general surface, that they wish to build a neatly interpolated, smooth surface. It might be something as simple as the surface of a sphere, or something wildly more complex. (These things are never simple.) For this, you might be able to use a convex hull to approximate something, if it is a closed convex surface. More complex surfaces might require a tool like CRUST, although I have no implementation of it I can offer to you. Google will help you there, if that is what you need.
The point of all this is, how you interpolate your data depends on what form the data is in, what it represents, and the shape of the relationship you will be interpolating.

Mesh Generation in MATLAB

Is there any subroutine, in MATLAB, that takes in a list of points, and return me a good mesh that I can use to show to my colleagues, such as this?
Actually, all I need is just a simple 2D mesh generator that takes in a series of X, Y coordinates (that defines the boundary of the area), and give me back a list of elements that can mesh that area well. I can do the rest by using MATLAB command to interpolate the Z value.
Edit : I am not interested to use MATLAB to produce the above looking plot. I am interested in using a MATLAB library to obtain a list of elements so that when I plot those element myself (not in MATLAB itself; but in my own C# program), I can obtain this meshed surface.
PS: I know there is this DistMesh, but I am looking for something simpler - something built-in direct in MATLAB perhaps. And no, meshgrid is not mesh generation.
It sounds like you want to create a finite element mesh, starting with a set of points defining a boundary of a region and then generating a triangular mesh that creates more points within that region. I don't think there's a "simple" solution for this problem.
The closest "built-in" solution would probably be the Partial Differential Equation Toolbox, specifically some of the Geometry Algorithms like INITMESH and REFINEMESH.
The link you gave to DistMesh appears to be another good solution. There are also a few submissions on the MathWorks File Exchange that you could take a look at:
MESH2D by Darren Engwirda
Finite Element Toolbox 2.1 by Rasmus Anthin
That picture looks exactly like the one from the griddata documentation. The example in there looks like what you want.
SFTOOL will easily make the picture that you show.
A thin-plate spline, e.g., TPAPS, should also do the job.
I think the user-created 'gridfit' is the best I've come across for a single surface, much better/prettier than griddata.
Mesh generation as in Delaunay Triangulation + Steiner Points? There is a builtin Delaunay function in MATLAB.
If your surface is the z=f(x,y) form you can use:
http://www.advancedmcode.org/how-to-plot-a-coloured-surface-from-3d-scatter.html
If your surface is concave look for surface reconstruction on the same website.