[運算] 3D中關於patch 用圓座標取點
各位大大,小弟用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://www.ptt.cc/bbs/MATLAB/M.1400574240.A.C17.html
→
05/20 17:04, , 1F
05/20 17:04, 1F
→
05/21 11:15, , 2F
05/21 11:15, 2F
→
05/21 11:33, , 3F
05/21 11:33, 3F
MATLAB 近期熱門文章
PTT數位生活區 即時熱門文章