geom: move geometry primitives to own namespace

This commit is contained in:
Danny Robson 2015-10-13 18:19:47 +11:00
parent 51b8ef762a
commit 20bafa2cfb
15 changed files with 166 additions and 89 deletions

View File

@ -9,8 +9,6 @@ AM_CXXFLAGS = $(BOOST_CPPFLAGS) $(ZLIB_CFLAGS)
## Source definitions ## Source definitions
UTIL_FILES = \ UTIL_FILES = \
aabb.cpp \
aabb.hpp \
backtrace.hpp \ backtrace.hpp \
bezier.cpp \ bezier.cpp \
bezier.hpp \ bezier.hpp \
@ -56,6 +54,17 @@ UTIL_FILES = \
format.ipp \ format.ipp \
fourcc.cpp \ fourcc.cpp \
fourcc.hpp \ fourcc.hpp \
geom/fwd.hpp \
geom/aabb.cpp \
geom/aabb.hpp \
geom/iostream.cpp \
geom/iostream.hpp \
geom/plane.cpp \
geom/plane.hpp \
geom/ray.cpp \
geom/ray.hpp \
geom/sphere.cpp \
geom/sphere.hpp \
guid.cpp \ guid.cpp \
guid.hpp \ guid.hpp \
hash.hpp \ hash.hpp \
@ -196,8 +205,6 @@ UTIL_FILES = \
noise/turbulence.ipp \ noise/turbulence.ipp \
pascal.cpp \ pascal.cpp \
pascal.hpp \ pascal.hpp \
plane.cpp \
plane.hpp \
platform.hpp \ platform.hpp \
point.cpp \ point.cpp \
point.hpp \ point.hpp \
@ -221,8 +228,6 @@ UTIL_FILES = \
rational.cpp \ rational.cpp \
rational.hpp \ rational.hpp \
rational.ipp \ rational.ipp \
ray.cpp \
ray.hpp \
region.cpp \ region.cpp \
region.hpp \ region.hpp \
region.ipp \ region.ipp \
@ -231,8 +236,6 @@ UTIL_FILES = \
signal.hpp \ signal.hpp \
signal.ipp \ signal.ipp \
si.hpp \ si.hpp \
sphere.cpp \
sphere.hpp \
stats.cpp \ stats.cpp \
stats.hpp \ stats.hpp \
stream.cpp \ stream.cpp \
@ -352,7 +355,6 @@ AM_LDFLAGS += $(BOOST_LDFLAGS) $(BOOST_FILESYSTEM_LIB) $(BOOST_SYSTEM_LIB)
AM_CXXFLAGS += -I$(top_srcdir) AM_CXXFLAGS += -I$(top_srcdir)
TEST_BIN = \ TEST_BIN = \
test/aabb \
test/backtrace \ test/backtrace \
test/bezier \ test/bezier \
test/bitwise \ test/bitwise \
@ -368,6 +370,8 @@ TEST_BIN = \
test/fixed \ test/fixed \
test/float \ test/float \
test/format \ test/format \
test/geom/aabb \
test/geom/ray \
test/hash/murmur \ test/hash/murmur \
test/hash/fasthash \ test/hash/fasthash \
test/hmac \ test/hmac \
@ -377,7 +381,6 @@ TEST_BIN = \
test/introspection \ test/introspection \
test/ip \ test/ip \
test/json_types \ test/json_types \
test/ray \
test/maths \ test/maths \
test/matrix \ test/matrix \
test/md2 \ test/md2 \

View File

@ -18,7 +18,7 @@
#include "aabb.hpp" #include "aabb.hpp"
#include "debug.hpp" #include "debug.hpp"
using util::AABB; using util::geom::AABB;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
@ -223,7 +223,7 @@ namespace debug {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
std::ostream& std::ostream&
util::operator<< (std::ostream &os, AABB<S,T> b) util::geom::operator<< (std::ostream &os, util::geom::AABB<S,T> b)
{ {
os << "AABB(" << b.p0 << ", " << b.p1 << ")"; os << "AABB(" << b.p0 << ", " << b.p1 << ")";
return os; return os;
@ -232,9 +232,9 @@ util::operator<< (std::ostream &os, AABB<S,T> b)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#define INSTANTIATE_S_T(S,T) \ #define INSTANTIATE_S_T(S,T) \
namespace util { template struct AABB<S,T>; } \ namespace util { namespace geom { template struct AABB<S,T>; } } \
template bool debug::valid (const AABB<S,T>&); \ template bool debug::valid (const AABB<S,T>&); \
template std::ostream& util::operator<< (std::ostream&, AABB<S,T>); template std::ostream& util::geom::operator<< (std::ostream&, AABB<S,T>);
#define INSTANTIATE(T) \ #define INSTANTIATE(T) \
INSTANTIATE_S_T(2,T) \ INSTANTIATE_S_T(2,T) \

View File

@ -15,16 +15,15 @@
*/ */
#ifndef __UTIL_AABB_HPP #ifndef __UTIL_GEOM_AABB_HPP
#define __UTIL_AABB_HPP #define __UTIL_GEOM_AABB_HPP
#include "point.hpp" #include "../point.hpp"
#include "extent.hpp" #include "../extent.hpp"
#include <cstdint> #include <cstdint>
#include <iostream>
namespace util { namespace util { namespace geom {
template <size_t S, typename T> template <size_t S, typename T>
struct AABB { struct AABB {
AABB () = default; AABB () = default;
@ -68,6 +67,6 @@ namespace util {
template <size_t S, typename T> template <size_t S, typename T>
std::ostream& operator<< (std::ostream&, AABB<S,T>); std::ostream& operator<< (std::ostream&, AABB<S,T>);
} } }
#endif #endif

33
geom/fwd.hpp Normal file
View File

@ -0,0 +1,33 @@
/*
* 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_FWD_HPP
#define __UTIL_GEOM_FWD_HPP
#include <cstdlib>
namespace util { namespace geom {
template <size_t S, typename T> struct ray;
template <size_t S, typename T> struct plane;
template <size_t S, typename T> struct AABB;
template <size_t S, typename T> struct sphere;
template <size_t S, typename T> struct ellipse;
template <size_t S, typename T> struct rect;
template <size_t S, typename T> struct cylinder;
template <size_t S, typename T> struct tri;
} }
#endif

View File

@ -14,32 +14,20 @@
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/ */
#include "sphere.hpp" #include "./iostream.hpp"
using util::sphere; #include "../geom/sphere.hpp"
//-----------------------------------------------------------------------------
template <size_t S, typename T>
sphere<S,T>::sphere (point<S,T> _centre, T _radius):
centre (_centre),
radius (_radius)
{
CHECK_GE (_radius, 0);
}
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
std::ostream& std::ostream&
util::operator<< (std::ostream &os, sphere<S,T> s) util::geom::operator<< (std::ostream &os, util::geom::sphere<S,T> s)
{ {
return os << "sphere(" << s.centre << ',' << s.radius << ')'; return os << "sphere(" << s.centre << ',' << s.radius << ')';
} }
template std::ostream& util::operator<< (std::ostream&, sphere<2,float>);
template std::ostream& util::operator<< (std::ostream&, sphere<3,float>);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template struct util::sphere<2,float>; template std::ostream& util::geom::operator<< (std::ostream&, util::geom::sphere<2,float>);
template struct util::sphere<3,float>; template std::ostream& util::geom::operator<< (std::ostream&, util::geom::sphere<3,float>);

30
geom/iostream.hpp Normal file
View File

@ -0,0 +1,30 @@
/*
* 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_IOSTREAM_HPP
#define __UTIL_GEOM_IOSTREAM_HPP
#include "./fwd.hpp"
#include <iostream>
namespace util { namespace geom {
template <size_t S, typename T>
std::ostream&
operator<< (std::ostream&, sphere<S,T>);
} }
#endif

View File

@ -19,10 +19,13 @@
#include "debug.hpp" #include "debug.hpp"
using util::geom::plane;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
util::plane<S,T>::plane (util::point<S,T> _p, plane<S,T>::plane (point<S,T> _p,
util::vector<S,T> _n): vector<S,T> _n):
p (_p), p (_p),
n (_n) n (_n)
{ {
@ -31,5 +34,5 @@ util::plane<S,T>::plane (util::point<S,T> _p,
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template struct util::plane<2,float>; template struct util::geom::plane<2,float>;
template struct util::plane<3,float>; template struct util::geom::plane<3,float>;

View File

@ -20,7 +20,7 @@
#include "point.hpp" #include "point.hpp"
#include "vector.hpp" #include "vector.hpp"
namespace util { namespace util { namespace geom {
template <size_t S, typename T> template <size_t S, typename T>
struct plane { struct plane {
plane (util::point<S,T> p, plane (util::point<S,T> p,
@ -33,6 +33,6 @@ namespace util {
typedef plane<2,float> plane2f; typedef plane<2,float> plane2f;
typedef plane<3,float> plane3f; typedef plane<3,float> plane3f;
} } }
#endif #endif

View File

@ -16,9 +16,10 @@
#include "ray.hpp" #include "ray.hpp"
#include "debug.hpp" #include "ops.hpp"
#include "../debug.hpp"
using util::ray; using util::geom::ray;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -137,15 +138,15 @@ ray<S,T>::at (T t) const
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
std::ostream& std::ostream&
util::operator<< (std::ostream &os, ray<S,T> r) util::geom::operator<< (std::ostream &os, ray<S,T> r)
{ {
return os << "ray(" << r.origin << ',' << r.direction << ')'; return os << "ray(" << r.origin << ',' << r.direction << ')';
} }
template std::ostream& util::operator<< (std::ostream&, ray<3,float>); template std::ostream& util::geom::operator<< (std::ostream&, ray<3,float>);
template std::ostream& util::operator<< (std::ostream&, ray<3,double>); template std::ostream& util::geom::operator<< (std::ostream&, ray<3,double>);
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template struct util::ray<2,float>; template struct util::geom::ray<2,float>;
template struct util::ray<3,float>; template struct util::geom::ray<3,float>;

View File

@ -14,8 +14,8 @@
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_RAY_HPP #ifndef __UTIL_GEOM_RAY_HPP
#define __UTIL_RAY_HPP #define __UTIL_GEOM_RAY_HPP
#include "point.hpp" #include "point.hpp"
@ -26,7 +26,7 @@
#include <iostream> #include <iostream>
namespace util { namespace util { namespace geom {
template <size_t S, typename T> template <size_t S, typename T>
struct ray { struct ray {
ray (point<S,T> origin, ray (point<S,T> origin,
@ -56,6 +56,6 @@ namespace util {
typedef ray<2,float> ray2f; typedef ray<2,float> ray2f;
typedef ray<3,float> ray3f; typedef ray<3,float> ray3f;
} } }
#endif #endif

20
geom/sphere.cpp Normal file
View File

@ -0,0 +1,20 @@
/*
* 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 "sphere.hpp"
using util::geom::sphere;

View File

@ -14,27 +14,20 @@
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_SPHERE_HPP #ifndef __UTIL_GEOM_SPHERE_HPP
#define __UTIL_SPHERE_HPP #define __UTIL_GEOM_SPHERE_HPP
#include "point.hpp" #include "../point.hpp"
#include <iostream> namespace util { namespace geom {
namespace util {
template <size_t S, typename T> template <size_t S, typename T>
struct sphere { struct sphere {
sphere (point<S,T> centre, T radius);
point<S,T> centre; point<S,T> centre;
T radius; T radius;
}; };
template <size_t S, typename T>
std::ostream& operator<< (std::ostream&, sphere<S,T>);
typedef sphere<2,float> sphere2f; typedef sphere<2,float> sphere2f;
typedef sphere<3,float> sphere3f; typedef sphere<3,float> sphere3f;
} } }
#endif #endif

View File

@ -20,7 +20,6 @@
#define __UTIL_NOISE_BASIS_PATCH_IPP #define __UTIL_NOISE_BASIS_PATCH_IPP
#include "../../types.hpp" #include "../../types.hpp"
#include "../../ray.hpp"
#include "../../vector.hpp" #include "../../vector.hpp"

View File

@ -1,9 +1,11 @@
#include "aabb.hpp" #include "geom/aabb.hpp"
#include "tap.hpp" #include "tap.hpp"
#include <tuple> #include <tuple>
using util::geom::AABB2f;
int int
main (int, char**) main (int, char**)
@ -12,38 +14,38 @@ main (int, char**)
{ {
// Test contraction // Test contraction
util::AABB2f box { AABB2f box {
{ 2, 2 }, { 2, 2 },
{ 8, 8 } { 8, 8 }
}; };
box.contract (2.f); box.contract (2.f);
tap.expect_eq<util::AABB2f, util::AABB2f> (box, { { 3, 3 }, { 7, 7 }}, "over contraction"); tap.expect_eq<AABB2f, AABB2f> (box, { { 3, 3 }, { 7, 7 }}, "over contraction");
} }
{ {
// Test expansion // Test expansion
util::AABB2f box { AABB2f box {
{ 2, 2 }, { 2, 2 },
{ 8, 8 } { 8, 8 }
}; };
box.expand (2.f); box.expand (2.f);
tap.expect_eq<util::AABB2f, util::AABB2f> (box, { { 1, 1 }, { 9, 9 }}, "expansion"); tap.expect_eq<AABB2f, AABB2f> (box, { { 1, 1 }, { 9, 9 }}, "expansion");
} }
{ {
// Ensure we don't wrap-around on unsigned position types when contracting // Ensure we don't wrap-around on unsigned position types when contracting
util::AABB2f small { AABB2f small {
{ 0, 0 }, { 0, 0 },
{ 1, 1 } { 1, 1 }
}; };
small.contract (10); small.contract (10);
tap.expect_eq<util::AABB2f, util::AABB2f> (small, { { 0.5f, 0.5f }, { 0.5f, 0.5f }}, "unsigned over-contract"); tap.expect_eq<AABB2f, AABB2f> (small, { { 0.5f, 0.5f }, { 0.5f, 0.5f }}, "unsigned over-contract");
} }
} }

View File

@ -1,17 +1,19 @@
#include "aabb.hpp" #include "geom/aabb.hpp"
#include "debug.hpp" #include "geom/plane.hpp"
#include "plane.hpp" #include "geom/ray.hpp"
#include "ray.hpp"
#include "tap.hpp" #include "tap.hpp"
using util::geom::ray2f;
using util::geom::ray3f;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void void
test_intersect_plane (util::TAP::logger &tap) test_intersect_plane (util::TAP::logger &tap)
{ {
// trivial case: origin ray facing z, plane at unit z facing -z. // trivial case: origin ray facing z, plane at unit z facing -z.
const util::ray3f l ({0,0,0}, {0,0, 1}); const util::geom::ray3f l ({0,0,0}, {0,0, 1});
const util::plane3f p ({0,0,1}, {0,0,-1}); const util::geom::plane3f p ({0,0,1}, {0,0,-1});
tap.expect_eq (l.intersect (p), 1.f, "ray-plane intersect"); tap.expect_eq (l.intersect (p), 1.f, "ray-plane intersect");
} }
@ -21,20 +23,22 @@ test_intersect_plane (util::TAP::logger &tap)
void void
test_intersect_aabb (util::TAP::logger &tap) test_intersect_aabb (util::TAP::logger &tap)
{ {
using util::geom::AABB2f;
// trivial case: unit aabb at origin, ray from (0.5,-0.5) upwards // trivial case: unit aabb at origin, ray from (0.5,-0.5) upwards
const util::AABB2f box { const AABB2f box {
{ 0.f, 0.f }, { 0.f, 0.f },
{ 1.f, 1.f } { 1.f, 1.f }
}; };
const util::ray2f forward { const ray2f forward {
{ 0.5f, -0.5f }, { 0.5f, -0.5f },
{ 0.f, 1.f } { 0.f, 1.f }
}; };
tap.expect_eq (forward.intersect (box), 0.5f, "ray-aabb intersect"); tap.expect_eq (forward.intersect (box), 0.5f, "ray-aabb intersect");
const util::ray2f behind { const ray2f behind {
{ 0.5f, 2.f }, { 0.5f, 2.f },
{ 0.f, 1.f } { 0.f, 1.f }
}; };
@ -47,15 +51,17 @@ test_intersect_aabb (util::TAP::logger &tap)
void void
test_intersect_sphere (util::TAP::logger &tap) test_intersect_sphere (util::TAP::logger &tap)
{ {
const util::sphere3f s = {{0.f, 0.f, 0.f}, 1.f}; using util::geom::sphere3f;
const util::ray3f r0 {{0.f, 2.f, 0.f}, {0.f, -1.f, 0.f}}; const sphere3f s = {{0.f, 0.f, 0.f}, 1.f};
const ray3f r0 {{0.f, 2.f, 0.f}, {0.f, -1.f, 0.f}};
tap.expect_eq (r0.intersect (s), 1.f, "ray-sphere simple"); tap.expect_eq (r0.intersect (s), 1.f, "ray-sphere simple");
const util::ray3f r1 {{0.f, 1.f, 0.f}, {0.f, 1.f, 0.f}}; const ray3f r1 {{0.f, 1.f, 0.f}, {0.f, 1.f, 0.f}};
tap.expect_eq (r1.intersect (s), 0.f, "ray-sphere adjacent"); tap.expect_eq (r1.intersect (s), 0.f, "ray-sphere adjacent");
const util::ray3f r2 {{0.f, 2.f, 0.f}, {0.f, 1.f, 0.f}}; const ray3f r2 {{0.f, 2.f, 0.f}, {0.f, 1.f, 0.f}};
tap.expect_nan (r2.intersect (s), "ray-sphere no-intersect"); tap.expect_nan (r2.intersect (s), "ray-sphere no-intersect");
} }