How could I color specific patches in an alphaShape ?

조회 수: 7 (최근 30일)
Hugo Bitard
Hugo Bitard 2022년 8월 31일
답변: VINAYAK LUHA 2023년 10월 6일
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
Hugo Bitard
Hugo Bitard 2022년 8월 31일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

VINAYAK LUHA
VINAYAK LUHA 2023년 10월 6일
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

카테고리

Help CenterFile Exchange에서 Bounding Regions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by