Open3Dの使い方：点と法線の変更


pythonで点群処理できる[Open3D](http://www.open3d.org)の探検．[Open3Dの使い方：読み込みと表示，点と法線の取得](https://qiita.com/tttamaki/items/2be476cc15fa39601243)の続き．

内部で点の情報はEigenで保持している様子．
ndarrayのように代入やインデキシングをしようとするとダメ．
取得は`np.asarray()`で変換し，設定は`py3d.Vector3dVector()`で変換する

#使い方探検

ここではPCL tutorialのpcdファイルを利用．

```python:open3d
import sys
sys.path.append("../..") # Open3D/build/lib/ へのパス
import numpy as np
import py3d

pcd3 = py3d.read_point_cloud('milk_cartoon_all_small_clorox.pcd')
print("has points?", pcd3.has_points())
point_array = np.asarray(pcd3.points)
print(point_array.shape, "points:\n", point_array)
 print("has color?", pcd3.has_colors())
print("colors:", np.asarray(pcd3.colors))
py3d.draw_geometries([pcd3], "pcd3", 640, 480)


# カラーへアクセス
print("color point 0:", pcd3.colors[0])
print("color point 0..10:", pcd3.colors[:10]) # このままだとダメ．エラーではないが
print("color point 0..10:", np.asarray(pcd3.colors[:10])) # ndarrayへ変換
colors3 = np.asarray(pcd3.colors)
print("color point 0..10:", colors3[:10]) # ndarrayにしてからインデキシング
pcd3.colors[50000] = [255,0,0] # 代入はこれでもいける．でもインデキシングはだめ
colors3[:10000] = np.array([255, 0, 0]) # 最初の10000点を赤にする
pcd3.colors = py3d.Vector3dVector(colors3) # 型変換してから代入
py3d.draw_geometries([pcd3], "pcd3 with some points colored in red", 640, 480)


# 点へアクセス
print("point 0:", pcd3.points[0])
points3 = np.asarray(pcd3.points)
print("points 0..10:", points3[:10]) # ndarrayにしてからインデキシング
points3[10000:20000] += np.array([1.0, -0.9, 0.5]) # 一部の点の座標を移動
pcd3.points = py3d.Vector3dVector(points3) # 型変換してから代入
py3d.draw_geometries([pcd3], "pcd3 with some points moved", 640, 480)
```


##結果

```text:
has points? True
(241407, 3) points:
 [[ 0.96564567  0.84802288 -1.99199998]
 [ 0.98647338  0.86292291 -2.02699995]
 [ 0.9844715   0.85781431 -2.0150001 ]
 ...
 [ 0.27123341 -0.2192429  -0.51499999]
 [ 0.2727429  -0.2196686  -0.51599997]
 [ 0.27372569 -0.2196686  -0.51599997]]
has color? True
colors: [[0.34509804 0.33333333 0.26666667]
 [0.35686275 0.32941176 0.25882353]
 [0.34509804 0.32941176 0.25098039]
 ...
 [0.27058824 0.15686275 0.21176471]
 [0.2745098  0.14901961 0.11764706]
 [0.24313725 0.16862745 0.02745098]]
```

![スクリーンショット 2018-03-19 21.37.22.png](https://qiita-image-store.s3.amazonaws.com/0/44835/c58d4506-4e10-d944-bf1f-f08ecff2bd25.png)


```text:点に色ついた
color point 0: [0.34509804 0.33333333 0.26666667]
color point 0..10: std::vector<Eigen::Vector3d> with 10 elements.
Use numpy.asarray() to access data.
color point 0..10: [[0.34509804 0.33333333 0.26666667]
 [0.35686275 0.32941176 0.25882353]
 [0.34509804 0.32941176 0.25098039]
 [0.33333333 0.32941176 0.2627451 ]
 [0.34509804 0.32941176 0.27843137]
 [0.35686275 0.33333333 0.23921569]
 [0.35686275 0.3372549  0.20392157]
 [0.36078431 0.33333333 0.19607843]
 [0.36078431 0.32941176 0.18823529]
 [0.36078431 0.3254902  0.20392157]]
color point 0..10: [[0.34509804 0.33333333 0.26666667]
 [0.35686275 0.32941176 0.25882353]
 [0.34509804 0.32941176 0.25098039]
 [0.33333333 0.32941176 0.2627451 ]
 [0.34509804 0.32941176 0.27843137]
 [0.35686275 0.33333333 0.23921569]
 [0.35686275 0.3372549  0.20392157]
 [0.36078431 0.33333333 0.19607843]
 [0.36078431 0.32941176 0.18823529]
 [0.36078431 0.3254902  0.20392157]]
```

![スクリーンショット 2018-03-19 21.37.58.png](https://qiita-image-store.s3.amazonaws.com/0/44835/1a9cb38c-aaca-c2ed-9c93-8096f7a509d4.png)


```text:点が動いた
point 0: [ 0.96564567  0.84802288 -1.99199998]
points 0..10: [[ 0.96564567  0.84802288 -1.99199998]
 [ 0.98647338  0.86292291 -2.02699995]
 [ 0.9844715   0.85781431 -2.0150001 ]
 [ 0.99419528  0.86292291 -2.02699995]
 [ 0.99805629  0.86292291 -2.02699995]
 [ 1.001917    0.86292291 -2.02699995]
 [ 1.00577796  0.86292291 -2.02699995]
 [ 0.99220568  0.84802288 -1.99199998]
 [ 0.99600011  0.84802288 -1.99199998]
 [ 0.9997943   0.84802288 -1.99199998]]
```

![スクリーンショット 2018-03-19 21.38.05.png](https://qiita-image-store.s3.amazonaws.com/0/44835/ea6b3812-2fe2-6242-c47e-8dda546f9440.png)