2014-10-06 114 views
2

我正在使用android的旋轉矩陣在空間中旋轉多個點。使用旋轉矩陣在空間中旋轉點

工作至今

我通過讀取SensorManager.getRotationMatrix函數矩陣開始。接下來,我使用this link中給出的解釋將旋轉矩陣轉換爲四元數。我這樣做是因爲我讀到歐拉角可能導致Gimbal鎖定問題,並且使用3x3矩陣的操作可能是詳盡無遺的。 source

問題

現在我想做的是:想象一下手機是參考的起源和給定一組點(預期的LAT /經度座標轉換爲XYZ座標系參見方法波紋管)我想旋轉它們,以便檢查哪些是在我的視線上。爲此我使用的是this SO question,它返回一個X和Y(分別是左和頂)來顯示屏幕上的點。它工作正常,但只在朝北時才起作用(因爲它不考慮方向,我的投影向量使用南/北作爲X,東/西作爲Z)。所以我的想法是旋轉所有對象。即使初始高度(Y)爲0,我也希望能夠根據手機的方向來定位點的上/下。

我認爲部分解決方案可能在this post。但由於這使用歐拉角度,我不認爲這是最好的方法。

結論

所以,如果它真的不如旋轉每個點的位置我怎麼能存檔,使用旋轉四元數?否則哪種方法更好?

對不起,如果我在這篇文章中說錯了。我不擅長物理學。

代碼

//this functions returns a 3d vector (0 for Y since I'm discarding altitude) using 2 coordinates 
public static float[] convLocToVec(LatLng source, LatLng destination) 
{ 
    float[] z = new float[1]; 
    z[0] = 0; 
    Location.distanceBetween(source.latitude, source.longitude, destination 
      .latitude, source.longitude, z); 
    float[] x = new float[1]; 
    Location.distanceBetween(source.latitude, source.longitude, source 
      .latitude, destination.longitude, x); 
    if (source.latitude < destination.latitude) 
     z[0] *= -1; 
    if (source.longitude > destination.longitude) 
     x[0] *= -1; 

    return new float[]{x[0], (float) 0, z[0]}; 
} 

感謝您的幫助,並有一個愉快的一天。


UPDATE 1

根據Wikipedia

計算3×3旋轉矩陣R的矩陣積和 原來3×1列的矩陣表示v→。這需要3×(3 乘法+2加法)= 9乘法和6加法, 旋轉矢量的最有效的方法。

我真的應該使用旋轉矩陣來旋轉矢量嗎?

回答

0

由於沒有人回答我在這裏回答自己。

經過一番研究(實際上很多)後,我得出結論是的,它有可能使用四元數旋轉矢量但你最好將它轉換成旋轉矩陣。

  • 旋轉矩陣 - 9個乘法和加法6
  • Quartenion - 15乘法和加法15

來源:Performance comparisons

最好是透過Android提供的旋轉矩陣。另外,如果您打算使用四元數(例如Sensor.TYPE_ROTATION_VECTOR + SensorManager.getQuaternionFromVector),您可以(也應該)將其轉換爲旋轉矩陣。您可以使用方法SensorManager.getRotationMatrixFromVector將旋轉向量轉換爲矩陣。獲得旋轉矩陣後,只需將其乘以所需的投影向量即可。你可以使用這個功能:

public float[] multiplyByVector(float[][] A, float[] x) { 
     int m = A.length; 
     int n = A[0].length; 
     if (x.length != n) throw new RuntimeException("Illegal matrix dimensions."); 
     float[] y = new float[m]; 
     for (int i = 0; i < m; i++) 
      for (int j = 0; j < n; j++) 
       y[i] += (A[i][j] * x[j]); 
     return y; 
    } 

雖然我仍然無法正確運行,我會將其標記爲答案。

+0

它有一個更好的解決方案。你還在尋找解決方案嗎? – 2016-05-09 07:17:32

+0

不,我已經離開了這個項目:)我最終選擇了一種不同的方法 – 2016-05-09 07:22:46