Unravel Engine C++ Reference
Loading...
Searching...
No Matches
bezier_mesh.hpp
Go to the documentation of this file.
1#ifndef GENERATOR_BEZIER_MESH_HPP
2#define GENERATOR_BEZIER_MESH_HPP
3
4#include <algorithm>
5#include <limits>
6
7#include "parametric_mesh.hpp"
8
9namespace generator
10{
11
16template<int D0, int D1>
18{
19private:
20 static_assert(D0 > 1, "D0 must be > 1.");
21 static_assert(D1 > 1, "D1 must be > 1.");
22
24 impl_t parametric_mesh_;
25
26 struct array_wrapper
27 {
28 gml::dvec3 data[D1][D0];
29
30 explicit array_wrapper(const gml::dvec3 (&p)[D1][D0])
31 {
32 std::copy(&p[0][0], &p[0][0] + D1 * D0, &data[0][0]);
33 }
34 };
35
36 explicit bezier_mesh_t(const array_wrapper& p, const gml::ivec2& segments)
37 : parametric_mesh_{[p](const gml::dvec2& t)
38 {
39 mesh_vertex_t vertex;
40
41 vertex.position = gml::bezier2(p.data, t);
42
43 gml::dmat2x3 J = gml::bezier2Jacobian<1>(p.data, t);
44 vertex.normal = gml::cross(J[0], J[1]);
45
46 // If the normal was zero try a again near by.
47 const double e = std::numeric_limits<double>::epsilon();
48 if(dot(vertex.normal, vertex.normal) < e)
49 {
50 J = gml::bezier2Jacobian<1>(p.data, t + 10.0 * e);
51 vertex.normal = gml::cross(J[0], J[1]);
52 }
53 vertex.normal = gml::normalize(vertex.normal);
54
55 vertex.tex_coord = t;
56
57 return vertex;
58 },
60 {
61 }
62
63public:
66 explicit bezier_mesh_t(const gml::dvec3 (&p)[D1][D0], const gml::ivec2& segments = {16, 16})
67 : // Work around a msvc lambda capture bug by wrapping the array.
68 bezier_mesh_t{array_wrapper{p}, segments}
69 {
70 }
71
73
74 triangles_t triangles() const noexcept
75 {
76 return parametric_mesh_.triangles();
77 }
78
80
81 vertices_t vertices() const noexcept
82 {
83 return parametric_mesh_.vertices();
84 }
85};
86} // namespace generator
87
88#endif
bezier_mesh_t(const gml::dvec3(&p)[D1][D0], const gml::ivec2 &segments={16, 16})
vertices_t vertices() const noexcept
typename impl_t::triangles_t triangles_t
triangles_t triangles() const noexcept
typename impl_t::vertices_t vertices_t
gml::dvec2 tex_coord
UV texture coordinates.
gml::dvec3 normal
Unit vector perpendicular to the surface.
A mesh with values evaluated using a callback function.
vertices_t vertices() const noexcept
triangles_t triangles() const noexcept
glm::tvec2< T > cross(const glm::tvec2< T > &v)
Definition math.hpp:108
glm::tvec3< T > bezier2(const glm::tvec3< T >(&p)[D1][D0], const glm::tvec2< T > &t)
Definition math.hpp:257
glm::tmat2x3< T > bezier2Jacobian(const glm::tvec3< T >(&p)[D1][D0], const glm::tvec2< T > &t)
Definition math.hpp:333
const segment_list * segments