Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/PackingPlanVisualizations/3D/CuboidShapePreperations.cs @ 13028

Last change on this file since 13028 was 13028, checked in by gkronber, 8 years ago

#1966

  • added PackingPlanVisualizations plugin received from jhelm.
  • this project also contains necessary binaries of SharpDX
  • visualization in HL works now
File size: 17.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using SharpDX;
6
7namespace PackingPlanVisualizations {
8  public class CuboidShapePreparations {
9
10    #region Private Members
11    //Define the shape in a way so that the position is in the center of the shape.
12    //Define the 8 defining points of the cube
13    Vector3 topLeftFront;
14    Vector3 bottomLeftFront;
15    Vector3 topRightFront;
16    Vector3 bottomRightFront;
17    Vector3 topLeftBack;
18    Vector3 topRightBack;
19    Vector3 bottomLeftBack;
20    Vector3 bottomRightBack;
21
22    //Define normal vectors of the 6 surfaces
23    Vector3 frontNormal;
24    Vector3 backNormal;
25    Vector3 topNormal;
26    Vector3 bottomNormal;
27    Vector3 leftNormal;
28    Vector3 rightNormal;
29
30    //Define colors of the 6 surfaces
31    Color frontColor = Color.Black;
32    Color backColor = Color.Black;
33    Color topColor = Color.Black;
34    Color bottomColor = Color.Black;
35    Color leftColor = Color.Black;
36    Color rightColor = Color.Black;
37
38    //Define positions of the textures
39    Vector2 textureTopLeft;
40    Vector2 textureTopRight;
41    Vector2 textureBottomLeft;
42    Vector2 textureBottomRight;
43    #endregion Private Members
44
45
46    public CuboidShapePreparations(Vector3 shapeSize, Vector3 shapePosition) : this (shapeSize, shapePosition, Color.Black) { }
47    public CuboidShapePreparations(Vector3 shapeSize, Vector3 shapePosition, Color color) {
48      var halfShapeSize = shapeSize / 2;
49      //Define the shape in a way so that the position is in the center of the shape.
50      //Define the 8 defining points of the cube
51      topLeftFront = shapePosition + new Vector3(-1.0f, 1.0f, 1.0f) * halfShapeSize;
52      bottomLeftFront = shapePosition + new Vector3(-1.0f, -1.0f, 1.0f) * halfShapeSize;
53      topRightFront = shapePosition + new Vector3(1.0f, 1.0f, 1.0f) * halfShapeSize;
54      bottomRightFront = shapePosition + new Vector3(1.0f, -1.0f, 1.0f) * halfShapeSize;
55      topLeftBack = shapePosition + new Vector3(-1.0f, 1.0f, -1.0f) * halfShapeSize;
56      topRightBack = shapePosition + new Vector3(1.0f, 1.0f, -1.0f) * halfShapeSize;
57      bottomLeftBack = shapePosition + new Vector3(-1.0f, -1.0f, -1.0f) * halfShapeSize;
58      bottomRightBack = shapePosition + new Vector3(1.0f, -1.0f, -1.0f) * halfShapeSize;
59
60      //Define normal vectors of the 6 surfaces
61      frontNormal = new Vector3(0.0f, 0.0f, 1.0f) * halfShapeSize;
62      backNormal = new Vector3(0.0f, 0.0f, -1.0f) * halfShapeSize;
63      topNormal = new Vector3(0.0f, 1.0f, 0.0f) * halfShapeSize;
64      bottomNormal = new Vector3(0.0f, -1.0f, 0.0f) * halfShapeSize;
65      leftNormal = new Vector3(-1.0f, 0.0f, 0.0f) * halfShapeSize;
66      rightNormal = new Vector3(1.0f, 0.0f, 0.0f) * halfShapeSize;
67
68      //Define colors of the 6 surfaces
69      frontColor = color;
70      backColor = color;
71      topColor = color;
72      bottomColor = color;
73      leftColor = color;
74      rightColor = color;
75
76      //Define positions of the textures
77      textureTopLeft = new Vector2(0.5f * halfShapeSize.X, 0.0f * halfShapeSize.Y);
78      textureTopRight = new Vector2(0.0f * halfShapeSize.X, 0.0f * halfShapeSize.Y);
79      textureBottomLeft = new Vector2(0.5f * halfShapeSize.X, 0.5f * halfShapeSize.Y);
80      textureBottomRight = new Vector2(0.0f * halfShapeSize.X, 0.5f * halfShapeSize.Y);
81    }
82
83    public VertexPositionColorNormal[] CreateVertexDefinitionsForTriangles () {
84      //Definition of the actual triangles
85      VertexPositionColorNormal[] shapeVertices = new VertexPositionColorNormal[36];
86
87      // Front face.
88      shapeVertices[0] = new VertexPositionColorNormal(frontColor, topLeftFront, frontNormal);
89      shapeVertices[1] = new VertexPositionColorNormal(frontColor, bottomLeftFront, frontNormal);
90      shapeVertices[2] = new VertexPositionColorNormal(frontColor, topRightFront, frontNormal);
91
92      shapeVertices[3] = new VertexPositionColorNormal(frontColor, bottomLeftFront, frontNormal);
93      shapeVertices[4] = new VertexPositionColorNormal(frontColor, bottomRightFront, frontNormal);
94      shapeVertices[5] = new VertexPositionColorNormal(frontColor, topRightFront, frontNormal);
95
96
97      // Back face.
98      shapeVertices[6] = new VertexPositionColorNormal(backColor, topLeftBack, backNormal);
99      shapeVertices[7] = new VertexPositionColorNormal(backColor, topRightBack, backNormal);
100      shapeVertices[8] = new VertexPositionColorNormal(backColor, bottomLeftBack, backNormal);
101
102      shapeVertices[9] = new VertexPositionColorNormal(backColor, bottomLeftBack, backNormal);
103      shapeVertices[10] = new VertexPositionColorNormal(backColor, topRightBack, backNormal);
104      shapeVertices[11] = new VertexPositionColorNormal(backColor, bottomRightBack, backNormal);
105
106
107      // Top face.
108      shapeVertices[12] = new VertexPositionColorNormal(topColor, topLeftFront, topNormal);
109      shapeVertices[13] = new VertexPositionColorNormal(topColor, topRightBack, topNormal);
110      shapeVertices[14] = new VertexPositionColorNormal(topColor, topLeftBack, topNormal);
111
112      shapeVertices[15] = new VertexPositionColorNormal(topColor, topLeftFront, topNormal);
113      shapeVertices[16] = new VertexPositionColorNormal(topColor, topRightFront, topNormal);
114      shapeVertices[17] = new VertexPositionColorNormal(topColor, topRightBack, topNormal);
115
116
117      // Bottom face.
118      shapeVertices[18] = new VertexPositionColorNormal(bottomColor, bottomLeftFront, bottomNormal);
119      shapeVertices[19] = new VertexPositionColorNormal(bottomColor, bottomLeftBack, bottomNormal);
120      shapeVertices[20] = new VertexPositionColorNormal(bottomColor, bottomRightBack, bottomNormal);
121
122      shapeVertices[21] = new VertexPositionColorNormal(bottomColor, bottomLeftFront, bottomNormal);
123      shapeVertices[22] = new VertexPositionColorNormal(bottomColor, bottomRightBack, bottomNormal);
124      shapeVertices[23] = new VertexPositionColorNormal(bottomColor, bottomRightFront, bottomNormal);
125
126
127      // Left face.
128      shapeVertices[24] = new VertexPositionColorNormal(leftColor, topLeftFront, leftNormal);
129      shapeVertices[25] = new VertexPositionColorNormal(leftColor, bottomLeftBack, leftNormal);
130      shapeVertices[26] = new VertexPositionColorNormal(leftColor, bottomLeftFront, leftNormal);
131
132      shapeVertices[27] = new VertexPositionColorNormal(leftColor, topLeftBack, leftNormal);
133      shapeVertices[28] = new VertexPositionColorNormal(leftColor, bottomLeftBack, leftNormal);
134      shapeVertices[29] = new VertexPositionColorNormal(leftColor, topLeftFront, leftNormal);
135
136
137      // Right face.
138      shapeVertices[30] = new VertexPositionColorNormal(rightColor, topRightFront, rightNormal);
139      shapeVertices[31] = new VertexPositionColorNormal(rightColor, bottomRightFront, rightNormal);
140      shapeVertices[32] = new VertexPositionColorNormal(rightColor, bottomRightBack, rightNormal);
141
142      shapeVertices[33] = new VertexPositionColorNormal(rightColor, topRightBack, rightNormal);
143      shapeVertices[34] = new VertexPositionColorNormal(rightColor, topRightFront, rightNormal);
144      shapeVertices[35] = new VertexPositionColorNormal(rightColor, bottomRightBack, rightNormal);
145
146      return shapeVertices;
147    }
148
149    public VertexPositionColorNormal[] CreateVertexDefinitionsForEdgeLines() {
150      return CreateVertexDefinitionsForEdgeLines(frontColor);
151    }
152
153    public VertexPositionColorNormal[] CreateVertexDefinitionsForEdgeLines(Color color) {
154      //Definition of the actual lines
155      VertexPositionColorNormal[] shapeVertices = new VertexPositionColorNormal[48];
156
157      //shapeVertices[0] = new VertexPositionColorNormal(color, topRightFront, frontNormal);
158      //shapeVertices[1] = new VertexPositionColorNormal(color, topLeftFront, frontNormal);
159      //shapeVertices[2] = new VertexPositionColorNormal(color, topLeftFront, frontNormal);
160      //shapeVertices[3] = new VertexPositionColorNormal(color, bottomLeftFront, frontNormal);
161      //shapeVertices[4] = new VertexPositionColorNormal(color, bottomLeftFront, frontNormal);
162      //shapeVertices[5] = new VertexPositionColorNormal(color, bottomRightFront, frontNormal);
163      //shapeVertices[6] = new VertexPositionColorNormal(color, bottomRightFront, frontNormal);
164      //shapeVertices[7] = new VertexPositionColorNormal(color, topRightFront, frontNormal);
165
166      //shapeVertices[8] = new VertexPositionColorNormal(color, topRightBack, backNormal);
167      //shapeVertices[9] = new VertexPositionColorNormal(color, topLeftBack, backNormal);
168      //shapeVertices[10] = new VertexPositionColorNormal(color, topLeftBack, backNormal);
169      //shapeVertices[11] = new VertexPositionColorNormal(color, bottomLeftBack, backNormal);
170      //shapeVertices[12] = new VertexPositionColorNormal(color, bottomLeftBack, backNormal);
171      //shapeVertices[13] = new VertexPositionColorNormal(color, bottomRightBack, backNormal);
172      //shapeVertices[14] = new VertexPositionColorNormal(color, bottomRightBack, backNormal);
173      //shapeVertices[15] = new VertexPositionColorNormal(color, topRightBack, backNormal);
174
175      //shapeVertices[16] = new VertexPositionColorNormal(color, topLeftFront, topNormal);
176      //shapeVertices[17] = new VertexPositionColorNormal(color, topLeftBack, topNormal);
177      //shapeVertices[18] = new VertexPositionColorNormal(color, topRightFront, topNormal);
178      //shapeVertices[19] = new VertexPositionColorNormal(color, topRightBack, topNormal);
179      //shapeVertices[20] = new VertexPositionColorNormal(color, topLeftFront, topNormal);
180      //shapeVertices[21] = new VertexPositionColorNormal(color, topRightFront, topNormal);
181      //shapeVertices[22] = new VertexPositionColorNormal(color, topRightBack, topNormal);
182      //shapeVertices[23] = new VertexPositionColorNormal(color, topLeftBack, topNormal);
183
184      //shapeVertices[24] = new VertexPositionColorNormal(color, bottomLeftFront, bottomNormal);
185      //shapeVertices[25] = new VertexPositionColorNormal(color, bottomRightFront, bottomNormal);
186      //shapeVertices[26] = new VertexPositionColorNormal(color, bottomLeftBack, bottomNormal);
187      //shapeVertices[27] = new VertexPositionColorNormal(color, bottomRightBack, bottomNormal);
188      //shapeVertices[28] = new VertexPositionColorNormal(color, bottomRightFront, bottomNormal);
189      //shapeVertices[29] = new VertexPositionColorNormal(color, bottomRightBack, bottomNormal);
190      //shapeVertices[30] = new VertexPositionColorNormal(color, bottomLeftFront, bottomNormal);
191      //shapeVertices[31] = new VertexPositionColorNormal(color, bottomLeftBack, bottomNormal);
192
193      //shapeVertices[32] = new VertexPositionColorNormal(color, topLeftFront, leftNormal);
194      //shapeVertices[33] = new VertexPositionColorNormal(color, topLeftBack, leftNormal);
195      //shapeVertices[34] = new VertexPositionColorNormal(color, topLeftBack, leftNormal);
196      //shapeVertices[35] = new VertexPositionColorNormal(color, bottomLeftBack, leftNormal);
197      //shapeVertices[36] = new VertexPositionColorNormal(color, bottomLeftBack, leftNormal);
198      //shapeVertices[37] = new VertexPositionColorNormal(color, bottomLeftFront, leftNormal);
199      //shapeVertices[38] = new VertexPositionColorNormal(color, bottomLeftFront, leftNormal);
200      //shapeVertices[39] = new VertexPositionColorNormal(color, topLeftFront, leftNormal);
201
202      //shapeVertices[40] = new VertexPositionColorNormal(color, topRightFront, rightNormal);
203      //shapeVertices[41] = new VertexPositionColorNormal(color, topRightBack, rightNormal);
204      //shapeVertices[42] = new VertexPositionColorNormal(color, topRightBack, rightNormal);
205      //shapeVertices[43] = new VertexPositionColorNormal(color, bottomRightBack, rightNormal);
206      //shapeVertices[44] = new VertexPositionColorNormal(color, bottomRightBack, rightNormal);
207      //shapeVertices[45] = new VertexPositionColorNormal(color, bottomRightFront, rightNormal);
208      //shapeVertices[46] = new VertexPositionColorNormal(color, bottomRightFront, rightNormal);
209      //shapeVertices[47] = new VertexPositionColorNormal(color, topRightFront, rightNormal);
210
211      Vector3 topRightFrontNormal = (topNormal + rightNormal + frontNormal) / 3;
212      Vector3 topRightBackNormal = (topNormal + rightNormal + backNormal) / 3;
213      Vector3 topLeftFrontNormal = (topNormal + leftNormal + frontNormal) / 3;
214      Vector3 topLeftBackNormal = (topNormal + leftNormal + backNormal) / 3;
215      Vector3 bottomRightFrontNormal = (topNormal + rightNormal + frontNormal) / 3;
216      Vector3 bottomRightBackNormal = (topNormal + rightNormal + backNormal) / 3;
217      Vector3 bottomLeftFrontNormal = (topNormal + leftNormal + frontNormal) / 3;
218      Vector3 bottomLeftBackNormal = (topNormal + leftNormal + backNormal) / 3;
219
220      shapeVertices[0] = new VertexPositionColorNormal(color, topRightFront, topRightFrontNormal);
221      shapeVertices[1] = new VertexPositionColorNormal(color, topLeftFront, topLeftFrontNormal);
222      shapeVertices[2] = new VertexPositionColorNormal(color, topLeftFront, topLeftFrontNormal);
223      shapeVertices[3] = new VertexPositionColorNormal(color, bottomLeftFront, bottomLeftFrontNormal);
224      shapeVertices[4] = new VertexPositionColorNormal(color, bottomLeftFront, bottomLeftFrontNormal);
225      shapeVertices[5] = new VertexPositionColorNormal(color, bottomRightFront, bottomRightFrontNormal);
226      shapeVertices[6] = new VertexPositionColorNormal(color, bottomRightFront, bottomRightFrontNormal);
227      shapeVertices[7] = new VertexPositionColorNormal(color, topRightFront, topRightFrontNormal);
228
229      shapeVertices[8] = new VertexPositionColorNormal(color, topRightBack, topRightBackNormal);
230      shapeVertices[9] = new VertexPositionColorNormal(color, topLeftBack, topLeftBackNormal);
231      shapeVertices[10] = new VertexPositionColorNormal(color, topLeftBack, topLeftBackNormal);
232      shapeVertices[11] = new VertexPositionColorNormal(color, bottomLeftBack, bottomLeftBackNormal);
233      shapeVertices[12] = new VertexPositionColorNormal(color, bottomLeftBack, bottomLeftBackNormal);
234      shapeVertices[13] = new VertexPositionColorNormal(color, bottomRightBack, bottomRightBackNormal);
235      shapeVertices[14] = new VertexPositionColorNormal(color, bottomRightBack, bottomRightBackNormal);
236      shapeVertices[15] = new VertexPositionColorNormal(color, topRightBack, topRightBackNormal);
237
238      shapeVertices[16] = new VertexPositionColorNormal(color, topLeftFront, topLeftFrontNormal);
239      shapeVertices[17] = new VertexPositionColorNormal(color, topLeftBack, topLeftBackNormal);
240      shapeVertices[18] = new VertexPositionColorNormal(color, topRightFront, topRightFrontNormal);
241      shapeVertices[19] = new VertexPositionColorNormal(color, topRightBack, topRightBackNormal);
242      shapeVertices[20] = new VertexPositionColorNormal(color, topLeftFront, topLeftFrontNormal);
243      shapeVertices[21] = new VertexPositionColorNormal(color, topRightFront, topRightFrontNormal);
244      shapeVertices[22] = new VertexPositionColorNormal(color, topRightBack, topRightBackNormal);
245      shapeVertices[23] = new VertexPositionColorNormal(color, topLeftBack, topLeftBackNormal);
246
247      shapeVertices[24] = new VertexPositionColorNormal(color, bottomLeftFront, bottomLeftFrontNormal);
248      shapeVertices[25] = new VertexPositionColorNormal(color, bottomRightFront, bottomRightFrontNormal);
249      shapeVertices[26] = new VertexPositionColorNormal(color, bottomLeftBack, bottomLeftBackNormal);
250      shapeVertices[27] = new VertexPositionColorNormal(color, bottomRightBack, bottomRightBackNormal);
251      shapeVertices[28] = new VertexPositionColorNormal(color, bottomRightFront, bottomRightFrontNormal);
252      shapeVertices[29] = new VertexPositionColorNormal(color, bottomRightBack, bottomRightBackNormal);
253      shapeVertices[30] = new VertexPositionColorNormal(color, bottomLeftFront, bottomLeftFrontNormal);
254      shapeVertices[31] = new VertexPositionColorNormal(color, bottomLeftBack, bottomLeftBackNormal);
255
256      shapeVertices[32] = new VertexPositionColorNormal(color, topLeftFront, topLeftFrontNormal);
257      shapeVertices[33] = new VertexPositionColorNormal(color, topLeftBack, topLeftBackNormal);
258      shapeVertices[34] = new VertexPositionColorNormal(color, topLeftBack, topLeftBackNormal);
259      shapeVertices[35] = new VertexPositionColorNormal(color, bottomLeftBack, bottomLeftBackNormal);
260      shapeVertices[36] = new VertexPositionColorNormal(color, bottomLeftBack, bottomLeftBackNormal);
261      shapeVertices[37] = new VertexPositionColorNormal(color, bottomLeftFront, bottomLeftFrontNormal);
262      shapeVertices[38] = new VertexPositionColorNormal(color, bottomLeftFront, bottomLeftFrontNormal);
263      shapeVertices[39] = new VertexPositionColorNormal(color, topLeftFront, topLeftFrontNormal);
264
265      shapeVertices[40] = new VertexPositionColorNormal(color, topRightFront, topRightFrontNormal);
266      shapeVertices[41] = new VertexPositionColorNormal(color, topRightBack, topRightBackNormal);
267      shapeVertices[42] = new VertexPositionColorNormal(color, topRightBack, topRightBackNormal);
268      shapeVertices[43] = new VertexPositionColorNormal(color, bottomRightBack, bottomRightBackNormal);
269      shapeVertices[44] = new VertexPositionColorNormal(color, bottomRightBack, bottomRightBackNormal);
270      shapeVertices[45] = new VertexPositionColorNormal(color, bottomRightFront, bottomRightFrontNormal);
271      shapeVertices[46] = new VertexPositionColorNormal(color, bottomRightFront, bottomRightFrontNormal);
272      shapeVertices[47] = new VertexPositionColorNormal(color, topRightFront, topRightFrontNormal);
273
274      return shapeVertices;
275    }
276
277
278  }
279}
Note: See TracBrowser for help on using the repository browser.