Predator's Vision

画像処理、3D点群処理、DeepLearning等の備忘録

SketchUpにポイントクラウドをインポート

  • 2014/09/04追記

デプスセンサで取得したポイントクラウドSketchUpで作成した構造データとどれぐらい合うか目視で比較するために、SketchUpにポイントクラウドをインポートするプラグインを作りました。

メニューバー「プラグイン」から"Import Point Cloud..."を選択すると、ファイル選択ダイアログが表示され、ポイントクラウドをインポートできます。ポイントクラウドデータ形式は、*.xyzPCL (Point Cloud Library)で使われる*.pcd(ASCIIに限る)に対応しています。

なお、実際に取り込むのは点ではなく小さい立方体です。

  • 使い方

下のコードをRubyコンソールに流し込むか、プラグインのディレクトリ(C:\Program Files (x86)\SketchUp\SketchUp 2014\Tools)に任意のファイル名で保存します。

# First we pull in the standard API hooks.
require 'sketchup.rb'

# Create a tiny box (2r*2r*2r)
def draw_point(x, y, z, r=5.mm)
  model = Sketchup.active_model
  entities = model.entities

  # Create a plane on x-y plane
  pt1 = [x - r, y - r, z - r]
  pt2 = [x + r, y - r, z - r]
  pt3 = [x + r, y + r, z - r]
  pt4 = [x - r, y + r, z - r]
  new_face = entities.add_face pt1, pt2, pt3, pt4

  # Thicken the plane
  new_face.pushpull r * 2
end

# Create a lot of tiny boxes
def draw_pointcloud(filename)
  index = 0;
  index_begin = 0;

  # If ".pcd", data starts from line 11
  if(File.extname(filename) == ".pcd") then
    index_begin = 11;
  end
  
  begin
    open(filename) {|file|
      while l = file.gets
        if index >= index_begin then
          p = l.split(' ')     # Modified from "p = l.encode.split(' ')"
          if p.size >= 3 then
            draw_point(1000.mm * Float(p[0]), 1000.mm * Float(p[1]), 1000.mm * Float(p[2]))
          end
        end
        index += 1
      end
   }
  rescue => ex
    UI.messagebox("Error!\n" + ex.message)
  end
end

# Add a menu item to launch this plugin.
UI.menu("Plugins").add_item("Import Point Cloud...") {
  file = UI.openpanel("Open Point Cloud File", "C:/", "Point Cloud File (*.pcd,*.xyz,*.txt)|*.pcd;*.xyz;*.txt;||")
  if file then
    draw_pointcloud(file)
  end
}


インポートするとこんな感じです。

f:id:presan:20140610161714p:plain