// Begin License: // Copyright (C) 2006-2008 Tobias Sargeant (tobias.sargeant@gmail.com). // All rights reserved. // // This file is part of the Carve CSG Library (http://carve-csg.com/) // // This file may be used under the terms of the GNU General Public // License version 2.0 as published by the Free Software Foundation // and appearing in the file LICENSE.GPL2 included in the packaging of // this file. // // This file is provided "AS IS" with NO WARRANTY OF ANY KIND, // INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE. // End: #pragma once #include #include #include #include #include #include namespace carve { namespace geom3d { struct hash_vector_ptr { size_t operator()(const Vector * const &v) const { return (size_t)v; } size_t operator()(const std::pair &v) const { size_t r = (size_t)v.first; size_t s = (size_t)v.second; return r ^ ((s >> 16) | (s << 16)); } }; struct vec_adapt_ident { const Vector &operator()(const Vector &v) const { return v; } Vector &operator()(Vector &v) const { return v; } }; struct vec_adapt_ptr { const Vector &operator()(const Vector * const &v) const { return *v; } Vector &operator()(Vector *&v) const { return *v; } }; struct vec_adapt_pair_first { template const Vector &operator()(const pair_t &v) const { return v.first; } template Vector &operator()(pair_t &v) const { return v.first; } }; struct vec_adapt_pair_second { template const Vector &operator()(const pair_t &v) const { return v.second; } template Vector &operator()(pair_t &v) const { return v.second; } }; template struct vec_cmp_lt_x { adapt_t adapt; vec_cmp_lt_x(adapt_t _adapt = adapt_t()) : adapt(_adapt) {} template bool operator()(const input_t &a, const input_t &b) const { return adapt(a).x < adapt(b).x; } }; template vec_cmp_lt_x vec_lt_x(adapt_t &adapt) { return vec_cmp_lt_x(adapt); } template struct vec_cmp_lt_y { adapt_t adapt; vec_cmp_lt_y(adapt_t _adapt = adapt_t()) : adapt(_adapt) {} template bool operator()(const input_t &a, const input_t &b) const { return adapt(a).y < adapt(b).y; } }; template vec_cmp_lt_y vec_lt_y(adapt_t &adapt) { return vec_cmp_lt_y(adapt); } template struct vec_cmp_lt_z { adapt_t adapt; vec_cmp_lt_z(adapt_t _adapt = adapt_t()) : adapt(_adapt) {} template bool operator()(const input_t &a, const input_t &b) const { return adapt(a).z < adapt(b).z; } }; template vec_cmp_lt_z vec_lt_z(adapt_t &adapt) { return vec_cmp_lt_z(adapt); } template struct vec_cmp_gt_x { adapt_t adapt; vec_cmp_gt_x(adapt_t _adapt = adapt_t()) : adapt(_adapt) {} template bool operator()(const input_t &a, const input_t &b) const { return adapt(a).x > adapt(b).x; } }; template vec_cmp_gt_x vec_gt_x(adapt_t &adapt) { return vec_cmp_gt_x(adapt); } template struct vec_cmp_gt_y { adapt_t adapt; vec_cmp_gt_y(adapt_t _adapt = adapt_t()) : adapt(_adapt) {} template bool operator()(const input_t &a, const input_t &b) const { return adapt(a).y > adapt(b).y; } }; template vec_cmp_gt_y vec_gt_y(adapt_t &adapt) { return vec_cmp_gt_y(adapt); } template struct vec_cmp_gt_z { adapt_t adapt; vec_cmp_gt_z(adapt_t _adapt = adapt_t()) : adapt(_adapt) {} template bool operator()(const input_t &a, const input_t &b) const { return adapt(a).z > adapt(b).z; } }; template vec_cmp_gt_z vec_gt_z(adapt_t &adapt) { return vec_cmp_gt_z(adapt); } template void sortInDirectionOfRay(const Vector &ray_dir, iter_t begin, iter_t end, adapt_t adapt) { switch (carve::geom::largestAxis(ray_dir)) { case 0: if (ray_dir.x > 0) { std::sort(begin, end, vec_lt_x(adapt)); } else { std::sort(begin, end, vec_gt_x(adapt)); } break; case 1: if (ray_dir.y > 0) { std::sort(begin, end, vec_lt_y(adapt)); } else { std::sort(begin, end, vec_gt_y(adapt)); } break; case 2: if (ray_dir.z > 0) { std::sort(begin, end, vec_lt_z(adapt)); } else { std::sort(begin, end, vec_gt_z(adapt)); } break; } } } }