Shape.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /******************************************************************************
  2. Use 'Shape' to store custom kind of shape.
  3. /******************************************************************************/
  4. enum SHAPE_TYPE : Byte // Shape Type
  5. {
  6. SHAPE_NONE , // none
  7. SHAPE_POINT , // point
  8. SHAPE_EDGE , // edge
  9. SHAPE_RECT , // rectangle
  10. SHAPE_BOX , // box
  11. SHAPE_OBOX , // oriented box
  12. SHAPE_CIRCLE , // circle
  13. SHAPE_BALL , // ball
  14. SHAPE_CAPSULE, // capsule
  15. SHAPE_TUBE , // tube
  16. SHAPE_TORUS , // torus
  17. SHAPE_CONE , // cone
  18. SHAPE_PYRAMID, // pyramid
  19. SHAPE_PLANE , // plane
  20. SHAPE_NUM , // number of shape types
  21. };
  22. /******************************************************************************/
  23. struct Shape // General Shape
  24. {
  25. SHAPE_TYPE type; // shape type
  26. union
  27. {
  28. Vec point ;
  29. Edge edge ;
  30. Rect rect ;
  31. Box box ;
  32. OBox obox ;
  33. Circle circle ;
  34. Ball ball ;
  35. Capsule capsule;
  36. Tube tube ;
  37. Torus torus ;
  38. Cone cone ;
  39. Pyramid pyramid;
  40. Plane plane ;
  41. };
  42. Shape& operator+=(C Vec &v);
  43. Shape& operator-=(C Vec &v);
  44. Shape& operator*=( Flt f);
  45. Shape& operator/=( Flt f);
  46. Shape& operator*=(C Vec &v);
  47. Shape& operator/=(C Vec &v);
  48. Shape& operator*=(C Matrix3 &m);
  49. Shape& operator*=(C Matrix &m);
  50. friend Shape operator+ (C Shape &shape, C Vec &v);
  51. friend Shape operator- (C Shape &shape, C Vec &v);
  52. friend Shape operator* (C Shape &shape, Flt f);
  53. friend Shape operator/ (C Shape &shape, Flt f);
  54. friend Shape operator* (C Shape &shape, C Vec &v);
  55. friend Shape operator/ (C Shape &shape, C Vec &v);
  56. friend Shape operator* (C Shape &shape, C Matrix3 &m);
  57. friend Shape operator* (C Shape &shape, C Matrix &m);
  58. // get / set
  59. Flt area ( )C; // get surface area
  60. Flt volume ( )C; // get volume
  61. Vec pos ( )C; // get position
  62. void pos (C Vec &pos) ; // set position
  63. Matrix asMatrix ( )C; // try to return the shape as a matrix, where matrix position is the shape position, and matrix orientation is the shape orientation, matrix scale is identity
  64. Matrix asMatrixScaled( )C; // try to return the shape as a matrix, where matrix position is the shape position, and matrix orientation is the shape orientation, matrix scale depends on shape size
  65. Str asText (Bool include_shape_type_name)C; // get shape text description
  66. // operations
  67. Shape& extend(Flt e); // extend
  68. Shape& move (C Vec &v) {return T+=v;}
  69. Shape& scale ( Flt f) {return T*=f;}
  70. Shape& scale (C Vec &v) {return T*=v;}
  71. Shape& transform(C Matrix3 &m) {return T*=m;}
  72. Shape& transform(C Matrix &m) {return T*=m;}
  73. Shape& mirrorX(); // mirror in X axis
  74. Shape& mirrorY(); // mirror in Y axis
  75. Shape& mirrorZ(); // mirror in Z axis
  76. // draw
  77. void draw(C Color &color=WHITE, Bool fill=false)C; // this can be optionally called outside of Render function, this relies on active object matrix which can be set using 'SetMatrix' function
  78. // io
  79. Bool save(File &f)C; // save, false on fail
  80. Bool load(File &f) ; // load, false on fail
  81. Shape( ) {type=SHAPE_NONE ; }
  82. Shape(C Vec &point ) {type=SHAPE_POINT ; T.point =point ;}
  83. Shape(C Edge &edge ) {type=SHAPE_EDGE ; T.edge =edge ;}
  84. Shape(C Rect &rect ) {type=SHAPE_RECT ; T.rect =rect ;}
  85. Shape(C Box &box ) {type=SHAPE_BOX ; T.box =box ;}
  86. Shape(C OBox &obox ) {type=SHAPE_OBOX ; T.obox =obox ;}
  87. Shape(C Extent &ext ) {type=SHAPE_BOX ; T.box =ext ;}
  88. Shape(C Circle &circle ) {type=SHAPE_CIRCLE ; T.circle =circle ;}
  89. Shape(C Ball &ball ) {type=SHAPE_BALL ; T.ball =ball ;}
  90. Shape(C Capsule &capsule) {type=SHAPE_CAPSULE; T.capsule=capsule;}
  91. Shape(C Tube &tube ) {type=SHAPE_TUBE ; T.tube =tube ;}
  92. Shape(C Torus &torus ) {type=SHAPE_TORUS ; T.torus =torus ;}
  93. Shape(C Cone &cone ) {type=SHAPE_CONE ; T.cone =cone ;}
  94. Shape(C Pyramid &pyramid) {type=SHAPE_PYRAMID; T.pyramid=pyramid;}
  95. Shape(C Plane &plane ) {type=SHAPE_PLANE ; T.plane =plane ;}
  96. };
  97. /******************************************************************************/
  98. // shape type
  99. #if EE_PRIVATE
  100. Bool ShapeType2D (SHAPE_TYPE type); // if shape type is 2-dimensional
  101. Bool ShapeTypeRound(SHAPE_TYPE type); // if shape type has round elements
  102. #endif
  103. CChar* ShapeTypeName(SHAPE_TYPE type); // get shape type name from 'type'
  104. // sweep
  105. Bool SweepPointShape(C Vec &point, C Vec &move, C Shape &shape, Flt *hit_frac=null, Vec *hit_normal=null); // if moving point cuts through a static shape
  106. // distance
  107. Flt Dist(C Shape &a, C Shape &b); // get the distance between 'a' and 'b' shape, warning: not all 'a' 'b' shape type combinations are supported in this function, to check if a certain combination is supported please check the headers for 'a' and 'b' shape types, and check if there is a separate 'Dist' function of 'a' type and 'b' type
  108. // cuts
  109. Bool Cuts(C Vec2 &p, C Shape &s); // if point 'p' cuts 's'
  110. Bool Cuts(C Vec &p, C Shape &s); // if point 'p' cuts 's'
  111. Bool Cuts(C Shape &a, C Shape &b); // if shape 'a' cuts 'b', warning: not all 'a' 'b' shape type combinations are supported in this function, to check if a certain combination is supported please check the headers for 'a' and 'b' shape types, and check if there is a separate 'Cuts' function of 'a' type and 'b' type
  112. /******************************************************************************/