// 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 #define F(a) static_cast(a) template NzCube::NzCube() { } template NzCube::NzCube(T X, T Y, T Z, T Width, T Height, T Depth) { Set(X, Y, Z, Width, Height, Depth); } template NzCube::NzCube(const T vec[6]) { Set(vec); } template NzCube::NzCube(const NzRect& rect) { Set(rect); } template NzCube::NzCube(const NzVector3& vec1, const NzVector3& vec2) { Set(vec1, vec2); } template template NzCube::NzCube(const NzCube& rect) { Set(rect); } 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)/F(2.0), (y+height)/F(2.0), (z+depth)/F(2.0)); } 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) { if (intersection) { 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 > F(0.0) && height > F(0.0) && depth > F(0.0); } template void NzCube::Set(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 void NzCube::Set(const T rect[6]) { x = rect[0]; y = rect[1]; z = rect[2]; width = rect[3]; height = rect[4]; depth = rect[5]; } template void NzCube::Set(const NzRect& rect) { x = rect.x; y = rect.y; z = 0; width = rect.width; height = rect.height; depth = 1; } template void NzCube::Set(const NzVector3& vec1, const NzVector3& vec2) { x = std::min(vec1.x, vec2.x); y = std::min(vec1.y, vec2.y); z = std::min(vec1.z, vec2.z); width = (vec2.x > vec1.x) ? vec2.x-vec1.x : vec1.x-vec2.x; height = (vec2.y > vec1.y) ? vec2.y-vec1.y : vec1.y-vec2.y; depth = (vec2.z > vec1.z) ? vec2.z-vec1.z : vec1.z-vec2.z; } template template void NzCube::Set(const NzCube& cube) { x = F(cube.x); y = F(cube.y); z = F(cube.z); width = F(cube.width); height = F(cube.height); depth = F(cube.depth); } 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 NAZARA_MATH_SAFE if (i >= 6) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Index out of range (" << i << " >= 6)"; throw std::domain_error(ss.ToString()); } #endif return *(&x+i); } template T NzCube::operator[](unsigned int i) const { #if NAZARA_MATH_SAFE if (i >= 6) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Index out of range (" << i << " >= 6)"; throw std::domain_error(ss.ToString()); } #endif return *(&x+i); } template std::ostream& operator<<(std::ostream& out, const NzCube& rect) { return out << rect.ToString(); } #undef F #include