More efficient rigid 3D transformation in Java












2















I want to calculate a rigid 3D transformation between two sets of 3D points. I googled myself, found no suitable implementation and implemented it myself with the help of the Apache Commons Math library, based on this guide. The implementation can be seen below:



  public static RigidTransformation3dAnswer computeRigidTransformation3D(RealMatrix src,
RealMatrix dst) {
if (src.getRowDimension() == dst.getRowDimension() && src.getColumnDimension() == dst
.getColumnDimension()) {

int n = src.getRowDimension();
RealMatrix centroidSrc = computeCentroid(src);
RealMatrix centroidDst = computeCentroid(dst);

RealMatrix aa = src.subtract(tile(centroidSrc, n));
RealMatrix bb = dst.subtract(tile(centroidDst, n));

RealMatrix h = aa.transpose().multiply(bb);
SingularValueDecomposition singularValueDecomposition = new SingularValueDecomposition(h);

RealMatrix u = singularValueDecomposition.getU();
RealMatrix vt = singularValueDecomposition.getVT();
RealMatrix rotationMatrix = vt.transpose().multiply(u.transpose());

if (new LUDecomposition(rotationMatrix).getDeterminant() < 0) {
vt.setColumn(2, vt.getColumnVector(2).mapMultiplyToSelf(-1).toArray());
rotationMatrix = vt.transpose().multiply(u.transpose());
}

RealMatrix transpose = (rotationMatrix.scalarMultiply(-1).multiply(centroidSrc.transpose()))
.add(centroidDst.transpose());

RigidTransformation3dAnswer answer = new RigidTransformation3dAnswer();
answer.setRotationMatrix(rotationMatrix);
answer.setTranslationMatrix(transpose);
return answer;
}
return null;
}

private static RealMatrix tile(RealMatrix a, int n) {
RealMatrix realMatrix = new Array2DRowRealMatrix(n, a.getColumnDimension());
for (int i = 0; i < n; i++) {
realMatrix.setEntry(i, 0, a.getEntry(0, 0));
realMatrix.setEntry(i, 1, a.getEntry(0, 1));
realMatrix.setEntry(i, 2, a.getEntry(0, 2));
}
return realMatrix;
}

private static RealMatrix computeCentroid(RealMatrix mat) {
double sumX = 0;
double sumY = 0;
double sumZ = 0;
double returnArray = new double[1][3];
for (int i = 0; i < mat.getRowDimension(); i++) {
double a = mat.getEntry(i, 0);
sumX = sumX + a;
a = mat.getEntry(i, 1);
sumY = sumY + a;
a = mat.getEntry(i, 2);
sumZ = sumZ + a;
}
double centroidX = sumX / (double) mat.getRowDimension();
double centroidY = sumY / (double) mat.getRowDimension();
double centroidZ = sumZ / (double) mat.getRowDimension();
returnArray[0][0] = centroidX;
returnArray[0][1] = centroidY;
returnArray[0][2] = centroidZ;
return new Array2DRowRealMatrix(returnArray);
}


Although this implementation works perfectly, it is not very efficient and takes around 50 ms on desktop, which is not suitable because I want to use it in an Android application.
So here are three question:



a) is there a more efficient library or framework method to use to compute a rigid 3D transformation?



b) If not, is there a heuristic, and therefore more efficient, implementation of such a transformation, if, for example, I exclude the translation and only want rotation around the Z (vertical) axis?



c) If neither of those two points has an answer, is there a way to improve my code efficiency-wise?










share|improve this question


















  • 2





    In src.subtract(tile(centroidSrc, n)) you create a new matrix just for the subtraction and then a new one for the result, even though you don't use src later. It would probably be faster to write a method void subtractFromEachRow(RealMatrix minuend, RealMatrix subtrahendRow) that does the subtraction without creating any new matrices. Modify src using the addToEntry method.

    – Socowi
    Nov 22 '18 at 14:26













  • Also vt.transpose().multiply(u.transpose()) is the same as u.multiply(vt).transpose(), but the latter is faster. However, since vt and u are small, that shouldn't change much.

    – Socowi
    Nov 22 '18 at 14:32













  • How many point pairs do you have?

    – Nico Schertler
    Nov 22 '18 at 17:36











  • Normally around 7, 10 is the maximum

    – Noltibus
    Nov 22 '18 at 18:14






  • 1





    Ok, then 50ms is very slow, indeed. I think the most important factor is the allocation of new memory as @Socowi pointed out. If that does not help, you need to profile where the time is spent. Minor thing: You can calculate the determinant of a 3x3 matrix directly and do not need to calculate a LU decomposition.

    – Nico Schertler
    Nov 22 '18 at 18:37
















2















I want to calculate a rigid 3D transformation between two sets of 3D points. I googled myself, found no suitable implementation and implemented it myself with the help of the Apache Commons Math library, based on this guide. The implementation can be seen below:



  public static RigidTransformation3dAnswer computeRigidTransformation3D(RealMatrix src,
RealMatrix dst) {
if (src.getRowDimension() == dst.getRowDimension() && src.getColumnDimension() == dst
.getColumnDimension()) {

int n = src.getRowDimension();
RealMatrix centroidSrc = computeCentroid(src);
RealMatrix centroidDst = computeCentroid(dst);

RealMatrix aa = src.subtract(tile(centroidSrc, n));
RealMatrix bb = dst.subtract(tile(centroidDst, n));

RealMatrix h = aa.transpose().multiply(bb);
SingularValueDecomposition singularValueDecomposition = new SingularValueDecomposition(h);

RealMatrix u = singularValueDecomposition.getU();
RealMatrix vt = singularValueDecomposition.getVT();
RealMatrix rotationMatrix = vt.transpose().multiply(u.transpose());

if (new LUDecomposition(rotationMatrix).getDeterminant() < 0) {
vt.setColumn(2, vt.getColumnVector(2).mapMultiplyToSelf(-1).toArray());
rotationMatrix = vt.transpose().multiply(u.transpose());
}

RealMatrix transpose = (rotationMatrix.scalarMultiply(-1).multiply(centroidSrc.transpose()))
.add(centroidDst.transpose());

RigidTransformation3dAnswer answer = new RigidTransformation3dAnswer();
answer.setRotationMatrix(rotationMatrix);
answer.setTranslationMatrix(transpose);
return answer;
}
return null;
}

private static RealMatrix tile(RealMatrix a, int n) {
RealMatrix realMatrix = new Array2DRowRealMatrix(n, a.getColumnDimension());
for (int i = 0; i < n; i++) {
realMatrix.setEntry(i, 0, a.getEntry(0, 0));
realMatrix.setEntry(i, 1, a.getEntry(0, 1));
realMatrix.setEntry(i, 2, a.getEntry(0, 2));
}
return realMatrix;
}

private static RealMatrix computeCentroid(RealMatrix mat) {
double sumX = 0;
double sumY = 0;
double sumZ = 0;
double returnArray = new double[1][3];
for (int i = 0; i < mat.getRowDimension(); i++) {
double a = mat.getEntry(i, 0);
sumX = sumX + a;
a = mat.getEntry(i, 1);
sumY = sumY + a;
a = mat.getEntry(i, 2);
sumZ = sumZ + a;
}
double centroidX = sumX / (double) mat.getRowDimension();
double centroidY = sumY / (double) mat.getRowDimension();
double centroidZ = sumZ / (double) mat.getRowDimension();
returnArray[0][0] = centroidX;
returnArray[0][1] = centroidY;
returnArray[0][2] = centroidZ;
return new Array2DRowRealMatrix(returnArray);
}


Although this implementation works perfectly, it is not very efficient and takes around 50 ms on desktop, which is not suitable because I want to use it in an Android application.
So here are three question:



a) is there a more efficient library or framework method to use to compute a rigid 3D transformation?



b) If not, is there a heuristic, and therefore more efficient, implementation of such a transformation, if, for example, I exclude the translation and only want rotation around the Z (vertical) axis?



c) If neither of those two points has an answer, is there a way to improve my code efficiency-wise?










share|improve this question


















  • 2





    In src.subtract(tile(centroidSrc, n)) you create a new matrix just for the subtraction and then a new one for the result, even though you don't use src later. It would probably be faster to write a method void subtractFromEachRow(RealMatrix minuend, RealMatrix subtrahendRow) that does the subtraction without creating any new matrices. Modify src using the addToEntry method.

    – Socowi
    Nov 22 '18 at 14:26













  • Also vt.transpose().multiply(u.transpose()) is the same as u.multiply(vt).transpose(), but the latter is faster. However, since vt and u are small, that shouldn't change much.

    – Socowi
    Nov 22 '18 at 14:32













  • How many point pairs do you have?

    – Nico Schertler
    Nov 22 '18 at 17:36











  • Normally around 7, 10 is the maximum

    – Noltibus
    Nov 22 '18 at 18:14






  • 1





    Ok, then 50ms is very slow, indeed. I think the most important factor is the allocation of new memory as @Socowi pointed out. If that does not help, you need to profile where the time is spent. Minor thing: You can calculate the determinant of a 3x3 matrix directly and do not need to calculate a LU decomposition.

    – Nico Schertler
    Nov 22 '18 at 18:37














2












2








2








I want to calculate a rigid 3D transformation between two sets of 3D points. I googled myself, found no suitable implementation and implemented it myself with the help of the Apache Commons Math library, based on this guide. The implementation can be seen below:



  public static RigidTransformation3dAnswer computeRigidTransformation3D(RealMatrix src,
RealMatrix dst) {
if (src.getRowDimension() == dst.getRowDimension() && src.getColumnDimension() == dst
.getColumnDimension()) {

int n = src.getRowDimension();
RealMatrix centroidSrc = computeCentroid(src);
RealMatrix centroidDst = computeCentroid(dst);

RealMatrix aa = src.subtract(tile(centroidSrc, n));
RealMatrix bb = dst.subtract(tile(centroidDst, n));

RealMatrix h = aa.transpose().multiply(bb);
SingularValueDecomposition singularValueDecomposition = new SingularValueDecomposition(h);

RealMatrix u = singularValueDecomposition.getU();
RealMatrix vt = singularValueDecomposition.getVT();
RealMatrix rotationMatrix = vt.transpose().multiply(u.transpose());

if (new LUDecomposition(rotationMatrix).getDeterminant() < 0) {
vt.setColumn(2, vt.getColumnVector(2).mapMultiplyToSelf(-1).toArray());
rotationMatrix = vt.transpose().multiply(u.transpose());
}

RealMatrix transpose = (rotationMatrix.scalarMultiply(-1).multiply(centroidSrc.transpose()))
.add(centroidDst.transpose());

RigidTransformation3dAnswer answer = new RigidTransformation3dAnswer();
answer.setRotationMatrix(rotationMatrix);
answer.setTranslationMatrix(transpose);
return answer;
}
return null;
}

private static RealMatrix tile(RealMatrix a, int n) {
RealMatrix realMatrix = new Array2DRowRealMatrix(n, a.getColumnDimension());
for (int i = 0; i < n; i++) {
realMatrix.setEntry(i, 0, a.getEntry(0, 0));
realMatrix.setEntry(i, 1, a.getEntry(0, 1));
realMatrix.setEntry(i, 2, a.getEntry(0, 2));
}
return realMatrix;
}

private static RealMatrix computeCentroid(RealMatrix mat) {
double sumX = 0;
double sumY = 0;
double sumZ = 0;
double returnArray = new double[1][3];
for (int i = 0; i < mat.getRowDimension(); i++) {
double a = mat.getEntry(i, 0);
sumX = sumX + a;
a = mat.getEntry(i, 1);
sumY = sumY + a;
a = mat.getEntry(i, 2);
sumZ = sumZ + a;
}
double centroidX = sumX / (double) mat.getRowDimension();
double centroidY = sumY / (double) mat.getRowDimension();
double centroidZ = sumZ / (double) mat.getRowDimension();
returnArray[0][0] = centroidX;
returnArray[0][1] = centroidY;
returnArray[0][2] = centroidZ;
return new Array2DRowRealMatrix(returnArray);
}


Although this implementation works perfectly, it is not very efficient and takes around 50 ms on desktop, which is not suitable because I want to use it in an Android application.
So here are three question:



a) is there a more efficient library or framework method to use to compute a rigid 3D transformation?



b) If not, is there a heuristic, and therefore more efficient, implementation of such a transformation, if, for example, I exclude the translation and only want rotation around the Z (vertical) axis?



c) If neither of those two points has an answer, is there a way to improve my code efficiency-wise?










share|improve this question














I want to calculate a rigid 3D transformation between two sets of 3D points. I googled myself, found no suitable implementation and implemented it myself with the help of the Apache Commons Math library, based on this guide. The implementation can be seen below:



  public static RigidTransformation3dAnswer computeRigidTransformation3D(RealMatrix src,
RealMatrix dst) {
if (src.getRowDimension() == dst.getRowDimension() && src.getColumnDimension() == dst
.getColumnDimension()) {

int n = src.getRowDimension();
RealMatrix centroidSrc = computeCentroid(src);
RealMatrix centroidDst = computeCentroid(dst);

RealMatrix aa = src.subtract(tile(centroidSrc, n));
RealMatrix bb = dst.subtract(tile(centroidDst, n));

RealMatrix h = aa.transpose().multiply(bb);
SingularValueDecomposition singularValueDecomposition = new SingularValueDecomposition(h);

RealMatrix u = singularValueDecomposition.getU();
RealMatrix vt = singularValueDecomposition.getVT();
RealMatrix rotationMatrix = vt.transpose().multiply(u.transpose());

if (new LUDecomposition(rotationMatrix).getDeterminant() < 0) {
vt.setColumn(2, vt.getColumnVector(2).mapMultiplyToSelf(-1).toArray());
rotationMatrix = vt.transpose().multiply(u.transpose());
}

RealMatrix transpose = (rotationMatrix.scalarMultiply(-1).multiply(centroidSrc.transpose()))
.add(centroidDst.transpose());

RigidTransformation3dAnswer answer = new RigidTransformation3dAnswer();
answer.setRotationMatrix(rotationMatrix);
answer.setTranslationMatrix(transpose);
return answer;
}
return null;
}

private static RealMatrix tile(RealMatrix a, int n) {
RealMatrix realMatrix = new Array2DRowRealMatrix(n, a.getColumnDimension());
for (int i = 0; i < n; i++) {
realMatrix.setEntry(i, 0, a.getEntry(0, 0));
realMatrix.setEntry(i, 1, a.getEntry(0, 1));
realMatrix.setEntry(i, 2, a.getEntry(0, 2));
}
return realMatrix;
}

private static RealMatrix computeCentroid(RealMatrix mat) {
double sumX = 0;
double sumY = 0;
double sumZ = 0;
double returnArray = new double[1][3];
for (int i = 0; i < mat.getRowDimension(); i++) {
double a = mat.getEntry(i, 0);
sumX = sumX + a;
a = mat.getEntry(i, 1);
sumY = sumY + a;
a = mat.getEntry(i, 2);
sumZ = sumZ + a;
}
double centroidX = sumX / (double) mat.getRowDimension();
double centroidY = sumY / (double) mat.getRowDimension();
double centroidZ = sumZ / (double) mat.getRowDimension();
returnArray[0][0] = centroidX;
returnArray[0][1] = centroidY;
returnArray[0][2] = centroidZ;
return new Array2DRowRealMatrix(returnArray);
}


Although this implementation works perfectly, it is not very efficient and takes around 50 ms on desktop, which is not suitable because I want to use it in an Android application.
So here are three question:



a) is there a more efficient library or framework method to use to compute a rigid 3D transformation?



b) If not, is there a heuristic, and therefore more efficient, implementation of such a transformation, if, for example, I exclude the translation and only want rotation around the Z (vertical) axis?



c) If neither of those two points has an answer, is there a way to improve my code efficiency-wise?







java math optimization geometry






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 13:33









NoltibusNoltibus

18515




18515








  • 2





    In src.subtract(tile(centroidSrc, n)) you create a new matrix just for the subtraction and then a new one for the result, even though you don't use src later. It would probably be faster to write a method void subtractFromEachRow(RealMatrix minuend, RealMatrix subtrahendRow) that does the subtraction without creating any new matrices. Modify src using the addToEntry method.

    – Socowi
    Nov 22 '18 at 14:26













  • Also vt.transpose().multiply(u.transpose()) is the same as u.multiply(vt).transpose(), but the latter is faster. However, since vt and u are small, that shouldn't change much.

    – Socowi
    Nov 22 '18 at 14:32













  • How many point pairs do you have?

    – Nico Schertler
    Nov 22 '18 at 17:36











  • Normally around 7, 10 is the maximum

    – Noltibus
    Nov 22 '18 at 18:14






  • 1





    Ok, then 50ms is very slow, indeed. I think the most important factor is the allocation of new memory as @Socowi pointed out. If that does not help, you need to profile where the time is spent. Minor thing: You can calculate the determinant of a 3x3 matrix directly and do not need to calculate a LU decomposition.

    – Nico Schertler
    Nov 22 '18 at 18:37














  • 2





    In src.subtract(tile(centroidSrc, n)) you create a new matrix just for the subtraction and then a new one for the result, even though you don't use src later. It would probably be faster to write a method void subtractFromEachRow(RealMatrix minuend, RealMatrix subtrahendRow) that does the subtraction without creating any new matrices. Modify src using the addToEntry method.

    – Socowi
    Nov 22 '18 at 14:26













  • Also vt.transpose().multiply(u.transpose()) is the same as u.multiply(vt).transpose(), but the latter is faster. However, since vt and u are small, that shouldn't change much.

    – Socowi
    Nov 22 '18 at 14:32













  • How many point pairs do you have?

    – Nico Schertler
    Nov 22 '18 at 17:36











  • Normally around 7, 10 is the maximum

    – Noltibus
    Nov 22 '18 at 18:14






  • 1





    Ok, then 50ms is very slow, indeed. I think the most important factor is the allocation of new memory as @Socowi pointed out. If that does not help, you need to profile where the time is spent. Minor thing: You can calculate the determinant of a 3x3 matrix directly and do not need to calculate a LU decomposition.

    – Nico Schertler
    Nov 22 '18 at 18:37








2




2





In src.subtract(tile(centroidSrc, n)) you create a new matrix just for the subtraction and then a new one for the result, even though you don't use src later. It would probably be faster to write a method void subtractFromEachRow(RealMatrix minuend, RealMatrix subtrahendRow) that does the subtraction without creating any new matrices. Modify src using the addToEntry method.

– Socowi
Nov 22 '18 at 14:26







In src.subtract(tile(centroidSrc, n)) you create a new matrix just for the subtraction and then a new one for the result, even though you don't use src later. It would probably be faster to write a method void subtractFromEachRow(RealMatrix minuend, RealMatrix subtrahendRow) that does the subtraction without creating any new matrices. Modify src using the addToEntry method.

– Socowi
Nov 22 '18 at 14:26















Also vt.transpose().multiply(u.transpose()) is the same as u.multiply(vt).transpose(), but the latter is faster. However, since vt and u are small, that shouldn't change much.

– Socowi
Nov 22 '18 at 14:32







Also vt.transpose().multiply(u.transpose()) is the same as u.multiply(vt).transpose(), but the latter is faster. However, since vt and u are small, that shouldn't change much.

– Socowi
Nov 22 '18 at 14:32















How many point pairs do you have?

– Nico Schertler
Nov 22 '18 at 17:36





How many point pairs do you have?

– Nico Schertler
Nov 22 '18 at 17:36













Normally around 7, 10 is the maximum

– Noltibus
Nov 22 '18 at 18:14





Normally around 7, 10 is the maximum

– Noltibus
Nov 22 '18 at 18:14




1




1





Ok, then 50ms is very slow, indeed. I think the most important factor is the allocation of new memory as @Socowi pointed out. If that does not help, you need to profile where the time is spent. Minor thing: You can calculate the determinant of a 3x3 matrix directly and do not need to calculate a LU decomposition.

– Nico Schertler
Nov 22 '18 at 18:37





Ok, then 50ms is very slow, indeed. I think the most important factor is the allocation of new memory as @Socowi pointed out. If that does not help, you need to profile where the time is spent. Minor thing: You can calculate the determinant of a 3x3 matrix directly and do not need to calculate a LU decomposition.

– Nico Schertler
Nov 22 '18 at 18:37












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432155%2fmore-efficient-rigid-3d-transformation-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432155%2fmore-efficient-rigid-3d-transformation-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Fotorealismo