Klocwork fixes.
[platform/core/uifw/dali-core.git] / dali / internal / event / common / projection.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/event/common/projection.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/public-api/math/rect.h>
22 #include <dali/public-api/math/matrix.h>
23 #include <dali/public-api/math/vector4.h>
24 #include <dali/integration-api/debug.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 bool Unproject( const Vector4& windowPos,
33                 const Matrix& inverseMvp,
34                 float viewportWidth,
35                 float viewportHeight,
36                 Vector4& objectPos )
37 {
38   objectPos.x = windowPos.x;
39   objectPos.y = windowPos.y;
40   objectPos.z = windowPos.z;
41   objectPos.w = 1.0f;
42
43   objectPos.x = objectPos.x / viewportWidth;
44   objectPos.y = objectPos.y / viewportHeight;
45
46   objectPos.x = objectPos.x * 2.0f - 1.0f;
47   objectPos.y = objectPos.y * 2.0f - 1.0f;
48   objectPos.z = objectPos.z * 2.0f - 1.0f;
49
50   objectPos = inverseMvp * objectPos;
51
52   // In the case where objectPos.w is exactly zero, the unproject fails
53   if ( EqualsZero( objectPos.w ) )
54   {
55     return false;
56   }
57
58   objectPos.x /= objectPos.w;
59   objectPos.y /= objectPos.w;
60   objectPos.z /= objectPos.w;
61
62   return true;
63 }
64
65 bool UnprojectFull( const Vector4& windowPos,
66                     const Matrix& modelView,
67                     const Matrix& projection,
68                     float viewportWidth,
69                     float viewportHeight,
70                     Vector4& objectPos )
71 {
72   Matrix invertedMvp( false ); // Don't initialize.
73   Matrix::Multiply( invertedMvp, modelView, projection );
74
75   if (invertedMvp.Invert())
76   {
77     return Unproject( windowPos, invertedMvp, viewportWidth, viewportHeight, objectPos );
78   }
79
80   return false;
81 }
82
83 bool XyPlaneIntersect( const Vector4& pointA, const Vector4& pointB, Vector4& intersect )
84 {
85   const Vector4* near = NULL;
86   const Vector4* far = NULL;
87
88   if ( pointA.z > 0.0f && pointB.z < 0.0f )
89   {
90     near = &pointA;
91     far  = &pointB;
92   }
93   else if ( pointB.z > 0.0f && pointA.z < 0.0f )
94   {
95     near = &pointB;
96     far  = &pointA;
97   }
98   else
99   {
100     return false; // ray does not cross xy plane
101   }
102
103   float dist = near->z / (near->z - far->z);
104
105   intersect.x = near->x + (far->x - near->x) * dist;
106   intersect.y = near->y + (far->y - near->y) * dist;
107   intersect.z = 0.0f;
108
109   return true;
110 }
111
112 } // namespace Internal
113
114 } // namespace Dali