geom: add more primitives

This commit is contained in:
Danny Robson 2015-10-14 15:32:53 +11:00
parent c9d5014e8a
commit ac06282f03
17 changed files with 544 additions and 5 deletions

View File

@ -57,14 +57,27 @@ UTIL_FILES = \
geom/fwd.hpp \
geom/aabb.cpp \
geom/aabb.hpp \
geom/aabb.ipp \
geom/cylinder.cpp \
geom/cylinder.hpp \
geom/ellipse.cpp \
geom/ellipse.hpp \
geom/ellipse.ipp \
geom/iostream.cpp \
geom/iostream.hpp \
geom/ops.hpp \
geom/plane.cpp \
geom/plane.hpp \
geom/ray.cpp \
geom/ray.hpp \
geom/rect.cpp \
geom/rect.hpp \
geom/sample.hpp \
geom/sample.ipp \
geom/sphere.cpp \
geom/sphere.hpp \
geom/tri.cpp \
geom/tri.hpp \
guid.cpp \
guid.hpp \
hash.hpp \

View File

@ -69,4 +69,6 @@ namespace util { namespace geom {
std::ostream& operator<< (std::ostream&, AABB<S,T>);
} }
#include "aabb.ipp"
#endif

40
geom/aabb.ipp Normal file
View File

@ -0,0 +1,40 @@
/*
* 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_AABB_IPP)
#error
#endif
#define __UTIL_GEOM_AABB_IPP
#include "sample.hpp"
namespace util { namespace geom {
template <size_t S, typename T, typename G>
struct sampler<S,T,AABB,G> {
static point<S,T>
fn (AABB<S,T> b, G &g)
{
std::uniform_real_distribution<T> d;
point<S,T> p;
std::generate (p.begin (), p.end (), [&] (void) { return d (g); });
return p * (b.p1 - b.p0) + b.p0.template as<util::vector> ();
}
};
} }

44
geom/cylinder.cpp Normal file
View File

@ -0,0 +1,44 @@
/*
* 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>
*/
#include "cylinder.hpp"
using util::geom::cylinder;
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
bool
cylinder<S,T>::includes (util::point<S,T> p) const
{
auto ab = b - a;
auto pa = p - a;
auto l = dot (ab, pa);
if (l < 0 || l > ab.magnitude2 ())
return false;
auto d = dot (pa, pa) - l * l * ab.magnitude2 ();
if (d > radius * radius)
return false;
return true; // d
}
///////////////////////////////////////////////////////////////////////////////
template struct util::geom::cylinder<2,float>;
template struct util::geom::cylinder<3,float>;

35
geom/cylinder.hpp Normal file
View File

@ -0,0 +1,35 @@
/*
* 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>
*/
#ifndef __UTIL_GEOM_CYLINDER_HPP
#define __UTIL_GEOM_CYLINDER_HPP
#include "../point.hpp"
namespace util { namespace geom {
///////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
struct cylinder {
util::point<S,T> a, b;
T radius;
float distance (util::point<S,T>) const;
bool includes (util::point<S,T>) const;
};
} }
#endif

71
geom/ellipse.cpp Normal file
View File

@ -0,0 +1,71 @@
/*
* 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>
*/
#include "ellipse.hpp"
#include "ops.hpp"
using util::geom::ellipse;
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
static bool
intersects (ellipse<S,T> e, util::point<S,T> p)
{
auto mag = (p - e.origin) * (p - e.origin) / (e.radius * e.radius);
return std::accumulate (mag.begin (), mag.end (), 0) <= 1;
}
//-----------------------------------------------------------------------------
template <size_t S, typename T, template <size_t,typename> class A, template <size_t,typename> class B>
bool
util::geom::intersects (A<S,T> a, B<S,T> b)
{
return ::intersects (a, b);
}
//-----------------------------------------------------------------------------
template bool util::geom::intersects (ellipse<2,float>, util::point<2,float>);
template bool util::geom::intersects (ellipse<3,float>, util::point<3,float>);
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
static util::geom::AABB<S,T>
bounds (ellipse<S,T> e)
{
return {
e.origin - e.radius,
e.origin + e.radius
};
}
//-----------------------------------------------------------------------------
template <size_t S, typename T, template <size_t,typename> class K>
util::geom::AABB<S,T>
util::geom::bounds (K<S,T> k)
{
return ::bounds (k);
}
//-----------------------------------------------------------------------------
template util::geom::AABB<2,float> util::geom::bounds (ellipse<2,float>);
template util::geom::AABB<3,float> util::geom::bounds (ellipse<3,float>);

36
geom/ellipse.hpp Normal file
View File

@ -0,0 +1,36 @@
/*
* 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>
*/
#ifndef __UTIL_GEOM_ELLIPSE_HPP
#define __UTIL_GEOM_ELLIPSE_HPP
#include <cstdlib>
#include "../point.hpp"
#include "../vector.hpp"
namespace util { namespace geom {
///////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
struct ellipse {
util::point<S,T> origin;
util::vector<S,T> radius;
};
} }
#include "ellipse.ipp"
#endif

46
geom/ellipse.ipp Normal file
View File

@ -0,0 +1,46 @@
/*
* 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_ELLIPSE_IPP)
#error
#endif
#define __UTIL_GEOM_ELLIPSE_IPP
#include "sample.hpp"
#include <cmath>
#include <random>
///////////////////////////////////////////////////////////////////////////////
namespace util { namespace geom {
template <typename T, template <size_t,typename> class K, typename G>
struct sampler<2,T,K,G>
{
static util::point<2,T> fn (K<2,T> k, G &g)
{
std::uniform_real_distribution<T> dist;
float phi = dist (g) * 2 * PI<T>;
float rho = std::sqrt (dist (g));
return util::point<2,T> {
std::cos (phi),
std::sin (phi)
} * rho * k.radius + k.origin.template as<util::vector> ();
}
};
} }

92
geom/ops.hpp Normal file
View File

@ -0,0 +1,92 @@
/*
* 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>
*/
#ifndef __UTIL_GEOM_OPS_HPP
#define __UTIL_GEOM_OPS_HPP
#include "aabb.hpp"
#include "../point.hpp"
namespace util { namespace geom {
template <
size_t S,
typename T,
template <size_t,typename> class A,
template <size_t,typename> class B
>
bool
intersects (A<S,T>, B<S,T>);
template <
size_t S,
typename T,
template <size_t,typename> class A,
template <size_t,typename> class B
>
T
distance2 (A<S,T>, B<S,T>);
template <
size_t S,
typename T,
template <size_t,typename> class A,
template <size_t,typename> class B
>
T
distance (A<S,T>, B<S,T>);
template <
size_t S,
typename T,
template <size_t,typename> class K
>
AABB<S,T>
bounds (K<S,T>);
template <
size_t S,
typename T,
template <size_t,typename> class K
>
T
diameter (K<S,T>);
template <
size_t S,
typename T,
template <size_t,typename> class K
>
point<S,T>
closest (K<S,T>, point<S,T>);
template <
size_t S,
typename T,
template <size_t,typename> class K
>
vector<S,T>
magnitude (K<S,T>);
template <
size_t S,
typename T,
template <size_t,typename> class K
>
K<S,T>
scale (K<S,T>, T);
} }
#endif

View File

@ -17,8 +17,8 @@
#ifndef __UTIL_PLANE_HPP
#define __UTIL_PLANE_HPP
#include "point.hpp"
#include "vector.hpp"
#include "../point.hpp"
#include "../vector.hpp"
namespace util { namespace geom {
template <size_t S, typename T>

View File

@ -17,12 +17,11 @@
#ifndef __UTIL_GEOM_RAY_HPP
#define __UTIL_GEOM_RAY_HPP
#include "point.hpp"
#include "aabb.hpp"
#include "plane.hpp"
#include "sphere.hpp"
#include "vector.hpp"
#include "../vector.hpp"
#include "../point.hpp"
#include <iostream>

17
geom/rect.cpp Normal file
View File

@ -0,0 +1,17 @@
/*
* 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>
*/

17
geom/rect.hpp Normal file
View File

@ -0,0 +1,17 @@
/*
* 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>
*/

48
geom/sample.hpp Normal file
View File

@ -0,0 +1,48 @@
/*
* 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>
*/
#ifndef __UTIL_GEOM_SAMPLE_HPP
#define __UTIL_GEOM_SAMPLE_HPP
#include "../point.hpp"
namespace util { namespace geom {
template <
size_t S,
typename T,
template <size_t,typename> class K,
typename G
>
struct sampler {
static point<S,T> fn (K<S,T>, G&);
};
template <
size_t S,
typename T,
template <size_t,typename> class K,
typename G // random generator
>
point<S,T>
sample (K<S,T> k, G &g)
{
return sampler<S,T,K,G>::fn (k, g);
}
} }
#include "sample.ipp"
#endif

45
geom/sample.ipp Normal file
View File

@ -0,0 +1,45 @@
/*
* 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 { namespace 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;
}
}
} }

17
geom/tri.cpp Normal file
View File

@ -0,0 +1,17 @@
/*
* 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>
*/

17
geom/tri.hpp Normal file
View File

@ -0,0 +1,17 @@
/*
* 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>
*/