Mathematica中文论坛-非官方

标题: 豆粑粑 matlab contour3D 散点的点密度 3D等值面 [打印本页]

作者: meatball1982    时间: 2017-3-16 15:25
标题: 豆粑粑 matlab contour3D 散点的点密度 3D等值面
本帖最后由 meatball1982 于 2017-3-16 15:26 编辑

空间散点,根据点密度画图。在3维空间中等高值曲面显示。
这个问题经常被问到,其它软件也有相似的实现。
记录下来,给以后的我\footnote{如果能活到再次使用这类图的时候}以帮助。
数据,m文件附件上传。


cv_357.txt.tar (271 KB, 下载次数: 0)


work_contour_3D.m.tar (4.5 KB, 下载次数: 0)


复制代码
  1. clear all
  2. clc
  3. close all

  4. %% outline
  5. % plot scatter points, surf, countour3D

  6. %% main
  7. % load data ---------------------------------------------------------------
  8. dat=load('cv_357.txt');
  9. n=length(dat(:,1));

  10. x=dat(:,1);
  11. y=dat(:,2);
  12. z=dat(:,3);

  13. % grid size ---------------------------------------------------------------
  14. n_cv=30;
  15. cv_g=linspace(-pi,pi,n_cv);

  16. cv_int=0.5; % 2 calculate point denisty, half of cubic size
  17. cv_x=cv_g;
  18. cv_y=cv_g;
  19. cv_z=cv_g;

  20. [cv_X,cv_Y,cv_Z]=meshgrid(cv_g,cv_g,cv_g);

  21. % points denisty ----------------------------------------------------------
  22. for i=1:n_cv
  23.     ind_x= (x>=cv_x(i)-cv_int) & (x<cv_x(i)+cv_int);
  24.     for j=1:n_cv
  25.         ind_y= (y>=cv_y(j)-cv_int) & (y<cv_y(j)+cv_int);
  26.         for k=1:n_cv
  27.             ind_z= (z>=cv_z(k)-cv_int) & (z<cv_z(k)+cv_int);
  28.             p_n = sum(ind_x & ind_y & ind_z);
  29.             cv_V(j,i,k)= p_n;
  30.         end
  31.     end
  32. end

  33. % %% 4 conv
  34. % save Mat_post_3D_contour.mat
  35. %
  36. % load Mat_post_3D_contour.mat
  37. % 2 smooth the points density, change 1 to 3, or 5 or 7 according to your
  38. % data
  39. cv_V=smooth3(cv_V,'box',1);

  40. %% original points --------------------------------------------------------
  41. figure(1)
  42. plot3(x,y,z,'.')
  43. axis([-pi pi -pi pi -pi pi])

  44. axis equal tight
  45. view(20,20)
  46. box on
  47. ax=gca;
  48. ax.BoxStyle = 'full';

  49. %% surf version -----------------------------------------------------------

  50. figure(2)
  51. hold on
  52. for i=1:3:n_cv
  53.     surf( cv_X(:,:,i),cv_Y(:,:,i),cv_Z(:,:,i),cv_V(:,:,i),...
  54.         'edgecolor','none','facealpha',0.05+0.01*i)
  55. end
  56. axis([-pi pi -pi pi -pi pi])
  57. colormap(flipud(jet))

  58. axis equal tight
  59. view(20,20)
  60. box on
  61. ax=gca;
  62. ax.BoxStyle = 'full';

  63. %% contour3D --------------------------------------------------------------
  64. figure(3)
  65. n_layer=9; % layer number
  66. col_mm=flipud(jet(n_layer)); % color map

  67. lev_add=2.3.^[2:n_layer+1]; % which layer you want to plot
  68. lev_beg=80;                 % begin layer

  69. % contour 3D with each layer
  70. for i = 1: n_layer
  71.     mm_lev=lev_beg+lev_add(i);
  72.     p1 = patch(isosurface(cv_V,mm_lev),'FaceColor',col_mm(i,:),...
  73.     'EdgeColor','none','FaceAlpha',0.1+0.015*i);
  74.     isonormals(cv_V,p1);
  75. end
  76. % other setting
  77. axis equal
  78. view(20,20);
  79. axis vis3d equal tight
  80. camlight headlight;
  81. lighting phong
  82. set(gca,'GridLineStyle',':')
  83. box on
  84. ax=gca;
  85. ax.BoxStyle = 'full';
  86. grid on



  87. %% logs
  88. % typed by : mm
  89. % mod : 2017年 03月 16日 星期四 15:23:22 CST
  90. % contact me : meatball1982@163.com
  91. %
复制代码








欢迎光临 Mathematica中文论坛-非官方 (http://ilovemathematica.com/) Powered by Discuz! X3.2