Merge math_util and model_util and rename to filament_util.

Move filament_util to the main folder so as to indicate that
these utilities can be used by anyone and aren't internal to
the filament renderer implementation.

PiperOrigin-RevId: 911884375
Change-Id: Ieb58184263bdac7e44a07f1e44a4649c68c6bf77
This commit is contained in:
Haroon Qureshi
2026-05-07 04:19:55 -07:00
committed by Copybara-Service
parent 7ade42ca88
commit e1b27afcda
14 changed files with 67 additions and 103 deletions
+1 -3
View File
@@ -24,6 +24,7 @@ target_sources(${MUJOCO_FILAMENT_TARGET_NAME}
render_context_filament.h
render_context_filament.cc
render_context_filament_cpp.h
filament_util.h
filament/builtins.cc
filament/builtins.h
filament/color_grading_options.cc
@@ -37,11 +38,8 @@ target_sources(${MUJOCO_FILAMENT_TARGET_NAME}
filament/light.h
filament/material.cc
filament/material.h
filament/math_util.cc
filament/math_util.h
filament/mesh.cc
filament/mesh.h
filament/model_util.h
filament/object_manager.cc
filament/object_manager.h
filament/render_target.cc
@@ -18,17 +18,14 @@
#include <cstdint>
#include <memory>
#include <math/mathfwd.h>
#include <math/vec4.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mjvisualize.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/compat/imgui_bridge.h"
#include "experimental/filament/compat/scene_bridge.h"
#include "experimental/filament/filament/filament_context.h"
#include "experimental/filament/filament/model_util.h"
#include "experimental/filament/render_context_filament.h"
#include "experimental/filament/render_context_filament_cpp.h"
#include "experimental/filament/render_context_filament.h"
namespace mujoco {
@@ -29,10 +29,9 @@
#include <math/vec3.h>
#include <math/vec4.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament/math_util.h"
#include "experimental/filament/filament/model_util.h"
#include "experimental/filament/render_context_filament.h"
#include "experimental/filament/filament_util.h"
#include "experimental/filament/render_context_filament_cpp.h"
#include "experimental/filament/render_context_filament.h"
namespace mujoco {
@@ -23,16 +23,15 @@
#include <math/TMatHelpers.h>
#include <math/mat4.h>
#include <math/mathfwd.h>
#include <math/TVecHelpers.h>
#include <math/vec3.h>
#include <math/vec4.h>
#include <math/TVecHelpers.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/compat/model_objects.h"
#include "experimental/filament/compat/scene_geom_util.h"
#include "experimental/filament/filament/math_util.h"
#include "experimental/filament/filament/model_util.h"
#include "experimental/filament/render_context_filament.h"
#include "experimental/filament/filament_util.h"
#include "experimental/filament/render_context_filament_cpp.h"
#include "experimental/filament/render_context_filament.h"
namespace mujoco {
@@ -26,7 +26,7 @@
#include <math/vec2.h>
#include <math/vec3.h>
#include <math/vec4.h>
#include "experimental/filament/filament/math_util.h"
#include "experimental/filament/filament_util.h"
#include "experimental/filament/filament/mesh.h"
#include "experimental/filament/render_context_filament.h"
+1 -1
View File
@@ -25,8 +25,8 @@
#include <utils/Entity.h>
#include <utils/EntityManager.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament_util.h"
#include "experimental/filament/filament/filament_context.h"
#include "experimental/filament/filament/math_util.h"
#include "experimental/filament/filament/texture.h"
#include "experimental/filament/render_context_filament.h"
@@ -20,9 +20,9 @@
#include <filament/RenderableManager.h>
#include <filament/TextureSampler.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament/math_util.h"
#include "experimental/filament/filament/texture.h"
#include "experimental/filament/filament_util.h"
#include "experimental/filament/filament/object_manager.h"
#include "experimental/filament/filament/texture.h"
#include "experimental/filament/render_context_filament.h"
namespace mujoco {
+1 -1
View File
@@ -32,7 +32,7 @@
#include <math/vec3.h>
#include <math/vec4.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament/math_util.h"
#include "experimental/filament/filament_util.h"
#include "experimental/filament/render_context_filament.h"
namespace mujoco {
@@ -1,75 +0,0 @@
// Copyright 2025 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_MODEL_UTIL_H_
#define MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_MODEL_UTIL_H_
#include <string_view>
#include <math/vec2.h>
#include <math/vec3.h>
#include <math/vec4.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mujoco.h>
namespace mujoco {
// Reads a value with the given name from the mjModel's data sections. The
// default_value is returned if the named element is not found.
template <typename T>
T ReadElement(const mjModel* model, const char* name, T default_value = T()) {
constexpr bool is_string =
std::is_same_v<T, const char*> || std::is_same_v<T, std::string_view>;
const int type = is_string ? mjOBJ_TEXT : mjOBJ_NUMERIC;
const int id = mj_name2id(model, type, name);
if (id < 0) {
return default_value;
}
if constexpr (std::is_same_v<T, const char*>) {
const char* ptr = model->text_data + model->text_adr[id];
return ptr;
} else if constexpr (std::is_same_v<T, std::string_view>) {
const char* ptr = model->text_data + model->text_adr[id];
// Do not include the null terminator in the string view.
return std::string_view(ptr, model->text_size[id] - 1);
} else if constexpr (std::is_arithmetic_v<T>) {
const mjtNum* ptr = model->numeric_data + model->numeric_adr[id];
return static_cast<T>(*ptr);
} else if constexpr (std::is_enum_v<T>) {
const mjtNum* ptr = model->numeric_data + model->numeric_adr[id];
return static_cast<T>(static_cast<int>(*ptr));
} else if constexpr (std::is_same_v<T, filament::math::float2>) {
const mjtNum* ptr = model->numeric_data + model->numeric_adr[id];
if (model->numeric_size[id] != 2) mju_error("Invalid numeric size.");
return T{ptr[0], ptr[1]};
} else if constexpr (std::is_same_v<T, filament::math::float3>) {
const mjtNum* ptr = model->numeric_data + model->numeric_adr[id];
if (model->numeric_size[id] != 3) mju_error("Invalid numeric size.");
return T{ptr[0], ptr[1], ptr[2]};
} else if constexpr (std::is_same_v<T, filament::math::float4>) {
const mjtNum* ptr = model->numeric_data + model->numeric_adr[id];
if (model->numeric_size[id] != 4) mju_error("Invalid numeric size.");
return T{ptr[0], ptr[1], ptr[2], ptr[3]};
} else if constexpr (std::is_same_v<T, bool>) {
const mjtNum* ptr = model->numeric_data + model->numeric_adr[id];
return static_cast<T>(*ptr != 0);
}
return default_value;
}
} // namespace mujoco
#endif // MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_MODEL_UTIL_H_
@@ -29,10 +29,10 @@
#include <math/vec4.h>
#include <utils/EntityManager.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament_util.h"
#include "experimental/filament/filament/builtins.h"
#include "experimental/filament/filament/filament_context.h"
#include "experimental/filament/filament/material.h"
#include "experimental/filament/filament/math_util.h"
#include "experimental/filament/filament/mesh.h"
#include "experimental/filament/filament/object_manager.h"
#include "experimental/filament/filament/texture.h"
@@ -24,8 +24,8 @@
#include <math/mat4.h>
#include <utils/Entity.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament_util.h"
#include "experimental/filament/filament/filament_context.h"
#include "experimental/filament/filament/math_util.h"
#include "experimental/filament/filament/mesh.h"
#include "experimental/filament/filament/object_manager.h"
#include "experimental/filament/render_context_filament.h"
@@ -35,16 +35,15 @@
#include <math/mat4.h>
#include <math/mathfwd.h>
#include <math/scalar.h>
#include <math/TVecHelpers.h>
#include <math/vec3.h>
#include <math/vec4.h>
#include <math/TVecHelpers.h>
#include <utils/EntityManager.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament_util.h"
#include "experimental/filament/filament/color_grading_options.h"
#include "experimental/filament/filament/filament_context.h"
#include "experimental/filament/filament/light.h"
#include "experimental/filament/filament/math_util.h"
#include "experimental/filament/filament/model_util.h"
#include "experimental/filament/filament/render_target.h"
#include "experimental/filament/filament/renderable.h"
#include "experimental/filament/filament/texture.h"
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "experimental/filament/filament/math_util.h"
#include "experimental/filament/filament_util.h"
#include <limits>
#include <math/mat3.h>
@@ -12,14 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_MATH_UTIL_H_
#define MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_MATH_UTIL_H_
#ifndef MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_UTIL_H_
#define MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_UTIL_H_
#include <math/mat3.h>
#include <math/mat4.h>
#include <math/vec2.h>
#include <math/vec3.h>
#include <math/vec4.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mujoco.h>
namespace mujoco {
@@ -96,6 +98,51 @@ filament::math::float4 CalculateOrientation(
const filament::math::float3& p2,
const filament::math::float3& p3);
// Reads a value with the given name from the mjModel's data sections. The
// default_value is returned if the named element is not found.
template <typename T>
T ReadElement(const mjModel* model, const char* name, T default_value = T()) {
constexpr bool is_string =
std::is_same_v<T, const char*> || std::is_same_v<T, std::string_view>;
const int type = is_string ? mjOBJ_TEXT : mjOBJ_NUMERIC;
const int id = mj_name2id(model, type, name);
if (id < 0) {
return default_value;
}
if constexpr (std::is_same_v<T, const char*>) {
const char* ptr = model->text_data + model->text_adr[id];
return ptr;
} else if constexpr (std::is_same_v<T, std::string_view>) {
const char* ptr = model->text_data + model->text_adr[id];
// Do not include the null terminator in the string view.
return std::string_view(ptr, model->text_size[id] - 1);
} else if constexpr (std::is_arithmetic_v<T>) {
const mjtNum* ptr = model->numeric_data + model->numeric_adr[id];
return static_cast<T>(*ptr);
} else if constexpr (std::is_enum_v<T>) {
const mjtNum* ptr = model->numeric_data + model->numeric_adr[id];
return static_cast<T>(static_cast<int>(*ptr));
} else if constexpr (std::is_same_v<T, filament::math::float2>) {
const mjtNum* ptr = model->numeric_data + model->numeric_adr[id];
if (model->numeric_size[id] != 2) mju_error("Invalid numeric size.");
return T{ptr[0], ptr[1]};
} else if constexpr (std::is_same_v<T, filament::math::float3>) {
const mjtNum* ptr = model->numeric_data + model->numeric_adr[id];
if (model->numeric_size[id] != 3) mju_error("Invalid numeric size.");
return T{ptr[0], ptr[1], ptr[2]};
} else if constexpr (std::is_same_v<T, filament::math::float4>) {
const mjtNum* ptr = model->numeric_data + model->numeric_adr[id];
if (model->numeric_size[id] != 4) mju_error("Invalid numeric size.");
return T{ptr[0], ptr[1], ptr[2], ptr[3]};
} else if constexpr (std::is_same_v<T, bool>) {
const mjtNum* ptr = model->numeric_data + model->numeric_adr[id];
return static_cast<T>(*ptr != 0);
}
return default_value;
}
} // namespace mujoco
#endif // MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_MATH_UTIL_H_
#endif // MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_UTIL_H_