-
Notifications
You must be signed in to change notification settings - Fork 5
/
mathutility.cpp
280 lines (205 loc) · 6.83 KB
/
mathutility.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**************************************************************************
(c)Jothybasu Selvaraj
This program comes with no gaurantee of it accuracy.
Clinical decisions should not be made using this program
**************************************************************************/
#include "mathutility.h"
#include<vtkMath.h>
#include<time.h>
#include<vector>
#include<numeric>
#include<cmath>
#include<vtkPiecewiseFunction.h>
#include<vtkSmartPointer.h>
#include<QDebug>
MathUtility::MathUtility()
{
//vtkMath::RandomSeed(time(NULL));
}
//int MathUtility::getGaussRandomInt(double mean,double std)
//{
// int rand=vtkMath::Round(vtkMath::Gaussian(mean,std));
// return rand;
//}
//std::vector<int> MathUtility::getArrayOfGaussRandomInts(unsigned int length,double mean, double std)
//{
// std::vector<int>randArray;
// for (unsigned int i=0;i<length;i++)
// {
// randArray.push_back(this->getGaussRandomInt(mean,std));
// }
// return randArray;
//}
//std::vector<int> MathUtility::getGaussRandomSys(unsigned int fractions,double mean,double std)
//{
// int x=this->getGaussRandomInt(mean,std);
// std::vector<int>sysVectors; //Initialize to 0;
// for (unsigned int i=0;i<fractions;i++)
// {
// sysVectors.push_back(x);
// }
// return sysVectors;
//}
//std::vector<int> MathUtility::getGaussRandomRand(unsigned int fractions,double mean,double std)
//{
// std::vector<int>randVectors;
// for (unsigned int i=0;i<fractions;i++)
// {
// int x=this->getGaussRandomInt(mean,std);
// //qDebug()<<x<<"Random";
// randVectors.push_back(x);
// }
// return randVectors;
//}
//std::vector<int> MathUtility::addTwoIntArrays(std::vector<int> array1,std::vector<int>array2)
//{
// unsigned int array1Size=array1.size();
// unsigned int array2Size=array2.size();
// std::vector<int>summedArray;
// //Check sizes are equal
// if(array1Size!=array2Size)
// {
// qDebug()<<"Array sizes do not match!";
// }
// else
// {
// for (unsigned int i=0;i<array1Size;i++)
// {
// int sum=array1[i]+array2[i];
// summedArray.push_back(sum);
// }
// }
// return summedArray;
//}
//std::vector<double> MathUtility::addTwoDoubleArrays(std::vector<double> array1,std::vector<double>array2)
//{
// unsigned int array1Size=array1.size();
// unsigned int array2Size=array2.size();
// std::vector<double>summedArray;
////Check sizes are equal
// if(array1Size!=array2Size)
// {
// qDebug()<<"Array sizes do not match!";
// }
// else
// {
// for (unsigned int i=0;i<array1Size;i++)
// {
// double sum=array1[i]+array2[i];
// summedArray.push_back(sum);
// }
// }
// return summedArray;
//}
//std::vector<double> MathUtility::divideDoubleArrayByInt(std::vector<double> array,int dividerIn)
//{
// unsigned int arraySize=array.size();
// std::vector<double>dividedArray;
// for (unsigned int i=0;i<arraySize;i++)
// {
// double divider=static_cast<double>(dividerIn);
// double sum=array[i]/divider;
// dividedArray.push_back(sum);
// }
// return dividedArray;
//}
//std::vector<int> MathUtility::addConstIntToIntArray(std::vector<int> input,int num)
//{
// unsigned int size=input.size();
// for(unsigned int i=0;i<size;i++)
// {
// input[i]=input[i]+num;
// }
// return input;
//}
//double MathUtility::productOfVector(std::vector<double>input)
//{
// int size=input.size();
// double result=1.0;
// for (int i=0;i<size;i++)
// {
// result=result*input[i];
// }
// return result;
//}
double MathUtility::calcMean(std::vector<double> input)
{
double mean=std::accumulate(input.begin(),input.end(),0.0)/input.size();
return mean;
}
double MathUtility::calcStd(std::vector<double> input)
{
double mean=this->calcMean(input);
unsigned int size=input.size();
std::vector<double>difference;
for(int i=0;i<size;i++)
{
difference.push_back(std::pow((input[i]-mean),2));
}
double sum=std::accumulate(difference.begin(),difference.end(),0.0);
double std=std::sqrt((sum/difference.size()));
return std;
}
double MathUtility::calcSEM(std::vector<double> input)
{
double std=this->calcStd(input);
//qDebug()<<std<<"std"<<std::sqrt(input.size())<<"sqrt";
double sem=std/std::sqrt(input.size());
return sem;
}
int MathUtility::getRandomIntWithinLimits(int lowerLimit, int upperLimit)
{
int randInt=static_cast<int>(vtkMath::Random(lowerLimit,upperLimit));
return randInt;
}
//double MathUtility::distanceBetweenTwoPoints(double p1[],double p2[])//target,src
//{
// double d12 = sqrt(((p1[0] - p2[0]) * (p1[0] - p2[0]))+
// ( (p1[1] - p2[1]) * (p1[1] - p2[1]))+
// ((p1[2] - p2[2]) * (p1[2] - p2[2])));
// //qDebug()<<d12<<"dconv";
// return d12;
//}
//void MathUtility::extendRay(double raySrc[3],double rayTarget[3],double length)
//{
// double dist=this->distanceBetweenTwoPoints(raySrc,rayTarget);
// double xDiff=(rayTarget[0]-raySrc[0])/dist;
// double yDiff=(rayTarget[1]-raySrc[1])/dist;
// double zDiff=(rayTarget[2]-raySrc[2])/dist;
// this->extendedRay[0]=(xDiff*length)+rayTarget[0];
// this->extendedRay[1]=(yDiff*length)+rayTarget[1];
// this->extendedRay[2]=(zDiff*length)+rayTarget[2];
// //qDebug()<<extendedRay[0]<<extendedRay[1]<<extendedRay[2];
//}
void MathUtility::pieceWiseInterpolate(std::vector<double>xPts,std::vector<double>yPts)
{
vtkSmartPointer<vtkPiecewiseFunction>interpolator=
vtkSmartPointer<vtkPiecewiseFunction>::New();
int size=xPts.size();
qDebug()<<size;
for(int i=0;i<size;i++)
{
interpolator->AddPoint(xPts[i],yPts[i]);
}
//qDebug()<<interpolator->GetValue(500.0);
}
std::vector<int> MathUtility::xyzTOijk(double xyz[3], double* origin, double *spacing)
{
std::vector<int>ijk;
ijk.push_back(vtkMath::Round((xyz[0]-(origin[0]))/spacing[0]));
ijk.push_back(vtkMath::Round((xyz[1]-(origin[1]))/spacing[1]));
ijk.push_back(vtkMath::Round((xyz[2]-(origin[2]))/spacing[2]));
return ijk;
}
std::vector<double> MathUtility::extendVertex(double vertexCoords[3],double ROIOrigin[3], double distance)
{
double distanceFromOrigin= std::sqrt((std::pow((vertexCoords[0]-ROIOrigin[0]),2)+std::pow((vertexCoords[1]-ROIOrigin[1]),2)+std::pow((vertexCoords[2]-ROIOrigin[2]),2)));
double x=(vertexCoords[0]/distanceFromOrigin)*(distanceFromOrigin+distance);
double y=(vertexCoords[1]/distanceFromOrigin)*(distanceFromOrigin+distance);
double z=(vertexCoords[2]/distanceFromOrigin)*(distanceFromOrigin+distance);
std::vector<double>extendedVertex;
extendedVertex.push_back(x);
extendedVertex.push_back(y);
extendedVertex.push_back(z);
return extendedVertex;
}