1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Problems.BinPacking3D.Geometry {
|
---|
8 | public class Vector3D {
|
---|
9 |
|
---|
10 | public int X { get; set; }
|
---|
11 | public int Y { get; set; }
|
---|
12 | public int Z { get; set; }
|
---|
13 |
|
---|
14 | public Vector3D() {
|
---|
15 | X = 0;
|
---|
16 | Y = 0;
|
---|
17 | Z = 0;
|
---|
18 | }
|
---|
19 | public Vector3D(int x, int y, int z) {
|
---|
20 | X = x;
|
---|
21 | Y = y;
|
---|
22 | Z = z;
|
---|
23 | }
|
---|
24 | public Vector3D(PackingPosition pos) {
|
---|
25 | X = pos.X;
|
---|
26 | Y = pos.Y;
|
---|
27 | Z = pos.Z;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public PackingPosition ToPackingPosition(int assignedBin) {
|
---|
31 | return new PackingPosition(assignedBin, X, Y, Z);
|
---|
32 | }
|
---|
33 |
|
---|
34 | public static Vector3D AlongX(Vector3D pos, PackingItem item) {
|
---|
35 | return new Vector3D(
|
---|
36 | pos.X + item.Width,
|
---|
37 | pos.Y,
|
---|
38 | pos.Z
|
---|
39 | );
|
---|
40 | }
|
---|
41 | public static Vector3D AlongY(Vector3D pos, PackingItem item) {
|
---|
42 | return new Vector3D(
|
---|
43 | pos.X,
|
---|
44 | pos.Y + item.Height,
|
---|
45 | pos.Z
|
---|
46 | );
|
---|
47 | }
|
---|
48 | public static Vector3D AlongZ(Vector3D pos, PackingItem item) {
|
---|
49 | return new Vector3D(
|
---|
50 | pos.X,
|
---|
51 | pos.Y,
|
---|
52 | pos.Z + item.Depth
|
---|
53 | );
|
---|
54 | }
|
---|
55 | public static Vector3D AlongX(PackingPosition pos, PackingItem item) {
|
---|
56 | return new Vector3D(
|
---|
57 | pos.X + item.Width,
|
---|
58 | pos.Y,
|
---|
59 | pos.Z
|
---|
60 | );
|
---|
61 | }
|
---|
62 | public static Vector3D AlongY(PackingPosition pos, PackingItem item) {
|
---|
63 | return new Vector3D(
|
---|
64 | pos.X,
|
---|
65 | pos.Y + item.Height,
|
---|
66 | pos.Z
|
---|
67 | );
|
---|
68 | }
|
---|
69 | public static Vector3D AlongZ(PackingPosition pos, PackingItem item) {
|
---|
70 | return new Vector3D(
|
---|
71 | pos.X,
|
---|
72 | pos.Y,
|
---|
73 | pos.Z + item.Depth
|
---|
74 | );
|
---|
75 | }
|
---|
76 |
|
---|
77 | public Vector3D Cross(Vector3D b) {
|
---|
78 | return new Vector3D(
|
---|
79 | Y * b.Z - Z * b.Y,
|
---|
80 | -X * b.Z + Z * b.X,
|
---|
81 | X * b.Y - Y * b.X
|
---|
82 | );
|
---|
83 | }
|
---|
84 |
|
---|
85 | public bool IsInside(PackingPosition pos, ResidualSpace rs) {
|
---|
86 | return X >= pos.X && X < pos.X + rs.Width
|
---|
87 | && Y >= pos.Y && Y < pos.Y + rs.Height
|
---|
88 | && Z >= pos.Z && Z < pos.Z + rs.Depth;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public bool IsInside(PackingPosition pos, IEnumerable<ResidualSpace> rs) {
|
---|
92 | return rs.Any(x => IsInside(pos, x));
|
---|
93 | }
|
---|
94 |
|
---|
95 | public static int operator *(Vector3D a, Vector3D b) {
|
---|
96 | return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
|
---|
97 | }
|
---|
98 | public static Vector3D operator *(int a, Vector3D b) {
|
---|
99 | return new Vector3D(a * b.X, a * b.Y, a * b.Z);
|
---|
100 | }
|
---|
101 |
|
---|
102 | public static Vector3D operator *(double a, Vector3D b) {
|
---|
103 | return new Vector3D((int)(a * b.X), (int)(a * b.Y), (int)(a * b.Z));
|
---|
104 | }
|
---|
105 |
|
---|
106 | public static Vector3D operator *(Vector3D a, int b) {
|
---|
107 | return new Vector3D(a.X * b, a.Y * b, a.Z * b);
|
---|
108 | }
|
---|
109 |
|
---|
110 | public static Vector3D operator *(Vector3D a, double b) {
|
---|
111 | return new Vector3D((int)(b * a.X), (int)(b * a.Y), (int)(b * a.Z));
|
---|
112 | }
|
---|
113 |
|
---|
114 | public static Vector3D operator +(Vector3D a, Vector3D b) {
|
---|
115 | return new Vector3D(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
|
---|
116 | }
|
---|
117 | public static Vector3D operator -(Vector3D a, Vector3D b) {
|
---|
118 | return new Vector3D(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
|
---|
119 | }
|
---|
120 |
|
---|
121 | public override bool Equals(object obj) {
|
---|
122 | var packPos = obj as PackingPosition;
|
---|
123 | if (packPos != null) {
|
---|
124 | return X == packPos.X && Y == packPos.Y && Z == packPos.Z;
|
---|
125 | }
|
---|
126 | var vec = obj as Vector3D;
|
---|
127 | if (vec != null) {
|
---|
128 | return X == vec.X && Y == vec.Y && Z == vec.Z;
|
---|
129 | }
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|