我想使用Kabsch算法(https://gist.github.com/JDevlieghere/7039971)的Java实现。我不明白如何为矩阵P和Q指定数据集。
我需要从文件中读取这些矩阵,并尝试了以下操作:
public class Kabsch {
/**
* Data set P
*/
SimpleMatrix P;
/**
* Data set Q
*/
SimpleMatrix Q;
/**
* Rotation matrix
*/
SimpleMatrix rotation;
/**
* Translation matrix
*/
SimpleMatrix translation;
public static void main(String args[]) {
try {
String line1 = "";
double[][] num = new double[50][50];
int i = 0;
int j = 0;
BufferedReader file1 = new BufferedReader(new FileReader(new File("./file.txt")));
while ((line1 = file1.readLine()) != null) {
j = 0;
StringTokenizer st1 = new StringTokenizer(line1, " ");
while (st1.hasMoreTokens()) {
num[i][j] = Double.parseDouble(st1.nextToken());
j++;
}
i++;
}
file1.close();
P = new SimpleMatrix(2,3,true,new double[]{num[0][0],num[0][1],num[0][2],num[1][0],num[1][1],num[1][2]});
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Kabsch(SimpleMatrix P, SimpleMatrix Q) throws IllegalArgumentException {
if(!isValidInputMatrix(P) || !isValidInputMatrix(Q))
throw new IllegalArgumentException();
if(P.numCols() != Q.numCols() || P.numRows() != Q.numRows())
throw new IllegalArgumentException();
this.P = P;
this.Q = Q;
calculate();
}
...
}
当我尝试从输入文件中将P设置为矩阵时,出现错误“无法对非静态字段P进行静态引用”。如何正确执行?