Commit eac274e2 authored by Philip Trettner's avatar Philip Trettner
Browse files

Merge branch 'feature/dodecahedron' into 'develop'

Added dodecahedron object

See merge request !28
parents fb5a1bc7 a3ab14df
#include "dodecahedron.hh"
#include <cmath> // std::sqrt
polymesh::unique_array<polymesh::detail::pos3f> polymesh::detail::add_unit_dodecahedron_impl(polymesh::Mesh& m)
{
polymesh::unique_array<pos3f> position(20);
auto const phi = (1 + std::sqrt(5.0f)) / 2.0f;
auto const phi_inv = 1 / phi;
pm::vertex_handle vs[20];
for (size_t i = 0; i < 20; ++i)
vs[i] = m.vertices().add();
position[0] = {1, 1, 1};
position[1] = {1, 1, -1};
position[2] = {1, -1, 1};
position[3] = {1, -1, -1};
position[4] = {-1, 1, 1};
position[5] = {-1, 1, -1};
position[6] = {-1, -1, 1};
position[7] = {-1, -1, -1};
position[8] = {0, phi, phi_inv};
position[9] = {0, phi, -phi_inv};
position[10] = {0, -phi, phi_inv};
position[11] = {0, -phi, -phi_inv};
position[12] = {phi_inv, 0, phi};
position[13] = {phi_inv, 0, -phi};
position[14] = {-phi_inv, 0, phi};
position[15] = {-phi_inv, 0, -phi};
position[16] = {phi, phi_inv, 0};
position[17] = {phi, -phi_inv, 0};
position[18] = {-phi, phi_inv, 0};
position[19] = {-phi, -phi_inv, 0};
m.faces().add({vs[8], vs[9], vs[5], vs[18], vs[4]});
m.faces().add({vs[9], vs[8], vs[0], vs[16], vs[1]});
m.faces().add({vs[8], vs[4], vs[14], vs[12], vs[0]});
m.faces().add({vs[9], vs[1], vs[13], vs[15], vs[5]});
m.faces().add({vs[4], vs[18], vs[19], vs[6], vs[14]});
m.faces().add({vs[5], vs[15], vs[7], vs[19], vs[18]});
m.faces().add({vs[13], vs[3], vs[11], vs[7], vs[15]});
m.faces().add({vs[16], vs[17], vs[3], vs[13], vs[1]});
m.faces().add({vs[0], vs[12], vs[2], vs[17], vs[16]});
m.faces().add({vs[14], vs[6], vs[10], vs[2], vs[12]});
m.faces().add({vs[19], vs[7], vs[11], vs[10], vs[6]});
m.faces().add({vs[17], vs[2], vs[10], vs[11], vs[3]});
return position;
}
#pragma once
#include <polymesh/Mesh.hh>
#include <polymesh/detail/math.hh>
#include <polymesh/detail/unique_array.hh>
#include <polymesh/fields.hh>
namespace polymesh::detail
{
unique_array<pos3f> add_unit_dodecahedron_impl(Mesh& m);
}
namespace polymesh::objects
{
/// Adds a dodecahedron to the given mesh.
/// Note: This will not triangulate the mesh!
/// sf is called with (v, x, y, z), with vertex handle v and coordinates (x,y,z) on the unit sphere
template <class SphereF>
vertex_handle add_dodecahedron(Mesh& m, SphereF&& sf);
/// same as add_dodecahedron but directly fills a position attribute
template <class Pos3>
vertex_handle add_dodecahedron(Mesh& m, vertex_attribute<Pos3>& pos);
// ======== IMPLEMENTATION ========
template <class SphereF>
vertex_handle add_dodecahedron(Mesh& m, SphereF&& sf)
{
auto const last_vertex_count = m.all_vertices().size();
auto const positions = detail::add_unit_dodecahedron_impl(m);
for (auto i = 0; i < 20; ++i)
{
sf(vertex_index(last_vertex_count + i).of(m), positions[i].x, positions[i].y, positions[i].z);
}
return vertex_index(last_vertex_count).of(m);
}
template <class Pos3>
auto add_dodecahedron(Mesh& m, vertex_attribute<Pos3>& pos) -> vertex_handle
{
return add_dodecahedron(m, [&](vertex_handle v, float x, float y, float z) { pos[v] = field3<Pos3>::make_pos(x, y, z); });
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment