// Copyright (C) 2012 Jérôme Leclercq // This file is part of the "Nazara Engine". // For conditions of distribution and use, see copyright notice in Config.hpp #include #include #include template NzCube::NzCube() { } template NzCube::NzCube(T X, T Y, T Z, T Width, T Height, T Depth) : x(X), y(Y), z(Z), width(Width), height(Height), depth(Depth) { } template NzCube::NzCube(T vec[6]) : x(vec[0]), y(vec[1]), z(vec[2]), width(vec[3]), height(vec[4]), depth(vec[5]) { } template NzCube::NzCube(const NzRect& rect) : x(rect.x), y(rect.y), z(0), width(rect.width), height(rect.height), depth(1) { } template template NzCube::NzCube(const NzCube& rect) : x(static_cast(rect.x)), y(static_cast(rect.y)), z(static_cast(rect.z)), width(static_cast(rect.width)), height(static_cast(rect.height)), depth(static_cast(rect.depth)) { } template bool NzCube::Contains(T X, T Y, T Z) const { return X >= x && X < x+width && Y >= y && Y < y+height && Z >= z && Z < z+depth; } template bool NzCube::Contains(const NzVector3& point) const { return Contains(point.x, point.y, point.z); } template bool NzCube::Contains(const NzCube& rect) const { return Contains(rect.x, rect.y, rect.z) && Contains(rect.x + rect.width, rect.y + rect.height, rect.z + rect.depth); } template void NzCube::ExtendTo(const NzVector3& point) { x = std::min(x, point.x); y = std::min(y, point.y); z = std::min(z, point.z); width = std::max(x+width, point.x)-x; height = std::max(y+height, point.x)-y; depth = std::max(z+depth, point.x)-z; } template void NzCube::ExtendTo(const NzCube& rect) { x = std::min(x, rect.x); y = std::min(y, rect.y); z = std::min(y, rect.z); width = std::max(x+width, rect.x+rect.width)-x; height = std::max(x+height, rect.y+rect.height)-y; depth = std::max(x+depth, rect.z+rect.depth)-z; } template NzVector3 NzCube::GetCenter() const { return NzVector3((x+width)/2, (y+height)/2, (z+depth)/2); } template bool NzCube::Intersect(const NzCube& rect) const { NzCube intersection; // Optimisé par le compilateur return Intersect(rect, intersection); } template bool NzCube::Intersect(const NzCube& rect, NzCube& intersection) const { T left = std::max(x, rect.x); T right = std::min(x+width, rect.x+rect.width); T top = std::max(y, rect.y); T bottom = std::min(y+height, rect.y+rect.height); T up = std::max(z, rect.z); T down = std::min(z+depth, rect.z+rect.depth); if (left < right && top < bottom && up < down) { intersection.x = left; intersection.y = top; intersection.z = up; intersection.width = right-left; intersection.height = bottom-top; intersection.depth = down-up; return true; } else return false; } template bool NzCube::IsValid() const { return width > 0 && height > 0 && depth > 0; } template NzString NzCube::ToString() const { NzStringStream ss; return ss << "Cube(" << x << ", " << y << ", " << z << ", " << width << ", " << height << ", " << depth << ')'; } template NzCube::operator NzString() const { return ToString(); } template T& NzCube::operator[](unsigned int i) { if (i >= 6) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Index out of range (" << i << " >= 4)"; throw std::domain_error(ss.ToString()); } return *(&x+i); } template T NzCube::operator[](unsigned int i) const { if (i >= 6) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Index out of range (" << i << " >= 4)"; throw std::domain_error(ss.ToString()); } return *(&x+i); } template std::ostream& operator<<(std::ostream& out, const NzCube& rect) { return out << rect.ToString(); } #include