質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

2088閲覧

【楕円の近似(Python)】英語質問サイトより,コードを引用したが,中身がわかりません.

y08

総合スコア1

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/09/14 13:40

前提・実現したいこと

使用言語: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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

https://stackoverflow.com/questions/58501545/python-fit-3d-ellipsoid-oblate-prolate-to-3d-pointsから引用したとのことですが、

質問者の"The starting data are a set of x, y and z coordinates (cartesian coordinates). "
から始まる質問文を素直に読めば
「与えられた点群にフィットする楕円体を算出するための係数を求めたいんだけど」っていう質問なわけです。

ですから、、confっていうのは、その「所与の点群」の座標に該当するんじゃないでしょうか。

なので

# let us assume some definition of x, y and z

の下に下記のコードを入れてみればよい。Configurationの中身はテキトー。

class Configuration(): def __init__(self, px ,py, pz): self.x = px self.y = py self.z = pz conf = Configuration([0,1,1,0.8],[1,0,-2,0.9],[-1,0,1.5,0.4])

出力結果:

coefficients: [-9. 1.5 0.6875 1.75 0. 0.4375 3.75 0.1875 -0.25 -1. ] center: [ 0.1880728 -0.20839408 0.24812539] axes: [0.27735711 0.65571365 1.04693673] rotationMatrix: [[-0.99658785 0.08251814 -0.00184948] [-0.07994754 -0.97062807 -0.22691307] [-0.0205196 -0.22599095 0.97391326]]

投稿2020/09/14 14:35

編集2020/09/15 02:53
sfdust

総合スコア1135

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y08

2020/09/14 14:41

理解しました. あとは自力で頑張って見ます.丁寧な回答,ありがとうございました.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問