Advertisement
Krythic

BoundingBoxBuilder

Jul 31st, 2020
1,120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.42 KB | None | 0 0
  1. using SharpDX;
  2. using VoidwalkerEngine.Framework.Maths;
  3.  
  4. namespace VoidwalkerEngine.Framework.DirectX.Rendering
  5. {
  6.     public class BoundingBoxBuilder
  7.     {
  8.         private Vector3 _minimum;
  9.         public Vector3 Minimum
  10.         {
  11.             get
  12.             {
  13.                 return _minimum;
  14.             }
  15.             set
  16.             {
  17.                 this._minimum = value;
  18.                 Rebuild();
  19.             }
  20.         }
  21.  
  22.         private Vector3 _maximum;
  23.         public Vector3 Maximum
  24.         {
  25.             get
  26.             {
  27.                 return _maximum;
  28.             }
  29.             set
  30.             {
  31.                 this._maximum = value;
  32.                 Rebuild();
  33.             }
  34.         }
  35.  
  36.         private Vector3 _translation;
  37.         public Vector3 Translation
  38.         {
  39.             get
  40.             {
  41.                 return _translation;
  42.             }
  43.             set
  44.             {
  45.                 this._translation = value;
  46.                 Rebuild();
  47.             }
  48.         }
  49.  
  50.         private Vector3 _rotation;
  51.         public Vector3 Rotation
  52.         {
  53.             get
  54.             {
  55.                 return _rotation;
  56.             }
  57.             set
  58.             {
  59.                 this._rotation = value;
  60.                 Rebuild();
  61.             }
  62.         }
  63.  
  64.         private BoundingBox _internalBoundingBox;
  65.  
  66.         public Vector3[] Corners { get; private set; }
  67.  
  68.         public BoundingBoxBuilder()
  69.         {
  70.             Corners = new Vector3[8];
  71.             Rebuild();
  72.         }
  73.  
  74.         public void Update(Vector3 translation, Vector3 rotation)
  75.         {
  76.             this._translation = translation;
  77.             this._rotation = rotation;
  78.             Rebuild();
  79.         }
  80.  
  81.         public BoundingBoxBuilder(Vector3 minimum, Vector3 maximum)
  82.         {
  83.             this._minimum = minimum;
  84.             this._maximum = maximum;
  85.             Corners = new Vector3[8];
  86.             Rebuild();
  87.         }
  88.  
  89.         /// <summary>
  90.         /// Clears the current Translation and Rotation of this builder, then rebuilds it.
  91.         /// </summary>
  92.         public void Clear()
  93.         {
  94.             this._translation = Vector3.Zero;
  95.             this._rotation = Vector3.Zero;
  96.             Rebuild();
  97.         }
  98.  
  99.         /// <summary>
  100.         /// Creates BoundingBox data using vertices.
  101.         /// </summary>
  102.         /// <param name="vertices"></param>
  103.         public void FromVertices(Vertex[] vertices)
  104.         {
  105.             // Calculate Bounding Box
  106.             float minX = float.PositiveInfinity;
  107.             float maxX = float.NegativeInfinity;
  108.             float minY = float.PositiveInfinity;
  109.             float maxY = float.NegativeInfinity;
  110.             float minZ = float.PositiveInfinity;
  111.             float maxZ = float.NegativeInfinity;
  112.             for (int i = 0; i < vertices.Length; i++)
  113.             {
  114.                 Vector3 vertex = vertices[i].Location;
  115.                 // Check for maximum
  116.                 if (vertex.X > maxX)
  117.                 {
  118.                     maxX = vertex.X;
  119.                 }
  120.                 if (vertex.Y > maxY)
  121.                 {
  122.                     maxY = vertex.Y;
  123.                 }
  124.                 if (vertex.Z > maxZ)
  125.                 {
  126.                     maxZ = vertex.Z;
  127.                 }
  128.                 // Check for Minimum
  129.                 if (vertex.X < minX)
  130.                 {
  131.                     minX = vertex.X;
  132.                 }
  133.                 if (vertex.Y < minY)
  134.                 {
  135.                     minY = vertex.Y;
  136.                 }
  137.                 if (vertex.Z < minZ)
  138.                 {
  139.                     minZ = vertex.Z;
  140.                 }
  141.             }
  142.             this.Minimum = new Vector3(minX, minY, minZ);
  143.             this.Maximum = new Vector3(maxX, maxY, maxZ);
  144.         }
  145.  
  146.         public Matrix GetTransform()
  147.         {
  148.             return VoidwalkerMath.CreateRotationMatrix(this.Rotation) * Matrix.Translation(this.Translation);
  149.         }
  150.  
  151.         private void Rebuild()
  152.         {
  153.             Matrix transform = GetTransform();
  154.             this.Corners[0] = Vector3.TransformCoordinate(new Vector3(this.Minimum.X, this.Maximum.Y, this.Maximum.Z), transform);
  155.             this.Corners[1] = Vector3.TransformCoordinate(new Vector3(this.Maximum.X, this.Maximum.Y, this.Maximum.Z), transform);
  156.             this.Corners[2] = Vector3.TransformCoordinate(new Vector3(this.Maximum.X, this.Minimum.Y, this.Maximum.Z), transform);
  157.             this.Corners[3] = Vector3.TransformCoordinate(new Vector3(this.Minimum.X, this.Minimum.Y, this.Maximum.Z), transform);
  158.             this.Corners[4] = Vector3.TransformCoordinate(new Vector3(this.Minimum.X, this.Maximum.Y, this.Minimum.Z), transform);
  159.             this.Corners[5] = Vector3.TransformCoordinate(new Vector3(this.Maximum.X, this.Maximum.Y, this.Minimum.Z), transform);
  160.             this.Corners[6] = Vector3.TransformCoordinate(new Vector3(this.Maximum.X, this.Minimum.Y, this.Minimum.Z), transform);
  161.             this.Corners[7] = Vector3.TransformCoordinate(new Vector3(this.Minimum.X, this.Minimum.Y, this.Minimum.Z), transform);
  162.             this._internalBoundingBox = BoundingBox.FromPoints(this.Corners);
  163.         }
  164.  
  165.         /// <summary>
  166.         /// Gets the currently built BoundingBox.
  167.         /// </summary>
  168.         /// <returns></returns>
  169.         public BoundingBox ToBoundingBox()
  170.         {
  171.             return _internalBoundingBox;
  172.         }
  173.     }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement