geom/sample: prefer inline impl over ipp files

This commit is contained in:
Danny Robson 2017-08-29 12:21:03 +10:00
parent a91df8895d
commit 2cb5fd7391
5 changed files with 37 additions and 55 deletions

View File

@ -245,7 +245,6 @@ list (
geom/rect.cpp geom/rect.cpp
geom/rect.hpp geom/rect.hpp
geom/sample.hpp geom/sample.hpp
geom/sample.ipp
geom/sphere.cpp geom/sphere.cpp
geom/sphere.hpp geom/sphere.hpp
geom/tri.cpp geom/tri.cpp

View File

@ -11,12 +11,13 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015-2017 Danny Robson <danny@nerdcruft.net>
*/ */
#include "ellipse.hpp" #include "./ellipse.hpp"
#include "ops.hpp" #include "./ops.hpp"
#include "./aabb.hpp"
using util::geom::ellipse; using util::geom::ellipse;

View File

@ -17,7 +17,7 @@
#ifndef __UTIL_GEOM_OPS_HPP #ifndef __UTIL_GEOM_OPS_HPP
#define __UTIL_GEOM_OPS_HPP #define __UTIL_GEOM_OPS_HPP
#include "aabb.hpp" #include "./fwd.hpp"
#include "../point.hpp" #include "../point.hpp"

View File

@ -11,17 +11,32 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015-2017 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_GEOM_SAMPLE_HPP #ifndef __UTIL_GEOM_SAMPLE_HPP
#define __UTIL_GEOM_SAMPLE_HPP #define __UTIL_GEOM_SAMPLE_HPP
#include "../point.hpp" #include "../coord/fwd.hpp"
#include "./ops.hpp"
#include <cstddef>
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
namespace util::geom { namespace util::geom {
/// a function object that selects a uniformly random point inside a shape
/// using a provided random generator. the point will lie within the shape,
/// inclusive of boundaries.
///
/// may be specialised for arbitrary shapes but uses rejection sampling
/// as a safe default. this implies that execution may not take a constant
/// time.
///
/// \tparam S coordinate type dimension
/// \tparam T value type for the shape/coordinate
/// \tparam K the shape type to test
/// \tparam G a UniformRandomBitGenerator, eg std::min19937
template < template <
size_t S, size_t S,
typename T, typename T,
@ -29,9 +44,23 @@ namespace util::geom {
typename G typename G
> >
struct sampler { struct sampler {
static point<S,T> fn (K<S,T>, G&); static point<S,T>
fn (K<S,T> k, G &g)
{
auto b = bounds (k);
while (true) {
auto p = sample (b, g);
if (intersects (k, p))
return p;
}
}
}; };
///////////////////////////////////////////////////////////////////////////
/// a convenience function that calls sample::fn to select a random point
/// in a provided shape.
template < template <
size_t S, size_t S,
typename T, typename T,
@ -45,6 +74,4 @@ namespace util::geom {
} }
} }
#include "sample.ipp"
#endif #endif

View File

@ -1,45 +0,0 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#if defined(__UTIL_GEOM_SAMPLE_IPP)
#error
#endif
#define __UTIL_GEOM_SAMPLE_IPP
#include "ops.hpp"
namespace util::geom {
// use rejection sampling by default
template <
size_t S,
typename T,
template <size_t,typename> class K,
typename G
>
point<S,T>
sampler<S,T,K,G>::fn (K<S,T> k, G &g)
{
auto b = bounds (k);
while (true) {
auto p = sample (b, g);
if (intersects (k, p))
return p;
}
}
}