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