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  Solid<N>& fitDimension(float newMaxDimension) {
41  float maxDimension = 0;
42  for (const auto& vertex : vertices) {
43  maxDimension = std::max(maxDimension, std::max(std::max(vertex.x, vertex.y), vertex.z));
44  }
45  float multiplier = newMaxDimension / maxDimension;
46  for (auto& vertex : vertices) {
47  vertex *= multiplier;
48  }
49  return *this;
50  }
51 
52  Vec getFaceNormal(size_t faceIndex) const {
53  Vec result;
54  const auto& face = faces[faceIndex];
55  for (size_t i = 0; i < N; ++i) {
56  result += vertices[face[i]];
57  }
58  result /= N;
59  return glm::normalize(result);
60  }
61  };
62 
63  template <size_t N>
64  size_t triangulatedFaceTriangleCount() {
65  return N - 2;
66  }
67 
68  template <size_t N>
69  size_t triangulatedFaceIndexCount() {
70  return triangulatedFaceTriangleCount<N>() * 3;
71  }
72 
73  Solid<3> tesselate(const Solid<3>& solid, int count);
74  const Solid<3>& tetrahedron();
75  const Solid<4>& cube();
76  const Solid<3>& octahedron();
77  const Solid<5>& dodecahedron();
78  const Solid<3>& icosahedron();
79 }
80 
81 #endif