// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp) // Distributed under the MIT license. See the LICENSE.md file in the project root for more information. // // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel // // 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 the rights // to use, copy, modify, merge, publish, distribute, sublicense, 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 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 NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS 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. using System; using System.Runtime.InteropServices; using System.Runtime.Serialization; namespace math { /// /// Structure providing Width, Height and Depth. /// [DataContract( Name = "!Size3" )] [DataStyle( DataStyle.Compact )] [StructLayout( LayoutKind.Sequential )] public struct Size3 : IEquatable, IComparable { /// /// A zero size with (width, height, depth) = (0,0,0) /// public static readonly Size3 Zero = new Size3( 0, 0, 0 ); /// /// A one size with (width, height, depth) = (1,1,1) /// public static readonly Size3 One = new Size3( 1, 1, 1 ); /// /// A zero size with (width, height, depth) = (0,0,0) /// public static readonly Size3 Empty = Zero; /// /// Initializes a new instance of the struct. /// /// The x. /// The y. /// The depth. public Size3( int width, int height, int depth ) { Width = width; Height = height; Depth = depth; } /// /// Width. /// [DataMember( Order = 0 )] public int Width; /// /// Height. /// [DataMember( Order = 1 )] public int Height; /// /// Height. /// [DataMember( Order = 2 )] public int Depth; /// /// Gets a volume size. /// private long VolumeSize { get { return (long)Width * Height * Depth; } } /// public bool Equals( Size3 other ) { return Width == other.Width && Height == other.Height && Depth == other.Depth; } /// public override bool Equals( object obj ) { if( ReferenceEquals( null, obj ) ) return false; return obj is Size3 && Equals( (Size3)obj ); } /// public override int GetHashCode() { unchecked { int hashCode = Width; hashCode = ( hashCode * 397 ) ^ Height; hashCode = ( hashCode * 397 ) ^ Depth; return hashCode; } } /// public int CompareTo( Size3 other ) { return Math.Sign( this.VolumeSize - other.VolumeSize ); } /// public override string ToString() { return string.Format( "({0},{1},{2})", Width, Height, Depth ); } /// /// Implements the <. /// /// The left. /// The right. /// The result of the operator. public static bool operator <( Size3 left, Size3 right ) { return left.CompareTo( right ) < 0; } /// /// Implements the <. /// /// The left. /// The right. /// The result of the operator. public static bool operator <=( Size3 left, Size3 right ) { return left.CompareTo( right ) <= 0; } /// /// Implements the < or ==. /// /// The left. /// The right. /// The result of the operator. public static bool operator >( Size3 left, Size3 right ) { return left.CompareTo( right ) > 0; } /// /// Implements the > or ==. /// /// The left. /// The right. /// The result of the operator. public static bool operator >=( Size3 left, Size3 right ) { return left.CompareTo( right ) >= 0; } /// /// Implements the ==. /// /// The left. /// The right. /// The result of the operator. public static bool operator ==( Size3 left, Size3 right ) { return left.Equals( right ); } /// /// Implements the !=. /// /// The left. /// The right. /// The result of the operator. public static bool operator !=( Size3 left, Size3 right ) { return !left.Equals( right ); } /// /// Calculates the next up mip-level (*2) of this size. /// /// A next up mip-level Size3. public Size3 Up2( int count = 1 ) { if( count < 0 ) { throw new ArgumentOutOfRangeException( "count", "Must be >= 0" ); } return new Size3( Math.Max( 1, Width << count ), Math.Max( 1, Height << count ), Math.Max( 1, Depth << count ) ); } /// /// Calculates the next down mip-level (/2) of this size. /// /// The count. /// A next down mip-level Size3. public Size3 Down2( int count = 1 ) { if( count < 0 ) { throw new ArgumentOutOfRangeException( "count", "Must be >= 0" ); } return new Size3( Math.Max( 1, Width >> count ), Math.Max( 1, Height >> count ), Math.Max( 1, Depth >> count ) ); } /// /// Calculates the mip size based on a direction. /// /// The direction < 0 then , > 0 then , else this unchanged. /// Size3. public Size3 Mip( int direction ) { return direction == 0 ? this : direction < 0 ? Down2() : Up2(); } } }