// $scope, $element, $attrs, $injector, $sce, $timeout, $http, $ionicPopup, and $ionicPopover services are available
let sin = Math.sin;
let cos = Math.cos
let PI = 3.141;
var stack = [];
// Information needed for rotation
class WidgetInfo
{
constructor(initialX,initialY,initialZ,name){
this.initialX = initialX;
this.initialY = initialY;
this.initialZ = initialZ;
this.name = name;
}
}
$scope.GetScaleMatrix = function(w,h,d) {
return [
w, 0, 0, 0,
0, h, 0, 0,
0, 0, d, 0,
0, 0, 0, 1
];
}
$scope.GetRotationMatrixAroundXAxis = function(a) {
a = a * (3.141 / 180);
return [
1, 0, 0, 0,
0, cos(a), -sin(a), 0,
0, sin(a), cos(a), 0,
0, 0, 0, 1
];
}
$scope.GetRotationMatrixAroundYAxis = function(a) {
a = a * (3.141 / 180);
return [
cos(a), 0, sin(a), 0,
0, 1, 0, 0,
-sin(a), 0, cos(a), 0,
0, 0, 0, 1
];
}
$scope.GetRotationMatrixAroundZAxis = function(a) {
a = a * (3.141 / 180);
return [
cos(a), -sin(a), 0, 0,
sin(a), cos(a), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
];
}
$scope.GetTranslationMatrix = function(x,y,z) {
return [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
x, y, z, 1
];
}
$scope.GetIdentityMatrix = function() {
return [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
];
}
function multiplyMatrixAndPoint(matrix, point) {
// Give a simple variable name to each part of the matrix, a column and row number
let c0r0 = matrix[ 0], c1r0 = matrix[ 1], c2r0 = matrix[ 2], c3r0 = matrix[ 3];
let c0r1 = matrix[ 4], c1r1 = matrix[ 5], c2r1 = matrix[ 6], c3r1 = matrix[ 7];
let c0r2 = matrix[ 8], c1r2 = matrix[ 9], c2r2 = matrix[10], c3r2 = matrix[11];
let c0r3 = matrix[12], c1r3 = matrix[13], c2r3 = matrix[14], c3r3 = matrix[15];
// Now set some simple names for the point
let x = point[0];
let y = point[1];
let z = point[2];
let w = point[3];
// Multiply the point against each part of the 1st column, then add together
let resultX = (x * c0r0) + (y * c0r1) + (z * c0r2) + (w * c0r3);
// Multiply the point against each part of the 2nd column, then add together
let resultY = (x * c1r0) + (y * c1r1) + (z * c1r2) + (w * c1r3);
// Multiply the point against each part of the 3rd column, then add together
let resultZ = (x * c2r0) + (y * c2r1) + (z * c2r2) + (w * c2r3);
// Multiply the point against each part of the 4th column, then add together
let resultW = (x * c3r0) + (y * c3r1) + (z * c3r2) + (w * c3r3);
return [resultX, resultY, resultZ, resultW];
}
function multiplyMatrices(matrixA, matrixB) {
// Slice the second matrix up into rows
let row0 = [matrixB[ 0], matrixB[ 1], matrixB[ 2], matrixB[ 3]];
let row1 = [matrixB[ 4], matrixB[ 5], matrixB[ 6], matrixB[ 7]];
let row2 = [matrixB[ 8], matrixB[ 9], matrixB[10], matrixB[11]];
let row3 = [matrixB[12], matrixB[13], matrixB[14], matrixB[15]];
// Multiply each row by matrixA
let result0 = multiplyMatrixAndPoint(matrixA, row0);
let result1 = multiplyMatrixAndPoint(matrixA, row1);
let result2 = multiplyMatrixAndPoint(matrixA, row2);
let result3 = multiplyMatrixAndPoint(matrixA, row3);
// Turn the result rows back into a single matrix
return [
result0[0], result0[1], result0[2], result0[3],
result1[0], result1[1], result1[2], result1[3],
result2[0], result2[1], result2[2], result2[3],
result3[0], result3[1], result3[2], result3[3]
];
}
$scope.UpdateImagePosition = function() {
let transformMatrix = $scope.GetIdentityMatrix();
for(i=0; i<stack.length; i++)
{
name = stack[i].name;
initialX = stack[i].initialX;
initialY = stack[i].initialY;
initialZ = stack[i].initialZ;
let transformMatrix = $scope.GetIdentityMatrix();
transformMatrix = multiplyMatrices(transformMatrix , $scope.GetTranslationMatrix( $scope.view.wdg['model-1'].x, $scope.view.wdg['model-1'].y,$scope.view.wdg['model-1'].z));
transformMatrix = multiplyMatrices(transformMatrix , $scope.GetRotationMatrixAroundYAxis(-$scope.view.wdg['model-1'].ry));
transformMatrix = multiplyMatrices(transformMatrix , $scope.GetTranslationMatrix( initialX, initialY, initialZ));
$scope.view.wdg[name].x = transformMatrix [12];
$scope.view.wdg[name].y = transformMatrix [13];
$scope.view.wdg[name].z = transformMatrix [14];
}
$scope.intervalPromise = $interval($scope.UpdateImagePosition, 100, 1, true);
}
$scope.UpdateTimer = function() {
}
$scope.init = function() {
$scope.AddWidgetToRotateSystem('3DImage-1');
$scope.AddWidgetToRotateSystem('3DImage-2');
$scope.UpdateImagePosition();
}
$scope.AddWidgetToRotateSystem = function(name)
{
widgetInfo = new WidgetInfo($scope.view.wdg[name].x,$scope.view.wdg[name].y,$scope.view.wdg[name].z,name);
stack.push(widgetInfo);
}
angular.element(document).ready(function () {
$scope.init();
});
I made it so that in the Init function you can add all the images that you want to rotate.
This approach is good becasue you are not forced to rotate all the widgets, instead you rotate only the ones you need.
You can see in the video below that I am rotating 2/3 3D images.