1 /*
2  * Copyright (C) 2014 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 #ifndef LATINIME_NORMAL_DISTRIBUTION_2D_H
18 #define LATINIME_NORMAL_DISTRIBUTION_2D_H
19 
20 #include <cmath>
21 
22 #include "defines.h"
23 #include "suggest/core/layout/geometry_utils.h"
24 #include "suggest/core/layout/normal_distribution.h"
25 
26 namespace latinime {
27 
28 // Normal distribution on a 2D plane. The covariance is always zero, but the distribution can be
29 // rotated.
30 class NormalDistribution2D {
31  public:
NormalDistribution2D(const float uX,const float sigmaX,const float uY,const float sigmaY,const float theta)32     NormalDistribution2D(const float uX, const float sigmaX, const float uY, const float sigmaY,
33             const float theta)
34             : mXDistribution(0.0f, sigmaX), mYDistribution(0.0f, sigmaY), mUX(uX), mUY(uY),
35               mSinTheta(sinf(theta)), mCosTheta(cosf(theta)) {}
36 
getProbabilityDensity(const float x,const float y)37     float getProbabilityDensity(const float x, const float y) const {
38         // Shift
39         const float shiftedX = x - mUX;
40         const float shiftedY = y - mUY;
41         // Rotate
42         const float rotatedShiftedX = mCosTheta * shiftedX + mSinTheta * shiftedY;
43         const float rotatedShiftedY = -mSinTheta * shiftedX + mCosTheta * shiftedY;
44         return mXDistribution.getProbabilityDensity(rotatedShiftedX)
45                 * mYDistribution.getProbabilityDensity(rotatedShiftedY);
46     }
47 
48  private:
49     DISALLOW_IMPLICIT_CONSTRUCTORS(NormalDistribution2D);
50 
51     const NormalDistribution mXDistribution;
52     const NormalDistribution mYDistribution;
53     const float mUX;
54     const float mUY;
55     const float mSinTheta;
56     const float mCosTheta;
57 };
58 } // namespace latinime
59 #endif // LATINIME_NORMAL_DISTRIBUTION_2D_H
60