source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Geometry/Plane3D.cs @ 15554

Last change on this file since 15554 was 15554, checked in by rhanghof, 21 months ago

#2817:

  • Unittests
  • Bugfixes on the line projection based extreme point creation method
File size: 6.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6
7namespace HeuristicLab.Problems.BinPacking3D.Geometry {
8  public enum Side { Top, Left, Bottom, Right, Front, Back }
9
10  /// <summary>
11  /// A plane is given as a point and two directing vectors
12  /// </summary>
13  public class Plane3D {
14    public Vector3D Point { get; private set; }
15    public Vector3D Direction1 { get; private set; }
16    public Vector3D Direction2 { get; private set; }
17    public Vector3D Normal { get; private set; }
18
19    private int rhs;
20
21    public Plane3D(PackingShape bin, Side side) {
22      switch (side) {
23        case Side.Top: // ZX plane
24          Point = new Vector3D(0, bin.Height, 0);
25          Direction1 = new Vector3D(0, 0, bin.Depth);
26          Direction2 = new Vector3D(bin.Width, 0, 0);
27          break;
28        case Side.Left: // ZY plane
29          Point = new Vector3D(0, 0, 0);
30          Direction1 = new Vector3D(0, 0, bin.Depth);
31          Direction2 = new Vector3D(0, bin.Height, 0);
32          break;
33        case Side.Bottom: // XZ plane
34          Point = new Vector3D(0, 0, 0);
35          Direction1 = new Vector3D(bin.Width, 0, 0);
36          Direction2 = new Vector3D(0, 0, bin.Depth);
37          break;
38        case Side.Right: // YZ plane
39          Point = new Vector3D(bin.Width, 0, 0);
40          Direction1 = new Vector3D(0, bin.Height, 0);
41          Direction2 = new Vector3D(0, 0, bin.Depth);
42          break;
43        case Side.Front: // XY plane
44          Point = new Vector3D(0, 0, bin.Depth);
45          Direction1 = new Vector3D(bin.Width, 0, 0);
46          Direction2 = new Vector3D(0, bin.Height, 0);
47          break;
48        case Side.Back: // YX plane
49          Point = new Vector3D(0, 0, 0);
50          Direction1 = new Vector3D(0, bin.Height, 0);
51          Direction2 = new Vector3D(bin.Width, 0, 0);
52          break;
53      }
54      Normal = Direction1.Cross(Direction2);
55      rhs = 0;
56    }
57
58    public Plane3D(PackingPosition pos, PackingItem item, Side side) {
59      // the directing vectors are chosen such that normal always points outside the packing item
60      switch (side) {
61        case Side.Top: // ZX plane
62          Point = new Vector3D(pos.X, pos.Y + item.Height, pos.Z);
63          Direction1 = new Vector3D(0, 0, pos.Rotated ? item.Width : item.Depth);
64          Direction2 = new Vector3D(pos.Rotated ? item.Depth : item.Width, 0, 0);
65          break;
66        case Side.Left: // ZY plane
67          Point = new Vector3D(pos.X, pos.Y, pos.Z);
68          Direction1 = new Vector3D(0, 0, pos.Rotated ? item.Width : item.Depth);
69          Direction2 = new Vector3D(0, item.Height, 0);
70          break;
71        case Side.Bottom: // XZ plane
72          Point = new Vector3D(pos.X, pos.Y, pos.Z);
73          Direction1 = new Vector3D(pos.Rotated ? item.Depth : item.Width, 0, 0);
74          Direction2 = new Vector3D(0, 0, pos.Rotated ? item.Width : item.Depth);
75          break;
76        case Side.Right: // YZ plane
77          Point = new Vector3D(pos.X + (pos.Rotated ? item.Depth : item.Width), pos.Y, pos.Z);
78          Direction1 = new Vector3D(0, item.Height, 0);
79          Direction2 = new Vector3D(0, 0, pos.Rotated ? item.Width : item.Depth);
80          break;
81        case Side.Front: // XY plane
82          Point = new Vector3D(pos.X, pos.Y, pos.Z + (pos.Rotated ? item.Width : item.Depth));
83          Direction1 = new Vector3D(pos.Rotated ? item.Depth : item.Width, 0, 0);
84          Direction2 = new Vector3D(0, item.Height, 0);
85          break;
86        case Side.Back: // YX plane
87          Point = new Vector3D(pos.X, pos.Y, pos.Z);
88          Direction1 = new Vector3D(0, item.Height, 0);
89          Direction2 = new Vector3D(pos.Rotated ? item.Depth : item.Width, 0, 0);
90          break;
91      }
92      Normal = Direction1.Cross(Direction2);
93      rhs = Normal.X * Point.X + Normal.Y * Point.Y + Normal.Z * Point.Z;
94    }
95
96    /// <summary>
97    /// Returns true if the given point is an element of the current plane
98    /// </summary>
99    /// <param name="point"></param>
100    /// <returns></returns>
101    public bool IsElementOf(Vector3D point) {
102      return Normal.X * point.X + Normal.Y * point.Y + Normal.Z * point.Z == rhs;
103    }
104
105    /// <summary>
106    /// Returns true, if the given line intersects with the current plane
107    /// </summary>
108    /// <param name="line"></param>
109    /// <returns></returns>
110    public bool Intersects(Line3D line) {
111      return Intersect(line) != null;
112    }
113
114    /// <summary>
115    /// Returns the point of intersection of a given line and the current plane.
116    /// It returns null if there is no intersection.
117    /// </summary>
118    /// <param name="line"></param>
119    /// <returns></returns>
120    public Vector3D Intersect(Line3D line) {
121      var denom = Normal * line.Direction;
122      if (denom == 0) {
123        // dir is perpendicular to the normal vector of the plane
124        // this means they intersect if p1 is element of the plane
125        // also the plane does not stretch infinitely, but is bound
126        // to the parallelogram spanned by the point and the two
127        // directing vectors
128        if (IsElementOf(line.Point) && IsWithinDirectionalVectors(line.Point))
129          return line.Point;
130        else
131          return null;
132      }
133      var intersect = line.Point + ((Normal * (Point - line.Point)) / denom) * line.Direction;
134      if (IsWithinDirectionalVectors(intersect))
135        return intersect;
136      return null;
137    }
138
139    public bool IsWithinDirectionalVectors(Vector3D point) {
140      return point.X >= Point.X && (Direction1.X + Direction2.X == 0 || (point.X < Point.X + Direction1.X || point.X < Point.X + Direction2.X))
141          && point.Y >= Point.Y && (Direction1.Y + Direction2.Y == 0 || (point.Y < Point.Y + Direction1.Y || point.Y < Point.Y + Direction2.Y))
142          && point.Z >= Point.Z && (Direction1.Z + Direction2.Z == 0 || (point.Z < Point.Z + Direction1.Z || point.Z < Point.Z + Direction2.Z));
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.