前提・実現したいこと
使用言語:python
やりたいこと
x, y, zの3座標から楕円の近似をする.
プログラムコードは英語の質問サイトの方より参考にした.
コードないの「conf.」が何を表しているかわかりません.
わかる方,教えていただきたいです.
よろしくお願いいたします.
該当のソースコード
python
1from scipy.spatial 2import ConvexHull, convex_hull_plot_2d 3import numpy as np 4from numpy.linalg import eig, inv 5 6def ls_ellipsoid(xx,yy,zz): 7 #finds best fit ellipsoid. Found at http://www.juddzone.com/ALGORITHMS/least_squares_3D_ellipsoid.html 8 #least squares fit to a 3D-ellipsoid 9 # Ax^2 + By^2 + Cz^2 + Dxy + Exz + Fyz + Gx + Hy + Iz = 1 10 # 11 # Note that sometimes it is expressed as a solution to 12 # Ax^2 + By^2 + Cz^2 + 2Dxy + 2Exz + 2Fyz + 2Gx + 2Hy + 2Iz = 1 13 # where the last six terms have a factor of 2 in them 14 # This is in anticipation of forming a matrix with the polynomial coefficients. 15 # Those terms with factors of 2 are all off diagonal elements. These contribute 16 # two terms when multiplied out (symmetric) so would need to be divided by two 17 18 # change xx from vector of length N to Nx1 matrix so we can use hstack 19 x = xx[:,np.newaxis] 20 y = yy[:,np.newaxis] 21 z = zz[:,np.newaxis] 22 23 # Ax^2 + By^2 + Cz^2 + Dxy + Exz + Fyz + Gx + Hy + Iz = 1 24 J = np.hstack((x*x,y*y,z*z,x*y,x*z,y*z, x, y, z)) 25 K = np.ones_like(x) #column of ones 26 27 #np.hstack performs a loop over all samples and creates 28 #a row in J for each x,y,z sample: 29 # J[ix,0] = x[ix]*x[ix] 30 # J[ix,1] = y[ix]*y[ix] 31 # etc. 32 33 JT=J.transpose() 34 JTJ = np.dot(JT,J) 35 InvJTJ=np.linalg.inv(JTJ); 36 ABC= np.dot(InvJTJ, np.dot(JT,K)) 37 38 # Rearrange, move the 1 to the other side 39 # Ax^2 + By^2 + Cz^2 + Dxy + Exz + Fyz + Gx + Hy + Iz - 1 = 0 40 # or 41 # Ax^2 + By^2 + Cz^2 + Dxy + Exz + Fyz + Gx + Hy + Iz + J = 0 42 # where J = -1 43 eansa=np.append(ABC,-1) 44 45 return (eansa) 46 47def polyToParams3D(vec,printMe): 48 #gets 3D parameters of an ellipsoid. Found at http://www.juddzone.com/ALGORITHMS/least_squares_3D_ellipsoid.html 49 # convert the polynomial form of the 3D-ellipsoid to parameters 50 # center, axes, and transformation matrix 51 # vec is the vector whose elements are the polynomial 52 # coefficients A..J 53 # returns (center, axes, rotation matrix) 54 55 #Algebraic form: X.T * Amat * X --> polynomial form 56 57 if printMe: print('\npolynomial\n',vec) 58 59 Amat=np.array( 60 [ 61 [ vec[0], vec[3]/2.0, vec[4]/2.0, vec[6]/2.0 ], 62 [ vec[3]/2.0, vec[1], vec[5]/2.0, vec[7]/2.0 ], 63 [ vec[4]/2.0, vec[5]/2.0, vec[2], vec[8]/2.0 ], 64 [ vec[6]/2.0, vec[7]/2.0, vec[8]/2.0, vec[9] ] 65 ]) 66 67 if printMe: print('\nAlgebraic form of polynomial\n',Amat) 68 69 #See B.Bartoni, Preprint SMU-HEP-10-14 Multi-dimensional Ellipsoidal Fitting 70 # equation 20 for the following method for finding the center 71 A3=Amat[0:3,0:3] 72 A3inv=inv(A3) 73 ofs=vec[6:9]/2.0 74 center=-np.dot(A3inv,ofs) 75 if printMe: print('\nCenter at:',center) 76 77 # Center the ellipsoid at the origin 78 Tofs=np.eye(4) 79 Tofs[3,0:3]=center 80 R = np.dot(Tofs,np.dot(Amat,Tofs.T)) 81 if printMe: print('\nAlgebraic form translated to center\n',R,'\n') 82 83 R3=R[0:3,0:3] 84 R3test=R3/R3[0,0] 85 # print('normed \n',R3test) 86 s1=-R[3, 3] 87 R3S=R3/s1 88 (el,ec)=eig(R3S) 89 90 recip=1.0/np.abs(el) 91 axes=np.sqrt(recip) 92 if printMe: print('\nAxes are\n',axes ,'\n') 93 94 inve=inv(ec) #inverse is actually the transpose here 95 if printMe: print('\nRotation matrix\n',inve) 96 return (center,axes,inve) 97 98 99#let us assume some definition of x, y and z 100 101#get convex hull 102surface = np.stack((conf.x,conf.y,conf.z), axis=-1) 103hullV = ConvexHull(surface) 104lH = len(hullV.vertices) 105hull = np.zeros((lH,3)) 106for i in range(len(hullV.vertices)): 107 hull[i] = surface[hullV.vertices[i]] 108hull = np.transpose(hull) 109 110#fit ellipsoid on convex hull 111eansa = ls_ellipsoid(hull[0],hull[1],hull[2]) #get ellipsoid polynomial coefficients 112print("coefficients:" , eansa) 113center,axes,inve = polyToParams3D(eansa,False) #get ellipsoid 3D parameters 114print("center:" , center) 115print("axes:" , axes) 116print("rotationMatrix:", inve) 117
コード引用:https://stackoverflow.com/questions/58501545/python-fit-3d-ellipsoid-oblate-prolate-to-3d-points
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/14 14:41