1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/***************************************************
Author : Shashank Shekhar
**************************************************/
#include <bits/stdc++.h>
using namespace std;
extern "C"
{
#include "api_scilab.h"
#include "Scierror.h"
#include "BOOL.h"
#include <localization.h>
#include "sciprint.h"
int rotationMTV(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int *piAddr = NULL;
int iRows = 0;
int iCols = 0;
int *piLen = 0;
char **pstData = NULL;
double RotMat[9];
double *temp = NULL;
int noOfarguments = *getNbInputArgument(pvApiCtx);
double outMat[3];
double theta;
int i;
CheckInputArgument(pvApiCtx,1,1);
CheckOutputArgument(pvApiCtx,1,1);
sciErr = getVarAddressFromPosition(pvApiCtx,1,&piAddr);
if (sciErr.iErr)
{
printError(&scisErr, 0);
return 0;
}
if(!isDoubleType(pvApiCtx, piAddr) || isVarComplex(pvApiCtx, piAddr))
{
Scierror(999,"A 3x3 Matrix expected.\n");
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &temp);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(iRows!=3 || iCols!=3)
{
Scierror(999,"Invalid Argument\n");
return 0;
}
for(i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
RotMat[j*3+i]=temp[i*3+j];
}
}
// ----------------------------------------------------------- Evaluation --------------------------------------------------------
double myMat[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
myMat[i][j] = RotMat[i*3+j];
double trace = RotMat[0]+RotMat[4]+RotMat[8];
theta = acos((trace-1)/2);
outMat[0] = myMat[2][1]-myMat[1][2];
outMat[1] = myMat[0][2]-myMat[2][0];
outMat[2] = myMat[1][0]-myMat[0][1];
double element;
double threshold = 1e-4;
if(sin(theta) >= threshold)
{
element = theta/(2*sin(theta));
for(int i=0;i<3;i++)
outMat[i]=outMat[i]*element;
}
else if (theta-1 > 0)
{
element = (0.5-(theta-3)/12);
for(int i=0;i<3;i++)
outMat[i]=outMat[i]*element;
}
else
{
double myMax=myMat[0][0];
int iter1=0;
int iter2,iter3;
for(int i=0;i<3;i++)
{
if(myMat[i][i]>myMax)
{
myMax=myMat[i][i];
iter1=i;
}
}
iter2 = (iter1%2) +1;
iter3 = ((iter1+1)%2) +1;
double ev = sqrt(myMat[iter1][iter1] - myMat[iter2][iter2] - myMat[iter3][iter3] + 1);
outMat[0] = ev/2;
outMat[1] = (myMat[iter2][iter1] + myMat[iter1][iter2])/(2*ev);
outMat[2] = (myMat[iter3][iter1] + myMat[iter1][iter3])/(2*ev);
for(int i=0;i<3;i++)
ev+=pow(outMat[i],2);
ev = sqrt(ev);
for(int i=0;i<3;i++)
outMat[i]=(outMat[i]/ev)*theta;
}
// --------------------------------------------------------Creating 1x3 Mat to Return -------------------------------------------------------------
sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 3,1,outMat);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
ReturnArguments(pvApiCtx);
return 0;
}
}
|