nurbs.h

Go to the documentation of this file.
00001 
00002 //**************************************************************************/
00003 // Copyright (c) 2008 Autodesk, Inc.
00004 // All rights reserved.
00005 //
00006 // Use of this software is subject to the terms of the Autodesk license
00007 // agreement provided at the time of installation or download, or which
00008 // otherwise accompanies this software in either electronic or hard copy form.
00009 //
00010 //**************************************************************************/
00011 // DESCRIPTION:
00012 // CREATED: August 2009
00013 //**************************************************************************/
00014 
00015 namespace mudbox {
00016 
00017 class MBDLL_DECL NURBSCurve : public CurveBase
00018 {
00019 public:
00020     DECLARE_CLASS;
00021 
00022     NURBSCurve( int iDegree = 2, bool bFalloff = false );
00023     ~NURBSCurve( void );
00024 
00025     inline bool operator ==( const NURBSCurve & ) const throw() { return false; };
00026     inline bool operator !=( const NURBSCurve &v ) const throw() { return !operator ==( v ); };
00027 
00028     void SetVisible( bool bVisible ) { m_bVisible = bVisible; };
00029     bool Visible( void ) { return m_bVisible; };
00030     bool AddCP( const Vector &v, int iIndex = -1 );
00031     void RemoveCP( unsigned int iIndex );
00032     void MoveCP( unsigned int iIndex, const Vector &v );
00033     void Interpolate( const QList<CurvePoint> &points );
00034     void RecalcKnots( void );
00035     int GetDegree( void ) { return m_iDegree; };
00036     void SetDegree( int iDegree );
00037     Matrix GetMatrix( void );
00038     void Display( bool bScreen = true );
00039     void Render();
00040     void Recalculate();
00041     void ProjectPoint( const Vector &vOrig, float fGuess, float &u, Vector &vProjected, float fMin = 0.001f, float fMax = 0.001f, int iMaxTry = 100 ) const;
00042     void BasisFunction( float u, int i, Store<float> &N ) const;
00043     void DeriveBasisFunction( int n, float u, int span, Store< Store<float> > &aDerF ) const;
00044     int FindSpan( float u ) const;
00045     void DeriveAt( float u, int iDeg, Store<Vector> &aDerivs ) const;
00046     inline float GetMaxParam( void ) const
00047     {
00048         if( m_aKnots.ItemCount() == 0 )
00049             return 0.0f;
00050         return m_aKnots[m_aKnots.ItemCount()-1];
00051     };
00052     inline Vector operator()( float u ) const
00053     {
00054         Vector v;
00055         if( m_iCPs <= m_iDegree )
00056             return v;
00057 
00058         Store<float> aN( "NURBSCurve temp" );
00059         int iSpan = FindSpan(u);
00060         BasisFunction( u, iSpan, aN );
00061 
00062         for( int i = m_iDegree; i >= 0; --i )
00063         {
00064             v += aN[i] * m_aCPs[iSpan-m_iDegree+i];
00065         };
00066         return v;
00067     };
00068     void Serialize( Stream &s );
00069     inline Vector TransformToWorld( const Vector &v )
00070     {
00071         Matrix m = GetMatrix();
00072         return m.Transform( v );
00073     };
00074     inline Vector TransformToLocal( const Vector &v )
00075     {
00076         Matrix m = GetMatrix();
00077         m.Invert();
00078         return m.Transform( v );
00079     };
00080     inline NURBSCurve &operator =( const NURBSCurve &c )
00081     {
00082         m_iDegree = c.m_iDegree;
00083         m_iCPs = c.m_iCPs;
00084         m_iSelectedCP = c.m_iSelectedCP;
00085         m_bEdited = c.m_bEdited;
00086         m_bVisible = c.m_bVisible;
00087         m_bClosed = c.m_bClosed;
00088         m_bPrecalculated = c.m_bPrecalculated;
00089         m_fTotalLength = c.m_fTotalLength;
00090         m_fPosX = c.m_fPosX;
00091         m_fPosY = c.m_fPosY;
00092         m_fAngle = c.m_fAngle;
00093         m_fScale = c.m_fScale;
00094         m_sName = c.m_sName;
00095         if( !m_pFalloff && c.m_pFalloff )
00096             m_pFalloff = new NURBSCurve( 3, false );
00097         if( c.m_pFalloff )
00098             *m_pFalloff = *(c.m_pFalloff);
00099         c.m_aCPs.Clone( m_aCPs );
00100         c.m_aKnots.Clone( m_aKnots );
00101         c.m_aPrecalc.Clone( m_aPrecalc );
00102         return *this;
00103     };
00104     void PrecalculateValues( void );
00105     void CalculateChordLengths( void );
00106     float GetChordPos( float fLength );
00107     void Close( void );
00108 
00109     int m_iCPs, m_iSelectedCP;
00110     bool m_bEdited, m_bVisible, m_bClosed, m_bPrecalculated;
00111     float m_fTotalLength;
00112     float m_fPosX, m_fPosY, m_fAngle, m_fScale;
00113     QString m_sName;
00114     Store<Vector> m_aCPs;
00115     Store<float> m_aKnots;
00116     Store<float> m_aPrecalc;
00117     NURBSCurve *m_pFalloff;
00118 
00119 protected:
00120     int m_iDegree;
00121 
00122     struct ChordLength
00123     {
00124         ChordLength( float fPos = 0.0f, float fLength = 0.0f ) {m_fPos = fPos; m_fLength = fLength;};
00125         float m_fPos, m_fLength;
00126     };
00127     void CalculateChordLength( float fStart, float fEnd );
00128     Store<ChordLength> m_aChordLengths;
00129 };
00130 
00131 class MBDLL_DECL NURBSCurveEditor
00132 {
00133 public:
00134     NURBSCurveEditor( NURBSCurve *pCurve );
00135     ~NURBSCurveEditor( void );
00136     void Refresh( void );
00137     void OnMouseClick( float fX, float fY );
00138     void OnMouseRelease( float fX, float fY );
00139     void OnMouseMove( float fX, float fY );
00140     void OnDelete( void );
00141     void MoveCPTo( const Vector &v );
00142 
00143     NURBSCurve *m_pCurve;
00144     Vector m_vDragDistance, m_vStartDrag;
00145 };
00146 
00147 
00148 MBDLL_DECL AttributeWidget *CreateNewCurveWidget( QWidget *pParent, int iWidth, Attribute *pAttribute, bool bAddSnapAndStoreTo = true,
00149                                                  bool bInAdjustColor = false );
00150 
00151 class AttributeCurvePointer : public AttributePointer<NURBSCurve>
00152 {
00153     AttributeWidget *CreateEditorWidget( QWidget *pParent, int iWidth ) { return CreateNewCurveWidget( pParent, iWidth, this ); };
00154 
00155 public:
00156     AttributeCurvePointer( Node *pOwner = 0, const QString &sID = "") : AttributePointer<NURBSCurve>( pOwner, sID ) { };
00157 
00158     void SetPointerValue( NURBSCurve *cValue, bool bX = false )
00159     {
00160         NURBSCurve *curve = AttributePointer<NURBSCurve>::Value();
00161         NURBSCurve *newCurve = new NURBSCurve( 3 );
00162         if( curve  )
00163             delete curve;
00164         if( cValue )
00165             *newCurve = *cValue;
00166         AttributePointer<NURBSCurve>::SetPointerValue( newCurve, bX );
00167     };
00168 };
00169 
00170 typedef AttributeCurvePointer acurveptr;
00171 
00172 }; // end of namespace mudbox