Home

hersto:  Library/Follower_Trait.h

Source of Library/Follower_Trait.h:

Here is a link for right-clicking and downloading the file: Library/Follower_Trait.h (5.20 K Byte),

and below is its content to view online.



/*
 Follower_Trait.h
   
 A trait class for specifying how to perform
 operations on the data type used by the CTDamper
 and CTChaser template classes.

 Author:
     Herbert Stocker

 HomePage:
     http://www.hersto.net/Followers
     (various resources there)

 License:
     You may use this file in any project and
     modify it as long as you keep this comment
     block unchanged.

 Accompanying Files:
     Follower_Trait.cpp
     Follower_Trait_impl.h

 Documentation:
     See http://www.hersto.net/Followers
     and the comments below.

 */



#pragma once


// The CTFollowerVarTrait describes how certain operations that CTDamper and CTChaser
// need to do on their variables are to be performed.
// To use a CTDamper or CTChaser on a new, unsupported type, you need to customized
// the behaviour by providing a specialization of the CTFollowerVarTrait template.
// Writing a specialization is easy, code fragments are provided here.
// The default CTFollowerVarTrait templates provides these operations for types
// that have operators like float and double, and it offers to use the .length()
// method for calculating the magnitude if the type is neither float nor double.
//
// The following operations have to be provided to CTDamper and CTChaser:
//  - Initialization.
//    The CTFollowerVarTrait relies on the default constructor of the variable
//    type to initialize the variable to some kind of zero, but a specialization
//    for float and double initializes them to 0. For other types that do not
//    initialize in the constructor you have to provide a specialization for
//    the default constructor. Use a pice of code like this:
//        inline
//        CTFollowerVarTrait<YourType>::CTFollowerVarTrait()
//        :   mValue(YourInitializerHere)
//        {}
//
//  - Linear Interpolation between two values.
//    The Interpolate(.) method does a linear interpolation between This and That,
//    so that if Frac is 0, returns This, and if Frac is 1, returns That.
//    In the default implementation the term 
//        This.mValue * (1 - T) + That.mValue * T
//    is used. If your data type does not support these operations you need to
//    provide a specialization for Interpolate(.). Use a piece of code like this:
//        inline
//        CTFollowerVarTrait<YourType> CTFollowerVarTrait<YourType>::Interpolate(const CTFollowerVarTrait<YourType>& That, double Frac) const
//        {
//            //your calculation here.
//        }
//    As an example, for 3D rotations the slerp function provides this kind of
//    interpolation.
//
//  - Distance between two values:
//    The Distance(.) function calculates a double value that measures the distance
//    between two values. For float and double, this is abs(A - B), and for vectors
//    this is (A - B).length(). The default implementation uses this later term
//    for calculating the distance, in which Length(.) is another method that can
//    be customized.
//    Therefore, you can customize either Distance(.) if your data type does not
//    support operator- , or Length(.) if you just need to override this.
//    Per default Length(.) assumes a .length() method be present on the variable
//    type and there are two specializations that calculate abs(.) for float and double.
//    To Customize Distance(.), use a piece of code like this:
//        inline
//        double CTFollowerVarTrait<YourType>::Distance(const CTDamperableTraits<Type>& That) const
//        {
//            //your calculation here. It can use Length(.) but does not need to.
//        }
//    To Customize Length(.), use a piece of code like this:
//        inline
//        double CTDamperableTraits<float>::Length() const
//        {
//            //your claculation here.
//        }
//
//   - Comparision of values:
//     Not really a customization option as comparision virtually never needs to be
//     customized. The operator!= of CTFollowerVarTrait just calls the the operator!=
//     of the variable type. Should it be necessary, use the following code for
//     customization:
//         inline
//         bool CTDamperableTraits<YourType>::operator!= (const CTDamperableTraits<YourType>& That) const
//         {
//             // your comparision here.
//         }
//
// Of course everything is customizable, as you receive the source code.
//


template<class Type>
class CTFollowerVarTrait
{
public:
    CTFollowerVarTrait();
   ~CTFollowerVarTrait();
    
    CTFollowerVarTrait(const Type& Value);
    CTFollowerVarTrait(const CTFollowerVarTrait<Type>& That);
    const Type& get_Value() const;

    CTFollowerVarTrait<Type> Interpolate(const CTFollowerVarTrait<Type>& That, double Frac) const;
    double Distance(const CTFollowerVarTrait<Type>& That) const;
    bool operator!= (const CTFollowerVarTrait<Type>& That) const;
    double Length() const;
    // returns the magnitude of the value.
    // This is not used by the CTDamper<.> or CTChaser<.> , but 
    // by the Distance(.) function. This way one can customize either
    // Distance(.) or length(.), whichever is more appropriate.


protected:
    Type mValue;
};



#include "Follower_Trait_impl.h"


__.-.__
end of document