Overte C++ Documentation
ShapeInfo.h
1 //
2 // ShapeInfo.h
3 // libraries/physics/src
4 //
5 // Created by Andrew Meadows 2014.10.29
6 // Copyright 2014 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_ShapeInfo_h
13 #define hifi_ShapeInfo_h
14 
15 #include <QVector>
16 #include <QString>
17 #include <QUrl>
18 #include <glm/glm.hpp>
19 #include <glm/gtx/norm.hpp>
20 
21 const float MIN_SHAPE_OFFSET = 0.001f; // offsets less than 1mm will be ignored
22 
23 // Bullet has a mesh generation util for convex shapes that we used to
24 // trim convex hulls with many points down to only 42 points.
25 const int MAX_HULL_POINTS = 42;
26 
27 
28 const int32_t END_OF_MESH_PART = -1; // bogus vertex index at end of mesh part
29 const int32_t END_OF_MESH = -2; // bogus vertex index at end of mesh
30 
31 enum ShapeType {
32  SHAPE_TYPE_NONE,
33  SHAPE_TYPE_BOX,
34  SHAPE_TYPE_SPHERE,
35  SHAPE_TYPE_CAPSULE_X,
36  SHAPE_TYPE_CAPSULE_Y,
37  SHAPE_TYPE_CAPSULE_Z,
38  SHAPE_TYPE_CYLINDER_X,
39  SHAPE_TYPE_CYLINDER_Y,
40  SHAPE_TYPE_CYLINDER_Z,
41  SHAPE_TYPE_HULL,
42  SHAPE_TYPE_PLANE,
43  SHAPE_TYPE_COMPOUND,
44  SHAPE_TYPE_SIMPLE_HULL,
45  SHAPE_TYPE_SIMPLE_COMPOUND,
46  SHAPE_TYPE_STATIC_MESH,
47  SHAPE_TYPE_ELLIPSOID,
48  SHAPE_TYPE_CIRCLE,
49  SHAPE_TYPE_MULTISPHERE
50 };
51 
52 class ShapeInfo {
53 
54 public:
55 
56  using PointList = QVector<glm::vec3>;
57  using PointCollection = QVector<PointList>;
58  using TriangleIndices = QVector<int32_t>;
59  using SphereData = glm::vec4;
60  using SphereCollection = QVector<SphereData>;
61 
62  static QString getNameForShapeType(ShapeType type);
63  static ShapeType getShapeTypeForName(QString string);
64 
65  void clear();
66 
67  void setParams(ShapeType type, const glm::vec3& halfExtents, QString url="");
68  void setBox(const glm::vec3& halfExtents);
69  void setSphere(float radius);
70  void setPointCollection(const PointCollection& pointCollection);
71  void setCapsuleY(float radius, float cylinderHalfHeight);
72  void setMultiSphere(const std::vector<glm::vec3>& centers, const std::vector<float>& radiuses);
73  void setOffset(const glm::vec3& offset);
74 
75  ShapeType getType() const { return _type; }
76 
77  const glm::vec3& getHalfExtents() const { return _halfExtents; }
78  const glm::vec3& getOffset() const { return _offset; }
79  uint32_t getNumSubShapes() const;
80 
81  PointCollection& getPointCollection() { return _pointCollection; }
82  const PointCollection& getPointCollection() const { return _pointCollection; }
83  const SphereCollection& getSphereCollection() const { return _sphereCollection; }
84 
85  TriangleIndices& getTriangleIndices() { return _triangleIndices; }
86  const TriangleIndices& getTriangleIndices() const { return _triangleIndices; }
87 
88  int getLargestSubshapePointCount() const;
89 
90  float computeVolume() const;
91 
92  uint64_t getHash() const;
93 
94 protected:
95  void setHalfExtents(const glm::vec3& halfExtents);
96 
97  QUrl _url; // url for model of convex collision hulls
98  SphereCollection _sphereCollection;
99  PointCollection _pointCollection;
100  TriangleIndices _triangleIndices;
101  glm::vec3 _halfExtents = glm::vec3(0.0f);
102  glm::vec3 _offset = glm::vec3(0.0f);
103  mutable uint64_t _hash64;
104  ShapeType _type = SHAPE_TYPE_NONE;
105 };
106 
107 #endif // hifi_ShapeInfo_h
108