Lagrange
Loading...
Searching...
No Matches
generate_hexahedron.h
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13#include <lagrange/Mesh.h>
14#include <lagrange/create_mesh.h>
15#include <lagrange/legacy/inline.h>
16#include <lagrange/primitive/generation_utils.h>
17#include <lagrange/subdivision/mesh_subdivision.h>
18
19
20namespace lagrange {
21namespace primitive {
22LAGRANGE_LEGACY_INLINE
23namespace legacy {
24
25namespace Hexahedron {
26
27template <typename Scalar>
28constexpr Scalar TOLERANCE = safe_cast<Scalar>(1e-6);
29
30template <typename MeshType>
31typename MeshType::VertexArray generate_vertices(
32 const typename MeshType::Scalar width,
33 const typename MeshType::Scalar height,
34 const typename MeshType::Scalar depth);
35
36template <typename MeshType>
37typename MeshType::FacetArray generate_facets();
38
39} // namespace Hexahedron
40
41template <typename MeshType>
42std::unique_ptr<MeshType> generate_hexahedron(
43 typename MeshType::Scalar radius,
44 const Eigen::Matrix<typename MeshType::Scalar, 3, 1>& center = {0, 0, 0})
45{
46 using namespace Hexahedron;
47
48 using VertexArray = typename MeshType::VertexArray;
49 using FacetArray = typename MeshType::FacetArray;
50 using Scalar = typename MeshType::Scalar;
51 using UVArray = typename MeshType::UVArray;
52 using UVIndices = typename MeshType::UVIndices;
53
54 la_runtime_assert(radius >= 0.0, "Invalid radius: " + std::to_string(radius));
55
56 Scalar length = 2.0 / sqrt(3) * radius;
57 VertexArray vertices = generate_vertices<MeshType>(length, length, length);
58 FacetArray facets = generate_facets<MeshType>();
59
60 // Translate to center
61 if (center.squaredNorm() > TOLERANCE<Scalar>) {
62 vertices.rowwise() += center.transpose();
63 }
64
65 auto mesh = create_mesh(vertices, facets);
66 UVArray uvs(14, 2);
67 uvs << 0.0, 0.25, 0.25, 0.25, 0.5, 0.25, 0.75, 0.25, 1.0, 0.25, 0.0, 0.5, 0.25, 0.5, 0.5, 0.5,
68 0.75, 0.5, 1.0, 0.5, 0.25, 0.0, 0.5, 0.0, 0.25, 0.75, 0.5, 0.75;
69
70 UVIndices uv_indices(6, 4);
71 uv_indices << 10, 11, 2, 1, 12, 6, 7, 13, 1, 2, 7, 6, 3, 4, 9, 8, 0, 1, 6, 5, 2, 3, 8, 7;
72 mesh->initialize_uv(uvs, uv_indices);
73
74 // Set uniform semantic label
75 lagrange::set_uniform_semantic_label(*mesh, PrimitiveSemanticLabel::SIDE);
76
77 return mesh;
78}
79
80namespace Hexahedron {
81
82template <typename MeshType>
83typename MeshType::VertexArray generate_vertices(
84 const typename MeshType::Scalar width,
85 const typename MeshType::Scalar height,
86 const typename MeshType::Scalar depth)
87{
88 typename MeshType::VertexArray vertices(8, 3);
89
90 // Bottom
91 vertices.row(0) << -width, -height, depth;
92 vertices.row(1) << -width, -height, -depth;
93 vertices.row(2) << width, -height, -depth;
94 vertices.row(3) << width, -height, depth;
95
96 // Top
97 vertices.row(4) << -width, height, depth;
98 vertices.row(5) << width, height, depth;
99 vertices.row(6) << width, height, -depth;
100 vertices.row(7) << -width, height, -depth;
101
102 return vertices / 2;
103}
104
105template <typename MeshType>
106typename MeshType::FacetArray generate_facets()
107{
108 typename MeshType::FacetArray facets(6, 4);
109
110 facets <<
111 // bottom
112 0,
113 1, 2, 3,
114 // top
115 4, 5, 6, 7,
116 // front
117 3, 2, 6, 5,
118 // back
119 1, 0, 4, 7,
120 // left
121 0, 3, 5, 4,
122 // right
123 2, 1, 7, 6;
124
125 return facets;
126}
127} // namespace Hexahedron
128} // namespace legacy
129} // namespace primitive
130} // namespace lagrange
@ Scalar
Mesh attribute must have exactly 1 channel.
Definition AttributeFwd.h:56
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:174
constexpr auto safe_cast(SourceType value) -> std::enable_if_t<!std::is_same< SourceType, TargetType >::value, TargetType >
Perform safe cast from SourceType to TargetType, where "safe" means:
Definition safe_cast.h:50
Main namespace for Lagrange.
auto create_mesh(const Eigen::MatrixBase< DerivedV > &vertices, const Eigen::MatrixBase< DerivedF > &facets)
This function create a new mesh given the vertex and facet arrays by copying data into the Mesh objec...
Definition create_mesh.h:39