Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
7.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Mesh Manipulation Utilities
  4. //
  5. //=============================================================================
  6. #ifndef DMMESHUTILS_H
  7. #define DMMESHUTILS_H
  8. #if defined( _WIN32 )
  9. #pragma once
  10. #endif
  11. #include "movieobjects/dmevertexdata.h"
  12. #include "tier1/utlstring.h"
  13. #include "tier1/UtlStringMap.h"
  14. #include "tier1/utlvector.h"
  15. class CDmeMesh;
  16. class CDmMeshComp::CEdge;
  17. //-----------------------------------------------------------------------------
  18. // Mesh Utility function class (more like a namespace)
  19. //-----------------------------------------------------------------------------
  20. class CDmMeshUtils
  21. {
  22. public:
  23. static bool RemoveLargeAxisAlignedPlanarFaces( CDmeMesh *pMesh );
  24. static bool RemoveFacesWithMaterial( CDmeMesh *pMesh, const char *pMaterialName );
  25. static bool RemoveFacesWithMoreThanNVerts( CDmeMesh *pMesh, const int nVertexCount );
  26. enum Axis_t
  27. {
  28. kXAxis,
  29. kYAxis,
  30. kZAxis
  31. };
  32. static bool Mirror( CDmeMesh *pMesh, int axis = kXAxis );
  33. static bool RemapMaterial( CDmeMesh *pMesh, const CUtlString &src, const CUtlString &dst );
  34. static bool RemapMaterial( CDmeMesh *pMesh, const int nMaterialIndex, const CUtlString &dst );
  35. // Merge the specified mesh onto the first mesh under pRoot that fits it
  36. static bool Merge( CDmeMesh *pSrcMesh, CDmElement *pRoot );
  37. static bool Merge( CDmeMesh *pSrcMesh, CDmeMesh *pDstMesh, int nSkinningJointIndex );
  38. static bool CreateDeltasFromPresets( CDmeMesh *pMesh, CDmeVertexData *pPassedDst, const CUtlStringMap< CUtlString > &presetMap, bool bPurge, const CUtlVector< CUtlString > *pPurgeAllButThese = NULL );
  39. static bool PurgeUnusedDeltas( CDmeMesh *pMesh );
  40. enum WrinkleOp
  41. {
  42. kReplace,
  43. kAdd
  44. };
  45. static bool CreateWrinkleDeltaFromBaseState( CDmeVertexDeltaData *pDelta, float flScale = 1.0f, WrinkleOp wrinkleOp = kReplace, CDmeMesh *pMesh = NULL, CDmeVertexData *pBind = NULL, CDmeVertexData *pCurrent = NULL );
  46. static int FindMergeSocket(
  47. const CUtlVector< CUtlVector< CDmMeshComp::CEdge * > > &srcBorderEdgesList,
  48. CDmeMesh *pDstMesh );
  49. static bool Merge( CDmMeshComp &srcComp, const CUtlVector< CDmMeshComp::CEdge * > &edgeList, CDmeMesh *pDstMesh );
  50. protected:
  51. static const int *BuildDataMirrorMap( CDmeVertexData *pBase, int axis, CDmeVertexData::StandardFields_t standardField, CUtlVector< int > &dataMirrorMap );
  52. static bool MirrorVertices( CDmeMesh *pMesh, CDmeVertexData *pBase, int axis, CUtlVector< int > &mirrorMap );
  53. static bool MirrorVertices( CDmeVertexData *pBase, int axis, int nOldVertexCount, int nMirrorCount, const CUtlVector< int > &mirrorMap, const CUtlVector< int > &posMirrorMap, const CUtlVector< int > &normalMirrorMap, const CUtlVector< int > &uvMirrorMap );
  54. static void MirrorVertices( CDmeVertexData *pBase, FieldIndex_t fieldIndex, int nOldVertexCount, int nMirrorCount, const CUtlVector< int > &baseIndices, const CUtlVector< int > &mirrorMap );
  55. static bool MirrorDelta( CDmeVertexDeltaData *pDelta, int axis, const CUtlVector< int > &posMirrorMap, const CUtlVector< int > &normalMirrorMap, const CUtlVector< int > &uvMirrorMap );
  56. static bool PurgeUnusedData( CDmeMesh *pMesh );
  57. static void CreateDeltasFromPresetGroup( CDmePresetGroup *pPresetGroup, CDmeCombinationOperator * pComboOp, const CUtlVector< CUtlString > * pPurgeAllButThese, CDmeMesh * pMesh, CDmeVertexData * pDst, CUtlStringMap< CUtlString > &conflictingNames, CUtlStringMap< CDmePreset * > &presetMap );
  58. static void PurgeUnreferencedDeltas( CDmeMesh *pMesh, CUtlStringMap< CDmePreset * > &presetMap, const CUtlVector< CUtlString > *pPurgeAllButThese, CDmeCombinationOperator *pComboOp );
  59. };
  60. //-----------------------------------------------------------------------------
  61. // Iterate over all of the faces in a mesh. Use it like this:
  62. //
  63. // for ( CDmMeshFaceIt fIt( pMesh ); !fIt.IsDone(); fIt.Next() )
  64. // {
  65. // }
  66. //-----------------------------------------------------------------------------
  67. class CDmMeshFaceIt
  68. {
  69. public:
  70. // Constructs a new face iterator for the specified mesh
  71. CDmMeshFaceIt( const CDmeMesh *pMesh, const CDmeVertexData *pVertexData = NULL );
  72. // Resets the iterator to the start of the specified mesh or for another iteration of the
  73. // current mesh if the specified mesh is NULL
  74. bool Reset( const CDmeMesh *pMesh = NULL, const CDmeVertexData *pVertexData = NULL );
  75. // Returns the total number of faces in the mesh
  76. int Count() const;
  77. // The number of vertices in the face
  78. int VertexCount() const;
  79. // Is the iterator at the end of the mesh?
  80. bool IsDone() const;
  81. // Move the iterator to the next face
  82. bool Next();
  83. // The face index of the current face in the range [ 0, Count() ]
  84. int Index() const;
  85. // Returns opposite sense of IsDone(), i.e. !IsDone() so true if iterator still has stuff to do
  86. operator bool() const { return !IsDone(); }
  87. // Prefix ++ which just calls Next() for syntax sugar
  88. CDmMeshFaceIt &operator ++() { Next(); return *this; }
  89. // Postfix ++ which just calls Next() for syntax sugar
  90. CDmMeshFaceIt operator ++( int ) { const CDmMeshFaceIt thisCopy( *this ); ++( *this ); return thisCopy; }
  91. // Gets the vertex indices for the vertices of this face
  92. bool GetVertexIndices( int *pIndices, int nIndices ) const;
  93. // Gets the vertex indices for the vertices of this face
  94. bool GetVertexIndices( CUtlVector< int > &vertexIndices ) const;
  95. // Gets the mesh relative vertex index given the face relative vertex index
  96. int GetVertexIndex( int nFaceRelativeVertexIndex ) const;
  97. // Gets the specified vertex data for the vertices of this face (if it exists) and the type matches
  98. template < class T_t >
  99. bool GetVertexData(
  100. CUtlVector< T_t > &vertexData,
  101. CDmeVertexDataBase::StandardFields_t nStandardField,
  102. CDmeVertexData *pPassedBase = NULL ) const;
  103. protected:
  104. bool SetFaceSet();
  105. const CDmeMesh *m_pMesh; // Current Mesh
  106. const CDmeVertexData *m_pVertexData; // Current Vertex Data
  107. int m_nFaceSetCount; // Number of face sets in current mesh: m_pMesh->FaceSetCount();
  108. int m_nFaceSetIndex; // Index of current face set: [ 0, m_nFaceSetCount ]
  109. const CDmeFaceSet *m_pFaceSet; // Current face set: m_pMesh->GetFaceSet( m_nFaceSetIndex )
  110. int m_nFaceSetIndexCount; // Number of indices in current face set: m_pFaceSet->NumIndices();
  111. int m_nFaceSetIndexIndex; // Index of current face set's indices: [ 0, m_nFaceSetIndexCount ]
  112. int m_nFaceCount; // Number of faces in current mesh
  113. int m_nFaceIndex; // The current face in the iteration [ 0, m_nFaceCount ]
  114. };
  115. //-----------------------------------------------------------------------------
  116. //
  117. //-----------------------------------------------------------------------------
  118. template < class T_t >
  119. inline bool CDmMeshFaceIt::GetVertexData(
  120. CUtlVector< T_t > &vertexData,
  121. CDmeVertexDataBase::StandardFields_t nStandardField,
  122. CDmeVertexData *pPassedBase /* = NULL */ ) const
  123. {
  124. vertexData.RemoveAll();
  125. if ( IsDone() )
  126. return false;
  127. CDmeVertexData *pBase = pPassedBase ? pPassedBase : m_pMesh->GetCurrentBaseState();
  128. if ( !pBase )
  129. return false;
  130. const FieldIndex_t nFieldIndex = pBase->FindFieldIndex( nStandardField );
  131. if ( nFieldIndex < 0 )
  132. return false;
  133. CDmAttribute *pDataAttr = pBase->GetVertexData( nFieldIndex );
  134. if ( pDataAttr->GetType() != CDmAttributeInfo< CUtlVector< T_t > >().AttributeType() )
  135. return false;
  136. const CDmrArrayConst< T_t > data( pDataAttr );
  137. const CUtlVector< int > &indices( pBase->GetVertexIndexData( nFieldIndex ) );
  138. }
  139. #endif // DMMESHUTILS_H