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>
39 // Name Type writable animatable constraint-input enum for index-checking
40 DALI_PROPERTY_TABLE_BEGIN
41 DALI_PROPERTY( "points", ARRAY, true, false, false, Dali::Path::Property::POINTS )
42 DALI_PROPERTY( "control-points", ARRAY, true, false, false, Dali::Path::Property::CONTROL_POINTS )
43 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
46 * These coefficient arise from the cubic polynomial equations for
49 * A bezier curve is defined by a cubic polynomial. Given two end points p0 and p1
50 * and two control points cp0 and cp1, the bezier curve will be defined by a polynomial in the form
51 * f(x) = a3*x^3 + a2*x^2 + a1*x + a0 with this restrictions:
54 * f'(0) = 3*(cp0 - p0)
57 const float BezierBasisCoeff[] = { -1.0f, 3.0f, -3.0f, 1.0f,
58 3.0f, -6.0f, 3.0f, 0.0f,
59 -3.0f, 3.0f, 0.0f, 0.0f,
60 1.0f, 0.0f, 0.0f, 0.0f };
62 const Dali::Matrix BezierBasis = Dali::Matrix( BezierBasisCoeff );
80 Path* Path::Clone(const Path& path)
82 Path* clone = new Path();
83 clone->SetPoints( path.GetPoints() );
84 clone->SetControlPoints( path.GetControlPoints() );
89 unsigned int Path::GetDefaultPropertyCount() const
91 return DEFAULT_PROPERTY_COUNT;
94 void Path::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
96 indices.Reserve( DEFAULT_PROPERTY_COUNT );
98 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
100 indices.PushBack( i );
104 const char* Path::GetDefaultPropertyName(Property::Index index) const
106 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
108 return DEFAULT_PROPERTY_DETAILS[index].name;
111 // index out of range
115 Property::Index Path::GetDefaultPropertyIndex(const std::string& name) const
117 Property::Index index = Property::INVALID_INDEX;
119 // Look for name in default properties
120 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
122 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
123 if( 0 == strcmp( name.c_str(), property->name ) ) // dont want to convert rhs to string
132 Property::Type Path::GetDefaultPropertyType(Property::Index index) const
134 if( index < DEFAULT_PROPERTY_COUNT )
136 return DEFAULT_PROPERTY_DETAILS[index].type;
139 // index out of range
140 return Property::NONE;
143 Property::Value Path::GetDefaultProperty( Property::Index index ) const
145 if( index == Dali::Path::Property::POINTS )
147 Property::Value value( Property::ARRAY );
148 Property::Array* array = value.GetArray();
149 Property::Array::SizeType pointCount = mPoint.Count();
150 array->Reserve( pointCount );
151 for( Property::Array::SizeType i = 0; i < pointCount; ++i )
153 array->PushBack( mPoint[i] );
157 else if( index == Dali::Path::Property::CONTROL_POINTS )
159 Property::Value value( Property::ARRAY );
160 Property::Array* array = value.GetArray();
161 Property::Array::SizeType controlpointCount = mControlPoint.Count();
162 array->Reserve( controlpointCount );
163 for( Property::Array::SizeType i = 0; i < controlpointCount; ++i )
165 array->PushBack( mControlPoint[i] );
170 return Property::Value();
173 void Path::SetDefaultProperty(Property::Index index, const Property::Value& propertyValue)
175 const Property::Array* array = propertyValue.GetArray();
178 Property::Array::SizeType propertyArrayCount = array->Count();
179 if( index == Dali::Path::Property::POINTS )
181 mPoint.Reserve( propertyArrayCount );
182 for( Property::Array::SizeType i = 0; i < propertyArrayCount; ++i )
185 array->GetElementAt( i ).Get( point );
186 mPoint.PushBack( point );
189 else if( index == Dali::Path::Property::CONTROL_POINTS )
191 mControlPoint.Reserve( propertyArrayCount );
192 for( Property::Array::SizeType i = 0; i < propertyArrayCount; ++i )
195 array->GetElementAt( i ).Get( point );
196 mControlPoint.PushBack( point );
202 bool Path::IsDefaultPropertyWritable(Property::Index index) const
204 if( index < DEFAULT_PROPERTY_COUNT )
206 return DEFAULT_PROPERTY_DETAILS[index].writable;
212 bool Path::IsDefaultPropertyAnimatable(Property::Index index) const
214 if( index < DEFAULT_PROPERTY_COUNT )
216 return DEFAULT_PROPERTY_DETAILS[index].animatable;
222 bool Path::IsDefaultPropertyAConstraintInput( Property::Index index ) const
224 if( index < DEFAULT_PROPERTY_COUNT )
226 return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
232 void Path::AddPoint(const Vector3& point )
234 mPoint.PushBack( point );
237 void Path::AddControlPoint(const Vector3& point )
239 mControlPoint.PushBack( point );
242 unsigned int Path::GetNumberOfSegments() const
244 return (mPoint.Size()>1)?mPoint.Size()-1:0;
247 void Path::GenerateControlPoints( float curvature )
249 unsigned int numSegments = GetNumberOfSegments();
250 DALI_ASSERT_ALWAYS( numSegments > 0 && "Need at least 1 segment to generate control points" ); // need at least 1 segment
252 mControlPoint.Resize( numSegments * 2);
254 //Generate two control points for each segment
255 for( unsigned int i(0); i<numSegments; ++i )
258 Vector3 p1 = mPoint[i];
259 Vector3 p2 = mPoint[i+1];
264 //There's no previous point. We chose a point in the line defined by the two end points at
265 //a 1/8th of the distance between them.
266 p0 = p1 - (p2 - p1)/8.0f;
275 if( i == numSegments - 1)
277 //There's no next point. We chose a point in the line defined by the two end points at
278 //a 1/8th of the distance between them.
279 p3 = p2 - (p1 - p2)/8.0f;
287 Vector3 p0p1 = p1 - p0;
288 Vector3 p1p2 = p2 - p1;
289 Vector3 p2p3 = p3 - p2;
291 float length = p1p2.Length();
293 Vector3 tangentOut = ( p0p1*length + p1p2*p0p1.Length() ) * 0.5f;
294 tangentOut.Normalize();
296 Vector3 tangentIn = ( p1p2*p2p3.Length() + p2p3*length ) * 0.5f;
297 tangentIn.Normalize();
299 //Use curvature to scale the tangents
301 mControlPoint[2*i] = p1 + tangentOut*length;
302 mControlPoint[2*i+1] = p2 - tangentIn*length;
306 void Path::FindSegmentAndProgress( float t, unsigned int& segment, float& tLocal ) const
308 //Find segment and local progress
309 unsigned int numSegs = GetNumberOfSegments();
323 segment = t * numSegs;
324 float segLength = 1.0f / numSegs;
325 float segStart = (float)segment * segLength;
326 tLocal = (t - segStart) * numSegs;
330 void Path::Sample( float t, Vector3& position, Vector3& tangent ) const
332 DALI_ASSERT_ALWAYS(mPoint.Size() > 1 && mControlPoint.Size() == (mPoint.Size()-1)*2 && "Spline not fully initialized" );
334 unsigned int segment;
336 FindSegmentAndProgress( t, segment, tLocal );
338 //Get points and control points in the segment
339 const Vector3& controlPoint0 = mControlPoint[2*segment];
340 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
341 const Vector3& point0 = mPoint[segment];
342 const Vector3& point1 = mPoint[segment+1];
344 if(tLocal < Math::MACHINE_EPSILON_1)
347 tangent = ( controlPoint0 - point0 ) * 3.0f;
350 else if( (1.0 - tLocal) < Math::MACHINE_EPSILON_1)
353 tangent = ( point1 - controlPoint1 ) * 3.0f;
358 const Vector4 sVect(tLocal*tLocal*tLocal, tLocal*tLocal, tLocal, 1.0f );
359 const Vector3 sVectDerivative(3.0f*tLocal*tLocal, 2.0f*tLocal, 1.0f );
362 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
364 Vector4 A = BezierBasis * cVect;
365 position.x = sVect.Dot4(A);
366 tangent.x = sVectDerivative.Dot(Vector3(A));
370 cVect.y = controlPoint0.y;
371 cVect.z = controlPoint1.y;
374 A = BezierBasis * cVect;
375 position.y = sVect.Dot4(A);
376 tangent.y = sVectDerivative.Dot(Vector3(A));
380 cVect.y = controlPoint0.z;
381 cVect.z = controlPoint1.z;
384 A = BezierBasis * cVect;
385 position.z = sVect.Dot4(A);
386 tangent.z = sVectDerivative.Dot(Vector3(A));
392 Vector3 Path::SamplePosition( float t ) const
394 DALI_ASSERT_ALWAYS(mPoint.Size() > 1 && mControlPoint.Size() == (mPoint.Size()-1)*2 && "Spline not fully initialized" );
396 unsigned int segment;
398 FindSegmentAndProgress( t, segment, tLocal );
400 const Vector3& controlPoint0 = mControlPoint[2*segment];
401 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
402 const Vector3& point0 = mPoint[segment];
403 const Vector3& point1 = mPoint[segment+1];
406 if(tLocal < Math::MACHINE_EPSILON_1)
410 else if( (1.0 - tLocal) < Math::MACHINE_EPSILON_1)
416 const Vector4 sVect(tLocal*tLocal*tLocal, tLocal*tLocal, tLocal, 1.0f );
419 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
420 position.x = sVect.Dot4(BezierBasis * cVect);
424 cVect.y = controlPoint0.y;
425 cVect.z = controlPoint1.y;
427 position.y = sVect.Dot4(BezierBasis * cVect);
431 cVect.y = controlPoint0.z;
432 cVect.z = controlPoint1.z;
434 position.z = sVect.Dot4(BezierBasis * cVect);
440 Vector3 Path::SampleTangent( float t ) const
442 DALI_ASSERT_ALWAYS(mPoint.Size() > 1 && mControlPoint.Size() == (mPoint.Size()-1)*2 && "Spline not fully initialized" );
444 unsigned int segment;
446 FindSegmentAndProgress( t, segment, tLocal );
448 const Vector3& controlPoint0 = mControlPoint[2*segment];
449 const Vector3& controlPoint1 = mControlPoint[2*segment+1];
450 const Vector3& point0 = mPoint[segment];
451 const Vector3& point1 = mPoint[segment+1];
454 if(tLocal < Math::MACHINE_EPSILON_1)
456 tangent = ( controlPoint0 - point0 ) * 3.0f;
458 else if( (1.0f - tLocal) < Math::MACHINE_EPSILON_1)
460 tangent = ( point1 - controlPoint1 ) * 3.0f;
464 const Vector3 sVectDerivative(3.0f*tLocal*tLocal, 2.0f*tLocal, 1.0f );
467 Vector4 cVect( point0.x, controlPoint0.x, controlPoint1.x, point1.x);
468 tangent.x = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
472 cVect.y = controlPoint0.y;
473 cVect.z = controlPoint1.y;
475 tangent.y = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
479 cVect.y = controlPoint0.z;
480 cVect.z = controlPoint1.z;
482 tangent.z = sVectDerivative.Dot(Vector3(BezierBasis * cVect));
489 Vector3& Path::GetPoint( size_t index )
491 DALI_ASSERT_ALWAYS( index < mPoint.Size() && "Path: Point index out of bounds" );
493 return mPoint[index];
496 Vector3& Path::GetControlPoint( size_t index )
498 DALI_ASSERT_ALWAYS( index < mControlPoint.Size() && "Path: Control Point index out of bounds" );
500 return mControlPoint[index];
503 size_t Path::GetPointCount() const
505 return mPoint.Size();
508 void Path::ClearPoints()
513 void Path::ClearControlPoints()
515 mControlPoint.Clear();