diff --git a/src/gmio_core/internal/vecgeom_utils.h b/src/gmio_core/internal/vecgeom_utils.h index 99f59e0..f556723 100644 --- a/src/gmio_core/internal/vecgeom_utils.h +++ b/src/gmio_core/internal/vecgeom_utils.h @@ -18,18 +18,29 @@ #include "../global.h" -/*! Cross product of float32 vectors (ux,uy,uz) and (vx,vy,vz) */ +/*! Computes in-place cross product of float32 coords (ux,uy,uz) ^ (vx,vy,vz) */ GMIO_INLINE void gmio_cross_product_f32( float ux, float uy, float uz, float vx, float vy, float vz, float* nx, float* ny, float* nz); -/*! Cross product of float64 vectors (ux,uy,uz) and (vx,vy,vz) */ +/*! Computes in-place cross product of float64 coords (ux,uy,uz) ^ (vx,vy,vz) */ GMIO_INLINE void gmio_cross_product_f64( double ux, double uy, double uz, double vx, double vy, double vz, double* nx, double* ny, double* nz); +/*! Returns the squared length of float32 vector (x, y, z) */ +GMIO_INLINE float gmio_sqr_length_f32(float x, float y, float z); + +/*! Returns the squared length of float64 vector (x, y, z) */ +GMIO_INLINE double gmio_sqr_length_f64(double x, double y, double z); + +/*! Normalizes in-place the float32 (x,y,z) coords */ +GMIO_INLINE void gmio_normalize_f32(float* x, float* y, float* z); + +/*! Normalizes in-place the float64 (x,y,z) coords */ +GMIO_INLINE void gmio_normalize_f64(double* x, double* y, double* z); /* * Implementation @@ -45,14 +56,7 @@ void gmio_cross_product_f32( *nx = uy*vz - uz*vx; *ny = uz*vx - ux*vz; *nz = ux*vy - uy*vx; - { - const float d = (float)sqrt((*nx)*(*nx) + (*ny)*(*ny) + (*nz)*(*nz)); - if (d > 0.f) { - *nx /= d; - *ny /= d; - *nz /= d; - } - } + gmio_normalize_f32(nx, ny, nz); } void gmio_cross_product_f64( @@ -63,13 +67,32 @@ void gmio_cross_product_f64( *nx = uy*vz - uz*vx; *ny = uz*vx - ux*vz; *nz = ux*vy - uy*vx; - { - const double d = sqrt((*nx)*(*nx) + (*ny)*(*ny) + (*nz)*(*nz)); - if (d > 0.) { - *nx /= d; - *ny /= d; - *nz /= d; - } + gmio_normalize_f64(nx, ny, nz); +} + +float gmio_sqr_length_f32(float x, float y, float z) +{ return x*x + y*y + z*z; } + +double gmio_sqr_length_f64(double x, double y, double z) +{ return x*x + y*y + z*z; } + +void gmio_normalize_f32(float* x, float* y, float* z) +{ + const float d = (float)sqrt(gmio_sqr_length_f32(*x, *y, *z)); + if (d > 0.f) { + *x /= d; + *y /= d; + *z /= d; + } +} + +void gmio_normalize_f64(double* x, double* y, double* z) +{ + const double d = sqrt(gmio_sqr_length_f64(*x, *y, *z)); + if (d > 0.) { + *x /= d; + *y /= d; + *z /= d; } } diff --git a/src/gmio_stl/stl_format.c b/src/gmio_stl/stl_format.c index 565a1fd..fd8e66e 100644 --- a/src/gmio_stl/stl_format.c +++ b/src/gmio_stl/stl_format.c @@ -18,6 +18,7 @@ #include "stl_triangle.h" #include "stlb_header.h" #include "internal/stlb_byte_swap.h" +#include "internal/stlb_infos_get.h" #include "../gmio_core/endian.h" #include "../gmio_core/internal/byte_codec.h" @@ -26,6 +27,7 @@ #include "../gmio_core/internal/min_max.h" #include "../gmio_core/internal/numeric_utils.h" #include "../gmio_core/internal/string_ascii_utils.h" +#include "../gmio_core/internal/vecgeom_utils.h" #include @@ -33,15 +35,7 @@ enum { GMIO_FIXED_BUFFER_SIZE = 1024 }; GMIO_INLINE float gmio_sqrlen(const struct gmio_stl_coords* c) { - const float cx = c->x; - const float cy = c->y; - const float cz = c->z; - return cx*cx + cy*cy + cz*cz; -} - -GMIO_INLINE gmio_streamsize_t gmio_stlb_streamsize(uint32_t facet_count) -{ - return GMIO_STLB_HEADER_SIZE + 4 + facet_count*GMIO_STLB_TRIANGLE_RAWSIZE; + return gmio_sqr_length_f32(c->x, c->y, c->z); } /* Does \p str contains token ? */ @@ -64,9 +58,9 @@ static enum gmio_stl_format gmio_stlb_format( /* Assume the stream contains one solid */ { const gmio_streamsize_t stream_size = gmio_stream_size(stream); - if (gmio_stlb_streamsize(le_facet_count) == stream_size) + if (gmio_stlb_infos_size(le_facet_count) == stream_size) return GMIO_STL_FORMAT_BINARY_LE; - if (gmio_stlb_streamsize(be_facet_count) == stream_size) + if (gmio_stlb_infos_size(be_facet_count) == stream_size) return GMIO_STL_FORMAT_BINARY_BE; }