diff --git a/src/experimental/filament/CMakeLists.txt b/src/experimental/filament/CMakeLists.txt index ca8f3f07..7e972270 100644 --- a/src/experimental/filament/CMakeLists.txt +++ b/src/experimental/filament/CMakeLists.txt @@ -61,8 +61,6 @@ target_sources(${MUJOCO_FILAMENT_TARGET_NAME} filament/scene_view.h filament/texture.cc filament/texture.h - filament/vertex_util.cc - filament/vertex_util.h ) if(MUJOCO_USE_FILAMENT_MJR_COMPAT) target_sources(${MUJOCO_FILAMENT_TARGET_NAME} diff --git a/src/experimental/filament/filament/builtins.cc b/src/experimental/filament/filament/builtins.cc index 40b727af..5ee489b3 100644 --- a/src/experimental/filament/filament/builtins.cc +++ b/src/experimental/filament/filament/builtins.cc @@ -27,8 +27,8 @@ #include #include #include +#include "experimental/filament/filament/math_util.h" #include "experimental/filament/filament/mesh.h" -#include "experimental/filament/filament/vertex_util.h" namespace mujoco { diff --git a/src/experimental/filament/filament/math_util.cc b/src/experimental/filament/filament/math_util.cc index 4449b16c..34e0cb24 100644 --- a/src/experimental/filament/filament/math_util.cc +++ b/src/experimental/filament/filament/math_util.cc @@ -13,8 +13,11 @@ // limitations under the License. #include "experimental/filament/filament/math_util.h" +#include +#include #include +#include #include #include #include @@ -23,7 +26,9 @@ namespace mujoco { using filament::math::float3; using filament::math::float4; +using filament::math::mat3f; using filament::math::mat4; +using filament::math::quatf; mat4 ToReflectionMatrix(const mat4& xform) { const float3 normal = xform[2].xyz; @@ -81,4 +86,38 @@ mat4 CalculateObliqueProjection(const mat4& projection, const float4& plane) { return res; } + +float4 CalculateOrientation(const float3& normal) { + float3 tangent; + float3 bitangent; + if (normal.y < -1.0f + std::numeric_limits::epsilon()) { + // Handle the singularity. + tangent = float3{-1.0f, 0.0f, 0.0f}; + bitangent = float3{0.0f, 0.0f, -1.0f}; + } else { + const float a = 1.0f / (1.0f + normal.y); + const float b = -normal.z * normal.x * a; + tangent = float3(b, -normal.z, 1.0f - normal.z * normal.z * a); + bitangent = float3(1.0f - normal.x * normal.x * a, -normal.x, b); + } + quatf orientation = mat3f::packTangentFrame({tangent, bitangent, normal}); + return float4(orientation.xyz, orientation.w); +} + +float3 CalculateNormal( + const filament::math::float3& p1, + const filament::math::float3& p2, + const filament::math::float3& p3) { + const float3 v12 = p2 - p1; + const float3 v13 = p3 - p1; + return normalize(cross(v12, v13)); +} + +float4 CalculateOrientation( + const filament::math::float3& p1, + const filament::math::float3& p2, + const filament::math::float3& p3) { + return CalculateOrientation(CalculateNormal(p1, p2, p3)); +} + } // namespace mujoco diff --git a/src/experimental/filament/filament/math_util.h b/src/experimental/filament/filament/math_util.h index 61473cec..ee3b2741 100644 --- a/src/experimental/filament/filament/math_util.h +++ b/src/experimental/filament/filament/math_util.h @@ -64,6 +64,22 @@ filament::math::mat4 CalculateObliqueProjection( const filament::math::mat4& projection, const filament::math::float4& plane); +// Calculates the normal of a triangle given its three vertices. +filament::math::float3 CalculateNormal( + const filament::math::float3& p1, + const filament::math::float3& p2, + const filament::math::float3& p3); + +// Calculates the orientation of a vertex given just its normal. +filament::math::float4 CalculateOrientation( + const filament::math::float3& normal); + +// Calculates the orientation of a triangle given its three vertices. +filament::math::float4 CalculateOrientation( + const filament::math::float3& p1, + const filament::math::float3& p2, + const filament::math::float3& p3); + } // namespace mujoco #endif // MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_MATH_UTIL_H_ diff --git a/src/experimental/filament/filament/mesh.cc b/src/experimental/filament/filament/mesh.cc index bfdad8be..8acc2d59 100644 --- a/src/experimental/filament/filament/mesh.cc +++ b/src/experimental/filament/filament/mesh.cc @@ -30,7 +30,6 @@ #include #include #include "experimental/filament/filament/math_util.h" -#include "experimental/filament/filament/vertex_util.h" namespace mujoco { diff --git a/src/experimental/filament/filament/model_util.cc b/src/experimental/filament/filament/model_util.cc index e1309bc1..9a4d9e21 100644 --- a/src/experimental/filament/filament/model_util.cc +++ b/src/experimental/filament/filament/model_util.cc @@ -28,7 +28,6 @@ #include #include "experimental/filament/filament/math_util.h" #include "experimental/filament/filament/mesh.h" -#include "experimental/filament/filament/vertex_util.h" namespace mujoco { diff --git a/src/experimental/filament/filament/vertex_util.cc b/src/experimental/filament/filament/vertex_util.cc deleted file mode 100644 index 3304921d..00000000 --- a/src/experimental/filament/filament/vertex_util.cc +++ /dev/null @@ -1,65 +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. - -#include "experimental/filament/filament/vertex_util.h" - -#include - -#include -#include -#include -#include -#include - -namespace mujoco { - -using filament::math::float3; -using filament::math::float4; -using filament::math::mat3f; -using filament::math::quatf; - -float4 CalculateOrientation(const float3& normal) { - float3 tangent; - float3 bitangent; - if (normal.y < -1.0f + std::numeric_limits::epsilon()) { - // Handle the singularity. - tangent = float3{-1.0f, 0.0f, 0.0f}; - bitangent = float3{0.0f, 0.0f, -1.0f}; - } else { - const float a = 1.0f / (1.0f + normal.y); - const float b = -normal.z * normal.x * a; - tangent = float3(b, -normal.z, 1.0f - normal.z * normal.z * a); - bitangent = float3(1.0f - normal.x * normal.x * a, -normal.x, b); - } - quatf orientation = mat3f::packTangentFrame({tangent, bitangent, normal}); - return float4(orientation.xyz, orientation.w); -} - -float3 CalculateNormal( - const filament::math::float3& p1, - const filament::math::float3& p2, - const filament::math::float3& p3) { - const float3 v12 = p2 - p1; - const float3 v13 = p3 - p1; - return normalize(cross(v12, v13)); -} - -float4 CalculateOrientation( - const filament::math::float3& p1, - const filament::math::float3& p2, - const filament::math::float3& p3) { - return CalculateOrientation(CalculateNormal(p1, p2, p3)); -} - -} // namespace mujoco diff --git a/src/experimental/filament/filament/vertex_util.h b/src/experimental/filament/filament/vertex_util.h deleted file mode 100644 index e8bd7919..00000000 --- a/src/experimental/filament/filament/vertex_util.h +++ /dev/null @@ -1,41 +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_VERTEX_UTIL_H_ -#define MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_VERTEX_UTIL_H_ - -#include -#include - -namespace mujoco { - -// Calculates the normal of a triangle given its three vertices. -filament::math::float3 CalculateNormal( - const filament::math::float3& p1, - const filament::math::float3& p2, - const filament::math::float3& p3); - -// Calculates the orientation of a vertex given just its normal. -filament::math::float4 CalculateOrientation( - const filament::math::float3& normal); - -// Calculates the orientation of a triangle given its three vertices. -filament::math::float4 CalculateOrientation( - const filament::math::float3& p1, - const filament::math::float3& p2, - const filament::math::float3& p3); - -} // namespace mujoco - -#endif // MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_VERTEX_UTIL_H_