0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「空間上の3点をxy平面上に移動する変換行列を教えて下さい。」をAI先生に聞きました。sympyのソースコードで。

Last updated at Posted at 2025-04-25

(本日の)ChatGPT先生へ

質問リスト

# 質問リスト
1. 空間上の3点をxy平面上に移動する変換行列を教えて下さい。
2. sympyのソースコードを教えて下さい。
3. 上記の条件で、ユーザー定義関数で、入力の引数は、sympyのPointでお願いします。
4. ソースコードを1つにして下さい。
5. 私の質問をMarkdown形式でリスト化して出力して。Markdown形式でリストからアスタリスクは削除して。1行目に、# 質問リスト の文字を出力して。最後の質問も追加して下さい。追番を追加して下さい。Markdown形式で。

実行結果の概要:
このスクリプトを実行すると、与えた3点の張る平面を XY 平面に一致させるための 4×4の同次変換行列 T が表示されます。
必要なら、この行列で他の点を変換する方法も追加できます!お気軽にどうぞ。

import sympy as sp
from sympy.geometry import Point3D

def get_plane_to_xy_transform(A: Point3D, B: Point3D, C: Point3D):
    # ベクトル AB, AC を計算
    u = sp.Matrix(B - A)
    v = sp.Matrix(C - A)

    # 法線ベクトル(平面の法線)
    n = u.cross(v)
    n_unit = n / n.norm()

    # Z軸ベクトル
    z_axis = sp.Matrix([0, 0, 1])

    # 回転軸(n と Z軸の外積)
    axis = n_unit.cross(z_axis)
    axis_norm = axis.norm()

    # 回転角
    cos_theta = n_unit.dot(z_axis)
    theta = sp.acos(cos_theta)

    # Rodriguesの回転行列
    def rotation_matrix(axis, theta):
        axis = axis / axis.norm()
        x, y, z = axis
        c = sp.cos(theta)
        s = sp.sin(theta)
        C = 1 - c
        return sp.Matrix([
            [x*x*C + c,   x*y*C - z*s, x*z*C + y*s],
            [y*x*C + z*s, y*y*C + c,   y*z*C - x*s],
            [z*x*C - y*s, z*y*C + x*s, z*z*C + c  ]
        ])

    # 回転行列(軸がゼロ=すでにXY平面の場合は単位行列)
    R = sp.eye(3) if axis_norm == 0 else rotation_matrix(axis, theta)

    # 平行移動ベクトル(点Aを原点へ)
    t = -R * sp.Matrix(A)

    # 同次変換行列(4x4)
    T = R.row_join(t)
    T = T.col_join(sp.Matrix([[0, 0, 0, 1]]))

    return T

# === 使用例 ===
if __name__ == "__main__":
    A = Point3D(1, 2, 3)
    B = Point3D(4, 5, 6)
    C = Point3D(7, 8, 10)

    T = get_plane_to_xy_transform(A, B, C)

    print("変換行列 T:")
    sp.pprint(T, use_unicode=True)

Gemini先生へ

copilot先生へ

いつもの? sympyの実行環境と 参考のおすすめです。

(テンプレート)

いつもと違うおすすめです。

wikipedia

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?