88#include " omath/linear_algebra/triangle.hpp"
99#include " omath/linear_algebra/vector3.hpp"
1010#include " omath/projection/error_codes.hpp"
11+ #include < cmath>
1112#include < expected>
1213#include < omath/trigonometry/angle.hpp>
1314#include < type_traits>
@@ -229,10 +230,12 @@ namespace omath::projection
229230 auto projected = get_view_projection_matrix ()
230231 * mat_column_from_vector<float , Mat4X4Type::get_store_ordering ()>(world_position);
231232
232- if (projected.at (3 , 0 ) == 0 .0f )
233+ constexpr float w_epsilon = 1e-6f ;
234+ const auto w = projected.at (3 , 0 );
235+ if (std::abs (w) < w_epsilon)
233236 return std::unexpected (Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
234237
235- projected /= projected. at ( 3 , 0 ) ;
238+ projected /= w ;
236239
237240 if (is_ndc_out_of_bounds (projected))
238241 return std::unexpected (Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
@@ -250,10 +253,12 @@ namespace omath::projection
250253 auto inverted_projection =
251254 inv_view_proj.value () * mat_column_from_vector<float , Mat4X4Type::get_store_ordering ()>(ndc);
252255
253- if (!inverted_projection.at (3 , 0 ))
256+ constexpr float w_epsilon = 1e-6f ;
257+ const auto w = inverted_projection.at (3 , 0 );
258+ if (std::abs (w) < w_epsilon)
254259 return std::unexpected (Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
255260
256- inverted_projection /= inverted_projection. at ( 3 , 0 ) ;
261+ inverted_projection /= w ;
257262
258263 return Vector3<float >{inverted_projection.at (0 , 0 ), inverted_projection.at (1 , 0 ),
259264 inverted_projection.at (2 , 0 )};
0 commit comments