IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

PythonでBlenderのオブジェクトを操作する

トーラスを追加します。リングのカーソルがある位置に追加されます。下の画像では、X=5, Y=4の位置に追加されます。

>>> bpy.ops.mesh.primitive_torus_add()
{'FINISHED'}

f:id:naotoogawa:20170212182049p:plainf:id:naotoogawa:20170212182058p:plain

追加したトーラスが選択されているものとします。選択オブジェウトをobjに参照をもたせます。

>>> obj = bpy.context.active_object

X=-1, Y=-1に移動します。

>>> obj.matrix_world = Matrix.Translation(Vector((-1,-1,0))) 

f:id:naotoogawa:20170212182154p:plain

X=-1, Y=-1, Z=5に移動します。

>>> obj.matrix_world = Matrix.Translation(Vector((-1,-1, 5)))

f:id:naotoogawa:20170212182215p:plain

X軸方向に2倍拡大します。

>>> obj.matrix_world = Matrix.Translation(Vector((-1,-1, 5))) * (Matrix.Scale(2,4,(1,0,0))) 

f:id:naotoogawa:20170212182926p:plain

HaskellでMySQLにアクセス

サンドボックスの作成

(root)foo:mysqlsample foo$ cabal sandbox init
Writing a default package environment file to
/Users/foo/work03/haskelldatabase/mysqlsample/cabal.sandbox.config
Creating a new sandbox at
/Users/foo/work03/haskelldatabase/mysqlsample/.cabal-sandbox

ライブラリのインストール

(root)foo:mysqlsample foo$ cabal install HDBC-mysql 
Warning: The package list for 'hackage.haskell.org' is 22 days old.
Run 'cabal update' to get the latest list of available packages.
Resolving dependencies...
Notice: installing into a sandbox located at
/Users/foo/work03/haskelldatabase/mysqlsample/.cabal-sandbox
Configuring old-locale-1.0.0.7...
Configuring mtl-2.2.1...
Configuring utf8-string-1.0.1.1...
Configuring text-1.2.2.1...
Building utf8-string-1.0.1.1...
Building old-locale-1.0.0.7...
Building mtl-2.2.1...
Building text-1.2.2.1...
Installed old-locale-1.0.0.7
Configuring old-time-1.1.0.3...
Building old-time-1.1.0.3...
Installed mtl-2.2.1
Installed utf8-string-1.0.1.1
Installed old-time-1.1.0.3
Installed text-1.2.2.1
Configuring convertible-1.1.1.0...
Building convertible-1.1.1.0...
Installed convertible-1.1.1.0
Configuring HDBC-2.4.0.1...
Building HDBC-2.4.0.1...
Installed HDBC-2.4.0.1
Downloading HDBC-mysql-0.7.1.0...
Configuring HDBC-mysql-0.7.1.0...
Building HDBC-mysql-0.7.1.0...
Installed HDBC-mysql-0.7.1.0
Updating documentation index
/Users/foo/work03/haskelldatabase/mysqlsample/.cabal-sandbox/share/doc/x86_64-osx-ghc-8.0.1/index.html

インポート

(root)foo:mysqlsample foo$ cabal repl
GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
Prelude> import Database.HDBC 
Prelude Database.HDBC> import Database.HDBC.MySQL

コネクション取得

Prelude Database.HDBC Database.HDBC.MySQL> connectMySQL defaultMySQLConnectInfo {mysqlUser = "redmine", mysqlPassword = "my_password", mysqlDatabase="redmine"}
Prelude Database.HDBC Database.HDBC.MySQL> let con = connectMySQL defaultMySQLConnectInfo {mysqlUser = "redmine", mysqlPassword = "my_password", mysqlDatabase="redmine"}
Prelude Database.HDBC Database.HDBC.MySQL> :t con
con :: IO Connection

select実行

Prelude Database.HDBC Database.HDBC.MySQL> con >>= \x -> prepare x "select * from USERS" >>= (\y -> execute y [] >>= \_ -> fetchRow y)
Just [SqlInt32 1,SqlByteString "admin",SqlByteString "a99fe2fb647cb2a5e059e095c93d2ccc0588f76d",SqlByteString "Redmine",SqlByteString "Admin",SqlInt32 1,SqlInt32 1,SqlLocalTime 2015-02-01 20:33:33,SqlByteString "",SqlNull,SqlLocalTime 2015-12-07 20:51:38,SqlLocalTime 2015-12-07 21:39:45,SqlByteString "User",SqlNull,SqlByteString "all",SqlByteString "cb24cc9080698d1eb43f3c8b32189e4e",SqlInt32 0,SqlLocalTime 2015-12-07 21:39:45]

Mac, Haskell, OpenGLでサンプルを動かすまで(2017/02時点)

github.com

このサンプルを動かそうとしたのですが、かなりハマったので、忘れないうちにメモを取ります。サンプルそのものの問題ではないです。

基本的な考え方

利用環境のOpenGLのバージョンを確認する。

support.apple.com

GLSLのバージョンを確認する。

stackoverflow.com

確認したOpenGLのバージョンに合わせてGLFWでWindowを生成する時にHintを与える

106 main :: IO ()
107 main = do
108    GLFW.init
109    -- GLFW.defaultWindowHints  ### このようなHintを与えている部分を利用環境に合わせて下記のように明示的に指定する。
110 
111    mapM_ GLFW.windowHint
112         [ GLFW.WindowHint'Samples 4 -- 4x antialiasing
113         , GLFW.WindowHint'ContextVersionMajor 4 -- OpenGL 4.1
114         , GLFW.WindowHint'ContextVersionMinor 1
115         -- we don't want the old OpenGL
116         , GLFW.WindowHint'OpenGLProfile GLFW.OpenGLProfile'Core
117         ,GLFW.WindowHint'OpenGLForwardCompat True 
118         ]
119 
120    Just win <- GLFW.createWindow 640 480 "GLFW Demo" Nothing Nothing
121    GLFW.makeContextCurrent (Just win)
122    GLFW.setWindowSizeCallback win (Just resizeWindow)
123    GLFW.setKeyCallback win (Just keyPressed)
124    GLFW.setWindowCloseCallback win (Just shutdown)
125    descriptor <- initResources
126    onDisplay win descriptor
127    GLFW.destroyWindow win
128    GLFW.terminate
確認したGLSLのバージョンに合わせてシェーダーのファイル(triangles.frac,triangles.vert等)を作成/修正する。
 #version 410 core
必要に応じてコードの修正をする。

利用しているAPIのバージョンが変わっている場合は、それに応じて修正を行う。

実際の手順 その1 (記憶を辿って書いているので抜けがあると思います)

サンドボックスの作成
cabal sandbox
必要なライブラリのインストール(足りなかったら追加)
cabal install GLFW-b 
cabal install OpenGL
cabal install GLUtil
cabal install mtl
サンプルの取得
git clone https://github.com/madjestic/Haskell-OpenGL-Tutorial
チュートリアルディレクトリに移動してcabalファイルを作成する。
foo$ ls
Main.hs     NGL     README.md   Shaders     tutorial05.png
foo$ vim ./NGL/
Linear.hs       LoadShaders.hs  Rendering.hs    Shape.hs        Utils.hs        
foo$ vim ./NGL/Rendering.hs 
foo$ cabal init
Warning: The package list for 'hackage.haskell.org' is 22 days old.
Run 'cabal update' to get the latest list of available packages.
Package name? [default: tutorial05] 
Package version? [default: 0.1.0.0] 
Please choose a license:
   1) GPL-2
   2) GPL-3
   3) LGPL-2.1
   4) LGPL-3
   5) AGPL-3
   6) BSD2
 * 7) BSD3
   8) MIT
   9) ISC
  10) MPL-2.0
  11) Apache-2.0
  12) PublicDomain
  13) AllRightsReserved
  14) Other (specify)
Your choice? [default: BSD3] 8
Author name? [default: who] foo
Maintainer email? [default: who@gmail.com] foo@bar.com
Project homepage URL? 
Project synopsis? 
Project category:
 * 1) (none)
   2) Codec
   3) Concurrency
   4) Control
   5) Data
   6) Database
   7) Development
   8) Distribution
   9) Game
  10) Graphics
  11) Language
  12) Math
  13) Network
  14) Sound
  15) System
  16) Testing
  17) Text
  18) Web
  19) Other (specify)
Your choice? [default: (none)] 10
What does the package build:
   1) Library
   2) Executable
Your choice? 2
What is the main module of the executable:
 * 1) Main.hs
   2) Other (specify)
Your choice? [default: Main.hs] 
Source directory:
 * 1) (none)
   2) src
   3) Other (specify)
Your choice? [default: (none)] 
What base language is the package written in:
 * 1) Haskell2010
   2) Haskell98
   3) Other (specify)
Your choice? [default: Haskell2010] 
Add informative comments to each field in the cabal file (y/n)? [default: n] n

Guessing dependencies...

Warning: no package found providing Graphics.Rendering.OpenGL.

Warning: no package found providing Graphics.UI.GLFW.

Warning: no package found providing Graphics.GLUtil.

Warning: no package found providing Linear.

Warning: no package found providing Graphics.UI.GLUT.

Generating LICENSE...
Generating Setup.hs...
Generating ChangeLog.md...
Generating tutorial05.cabal...

Warning: no synopsis given. You should edit the .cabal file and add one.
You may want to edit the .cabal file and add a Description field.
生成したcabalファイルをsandbox配下に移動する。
foo$ mv tutorial05.cabal ../../
cabalファイルのライブラリ依存関係とソースディレクトリを必要に応じて追加修正を行う。
 22   build-depends:       base >=4.9 && <4.10,
 23                        filepath >=1.4 && <1.5,
 24                        bytestring >=0.10 && <0.11,
 25                        text >=1.2 && <1.3,
 26                        OpenGL >=3.0 && <3.1,
 27                        GLFW-b,
 28                        GLUtil
 29   hs-source-dirs:      Haskell-OpenGL-Tutorial/tutorial05
プログラムのWindowHintを修正する。(上述)
シェーダのファイルのバージョンを修正する。(上述)
ビルドする。
foo$ cabal clean
cleaning...
foo$ cabal build
Package has never been configured. Configuring with default flags. If this
fails, please run configure manually.
Warning: The package list for 'hackage.haskell.org' is 22 days old.
Run 'cabal update' to get the latest list of available packages.
Resolving dependencies...
Configuring tutorial05-0.1.0.0...
Building tutorial05-0.1.0.0...
Preprocessing executable 'tutorial05' for tutorial05-0.1.0.0...
[1 of 5] Compiling NGL.Utils        ( Haskell-OpenGL-Tutorial/tutorial05/NGL/Utils.hs, dist/build/tutorial05/tutorial05-tmp/NGL/Utils.o )
[2 of 5] Compiling NGL.LoadShaders  ( Haskell-OpenGL-Tutorial/tutorial05/NGL/LoadShaders.hs, dist/build/tutorial05/tutorial05-tmp/NGL/LoadShaders.o )

Haskell-OpenGL-Tutorial/tutorial05/NGL/LoadShaders.hs:42:41: error:
    Ambiguous occurrence ‘packUtf8’
    It could refer to either ‘Graphics.Rendering.OpenGL.packUtf8’,
                             imported from ‘Graphics.Rendering.OpenGL’ at Haskell-OpenGL-Tutorial/tutorial05/NGL/LoadShaders.hs:25:1-32
                             (and originally defined in ‘OpenGL-3.0.1.0:Graphics.Rendering.OpenGL.GL.ByteString’)
                          or ‘NGL.LoadShaders.packUtf8’,
                             defined at Haskell-OpenGL-Tutorial/tutorial05/NGL/LoadShaders.hs:46:1
[3 of 5] Compiling NGL.Shape        ( Haskell-OpenGL-Tutorial/tutorial05/NGL/Shape.hs, dist/build/tutorial05/tutorial05-tmp/NGL/Shape.o )

エラーが出たら適宜修正する。 今回の場合は、packUtf8が重複したのでLoadShaders.hsにあるpackUtf8packUtf8'に変更した。

foo$ vim Haskell-OpenGL-Tutorial/tutorial05/NGL/LoadShaders.hs

ビルドする。

foo$ cabal build
Building tutorial05-0.1.0.0...
Preprocessing executable 'tutorial05' for tutorial05-0.1.0.0...
[3 of 5] Compiling NGL.LoadShaders  ( Haskell-OpenGL-Tutorial/tutorial05/NGL/LoadShaders.hs, dist/build/tutorial05/tutorial05-tmp/NGL/LoadShaders.o )
[4 of 5] Compiling NGL.Rendering    ( Haskell-OpenGL-Tutorial/tutorial05/NGL/Rendering.hs, dist/build/tutorial05/tutorial05-tmp/NGL/Rendering.o )

Haskell-OpenGL-Tutorial/tutorial05/NGL/Rendering.hs:114:2: warning: [-Wtabs]
    Tab character found here.
    Please use spaces instead.
[5 of 5] Compiling Main             ( Haskell-OpenGL-Tutorial/tutorial05/Main.hs, dist/build/tutorial05/tutorial05-tmp/Main.o )
Linking dist/build/tutorial05/tutorial05 ...
生成されたファイルをチュートリアルの場所に移動する。

シェーダファイルを読み込む必要があるのでパスを合わせる必要があるため。

foo$ mv dist/build/tutorial05/tutorial05 Haskell-OpenGL-Tutorial/tutorial05/
実行する。
foo$ ./tutorial05

f:id:naotoogawa:20170205142729p:plain

実際の手順 その2 (記憶を辿って書いているので抜けがあると思います)

下記のサンプルの場合

Linus's Blog - OpenGL from Haskell

修正ポイント

OpenGLRawのバージョンに関して、サンプルでは2系ですが、最新の3系に修正する必要があります。

OpenGLRaw: A raw binding for the OpenGL graphics system

WindowHintの変更だけでなく、importの変更と、プレフィックスのgl_GL_に変更する必要がありました。

修正箇所

modify 2014-03-15-opengl-from-haskell.html · GitHub

その他参考

c++ - GLFW Fails to Open Window in OSX - Stack Overflow

Railsのリクエストパラメータの確認方法

logディレクトリ配下にログファイルがある。

(root)$ ls log
delete.me   development.log production.log
(root)$ vim log/development.log 

Started POSTを目印に探す。

例1:チケット作成時

Started POST "/projects/proj_hoge/issue_categories" for 127.0.0.1 at 2016-05-01 12:32:53 +0900
Processing by IssueCategoriesController#create as HTML 
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"tLh26w1pZDyrZ+7e3LywL+CMI1EUl3oM93udrssJHUEe7uFhfjLhYBBKL0ENht7pa9Htfk1eaJqlkMYbgdPtGA==", "issue_category"=>{"name"=>"新しいカテゴリ01", "assigned_to_id"=>""}, "commit"=>"作成", "project_id"=>"proj_hoge"}
  ^[[1m^[[35mSQL (8.8ms)^[[0m  UPDATE `tokens` SET `tokens`.`updated_on` = '2016-05-01 21:32:53' WHERE `tokens`.`user_id` = 1 AND `tokens`.`value` = '44c8a7afb44b4045a8dabe89b8b178b857183b1f' AND `tokens`.`action` = 'session' 

例2:カテゴリー作成時

Started POST "/projects/proj_hoge/issues" for 127.0.0.1 at 2016-05-01 12:25:30 +0900
Processing by IssuesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8el3FevbMDoiQr70DxFP4QtfcRIhI9e51V6WR8oPGrJbv+CfmIC1Zplvf2veKyEngAK/PXjqxS+Htc3ygNXq6w==", "form_update_triggered_by"=>"", "issue"=>{"is_private"=>"0", "tracker_id"=>"1", "subject"=>"aaaaa", "description"=>"", "status_id"=>"1", "priority_id"=>"2", "assigned_to_id"=>"", "parent_issue_id"=>"", "start_date"=>"2016-05-01", "due_date"=>"", "estimated_hours"=>"", "done_ratio"=>"0"}, "was_default_status"=>"1", "commit"=>"作成", "project_id"=>"proj_hoge"}

HaskellでOpenGLのサンプルを動かす

どうもGLUTは描画がメインで、GUIの操作はうまくできないらしく、GUI操作までやりたい場合はGLFWを使うようです(この点は理解があやふや)。ともかく動いたのでメモ。

サンプル

f:id:naotoogawa:20170202212747p:plain

ライブラリ

OpenGLのライブラリ(Macではデフォルトで入っている??)

GLFW - An OpenGL library

Haskellバインディング

GitHub - bsl/GLFW-b: Haskell bindings to GLFW

サンプル

GitHub - bsl/GLFW-b-demo: GLFW-b demo

インストール手順

サンドボックスを作成

cabal sandbox

OpenGLのインストール

cabal install OpenGL

GLFW-bのインストール

cabal install GLFW-b 

mtlのインストール

cabal install mtl

srcディレクトリを作り、そこに、サンプルのソースをコピーする。

(root)$ ls src
Gear.hs         Main.hs         

cabalファイルをinit``で作成する。あらかじめソースがあると依存関係をよしなにcabal```ファイルに自動的に記述してくれるようです。

(root)$ cabal init

依存関係の該当部分は下記のようになっていました。(実際に生成された依存関係は1行でした。)

executable Main
  main-is:             Main.hs
  -- other-modules:       
  -- other-extensions:    
  build-depends:       base >=4.9 && <4.10,
                       OpenGL >=3.0 && <3.1,
                       stm >=2.4 && <2.5,
                       mtl >=2.2 && <2.3,
                       transformers >=0.5 && <0.6,
                       pretty >=1.1 && <1.2,
                       GLFW-b >=1.4 && <1.5

ビルドする。

(root)$ cabal build
Package has never been configured. Configuring with default flags. If this
fails, please run configure manually.
Warning: The package list for 'hackage.haskell.org' is 19 days old.
Run 'cabal update' to get the latest list of available packages.
Resolving dependencies...
Configuring Main-0.1.0.0...
Building Main-0.1.0.0...
Preprocessing executable 'Main' for Main-0.1.0.0...
[1 of 2] Compiling Gear             ( src/Gear.hs, dist/build/Main/Main-tmp/Gear.o )
[2 of 2] Compiling Main             ( src/Main.hs, dist/build/Main/Main-tmp/Main.o )
Linking dist/build/Main/Main ...

実行する。

(root)$ dist/build/Main/Main 
    ------------------------------------------------------------
    '?': Print these instructions
    'i': Print GLFW information
    
    * Mouse cursor, keyboard cursor keys, and/or joystick
      control rotation.
    * Mouse scroll wheel controls distance from scene.
    ------------------------------------------------------------
window focus: FocusState'Focused
cursor pos: 26 695
cursor pos: 15 682
cursor pos: 11 665

2次曲線と4次曲線の原点での曲率

じっくり学ぶ曲線と曲面―微分幾何学初歩

じっくり学ぶ曲線と曲面―微分幾何学初歩

p.15 に 例1.1.4(放物線の曲率)、例1.1.5(4次曲線の曲率)があります。

  • 放物線の原点での曲率は2|a|
  • 4次曲線の原点での曲率は0

と説明されています。

これはグラフを書いてみるとイメージができると思います。

放物線

f:id:naotoogawa:20170131212449p:plain

https://sandbox.open.wolframcloud.com/app/objects/65966c15-2789-4f0c-bf88-06d0ed98093d#sidebar=compute

4次曲線

4次曲線は原点で直線のように見えます。

f:id:naotoogawa:20170131212456p:plain

https://sandbox.open.wolframcloud.com/app/objects/05e4c0d1-f853-43ae-8a68-b257c150b931#sidebar=compute