[Tizen] Add InterceptKeyEvent
[platform/core/uifw/dali-core.git] / dali / internal / event / common / projection.cpp
1 /*
2  * Copyright (c) 2021 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/integration-api/debug.h>
23 #include <dali/public-api/math/math-utils.h>
24 #include <dali/public-api/math/matrix.h>
25 #include <dali/public-api/math/rect.h>
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/public-api/math/vector4.h>
28 #include <dali/public-api/math/viewport.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 bool Unproject(const Vector4& windowPos,
35                const Matrix&  inverseMvp,
36                float          viewportWidth,
37                float          viewportHeight,
38                Vector4&       objectPos)
39 {
40   objectPos.x = windowPos.x;
41   objectPos.y = windowPos.y;
42   objectPos.z = windowPos.z;
43   objectPos.w = 1.0f;
44
45   objectPos.x = objectPos.x / viewportWidth;
46   objectPos.y = objectPos.y / viewportHeight;
47
48   objectPos.x = objectPos.x * 2.0f - 1.0f;
49   objectPos.y = objectPos.y * 2.0f - 1.0f;
50   objectPos.z = objectPos.z * 2.0f - 1.0f;
51
52   objectPos = inverseMvp * objectPos;
53
54   // In the case where objectPos.w is exactly zero, the unproject fails
55   if(EqualsZero(objectPos.w))
56   {
57     return false;
58   }
59
60   objectPos.x /= objectPos.w;
61   objectPos.y /= objectPos.w;
62   objectPos.z /= objectPos.w;
63
64   return true;
65 }
66
67 bool UnprojectFull(const Vector4& windowPos,
68                    const Matrix&  modelView,
69                    const Matrix&  projection,
70                    float          viewportWidth,
71                    float          viewportHeight,
72                    Vector4&       objectPos)
73 {
74   Matrix invertedMvp(false); // Don't initialize.
75   Matrix::Multiply(invertedMvp, modelView, projection);
76
77   if(invertedMvp.Invert())
78   {
79     return Unproject(windowPos, invertedMvp, viewportWidth, viewportHeight, objectPos);
80   }
81
82   return false;
83 }
84
85 bool XyPlaneIntersect(const Vector4& pointA, const Vector4& pointB, Vector4& intersect)
86 {
87   const Vector4* near = nullptr;
88   const Vector4* far  = nullptr;
89
90   if(pointA.z > 0.0f && pointB.z < 0.0f)
91   {
92     near = &pointA;
93     far  = &pointB;
94   }
95   else if(pointB.z > 0.0f && pointA.z < 0.0f)
96   {
97     near = &pointB;
98     far  = &pointA;
99   }
100   else
101   {
102     return false; // ray does not cross xy plane
103   }
104
105   float dist = near->z / (near->z - far->z);
106
107   intersect.x = near->x + (far->x - near->x) * dist;
108   intersect.y = near->y + (far->y - near->y) * dist;
109   intersect.z = 0.0f;
110
111   return true;
112 }
113
114 bool ProjectFull(const Vector4& position,
115                  const Matrix&  modelView,
116                  const Matrix&  projection,
117                  float          viewportX,
118                  float          viewportY,
119                  float          viewportWidth,
120                  float          viewportHeight,
121                  Vector4&       windowPos)
122 {
123   bool ok = false;
124
125   Matrix Mvp(false); // Don't initialize.
126   Matrix::Multiply(Mvp, modelView, projection);
127
128   Vector4 p = Mvp * position;
129
130   Vector2 depthRange(0, 1);
131
132   if(!EqualsZero(p.w))
133   {
134     float div = 1.0f / p.w;
135
136     windowPos = Vector4((1 + p.x * div) * viewportWidth / 2 + viewportX,
137                         (1 - p.y * div) * viewportHeight / 2 + viewportY,
138                         (p.z * div) * (depthRange.y - depthRange.x) + depthRange.x,
139                         div);
140     ok        = true;
141   }
142
143   return ok;
144 }
145
146 } // namespace Internal
147
148 } // namespace Dali