grasp
All Data Structures Namespaces Functions Modules
helpers.cpp
1 /* Copyright: (C) 2014 iCub Facility - Istituto Italiano di Tecnologia
2  * Authors: Ilaria Gori
3  * email: ilaria.gori@iit.it
4  * Permission is granted to copy, distribute, and/or modify this program
5  * under the terms of the GNU General Public License, version 2 or any
6  * later version published by the Free Software Foundation.
7  *
8  * A copy of the license can be found in the file LICENSE located in the
9  * root directory.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details
15 */
16 
17 #include <iCub/data3D/private/helpers.h>
18 
19 using namespace std;
20 using namespace yarp::sig;
21 using namespace yarp::math;
22 
23 /************************************************************************/
24 void Helpers::min(const Matrix &mat, yarp::sig::Vector &out)
25 {
26  out.resize(mat.cols());
27  double minx=10000;
28  double miny=10000;
29  double minz=10000;
30  for (int i=0; i<mat.rows(); i++)
31  {
32  if (mat(i,0)<minx)
33  minx=mat(i,0);
34  if (mat(i,1)<miny)
35  miny=mat(i,1);
36  if (mat.cols()>2 && mat(i,2)<minz)
37  minz=mat(i,2);
38  }
39  out[0]=minx;
40  out[1]=miny;
41  if (mat.cols()>2)
42  out[2]=minz;
43 }
44 
45 /************************************************************************/
46 void Helpers::max(const Matrix &mat, yarp::sig::Vector &out)
47 {
48  out.resize(mat.cols());
49  double maxx=-10000;
50  double maxy=-10000;
51  double maxz=-10000;
52  for (int i=0; i<mat.rows(); i++)
53  {
54  if (mat(i,0)>maxx)
55  maxx=mat(i,0);
56  if (mat(i,1)>maxy)
57  maxy=mat(i,1);
58  if (mat.cols()>2 && mat(i,2)>maxz)
59  maxz=mat(i,2);
60  }
61  out[0]=maxx;
62  out[1]=maxy;
63  if (mat.cols()>2)
64  out[2]=maxz;
65 }
66 
67 /************************************************************************/
68 double Helpers::mod(const double a, const double b)
69 {
70  int result=floor(a/b);
71  return (a-static_cast<float>(result)*b);
72 }
73 
74 /************************************************************************/
75 double Helpers::sign(const double value)
76 {
77  double s=0.0;
78 
79  if (value>0)
80  s=1.0;
81  else if (value<0)
82  s=-1.0;
83 
84  return s;
85 }
86 
87 /************************************************************************/
88 yarp::sig::Vector Helpers::extractSubVector(const yarp::sig::Vector &vect, const int i, const int j)
89 {
90  yarp::sig::Vector out(2);
91  out[0]=vect[i];
92  out[1]=vect[j];
93  return out;
94 }
95