Debian Codegraph @

libs/filamentapp/src/IcoSphere.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <filamentapp/IcoSphere.h>
18
19 #include <array>
20
21 static constexpr float X = .525731112119133606f;
22 static constexpr float Z = .850650808352039932f;
23 static constexpr float N = 0.f;
24
25 const IcoSphere::VertexList IcoSphere::sVertices = {
26 { -X, N, Z }, { X, N, Z }, { -X, N, -Z }, { X, N, -Z },
27 { N, Z, X }, { N, Z, -X }, { N, -Z, X }, { N, -Z, -X },
28 { Z, X, N }, { -Z, X, N }, { Z, -X, N }, { -Z, -X, N }
29 };
30
31 const IcoSphere::TriangleList IcoSphere::sTriangles = {
32 { 1, 4, 0 }, { 4, 9, 0 }, { 4, 5, 9 }, { 8, 5, 4 }, { 1, 8, 4 },
33 { 1, 10, 8 }, { 10, 3, 8 }, { 8, 3, 5 }, { 3, 2, 5 }, { 3, 7, 2 },
34 { 3, 10, 7 }, { 10, 6, 7 }, { 6, 11, 7 }, { 6, 0, 11 }, { 6, 1, 0 },
35 { 10, 1, 6 }, { 11, 0, 9 }, { 2, 11, 9 }, { 5, 2, 9 }, { 11, 2, 7 }
36 };
37
38 IcoSphere::IcoSphere(size_t subdivisions) {
39 mMesh = make_icosphere(subdivisions);
40 }
41
42 IcoSphere::Index IcoSphere::vertex_for_edge(
43 Lookup& lookup, VertexList& vertices, Index first, Index second) {
44 Lookup::key_type key(first, second);
45 if (key.first > key.second) {
46 std::swap(key.first, key.second);
47 }
48
49 auto inserted = lookup.insert({ key, (Lookup::mapped_type)vertices.size() });
50 if (inserted.second) {
51 auto edge0 = vertices[first];
52 auto edge1 = vertices[second];
53 auto point = normalize(edge0 + edge1);
54 vertices.push_back(point);
55 }
56
57 return inserted.first->second;
58 }
59
60 IcoSphere::TriangleList IcoSphere::subdivide(VertexList& vertices, TriangleList const& triangles) {
61 Lookup lookup;
62 TriangleList result;
63 for (Triangle const& each : triangles) {
64 std::array<Index, 3> mid;
65 mid[0] = vertex_for_edge(lookup, vertices, each.vertex[0], each.vertex[1]);
66 mid[1] = vertex_for_edge(lookup, vertices, each.vertex[1], each.vertex[2]);
67 mid[2] = vertex_for_edge(lookup, vertices, each.vertex[2], each.vertex[0]);
68 result.push_back({ each.vertex[0], mid[0], mid[2] });
69 result.push_back({ each.vertex[1], mid[1], mid[0] });
70 result.push_back({ each.vertex[2], mid[2], mid[1] });
71 result.push_back({ mid[0], mid[1], mid[2] });
72 }
73 return result;
74 }
75
76 IcoSphere::IndexedMesh IcoSphere::make_icosphere(int subdivisions) {
77 VertexList vertices = sVertices;
78 TriangleList triangles = sTriangles;
79 for (int i = 0; i < subdivisions; ++i) {
80 triangles = subdivide(vertices, triangles);
81 }
82 return { vertices, triangles };
83 }