Overte C++ Documentation
Shapes.h
1 //
2 // Created by Bradley Austin Davis on 2016/05/26
3 // Copyright 2013-2016 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 
9 #pragma once
10 #ifndef hifi_shared_shapes
11 #define hifi_shared_shapes
12 
13 #include <stdint.h>
14 
15 #include <vector>
16 #include <array>
17 
18 #include <glm/glm.hpp>
19 
20 namespace geometry {
21 
22  using Index = uint32_t;
23  using Vec = glm::vec3;
24  using VertexVector = std::vector<Vec>;
25  using TexCoordVector = std::vector<glm::vec2>;
26  using IndexVector = std::vector<Index>;
27 
28  template <size_t N>
29  using Face = std::array<Index, N>;
30 
31  template <size_t N>
32  using FaceVector = std::vector<Face<N>>;
33 
34  template <size_t N>
35  struct Solid {
36  VertexVector vertices;
37  TexCoordVector texCoords;
38  FaceVector<N> faces;
39 
40  template <typename T>
41  std::vector<T> getIndices() const {
42  size_t count = faces.size() * N;
43 
44  std::vector<T> indices;
45  indices.reserve(count);
46  for (const auto& face : faces) {
47  for (const auto& index : face) {
48  indices.push_back((T)index);
49  }
50  }
51  return indices;
52  }
53 
54  Solid<N>& fitDimension(float newMaxDimension) {
55  float maxDimension = 0;
56  for (const auto& vertex : vertices) {
57  maxDimension = std::max(maxDimension, std::max(std::max(vertex.x, vertex.y), vertex.z));
58  }
59  float multiplier = newMaxDimension / maxDimension;
60  for (auto& vertex : vertices) {
61  vertex *= multiplier;
62  }
63  return *this;
64  }
65 
66  Vec getFaceNormal(size_t faceIndex) const {
67  Vec result;
68  const auto& face = faces[faceIndex];
69  for (size_t i = 0; i < N; ++i) {
70  result += vertices[face[i]];
71  }
72  result /= N;
73  return glm::normalize(result);
74  }
75  };
76 
77  template <size_t N>
78  size_t triangulatedFaceTriangleCount() {
79  return N - 2;
80  }
81 
82  template <size_t N>
83  size_t triangulatedFaceIndexCount() {
84  return triangulatedFaceTriangleCount<N>() * 3;
85  }
86 
87  Solid<3> tesselate(const Solid<3>& solid, int count);
88  const Solid<3>& tetrahedron();
89  const Solid<4>& cube();
90  const Solid<3>& octahedron();
91  const Solid<5>& dodecahedron();
92  const Solid<3>& icosahedron();
93 }
94 
95 #endif