]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/Contour/vectorFunctions.cxx
d8878702ccc05263eee527a882913869685f6bc5
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / Contour / vectorFunctions.cxx
1 #include "vectorFunctions.h"
2 #include <stdio.h>
3
4 //Constructor
5 vectorFunctions::vectorFunctions()
6 {
7 }
8 //Destructor
9 vectorFunctions::~vectorFunctions()
10 {
11 }
12
13 //---------------------------------------------------------------
14 //Copy Vector1 in Vector2 (double)
15 void vectorFunctions::copyVector(std::vector<double>*Vector1,std::vector<double>*Vector2)
16 {
17         int size = Vector1->size();
18         Vector2->clear();
19         int i;
20         if(size != 0)
21         {
22                 for(i=0; i<size; i++)
23                 {
24                         Vector2->push_back( (*Vector1)[i] );
25                 }
26         }
27 }
28 //---------------------------------------------------------------
29 //Copy Vector1 in Vector2 (int)
30 void vectorFunctions::copyintVector(std::vector<int>*Vector1,std::vector<int>*Vector2)
31 {
32         int size = Vector1->size();
33         Vector2->clear();
34         int i;
35         if( size != 0)
36         {
37                 for(i=0; i<size; i++)
38                 {
39                         Vector2->push_back( (*Vector1)[i] );
40                 }
41         }
42 }
43 //------------------------------------------------------------------------------------------------------------------------------------------
44 //Print 2 vectors of the same size
45 void vectorFunctions::printVector(std::vector<double>*Vector1,std::vector<double>*Vector2)
46 {
47         int i;
48         if( Vector1->size() == Vector2->size() )
49         {
50                 for(i=0; i<Vector1->size(); i++)
51                 {
52                         printf("\n v1(%d) = %f, v2(%d) = %f",i,(*Vector1)[i],i,(*Vector2)[i]);
53                 }
54         }
55 }
56 //------------------------------------------------------------------------------------------------------------------------------------------
57 //Returns the average value of the vector
58 double vectorFunctions::promVector(std::vector<double>*Vector1, bool OnNormal)
59 {
60         int i;
61         double suma = 0,prom;
62         std::vector<double> tempv;
63         int size = Vector1->size();
64         if(size != 0)
65         {
66                 if(OnNormal == false)
67                 {
68                         for(i=0; i<size; i++)
69                         {
70                                 suma = suma + (*Vector1)[i];
71                         }
72                         return prom = suma/Vector1->size();
73                 }
74                 int pos;
75                 double maxval;
76                 copyVector(Vector1,&tempv);
77                 if(OnNormal == true)
78                 {
79                         pos = maxVector(Vector1,&maxval);
80                         for(i=0; i<size; i++)
81                         {
82                                 tempv.push_back((*Vector1)[i]/maxval);
83                         }
84                         suma = 0;
85                         for(i=0; i<tempv.size(); i++)
86                         {
87                                 suma = suma + tempv[i];
88                         }
89                         return prom = suma/tempv.size();
90                 }
91         }
92         return -1;
93 }
94 //------------------------------------------------------------------------------------------------------------------------------------------
95 //Returns the maximum value of the vector
96 int vectorFunctions::maxVector(std::vector<double>*Vector1,double *val)
97 {
98         int i, pos;
99         double max = -1;
100         if(Vector1->size() != 0)
101         {
102                 for(i=0; i<Vector1->size(); i++)
103                 {
104                         if( (*Vector1)[i]>max )
105                         {
106                                 max = (*Vector1)[i];
107                                 pos = i;
108                         }
109                 }
110                 *val = max;
111                 return pos;
112         }
113         return -1;
114 }
115 //------------------------------------------------------------------------------------------------------------------------------------------
116 //Returns the minimum value of the vector
117 int vectorFunctions::minVector(std::vector<double>*Vector1,double *val)
118 {
119         int i, pos;
120         double min = 99999;
121         if(Vector1->size() != 0)
122         {
123                 for(i=0; i<Vector1->size(); i++)
124                 {
125                         if( (*Vector1)[i]<min )
126                         {
127                                 min = (*Vector1)[i];
128                                 pos = i;
129                         }
130                 }
131                 *val = min;
132                 return pos;
133         }
134         return -1;
135 }
136 //------------------------------------------------------------------------------------------------------------------------------------------
137 //Find the minimal distance between an input point and an input lists of points
138 int vectorFunctions::nearPoint(std::vector<double>*VectorX, std::vector<double>*VectorY, double px, double py)
139 {
140         int i, pos = -1;
141         double min = 10000, dist;
142         if(VectorX->size() != 0)
143         {
144                 for(i=0; i<VectorX->size(); i++)
145                 {
146                         dist = sqrt(pow(px-(*VectorX)[i],2) + pow(py-(*VectorY)[i],2));
147                         if(dist<min)
148                         {
149                                 min = dist;
150                                 pos = i;
151                         }
152                 }
153                 return pos;
154         }
155         return -1;
156 }
157 //------------------------------------------------------------------------------------------------------------------------------------------
158 //Returns the position of the point in the lsts. (Aprox. + or - 1).
159 int vectorFunctions::findPointInLst(std::vector<double>*vecX, std::vector<double>*vecY, std::vector<double>*vecZ, 
160                                                                         double x, double y, double z)
161 {
162         int i,flag = -1;
163         double apr = 1.5;
164         if(vecX->size() != 0)
165         {
166                 for(i=0; i<vecX->size(); i++)
167                 {
168                         if( ((*vecX)[i]-apr <= x)&&(x <=(*vecX)[i]+apr)&&((*vecY)[i]-apr <= y)&&(y <=(*vecY)[i]+apr)&&((*vecZ)[i]-apr <= z)&&(z <=(*vecZ)[i]+apr) )
169                         {
170                                 flag = i;
171                                 return i; 
172                         }
173                 }
174                 return flag;
175         }
176         return -1;
177 }