1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* $Id: db_utilities_geometry.h,v 1.3 2011/06/17 14:03:31 mbansal Exp $ */
18 
19 #ifndef DB_UTILITIES_GEOMETRY_H
20 #define DB_UTILITIES_GEOMETRY_H
21 
22 #include "db_utilities.h"
23 
24 
25 
26 /*****************************************************************
27 *    Lean and mean begins here                                   *
28 *****************************************************************/
29 /*! Get the inhomogenous 2D-point centroid of nr_point inhomogenous
30 points in X*/
db_PointCentroid2D(double c[2],const double * X,int nr_points)31 inline void db_PointCentroid2D(double c[2],const double *X,int nr_points)
32 {
33     int i;
34     double cx,cy,m;
35 
36     cx=0;cy=0;
37     for(i=0;i<nr_points;i++)
38     {
39         cx+= *X++;
40         cy+= *X++;
41     }
42     if(nr_points)
43     {
44         m=1.0/((double)nr_points);
45         c[0]=cx*m;
46         c[1]=cy*m;
47     }
48     else c[0]=c[1]=0;
49 }
50 
db_PointCentroid2D(double c[2],const double * const * X,int nr_points)51 inline void db_PointCentroid2D(double c[2],const double * const *X,int nr_points)
52 {
53     int i;
54     double cx,cy,m;
55     const double *temp;
56 
57     cx=0;cy=0;
58     for(i=0;i<nr_points;i++)
59     {
60         temp= *X++;
61         cx+=temp[0];
62         cy+=temp[1];
63     }
64     if(nr_points)
65     {
66         m=1.0/((double)nr_points);
67         c[0]=cx*m;
68         c[1]=cy*m;
69     }
70     else c[0]=c[1]=0;
71 }
72 
73 /*! Get the inhomogenous 3D-point centroid of nr_point inhomogenous
74 points in X*/
db_PointCentroid3D(double c[3],const double * X,int nr_points)75 inline void db_PointCentroid3D(double c[3],const double *X,int nr_points)
76 {
77     int i;
78     double cx,cy,cz,m;
79 
80     cx=0;cy=0;cz=0;
81     for(i=0;i<nr_points;i++)
82     {
83         cx+= *X++;
84         cy+= *X++;
85         cz+= *X++;
86     }
87     if(nr_points)
88     {
89         m=1.0/((double)nr_points);
90         c[0]=cx*m;
91         c[1]=cy*m;
92         c[2]=cz*m;
93     }
94     else c[0]=c[1]=c[2]=0;
95 }
96 
db_PointCentroid3D(double c[3],const double * const * X,int nr_points)97 inline void db_PointCentroid3D(double c[3],const double * const *X,int nr_points)
98 {
99     int i;
100     double cx,cy,cz,m;
101     const double *temp;
102 
103     cx=0;cy=0;cz=0;
104     for(i=0;i<nr_points;i++)
105     {
106         temp= *X++;
107         cx+=temp[0];
108         cy+=temp[1];
109         cz+=temp[2];
110     }
111     if(nr_points)
112     {
113         m=1.0/((double)nr_points);
114         c[0]=cx*m;
115         c[1]=cy*m;
116         c[2]=cz*m;
117     }
118     else c[0]=c[1]=c[2]=0;
119 }
120 
121 #endif /* DB_UTILITIES_GEOMETRY_H */
122