Browse Source

Primitives: added 2D circle primitive.

pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
032827fc3d
  1. 2
      src/Primitives/CMakeLists.txt
  2. 66
      src/Primitives/Circle.cpp
  3. 65
      src/Primitives/Circle.h
  4. 3
      src/Primitives/Test/CMakeLists.txt
  5. 71
      src/Primitives/Test/CircleTest.cpp

2
src/Primitives/CMakeLists.txt

@ -24,6 +24,7 @@
set(MagnumPrimitives_SRCS
Capsule.cpp
Circle.cpp
Crosshair.cpp
Cube.cpp
Cylinder.cpp
@ -34,6 +35,7 @@ set(MagnumPrimitives_SRCS
set(MagnumPrimitives_HEADERS
Capsule.h
Circle.h
Crosshair.h
Cube.h
Cylinder.h

66
src/Primitives/Circle.cpp

@ -0,0 +1,66 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "Circle.h"
#include "Math/Functions.h"
#include "Math/Vector2.h"
#include "Trade/MeshData2D.h"
namespace Magnum { namespace Primitives {
Trade::MeshData2D Circle::solid(UnsignedInt segments) {
CORRADE_ASSERT(segments >= 3, "Primitives::Circle::solid(): segments must be >= 3",
Trade::MeshData2D(Mesh::Primitive::TriangleFan, nullptr, {}, {}));
auto positions = new std::vector<Vector2>;
positions->reserve(segments+1);
/* Central point */
positions->emplace_back();
/* Points on circle */
const Rad angleIncrement(2*Constants::pi()/segments);
for(UnsignedInt i = 0; i != segments; ++i)
positions->emplace_back(Math::cos(i*angleIncrement), Math::sin(i*angleIncrement));
return Trade::MeshData2D(Mesh::Primitive::TriangleFan, nullptr, {positions}, {});
}
Trade::MeshData2D Circle::wireframe(UnsignedInt segments) {
CORRADE_ASSERT(segments >= 3, "Primitives::Circle::wireframe(): segments must be >= 3",
Trade::MeshData2D(Mesh::Primitive::LineLoop, nullptr, {}, {}));
auto positions = new std::vector<Vector2>;
positions->reserve(segments);
/* Points on circle */
const Rad angleIncrement(2*Constants::pi()/segments);
for(UnsignedInt i = 0; i != segments; ++i)
positions->emplace_back(Math::cos(i*angleIncrement), Math::sin(i*angleIncrement));
return Trade::MeshData2D(Mesh::Primitive::LineLoop, nullptr, {positions}, {});
}
}}

65
src/Primitives/Circle.h

@ -0,0 +1,65 @@
#ifndef Magnum_Primitives_Circle_h
#define Magnum_Primitives_Circle_h
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/** @file
* @brief Class Magnum::Primitives::Circle
*/
#include "Trade/Trade.h"
#include "Primitives/magnumPrimitivesVisibility.h"
namespace Magnum { namespace Primitives {
/**
@brief 2D circle primitive
Circle with radius 1.
*/
class MAGNUM_PRIMITIVES_EXPORT Circle {
public:
/**
* @brief Solid circle
* @param segments Number of segments. Must be greater or equal to 3.
*
* Non-indexed @ref Mesh::Primitive "TriangleFan".
*/
static Trade::MeshData2D solid(UnsignedInt segments);
/**
* @brief Wireframe circle
* @param segments Number of segments. Must be greater or equal to 3.
*
* Non-indexed @ref Mesh::Primitive "LineLoop".
*/
static Trade::MeshData2D wireframe(UnsignedInt segments);
Circle() = delete;
};
}}
#endif

3
src/Primitives/Test/CMakeLists.txt

@ -23,5 +23,6 @@
#
corrade_add_test(PrimitivesCapsuleTest CapsuleTest.cpp LIBRARIES MagnumPrimitives)
corrade_add_test(PrimitivesUVSphereTest UVSphereTest.cpp LIBRARIES MagnumPrimitives)
corrade_add_test(PrimitivesCircleTest CircleTest.cpp LIBRARIES MagnumPrimitives)
corrade_add_test(PrimitivesCylinderTest CylinderTest.cpp LIBRARIES MagnumPrimitives)
corrade_add_test(PrimitivesUVSphereTest UVSphereTest.cpp LIBRARIES MagnumPrimitives)

71
src/Primitives/Test/CircleTest.cpp

@ -0,0 +1,71 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <TestSuite/Tester.h>
#include "Math/Vector2.h"
#include "Primitives/Circle.h"
#include "Trade/MeshData2D.h"
namespace Magnum { namespace Primitives { namespace Test {
class CircleTest: public Corrade::TestSuite::Tester {
public:
explicit CircleTest();
void solid();
void wireframe();
};
CircleTest::CircleTest() {
addTests({&CircleTest::solid,
&CircleTest::wireframe});
}
void CircleTest::solid() {
Trade::MeshData2D circle = Primitives::Circle::solid(8);
CORRADE_COMPARE(*circle.positions(0), (std::vector<Vector2>{
{ 0.0f, 0.0f},
{ 1.0f, 0.0f}, { Constants::sqrt2()/2.0f, Constants::sqrt2()/2.0f},
{ 0.0f, 1.0f}, {-Constants::sqrt2()/2.0f, Constants::sqrt2()/2.0f},
{-1.0f, 0.0f}, {-Constants::sqrt2()/2.0f, -Constants::sqrt2()/2.0f},
{ 0.0f, -1.0f}, { Constants::sqrt2()/2.0f, -Constants::sqrt2()/2.0f}
}));
}
void CircleTest::wireframe() {
Trade::MeshData2D circle = Primitives::Circle::wireframe(8);
CORRADE_COMPARE(*circle.positions(0), (std::vector<Vector2>{
{ 1.0f, 0.0f}, { Constants::sqrt2()/2.0f, Constants::sqrt2()/2.0f},
{ 0.0f, 1.0f}, {-Constants::sqrt2()/2.0f, Constants::sqrt2()/2.0f},
{-1.0f, 0.0f}, {-Constants::sqrt2()/2.0f, -Constants::sqrt2()/2.0f},
{ 0.0f, -1.0f}, { Constants::sqrt2()/2.0f, -Constants::sqrt2()/2.0f}
}));
}
}}}
CORRADE_TEST_MAIN(Magnum::Primitives::Test::CircleTest)
Loading…
Cancel
Save