Browse Source

MeshTools: functions for flipping normals and face winding.

pull/279/head
Vladimír Vondruš 14 years ago
parent
commit
c754b1c1aa
  1. 1
      src/MeshTools/CMakeLists.txt
  2. 38
      src/MeshTools/FlipNormals.cpp
  3. 61
      src/MeshTools/FlipNormals.h
  4. 1
      src/MeshTools/Test/CMakeLists.txt
  5. 59
      src/MeshTools/Test/FlipNormalsTest.cpp
  6. 33
      src/MeshTools/Test/FlipNormalsTest.h

1
src/MeshTools/CMakeLists.txt

@ -1,4 +1,5 @@
set(MagnumMeshTools_SRCS
FlipNormals.cpp
GenerateFlatNormals.cpp
Tipsify.cpp
)

38
src/MeshTools/FlipNormals.cpp

@ -0,0 +1,38 @@
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include "FlipNormals.h"
using namespace std;
using namespace Corrade::Utility;
namespace Magnum { namespace MeshTools {
void flipFaceWinding(vector<unsigned int>& indices) {
if(indices.size()%3 != 0) {
Error() << "MeshTools::flipNormals(): index count is not divisible by 3!";
return;
}
for(size_t i = 0; i != indices.size(); i += 3)
swap(indices[i+1], indices[i+2]);
}
void flipNormals(vector<Vector3>& normals) {
for(Vector3& normal: normals)
normal = -normal;
}
}}

61
src/MeshTools/FlipNormals.h

@ -0,0 +1,61 @@
#ifndef Magnum_MeshTools_FlipNormals_h
#define Magnum_MeshTools_FlipNormals_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
/** @file
* @brief Function Magnum::MeshTools::flipNormals()
*/
#include "Magnum.h"
namespace Magnum { namespace MeshTools {
/**
@brief Flip face winding
The same as flipNormals(std::vector<unsigned int>&, std::vector<Vector3>&),
but flips only face winding.
*/
void flipFaceWinding(std::vector<unsigned int>& indices);
/**
@brief Flip mesh normals
The same as flipNormals(std::vector<unsigned int>&, std::vector<Vector3>&),
but flips only normals, not face winding.
*/
void flipNormals(std::vector<Vector3>& normals);
/**
@brief Flip mesh normals and face winding
@param indices Index array to operate on
@param normals Normal array to operate on
Flips normal vectors and face winding in index array for face culling to work
properly too. See also flipNormals(std::vector<Vector3>&) and
flipFaceWinding(), which flip normals or face winding only.
@attention The function requires the mesh to have triangle faces, thus index
count must be divisible by 3.
*/
inline void flipNormals(std::vector<unsigned int>& indices, std::vector<Vector3>& normals) {
flipFaceWinding(indices);
flipNormals(normals);
}
}}
#endif

1
src/MeshTools/Test/CMakeLists.txt

@ -1,6 +1,7 @@
corrade_add_test(CleanTest CleanTest.h CleanTest.cpp ${CORRADE_UTILITY_LIBRARY})
corrade_add_test(CombineIndexedArraysTest CombineIndexedArraysTest.h CombineIndexedArraysTest.cpp ${CORRADE_UTILITY_LIBRARY})
corrade_add_test(CompressIndicesTest CompressIndicesTest.h CompressIndicesTest.cpp ${CORRADE_UTILITY_LIBRARY} ${MAGNUM_LIBRARY})
corrade_add_test(FlipNormalsTest FlipNormalsTest.h FlipNormalsTest.cpp ${MAGNUM_MESHTOOLS_LIBRARY})
corrade_add_test(GenerateFlatNormalsTest GenerateFlatNormalsTest.h GenerateFlatNormalsTest.cpp ${MAGNUM_MESHTOOLS_LIBRARY})
corrade_add_test(InterleaveTest InterleaveTest.h InterleaveTest.cpp ${CORRADE_UTILITY_LIBRARY})
corrade_add_test(SubdivideTest SubdivideTest.h SubdivideTest.cpp ${CORRADE_UTILITY_LIBRARY})

59
src/MeshTools/Test/FlipNormalsTest.cpp

@ -0,0 +1,59 @@
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include "FlipNormalsTest.h"
#include <sstream>
#include <QtTest/QTest>
#include "MeshTools/FlipNormals.h"
QTEST_APPLESS_MAIN(Magnum::MeshTools::Test::FlipNormalsTest)
using namespace std;
using namespace Corrade::Utility;
namespace Magnum { namespace MeshTools { namespace Test {
void FlipNormalsTest::wrongIndexCount() {
stringstream ss;
Error::setOutput(&ss);
vector<unsigned int> indices{0, 1};
MeshTools::flipFaceWinding(indices);
QVERIFY(ss.str() == "MeshTools::flipNormals(): index count is not divisible by 3!\n");
}
void FlipNormalsTest::flipFaceWinding() {
vector<unsigned int> indices{0, 1, 2,
3, 4, 5};
MeshTools::flipFaceWinding(indices);
QVERIFY((indices == vector<unsigned int>{0, 2, 1,
3, 5, 4}));
}
void FlipNormalsTest::flipNormals() {
vector<Vector3> normals{Vector3::xAxis(),
Vector3::yAxis(),
Vector3::zAxis()};
MeshTools::flipNormals(normals);
QVERIFY((normals == vector<Vector3>{-Vector3::xAxis(),
-Vector3::yAxis(),
-Vector3::zAxis()}));
}
}}}

33
src/MeshTools/Test/FlipNormalsTest.h

@ -0,0 +1,33 @@
#ifndef Magnum_MeshTools_Test_FlipNormalsTest_h
#define Magnum_MeshTools_Test_FlipNormalsTest_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include <QtCore/QObject>
namespace Magnum { namespace MeshTools { namespace Test {
class FlipNormalsTest: public QObject {
Q_OBJECT
private slots:
void wrongIndexCount();
void flipFaceWinding();
void flipNormals();
};
}}}
#endif
Loading…
Cancel
Save