2 * Copyright (c) 2017 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/internal/event/common/property-helper.h>
26 #include <dali/public-api/object/property-array.h>
27 #include <dali/public-api/object/type-registry.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 )
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 Dali::TypeRegistration mType( typeid(Dali::Path), typeid(Dali::Handle), Create );
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 );
93 Path* Path::Clone(const Path& path)
95 Path* clone = new Path();
96 clone->SetPoints( path.GetPoints() );
97 clone->SetControlPoints( path.GetControlPoints() );
102 unsigned int Path::GetDefaultPropertyCount() const
104 return DEFAULT_PROPERTY_COUNT;
107 void Path::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
109 indices.Reserve( DEFAULT_PROPERTY_COUNT );
111 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
113 indices.PushBack( i );
117 const char* Path::GetDefaultPropertyName(Property::Index index) const
119 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
121 return DEFAULT_PROPERTY_DETAILS[index].name;
124 // index out of range
128 Property::Index Path::GetDefaultPropertyIndex(const std::string& name) const
130 Property::Index index = Property::INVALID_INDEX;
132 // Look for name in default properties
133 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
135 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
136 if( 0 == strcmp( name.c_str(), property->name ) ) // dont want to convert rhs to string
145 Property::Type Path::GetDefaultPropertyType(Property::Index index) const
147 if( index < DEFAULT_PROPERTY_COUNT )
149 return DEFAULT_PROPERTY_DETAILS[index].type;
152 // index out of range
153 return Property::NONE;
156 Property::Value Path::GetDefaultProperty( Property::Index index ) const
158 if( index == Dali::Path::Property::POINTS )
160 Property::Value value( Property::ARRAY );
161 Property::Array* array = value.GetArray();
162 Property::Array::SizeType pointCount = mPoint.Count();
166 array->Reserve( pointCount );
167 for( Property::Array::SizeType i = 0; i < pointCount; ++i )
169 array->PushBack( mPoint[i] );
174 else if( index == Dali::Path::Property::CONTROL_POINTS )
176 Property::Value value( Property::ARRAY );
177 Property::Array* array = value.GetArray();
178 Property::Array::SizeType controlpointCount = mControlPoint.Count();
182 array->Reserve( controlpointCount );
183 for( Property::Array::SizeType i = 0; i < controlpointCount; ++i )
185 array->PushBack( mControlPoint[i] );
191 return Property::Value();
194 Property::Value Path::GetDefaultPropertyCurrentValue( Property::Index index ) const
196 return GetDefaultProperty( index ); // Event-side only properties
199 void Path::SetDefaultProperty(Property::Index index, const Property::Value& propertyValue)
201 const Property::Array* array = propertyValue.GetArray();
204 Property::Array::SizeType propertyArrayCount = array->Count();
205 if( index == Dali::Path::Property::POINTS )
207 mPoint.Reserve( propertyArrayCount );
208 for( Property::Array::SizeType i = 0; i < propertyArrayCount; ++i )
211 array->GetElementAt( i ).Get( point );
212 mPoint.PushBack( point );
215 else if( index == Dali::Path::Property::CONTROL_POINTS )
217 mControlPoint.Reserve( propertyArrayCount );
218 for( Property::Array::SizeType i = 0; i < propertyArrayCount; ++i )
221 array->GetElementAt( i ).Get( point );
222 mControlPoint.PushBack( point );
228 bool Path::IsDefaultPropertyWritable(Property::Index index) const
230 if( index < DEFAULT_PROPERTY_COUNT )
232 return DEFAULT_PROPERTY_DETAILS[index].writable;
238 bool Path::IsDefaultPropertyAnimatable(Property::Index index) const
240 if( index < DEFAULT_PROPERTY_COUNT )
242 return DEFAULT_PROPERTY_DETAILS[index].animatable;
248 bool Path::IsDefaultPropertyAConstraintInput( Property::Index index ) const
250 if( index < DEFAULT_PROPERTY_COUNT )
252 return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
258 void Path::AddPoint(const Vector3& point )
260 mPoint.PushBack( point );
263 void Path::AddControlPoint(const Vector3& point )
265 mControlPoint.PushBack( point );
268 unsigned int Path::GetNumberOfSegments() const
270 return (mPoint.Size()>1)?mPoint.Size()-1:0;
273 void Path::GenerateControlPoints( float curvature )
275 unsigned int numSegments = GetNumberOfSegments();
276 DALI_ASSERT_ALWAYS( numSegments > 0 && "Need at least 1 segment to generate control points" ); // need at least 1 segment
278 mControlPoint.Resize( numSegments * 2);
280 //Generate two control points for each segment
281 for( unsigned int i(0); i<numSegments; ++i )
284 Vector3 p1 = mPoint[i];
285 Vector3 p2 = mPoint[i+1];
290 //There's no previous point. We chose a point in the line defined by the two end points at
291 //a 1/8th of the distance between them.
292 p0 = p1 - (p2 - p1)/8.0f;
301 if( i == numSegments - 1)
303 //There's no next point. We chose a point in the line defined by the two end points at
304 //a 1/8th of the distance between them.
305 p3 = p2 - (p1 - p2)/8.0f;
313 Vector3 p0p1 = p1 - p0;
314 Vector3 p1p2 = p2 - p1;
315 Vector3 p2p3 = p3 - p2;
317 float length = p1p2.Length();
319 Vector3 tangentOut = ( p0p1*length + p1p2*p0p1.Length() ) * 0.5f;
320 tangentOut.Normalize();
322 Vector3 tangentIn = ( p1p2*p2p3.Length() + p2p3*length ) * 0.5f;
323 tangentIn.Normalize();
325 //Use curvature to scale the tangents
327 mControlPoint[2*i] = p1 + tangentOut*length;
328 mControlPoint[2*i+1] = p2 - tangentIn*length;
332 void Path::FindSegmentAndProgress( float t, unsigned int& segment, float& tLocal ) const
334 //Find segment and local progress
335 unsigned int numSegs = GetNumberOfSegments();
337 if( t <= 0.0f || numSegs == 0 )
349 segment = t * numSegs;
350 float segLength = 1.0f / numSegs;
351 float segStart = static_cast<float>( segment ) * segLength;
352 tLocal = (t - segStart) * numSegs;
356 void Path::Sample( float t, Vector3& position, Vector3& tangent ) const
358 if( !SampleAt(t, position, tangent) )
360 DALI_ASSERT_ALWAYS(!"Spline not fully initialized" );
364 bool Path::SampleAt( float t, Vector3& position, Vector3& tangent ) const
368 if( PathIsComplete(mPoint, mControlPoint) )
370 unsigned int segment;
372 FindSegmentAndProgress( t, segment, tLocal );
374 //Get points and control points in the segment
375 const Vector3& controlPoint0 = mControlPoint[2*segment];
376 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
377 const Vector3& point0 = mPoint[segment];
378 const Vector3& point1 = mPoint[segment+1];
380 if(tLocal < Math::MACHINE_EPSILON_1)
383 tangent = ( controlPoint0 - point0 ) * 3.0f;
386 else if( (1.0 - tLocal) < Math::MACHINE_EPSILON_1)
389 tangent = ( point1 - controlPoint1 ) * 3.0f;
394 const Vector4 sVect(tLocal*tLocal*tLocal, tLocal*tLocal, tLocal, 1.0f );
395 const Vector3 sVectDerivative(3.0f*tLocal*tLocal, 2.0f*tLocal, 1.0f );
398 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
400 Vector4 A = BezierBasis * cVect;
401 position.x = sVect.Dot4(A);
402 tangent.x = sVectDerivative.Dot(Vector3(A));
406 cVect.y = controlPoint0.y;
407 cVect.z = controlPoint1.y;
410 A = BezierBasis * cVect;
411 position.y = sVect.Dot4(A);
412 tangent.y = sVectDerivative.Dot(Vector3(A));
416 cVect.y = controlPoint0.z;
417 cVect.z = controlPoint1.z;
420 A = BezierBasis * cVect;
421 position.z = sVect.Dot4(A);
422 tangent.z = sVectDerivative.Dot(Vector3(A));
433 bool Path::SamplePosition( float t, Vector3& position ) const
437 if( PathIsComplete(mPoint, mControlPoint) )
439 unsigned int segment;
441 FindSegmentAndProgress( t, segment, tLocal );
443 const Vector3& controlPoint0 = mControlPoint[2*segment];
444 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
445 const Vector3& point0 = mPoint[segment];
446 const Vector3& point1 = mPoint[segment+1];
448 if(tLocal < Math::MACHINE_EPSILON_1)
452 else if( (1.0 - tLocal) < Math::MACHINE_EPSILON_1)
458 const Vector4 sVect(tLocal*tLocal*tLocal, tLocal*tLocal, tLocal, 1.0f );
461 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
462 position.x = sVect.Dot4(BezierBasis * cVect);
466 cVect.y = controlPoint0.y;
467 cVect.z = controlPoint1.y;
469 position.y = sVect.Dot4(BezierBasis * cVect);
473 cVect.y = controlPoint0.z;
474 cVect.z = controlPoint1.z;
476 position.z = sVect.Dot4(BezierBasis * cVect);
485 bool Path::SampleTangent( float t, Vector3& tangent ) const
489 if( PathIsComplete(mPoint, mControlPoint) )
491 unsigned int segment;
493 FindSegmentAndProgress( t, segment, tLocal );
495 const Vector3& controlPoint0 = mControlPoint[2*segment];
496 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
497 const Vector3& point0 = mPoint[segment];
498 const Vector3& point1 = mPoint[segment+1];
500 if(tLocal < Math::MACHINE_EPSILON_1)
502 tangent = ( controlPoint0 - point0 ) * 3.0f;
504 else if( (1.0f - tLocal) < Math::MACHINE_EPSILON_1)
506 tangent = ( point1 - controlPoint1 ) * 3.0f;
510 const Vector3 sVectDerivative(3.0f*tLocal*tLocal, 2.0f*tLocal, 1.0f );
513 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
514 tangent.x = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
518 cVect.y = controlPoint0.y;
519 cVect.z = controlPoint1.y;
521 tangent.y = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
525 cVect.y = controlPoint0.z;
526 cVect.z = controlPoint1.z;
528 tangent.z = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
538 Vector3& Path::GetPoint( size_t index )
540 DALI_ASSERT_ALWAYS( index < mPoint.Size() && "Path: Point index out of bounds" );
542 return mPoint[index];
545 Vector3& Path::GetControlPoint( size_t index )
547 DALI_ASSERT_ALWAYS( index < mControlPoint.Size() && "Path: Control Point index out of bounds" );
549 return mControlPoint[index];
552 size_t Path::GetPointCount() const
554 return mPoint.Size();
557 void Path::ClearPoints()
562 void Path::ClearControlPoints()
564 mControlPoint.Clear();