Lagrange
Loading...
Searching...
No Matches
fill_with_spheres.h
1/*
2 * Copyright 2021 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/Logger.h>
15
16// clang-format off
17#include <lagrange/utils/warnoff.h>
18#include <openvdb/openvdb.h>
19#include <openvdb/tools/VolumeToSpheres.h>
20#include <lagrange/utils/warnon.h>
21// clang-format on
22
23#include <Eigen/Dense>
24
25namespace lagrange {
26namespace volume {
27
39template <typename GridType, typename Derived>
40void fill_with_spheres(
41 const GridType& grid,
42 Eigen::PlainObjectBase<Derived>& spheres,
43 int max_spheres,
44 bool overlapping = false)
45{
46 openvdb::initialize();
47
48 using Scalar = typename Derived::Scalar;
49 using RowVector4s = Eigen::Matrix<float, 1, 4>;
50
51 if (max_spheres <= 0) {
52 logger().warn("Max spheres needs to be >= 1.");
53 max_spheres = 1;
54 }
55
56 const openvdb::Vec2i sphere_count(1, max_spheres);
57
58 std::vector<openvdb::Vec4s> points;
59 openvdb::tools::fillWithSpheres(grid, points, sphere_count, overlapping);
60
61 spheres.resize(points.size(), 4);
62
63 for (size_t i = 0; i < points.size(); ++i) {
64 const RowVector4s p(points[i].x(), points[i].y(), points[i].z(), points[i].w());
65 spheres.row(i) << p.template cast<Scalar>();
66 }
67}
68
69} // namespace volume
70} // namespace lagrange
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:40
@ Scalar
Mesh attribute must have exactly 1 channel.
Definition AttributeFwd.h:56
SurfaceMesh< ToScalar, ToIndex > cast(const SurfaceMesh< FromScalar, FromIndex > &source_mesh, const AttributeFilter &convertible_attributes={}, std::vector< std::string > *converted_attributes_names=nullptr)
Cast a mesh to a mesh of different scalar and/or index type.
Main namespace for Lagrange.