GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Jun-2006, 15:57
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 172
Blake will become famous soon enough

overriding variable types


I've defined several classes for doing interpolation. They are:

Interpolator: The interpolator base class.
Scalar Interpolator: Interpolates scalars. Inherits from Interpolator.
QuaternionInterpolator: Interpolates quaternions. Inherits from Interpolator.
Keyframe: The keyframe base class.
ScalarKeyframe: A Keyframe used in scalar interpolation. Inherits from Keyframe.
QuaternionKeyframe: A Keyframe used in quaternion interpolation. Inherits from keyframe.

Each interpolator has an stl map containg Keyframe pointers and time values (floats). The map is contained in the base class so that I can define all the iterator functions, etc, there, rather than redefining them in both leaf classes. The one annoying thing is that I always need to cast a Keyframe* as either a ScalarKeyframe* or QuaternionKeyframe* when I dereference an iterator. Is there a way to override the typedefs in the leaf classes that will prevent that problem? I couild fix it be overriding the function definitions, but I would rather not do that.

Here's the interpolator header (QuaternionInterpolator not shown because it is still a work in progress). The list of virtual functions that return iterators in the Interpolator class are the ones causing problems.

CPP / C++ / C Code:
#include "keyframe.h"
#include <map>

typedef enum {REP_ABS, REP_REL, FLAT} OBType;

//----------------------------------------------------------
// Interpolator Base Class

class Interpolator
{
	public:
		typedef std::map<float, Keyframe *, std::less<float> > KeySet;
		typedef KeySet::iterator iterator;
		typedef KeySet::reverse_iterator reverse_iterator;
		typedef KeySet::const_iterator const_iterator;
		typedef KeySet::const_reverse_iterator const_reverse_iterator;
		Interpolator();
		Interpolator(const Interpolator&);
		virtual float fixRange(const float&);
		virtual void setLeftOB(const OBType& OB) {leftOB = OB;}
		virtual void setRightOB(const OBType& OB) {rightOB = OB;}
		virtual bool empty() {return keyframes.empty();}
		virtual unsigned int size() {return keyframes.size();}
		virtual iterator find(const float& t) {return keyframes.find(t);}
		virtual iterator upper_bound(const float& t) {return keyframes.upper_bound(t);}
		virtual iterator lower_bound(const float& t) {return keyframes.lower_bound(t);}
		virtual iterator begin() {return keyframes.begin();}
		virtual const_iterator begin() const {return keyframes.begin();}
		virtual iterator end() {return keyframes.end();}
		virtual const_iterator end() const {return keyframes.end();}
		virtual reverse_iterator rbegin() {return keyframes.rbegin();}
		virtual const_reverse_iterator rbegin() const {return keyframes.rbegin();}
		virtual reverse_iterator rend() {return keyframes.rend();}
		virtual const_reverse_iterator rend() const {return keyframes.rend();}
	protected:
		KeySet keyframes;
		float startTime;
		float endTime;
		float length;
		OBType leftOB;
		OBType rightOB;
};

//----------------------------------------------------------
// Scalar Interpolator

class ScalarInterpolator : public Interpolator
{
	public:
		typedef std::map<float, Keyframe *, std::less<float> > KeySet;
		typedef KeySet::iterator iterator;
		typedef KeySet::reverse_iterator reverse_iterator;
		typedef KeySet::const_iterator const_iterator;
		typedef KeySet::const_reverse_iterator const_reverse_iterator;
		ScalarInterpolator();
		ScalarInterpolator(const ScalarInterpolator&);
		~ScalarInterpolator();
		void operator=(const ScalarInterpolator&);
		void addKeyframe(ScalarKeyframe *, const float&);
		void addKeyframe(const ScalarKeyframe&, const float&);
		float operator()(const float& t) {return position(t);}
		float position(const float&);
		float velocityIn(const float&);
		float velocityOut(const float&);
		float accelerationIn(const float&);
		float accelerationOut(const float&);
		float jerkIn(const float&);
		float jerkOut(const float&);
	private:
		void updatePrevNext(const float&);
		iterator prevKey;
		iterator nextKey;
		float displacement;
};

And the keyframe header (QuaternionKeyframe not shown for the same reason)

CPP / C++ / C Code:
#include "quaternion.h"

struct Keyframe
{
	Keyframe(const float& f) : keyValue(f) {}
	float keyValue;
};

//----------------------------------------------------------
// Scalar Keyframe

struct ScalarKeyframe : public Keyframe
{
	ScalarKeyframe()
		: Keyframe(0), tangentIn(0), tangentOut(0) {}
	ScalarKeyframe(const float& p, const float& v)
		: Keyframe(p), tangentIn(v), tangentOut(v) {}
	ScalarKeyframe(const float& p, const float& v1, const float& v2)
		: Keyframe(p), tangentIn(v1), tangentOut(v2) {}
	float tangentIn;
	float tangentOut;
};

I'm using MS Visual C++ (the free version) on Windows XP Professional.
 
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
char variable problems Swys C++ Forum 11 08-Aug-2005 19:41
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35
Data types and Hardware dependencies karthikeyansen C Programming Language 2 03-Apr-2005 21:58
making string data into a variable dopee MySQL / PHP Forum 0 25-Oct-2004 04:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:44.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.