Commit 7d25a21c authored by Jan Möbius's avatar Jan Möbius

Merge branch 'feature-free-functions' into 'master'

Feature free functions

Closes #52

See merge request !168
parents b0a7d0b8 2312a9e4
Pipeline #6847 passed with stage
in 104 minutes and 55 seconds
......@@ -117,6 +117,8 @@ cd unittests
unittests.exe --gtest_output=xml
unittests_customvec.exe --gtest_output=xml
cd ..
cd ..
......@@ -143,6 +145,8 @@ cd unittests
unittests.exe --gtest_output=xml
unittests_customvec.exe --gtest_output=xml
IF %errorlevel% NEQ 0 exit /b %errorlevel%
cd ..
......
......@@ -96,6 +96,15 @@ cd Unittests
#execute tests
./unittests --gtest_color=yes --gtest_output=xml
echo -e "${OUTPUT}"
echo ""
echo "======================================================================"
echo "Running unittests Release version with custom vector type"
echo "======================================================================"
echo -e "${NC}"
./unittests_customvec --gtest_color=yes --gtest_output=xml
cd ..
cd ..
......@@ -134,5 +143,14 @@ cd Unittests
#execute tests
./unittests --gtest_color=yes --gtest_output=xml
echo -e "${OUTPUT}"
echo ""
echo "======================================================================"
echo "Running unittests Debug version with custom vector type"
echo "======================================================================"
echo -e "${NC}"
./unittests_customvec --gtest_color=yes --gtest_output=xml
cd ..
cd ..
......@@ -78,6 +78,15 @@ cd Unittests
#execute tests
./unittests --gtest_color=yes --gtest_output=xml
echo -e "${OUTPUT}"
echo ""
echo "======================================================================"
echo "Running unittests Release version with minimal vector type"
echo "======================================================================"
echo -e "${NC}"
./unittests_customvec --gtest_color=yes --gtest_output=xml
cd ..
cd ..
......@@ -116,6 +125,15 @@ cd Unittests
#execute tests
./unittests --gtest_color=yes --gtest_output=xml
echo -e "${OUTPUT}"
echo ""
echo "======================================================================"
echo "Running unittests Debug version with minimal vector type"
echo "======================================================================"
echo -e "${NC}"
./unittests_customvec --gtest_color=yes --gtest_output=xml
cd ..
cd ..
......
......@@ -718,6 +718,47 @@ noexcept(noexcept(_v1.swap(_v2))) {
_v1.swap(_v2);
}
/// \relates OpenMesh::VectorT
/// non-member norm
template<typename Scalar, int DIM>
Scalar norm(const VectorT<Scalar, DIM>& _v) {
return _v.norm();
}
/// \relates OpenMesh::VectorT
/// non-member sqrnorm
template<typename Scalar, int DIM>
Scalar sqrnorm(const VectorT<Scalar, DIM>& _v) {
return _v.sqrnorm();
}
/// \relates OpenMesh::VectorT
/// non-member vectorize
template<typename Scalar, int DIM, typename OtherScalar>
VectorT<Scalar, DIM>& vectorize(VectorT<Scalar, DIM>& _v, OtherScalar const& _val) {
return _v.vectorize(_val);
}
/// \relates OpenMesh::VectorT
/// non-member normalize
template<typename Scalar, int DIM>
VectorT<Scalar, DIM>& normalize(VectorT<Scalar, DIM>& _v) {
return _v.normalize();
}
/// \relates OpenMesh::VectorT
/// non-member maximize
template<typename Scalar, int DIM>
VectorT<Scalar, DIM>& maximize(VectorT<Scalar, DIM>& _v1, VectorT<Scalar, DIM>& _v2) {
return _v1.maximize(_v2);
}
/// \relates OpenMesh::VectorT
/// non-member minimize
template<typename Scalar, int DIM>
VectorT<Scalar, DIM>& minimize(VectorT<Scalar, DIM>& _v1, VectorT<Scalar, DIM>& _v2) {
return _v1.minimize(_v2);
}
//== TYPEDEFS =================================================================
/** 1-byte signed vector */
......
......@@ -274,6 +274,52 @@ cross(const VectorT<Scalar,N>& _v1, const VectorT<Scalar,N>& _v2) {
}
/// \relates OpenMesh::VectorT
/// non-member norm
template<typename Scalar, int DIM>
Scalar norm(const VectorT<Scalar, DIM>& _v) {
return _v.norm();
}
/// \relates OpenMesh::VectorT
/// non-member sqrnorm
template<typename Scalar, int DIM>
Scalar sqrnorm(const VectorT<Scalar, DIM>& _v) {
return _v.sqrnorm();
}
/// \relates OpenMesh::VectorT
/// non-member vectorize
template<typename Scalar, int DIM, typename OtherScalar>
VectorT<Scalar, DIM>& vectorize(VectorT<Scalar, DIM>& _v, OtherScalar const& _val) {
return _v.vectorize(_val);
}
/// \relates OpenMesh::VectorT
/// non-member normalize
template<typename Scalar, int DIM>
VectorT<Scalar, DIM>& normalize(VectorT<Scalar, DIM>& _v) {
return _v.normalize();
}
/// \relates OpenMesh::VectorT
/// non-member maximize
template<typename Scalar, int DIM>
VectorT<Scalar, DIM>& maximize(VectorT<Scalar, DIM>& _v1, VectorT<Scalar, DIM>& _v2) {
return _v1.maximize(_v2);
}
/// \relates OpenMesh::VectorT
/// non-member minimize
template<typename Scalar, int DIM>
VectorT<Scalar, DIM>& minimize(VectorT<Scalar, DIM>& _v1, VectorT<Scalar, DIM>& _v2) {
return _v1.minimize(_v2);
}
//== TYPEDEFS =================================================================
......
......@@ -139,18 +139,18 @@ PolyMeshT<Kernel>::calc_face_normal_impl(FaceHandle _fh, PointIs3DTag) const
// Due to traits, the value types of normals and points can be different.
// Therefore we cast them here.
n[0] += static_cast<typename Normal::value_type>(a[1] * b[2]);
n[1] += static_cast<typename Normal::value_type>(a[2] * b[0]);
n[2] += static_cast<typename Normal::value_type>(a[0] * b[1]);
n[0] += static_cast<typename vector_traits<Normal>::value_type>(a[1] * b[2]);
n[1] += static_cast<typename vector_traits<Normal>::value_type>(a[2] * b[0]);
n[2] += static_cast<typename vector_traits<Normal>::value_type>(a[0] * b[1]);
}
const typename vector_traits<Normal>::value_type norm = n.length();
const typename vector_traits<Normal>::value_type length = norm(n);
// The expression ((n *= (1.0/norm)),n) is used because the OpenSG
// vector class does not return self after component-wise
// self-multiplication with a scalar!!!
return (norm != typename vector_traits<Normal>::value_type(0))
? ((n *= (typename vector_traits<Normal>::value_type(1)/norm)), n)
return (length != typename vector_traits<Normal>::value_type(0))
? ((n *= (typename vector_traits<Normal>::value_type(1)/length)), n)
: Normal(0, 0, 0);
}
......@@ -194,20 +194,22 @@ calc_face_normal_impl(const Point& _p0,
Normal p1p2(vector_cast<Normal>(_p2)); p1p2 -= vector_cast<Normal>(_p1);
Normal n = cross(p1p2, p1p0);
typename vector_traits<Normal>::value_type norm = n.length();
typename vector_traits<Normal>::value_type length = norm(n);
// The expression ((n *= (1.0/norm)),n) is used because the OpenSG
// vector class does not return self after component-wise
// self-multiplication with a scalar!!!
return (norm != typename vector_traits<Normal>::value_type(0)) ? ((n *= (typename vector_traits<Normal>::value_type(1)/norm)),n) : Normal(0,0,0);
return (length != typename vector_traits<Normal>::value_type(0))
? ((n *= (typename vector_traits<Normal>::value_type(1)/length)),n)
: Normal(0,0,0);
#else
Point p1p0 = _p0; p1p0 -= _p1;
Point p1p2 = _p2; p1p2 -= _p1;
Normal n = vector_cast<Normal>(cross(p1p2, p1p0));
typename vector_traits<Normal>::value_type norm = n.length();
typename vector_traits<Normal>::value_type length = norm(n);
return (norm != 0.0) ? n *= (1.0/norm) : Normal(0,0,0);
return (length != 0.0) ? n *= (1.0/length) : Normal(0,0,0);
#endif
}
......@@ -226,7 +228,7 @@ PolyMeshT<Kernel>::
calc_face_centroid(FaceHandle _fh) const
{
Point _pt;
_pt.vectorize(0);
vectorize(_pt, 0);
Scalar valence = 0.0;
for (ConstFaceVertexIter cfv_it = this->cfv_iter(_fh); cfv_it.is_valid(); ++cfv_it, valence += 1.0)
{
......@@ -331,7 +333,7 @@ calc_halfedge_normal(HalfedgeHandle _heh, const double _feature_angle) const
for(unsigned int i=0; i<fhs.size(); ++i)
n += Kernel::normal(fhs[i]);
return n.normalize();
return normalize(n);
}
}
......@@ -378,8 +380,8 @@ calc_vertex_normal(VertexHandle _vh) const
Normal n;
calc_vertex_normal_fast(_vh,n);
Scalar norm = n.length();
if (norm != 0.0) n *= (Scalar(1.0)/norm);
Scalar length = norm(n);
if (length != 0.0) n *= (Scalar(1.0)/length);
return n;
}
......@@ -389,7 +391,7 @@ template <class Kernel>
void PolyMeshT<Kernel>::
calc_vertex_normal_fast(VertexHandle _vh, Normal& _n) const
{
_n.vectorize(0.0);
vectorize(_n, 0.0);
for (ConstVertexFaceIter vf_it = this->cvf_iter(_vh); vf_it.is_valid(); ++vf_it)
_n += this->normal(*vf_it);
}
......@@ -399,7 +401,7 @@ template <class Kernel>
void PolyMeshT<Kernel>::
calc_vertex_normal_correct(VertexHandle _vh, Normal& _n) const
{
_n.vectorize(0.0);
vectorize(_n, 0.0);
ConstVertexIHalfedgeIter cvih_it = this->cvih_iter(_vh);
if (! cvih_it.is_valid() )
{//don't crash on isolated vertices
......
......@@ -406,7 +406,7 @@ public:
{
Normal edge_vec;
calc_edge_vector(_heh, edge_vec);
return edge_vec.sqrnorm();
return sqrnorm(edge_vec);
}
/** Calculates the midpoint of the halfedge _heh, defined by the positions of
......@@ -444,7 +444,7 @@ public:
{
Normal v0, v1;
calc_sector_vectors(_in_heh, v0, v1);
Scalar denom = v0.norm()*v1.norm();
Scalar denom = norm(v0)*norm(v1);
if ( denom == Scalar(0))
{
return 0;
......@@ -470,7 +470,7 @@ public:
Normal in_vec, out_vec;
calc_edge_vector(_in_heh, in_vec);
calc_edge_vector(next_halfedge_handle(_in_heh), out_vec);
Scalar denom = in_vec.norm()*out_vec.norm();
Scalar denom = norm(in_vec)*norm(out_vec);
if (is_zero(denom))
{
_cos_a = 1;
......@@ -479,7 +479,7 @@ public:
else
{
_cos_a = dot(in_vec, out_vec)/denom;
_sin_a = cross(in_vec, out_vec).norm()/denom;
_sin_a = norm(cross(in_vec, out_vec))/denom;
}
}
*/
......@@ -499,7 +499,7 @@ public:
{
Normal sector_normal;
calc_sector_normal(_in_heh, sector_normal);
return sector_normal.norm()/2;
return norm(sector_normal)/2;
}
/** calculates the dihedral angle on the halfedge _heh
......@@ -539,7 +539,7 @@ public:
calc_sector_normal(_heh, n0);
calc_sector_normal(this->opposite_halfedge_handle(_heh), n1);
calc_edge_vector(_heh, he);
Scalar denom = n0.norm()*n1.norm();
Scalar denom = norm(n0)*norm(n1);
if (denom == Scalar(0))
{
return 0;
......
......@@ -359,9 +359,9 @@ public:
VertexHandle p2 = this->to_vertex_handle(he2);
// Calculate midpoint coordinates
const Point new0 = (this->point(p0) + this->point(p2)) * static_cast< typename Point::value_type >(0.5);
const Point new1 = (this->point(p0) + this->point(p1)) * static_cast< typename Point::value_type >(0.5);
const Point new2 = (this->point(p1) + this->point(p2)) * static_cast< typename Point::value_type >(0.5);
const Point new0 = (this->point(p0) + this->point(p2)) * static_cast<typename vector_traits<Point>::value_type >(0.5);
const Point new1 = (this->point(p0) + this->point(p1)) * static_cast<typename vector_traits<Point>::value_type >(0.5);
const Point new2 = (this->point(p1) + this->point(p2)) * static_cast<typename vector_traits<Point>::value_type >(0.5);
// Add vertices at midpoint coordinates
VertexHandle v0 = this->add_vertex(new0);
......
......@@ -95,7 +95,6 @@ inline void vector_cast( const src_t & /*_src*/, dst_t & /*_dst*/, GenProg::Int2
{
}
template <typename src_t, typename dst_t, int n>
inline void vector_copy( const src_t &_src, dst_t &_dst, GenProg::Int2Type<n> )
{
......
......@@ -181,7 +181,7 @@ compute_new_positions_C1()
if (diag) uu *= static_cast<typename Mesh::Scalar>(1.0) / diag;
// damping
uu *= static_cast<typename Mesh::Normal::value_type>(0.25);
uu *= static_cast<typename vector_traits<typename Mesh::Normal>::value_type>(0.25);
// store new position
p = vector_cast<typename Mesh::Normal>(Base::mesh_.point(*v_it));
......
......@@ -263,13 +263,13 @@ set_relative_local_error(Scalar _err)
bb_min = bb_max = mesh_.point(*v_it);
for (++v_it; v_it!=v_end; ++v_it)
{
bb_min.minimize(mesh_.point(*v_it));
bb_max.maximize(mesh_.point(*v_it));
minimize(bb_min, mesh_.point(*v_it));
maximize(bb_max, mesh_.point(*v_it));
}
// abs. error = rel. error * bounding-diagonal
set_absolute_local_error(_err * (bb_max-bb_min).norm());
set_absolute_local_error(norm(_err * (bb_max-bb_min)));
}
}
......
......@@ -346,7 +346,7 @@ CatmullClarkT<MeshType,RealType>::update_vertex( MeshType& _m, const VertexHandl
for ( ve_itr = _m.ve_iter( _vh); ve_itr.is_valid(); ++ve_itr)
if ( _m.is_boundary( *ve_itr))
pos += _m.property( ep_pos_, *ve_itr);
pos /= static_cast<typename MeshType::Point::value_type>(3.0);
pos /= static_cast<typename vector_traits<typename MeshType::Point>::value_type>(3.0);
}
else // inner vertex
{
......
......@@ -30,22 +30,28 @@ if ( OPENMESH_BUILD_UNIT_TESTS )
FILE(GLOB UNITTEST_SRC *.cc)
# Create unittest executable
acg_add_executable(unittests ${UNITTEST_SRC})
acg_add_executable(unittests_customvec ${UNITTEST_SRC})
target_compile_definitions(unittests_customvec PRIVATE TEST_CUSTOM_TRAITS)
# For the unittest we don't want the install rpath as set by acg_add_executable
set_target_properties ( unittests PROPERTIES BUILD_WITH_INSTALL_RPATH 0 )
set_target_properties ( unittests PROPERTIES BUILD_WITH_INSTALL_RPATH 0 )
set_target_properties ( unittests_customvec PROPERTIES BUILD_WITH_INSTALL_RPATH 0 )
# Set output directory to ${BINARY_DIR}/Unittests
set (OUTPUT_DIR "${CMAKE_BINARY_DIR}/Unittests")
set_target_properties(unittests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR})
set_target_properties(unittests_customvec PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR})
foreach(CONFIG ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER ${CONFIG} UPCONFIG)
set_target_properties(unittests PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${UPCONFIG} ${OUTPUT_DIR})
set_target_properties(unittests_customvec PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${UPCONFIG} ${OUTPUT_DIR})
endforeach()
if ( NOT WIN32)
# Link against all necessary libraries
target_link_libraries(unittests OpenMeshCore OpenMeshTools ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread)
target_link_libraries(unittests_customvec OpenMeshCore OpenMeshTools ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread)
......@@ -56,6 +62,7 @@ if ( OPENMESH_BUILD_UNIT_TESTS )
endif()
target_link_libraries(unittests OpenMeshCore OpenMeshTools ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
target_link_libraries(unittests_customvec OpenMeshCore OpenMeshTools ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
endif()
......@@ -63,10 +70,11 @@ if ( OPENMESH_BUILD_UNIT_TESTS )
if ( NOT WIN32 )
# Set compiler flags
set_target_properties(unittests PROPERTIES COMPILE_FLAGS "-g -pedantic -Wno-long-long")
set_target_properties(unittests_customvec PROPERTIES COMPILE_FLAGS "-g -pedantic -Wno-long-long")
else()
# Set compiler flags
set_target_properties(unittests PROPERTIES COMPILE_FLAGS "" )
set_target_properties(unittests_customvec PROPERTIES COMPILE_FLAGS "" )
endif()
if ( OPENMESH_BUILD_SHARED )
......@@ -80,12 +88,19 @@ if ( OPENMESH_BUILD_UNIT_TESTS )
"$<TARGET_FILE:${TAR}>"
"${CMAKE_BINARY_DIR}/Unittests/$<TARGET_FILE_NAME:${TAR}>"
COMMENT "Copying OpenMesh targets to unittests directory")
add_custom_command(TARGET unittests_customvec POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy
"$<TARGET_FILE:${TAR}>"
"${CMAKE_BINARY_DIR}/Unittests/$<TARGET_FILE_NAME:${TAR}>"
COMMENT "Copying OpenMesh targets to unittests directory")
endforeach(TAR)
endif()
acg_copy_after_build(unittests ${CMAKE_CURRENT_SOURCE_DIR}/TestFiles ${CMAKE_BINARY_DIR}/Unittests/)
acg_copy_after_build(unittests_customvec ${CMAKE_CURRENT_SOURCE_DIR}/TestFiles ${CMAKE_BINARY_DIR}/Unittests/)
add_test(NAME AllTestsIn_OpenMesh_tests WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/Unittests" COMMAND "${CMAKE_BINARY_DIR}/Unittests/unittests")
add_test(NAME AllTestsIn_OpenMesh_tests_with_minimal_vector WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/Unittests" COMMAND "${CMAKE_BINARY_DIR}/Unittests/unittests_customvec")
else(GTEST_FOUND)
message(STATUS "Google testing framework was not found, unittests disabled.")
......
......@@ -7,8 +7,13 @@
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>
#ifdef TEST_CUSTOM_TRAITS
#include <Unittests/unittests_common_customtraits.hh>
#else
struct CustomTraits : public OpenMesh::DefaultTraits {
};
#endif
typedef OpenMesh::TriMesh_ArrayKernelT<CustomTraits> Mesh;
......
#ifndef UNITTESTS_COMMON_DUMMYTRAITS
#define UNITTESTS_COMMON_DUMMYTRAITS
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Core/Utils/color_cast.hh>
#include <array>
namespace Custom {
/// A Vector class with the absolute minimum of built-in methods to test the
/// interface expected from Vectors used in Traits
template <int DIM> class Vec {
public:
// Constructor with DIM components
Vec(float x) : data({ x }) {}
Vec(float x, float y) : data({ x, y }) {}
Vec(float x, float y, float z) : data({ x, y, z }) {}
Vec(float x, float y, float z, float w) : data({ x, y, z, w }) {}
Vec() = default;
Vec(Vec<DIM> const &) = default;
float &operator[](int i) { return data[i]; }
float operator[](int i) const { return data[i]; }
private:
std::array<float, DIM> data;
};
template <int DIM> bool operator==(Vec<DIM> const &lhs, Vec<DIM> const &rhs) {
for (int i = 0; i < DIM; i++)
if (lhs[i] != rhs[i]) return false;
return true;
}
template <int DIM>
Vec<DIM> operator+(Vec<DIM> const &lhs, Vec<DIM> const &rhs) {
Vec<DIM> result;
for (int i = 0; i < DIM; i++)
result[i] = lhs[i] + rhs[i];
return result;
}
template <int DIM>
Vec<DIM> operator-(Vec<DIM> const &lhs, Vec<DIM> const &rhs) {
Vec<DIM> result;
for (int i = 0; i < DIM; i++)
result[i] = lhs[i] - rhs[i];
return result;
}
template <int DIM> Vec<DIM> operator*(Vec<DIM> const &lhs, float rhs) {
Vec<DIM> result;
for (int i = 0; i < DIM; i++)
result[i] = lhs[i] * rhs;
return result;
}
template <int DIM> Vec<DIM> operator*(float lhs, Vec<DIM> const &rhs) {
return rhs * lhs;
}
template <int DIM> Vec<DIM> operator/(Vec<DIM> const &lhs, float rhs) {
Vec<DIM> result;
for (int i = 0; i < DIM; i++)
result[i] = lhs[i] / rhs;
return result;
}
template <int DIM> Vec<DIM> &operator+=(Vec<DIM> &lhs, Vec<DIM> const &rhs) {
return lhs = lhs + rhs;
}
template <int DIM> Vec<DIM> &operator-=(Vec<DIM> &lhs, Vec<DIM> const &rhs) {
return lhs = lhs - rhs;
}
template <int DIM> Vec<DIM> &operator*=(Vec<DIM> &lhs, float rhs) {
return lhs = lhs * rhs;
}
template <int DIM> Vec<DIM> &operator/=(Vec<DIM> &lhs, float rhs) {
return lhs = lhs / rhs;
}
template <int DIM> float norm(Vec<DIM> const &v) {
float sum = 0.0f;
for (int i = 0; i < DIM; i++)
sum += v[i] * v[i];
return std::sqrt(sum);
}
template <int DIM> Vec<DIM> &normalize(Vec<DIM> &v) { return v /= norm(v); }
template <int DIM> Vec<DIM> &vectorize(Vec<DIM> &v, float val) {
for (int i = 0; i < DIM; i++)
v[i] = val;
return v;
}
template <int DIM> Vec<DIM> &minimize(Vec<DIM> &v1, Vec<DIM> const &v2) {
for (int i = 0; i < DIM; i++)
v1[i] = std::min(v1[i], v2[i]);
return v1;
}
template <int DIM> Vec<DIM> &maximize(Vec<DIM> &v1, Vec<DIM> const &v2) {
for (int i = 0; i < DIM; i++)
v1[i] = std::max(v1[i], v2[i]);
return v1;
}
template <int DIM> float dot(Vec<DIM> const &v1, Vec<DIM> const &v2) {
float sum = 0.f;
for (int i = 0; i < DIM; i++)
sum += v1[i] * v2[i];
return sum;
}
inline Vec<3> cross(Vec<3> const &v1, Vec<3> const &v2) {
return {v1[1] * v2[2] - v1[2] * v2[1], //
v1[2] * v2[0] - v1[0] * v2[2], //
v1[0] * v2[1] - v1[1] * v2[0]};
}
}
namespace OpenMesh {
template <int DIM> struct vector_traits<Custom::Vec<DIM>> {
using vector_type = Custom::Vec<DIM>;
using value_type = float;
static const size_t size_ = DIM;
static size_t size() { return size_; }
};
}
struct CustomTraits : public OpenMesh::DefaultTraits {
typedef Custom::Vec<3> Point;
typedef Custom::Vec<3> Normal;
typedef Custom::Vec<2> TexCoord2D;
typedef Custom::Vec<3> TexCoord3D;