Overte C++ Documentation
CubeProjectedPolygon.h
1 //
2 // CubeProjectedPolygon.h
3 // libraries/shared/src
4 //
5 // Created by Brad Hefta-Gaub on 06/11/13.
6 // Copyright 2013 High Fidelity, Inc.
7 //
8 // The projected shadow (on the 2D view plane) for a cube
9 //
10 // Distributed under the Apache License, Version 2.0.
11 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
12 //
13 
14 #ifndef hifi_CubeProjectedPolygon_h
15 #define hifi_CubeProjectedPolygon_h
16 
17 #include <glm/glm.hpp>
18 
19 // there's a max of 6 vertices of a project polygon, and a max of twice that when clipped to the screen
20 const int MAX_PROJECTED_POLYGON_VERTEX_COUNT = 6;
21 const int MAX_CLIPPED_PROJECTED_POLYGON_VERTEX_COUNT = MAX_PROJECTED_POLYGON_VERTEX_COUNT * 2;
22 typedef glm::vec2 ProjectedVertices[MAX_CLIPPED_PROJECTED_POLYGON_VERTEX_COUNT];
23 
24 class BoundingRectangle {
25 public:
26  enum { BOTTOM_LEFT, BOTTOM_RIGHT, TOP_RIGHT, TOP_LEFT, VERTEX_COUNT };
27 
28  BoundingRectangle(const glm::vec2 corner, const glm::vec2 size) : corner(corner), size(size), _set(true) {}
29  BoundingRectangle() : _set(false) {}
30  glm::vec2 corner;
31  glm::vec2 size;
32  bool contains(const BoundingRectangle& box) const;
33  bool contains(const glm::vec2& point) const;
34  bool pointInside(const glm::vec2& point) const { return contains(point); }
35 
36  void explandToInclude(const BoundingRectangle& box);
37 
38  float area() const { return size.x * size.y; }
39 
40  int getVertexCount() const { return VERTEX_COUNT; }
41  glm::vec2 getVertex(int vertexNumber) const;
42 
43  BoundingRectangle topHalf() const;
44  BoundingRectangle bottomHalf() const;
45  BoundingRectangle leftHalf() const;
46  BoundingRectangle rightHalf() const;
47 
48  float getMaxX() const { return corner.x + size.x; }
49  float getMaxY() const { return corner.y + size.y; }
50  float getMinX() const { return corner.x; }
51  float getMinY() const { return corner.y; }
52 
53  void printDebugDetails(const char* label=NULL) const;
54 private:
55  bool _set;
56 };
57 
58 const int PROJECTION_RIGHT = 1;
59 const int PROJECTION_LEFT = 2;
60 const int PROJECTION_BOTTOM = 4;
61 const int PROJECTION_TOP = 8;
62 const int PROJECTION_NEAR = 16;
63 const int PROJECTION_FAR = 32;
64 const int PROJECTION_CLIPPED = 64;
65 
66 class CubeProjectedPolygon {
67 
68 public:
69  CubeProjectedPolygon(const BoundingRectangle& box);
70 
71  CubeProjectedPolygon(int vertexCount = 0) :
72  _vertexCount(vertexCount),
73  _maxX(-FLT_MAX), _maxY(-FLT_MAX), _minX(FLT_MAX), _minY(FLT_MAX),
74  _distance(0)
75  { }
76 
77  ~CubeProjectedPolygon() { }
78  const ProjectedVertices& getVertices() const { return _vertices; }
79  const glm::vec2& getVertex(int i) const { return _vertices[i]; }
80  void setVertex(int vertex, const glm::vec2& point);
81 
82  int getVertexCount() const { return _vertexCount; }
83  float getDistance() const { return _distance; }
84  bool getAnyInView() const { return _anyInView; }
85  bool getAllInView() const { return _allInView; }
86  unsigned char getProjectionType() const { return _projectionType; }
87  void setVertexCount(int vertexCount) { _vertexCount = vertexCount; }
88  void setDistance(float distance) { _distance = distance; }
89  void setAnyInView(bool anyInView) { _anyInView = anyInView; }
90  void setAllInView(bool allInView) { _allInView = allInView; }
91  void setProjectionType(unsigned char type) { _projectionType = type; }
92 
93 
94  bool pointInside(const glm::vec2& point, bool* matchesVertex = NULL) const;
95  bool occludes(const CubeProjectedPolygon& occludee, bool checkAllInView = false) const;
96  bool occludes(const BoundingRectangle& occludee) const;
97  bool intersects(const CubeProjectedPolygon& testee) const;
98  bool intersects(const BoundingRectangle& box) const;
99  bool matches(const CubeProjectedPolygon& testee) const;
100  bool matches(const BoundingRectangle& testee) const;
101  bool intersectsOnAxes(const CubeProjectedPolygon& testee) const;
102 
103  bool canMerge(const CubeProjectedPolygon& that) const;
104  void merge(const CubeProjectedPolygon& that); // replaces vertices of this with new merged version
105 
106  float getMaxX() const { return _maxX; }
107  float getMaxY() const { return _maxY; }
108  float getMinX() const { return _minX; }
109  float getMinY() const { return _minY; }
110 
111  BoundingRectangle getBoundingBox() const {
112  return BoundingRectangle(glm::vec2(_minX,_minY), glm::vec2(_maxX - _minX, _maxY - _minY));
113  }
114 
115  void printDebugDetails() const;
116 
117  static long pointInside_calls;
118  static long occludes_calls;
119  static long intersects_calls;
120 
121 private:
122  int _vertexCount;
123  ProjectedVertices _vertices;
124  float _maxX;
125  float _maxY;
126  float _minX;
127  float _minY;
128  float _distance;
129  bool _anyInView; // if any points are in view
130  bool _allInView; // if all points are in view
131  unsigned char _projectionType;
132 };
133 
134 
135 #endif // hifi_CubeProjectedPolygon_h