source: branches/HeuristicLab.BinPacking/PackingPlanVisualizations/3D/CuboidShapePreparations.cs @ 13497

Last change on this file since 13497 was 13497, checked in by gkronber, 5 years ago

#1966: fixed various problems: bugs in cloning, bugs in persistence, method names, various minor improvements of source code for readability.

File size: 13.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using SharpDX;
23
24namespace PackingPlanVisualizations {
25  public class CuboidShapePreparations {
26
27    #region Private Members
28    //Define the shape in a way so that the position is in the center of the shape.
29    //Define the 8 defining points of the cube
30    private readonly Vector3 topLeftFront;
31    private readonly Vector3 bottomLeftFront;
32    private readonly Vector3 topRightFront;
33    private readonly Vector3 bottomRightFront;
34    private readonly Vector3 topLeftBack;
35    private readonly Vector3 topRightBack;
36    private readonly Vector3 bottomLeftBack;
37    private readonly Vector3 bottomRightBack;
38
39    //Define normal vectors of the 6 surfaces
40    private readonly Vector3 frontNormal;
41    private readonly Vector3 backNormal;
42    private readonly Vector3 topNormal;
43    private readonly Vector3 bottomNormal;
44    private readonly Vector3 leftNormal;
45    private readonly Vector3 rightNormal;
46
47    //Define colors of the 6 surfaces
48    private readonly Color frontColor = Color.Black;
49    private readonly Color backColor = Color.Black;
50    private readonly Color topColor = Color.Black;
51    private readonly Color bottomColor = Color.Black;
52    private readonly Color leftColor = Color.Black;
53    private readonly Color rightColor = Color.Black;
54
55    #endregion Private Members
56
57
58    public CuboidShapePreparations(Vector3 shapeSize, Vector3 shapePosition) : this(shapeSize, shapePosition, Color.Black) { }
59    public CuboidShapePreparations(Vector3 shapeSize, Vector3 shapePosition, Color color) {
60      var halfShapeSize = shapeSize / 2;
61      //Define the shape in a way so that the position is in the center of the shape.
62      //Define the 8 defining points of the cube
63      topLeftFront = shapePosition + new Vector3(-1.0f, 1.0f, 1.0f) * halfShapeSize;
64      bottomLeftFront = shapePosition + new Vector3(-1.0f, -1.0f, 1.0f) * halfShapeSize;
65      topRightFront = shapePosition + new Vector3(1.0f, 1.0f, 1.0f) * halfShapeSize;
66      bottomRightFront = shapePosition + new Vector3(1.0f, -1.0f, 1.0f) * halfShapeSize;
67      topLeftBack = shapePosition + new Vector3(-1.0f, 1.0f, -1.0f) * halfShapeSize;
68      topRightBack = shapePosition + new Vector3(1.0f, 1.0f, -1.0f) * halfShapeSize;
69      bottomLeftBack = shapePosition + new Vector3(-1.0f, -1.0f, -1.0f) * halfShapeSize;
70      bottomRightBack = shapePosition + new Vector3(1.0f, -1.0f, -1.0f) * halfShapeSize;
71
72      //Define normal vectors of the 6 surfaces
73      frontNormal = new Vector3(0.0f, 0.0f, 1.0f) * halfShapeSize;
74      backNormal = new Vector3(0.0f, 0.0f, -1.0f) * halfShapeSize;
75      topNormal = new Vector3(0.0f, 1.0f, 0.0f) * halfShapeSize;
76      bottomNormal = new Vector3(0.0f, -1.0f, 0.0f) * halfShapeSize;
77      leftNormal = new Vector3(-1.0f, 0.0f, 0.0f) * halfShapeSize;
78      rightNormal = new Vector3(1.0f, 0.0f, 0.0f) * halfShapeSize;
79
80      //Define colors of the 6 surfaces
81      frontColor = color;
82      backColor = color;
83      topColor = color;
84      bottomColor = color;
85      leftColor = color;
86      rightColor = color;
87    }
88
89    public VertexPositionColorNormal[] CreateVertexDefinitionsForTriangles() {
90      //Definition of the actual triangles
91      VertexPositionColorNormal[] shapeVertices = new VertexPositionColorNormal[36];
92
93      // Front face.
94      shapeVertices[0] = new VertexPositionColorNormal(frontColor, topLeftFront, frontNormal);
95      shapeVertices[1] = new VertexPositionColorNormal(frontColor, bottomLeftFront, frontNormal);
96      shapeVertices[2] = new VertexPositionColorNormal(frontColor, topRightFront, frontNormal);
97
98      shapeVertices[3] = new VertexPositionColorNormal(frontColor, bottomLeftFront, frontNormal);
99      shapeVertices[4] = new VertexPositionColorNormal(frontColor, bottomRightFront, frontNormal);
100      shapeVertices[5] = new VertexPositionColorNormal(frontColor, topRightFront, frontNormal);
101
102
103      // Back face.
104      shapeVertices[6] = new VertexPositionColorNormal(backColor, topLeftBack, backNormal);
105      shapeVertices[7] = new VertexPositionColorNormal(backColor, topRightBack, backNormal);
106      shapeVertices[8] = new VertexPositionColorNormal(backColor, bottomLeftBack, backNormal);
107
108      shapeVertices[9] = new VertexPositionColorNormal(backColor, bottomLeftBack, backNormal);
109      shapeVertices[10] = new VertexPositionColorNormal(backColor, topRightBack, backNormal);
110      shapeVertices[11] = new VertexPositionColorNormal(backColor, bottomRightBack, backNormal);
111
112
113      // Top face.
114      shapeVertices[12] = new VertexPositionColorNormal(topColor, topLeftFront, topNormal);
115      shapeVertices[13] = new VertexPositionColorNormal(topColor, topRightBack, topNormal);
116      shapeVertices[14] = new VertexPositionColorNormal(topColor, topLeftBack, topNormal);
117
118      shapeVertices[15] = new VertexPositionColorNormal(topColor, topLeftFront, topNormal);
119      shapeVertices[16] = new VertexPositionColorNormal(topColor, topRightFront, topNormal);
120      shapeVertices[17] = new VertexPositionColorNormal(topColor, topRightBack, topNormal);
121
122
123      // Bottom face.
124      shapeVertices[18] = new VertexPositionColorNormal(bottomColor, bottomLeftFront, bottomNormal);
125      shapeVertices[19] = new VertexPositionColorNormal(bottomColor, bottomLeftBack, bottomNormal);
126      shapeVertices[20] = new VertexPositionColorNormal(bottomColor, bottomRightBack, bottomNormal);
127
128      shapeVertices[21] = new VertexPositionColorNormal(bottomColor, bottomLeftFront, bottomNormal);
129      shapeVertices[22] = new VertexPositionColorNormal(bottomColor, bottomRightBack, bottomNormal);
130      shapeVertices[23] = new VertexPositionColorNormal(bottomColor, bottomRightFront, bottomNormal);
131
132
133      // Left face.
134      shapeVertices[24] = new VertexPositionColorNormal(leftColor, topLeftFront, leftNormal);
135      shapeVertices[25] = new VertexPositionColorNormal(leftColor, bottomLeftBack, leftNormal);
136      shapeVertices[26] = new VertexPositionColorNormal(leftColor, bottomLeftFront, leftNormal);
137
138      shapeVertices[27] = new VertexPositionColorNormal(leftColor, topLeftBack, leftNormal);
139      shapeVertices[28] = new VertexPositionColorNormal(leftColor, bottomLeftBack, leftNormal);
140      shapeVertices[29] = new VertexPositionColorNormal(leftColor, topLeftFront, leftNormal);
141
142
143      // Right face.
144      shapeVertices[30] = new VertexPositionColorNormal(rightColor, topRightFront, rightNormal);
145      shapeVertices[31] = new VertexPositionColorNormal(rightColor, bottomRightFront, rightNormal);
146      shapeVertices[32] = new VertexPositionColorNormal(rightColor, bottomRightBack, rightNormal);
147
148      shapeVertices[33] = new VertexPositionColorNormal(rightColor, topRightBack, rightNormal);
149      shapeVertices[34] = new VertexPositionColorNormal(rightColor, topRightFront, rightNormal);
150      shapeVertices[35] = new VertexPositionColorNormal(rightColor, bottomRightBack, rightNormal);
151
152      return shapeVertices;
153    }
154
155    public VertexPositionColorNormal[] CreateVertexDefinitionsForEdgeLines() {
156      return CreateVertexDefinitionsForEdgeLines(frontColor);
157    }
158
159    public VertexPositionColorNormal[] CreateVertexDefinitionsForEdgeLines(Color color) {
160      //Definition of the actual lines
161      var shapeVertices = new VertexPositionColorNormal[48];
162
163      var topRightFrontNormal = (topNormal + rightNormal + frontNormal) / 3;
164      var topRightBackNormal = (topNormal + rightNormal + backNormal) / 3;
165      var topLeftFrontNormal = (topNormal + leftNormal + frontNormal) / 3;
166      var topLeftBackNormal = (topNormal + leftNormal + backNormal) / 3;
167      var bottomRightFrontNormal = (topNormal + rightNormal + frontNormal) / 3;
168      var bottomRightBackNormal = (topNormal + rightNormal + backNormal) / 3;
169      var bottomLeftFrontNormal = (topNormal + leftNormal + frontNormal) / 3;
170      var bottomLeftBackNormal = (topNormal + leftNormal + backNormal) / 3;
171
172      shapeVertices[0] = new VertexPositionColorNormal(color, topRightFront, topRightFrontNormal);
173      shapeVertices[1] = new VertexPositionColorNormal(color, topLeftFront, topLeftFrontNormal);
174      shapeVertices[2] = new VertexPositionColorNormal(color, topLeftFront, topLeftFrontNormal);
175      shapeVertices[3] = new VertexPositionColorNormal(color, bottomLeftFront, bottomLeftFrontNormal);
176      shapeVertices[4] = new VertexPositionColorNormal(color, bottomLeftFront, bottomLeftFrontNormal);
177      shapeVertices[5] = new VertexPositionColorNormal(color, bottomRightFront, bottomRightFrontNormal);
178      shapeVertices[6] = new VertexPositionColorNormal(color, bottomRightFront, bottomRightFrontNormal);
179      shapeVertices[7] = new VertexPositionColorNormal(color, topRightFront, topRightFrontNormal);
180
181      shapeVertices[8] = new VertexPositionColorNormal(color, topRightBack, topRightBackNormal);
182      shapeVertices[9] = new VertexPositionColorNormal(color, topLeftBack, topLeftBackNormal);
183      shapeVertices[10] = new VertexPositionColorNormal(color, topLeftBack, topLeftBackNormal);
184      shapeVertices[11] = new VertexPositionColorNormal(color, bottomLeftBack, bottomLeftBackNormal);
185      shapeVertices[12] = new VertexPositionColorNormal(color, bottomLeftBack, bottomLeftBackNormal);
186      shapeVertices[13] = new VertexPositionColorNormal(color, bottomRightBack, bottomRightBackNormal);
187      shapeVertices[14] = new VertexPositionColorNormal(color, bottomRightBack, bottomRightBackNormal);
188      shapeVertices[15] = new VertexPositionColorNormal(color, topRightBack, topRightBackNormal);
189
190      shapeVertices[16] = new VertexPositionColorNormal(color, topLeftFront, topLeftFrontNormal);
191      shapeVertices[17] = new VertexPositionColorNormal(color, topLeftBack, topLeftBackNormal);
192      shapeVertices[18] = new VertexPositionColorNormal(color, topRightFront, topRightFrontNormal);
193      shapeVertices[19] = new VertexPositionColorNormal(color, topRightBack, topRightBackNormal);
194      shapeVertices[20] = new VertexPositionColorNormal(color, topLeftFront, topLeftFrontNormal);
195      shapeVertices[21] = new VertexPositionColorNormal(color, topRightFront, topRightFrontNormal);
196      shapeVertices[22] = new VertexPositionColorNormal(color, topRightBack, topRightBackNormal);
197      shapeVertices[23] = new VertexPositionColorNormal(color, topLeftBack, topLeftBackNormal);
198
199      shapeVertices[24] = new VertexPositionColorNormal(color, bottomLeftFront, bottomLeftFrontNormal);
200      shapeVertices[25] = new VertexPositionColorNormal(color, bottomRightFront, bottomRightFrontNormal);
201      shapeVertices[26] = new VertexPositionColorNormal(color, bottomLeftBack, bottomLeftBackNormal);
202      shapeVertices[27] = new VertexPositionColorNormal(color, bottomRightBack, bottomRightBackNormal);
203      shapeVertices[28] = new VertexPositionColorNormal(color, bottomRightFront, bottomRightFrontNormal);
204      shapeVertices[29] = new VertexPositionColorNormal(color, bottomRightBack, bottomRightBackNormal);
205      shapeVertices[30] = new VertexPositionColorNormal(color, bottomLeftFront, bottomLeftFrontNormal);
206      shapeVertices[31] = new VertexPositionColorNormal(color, bottomLeftBack, bottomLeftBackNormal);
207
208      shapeVertices[32] = new VertexPositionColorNormal(color, topLeftFront, topLeftFrontNormal);
209      shapeVertices[33] = new VertexPositionColorNormal(color, topLeftBack, topLeftBackNormal);
210      shapeVertices[34] = new VertexPositionColorNormal(color, topLeftBack, topLeftBackNormal);
211      shapeVertices[35] = new VertexPositionColorNormal(color, bottomLeftBack, bottomLeftBackNormal);
212      shapeVertices[36] = new VertexPositionColorNormal(color, bottomLeftBack, bottomLeftBackNormal);
213      shapeVertices[37] = new VertexPositionColorNormal(color, bottomLeftFront, bottomLeftFrontNormal);
214      shapeVertices[38] = new VertexPositionColorNormal(color, bottomLeftFront, bottomLeftFrontNormal);
215      shapeVertices[39] = new VertexPositionColorNormal(color, topLeftFront, topLeftFrontNormal);
216
217      shapeVertices[40] = new VertexPositionColorNormal(color, topRightFront, topRightFrontNormal);
218      shapeVertices[41] = new VertexPositionColorNormal(color, topRightBack, topRightBackNormal);
219      shapeVertices[42] = new VertexPositionColorNormal(color, topRightBack, topRightBackNormal);
220      shapeVertices[43] = new VertexPositionColorNormal(color, bottomRightBack, bottomRightBackNormal);
221      shapeVertices[44] = new VertexPositionColorNormal(color, bottomRightBack, bottomRightBackNormal);
222      shapeVertices[45] = new VertexPositionColorNormal(color, bottomRightFront, bottomRightFrontNormal);
223      shapeVertices[46] = new VertexPositionColorNormal(color, bottomRightFront, bottomRightFrontNormal);
224      shapeVertices[47] = new VertexPositionColorNormal(color, topRightFront, topRightFrontNormal);
225
226      return shapeVertices;
227    }
228  }
229}
Note: See TracBrowser for help on using the repository browser.