From 032827fc3d67b36b2ac7809f542ba0ba6b7f6e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 30 Mar 2013 19:18:08 +0100 Subject: [PATCH] Primitives: added 2D circle primitive. --- src/Primitives/CMakeLists.txt | 2 + src/Primitives/Circle.cpp | 66 +++++++++++++++++++++++++++ src/Primitives/Circle.h | 65 +++++++++++++++++++++++++++ src/Primitives/Test/CMakeLists.txt | 3 +- src/Primitives/Test/CircleTest.cpp | 71 ++++++++++++++++++++++++++++++ 5 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 src/Primitives/Circle.cpp create mode 100644 src/Primitives/Circle.h create mode 100644 src/Primitives/Test/CircleTest.cpp diff --git a/src/Primitives/CMakeLists.txt b/src/Primitives/CMakeLists.txt index a3c2efd95..24b978fa5 100644 --- a/src/Primitives/CMakeLists.txt +++ b/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 diff --git a/src/Primitives/Circle.cpp b/src/Primitives/Circle.cpp new file mode 100644 index 000000000..170fcef89 --- /dev/null +++ b/src/Primitives/Circle.cpp @@ -0,0 +1,66 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš + + 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; + 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; + 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}, {}); +} + +}} diff --git a/src/Primitives/Circle.h b/src/Primitives/Circle.h new file mode 100644 index 000000000..a27876e31 --- /dev/null +++ b/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š + + 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 diff --git a/src/Primitives/Test/CMakeLists.txt b/src/Primitives/Test/CMakeLists.txt index 987d1e01e..b12e72032 100644 --- a/src/Primitives/Test/CMakeLists.txt +++ b/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) diff --git a/src/Primitives/Test/CircleTest.cpp b/src/Primitives/Test/CircleTest.cpp new file mode 100644 index 000000000..da396522f --- /dev/null +++ b/src/Primitives/Test/CircleTest.cpp @@ -0,0 +1,71 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš + + 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 + +#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{ + { 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{ + { 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)