作者binjon (舍得斌~ 冲冲冲!!)
看板MATLAB
标题[运算] 3D中关於patch 用圆座标取点
时间Tue May 20 16:23:57 2014
各位大大,小弟用Matlab转档案
目前是用Matlab吃进来 stl 档案然後在用圆座标去取上面的vertex 三角形的点。
下面的范例是小弟尝试用
(phi,theta,r)圆座标去取点,其中的phi和theta 为固定几个角度,所以这边
是要取r 的值出来,最後再去描点(3dplot)再看结果。
小弟方法分两个步骤,
第一个就是patch.face 规定的三个点围成的平面和我所要描的
(phi,theta,r)对於圆心(0 0 0 )拉出来的线求交点。
第二个就是用这个交点作线性规划,找交点位於三点围成的三角形之类,才是我判别
点是在vertex 的平面上面,这样就可以确定 r 值了。
第二个线性规划上面,我是用交点 vs.三角形分开为一条线
和一个点作,如下图右边线为三角形的两个点所成,然後交点只要在三角形内,则d1>d2
以此类推,我只要作三次都符合,就可以知道交点在这个face 内了。
d1 |
三角形上之点。-----------|
d2 |
交点。-----|
|
当然小弟是觉得这个方法很烂,目前抓的效果也没很好
不知道有没有更聪明的办法或是现成的func来确定3D下,
"点是否在一个三角形内哩?"
感谢 !!
(如果有人想try我这版本的code,请在跟我讲哩,在给您详细pattern之类的)
input_pat = strcat('./data/',pattern,'.stl');
fv = stlread(input_pat);
%% Render
% The model is rendered with a PATCH graphics object. We also add some dynamic
% lighting, and adjust the material properties to change the specular
% highlighting.
patch(fv,'FaceColor', [0.8 0.8 1.0], ...
'EdgeColor', 'none', ...
'FaceLighting', 'gouraud', ...
'AmbientStrength', 0.15);
% Add a camera light, and tone down the specular highlighting
camlight('headlight');
material('dull');
% Fix the axes scaling, and set a nice view angle
axis('image');
%view([0 90]);
view([-175 55]);
size_pts = size(fv.vertices);
number_points = size_pts(1);
angle_C = zeros(1,number_points);
angle_G = zeros(1,number_points);
intensity = zeros(1,number_points);
% All points here
cart_points = fv.vertices;
% All triangle series here
face = fv.faces;
%======================
% Debug
figure,plot3(cart_points(1:end,1),cart_points(1:end,2),cart_points(1:end,3));
%======================
for i = 1:number_points
[angle_C(i),angle_G(i),intensity(i)] = cart2sph(cart_points(i,1),cart_points(i,2),cart_points(i,3));
end
%=========
% Shift the Angle to fir IES format
%==============================
angle_C = angle_C*180/pi+180;
angle_G = angle_G*180/pi+90;
% G value: vertical angle, theta
G_index = [0.00 2.00 4.00 6.00 8.00 10.00 12.00 14.00 16.00 18.00...
20.00 22.00 24.00 26.00 28.00 30.00 32.00 34.00 36.00 38.00...
40.00 42.00 44.00 46.00 48.00 50.00 52.00 54.00 56.00 58.00...
60.00 62.00 64.00 66.00 68.00 70.00 72.00 74.00 76.00 78.00...
80.00 82.00 84.00 86.00 88.00 90.00 92.00 94.00 96.00 98.00...
100.00 102.00 104.00 106.00 108.00 110.00 112.00 114.00 116.00 118.00...
120.00 122.00 124.00 126.00 128.00 130.00 132.00 134.00 136.00 138.00...
140.00 142.00 144.00 146.00 148.00 150.00 152.00 154.00 156.00 158.00...
160.00 162.00 164.00 166.00 168.00 170.00 172.00 174.00 176.00 178.00...
180.00];
% C value:horizontal angle, phi
C_index = [90.00 105.00 120.00 135.00...
150.00 165.00 180.00 195.00 210 225.00 240.00 255.00 270.00 285.00...
300.00 315.00 330.00 345.00 0.00 15.00 30.00 45.00 60.00 75.00 90];
length_C = size(C_index);
length_G = size(G_index);
length_output = length_C(2)*length_G(2);
output_intensity = zeros(1,length_output);
intesity_test = 1;
length_intensity = size(intesity_test);
test_points = zeros(3,length_intensity(2));
test_points1 = zeros(3,200);
tb_int1 = zeros(1,200);
tb_int = zeros(length_intensity(2),number_points/3);
%=========================
% Debug
sampled_x = zeros(1,number_points);
sampled_y = zeros(1,number_points);
sampled_z= zeros(1,number_points);
inter_point = zeros(3,number_points/3);
dist_point = zeros(1,number_points/3);
thd_dist = 0.01;
for i = 1:length_C(2)
for j = 1:length_G(2)
tmp_angle_C = (C_index(i) - 180)*pi/180;
tmp_angle_G = (G_index(j) - 90)*pi/180;
[test_points(1,intesity_test),test_points(2,intesity_test),test_points(3,intesity_test)] = ...
sph2cart(tmp_angle_C,tmp_angle_G,intesity_test);
for k =1:number_points/3
inter_point(:,k) = plane_line_intersection(cart_points(face(k,1),:),cart_points(face(k,2),:),cart_points(face(k,3),:),...
0,0,0,test_points(1),test_points(2),test_points(3));
% Linear Program
a_flag_direction = (round(test_points(1,intesity_test))*round(inter_point(1,k))>=0)&&...
(round(test_points(2,intesity_test))*round(inter_point(2,k))>=0)&&...
(round(test_points(3,intesity_test))*round(inter_point(3,k))>=0);
d_ori1 = dist(inter_point(:,k)',cart_points(face(k,1),:),cart_points(face(k,2),:));
d_ver1 = dist(cart_points(face(k,3),:),cart_points(face(k,1),:),cart_points(face(k,2),:));
d_ori2 = dist(inter_point(:,k)',cart_points(face(k,2),:),cart_points(face(k,3),:));
d_ver2 = dist(cart_points(face(k,1),:),cart_points(face(k,2),:),cart_points(face(k,3),:));
d_ori3 = dist(inter_point(:,k)',cart_points(face(k,1),:),cart_points(face(k,3),:));
d_ver3 = dist(cart_points(face(k,2),:),cart_points(face(k,1),:),cart_points(face(k,3),:));
if(d_ori1<(d_ver1-thd_dist))&&(d_ori2<(d_ver2-thd_dist))&&(d_ori3<(d_ver3-thd_dist))&&a_flag_direction
%disp(['k=',int2str(k),',[x y z]= ',int2str(inter_point(:,k)')]);
%disp(['P1= ',int2str(cart_points(face(k,1),:)),', P2= ',int2str(cart_points(face(k,2),:)),', P3= ',int2str(cart_points(face(k,3),:))]);
dist_point(k) = sum(d_ori1+d_ori2+d_ori3);
%disp(['dist_total = ',int2str(dist_point(k))]);
end
end
[B1,index1] = max(dist_point);
[tmp_C,tmp_G,tmp_intensity] = cart2sph(inter_point(1,index1),inter_point(2,index1),inter_point(3,index1));
output_intensity((i-1)*length_G(2)+j)= tmp_intensity;
sampled_x((i-1)*length_G(2)+j)= inter_point(1,index1);
sampled_y((i-1)*length_G(2)+j)= inter_point(2,index1);
sampled_z((i-1)*length_G(2)+j)= inter_point(3,index1);
end
end
figure,...
plot3(sampled_x,sampled_y,sampled_z);
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.112.17.237
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/MATLAB/M.1400574240.A.C17.html
1F:→ YoursEver:改用barycentric coor.;直接看交点是不是落在三角形内. 05/20 17:04
2F:→ binjon:感谢楼上,2014新func ? 我在试一下 !! 05/21 11:15