Browse Source

doc: compiling Types page code snippets.

pull/233/head
Vladimír Vondruš 8 years ago
parent
commit
901339eb75
  1. 84
      doc/snippets/MagnumMath.cpp
  2. 57
      doc/types.dox

84
doc/snippets/MagnumMath.cpp

@ -27,6 +27,7 @@
#include "Magnum/Math/Color.h" #include "Magnum/Math/Color.h"
#include "Magnum/Math/DualComplex.h" #include "Magnum/Math/DualComplex.h"
#include "Magnum/Math/DualQuaternion.h" #include "Magnum/Math/DualQuaternion.h"
#include "Magnum/Math/Half.h"
#include "Magnum/Math/Algorithms/GramSchmidt.h" #include "Magnum/Math/Algorithms/GramSchmidt.h"
using namespace Magnum; using namespace Magnum;
@ -509,4 +510,87 @@ transformation = transformation.normalized();
/* [transformations-normalization-quat] */ /* [transformations-normalization-quat] */
} }
{
/* [types-literals-colors] */
using namespace Math::Literals;
Color3 a = 0x33b27f_srgbf; // {0.0331048f, 0.445201f, 0.212231f}
Color4ub b = 0x33b27fcc_rgba; // {0x33, 0xb2, 0x7f, 0xcc}
/* [types-literals-colors] */
static_cast<void>(a);
static_cast<void>(b);
}
{
/* [types-literals-angles] */
using namespace Math::Literals;
//Deg a = 60.0f // error, no implicit conversion from Float
Deg a = 60.0_degf; // okay
Float b = 3.2831853f;
auto tau = Rad{b} + 3.0_radf;
Radd pi = 3.141592653589793_rad;
//Double c = pi; // error, no implicit conversion to Double
auto c = Double(pi); // okay
/* [types-literals-angles] */
static_cast<void>(a);
static_cast<void>(tau);
static_cast<void>(c);
/* [types-literals-angle-conversion] */
Rad d = 60.0_degf; // 1.0471976f
auto e = Degd{pi}; // 180.0
//Rad f = pi; // error, no implicit conversion of underlying types
auto f = Rad{pi}; // 3.141592654f
/* [types-literals-angle-conversion] */
static_cast<void>(d);
static_cast<void>(e);
static_cast<void>(f);
}
{
/* [types-literals-usage] */
Float a = Math::sin(1.32457_radf);
Complex b = Complex::rotation(60.0_degf);
/* [types-literals-usage] */
static_cast<void>(a);
static_cast<void>(b);
}
{
/* [types-literals-half] */
using namespace Math::Literals;
Half a = 3.5_h; // 0x4300 internally
/* [types-literals-half] */
static_cast<void>(a);
}
{
bool orthographic = false;
/* [types-literals-init] */
/* These are equivalent */
Vector3 a1;
Vector3 a2{Math::ZeroInit};
/* These too */
Quaternion q1;
Quaternion q2{Math::IdentityInit};
/* Avoid unnecessary initialization if is overwritten anyway */
Matrix4 projection{Math::NoInit};
if(orthographic)
projection = Matrix4::orthographicProjection({4.0f, 3.0f}, 0.1f, 100.0f);
else
projection = Matrix4::perspectiveProjection(35.0_degf, 1.33f, 0.1f, 100.0f);
/* [types-literals-init] */
static_cast<void>(a1);
static_cast<void>(a2);
static_cast<void>(q1);
static_cast<void>(q2);
}
} }

57
doc/types.dox

@ -102,12 +102,7 @@ For easier entering of (s)RGB colors in hexadecimal format there are
@ref Math::Literals namespace. See their documentation for more information @ref Math::Literals namespace. See their documentation for more information
about the differences. about the differences.
@code{.cpp} @snippet MagnumMath.cpp types-literals-colors
using namespace Math::Literals;
Color3 a = 0x33b27f_srgbf; // {0.0331048f, 0.445201f, 0.212231f}
Color4ub a = 0x33b27fcc_rgba; // {0x33, 0xb2, 0x7f, 0xcc}
@endcode
@section types-binary Binary representation @section types-binary Binary representation
@ -141,41 +136,20 @@ or use custom @link Math::Literals::operator""_degf() _degf @endlink /
/ @link Math::Literals::operator""_rad() _rad @endlink literals that are / @link Math::Literals::operator""_rad() _rad @endlink literals that are
provided in the @ref Math::Literals namespace: provided in the @ref Math::Literals namespace:
@code{.cpp} @snippet MagnumMath.cpp types-literals-angles
using namespace Math::Literals;
//Deg a = 60.0f // error, no implicit conversion from Float
Deg a = 60.0_degf; // okay
Float b = 3.2831853f;
auto tau = Rad{b} + 3.0_radf;
Radd pi = 3.141592653589793_rad;
//Double c = pi; // error, no implicit conversion to Double
auto c = Double{pi}; // okay
@endcode
They can be implicitly converted to each other, but conversion to different They can be implicitly converted to each other, but conversion to different
underlying type is *explicit* to avoid precision loss (or, on the other hand, underlying type is *explicit* to avoid precision loss (or, on the other hand,
unnecessarily high precision) during computations: unnecessarily high precision) during computations:
@code{.cpp} @snippet MagnumMath.cpp types-literals-angle-conversion
Rad d = 60.0_degf; // 1.0471976f
auto e = Degd{pi}; // 180.0
//Rad f = pi; // error, no implicit conversion of underlying types
auto f = Rad{pi}; // 3.141592654f
@endcode
These classes are used exclusively in all functions taking and returning angles These classes are used exclusively in all functions taking and returning angles
--- trigonometry, angle computation, rotating transformation etc. Thanks to --- trigonometry, angle computation, rotating transformation etc. Thanks to
implicit conversion you can seamlessly use either radians or degrees without implicit conversion you can seamlessly use either radians or degrees without
any need to care about what input the function expects: any need to care about what input the function expects:
@code{.cpp} @snippet MagnumMath.cpp types-literals-usage
Float a = Math::sin(1.32457_radf);
Complex b = Complex::rotation(60.0_degf);
@endcode
There is also a @ref Half type for handling half-precision floating point There is also a @ref Half type for handling half-precision floating point
values. By design it doesn't support any arithmetic operations as they would be values. By design it doesn't support any arithmetic operations as they would be
@ -185,11 +159,7 @@ conversion operators from/to @ref Float and @ref UnsignedShort and you can also
use the @link Math::Literals::operator""_h() _h @endlink literal that is use the @link Math::Literals::operator""_h() _h @endlink literal that is
provided in the @ref Math::Literals namespace: provided in the @ref Math::Literals namespace:
@code{.cpp} @snippet MagnumMath.cpp types-literals-half
using namespace Math::Literals;
Half a = 3.5_h; // 0x4300 internally
@endcode
@section types-other Other types @section types-other Other types
@ -229,21 +199,6 @@ explicit:
Example: Example:
@code{.cpp} @snippet MagnumMath.cpp types-literals-init
// These are equivalent
Vector3 a1;
Vector3 a1{Math::ZeroInit};
// These too
Quaternion q;
Quaternion q{Math::IdentityInit};
// Avoid unnecessary initialization if is overwritten anyway
Matrix4 projection{Math::NoInit};
if(orthographic)
projection = Matrix4::orthographicProjection(...);
else
projection = Matrix4::perspectiveProjection(...);
@endcode
*/ */
} }

Loading…
Cancel
Save