nurbs.h

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