From 20bafa2cfb8ea9ddf31db8e9706ebcfadf82a7b9 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 13 Oct 2015 18:19:47 +1100 Subject: [PATCH] geom: move geometry primitives to own namespace --- Makefile.am | 23 +++++++++++++---------- aabb.cpp => geom/aabb.cpp | 8 ++++---- aabb.hpp => geom/aabb.hpp | 13 ++++++------- geom/fwd.hpp | 33 +++++++++++++++++++++++++++++++++ sphere.cpp => geom/iostream.cpp | 24 ++++++------------------ geom/iostream.hpp | 30 ++++++++++++++++++++++++++++++ plane.cpp => geom/plane.cpp | 11 +++++++---- plane.hpp => geom/plane.hpp | 4 ++-- ray.cpp => geom/ray.cpp | 15 ++++++++------- ray.hpp => geom/ray.hpp | 8 ++++---- geom/sphere.cpp | 20 ++++++++++++++++++++ sphere.hpp => geom/sphere.hpp | 17 +++++------------ noise/basis/patch.ipp | 1 - test/{ => geom}/aabb.cpp | 16 +++++++++------- test/{ => geom}/ray.cpp | 32 +++++++++++++++++++------------- 15 files changed, 166 insertions(+), 89 deletions(-) rename aabb.cpp => geom/aabb.cpp (95%) rename aabb.hpp => geom/aabb.hpp (92%) create mode 100644 geom/fwd.hpp rename sphere.cpp => geom/iostream.cpp (56%) create mode 100644 geom/iostream.hpp rename plane.cpp => geom/plane.cpp (83%) rename plane.hpp => geom/plane.hpp (96%) rename ray.cpp => geom/ray.cpp (91%) rename ray.hpp => geom/ray.hpp (94%) create mode 100644 geom/sphere.cpp rename sphere.hpp => geom/sphere.hpp (76%) rename test/{ => geom}/aabb.cpp (55%) rename test/{ => geom}/ray.cpp (70%) diff --git a/Makefile.am b/Makefile.am index 7db4b970..d2bf0e48 100644 --- a/Makefile.am +++ b/Makefile.am @@ -9,8 +9,6 @@ AM_CXXFLAGS = $(BOOST_CPPFLAGS) $(ZLIB_CFLAGS) ## Source definitions UTIL_FILES = \ - aabb.cpp \ - aabb.hpp \ backtrace.hpp \ bezier.cpp \ bezier.hpp \ @@ -56,6 +54,17 @@ UTIL_FILES = \ format.ipp \ fourcc.cpp \ 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.hpp \ hash.hpp \ @@ -196,8 +205,6 @@ UTIL_FILES = \ noise/turbulence.ipp \ pascal.cpp \ pascal.hpp \ - plane.cpp \ - plane.hpp \ platform.hpp \ point.cpp \ point.hpp \ @@ -221,8 +228,6 @@ UTIL_FILES = \ rational.cpp \ rational.hpp \ rational.ipp \ - ray.cpp \ - ray.hpp \ region.cpp \ region.hpp \ region.ipp \ @@ -231,8 +236,6 @@ UTIL_FILES = \ signal.hpp \ signal.ipp \ si.hpp \ - sphere.cpp \ - sphere.hpp \ stats.cpp \ stats.hpp \ stream.cpp \ @@ -352,7 +355,6 @@ AM_LDFLAGS += $(BOOST_LDFLAGS) $(BOOST_FILESYSTEM_LIB) $(BOOST_SYSTEM_LIB) AM_CXXFLAGS += -I$(top_srcdir) TEST_BIN = \ - test/aabb \ test/backtrace \ test/bezier \ test/bitwise \ @@ -368,6 +370,8 @@ TEST_BIN = \ test/fixed \ test/float \ test/format \ + test/geom/aabb \ + test/geom/ray \ test/hash/murmur \ test/hash/fasthash \ test/hmac \ @@ -377,7 +381,6 @@ TEST_BIN = \ test/introspection \ test/ip \ test/json_types \ - test/ray \ test/maths \ test/matrix \ test/md2 \ diff --git a/aabb.cpp b/geom/aabb.cpp similarity index 95% rename from aabb.cpp rename to geom/aabb.cpp index c18a0699..617af305 100644 --- a/aabb.cpp +++ b/geom/aabb.cpp @@ -18,7 +18,7 @@ #include "aabb.hpp" #include "debug.hpp" -using util::AABB; +using util::geom::AABB; //----------------------------------------------------------------------------- template @@ -223,7 +223,7 @@ namespace debug { //----------------------------------------------------------------------------- template std::ostream& -util::operator<< (std::ostream &os, AABB b) +util::geom::operator<< (std::ostream &os, util::geom::AABB b) { os << "AABB(" << b.p0 << ", " << b.p1 << ")"; return os; @@ -232,9 +232,9 @@ util::operator<< (std::ostream &os, AABB b) //----------------------------------------------------------------------------- #define INSTANTIATE_S_T(S,T) \ -namespace util { template struct AABB; } \ +namespace util { namespace geom { template struct AABB; } } \ template bool debug::valid (const AABB&); \ -template std::ostream& util::operator<< (std::ostream&, AABB); +template std::ostream& util::geom::operator<< (std::ostream&, AABB); #define INSTANTIATE(T) \ INSTANTIATE_S_T(2,T) \ diff --git a/aabb.hpp b/geom/aabb.hpp similarity index 92% rename from aabb.hpp rename to geom/aabb.hpp index 01d51881..25c9b692 100644 --- a/aabb.hpp +++ b/geom/aabb.hpp @@ -15,16 +15,15 @@ */ -#ifndef __UTIL_AABB_HPP -#define __UTIL_AABB_HPP +#ifndef __UTIL_GEOM_AABB_HPP +#define __UTIL_GEOM_AABB_HPP -#include "point.hpp" -#include "extent.hpp" +#include "../point.hpp" +#include "../extent.hpp" #include -#include -namespace util { +namespace util { namespace geom { template struct AABB { AABB () = default; @@ -68,6 +67,6 @@ namespace util { template std::ostream& operator<< (std::ostream&, AABB); -} +} } #endif diff --git a/geom/fwd.hpp b/geom/fwd.hpp new file mode 100644 index 00000000..ce44f36b --- /dev/null +++ b/geom/fwd.hpp @@ -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 + */ + +#ifndef __UTIL_GEOM_FWD_HPP +#define __UTIL_GEOM_FWD_HPP + +#include + +namespace util { namespace geom { + template struct ray; + template struct plane; + template struct AABB; + template struct sphere; + template struct ellipse; + template struct rect; + template struct cylinder; + template struct tri; +} } + +#endif diff --git a/sphere.cpp b/geom/iostream.cpp similarity index 56% rename from sphere.cpp rename to geom/iostream.cpp index e8bb59be..2ef4b33b 100644 --- a/sphere.cpp +++ b/geom/iostream.cpp @@ -14,32 +14,20 @@ * Copyright 2015 Danny Robson */ -#include "sphere.hpp" +#include "./iostream.hpp" -using util::sphere; - -//----------------------------------------------------------------------------- -template -sphere::sphere (point _centre, T _radius): - centre (_centre), - radius (_radius) -{ - CHECK_GE (_radius, 0); -} +#include "../geom/sphere.hpp" -//----------------------------------------------------------------------------- +/////////////////////////////////////////////////////////////////////////////// template std::ostream& -util::operator<< (std::ostream &os, sphere s) +util::geom::operator<< (std::ostream &os, util::geom::sphere s) { 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 struct util::sphere<3,float>; +template std::ostream& util::geom::operator<< (std::ostream&, util::geom::sphere<2,float>); +template std::ostream& util::geom::operator<< (std::ostream&, util::geom::sphere<3,float>); diff --git a/geom/iostream.hpp b/geom/iostream.hpp new file mode 100644 index 00000000..2390ecd0 --- /dev/null +++ b/geom/iostream.hpp @@ -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 + */ + +#ifndef __UTIL_GEOM_IOSTREAM_HPP +#define __UTIL_GEOM_IOSTREAM_HPP + +#include "./fwd.hpp" + +#include + +namespace util { namespace geom { + template + std::ostream& + operator<< (std::ostream&, sphere); +} } + +#endif diff --git a/plane.cpp b/geom/plane.cpp similarity index 83% rename from plane.cpp rename to geom/plane.cpp index 9ae34834..2ace5b44 100644 --- a/plane.cpp +++ b/geom/plane.cpp @@ -19,10 +19,13 @@ #include "debug.hpp" +using util::geom::plane; + + //----------------------------------------------------------------------------- template -util::plane::plane (util::point _p, - util::vector _n): +plane::plane (point _p, + vector _n): p (_p), n (_n) { @@ -31,5 +34,5 @@ util::plane::plane (util::point _p, //----------------------------------------------------------------------------- -template struct util::plane<2,float>; -template struct util::plane<3,float>; +template struct util::geom::plane<2,float>; +template struct util::geom::plane<3,float>; diff --git a/plane.hpp b/geom/plane.hpp similarity index 96% rename from plane.hpp rename to geom/plane.hpp index 815f8d97..4e0c686f 100644 --- a/plane.hpp +++ b/geom/plane.hpp @@ -20,7 +20,7 @@ #include "point.hpp" #include "vector.hpp" -namespace util { +namespace util { namespace geom { template struct plane { plane (util::point p, @@ -33,6 +33,6 @@ namespace util { typedef plane<2,float> plane2f; typedef plane<3,float> plane3f; -} +} } #endif diff --git a/ray.cpp b/geom/ray.cpp similarity index 91% rename from ray.cpp rename to geom/ray.cpp index 7b269735..5fdf6ae8 100644 --- a/ray.cpp +++ b/geom/ray.cpp @@ -16,9 +16,10 @@ #include "ray.hpp" -#include "debug.hpp" +#include "ops.hpp" +#include "../debug.hpp" -using util::ray; +using util::geom::ray; /////////////////////////////////////////////////////////////////////////////// @@ -137,15 +138,15 @@ ray::at (T t) const /////////////////////////////////////////////////////////////////////////////// template std::ostream& -util::operator<< (std::ostream &os, ray r) +util::geom::operator<< (std::ostream &os, ray r) { return os << "ray(" << r.origin << ',' << r.direction << ')'; } -template std::ostream& util::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,float>); +template std::ostream& util::geom::operator<< (std::ostream&, ray<3,double>); /////////////////////////////////////////////////////////////////////////////// -template struct util::ray<2,float>; -template struct util::ray<3,float>; +template struct util::geom::ray<2,float>; +template struct util::geom::ray<3,float>; diff --git a/ray.hpp b/geom/ray.hpp similarity index 94% rename from ray.hpp rename to geom/ray.hpp index caca6810..d969cc81 100644 --- a/ray.hpp +++ b/geom/ray.hpp @@ -14,8 +14,8 @@ * Copyright 2015 Danny Robson */ -#ifndef __UTIL_RAY_HPP -#define __UTIL_RAY_HPP +#ifndef __UTIL_GEOM_RAY_HPP +#define __UTIL_GEOM_RAY_HPP #include "point.hpp" @@ -26,7 +26,7 @@ #include -namespace util { +namespace util { namespace geom { template struct ray { ray (point origin, @@ -56,6 +56,6 @@ namespace util { typedef ray<2,float> ray2f; typedef ray<3,float> ray3f; -} +} } #endif diff --git a/geom/sphere.cpp b/geom/sphere.cpp new file mode 100644 index 00000000..a8fbf8ef --- /dev/null +++ b/geom/sphere.cpp @@ -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 + */ + +#include "sphere.hpp" + +using util::geom::sphere; + diff --git a/sphere.hpp b/geom/sphere.hpp similarity index 76% rename from sphere.hpp rename to geom/sphere.hpp index 124d691f..05845309 100644 --- a/sphere.hpp +++ b/geom/sphere.hpp @@ -14,27 +14,20 @@ * Copyright 2015 Danny Robson */ -#ifndef __UTIL_SPHERE_HPP -#define __UTIL_SPHERE_HPP +#ifndef __UTIL_GEOM_SPHERE_HPP +#define __UTIL_GEOM_SPHERE_HPP -#include "point.hpp" +#include "../point.hpp" -#include - -namespace util { +namespace util { namespace geom { template struct sphere { - sphere (point centre, T radius); - point centre; T radius; }; - template - std::ostream& operator<< (std::ostream&, sphere); - typedef sphere<2,float> sphere2f; typedef sphere<3,float> sphere3f; -} +} } #endif diff --git a/noise/basis/patch.ipp b/noise/basis/patch.ipp index 152aa3ad..7109a6f5 100644 --- a/noise/basis/patch.ipp +++ b/noise/basis/patch.ipp @@ -20,7 +20,6 @@ #define __UTIL_NOISE_BASIS_PATCH_IPP #include "../../types.hpp" -#include "../../ray.hpp" #include "../../vector.hpp" diff --git a/test/aabb.cpp b/test/geom/aabb.cpp similarity index 55% rename from test/aabb.cpp rename to test/geom/aabb.cpp index efdf8d53..1493f8cf 100644 --- a/test/aabb.cpp +++ b/test/geom/aabb.cpp @@ -1,9 +1,11 @@ -#include "aabb.hpp" +#include "geom/aabb.hpp" #include "tap.hpp" #include +using util::geom::AABB2f; + int main (int, char**) @@ -12,38 +14,38 @@ main (int, char**) { // Test contraction - util::AABB2f box { + AABB2f box { { 2, 2 }, { 8, 8 } }; box.contract (2.f); - tap.expect_eq (box, { { 3, 3 }, { 7, 7 }}, "over contraction"); + tap.expect_eq (box, { { 3, 3 }, { 7, 7 }}, "over contraction"); } { // Test expansion - util::AABB2f box { + AABB2f box { { 2, 2 }, { 8, 8 } }; box.expand (2.f); - tap.expect_eq (box, { { 1, 1 }, { 9, 9 }}, "expansion"); + tap.expect_eq (box, { { 1, 1 }, { 9, 9 }}, "expansion"); } { // Ensure we don't wrap-around on unsigned position types when contracting - util::AABB2f small { + AABB2f small { { 0, 0 }, { 1, 1 } }; small.contract (10); - tap.expect_eq (small, { { 0.5f, 0.5f }, { 0.5f, 0.5f }}, "unsigned over-contract"); + tap.expect_eq (small, { { 0.5f, 0.5f }, { 0.5f, 0.5f }}, "unsigned over-contract"); } } diff --git a/test/ray.cpp b/test/geom/ray.cpp similarity index 70% rename from test/ray.cpp rename to test/geom/ray.cpp index f3da84c2..45d989e4 100644 --- a/test/ray.cpp +++ b/test/geom/ray.cpp @@ -1,17 +1,19 @@ -#include "aabb.hpp" -#include "debug.hpp" -#include "plane.hpp" -#include "ray.hpp" +#include "geom/aabb.hpp" +#include "geom/plane.hpp" +#include "geom/ray.hpp" #include "tap.hpp" +using util::geom::ray2f; +using util::geom::ray3f; + //----------------------------------------------------------------------------- void test_intersect_plane (util::TAP::logger &tap) { // trivial case: origin ray facing z, plane at unit z facing -z. - const util::ray3f l ({0,0,0}, {0,0, 1}); - const util::plane3f p ({0,0,1}, {0,0,-1}); + const util::geom::ray3f l ({0,0,0}, {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"); } @@ -21,20 +23,22 @@ test_intersect_plane (util::TAP::logger &tap) void test_intersect_aabb (util::TAP::logger &tap) { + using util::geom::AABB2f; + // trivial case: unit aabb at origin, ray from (0.5,-0.5) upwards - const util::AABB2f box { + const AABB2f box { { 0.f, 0.f }, { 1.f, 1.f } }; - const util::ray2f forward { + const ray2f forward { { 0.5f, -0.5f }, { 0.f, 1.f } }; tap.expect_eq (forward.intersect (box), 0.5f, "ray-aabb intersect"); - const util::ray2f behind { + const ray2f behind { { 0.5f, 2.f }, { 0.f, 1.f } }; @@ -47,15 +51,17 @@ test_intersect_aabb (util::TAP::logger &tap) void 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"); - 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"); - 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"); }