LoginSignup
2
3

More than 5 years have passed since last update.

幾何学 > STL(ASCII)に使うsurface normalを計算 > 外積 > numpyのcross()がある

Last updated at Posted at 2016-05-28

STLの資料
のp4などを見ると、ASCII形式でSTLファイルを作れるようだ。

その場合、3角形のsurface normalは計算しないといけないだろうか。

code v0.1

python実装を見つけた。
http://stackoverflow.com/questions/1984799/cross-product-of-two-vectors-in-python

実行してみた
http://ideone.com/KjukfJ

import numpy as np

def cross(a, b):
    c = [a[1]*b[2] - a[2]*b[1],
         a[2]*b[0] - a[0]*b[2],
         a[0]*b[1] - a[1]*b[0]]

    return c

a = np.array([0.0, 1.0, 0.0])
b = np.array([1.0, 0.0, 0.0])
c = cross(a,b)

print c
結果
Success time: 0.11 memory: 25336 signal:0
[0.0, 0.0, -1.0]

それっぽいのが出ているようだ(幾何学は苦手)。

STLのファイルにするには以下の手順になりそうだ。

  1. 頂点座標を設定
  2. 頂点座標からベクトルを作成
  3. 外積

code v0.2

numpyを使うとしたら、cross関数が用意されていた。

import numpy as np

def cross(a, b):
    c = [a[1]*b[2] - a[2]*b[1],
         a[2]*b[0] - a[0]*b[2],
         a[0]*b[1] - a[1]*b[0]]

    return c

a = np.array([0.0, 1.0, 0.0])
b = np.array([1.0, 0.0, 0.0])
c = cross(a,b)
d = np.cross(a,b)

print c
print d
[0.0, 0.0, -1.0]
[ 0.  0. -1.]
2
3
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
2
3