Counter Strike : Global Offensive Source Code
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.

104 lines
4.7 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. //
  3. // Utility functions for polygon simplification / convex decomposition.
  4. //
  5. //===============================================================================
  6. #ifndef POLYGON_H
  7. #define POLYGON_H
  8. #if defined( COMPILER_MSVC )
  9. #pragma once
  10. #endif
  11. #include "utlvector.h"
  12. //-----------------------------------------------------------------------------
  13. // NOTE: Polygons are assumed to be wound clockwise unless otherwise noted.
  14. // Holes in polygons wind counter-clockwise.
  15. //-----------------------------------------------------------------------------
  16. static const float POINT_IN_POLYGON_EPSILON = 0.01f;
  17. //-----------------------------------------------------------------------------
  18. // Simplifies a polygon by removing points such that the area will not
  19. // decrease by more than the specified amount (area increases are not
  20. // allowed).
  21. // With a low max deviation, this will perfectly remove all colinear points.
  22. //-----------------------------------------------------------------------------
  23. void SimplifyPolygon( CUtlVector< Vector > *pPoints, const Vector &vNormal, float flMaxDeviation );
  24. //-----------------------------------------------------------------------------
  25. // Simplifies a polygon using quadric error metrics.
  26. //-----------------------------------------------------------------------------
  27. void SimplifyPolygonQEM( CUtlVector< Vector > *pPoints, const Vector &vNormal, float flMaximumSquaredError, bool bUseOptimalPointPlacement );
  28. //-----------------------------------------------------------------------------
  29. // Returns whether a vertex of a polygon (v1) is concave, given its normal
  30. // and previous & next vertices (v0 and v2, respectively).
  31. //-----------------------------------------------------------------------------
  32. bool IsConcave( const Vector &v0, const Vector &v1, const Vector &v2, const Vector &vNormal );
  33. //-----------------------------------------------------------------------------
  34. // Returns whether a vertex (points[nVertex]) of a polygon is
  35. // concave, given the polygon's vertices, and its normal.
  36. //-----------------------------------------------------------------------------
  37. bool IsConcave( const Vector *pPolygonPoints, int nPointCount, int nVertex, const Vector &vNormal );
  38. //-----------------------------------------------------------------------------
  39. // Returns whether a polygon is concave.
  40. //-----------------------------------------------------------------------------
  41. bool IsConcave( const Vector *pPolygonPoints, int nPointCount, const Vector &vNormal );
  42. //-----------------------------------------------------------------------------
  43. // Given a set of points (i.e. vertex buffer), this represents an ordered
  44. // subset of points which comprise a polygon (i.e. index buffer).
  45. //-----------------------------------------------------------------------------
  46. struct SubPolygon_t
  47. {
  48. CUtlVector< int > m_Indices;
  49. int GetVertexIndex( int i ) const
  50. {
  51. i = i % m_Indices.Count();
  52. if ( i < 0 )
  53. {
  54. i += m_Indices.Count();
  55. }
  56. return m_Indices[i];
  57. }
  58. static const Vector &GetPoint( const Vector *pPolygonPoints, int nPointCount, int nVertex )
  59. {
  60. nVertex = nVertex % nPointCount;
  61. if ( nVertex < 0 )
  62. {
  63. nVertex += nPointCount;
  64. }
  65. return pPolygonPoints[nVertex];
  66. }
  67. static const Vector &GetPoint( const CUtlVector< Vector > &originalPoints, int nVertex )
  68. {
  69. return GetPoint( originalPoints.Base(), originalPoints.Count(), nVertex );
  70. }
  71. };
  72. //-----------------------------------------------------------------------------
  73. // Attempts to strip off one convex region from a concave/convex polygon.
  74. //-----------------------------------------------------------------------------
  75. void DecomposePolygon_Step( const CUtlVector< Vector > &polygonPoints, const Vector &vNormal, CUtlVector< SubPolygon_t > *pHoles, SubPolygon_t *pNewPartition, SubPolygon_t *pRemainingPolygon, int *pFirstIndex );
  76. //-----------------------------------------------------------------------------
  77. // Decomposes a polygon into one or more convex, non-overlapping parts.
  78. //-----------------------------------------------------------------------------
  79. void DecomposePolygon( const CUtlVector< Vector > &polygonPoints, const Vector &vNormal, SubPolygon_t *pOriginalPolygon, CUtlVector< SubPolygon_t > *pHoles, CUtlVector< SubPolygon_t > *pPartitions );
  80. //-----------------------------------------------------------------------------
  81. // Is a point in the prism formed by extruding the polygon?
  82. // If so, what is its height above/below the plane of the polygon?
  83. //-----------------------------------------------------------------------------
  84. bool IsPointInPolygonPrism( const Vector *pPolygonPoints, int nPointCount, const Vector &vPoint, float flThreshold = 0.0f, float *pHeight = NULL );
  85. #endif // POLYGON_H