2 * Copyright (c) 2015 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 Property::Array propertyArray;
148 value = Property::Value(propertyArray);
149 size_t pointCount( mPoint.Size() );
150 for( size_t i( 0 ); i != pointCount; ++i )
152 value.AppendItem( mPoint[i] );
155 else if( index == Dali::Path::Property::CONTROL_POINTS )
157 Property::Array propertyArray;
158 value = Property::Value(propertyArray);
159 size_t controlpointCount( mControlPoint.Size() );
160 for( size_t i( 0 ); i != controlpointCount; ++i )
162 value.AppendItem( mControlPoint[i] );
169 void Path::SetDefaultProperty(Property::Index index, const Property::Value& propertyValue)
171 if( index == Dali::Path::Property::POINTS )
173 Property::Array propertyArray;
174 propertyValue.Get(propertyArray);
176 size_t propertyArrayCount = propertyArray.size();
177 mPoint.Resize( propertyArrayCount );
178 for( size_t i(0); i!=propertyArrayCount; ++i )
180 propertyArray[i].Get( mPoint[i]);
183 else if( index == Dali::Path::Property::CONTROL_POINTS )
185 Property::Array propertyArray;
186 propertyValue.Get(propertyArray);
188 size_t propertyArrayCount = propertyArray.size();
189 mControlPoint.Resize( propertyArrayCount );
190 for( size_t i(0); i!=propertyArrayCount; ++i )
192 propertyArray[i].Get( mControlPoint[i]);
197 bool Path::IsDefaultPropertyWritable(Property::Index index) const
199 if( index < DEFAULT_PROPERTY_COUNT )
201 return DEFAULT_PROPERTY_DETAILS[index].writable;
207 bool Path::IsDefaultPropertyAnimatable(Property::Index index) const
209 if( index < DEFAULT_PROPERTY_COUNT )
211 return DEFAULT_PROPERTY_DETAILS[index].animatable;
217 bool Path::IsDefaultPropertyAConstraintInput( Property::Index index ) const
219 if( index < DEFAULT_PROPERTY_COUNT )
221 return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
227 void Path::AddPoint(const Vector3& point )
229 mPoint.PushBack( point );
232 void Path::AddControlPoint(const Vector3& point )
234 mControlPoint.PushBack( point );
237 unsigned int Path::GetNumberOfSegments() const
239 return (mPoint.Size()>1)?mPoint.Size()-1:0;
242 void Path::GenerateControlPoints( float curvature )
244 unsigned int numSegments = GetNumberOfSegments();
245 DALI_ASSERT_ALWAYS( numSegments > 0 && "Need at least 1 segment to generate control points" ); // need at least 1 segment
247 mControlPoint.Resize( numSegments * 2);
249 //Generate two control points for each segment
250 for( unsigned int i(0); i<numSegments; ++i )
253 Vector3 p1 = mPoint[i];
254 Vector3 p2 = mPoint[i+1];
259 //There's no previous point. We chose a point in the line defined by the two end points at
260 //a 1/8th of the distance between them.
261 p0 = p1 - (p2 - p1)/8.0f;
270 if( i == numSegments - 1)
272 //There's no next point. We chose a point in the line defined by the two end points at
273 //a 1/8th of the distance between them.
274 p3 = p2 - (p1 - p2)/8.0f;
282 Vector3 p0p1 = p1 - p0;
283 Vector3 p1p2 = p2 - p1;
284 Vector3 p2p3 = p3 - p2;
286 float length = p1p2.Length();
288 Vector3 tangentOut = ( p0p1*length + p1p2*p0p1.Length() ) * 0.5f;
289 tangentOut.Normalize();
291 Vector3 tangentIn = ( p1p2*p2p3.Length() + p2p3*length ) * 0.5f;
292 tangentIn.Normalize();
294 //Use curvature to scale the tangents
296 mControlPoint[2*i] = p1 + tangentOut*length;
297 mControlPoint[2*i+1] = p2 - tangentIn*length;
301 void Path::FindSegmentAndProgress( float t, unsigned int& segment, float& tLocal ) const
303 //Find segment and local progress
304 unsigned int numSegs = GetNumberOfSegments();
318 segment = t * numSegs;
319 float segLength = 1.0f / numSegs;
320 float segStart = (float)segment * segLength;
321 tLocal = (t - segStart) * numSegs;
325 void Path::Sample( float t, Vector3& position, Vector3& tangent ) const
327 DALI_ASSERT_ALWAYS(mPoint.Size() > 1 && mControlPoint.Size() == (mPoint.Size()-1)*2 && "Spline not fully initialized" );
329 unsigned int segment;
331 FindSegmentAndProgress( t, segment, tLocal );
333 //Get points and control points in the segment
334 const Vector3& controlPoint0 = mControlPoint[2*segment];
335 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
336 const Vector3& point0 = mPoint[segment];
337 const Vector3& point1 = mPoint[segment+1];
339 if(tLocal < Math::MACHINE_EPSILON_1)
342 tangent = ( controlPoint0 - point0 ) * 3.0f;
345 else if( (1.0 - tLocal) < Math::MACHINE_EPSILON_1)
348 tangent = ( point1 - controlPoint1 ) * 3.0f;
353 const Vector4 sVect(tLocal*tLocal*tLocal, tLocal*tLocal, tLocal, 1.0f );
354 const Vector3 sVectDerivative(3.0f*tLocal*tLocal, 2.0f*tLocal, 1.0f );
357 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
359 Vector4 A = BezierBasis * cVect;
360 position.x = sVect.Dot4(A);
361 tangent.x = sVectDerivative.Dot(Vector3(A));
365 cVect.y = controlPoint0.y;
366 cVect.z = controlPoint1.y;
369 A = BezierBasis * cVect;
370 position.y = sVect.Dot4(A);
371 tangent.y = sVectDerivative.Dot(Vector3(A));
375 cVect.y = controlPoint0.z;
376 cVect.z = controlPoint1.z;
379 A = BezierBasis * cVect;
380 position.z = sVect.Dot4(A);
381 tangent.z = sVectDerivative.Dot(Vector3(A));
387 Vector3 Path::SamplePosition( float t ) const
389 DALI_ASSERT_ALWAYS(mPoint.Size() > 1 && mControlPoint.Size() == (mPoint.Size()-1)*2 && "Spline not fully initialized" );
391 unsigned int segment;
393 FindSegmentAndProgress( t, segment, tLocal );
395 const Vector3& controlPoint0 = mControlPoint[2*segment];
396 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
397 const Vector3& point0 = mPoint[segment];
398 const Vector3& point1 = mPoint[segment+1];
401 if(tLocal < Math::MACHINE_EPSILON_1)
405 else if( (1.0 - tLocal) < Math::MACHINE_EPSILON_1)
411 const Vector4 sVect(tLocal*tLocal*tLocal, tLocal*tLocal, tLocal, 1.0f );
414 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
415 position.x = sVect.Dot4(BezierBasis * cVect);
419 cVect.y = controlPoint0.y;
420 cVect.z = controlPoint1.y;
422 position.y = sVect.Dot4(BezierBasis * cVect);
426 cVect.y = controlPoint0.z;
427 cVect.z = controlPoint1.z;
429 position.z = sVect.Dot4(BezierBasis * cVect);
435 Vector3 Path::SampleTangent( float t ) const
437 DALI_ASSERT_ALWAYS(mPoint.Size() > 1 && mControlPoint.Size() == (mPoint.Size()-1)*2 && "Spline not fully initialized" );
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];
449 if(tLocal < Math::MACHINE_EPSILON_1)
451 tangent = ( controlPoint0 - point0 ) * 3.0f;
453 else if( (1.0f - tLocal) < Math::MACHINE_EPSILON_1)
455 tangent = ( point1 - controlPoint1 ) * 3.0f;
459 const Vector3 sVectDerivative(3.0f*tLocal*tLocal, 2.0f*tLocal, 1.0f );
462 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
463 tangent.x = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
467 cVect.y = controlPoint0.y;
468 cVect.z = controlPoint1.y;
470 tangent.y = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
474 cVect.y = controlPoint0.z;
475 cVect.z = controlPoint1.z;
477 tangent.z = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
484 Vector3& Path::GetPoint( size_t index )
486 DALI_ASSERT_ALWAYS( index < mPoint.Size() && "Path: Point index out of bounds" );
488 return mPoint[index];
491 Vector3& Path::GetControlPoint( size_t index )
493 DALI_ASSERT_ALWAYS( index < mControlPoint.Size() && "Path: Control Point index out of bounds" );
495 return mControlPoint[index];
498 size_t Path::GetPointCount() const
500 return mPoint.Size();