Lagrange
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
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#include <openvdb/openvdb.h>
17#include <openvdb/tools/VolumeToSpheres.h>
18
19#include <Eigen/Dense>
20
21namespace lagrange {
22namespace volume {
23
35template <typename GridType, typename Derived>
36void fill_with_spheres(
37 const GridType& grid,
38 Eigen::PlainObjectBase<Derived>& spheres,
39 int max_spheres,
40 bool overlapping = false)
41{
42 openvdb::initialize();
43
44 using Scalar = typename Derived::Scalar;
45 using RowVector4s = Eigen::Matrix<float, 1, 4>;
46
47 if (max_spheres <= 0) {
48 logger().warn("Max spheres needs to be >= 1.");
49 max_spheres = 1;
50 }
51
52 const openvdb::Vec2i sphere_count(1, max_spheres);
53
54 std::vector<openvdb::Vec4s> points;
55 openvdb::tools::fillWithSpheres(grid, points, sphere_count, overlapping);
56
57 spheres.resize(points.size(), 4);
58
59 for (size_t i = 0; i < points.size(); ++i) {
60 const RowVector4s p(points[i].x(), points[i].y(), points[i].z(), points[i].w());
61 spheres.row(i) << p.template cast<Scalar>();
62 }
63}
64
65} // namespace volume
66} // namespace lagrange
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:40
Main namespace for Lagrange.
Definition: AABBIGL.h:30