2015年4月23日 星期四

齊次座標 (homogeneous coordinate)

除了使用三維直角座標來表示物體的空間位置之外,在圖學中,也常使用「齊次座標」(homogeneous coordinate)來呈現,這一方面是為了方便將空間的平移、縮放、旋轉等轉換使用矩陣來記錄。

齊次座標使用四個元素來表示,即(x, y, z, w),要將齊次座標轉換為三維座標,其關係為(x/w, y/w, z/w),其中w表示座標軸的遠近參數,通常設為1,如果要用來表示遠近感,則會設定為距離的倒數(1/距離),例如表示一個無限遠的距離時,我們會將w 設定為0。

可以直接將之前介紹過的公式使用齊次座標與矩陣來展現,就可以瞭解齊次座標的好處處,例如以三維座標常見的平移、縮放與旋轉為例,表示方法如下(原座標x,y,z,轉換後x1,y1,z1):

平移:假設三個平移量分別為Tx、Ty與Tz。。。。


縮放:假設x、y、z的縮放比例分別為a、b、c。。 。。


旋轉:

CANBUS

If the PHY is not properly configured, TX in CAN MAC might not work continuously.
Ex1: 2nd MCP2515 TX is NG if TJA1040 STB is not 0.
Ex2: MCP2515 TX is NG if no other CAN node is connected.

2015年4月22日 星期三

Integrate OpenCV and Python

Install WinPython-64bit-2.7.9.4 which includes:
   Python 2.7.9   (OpenCV 2.4 supports Pythin 2.x while OpenCV 3.0 supports Python 3.x)
   NumPy 1.9.2
   SciPy 0.15.1

Copy \build\python\2.7\x64\cv2.pyd to \\Lib\site-packages

Verify (using IDEL)
   import cv2
   print (cv2.__version__)

高中英文:which/that/what關係代名詞


原文: http://bearwithprim.pixnet.net/blog/post/312118271-%E9%AB%98%E4%B8%AD%E8%8B%B1%E6%96%87%3Awhich-that-what%E9%97%9C%E4%BF%82%E4%BB%A3%E5%90%8D%E8%A9%9E

which 指人以外之生命,無生命者都可以用which
例句:
1. He likes the house which has many windows.
2. Where is the letter which I received yesterday?
3. I like the house which he lives in.

that 用法
  1. 先行詞是人或動物
  2. Who,which 開頭疑問句,為避免重複,用that
-Who is the man that has white hair?
-Which is the book that you borrowed from the library?
  1. It is…that 的強調用法
It is Columbus that discovered America in 1492.
  1. that前不可有介詞或逗點 (所以有介詞或逗點時要選who,which)
I will lend you the book, which is interesting.
I have two brothers, who live in Taipei.
想一想上面的規則,考考你:
-This is the house (in that/ in which) he lives. 選哪一個?
-I will lend you this book (, which/, that) is useful. 選哪一個?
正解:
   - in which   that前不可有介詞
-, which   that前不可有介詞或逗點
  1. that的瑣事: (這裡解釋了為什麼有時候必選that不可。)
-先行詞中有序數
Henry Ford was the first man that built cars.
-先行詞中有最高級
This is the most interesting book that I have ever read.
-先行詞中有the 家族 (the only 唯一;the very正式那個;the same同一個)
Man is the only animal that can speak.
This is the very book that I like very much.
-先行詞中有任何/全部/無一 (任何any/全部all/無一no)
Anyone that listens to him is a fool.
 what 用法
what = 先行詞+關係代名詞
what = the thing which
     = that which
     = all that

請把以下” 先行詞+關係代名詞” 改成what
因為使用what句子變簡短有力了。
 This is the thing which I need. = ? This is what I need.
Do you understand all that I said? = ? Do you understand what I said?
He saves all that he earned. = ? He saves what he earned.
The thing which he said is true. = ? What he said is true.

注意:
  1. 因為what本身已經有先行詞和關係代名詞的意義了,所以如果出現what,前面就不應該再出現先行詞,例如: that, the thing,…
  2. what 本身就可以當作主詞、受詞或補語,(which, that不同,which, that 只能當關係代名詞使用)

2015年4月20日 星期一

用MinGW編譯DLL

原文: http://october388.blogspot.tw/2009/04/mingwdll.html

testdll.h:
#ifndef TESTDLL_H
#define TESTDLL_H

#ifdef DLL_EXPORT
#define DLLAPI __declspec(dllexport)
#else
#define DLLAPI __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C" {
#endif

DLLAPI float funadd(float a, float b);

#ifdef __cplusplus
}
#endif

#endif


testmain.c:#include
#include "testdll.h"

int main(void)
{
float a,b,c;

    a=1.0;
    b=2.0;
    c=funadd(a,b);
    
    printf("%f \n",c);
    getchar();    
}


testdll.c:
#include "testdll.h"

float funadd(float a, float b)
{
    return (a+b);
}


gcc -c -DDLL_EXPORT (-fPIC) testdll.cPS : 會產生testdll.o的檔案...還有x86平台,-fPIC會忽略

gcc -shared -o testdll.dll testdll.o -Wl,--output-def,testdll.def,--out-implib,testdll.lib
PS : 產生三個檔案:
testdll.dll:也就是程式執行時載入的dll檔 
testdll.def:若有需要和VC編譯器連結,這個檔會需要
testdll.lib:export library,用來連結主程式,這樣主程式才能知道找哪個dll,那個dll裡有哪些函式跟參數....

gcc -c testmain.cPS : 用來使用dll的主程式,會產生一個testmain.o的檔案

gcc -static -o test.exe testmain.o -L. -ltestdllPS : 會讓主程式和export library(testdll.lib)靜態連結起來,產生test.exe的可執行檔


由於dll和主程式都是用MinGW產生的,所以可以簡化上面的步驟,不過上面的解說,可以對執行檔組成了解多一些,以下舉兩個例子

方法一:
gcc -shared -o testdll.dll testdll.c
PS : -shared是linker的參數,這裡直接產生testdll.dll

gcc -c testmain.c

PS : -c:只編譯不連結,產生testmain.o

gcc -o test.exe testmain.o testdll.dll

PS : 我也不知給什麼規則,反正MinGW搞得定這個testdll.dll就對了

方法二:
gcc -shared -o testdll.dll testdll.c
gcc -o test.exe testmain.c -L. -ltestdll

PS : -l:unix環境裡,會找libtestdll.a(-static) 或 libtestdll.so(-shared),在函式庫名稱的命名上,前面一定要有lib

不過MinGW對於這個慣例,放寬許多,來讓熟悉windows的人習慣
for (-static) : 會找libtestdll.a 或 testdll.lib
for (-shared) : 會找libtestdll.so . testdll.dll 或 libtestdll.dll

希望看完之後是感覺"條條道路通羅馬",而不是給它一整個亂...

這裡又牽扯出另一個疑問,那就是MinGW的make,它對於字尾規則和函式庫的處理,是否有加上這些擴充,有空在研究好了

PS:MinGW的官方網站,有提供一個Shell,叫MSYS,之前發現MinGW和MSYS都有自己的make,MinGW的make叫mingw32-make.exe,MSYS的make叫make.exe,其中MSYS的make是unix style,不能分辨'\',組成的路徑字串(windows是用'\',組成路徑字串),也許這兩個make除了這個區別,還包括了字尾規則和函式庫處理的差異...


testdll.h的補充說明:
關於DLL_EXPORT 的定義,應該要在編譯testdll.c時定義,來分別dllimport和dllexport,不過我沒加,也沒錯誤,我不知道MinGW有沒有支援__declspec(dllimport)和__declspec(dllexport) 這些關鍵字...

關於extern "C" {},如果使用gcc,且souce code的附檔名是.c,會有錯誤,不過__cplusplus沒有定義,會避開...
如果附檔名是.cpp,或gcc改g++,則__cplusplus會定義,就會使函數的Name Mangling使用C的形式(簡單的那一種),常看到...

這裡的紀錄,主要都是看了別人的文章,試出來的...我盡量有系統性,不過最好還是參考相關主要說明文件

Creating a shared and static library with the gnu compiler [gcc]

Source: http://www.adp-gmbh.ch/cpp/gcc/create_lib.html


Creating a shared and static library with the gnu compiler [gcc]

Here's a summary on how to create a shared and a static library with gcc. The goal is to show the basic steps. I do not want to go into the hairy details. It should be possible to use this page as a reference.
These examples were tested and run on cygwin/Windows.

The code for the library

This is the code that goes into the library. It exhibits one single function that takes two doubles and calculates their mean value and returns it.
calc_mean.c
//#include 

double mean(double a, double b) {
  return (a+b) / 2;
}

The header file

Of course, we need a header file.
calc_mean.h
double mean(double, double);

Creating the static library

A static library is basically a set of object files that were copied into a single file. This single file is the static library. The static file is created with the archiver (ar).
First, calc_mean.c is turned into an object file:
gcc -c calc_mean.c -o calc_mean.o
Then, the archiver (ar) is invoked to produce a static library (named libmean.a) out of the object file calc_mean.o.
ar  rcs libmean.a      calc_mean.o
Note: the library must start with the three letters lib and have the suffix .a.

Creating the shared library

As with static libraries, an object file is created. The -fPIC option tells gcc to create position independant code which is necessary for shared libraries. Note also, that the object file created for the static library will be overwritten. That's not bad, however, because we have a static library that already contains the needed object file.
gcc -c -fPIC calc_mean.c -o calc_mean.o    
For some reason, gcc says:
cc1: warning: -fPIC ignored for target (all code is position independent)
It looks like -fPIC is not necessary on x86, but all manuals say, it's needed, so I use it too.
Now, the shared library is created
gcc -shared -Wl,-soname,libmean.so.1 -o libmean.so.1.0.1  calc_mean.o
Note: the library must start with the three letter lib.

The programm using the library

This is the program that uses the calc_mean library. Once, we will link it against the static library and once against the shared library.
main.c
#include 
#include "calc_mean.h"

int main(int argc, char* argv[]) {

  double v1, v2, m;
  v1 = 5.2;
  v2 = 7.9;

  m  = mean(v1, v2);

  printf("The mean of %3.2f and %3.2f is %3.2f\n", v1, v2, m);

  return 0;
}

Linking against static library

gcc -static main.c -L. -lmean -o statically_linked
Note: the first three letters (the lib) must not be specified, as well as the suffix (.a)

Linking against shared library

gcc main.c -o dynamically_linked -L. -lmean
Note: the first three letters (the lib) must not be specified, as well as the suffix (.so)

Executing the dynamically linked programm

LD_LIBRARY_PATH=.
./dynamically_linked

Thanks

Thanks to Donn Morrison who helped me improve this page.

2015年4月10日 星期五

Android開發人員選項

開啟: 設定 > 關於 > 軟體資訊 > (點七次)版本號碼
關閉: 設定 > 應用程式 > 設定 > 清除資料