Browse Source

Use the new math constants throughout the code.

pull/87/head
Vladimír Vondruš 12 years ago
parent
commit
c56940993b
  1. 17
      src/Magnum/Math/Geometry/Test/IntersectionTest.cpp
  2. 20
      src/Magnum/Math/Test/FunctionsTest.cpp
  3. 10
      src/Magnum/Math/Test/TypeTraitsTest.cpp
  4. 6
      src/Magnum/Primitives/Capsule.cpp
  5. 8
      src/Magnum/Primitives/Implementation/WireframeSpheroid.cpp
  6. 2
      src/Magnum/Primitives/UVSphere.cpp
  7. 3
      src/Magnum/SceneGraph/Animable.hpp
  8. 4
      src/Magnum/Shapes/Plane.cpp

17
src/Magnum/Math/Geometry/Test/IntersectionTest.cpp

@ -23,7 +23,6 @@
DEALINGS IN THE SOFTWARE.
*/
#include <limits>
#include <Corrade/TestSuite/Tester.h>
#include "Magnum/Math/Geometry/Intersection.h"
@ -40,6 +39,7 @@ class IntersectionTest: public Corrade::TestSuite::Tester {
typedef Math::Vector2<Float> Vector2;
typedef Math::Vector3<Float> Vector3;
typedef Math::Constants<Float> Constants;
IntersectionTest::IntersectionTest() {
addTests({&IntersectionTest::planeLine,
@ -60,11 +60,11 @@ void IntersectionTest::planeLine() {
/* Line lies on the plane */
CORRADE_COMPARE(Intersection::planeLine(planePosition, planeNormal,
{1.0f, 0.5f, 0.5f}, {-1.0f, 0.5f, 0.0f}), std::numeric_limits<Float>::quiet_NaN());
{1.0f, 0.5f, 0.5f}, {-1.0f, 0.5f, 0.0f}), Constants::nan());
/* Line is parallel to the plane */
CORRADE_COMPARE(Intersection::planeLine(planePosition, planeNormal,
{1.0f, 0.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}), -std::numeric_limits<Float>::infinity());
{1.0f, 0.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}), -Constants::inf());
}
void IntersectionTest::lineLine() {
@ -86,17 +86,16 @@ void IntersectionTest::lineLine() {
/* Collinear lines */
const auto tu = Intersection::lineSegmentLineSegment(p, r,
{0.0f, 1.0f}, {-1.0f, -2.0f});
CORRADE_COMPARE(tu.first, -std::numeric_limits<Float>::quiet_NaN());
CORRADE_COMPARE(tu.second, -std::numeric_limits<Float>::quiet_NaN());
CORRADE_COMPARE(tu.first, -Constants::nan());
CORRADE_COMPARE(tu.second, -Constants::nan());
CORRADE_COMPARE(Intersection::lineSegmentLine(p, r,
{0.0f, 1.0f}, {-1.0f, -2.0f}), -std::numeric_limits<Float>::quiet_NaN());
{0.0f, 1.0f}, {-1.0f, -2.0f}), -Constants::nan());
/* Parallel lines */
CORRADE_COMPARE(Intersection::lineSegmentLineSegment(p, r,
{0.0f, 0.0f}, {1.0f, 2.0f}), std::make_pair(std::numeric_limits<Float>::infinity(),
std::numeric_limits<Float>::infinity()));
{0.0f, 0.0f}, {1.0f, 2.0f}), std::make_pair(Constants::inf(), Constants::inf()));
CORRADE_COMPARE(Intersection::lineSegmentLine(p, r,
{0.0f, 0.0f}, {1.0f, 2.0f}), std::numeric_limits<Float>::infinity());
{0.0f, 0.0f}, {1.0f, 2.0f}), Constants::inf());
}
}}}}

20
src/Magnum/Math/Test/FunctionsTest.cpp

@ -161,19 +161,17 @@ void FunctionsTest::clamp() {
}
void FunctionsTest::nanPropagation() {
constexpr const Float NaN = std::numeric_limits<float>::quiet_NaN();
CORRADE_COMPARE(Math::min(Constants::nan(), 5.0f), Constants::nan());
CORRADE_COMPARE(Math::min(Vector2{Constants::nan(), 6.0f}, Vector2{5.0f})[0], Constants::nan());
CORRADE_COMPARE(Math::min(Vector2{Constants::nan(), 6.0f}, Vector2{5.0f})[1], 5.0f);
CORRADE_COMPARE(Math::min(NaN, 5.0f), NaN);
CORRADE_COMPARE(Math::min(Vector2{NaN, 6.0f}, Vector2{5.0f})[0], NaN);
CORRADE_COMPARE(Math::min(Vector2{NaN, 6.0f}, Vector2{5.0f})[1], 5.0f);
CORRADE_COMPARE(Math::max(Constants::nan(), 5.0f), Constants::nan());
CORRADE_COMPARE(Math::max(Vector2{Constants::nan(), 4.0f}, Vector2{5.0f})[0], Constants::nan());
CORRADE_COMPARE(Math::max(Vector2{Constants::nan(), 4.0f}, Vector2{5.0f})[1], 5.0f);
CORRADE_COMPARE(Math::max(NaN, 5.0f), NaN);
CORRADE_COMPARE(Math::max(Vector2{NaN, 4.0f}, Vector2{5.0f})[0], NaN);
CORRADE_COMPARE(Math::max(Vector2{NaN, 4.0f}, Vector2{5.0f})[1], 5.0f);
CORRADE_COMPARE(Math::clamp(NaN, 2.0f, 6.0f), NaN);
CORRADE_COMPARE(Math::clamp(Vector2{NaN, 1.0f}, 2.0f, 6.0f)[0], NaN);
CORRADE_COMPARE(Math::clamp(Vector2{NaN, 1.0f}, 2.0f, 6.0f)[1], 2.0f);
CORRADE_COMPARE(Math::clamp(Constants::nan(), 2.0f, 6.0f), Constants::nan());
CORRADE_COMPARE(Math::clamp(Vector2{Constants::nan(), 1.0f}, 2.0f, 6.0f)[0], Constants::nan());
CORRADE_COMPARE(Math::clamp(Vector2{Constants::nan(), 1.0f}, 2.0f, 6.0f)[1], 2.0f);
}
void FunctionsTest::sign() {

10
src/Magnum/Math/Test/TypeTraitsTest.cpp

@ -23,10 +23,10 @@
DEALINGS IN THE SOFTWARE.
*/
#include <limits>
#include <Corrade/TestSuite/Tester.h>
#include "Magnum/Math/TypeTraits.h"
#include "Magnum/Math/Constants.h"
namespace Magnum { namespace Math { namespace Test {
@ -118,8 +118,8 @@ void TypeTraitsTest::equalsFloatingPointInfinity() {
}
template<class T> void TypeTraitsTest::_equalsFloatingPointInfinity() {
CORRADE_VERIFY(TypeTraits<T>::equals(std::numeric_limits<T>::infinity(),
std::numeric_limits<T>::infinity()));
CORRADE_VERIFY(TypeTraits<T>::equals(Constants<T>::inf(),
Constants<T>::inf()));
}
void TypeTraitsTest::equalsFloatingPointNaN() {
@ -130,8 +130,8 @@ void TypeTraitsTest::equalsFloatingPointNaN() {
}
template<class T> void TypeTraitsTest::_equalsFloatingPointNaN() {
CORRADE_VERIFY(!TypeTraits<T>::equals(std::numeric_limits<T>::quiet_NaN(),
std::numeric_limits<T>::quiet_NaN()));
CORRADE_VERIFY(!TypeTraits<T>::equals(Constants<T>::nan(),
Constants<T>::nan()));
}
}}}

6
src/Magnum/Primitives/Capsule.cpp

@ -40,7 +40,7 @@ Trade::MeshData2D Capsule2D::wireframe(UnsignedInt hemisphereRings, UnsignedInt
std::vector<Vector2> positions;
positions.reserve(hemisphereRings*4+2+(cylinderRings-1)*2);
const Rad angleIncrement(Constants::pi()/(2.0f*hemisphereRings));
const Rad angleIncrement(Constants::piHalf()/hemisphereRings);
const Float cylinderIncrement = 2.0f*halfLength/cylinderRings;
/* Bottom cap vertex */
@ -99,13 +99,13 @@ Trade::MeshData3D Capsule3D::solid(UnsignedInt hemisphereRings, UnsignedInt cyli
Float height = 2.0f+2.0f*halfLength;
Float hemisphereTextureCoordsVIncrement = 1.0f/(hemisphereRings*height);
Rad hemisphereRingAngleIncrement(Constants::pi()/(2*hemisphereRings));
Rad hemisphereRingAngleIncrement(Constants::piHalf()/hemisphereRings);
/* Bottom cap vertex */
capsule.capVertex(-height/2, -1.0f, 0.0f);
/* Rings of bottom hemisphere */
capsule.hemisphereVertexRings(hemisphereRings-1, -halfLength, -Rad(Constants::pi())/2+hemisphereRingAngleIncrement, hemisphereRingAngleIncrement, hemisphereTextureCoordsVIncrement, hemisphereTextureCoordsVIncrement);
capsule.hemisphereVertexRings(hemisphereRings-1, -halfLength, -Rad(Constants::piHalf())+hemisphereRingAngleIncrement, hemisphereRingAngleIncrement, hemisphereTextureCoordsVIncrement, hemisphereTextureCoordsVIncrement);
/* Rings of cylinder */
capsule.cylinderVertexRings(cylinderRings+1, -halfLength, 2.0f*halfLength/cylinderRings, 1.0f/height, 2.0f*halfLength/(cylinderRings*height));

8
src/Magnum/Primitives/Implementation/WireframeSpheroid.cpp

@ -45,7 +45,7 @@ void WireframeSpheroid::bottomHemisphere(const Float endY, const UnsignedInt rin
_indices.insert(_indices.end(), {0, i+1});
/* Hemisphere vertices and indices */
const Rad ringAngleIncrement(Constants::pi()/(2*rings));
const Rad ringAngleIncrement(Constants::piHalf()/rings);
for(UnsignedInt j = 0; j != rings-1; ++j) {
const Rad angle = Float(j+1)*ringAngleIncrement;
@ -66,7 +66,7 @@ void WireframeSpheroid::topHemisphere(const Float startY, const UnsignedInt ring
_indices.insert(_indices.end(), {UnsignedInt(_positions.size())-4*_segments+i, UnsignedInt(_positions.size())+i});
/* Hemisphere vertices and indices */
const Rad ringAngleIncrement(Constants::pi()/(2*rings));
const Rad ringAngleIncrement(Constants::piHalf()/rings);
for(UnsignedInt j = 0; j != rings-1; ++j) {
const Rad angle = Float(j+1)*ringAngleIncrement;
@ -90,10 +90,10 @@ void WireframeSpheroid::topHemisphere(const Float startY, const UnsignedInt ring
void WireframeSpheroid::ring(const Float y) {
/* Ring vertices and indices */
const Rad segmentAngleIncrement(Constants::pi()/(2*_segments));
const Rad segmentAngleIncrement(Constants::piHalf()/_segments);
for(UnsignedInt j = 0; j != _segments; ++j) {
for(UnsignedInt i = 0; i != 4; ++i) {
const Rad segmentAngle = Rad(Float(i)*Constants::pi()/2) + Float(j)*segmentAngleIncrement;
const Rad segmentAngle = Rad(Float(i)*Constants::piHalf()) + Float(j)*segmentAngleIncrement;
if(j != 0) _indices.insert(_indices.end(), {UnsignedInt(_positions.size()-4), UnsignedInt(_positions.size())});
_positions.emplace_back(Math::sin(segmentAngle), y, Math::cos(segmentAngle));
}

2
src/Magnum/Primitives/UVSphere.cpp

@ -47,7 +47,7 @@ Trade::MeshData3D UVSphere::solid(UnsignedInt rings, UnsignedInt segments, Textu
sphere.capVertex(-1.0f, -1.0f, 0.0f);
/* Vertex rings */
sphere.hemisphereVertexRings(rings-1, 0.0f, -Rad(Constants::pi())/2+ringAngleIncrement, ringAngleIncrement, textureCoordsVIncrement, textureCoordsVIncrement);
sphere.hemisphereVertexRings(rings-1, 0.0f, -Rad(Constants::piHalf())+ringAngleIncrement, ringAngleIncrement, textureCoordsVIncrement, textureCoordsVIncrement);
/* Top cap vertex */
sphere.capVertex(1.0f, 1.0f, 1.0f);

3
src/Magnum/SceneGraph/Animable.hpp

@ -30,12 +30,13 @@
*/
#include "Magnum/Timeline.h"
#include "Magnum/Math/Constants.h"
#include "Magnum/SceneGraph/AnimableGroup.h"
#include "Magnum/SceneGraph/Animable.h"
namespace Magnum { namespace SceneGraph {
template<UnsignedInt dimensions, class T> Animable<dimensions, T>::Animable(AbstractObject<dimensions, T>& object, AnimableGroup<dimensions, T>* group): AbstractGroupedFeature<dimensions, Animable<dimensions, T>, T>(object, group), _duration(0.0f), startTime(std::numeric_limits<Float>::infinity()), pauseTime(-std::numeric_limits<Float>::infinity()), previousState(AnimationState::Stopped), currentState(AnimationState::Stopped), _repeated(false), _repeatCount(0), repeats(0) {}
template<UnsignedInt dimensions, class T> Animable<dimensions, T>::Animable(AbstractObject<dimensions, T>& object, AnimableGroup<dimensions, T>* group): AbstractGroupedFeature<dimensions, Animable<dimensions, T>, T>(object, group), _duration(0.0f), startTime(Constants::inf()), pauseTime(-Constants::inf()), previousState(AnimationState::Stopped), currentState(AnimationState::Stopped), _repeated(false), _repeatCount(0), repeats(0) {}
template<UnsignedInt dimensions, class T> Animable<dimensions, T>::~Animable() {}

4
src/Magnum/Shapes/Plane.cpp

@ -25,8 +25,6 @@
#include "Plane.h"
#include <limits>
#include "Magnum/Math/Matrix4.h"
#include "Magnum/Math/Geometry/Intersection.h"
#include "Magnum/Shapes/LineSegment.h"
@ -45,7 +43,7 @@ Plane Plane::transformed(const Matrix4& matrix) const {
bool Plane::operator%(const Line3D& other) const {
Float t = Intersection::planeLine(_position, _normal, other.a(), other.b()-other.a());
return t != t || (t != std::numeric_limits<Float>::infinity() && t != -std::numeric_limits<Float>::infinity());
return t != t || (t != Constants::inf() && t != -Constants::inf());
}
bool Plane::operator%(const LineSegment3D& other) const {

Loading…
Cancel
Save