2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/animation/path-impl.h>
22 #include <cstring> // for strcmp
25 #include <dali/public-api/object/property-array.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/internal/event/common/property-helper.h>
40 // Name Type writable animatable constraint-input enum for index-checking
41 DALI_PROPERTY_TABLE_BEGIN
42 DALI_PROPERTY( "points", ARRAY, true, false, false, Dali::Path::Property::POINTS )
43 DALI_PROPERTY( "controlPoints", ARRAY, true, false, false, Dali::Path::Property::CONTROL_POINTS )
44 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX, PathDefaultProperties )
47 * These coefficient arise from the cubic polynomial equations for
50 * A bezier curve is defined by a cubic polynomial. Given two end points p0 and p1
51 * and two control points cp0 and cp1, the bezier curve will be defined by a polynomial in the form
52 * f(x) = a3*x^3 + a2*x^2 + a1*x + a0 with this restrictions:
55 * f'(0) = 3*(cp0 - p0)
58 const float BezierBasisCoeff[] = { -1.0f, 3.0f, -3.0f, 1.0f,
59 3.0f, -6.0f, 3.0f, 0.0f,
60 -3.0f, 3.0f, 0.0f, 0.0f,
61 1.0f, 0.0f, 0.0f, 0.0f };
63 const Dali::Matrix BezierBasis = Dali::Matrix( BezierBasisCoeff );
65 Dali::BaseHandle Create()
67 return Dali::Path::New();
70 TypeRegistration mType( typeid(Dali::Path), typeid(Dali::Handle), Create, PathDefaultProperties );
72 inline bool PathIsComplete(const Dali::Vector<Vector3>& point, const Dali::Vector<Vector3>& controlPoint)
74 return ( point.Size() > 1 && controlPoint.Size() == (point.Size()-1)*2 );
85 : Object( nullptr ) // we don't have our own scene object
93 Path* Path::Clone(const Path& path)
95 Path* clone = new Path();
96 clone->SetPoints( path.GetPoints() );
97 clone->SetControlPoints( path.GetControlPoints() );
102 Property::Value Path::GetDefaultProperty( Property::Index index ) const
104 if( index == Dali::Path::Property::POINTS )
106 Property::Value value( Property::ARRAY );
107 Property::Array* array = value.GetArray();
108 Property::Array::SizeType pointCount = mPoint.Count();
112 array->Reserve( pointCount );
113 for( Property::Array::SizeType i = 0; i < pointCount; ++i )
115 array->PushBack( mPoint[i] );
120 else if( index == Dali::Path::Property::CONTROL_POINTS )
122 Property::Value value( Property::ARRAY );
123 Property::Array* array = value.GetArray();
124 Property::Array::SizeType controlpointCount = mControlPoint.Count();
128 array->Reserve( controlpointCount );
129 for( Property::Array::SizeType i = 0; i < controlpointCount; ++i )
131 array->PushBack( mControlPoint[i] );
137 return Property::Value();
140 void Path::SetDefaultProperty(Property::Index index, const Property::Value& propertyValue)
142 const Property::Array* array = propertyValue.GetArray();
145 Property::Array::SizeType propertyArrayCount = array->Count();
146 if( index == Dali::Path::Property::POINTS )
148 mPoint.Reserve( propertyArrayCount );
149 for( Property::Array::SizeType i = 0; i < propertyArrayCount; ++i )
152 array->GetElementAt( i ).Get( point );
153 mPoint.PushBack( point );
156 else if( index == Dali::Path::Property::CONTROL_POINTS )
158 mControlPoint.Reserve( propertyArrayCount );
159 for( Property::Array::SizeType i = 0; i < propertyArrayCount; ++i )
162 array->GetElementAt( i ).Get( point );
163 mControlPoint.PushBack( point );
169 void Path::AddPoint(const Vector3& point )
171 mPoint.PushBack( point );
174 void Path::AddControlPoint(const Vector3& point )
176 mControlPoint.PushBack( point );
179 uint32_t Path::GetNumberOfSegments() const
181 return static_cast<uint32_t>( (mPoint.Size()>1) ? mPoint.Size()-1 : 0 );
184 void Path::GenerateControlPoints( float curvature )
186 uint32_t numSegments = GetNumberOfSegments();
187 DALI_ASSERT_ALWAYS( numSegments > 0 && "Need at least 1 segment to generate control points" ); // need at least 1 segment
189 mControlPoint.Resize( numSegments * 2);
191 //Generate two control points for each segment
192 for( uint32_t i(0); i<numSegments; ++i )
195 Vector3 p1 = mPoint[i];
196 Vector3 p2 = mPoint[i+1];
201 //There's no previous point. We chose a point in the line defined by the two end points at
202 //a 1/8th of the distance between them.
203 p0 = p1 - (p2 - p1)/8.0f;
212 if( i == numSegments - 1)
214 //There's no next point. We chose a point in the line defined by the two end points at
215 //a 1/8th of the distance between them.
216 p3 = p2 - (p1 - p2)/8.0f;
224 Vector3 p0p1 = p1 - p0;
225 Vector3 p1p2 = p2 - p1;
226 Vector3 p2p3 = p3 - p2;
228 float length = p1p2.Length();
230 Vector3 tangentOut = ( p0p1*length + p1p2*p0p1.Length() ) * 0.5f;
231 tangentOut.Normalize();
233 Vector3 tangentIn = ( p1p2*p2p3.Length() + p2p3*length ) * 0.5f;
234 tangentIn.Normalize();
236 //Use curvature to scale the tangents
238 mControlPoint[2*i] = p1 + tangentOut*length;
239 mControlPoint[2*i+1] = p2 - tangentIn*length;
243 void Path::FindSegmentAndProgress( float t, uint32_t& segment, float& tLocal ) const
245 //Find segment and local progress
246 uint32_t numSegs = GetNumberOfSegments();
248 if( t <= 0.0f || numSegs == 0 )
260 segment = static_cast<uint32_t>( t * static_cast<float>( numSegs ) );
261 float segLength = 1.0f / static_cast<float>( numSegs );
262 float segStart = static_cast<float>( segment ) * segLength;
263 tLocal = (t - segStart) * static_cast<float>( numSegs );
267 void Path::Sample( float t, Vector3& position, Vector3& tangent ) const
269 if( !SampleAt(t, position, tangent) )
271 DALI_ASSERT_ALWAYS(!"Spline not fully initialized" );
275 bool Path::SampleAt( float t, Vector3& position, Vector3& tangent ) const
279 if( PathIsComplete(mPoint, mControlPoint) )
283 FindSegmentAndProgress( t, segment, tLocal );
285 //Get points and control points in the segment
286 const Vector3& controlPoint0 = mControlPoint[2*segment];
287 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
288 const Vector3& point0 = mPoint[segment];
289 const Vector3& point1 = mPoint[segment+1];
291 if(tLocal < Math::MACHINE_EPSILON_1)
294 tangent = ( controlPoint0 - point0 ) * 3.0f;
297 else if( (1.0 - tLocal) < Math::MACHINE_EPSILON_1)
300 tangent = ( point1 - controlPoint1 ) * 3.0f;
305 const Vector4 sVect(tLocal*tLocal*tLocal, tLocal*tLocal, tLocal, 1.0f );
306 const Vector3 sVectDerivative(3.0f*tLocal*tLocal, 2.0f*tLocal, 1.0f );
309 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
311 Vector4 A = BezierBasis * cVect;
312 position.x = sVect.Dot4(A);
313 tangent.x = sVectDerivative.Dot(Vector3(A));
317 cVect.y = controlPoint0.y;
318 cVect.z = controlPoint1.y;
321 A = BezierBasis * cVect;
322 position.y = sVect.Dot4(A);
323 tangent.y = sVectDerivative.Dot(Vector3(A));
327 cVect.y = controlPoint0.z;
328 cVect.z = controlPoint1.z;
331 A = BezierBasis * cVect;
332 position.z = sVect.Dot4(A);
333 tangent.z = sVectDerivative.Dot(Vector3(A));
344 bool Path::SamplePosition( float t, Vector3& position ) const
348 if( PathIsComplete(mPoint, mControlPoint) )
352 FindSegmentAndProgress( t, segment, tLocal );
354 const Vector3& controlPoint0 = mControlPoint[2*segment];
355 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
356 const Vector3& point0 = mPoint[segment];
357 const Vector3& point1 = mPoint[segment+1];
359 if(tLocal < Math::MACHINE_EPSILON_1)
363 else if( (1.0 - tLocal) < Math::MACHINE_EPSILON_1)
369 const Vector4 sVect(tLocal*tLocal*tLocal, tLocal*tLocal, tLocal, 1.0f );
372 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
373 position.x = sVect.Dot4(BezierBasis * cVect);
377 cVect.y = controlPoint0.y;
378 cVect.z = controlPoint1.y;
380 position.y = sVect.Dot4(BezierBasis * cVect);
384 cVect.y = controlPoint0.z;
385 cVect.z = controlPoint1.z;
387 position.z = sVect.Dot4(BezierBasis * cVect);
396 bool Path::SampleTangent( float t, Vector3& tangent ) const
400 if( PathIsComplete(mPoint, mControlPoint) )
404 FindSegmentAndProgress( t, segment, tLocal );
406 const Vector3& controlPoint0 = mControlPoint[2*segment];
407 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
408 const Vector3& point0 = mPoint[segment];
409 const Vector3& point1 = mPoint[segment+1];
411 if(tLocal < Math::MACHINE_EPSILON_1)
413 tangent = ( controlPoint0 - point0 ) * 3.0f;
415 else if( (1.0f - tLocal) < Math::MACHINE_EPSILON_1)
417 tangent = ( point1 - controlPoint1 ) * 3.0f;
421 const Vector3 sVectDerivative(3.0f*tLocal*tLocal, 2.0f*tLocal, 1.0f );
424 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
425 tangent.x = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
429 cVect.y = controlPoint0.y;
430 cVect.z = controlPoint1.y;
432 tangent.y = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
436 cVect.y = controlPoint0.z;
437 cVect.z = controlPoint1.z;
439 tangent.z = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
449 Vector3& Path::GetPoint( uint32_t index )
451 DALI_ASSERT_ALWAYS( index < mPoint.Size() && "Path: Point index out of bounds" );
453 return mPoint[index];
456 Vector3& Path::GetControlPoint( uint32_t index )
458 DALI_ASSERT_ALWAYS( index < mControlPoint.Size() && "Path: Control Point index out of bounds" );
460 return mControlPoint[index];
463 uint32_t Path::GetPointCount() const
465 return static_cast<uint32_t>( mPoint.Size() );
468 void Path::ClearPoints()
473 void Path::ClearControlPoints()
475 mControlPoint.Clear();