/*
 * (C) Copyright Ian D. Romanick 2007
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * AUTHORS, COPYRIGHT HOLDERS, AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "vector.h"

class aabb {
public:
    aabb(void)
    {
        initialized = false;
    }

    aabb(const idr_vec4 &p)
    {
        initialized = false;
        add_point(p);
    }

    void clear(void)
    {
        initialized = false;
    }

    void add_point(const idr_vec4 &p);
    void union_with(const aabb &a);
    void intersect_with(const aabb &a);

    bool overlaps(const aabb &a) const;

    float volume(void) const;
    float surface_area(void) const;
    float margin(void) const;

    bool compare_x(const aabb &a) const
    {
        return (corners[0][0] < a.corners[0][0])
            || ((corners[0][0] == a.corners[0][0])
                && (corners[1][0] < a.corners[1][0]));
    }

    bool compare_y(const aabb &a) const
    {
        return (corners[0][1] < a.corners[0][1])
            || ((corners[0][1] == a.corners[0][1])
                && (corners[1][1] < a.corners[1][1]));
    }

    bool compare_z(const aabb &a) const
    {
        return (corners[0][2] < a.corners[0][2])
            || ((corners[0][2] == a.corners[0][2])
                && (corners[1][2] < a.corners[1][2]));
    }

    const idr_vec4 min_corner(void) const
    {
        return corners[0];
    }

    const idr_vec4 max_corner(void) const
    {
        return corners[1];
    }

private:
    /**
     * Minimum and maximum corners of the AABB.
     *
     * Each component of \c corners[0] must be less than or equal to each
     * component of \c corners[1].
     */
    idr_vec4  corners[2];

    bool initialized;
};
