0

I want to show, with Matlab, a temperature distribution on an object surface.

I've got a 3D data in the form of (x, y, z, V) vectors. I would like to show this object in Matlab, with the colour representing the local total "value".

I can export the object as an STL file. It can be shown easily using the STL plotting (see stldemo):

fv = stlread('file.stl');
patch(fv, 'EdgeColor', 'none', 'FaceLighting', 'gouraud', 'AmbientStrength', 0.15, 'FaceColor', [0.8 0.8 1.0]);
camlight('headlight');
material('dull');

To colour it according to (x,y,z,V), I need to attach each (x, y, z) point to a vertex in the patch (the nearest one would work). If there are many (x,y,z) points for which a single STL vertex is the nearest, I add up the corresponding V values for that vertex.

The number of vertices is thousands. The number of (x, y, z) points is also large. So doing a loop through (x, y, z) points and then an internal loop over vertices to find the nearest one (which involves calculating distances between points) is out of question. Is there any smart way to do it quickly?

Note: I cannot control the location of the data points, they are defined by an external program. The STL points are controlled by another external program. So I have to marry two different point sets.

Here is the code illustrating what I want to achieve, with 4 vertices and 3 data points:

% Create patch
figure;
p = patch;
colorbar

p.Vertices = [...
    0, 0, 0; ...
    1, 0, 0; ...
    1, 1, 0;
    0, 1, 0];
p.Faces = [ ...
    1, 2, 3; ...
    1, 3, 4];

% Data points
x = [0.1, 0.1, 0.25];
y = [0.01, 0.02, 0.75];
z = [0.01, 0.2, -0.01];
v = [1, 1, 1];

p.FaceVertexCData = zeros(size(p.Vertices, 1), 1);
% Point 1 (0.1, 0.01, 0.01) is closest to vertex 1 (0, 0, 0). Its value
% goes to vertex 1.
p.FaceVertexCData(1) = p.FaceVertexCData(1) + v(1);
% Point 2 (0.1, 0.02, 0.2) is also closest to vertex 1 (0, 0, 0). Its
% value also goes to vertex 1
p.FaceVertexCData(1) = p.FaceVertexCData(1) + v(2);
% Point 3 (0.25, 0.75, -0.01) is closest to vertex 4 (0, 1, 0). Its power
% goes to vertex 4.
p.FaceVertexCData(4) = p.FaceVertexCData(4) + v(3);
% Other vertices are left with 0.

p.FaceColor = 'interp';
3
  • 3
    This is really the kind of case where an mvce would greatly help. Try to supply a minimal subset of data that people can work with, otherwise they would have to generate sample data and not everyone will do that ...
    – Hoki
    Jul 7, 2015 at 19:45
  • Also, how are the temperature data calculated ? Could you feed the patch points to the function which calculate the temperature distribution ? That would make the whole exercise a lot simpler (just a couple of settings on CData and colormap)
    – Hoki
    Jul 7, 2015 at 20:34
  • @Hoki, I added an example which hopefully explains what I am trying to achieve. And I cannot calculate the temperature and the predefined points, the (x, y, z) points are defined by an external modelling software in its own way.
    – texnic
    Jul 8, 2015 at 13:00

1 Answer 1

2

Attaching a volume scalar value (of Temperature in your case) of a point to a neighbouring point is a tricky exercise, requires complex for loops and defining special case rules (in your case you wanted to attach the value of 2 different points to the same patch vertex, what if the 2 values to attach are different? Do you average? discard ?).

A safer approach is to re-interpolate your temperature field over your object surface. the function griddata can do that for you.

First I had to define a scalar field. Since I do not have your temperature data, I use the flow function from Matlab. I generated a scalar field the same ways than in this article: flow data.
This gave me a scalar field v (flow value but let's say it's your temperature) at for every coordinates x, y, z.

flow

Then I created and introduced a 3D patch which will be your object. I chose a sphere but any 3D patch will work the same way. The code to get the sphere as a patch is borrowed from surf2patch
you will have to offset and inflate the sphere to get it exactly as in the figure below

simplesphere

Now is the interesting bit. In the following code, v is the value of the scalar field (temperature for you) at the coordinates x, y, z.

%% // Extract patch vertices coordinates in separate variables
xp = fv.vertices(:,1) ;
yp = fv.vertices(:,2) ;
zp = fv.vertices(:,3) ;

%% // interpolate the temperature field over the patch coordinates
Tpv = griddata(x,y,z,v,xp,yp,zp) ;

%% // Set the patch color data to the new interpolated temperature
set(hp,'FaceVertexCData',Tpv) ;

And your object surface is now at the right interpolated temperature:
you can delete the slice plane if you want to observe the patch alone

obectfinal

4
  • Your solution is great, but it won't work for me because of the incorrect problem statement. I oversimplified it without noticing that it changes the meaning. My field is not temperature, it's the "source of thermal radiation". I need to calculate (and show) the load on the surface. So basically, each point in the original (x,y,z,V) set can be considered a source of radiation. This is why it's OK to add multiple values to a single STL vertex. Of course, I am shifting the source position, but that's acceptable here (I can also easily refine the STL grid). I've edited the question.
    – texnic
    Jul 8, 2015 at 16:12
  • But then it will be completely dependant on the mesh size of your STL grid (large grid->2 sources A and B assigned to the same point, fine grid-> A and B assigned to distinct points (may be even separated by many points between them). Wouldn't it be better to calculate the radiation field in the volume based on the sources position and intensity, then interpolate this on the object surface ?
    – Hoki
    Jul 8, 2015 at 17:03
  • The size is thousands of points in both cases... Having read your answer, I was actually thinking in the same direction. But I am afraid there is a problem. Imagine that I have locally very high radiation point and high density of STL grid. If I apply the load directly to the STL grid, it will correctly show the hotspot. If I first create a field grid and this grid will be locally not dense enough, I'll introduce an error that may be significant. I'll check it though.
    – texnic
    Jul 8, 2015 at 17:24
  • Sure, the more coarse your interpolation grid, the more smoothed/averaged your field will be. That goes for any interpolation process, but I would still prefer having control on this grid size (because it's part of your model) than on the one coming from the STL file (an external object introduced into the model).
    – Hoki
    Jul 8, 2015 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.