Lagrange
normalize_meshes.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
14#include <lagrange/Mesh.h>
15#include <lagrange/MeshTrait.h>
16#include <lagrange/legacy/inline.h>
17
18namespace lagrange {
19LAGRANGE_LEGACY_INLINE
20namespace legacy {
21
29template <typename MeshTypePtr>
30void normalize_meshes(const std::vector<MeshTypePtr>& meshes)
31{
32 static_assert(MeshTrait<MeshTypePtr>::is_mesh_ptr(), "Input type is not a Mesh pointer type");
33 using MeshType = typename std::pointer_traits<MeshTypePtr>::element_type;
34 using Scalar = typename MeshType::Scalar;
35 using VertexArray = typename MeshType::VertexArray;
36 using VectorType = Eigen::Matrix<Scalar, 1, VertexArray::ColsAtCompileTime>;
37
38 if (meshes.empty()) {
39 return;
40 }
41
42 const auto DIM = meshes.front()->get_vertices().cols();
43
44 VectorType min_pos = VectorType::Constant(DIM, std::numeric_limits<Scalar>::max());
45 VectorType max_pos = VectorType::Constant(DIM, std::numeric_limits<Scalar>::lowest());
46
47 for (const auto& mesh : meshes) {
48 const auto& V = mesh->get_vertices();
49 min_pos = min_pos.cwiseMin(V.colwise().minCoeff());
50 max_pos = max_pos.cwiseMax(V.colwise().maxCoeff());
51 }
52
53 const Scalar scaling = Scalar(1) / (max_pos - min_pos).maxCoeff();
54 const auto origin = Scalar(0.5) * (min_pos + max_pos);
55
56 for (auto& mesh : meshes) {
57 VertexArray vertices;
58 mesh->export_vertices(vertices);
59 vertices = (vertices.array().rowwise() - origin.array()) * scaling;
60 mesh->import_vertices(vertices);
61 }
62}
63
64template <typename MeshType>
65void normalize_mesh(MeshType& mesh)
66{
67 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
68 std::vector<MeshType*> vec = {&mesh};
70}
71
72} // namespace legacy
73} // namespace lagrange
Definition: Mesh.h:48
void normalize_meshes(span< SurfaceMesh< Scalar, Index > * > meshes)
Normalize a list of meshes to fit in a unit box centered at the origin.
Definition: normalize_meshes.cpp:39
void normalize_mesh(SurfaceMesh< Scalar, Index > &mesh)
Normalize a mesh to fit in a unit box centered at the origin.
Definition: normalize_meshes.cpp:23
Main namespace for Lagrange.
Definition: AABBIGL.h:30