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