From 2f873e3089ed28f6e9e9c2962e70d667268c1af1 Mon Sep 17 00:00:00 2001 From: Hugues Delorme Date: Mon, 14 Mar 2016 16:31:48 +0100 Subject: [PATCH] Rename gmio_vec3 structs *gmio_vec3_f32 -> gmio_vec3f *gmio_vec3_f64 -> gmio_vec3d --- benchmarks/benchmark_assimp/main.cpp | 16 ++-- examples/stl_read_file.c | 2 +- examples/stl_write_file.c | 2 +- src/gmio_core/internal/vecgeom_utils.h | 110 ++++++++++++++----------- src/gmio_core/vecgeom.h | 8 +- src/gmio_stl/internal/stla_write.c | 2 +- src/gmio_stl/stl_constants.h | 2 +- src/gmio_stl/stl_format.c | 13 ++- src/gmio_stl/stl_triangle.c | 13 ++- src/gmio_stl/stl_triangle.h | 8 +- src/gmio_stl/stla_read.c | 4 +- src/gmio_support/stl_occ.cpp | 12 +-- tests/core_utils.c | 6 +- tests/core_utils.h | 6 +- tests/stl_utils.c | 8 +- tests/test_stl_triangle.c | 8 +- 16 files changed, 114 insertions(+), 106 deletions(-) diff --git a/benchmarks/benchmark_assimp/main.cpp b/benchmarks/benchmark_assimp/main.cpp index 8ba056f..edcce5a 100644 --- a/benchmarks/benchmark_assimp/main.cpp +++ b/benchmarks/benchmark_assimp/main.cpp @@ -43,16 +43,16 @@ static unsigned totalTriangleCount(const aiScene* scene) return meshnum; } -GMIO_INLINE void copy_gmio_vec3_f32( - aiVector3D* vec3, const gmio_vec3_f32& coords) +GMIO_INLINE void copy_gmio_vec3f( + aiVector3D* vec3, const gmio_vec3f& coords) { *vec3 = *((aiVector3D*)&coords); } GMIO_INLINE void copy_aiVector3D( - gmio_vec3_f32* coords, const aiVector3D& vec3) + gmio_vec3f* coords, const aiVector3D& vec3) { - *coords = *((gmio_vec3_f32*)&vec3); + *coords = *((gmio_vec3f*)&vec3); } namespace BmkAssimp { @@ -198,13 +198,13 @@ static void add_triangle( aiVector3D* vp = &pMesh->mVertices[tri_id * 3]; aiVector3D* vn = &pMesh->mNormals[tri_id * 3]; - copy_gmio_vec3_f32(vn, triangle->n); + copy_gmio_vec3f(vn, triangle->n); *(vn+1) = *vn; *(vn+2) = *vn; - copy_gmio_vec3_f32(vp, triangle->v1); - copy_gmio_vec3_f32(vp+1, triangle->v2); - copy_gmio_vec3_f32(vp+2, triangle->v3); + copy_gmio_vec3f(vp, triangle->v1); + copy_gmio_vec3f(vp+1, triangle->v2); + copy_gmio_vec3f(vp+2, triangle->v3); if (helper->hasToCountTriangle) ++(helper->totalTriangleCount); diff --git a/examples/stl_read_file.c b/examples/stl_read_file.c index c27ffb6..652d80b 100644 --- a/examples/stl_read_file.c +++ b/examples/stl_read_file.c @@ -66,7 +66,7 @@ static void my_3d_mesh__copy_triangle( { /* Copy new triangle */ struct my_3d_triangle* my_tri = &my_mesh->triangle_array[triangle_id]; - const struct gmio_vec3_f32* tri_vertices = &triangle->v1; + const struct gmio_vec3f* tri_vertices = &triangle->v1; int i; for (i = 0; i < 3; ++i) { my_tri->vertex[i].coords[0] = tri_vertices[i].x; diff --git a/examples/stl_write_file.c b/examples/stl_write_file.c index a79404b..0dd4a1c 100644 --- a/examples/stl_write_file.c +++ b/examples/stl_write_file.c @@ -25,7 +25,7 @@ static void my_3d_mesh__get_triangle( { const struct my_3d_mesh* my_mesh = (const struct my_3d_mesh*)cookie; const struct my_3d_triangle* my_tri = &my_mesh->triangle_array[tri_id]; - struct gmio_vec3_f32* tri_vertices = &triangle->v1; + struct gmio_vec3f* tri_vertices = &triangle->v1; int i; for (i = 0; i < 3; ++i) { tri_vertices[i].x = (float)my_tri->vertex[i].coords[0]; diff --git a/src/gmio_core/internal/vecgeom_utils.h b/src/gmio_core/internal/vecgeom_utils.h index f556723..11bfc3c 100644 --- a/src/gmio_core/internal/vecgeom_utils.h +++ b/src/gmio_core/internal/vecgeom_utils.h @@ -18,29 +18,29 @@ #include "../global.h" -/*! 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); +/*! Computes in-place cross product of two gmio_vec3f objects */ +GMIO_INLINE void gmio_vec3f_cross_product( + const struct gmio_vec3f* u, + const struct gmio_vec3f* v, + struct gmio_vec3f* n); -/*! 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); +/*! Computes in-place cross product of two gmio_vec3d objects */ +GMIO_INLINE void gmio_vec3d_cross_product( + const struct gmio_vec3d* u, + const struct gmio_vec3d* v, + struct gmio_vec3d* n); -/*! 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 a gmio_vec3f object */ +GMIO_INLINE float gmio_vec3f_sqr_length(const struct gmio_vec3f* v); -/*! Returns the squared length of float64 vector (x, y, z) */ -GMIO_INLINE double gmio_sqr_length_f64(double x, double y, double z); +/*! Returns the squared length of a gmio_vec3d object */ +GMIO_INLINE double gmio_vec3d_sqr_length(const struct gmio_vec3d* v); -/*! Normalizes in-place the float32 (x,y,z) coords */ -GMIO_INLINE void gmio_normalize_f32(float* x, float* y, float* z); +/*! Normalizes in-place a gmio_vec3f object */ +GMIO_INLINE void gmio_vec3f_normalize(struct gmio_vec3f* v); -/*! Normalizes in-place the float64 (x,y,z) coords */ -GMIO_INLINE void gmio_normalize_f64(double* x, double* y, double* z); +/*! Normalizes in-place a gmio_vec3d object */ +GMIO_INLINE void gmio_vec3d_normalize(struct gmio_vec3d* v); /* * Implementation @@ -48,51 +48,61 @@ GMIO_INLINE void gmio_normalize_f64(double* x, double* y, double* z); #include -void gmio_cross_product_f32( - float ux, float uy, float uz, - float vx, float vy, float vz, - float* nx, float* ny, float* nz) +void gmio_vec3f_cross_product( + const struct gmio_vec3f* u, + const struct gmio_vec3f* v, + struct gmio_vec3f* n) { - *nx = uy*vz - uz*vx; - *ny = uz*vx - ux*vz; - *nz = ux*vy - uy*vx; - gmio_normalize_f32(nx, ny, nz); + const float ux = u->x, uy = u->y, uz = u->z; + const float vx = v->x, vy = v->y, vz = v->z; + n->x = uy*vz - uz*vx; + n->y = uz*vx - ux*vz; + n->z = ux*vy - uy*vx; + gmio_vec3f_normalize(n); } -void gmio_cross_product_f64( - double ux, double uy, double uz, - double vx, double vy, double vz, - double* nx, double* ny, double* nz) +void gmio_vec3d_cross_product( + const struct gmio_vec3d* u, + const struct gmio_vec3d* v, + struct gmio_vec3d* n) { - *nx = uy*vz - uz*vx; - *ny = uz*vx - ux*vz; - *nz = ux*vy - uy*vx; - gmio_normalize_f64(nx, ny, nz); + const double ux = u->x, uy = u->y, uz = u->z; + const double vx = v->x, vy = v->y, vz = v->z; + n->x = uy*vz - uz*vx; + n->y = uz*vx - ux*vz; + n->z = ux*vy - uy*vx; + gmio_vec3d_normalize(n); } -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) +float gmio_vec3f_sqr_length(const struct gmio_vec3f* v) { - const float d = (float)sqrt(gmio_sqr_length_f32(*x, *y, *z)); + const float vx = v->x, vy = v->y, vz = v->z; + return vx*vx + vy*vy + vz*vz; +} + +double gmio_vec3d_sqr_length(const struct gmio_vec3d* v) +{ + const double vx = v->x, vy = v->y, vz = v->z; + return vx*vx + vy*vy + vz*vz; +} + +void gmio_vec3f_normalize(struct gmio_vec3f* v) +{ + const float d = (float)sqrt(gmio_vec3f_sqr_length(v)); if (d > 0.f) { - *x /= d; - *y /= d; - *z /= d; + v->x /= d; + v->y /= d; + v->z /= d; } } -void gmio_normalize_f64(double* x, double* y, double* z) +void gmio_vec3d_normalize(struct gmio_vec3d* v) { - const double d = sqrt(gmio_sqr_length_f64(*x, *y, *z)); + const double d = sqrt(gmio_vec3d_sqr_length(v)); if (d > 0.) { - *x /= d; - *y /= d; - *z /= d; + v->x /= d; + v->y /= d; + v->z /= d; } } diff --git a/src/gmio_core/vecgeom.h b/src/gmio_core/vecgeom.h index 5be4268..f62ad4c 100644 --- a/src/gmio_core/vecgeom.h +++ b/src/gmio_core/vecgeom.h @@ -23,16 +23,16 @@ #ifndef GMIO_CORE_VECGEOM_H #define GMIO_CORE_VECGEOM_H -/*! Vector of three float32 coords */ -struct gmio_vec3_f32 +/*! Vector of three float coords */ +struct gmio_vec3f { float x; float y; float z; }; -/*! Vector of three float64 coords */ -struct gmio_vec3_f64 +/*! Vector of three double coords */ +struct gmio_vec3d { double x; double y; diff --git a/src/gmio_stl/internal/stla_write.c b/src/gmio_stl/internal/stla_write.c index 9275490..01fbc6c 100644 --- a/src/gmio_stl/internal/stla_write.c +++ b/src/gmio_stl/internal/stla_write.c @@ -119,7 +119,7 @@ GMIO_INLINE char* gmio_write_stdio_format( GMIO_INLINE char* gmio_write_coords( char* buffer, const char* coords_format, - const struct gmio_vec3_f32* coords) + const struct gmio_vec3f* coords) { return buffer + sprintf(buffer, coords_format, coords->x, coords->y, coords->z); diff --git a/src/gmio_stl/stl_constants.h b/src/gmio_stl/stl_constants.h index 7539d91..7263c6c 100644 --- a/src/gmio_stl/stl_constants.h +++ b/src/gmio_stl/stl_constants.h @@ -27,7 +27,7 @@ enum gmio_stl_constants { - /*! Compact size of a gmio_vec3_f32 object */ + /*! Compact size of a gmio_vec3f object */ GMIO_STL_COORDS_RAWSIZE = (3 * sizeof(float)), /*! Compact size of a gmio_stl_triangle object, STL ascii format */ diff --git a/src/gmio_stl/stl_format.c b/src/gmio_stl/stl_format.c index 825f72e..080f568 100644 --- a/src/gmio_stl/stl_format.c +++ b/src/gmio_stl/stl_format.c @@ -33,11 +33,6 @@ enum { GMIO_FIXED_BUFFER_SIZE = 1024 }; -GMIO_INLINE float gmio_sqrlen(const struct gmio_vec3_f32* c) -{ - return gmio_sqr_length_f32(c->x, c->y, c->z); -} - /* Does \p str contains token ? */ static bool gmio_str_has_token(const char* str, const char* token) { @@ -72,7 +67,9 @@ static enum gmio_stl_format gmio_stlb_format( memcpy(&tri, buff + GMIO_STLB_HEADER_SIZE + 4, GMIO_STLB_TRIANGLE_RAWSIZE); - if (gmio_float32_ulp_equals(gmio_sqrlen(&tri.n), 1.f, 100)) { + if (gmio_float32_ulp_equals( + gmio_vec3f_sqr_length(&tri.n), 1.f, 100)) + { #ifdef GMIO_HOST_IS_BIG_ENDIAN return GMIO_STL_FORMAT_BINARY_BE; #else @@ -80,7 +77,9 @@ static enum gmio_stl_format gmio_stlb_format( #endif } gmio_stl_triangle_bswap(&tri); - if (gmio_float32_ulp_equals(gmio_sqrlen(&tri.n), 1.f, 100)) { + if (gmio_float32_ulp_equals( + gmio_vec3f_sqr_length(&tri.n), 1.f, 100)) + { #ifdef GMIO_HOST_IS_BIG_ENDIAN return GMIO_STL_FORMAT_BINARY_LE; #else diff --git a/src/gmio_stl/stl_triangle.c b/src/gmio_stl/stl_triangle.c index 337cda7..48ec24a 100644 --- a/src/gmio_stl/stl_triangle.c +++ b/src/gmio_stl/stl_triangle.c @@ -17,10 +17,10 @@ #include "../gmio_core/internal/vecgeom_utils.h" -GMIO_INLINE struct gmio_vec3_f32 gmio_vec3_f32_diff( - const struct gmio_vec3_f32* u, const struct gmio_vec3_f32* v) +GMIO_INLINE struct gmio_vec3f gmio_vec3f_sub( + const struct gmio_vec3f* u, const struct gmio_vec3f* v) { - struct gmio_vec3_f32 diff; + struct gmio_vec3f diff; diff.x = u->x - v->x; diff.y = u->y - v->y; diff.z = u->z - v->z; @@ -29,8 +29,7 @@ GMIO_INLINE struct gmio_vec3_f32 gmio_vec3_f32_diff( void gmio_stl_triangle_compute_normal(struct gmio_stl_triangle* tri) { - const struct gmio_vec3_f32 u = gmio_vec3_f32_diff(&tri->v2, &tri->v1); - const struct gmio_vec3_f32 v = gmio_vec3_f32_diff(&tri->v3, &tri->v1); - struct gmio_vec3_f32* n = &tri->n; - gmio_cross_product_f32(u.x, u.y, u.z, v.x, v.y, v.z, &n->x, &n->y, &n->z); + const struct gmio_vec3f u = gmio_vec3f_sub(&tri->v2, &tri->v1); + const struct gmio_vec3f v = gmio_vec3f_sub(&tri->v3, &tri->v1); + gmio_vec3f_cross_product(&u, &v, &tri->n); } diff --git a/src/gmio_stl/stl_triangle.h b/src/gmio_stl/stl_triangle.h index 5dea380..4ad0ef7 100644 --- a/src/gmio_stl/stl_triangle.h +++ b/src/gmio_stl/stl_triangle.h @@ -29,10 +29,10 @@ /*! STL mesh triangle */ struct gmio_stl_triangle { - struct gmio_vec3_f32 n; /*!< Normal vector */ - struct gmio_vec3_f32 v1; /*!< Vertex 1 */ - struct gmio_vec3_f32 v2; /*!< Vertex 2 */ - struct gmio_vec3_f32 v3; /*!< Vertex 3 */ + struct gmio_vec3f n; /*!< Normal vector */ + struct gmio_vec3f v1; /*!< Vertex 1 */ + struct gmio_vec3f v2; /*!< Vertex 2 */ + struct gmio_vec3f v3; /*!< Vertex 3 */ uint16_t attribute_byte_count; /*!< Useful only for STL binary format */ }; diff --git a/src/gmio_stl/stla_read.c b/src/gmio_stl/stla_read.c index 392cf5f..0518588 100644 --- a/src/gmio_stl/stla_read.c +++ b/src/gmio_stl/stla_read.c @@ -243,7 +243,7 @@ static bool parse_endsolid(struct gmio_stla_parse_data* data); /* Parses STL (x,y,z) coords, each coord being separated by whitespaces */ GMIO_INLINE int parse_xyz_coords( - struct gmio_stla_parse_data* data, struct gmio_vec3_f32* coords); + struct gmio_stla_parse_data* data, struct gmio_vec3f* coords); /* Parses a STL facet, ie. facet ... endfacet */ static int parse_facet( @@ -588,7 +588,7 @@ GMIO_INLINE int is_float_char(const char* str) || c == '+'; } -int parse_xyz_coords(struct gmio_stla_parse_data* data, struct gmio_vec3_f32* coords) +int parse_xyz_coords(struct gmio_stla_parse_data* data, struct gmio_vec3f* coords) { int errc = 0; struct gmio_stringstream* sstream = &data->strstream; diff --git a/src/gmio_support/stl_occ.cpp b/src/gmio_support/stl_occ.cpp index c7a2727..7af9cfc 100644 --- a/src/gmio_support/stl_occ.cpp +++ b/src/gmio_support/stl_occ.cpp @@ -36,10 +36,10 @@ static void occmesh_add_triangle( StlMesh_Mesh* mesh = static_cast(cookie); if (tri_id == 0) mesh->AddDomain(); - const gmio_vec3_f32& v1 = tri->v1; - const gmio_vec3_f32& v2 = tri->v2; - const gmio_vec3_f32& v3 = tri->v3; - const gmio_vec3_f32& n = tri->n; + const gmio_vec3f& v1 = tri->v1; + const gmio_vec3f& v2 = tri->v2; + const gmio_vec3f& v3 = tri->v3; + const gmio_vec3f& n = tri->n; mesh->AddTriangle(mesh->AddOnlyNewVertex(v1.x, v1.y, v1.z), mesh->AddOnlyNewVertex(v2.x, v2.y, v2.z), mesh->AddOnlyNewVertex(v3.x, v3.y, v3.z), @@ -47,7 +47,7 @@ static void occmesh_add_triangle( } static inline void gmio_stl_occ_copy_xyz( - gmio_vec3_f32* stl_coords, const gp_XYZ& coords) + gmio_vec3f* stl_coords, const gp_XYZ& coords) { stl_coords->x = static_cast(coords.X()); stl_coords->y = static_cast(coords.Y()); @@ -66,7 +66,7 @@ static void occmesh_get_triangle( int idV1, idV2, idV3; double xN, yN, zN; occTri->GetVertexAndOrientation(idV1, idV2, idV3, xN, yN, zN); - gmio_vec3_f32& n = tri->n; + gmio_vec3f& n = tri->n; n.x = static_cast(xN); n.y = static_cast(yN); n.z = static_cast(zN); diff --git a/tests/core_utils.c b/tests/core_utils.c index 334902f..20df0bc 100644 --- a/tests/core_utils.c +++ b/tests/core_utils.c @@ -31,9 +31,9 @@ void gmio_string_trim_from_end(char *str, size_t len) } } -bool gmio_vec3_f32_equal( - const struct gmio_vec3_f32 *lhs, - const struct gmio_vec3_f32 *rhs, +bool gmio_vec3f_equal( + const struct gmio_vec3f *lhs, + const struct gmio_vec3f *rhs, uint32_t max_ulp_diff) { return gmio_float32_ulp_equals(lhs->x, rhs->x, max_ulp_diff) diff --git a/tests/core_utils.h b/tests/core_utils.h index 9401064..6a08b00 100644 --- a/tests/core_utils.h +++ b/tests/core_utils.h @@ -25,9 +25,9 @@ void gmio_string_trim_from_end(char* str, size_t len); /*! Does \p lhs == \p rhs using gmio_float32_ulp_equals() on coords ? */ -bool gmio_vec3_f32_equal( - const struct gmio_vec3_f32* lhs, - const struct gmio_vec3_f32* rhs, +bool gmio_vec3f_equal( + const struct gmio_vec3f* lhs, + const struct gmio_vec3f* rhs, uint32_t max_ulp_diff); #endif /* GMIO_TESTS_CORE_UTILS_H */ diff --git a/tests/stl_utils.c b/tests/stl_utils.c index 15bce24..354a7df 100644 --- a/tests/stl_utils.c +++ b/tests/stl_utils.c @@ -133,10 +133,10 @@ bool gmio_stl_triangle_equal( const struct gmio_stl_triangle *rhs, uint32_t max_ulp_diff) { - return gmio_vec3_f32_equal(&lhs->n, &rhs->n, max_ulp_diff) - && gmio_vec3_f32_equal(&lhs->v1, &rhs->v1, max_ulp_diff) - && gmio_vec3_f32_equal(&lhs->v2, &rhs->v2, max_ulp_diff) - && gmio_vec3_f32_equal(&lhs->v3, &rhs->v3, max_ulp_diff) + return gmio_vec3f_equal(&lhs->n, &rhs->n, max_ulp_diff) + && gmio_vec3f_equal(&lhs->v1, &rhs->v1, max_ulp_diff) + && gmio_vec3f_equal(&lhs->v2, &rhs->v2, max_ulp_diff) + && gmio_vec3f_equal(&lhs->v3, &rhs->v3, max_ulp_diff) && lhs->attribute_byte_count == rhs->attribute_byte_count; } diff --git a/tests/test_stl_triangle.c b/tests/test_stl_triangle.c index 09d6193..9d6d8cf 100644 --- a/tests/test_stl_triangle.c +++ b/tests/test_stl_triangle.c @@ -27,10 +27,10 @@ GMIO_PRAGMA_MSVC_WARNING_PUSH_AND_DISABLE(4127) const char* test_stl_coords_packing() { - UTEST_COMPARE_UINT(0, offsetof(struct gmio_vec3_f32, x)); - UTEST_COMPARE_UINT(4, offsetof(struct gmio_vec3_f32, y)); - UTEST_COMPARE_UINT(8, offsetof(struct gmio_vec3_f32, z)); - UTEST_COMPARE_UINT(GMIO_STL_COORDS_RAWSIZE, sizeof(struct gmio_vec3_f32)); + UTEST_COMPARE_UINT(0, offsetof(struct gmio_vec3f, x)); + UTEST_COMPARE_UINT(4, offsetof(struct gmio_vec3f, y)); + UTEST_COMPARE_UINT(8, offsetof(struct gmio_vec3f, z)); + UTEST_COMPARE_UINT(GMIO_STL_COORDS_RAWSIZE, sizeof(struct gmio_vec3f)); return NULL; }