2 * Copyright (c) 2014 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>
38 // Name Type writable animatable constraint-input enum for index-checking
39 DALI_PROPERTY_TABLE_BEGIN
40 DALI_PROPERTY( "points", ARRAY, true, false, false, Dali::Path::Property::POINTS )
41 DALI_PROPERTY( "control-points", ARRAY, true, false, false, Dali::Path::Property::CONTROL_POINTS )
42 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
45 * These coefficient arise from the cubic polynomial equations for
48 * A bezier curve is defined by a cubic polynomial. Given two end points p0 and p1
49 * and two control points cp0 and cp1, the bezier curve will be defined by a polynomial in the form
50 * f(x) = a3*x^3 + a2*x^2 + a1*x + a0 with this restrictions:
53 * f'(0) = 3*(cp0 - p0)
56 const float BezierBasisCoeff[] = { -1.0f, 3.0f, -3.0f, 1.0f,
57 3.0f, -6.0f, 3.0f, 0.0f,
58 -3.0f, 3.0f, 0.0f, 0.0f,
59 1.0f, 0.0f, 0.0f, 0.0f };
61 const Dali::Matrix BezierBasis = Dali::Matrix( BezierBasisCoeff );
79 Path* Path::Clone(const Path& path)
81 Path* clone = new Path();
82 clone->SetPoints( path.GetPoints() );
83 clone->SetControlPoints( path.GetControlPoints() );
88 unsigned int Path::GetDefaultPropertyCount() const
90 return DEFAULT_PROPERTY_COUNT;
93 void Path::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
95 indices.reserve( DEFAULT_PROPERTY_COUNT );
97 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
99 indices.push_back( i );
103 const char* Path::GetDefaultPropertyName(Property::Index index) const
105 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
107 return DEFAULT_PROPERTY_DETAILS[index].name;
110 // index out of range
114 Property::Index Path::GetDefaultPropertyIndex(const std::string& name) const
116 Property::Index index = Property::INVALID_INDEX;
118 // Look for name in default properties
119 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
121 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
122 if( 0 == strcmp( name.c_str(), property->name ) ) // dont want to convert rhs to string
131 Property::Type Path::GetDefaultPropertyType(Property::Index index) const
133 if( index < DEFAULT_PROPERTY_COUNT )
135 return DEFAULT_PROPERTY_DETAILS[index].type;
138 // index out of range
139 return Property::NONE;
142 Property::Value Path::GetDefaultProperty( Property::Index index ) const
144 Property::Value value;
145 if( index == Dali::Path::Property::POINTS )
147 size_t pointCount( mPoint.Size() );
148 for( size_t i( 0 ); i != pointCount; ++i )
150 value.AppendItem( mPoint[i] );
153 else if( index == Dali::Path::Property::CONTROL_POINTS )
155 size_t controlpointCount( mControlPoint.Size() );
156 for( size_t i( 0 ); i != controlpointCount; ++i )
158 value.AppendItem( mControlPoint[i] );
165 void Path::SetDefaultProperty(Property::Index index, const Property::Value& propertyValue)
167 if( index == Dali::Path::Property::POINTS )
169 Property::Array propertyArray;
170 propertyValue.Get(propertyArray);
172 size_t propertyArrayCount = propertyArray.size();
173 mPoint.Resize( propertyArrayCount );
174 for( size_t i(0); i!=propertyArrayCount; ++i )
176 propertyArray[i].Get( mPoint[i]);
179 else if( index == Dali::Path::Property::CONTROL_POINTS )
181 Property::Array propertyArray;
182 propertyValue.Get(propertyArray);
184 size_t propertyArrayCount = propertyArray.size();
185 mControlPoint.Resize( propertyArrayCount );
186 for( size_t i(0); i!=propertyArrayCount; ++i )
188 propertyArray[i].Get( mControlPoint[i]);
193 bool Path::IsDefaultPropertyWritable(Property::Index index) const
195 if( index < DEFAULT_PROPERTY_COUNT )
197 return DEFAULT_PROPERTY_DETAILS[index].writable;
203 bool Path::IsDefaultPropertyAnimatable(Property::Index index) const
205 if( index < DEFAULT_PROPERTY_COUNT )
207 return DEFAULT_PROPERTY_DETAILS[index].animatable;
213 bool Path::IsDefaultPropertyAConstraintInput( Property::Index index ) const
215 if( index < DEFAULT_PROPERTY_COUNT )
217 return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
223 void Path::AddPoint(const Vector3& point )
225 mPoint.PushBack( point );
228 void Path::AddControlPoint(const Vector3& point )
230 mControlPoint.PushBack( point );
233 unsigned int Path::GetNumberOfSegments() const
235 return (mPoint.Size()>1)?mPoint.Size()-1:0;
238 void Path::GenerateControlPoints( float curvature )
240 unsigned int numSegments = GetNumberOfSegments();
241 DALI_ASSERT_ALWAYS( numSegments > 0 && "Need at least 1 segment to generate control points" ); // need at least 1 segment
243 mControlPoint.Resize( numSegments * 2);
245 //Generate two control points for each segment
246 for( unsigned int i(0); i<numSegments; ++i )
249 Vector3 p1 = mPoint[i];
250 Vector3 p2 = mPoint[i+1];
255 //There's no previous point. We chose a point in the line defined by the two end points at
256 //a 1/8th of the distance between them.
257 p0 = p1 - (p2 - p1)/8.0f;
266 if( i == numSegments - 1)
268 //There's no next point. We chose a point in the line defined by the two end points at
269 //a 1/8th of the distance between them.
270 p3 = p2 - (p1 - p2)/8.0f;
278 Vector3 p0p1 = p1 - p0;
279 Vector3 p1p2 = p2 - p1;
280 Vector3 p2p3 = p3 - p2;
282 float length = p1p2.Length();
284 Vector3 tangentOut = ( p0p1*length + p1p2*p0p1.Length() ) * 0.5f;
285 tangentOut.Normalize();
287 Vector3 tangentIn = ( p1p2*p2p3.Length() + p2p3*length ) * 0.5f;
288 tangentIn.Normalize();
290 //Use curvature to scale the tangents
292 mControlPoint[2*i] = p1 + tangentOut*length;
293 mControlPoint[2*i+1] = p2 - tangentIn*length;
297 void Path::FindSegmentAndProgress( float t, unsigned int& segment, float& tLocal ) const
299 //Find segment and local progress
300 unsigned int numSegs = GetNumberOfSegments();
314 segment = t * numSegs;
315 float segLength = 1.0f / numSegs;
316 float segStart = (float)segment * segLength;
317 tLocal = (t - segStart) * numSegs;
321 void Path::Sample( float t, Vector3& position, Vector3& tangent ) const
323 DALI_ASSERT_ALWAYS(mPoint.Size() > 1 && mControlPoint.Size() == (mPoint.Size()-1)*2 && "Spline not fully initialized" );
325 unsigned int segment;
327 FindSegmentAndProgress( t, segment, tLocal );
329 //Get points and control points in the segment
330 const Vector3& controlPoint0 = mControlPoint[2*segment];
331 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
332 const Vector3& point0 = mPoint[segment];
333 const Vector3& point1 = mPoint[segment+1];
335 if(tLocal < Math::MACHINE_EPSILON_1)
338 tangent = ( controlPoint0 - point0 ) * 3.0f;
341 else if( (1.0 - tLocal) < Math::MACHINE_EPSILON_1)
344 tangent = ( point1 - controlPoint1 ) * 3.0f;
349 const Vector4 sVect(tLocal*tLocal*tLocal, tLocal*tLocal, tLocal, 1.0f );
350 const Vector3 sVectDerivative(3.0f*tLocal*tLocal, 2.0f*tLocal, 1.0f );
353 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
355 Vector4 A = BezierBasis * cVect;
356 position.x = sVect.Dot4(A);
357 tangent.x = sVectDerivative.Dot(Vector3(A));
361 cVect.y = controlPoint0.y;
362 cVect.z = controlPoint1.y;
365 A = BezierBasis * cVect;
366 position.y = sVect.Dot4(A);
367 tangent.y = sVectDerivative.Dot(Vector3(A));
371 cVect.y = controlPoint0.z;
372 cVect.z = controlPoint1.z;
375 A = BezierBasis * cVect;
376 position.z = sVect.Dot4(A);
377 tangent.z = sVectDerivative.Dot(Vector3(A));
383 Vector3 Path::SamplePosition( float t ) const
385 DALI_ASSERT_ALWAYS(mPoint.Size() > 1 && mControlPoint.Size() == (mPoint.Size()-1)*2 && "Spline not fully initialized" );
387 unsigned int segment;
389 FindSegmentAndProgress( t, segment, tLocal );
391 const Vector3& controlPoint0 = mControlPoint[2*segment];
392 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
393 const Vector3& point0 = mPoint[segment];
394 const Vector3& point1 = mPoint[segment+1];
397 if(tLocal < Math::MACHINE_EPSILON_1)
401 else if( (1.0 - tLocal) < Math::MACHINE_EPSILON_1)
407 const Vector4 sVect(tLocal*tLocal*tLocal, tLocal*tLocal, tLocal, 1.0f );
410 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
411 position.x = sVect.Dot4(BezierBasis * cVect);
415 cVect.y = controlPoint0.y;
416 cVect.z = controlPoint1.y;
418 position.y = sVect.Dot4(BezierBasis * cVect);
422 cVect.y = controlPoint0.z;
423 cVect.z = controlPoint1.z;
425 position.z = sVect.Dot4(BezierBasis * cVect);
431 Vector3 Path::SampleTangent( float t ) const
433 DALI_ASSERT_ALWAYS(mPoint.Size() > 1 && mControlPoint.Size() == (mPoint.Size()-1)*2 && "Spline not fully initialized" );
435 unsigned int segment;
437 FindSegmentAndProgress( t, segment, tLocal );
439 const Vector3& controlPoint0 = mControlPoint[2*segment];
440 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
441 const Vector3& point0 = mPoint[segment];
442 const Vector3& point1 = mPoint[segment+1];
445 if(tLocal < Math::MACHINE_EPSILON_1)
447 tangent = ( controlPoint0 - point0 ) * 3.0f;
449 else if( (1.0f - tLocal) < Math::MACHINE_EPSILON_1)
451 tangent = ( point1 - controlPoint1 ) * 3.0f;
455 const Vector3 sVectDerivative(3.0f*tLocal*tLocal, 2.0f*tLocal, 1.0f );
458 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
459 tangent.x = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
463 cVect.y = controlPoint0.y;
464 cVect.z = controlPoint1.y;
466 tangent.y = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
470 cVect.y = controlPoint0.z;
471 cVect.z = controlPoint1.z;
473 tangent.z = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
480 Vector3& Path::GetPoint( size_t index )
482 DALI_ASSERT_ALWAYS( index < mPoint.Size() && "Path: Point index out of bounds" );
484 return mPoint[index];
487 Vector3& Path::GetControlPoint( size_t index )
489 DALI_ASSERT_ALWAYS( index < mControlPoint.Size() && "Path: Control Point index out of bounds" );
491 return mControlPoint[index];
494 size_t Path::GetPointCount() const
496 return mPoint.Size();