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>
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( "control-points", 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 void Path::SetDefaultProperty(Property::Index index, const Property::Value& propertyValue)
196 const Property::Array* array = propertyValue.GetArray();
199 Property::Array::SizeType propertyArrayCount = array->Count();
200 if( index == Dali::Path::Property::POINTS )
202 mPoint.Reserve( propertyArrayCount );
203 for( Property::Array::SizeType i = 0; i < propertyArrayCount; ++i )
206 array->GetElementAt( i ).Get( point );
207 mPoint.PushBack( point );
210 else if( index == Dali::Path::Property::CONTROL_POINTS )
212 mControlPoint.Reserve( propertyArrayCount );
213 for( Property::Array::SizeType i = 0; i < propertyArrayCount; ++i )
216 array->GetElementAt( i ).Get( point );
217 mControlPoint.PushBack( point );
223 bool Path::IsDefaultPropertyWritable(Property::Index index) const
225 if( index < DEFAULT_PROPERTY_COUNT )
227 return DEFAULT_PROPERTY_DETAILS[index].writable;
233 bool Path::IsDefaultPropertyAnimatable(Property::Index index) const
235 if( index < DEFAULT_PROPERTY_COUNT )
237 return DEFAULT_PROPERTY_DETAILS[index].animatable;
243 bool Path::IsDefaultPropertyAConstraintInput( Property::Index index ) const
245 if( index < DEFAULT_PROPERTY_COUNT )
247 return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
253 void Path::AddPoint(const Vector3& point )
255 mPoint.PushBack( point );
258 void Path::AddControlPoint(const Vector3& point )
260 mControlPoint.PushBack( point );
263 unsigned int Path::GetNumberOfSegments() const
265 return (mPoint.Size()>1)?mPoint.Size()-1:0;
268 void Path::GenerateControlPoints( float curvature )
270 unsigned int numSegments = GetNumberOfSegments();
271 DALI_ASSERT_ALWAYS( numSegments > 0 && "Need at least 1 segment to generate control points" ); // need at least 1 segment
273 mControlPoint.Resize( numSegments * 2);
275 //Generate two control points for each segment
276 for( unsigned int i(0); i<numSegments; ++i )
279 Vector3 p1 = mPoint[i];
280 Vector3 p2 = mPoint[i+1];
285 //There's no previous point. We chose a point in the line defined by the two end points at
286 //a 1/8th of the distance between them.
287 p0 = p1 - (p2 - p1)/8.0f;
296 if( i == numSegments - 1)
298 //There's no next point. We chose a point in the line defined by the two end points at
299 //a 1/8th of the distance between them.
300 p3 = p2 - (p1 - p2)/8.0f;
308 Vector3 p0p1 = p1 - p0;
309 Vector3 p1p2 = p2 - p1;
310 Vector3 p2p3 = p3 - p2;
312 float length = p1p2.Length();
314 Vector3 tangentOut = ( p0p1*length + p1p2*p0p1.Length() ) * 0.5f;
315 tangentOut.Normalize();
317 Vector3 tangentIn = ( p1p2*p2p3.Length() + p2p3*length ) * 0.5f;
318 tangentIn.Normalize();
320 //Use curvature to scale the tangents
322 mControlPoint[2*i] = p1 + tangentOut*length;
323 mControlPoint[2*i+1] = p2 - tangentIn*length;
327 void Path::FindSegmentAndProgress( float t, unsigned int& segment, float& tLocal ) const
329 //Find segment and local progress
330 unsigned int numSegs = GetNumberOfSegments();
332 if( t <= 0.0f || numSegs == 0 )
344 segment = t * numSegs;
345 float segLength = 1.0f / numSegs;
346 float segStart = (float)segment * segLength;
347 tLocal = (t - segStart) * numSegs;
351 void Path::Sample( float t, Vector3& position, Vector3& tangent ) const
353 if( !SampleAt(t, position, tangent) )
355 DALI_ASSERT_ALWAYS(!"Spline not fully initialized" );
359 bool Path::SampleAt( float t, Vector3& position, Vector3& tangent ) const
363 if( PathIsComplete(mPoint, mControlPoint) )
365 unsigned int segment;
367 FindSegmentAndProgress( t, segment, tLocal );
369 //Get points and control points in the segment
370 const Vector3& controlPoint0 = mControlPoint[2*segment];
371 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
372 const Vector3& point0 = mPoint[segment];
373 const Vector3& point1 = mPoint[segment+1];
375 if(tLocal < Math::MACHINE_EPSILON_1)
378 tangent = ( controlPoint0 - point0 ) * 3.0f;
381 else if( (1.0 - tLocal) < Math::MACHINE_EPSILON_1)
384 tangent = ( point1 - controlPoint1 ) * 3.0f;
389 const Vector4 sVect(tLocal*tLocal*tLocal, tLocal*tLocal, tLocal, 1.0f );
390 const Vector3 sVectDerivative(3.0f*tLocal*tLocal, 2.0f*tLocal, 1.0f );
393 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
395 Vector4 A = BezierBasis * cVect;
396 position.x = sVect.Dot4(A);
397 tangent.x = sVectDerivative.Dot(Vector3(A));
401 cVect.y = controlPoint0.y;
402 cVect.z = controlPoint1.y;
405 A = BezierBasis * cVect;
406 position.y = sVect.Dot4(A);
407 tangent.y = sVectDerivative.Dot(Vector3(A));
411 cVect.y = controlPoint0.z;
412 cVect.z = controlPoint1.z;
415 A = BezierBasis * cVect;
416 position.z = sVect.Dot4(A);
417 tangent.z = sVectDerivative.Dot(Vector3(A));
428 bool Path::SamplePosition( float t, Vector3& position ) const
432 if( PathIsComplete(mPoint, mControlPoint) )
434 unsigned int segment;
436 FindSegmentAndProgress( t, segment, tLocal );
438 const Vector3& controlPoint0 = mControlPoint[2*segment];
439 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
440 const Vector3& point0 = mPoint[segment];
441 const Vector3& point1 = mPoint[segment+1];
443 if(tLocal < Math::MACHINE_EPSILON_1)
447 else if( (1.0 - tLocal) < Math::MACHINE_EPSILON_1)
453 const Vector4 sVect(tLocal*tLocal*tLocal, tLocal*tLocal, tLocal, 1.0f );
456 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
457 position.x = sVect.Dot4(BezierBasis * cVect);
461 cVect.y = controlPoint0.y;
462 cVect.z = controlPoint1.y;
464 position.y = sVect.Dot4(BezierBasis * cVect);
468 cVect.y = controlPoint0.z;
469 cVect.z = controlPoint1.z;
471 position.z = sVect.Dot4(BezierBasis * cVect);
480 bool Path::SampleTangent( float t, Vector3& tangent ) const
484 if( PathIsComplete(mPoint, mControlPoint) )
486 unsigned int segment;
488 FindSegmentAndProgress( t, segment, tLocal );
490 const Vector3& controlPoint0 = mControlPoint[2*segment];
491 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
492 const Vector3& point0 = mPoint[segment];
493 const Vector3& point1 = mPoint[segment+1];
495 if(tLocal < Math::MACHINE_EPSILON_1)
497 tangent = ( controlPoint0 - point0 ) * 3.0f;
499 else if( (1.0f - tLocal) < Math::MACHINE_EPSILON_1)
501 tangent = ( point1 - controlPoint1 ) * 3.0f;
505 const Vector3 sVectDerivative(3.0f*tLocal*tLocal, 2.0f*tLocal, 1.0f );
508 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
509 tangent.x = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
513 cVect.y = controlPoint0.y;
514 cVect.z = controlPoint1.y;
516 tangent.y = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
520 cVect.y = controlPoint0.z;
521 cVect.z = controlPoint1.z;
523 tangent.z = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
533 Vector3& Path::GetPoint( size_t index )
535 DALI_ASSERT_ALWAYS( index < mPoint.Size() && "Path: Point index out of bounds" );
537 return mPoint[index];
540 Vector3& Path::GetControlPoint( size_t index )
542 DALI_ASSERT_ALWAYS( index < mControlPoint.Size() && "Path: Control Point index out of bounds" );
544 return mControlPoint[index];
547 size_t Path::GetPointCount() const
549 return mPoint.Size();
552 void Path::ClearPoints()
557 void Path::ClearControlPoints()
559 mControlPoint.Clear();