2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FUiEffects_RuntimeGraphicalSurface.cpp
19 * @brief This file contains an implementation of GraphicalSurface class methods
24 #include <FBaseSysLog.h>
25 #include <FBaseErrors.h>
26 #include "../FUiEffects_EffectErrorMessages.h"
27 #include "FUiEffects_RuntimeGraphicalSurface.h"
29 using namespace Tizen::Ui::Effects;
30 using namespace Tizen::Ui::Effects::_Utils;
32 namespace Tizen { namespace Ui { namespace Effects { namespace _Runtime
35 #define SIGN(x) (((x) >= 0) ? (1) : (-1))
38 GraphicalSurface::GraphicalSurface(long objId,
41 : ElementSurface(objId)
43 , __initTransparency(transpar)
44 , __initBitmapID(bitmapID)
45 , __initModelMtr(Mat4f::getIdentity())
49 GraphicalSurface::~GraphicalSurface(void)
54 GraphicalSurface::GetBitmapId(void) const
56 return _pRenderData->bitmapId;
60 GraphicalSurface::SetBitmapId(long bitmapId)
62 _pRenderData->bitmapId = bitmapId;
63 _pRenderData->bitmapIdChanged = true;
68 GraphicalSurface::Construct(TypeElemSurfaceCollection* pElemSurfaceCollection, RenderDataSurfaceCollection* pRenderDataSurfaceCollection)
70 pRenderDataSurfaceCollection->push_back(_pRenderData.get());
75 GraphicalSurface::GetProperty(ElementProperty propName)const
77 PropertyCast propCast = {PropertyCast::NO_PROPERTY, {0} };
81 propCast.value.numberValue = _pRenderData->transparency;
82 propCast.type = PropertyCast::NUMBER;
85 propCast.type = PropertyCast::NO_PROPERTY;
92 GraphicalSurface::SetProperty(ElementProperty propName, bool propValue)
98 GraphicalSurface::SetProperty(ElementProperty propName, LUA_NUMBER propValue)
103 _pRenderData->transparency = propValue;
104 _pRenderData->transparencyChanged = true;
114 GraphicalSurface::SetProperty(ElementProperty propName, const Vec3f &propValue)
120 GraphicalSurface::Initialize(void)
122 _pRenderData->transparency = __initTransparency;
123 _pRenderData->transparencyChanged = true;
124 _pRenderData->bitmapId = __initBitmapID;
125 _pRenderData->bitmapIdChanged = true;
126 _pRenderData->modelMtr = __initModelMtr;
127 _pRenderData->modelMtrChanged = true;
132 GraphicalSurface::GetNearestPointsIds(Vec3f &position) const
134 IndicesCollection::iterator itIndex;
135 IndicesCollection::iterator itIndex1;
136 IndicesCollection::iterator itIndex2;
137 IndicesCollection::iterator itIndex3;
138 std::list<IndicesCollection::iterator> listIndeces;
139 //if there are no crossing with a triangle it returns nil (in scripts)
140 PropertyCast res = {PropertyCast::NO_PROPERTY, {0}};
141 //Finds triangles from RenderData structure which are crossed by
142 //a ray with position (position.x, position.y)
143 for (itIndex = _pRenderData->indices.begin(); itIndex != _pRenderData->indices.end(); )
145 SysAssertf(itIndex != _pRenderData->indices.end(), _UiEffectError::INTERNAL_ERROR);
146 itIndex1 = itIndex++;
148 SysAssertf(itIndex != _pRenderData->indices.end(), _UiEffectError::INTERNAL_ERROR);
149 itIndex2 = itIndex++;
151 SysAssertf(itIndex != _pRenderData->indices.end(), _UiEffectError::INTERNAL_ERROR);
152 itIndex3 = itIndex++;
154 if (InTriangle(position, *itIndex1, *itIndex2, *itIndex3))
156 listIndeces.push_back(itIndex1);
160 if (listIndeces.empty())
162 res.type = PropertyCast::NO_PROPERTY;
166 res.type = PropertyCast::VEC3;
168 std::list<IndicesCollection::iterator>::iterator itList;
169 IndicesCollection::iterator itList1;
170 IndicesCollection::iterator itList2;
171 IndicesCollection::iterator itList3;
173 IndicesCollection::iterator itVertexTriangle;
174 //Finds z-coordinates of crossing planes of triangles from RenderData structure with
175 //a ray (position.x, position.y)
176 for (itList = listIndeces.begin(); itList != listIndeces.end(); ++itList)
181 float curZ = ZInPlane(position, *itList1, *itList2, *itList3);
182 //finding maximal z-coordinate and storing corresponding triangle
183 if (maxZ < curZ || itList == listIndeces.begin())
186 itVertexTriangle = *itList;
197 GraphicalSurface::InTriangle(const Vec3f &pos, short i1, short i2, short i3) const
199 //If point pos and any triangle vertex lie on opposite sides relatively an other side,
200 //then point pos lies outside of triangle
202 unsigned short i = 0;
203 unsigned short k = 0;
204 unsigned short p = 0;
207 for (short ic = 0; ic < 3; ic++)
228 const Vec3f &point1 = _pRenderData->vnt[k].pos;
229 const Vec3f &point2 = _pRenderData->vnt[p].pos;
230 const Vec3f &pointp = _pRenderData->vnt[i].pos;
232 if (fabsf(point1.x - point2.x) > std::numeric_limits<float>::epsilon())
234 c = (pos.x - point2.x) * (point1.y - point2.y) / (point1.x - point2.x) + point2.y;
235 c1 = (pointp.x - point2.x) * (point1.y-point2.y) / (point1.x - point2.x) + point2.y;
236 if (SIGN(pos.y - c) != SIGN(pointp.y - c1))
241 else if (fabsf(point1.y - point2.y) > std::numeric_limits<float>::epsilon())
243 c = (pos.y - point2.y) * (point1.x - point2.x) / (point1.y - point2.y) + point2.x;
244 c1 = (pointp.y - point2.y) * (point1.x-point2.x) / (point1.y - point2.y) + point2.x;
245 if (SIGN(pos.x - c) != SIGN(pointp.x - c1))
259 GraphicalSurface::ZInPlane(const Vec3f &pos, short i1, short i2, short i3) const
261 //restoring the plane passing through 3 triangle vertexes
263 //Kramer's method for solving the linear equations system with 3 equations and 3 variables is used
264 const Vec3f &p1 = _pRenderData->vnt[i1].pos;
265 const Vec3f &p2 = _pRenderData->vnt[i2].pos;
266 const Vec3f &p3 = _pRenderData->vnt[i3].pos;
268 float delta = p1.x * p2.y + p3.x * p1.y + p2.x * p3.y - p3.x * p2.y - p1.x * p3.y - p2.x * p1.y;
269 float deltaA = p2.y * p1.z + p1.y * p3.z + p3.y * p2.z - p2.y * p3.z - p1.y * p2.z - p3.y * p1.z;
270 float deltaB = p1.x * p2.z + p3.x * p1.z + p2.x * p3.z - p3.x * p2.z - p2.x * p1.z - p1.x * p3.z;
271 float deltaC = p1.x * p2.y * p3.z + p3.x * p1.y * p2.z + p2.x * p3.y * p1.z - p3.x * p2.y * p1.z - p1.x * p3.y * p2.z - p2.x * p1.y * p3.z;
274 //if 3 triangle vertexes lie on vertical plane, returns maximal z-coordinate of one of these points
275 if (fabsf(delta) <= std::numeric_limits<float>::epsilon())
280 res = (t1 >= t2 ? (t1 >= t3 ? t1 : t3) : (t2 >= t3 ? t2 : t3));
284 float koefA = deltaA / delta;
285 float koefB = deltaB / delta;
286 float koefC = deltaC / delta;
288 res = koefA * pos.x + koefB * pos.y + koefC;
294 GraphicalSurface::ResetSurfaceTransformation(void)
296 LuaMat4<float> matrix4;
297 _pRenderData->modelMtr = matrix4;
298 _pRenderData->modelMtrChanged = true;
303 GraphicalSurface::SetTransformationMatrix(const LuaMatrix4& luaMatrix4)
305 _pRenderData->modelMtr = luaMatrix4;
306 _pRenderData->modelMtrChanged = true;
311 GraphicalSurface::MoveSurface(float x0, float y0, float z0)
313 LuaMat4<float> matrix4(_pRenderData->modelMtr);
314 matrix4.Translate(x0, y0, z0);
316 _pRenderData->modelMtr = matrix4;
317 _pRenderData->modelMtrChanged = true;
323 GraphicalSurface::ScaleSurface(float ax, float ay, float az, float x0, float y0, float z0)
325 LuaMat4<float> matrix4(_pRenderData->modelMtr);
326 bool result = matrix4.Scale(ax, ay, az, x0, y0, z0);
330 _pRenderData->modelMtr = matrix4;
331 _pRenderData->modelMtrChanged = true;
337 GraphicalSurface::RotateSurface(float angle, _Axis axis,
338 float ax, float ay, float az, float x0, float y0, float z0)
342 LuaMat4<float> matrix4(_pRenderData->modelMtr);
346 matrix4.RotateAroundAxisX(angle, x0, y0, z0);
350 matrix4.RotateAroundAxisY(angle, x0, y0, z0);
354 matrix4.RotateAroundAxisZ(angle, x0, y0, z0);
358 res = matrix4.RotateAroundAxisArbitrary(angle, ax, ay, az, x0, y0, z0);
367 _pRenderData->modelMtr = matrix4;
368 _pRenderData->modelMtrChanged = true;
373 } } } } // Tizen::Ui::Effects::_Runtime