How could I color specific patches in an alphaShape ?

11 views (last 30 days)
Hi,
I am working on a 3D set of data so I have 3 Vectors of size (1,23497). I then use the alphaShape tool to create a 3D plot of my data with different solids in 3D, and I want to color those object differentely according to their elevation. I thus created a color vector of size (1,23497), containing only zeros and ones. I then try to color the data but i get the error "Number of colors must equal number of vertices".
How could I do what i want ?
Thanks a lot
plot(data,'FaceColor','interp','CData',Color)
  1 Comment
Hugo Bitard
Hugo Bitard on 31 Aug 2022
Okay I found a solution, but help still needed, I had to use the boundaryFacets function to extract boundaries of the alphaShape, then compute my color vector from those vertices. I can then color the "trisurf" object with the color vector computed through that way...
I would be happy if someone had another solution !
[tri, xyz] = boundaryFacets(alphaShap);
Color=zeros(size(xyz,1),1);
for i=1:size(xyz,1)
if xyz(i,3)<-Y2(I)+2
Color(i,:)=1;
end
end
colormap('parula')
trisurf(tri,xyz(:,1),xyz(:,2),xyz(:,3),Color,'FaceAlpha',0.3)
axis equal

Sign in to comment.

Answers (1)

VINAYAK LUHA
VINAYAK LUHA on 6 Oct 2023
Hi Hugo,
It is my understanding that you have obtained an 3D object using MATLAB “alphaShape” function and want to know how to apply a colormap to its constituent patches based on some elevation parameter. I have considered the elevation parameter as the values along the x-axis.
Follow the below steps to color the specific patches in “alphaShape” object:
  1. Loop over the patches and find the mean of each x coordinate of constituent vertices. Let it be “xcoords(i)”, for the ith patch.
  2. Sort the “xcoords” vector and extract the original occurring indices of the elements. Let it be “ids”.
  3. Create a vector of RGB triplets from the MATLAB “parula” colormap. Let it be “colorVector”
  4. Loop over the patches again and color each one using colors from the “colorVector” in the order specified by “ids” variable.
Refer to the following code snippet to color specific patches in the “alphaShape” object:
numPoints = 100;
x = rand(1, numPoints);
y = rand(1, numPoints);
z = rand(1, numPoints);
points = [x(:), y(:), z(:)];
alpha = 0.5;
alphaShapeObj = alphaShape(points, alpha);
boundaryFaces = alphaShapeObj.boundaryFacets;
boundaryVertices = alphaShapeObj.Points;
numPatches = size(boundaryFaces, 1);
xcoords = zeros(1,numPatches);
for i = 1:numPatches
patchVertices = boundaryVertices(boundaryFaces(i, :), :);
patchFaces = 1:size(patchVertices, 1);
xcoords(i) = sum(patchVertices(:,1));
xcoords(i) = xcoords(i)/size(patchVertices,1);
end
[~,ids]=sort(xcoords);
colorVector = parula(numPatches);
axis off;
for i = 1:numPatches
patchVertices = boundaryVertices(boundaryFaces(ids(i), :), :);
patchFaces = 1:size(patchVertices, 1);
patch('Faces', patchFaces, 'Vertices', patchVertices, 'FaceColor',colorVector(ids(i),:));
hold on;
end
colorbar
xlabel("x")
axis equal;
Refer to the following documentations to know more about the functions used in the code snippet.
Hope this helps in coloring the specific patches in the “alphaShape” object.
Regards,
Vinayak Luha

Categories

Find more on Bounding Regions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!