Tanoda
GetMeshInfo.cs
Go to the documentation of this file.
1using NaughtyAttributes;
2using Newtonsoft.Json;
3using System;
4using System.Collections;
5using System.Collections.Generic;
6using System.Linq;
7using UnityEngine;
8using UnityEngine.AI;
9
10public class GetMeshInfo : MonoBehaviour
11{
12 SortedDictionary<float, List<Vector3>> dist = new SortedDictionary<float, List<Vector3>>();
13 SortedDictionary<string, List<Vector3>> finalHoles = new SortedDictionary<string, List<Vector3>>();
14 SortedDictionary<string, List<Vector3>> collectHoles = new SortedDictionary<string, List<Vector3>>();
15 SortedDictionary<float, List<Vector3>> maxDistances = new SortedDictionary<float, List<Vector3>>();
16 SortedDictionary<float, List<Vector3>> holeDiameter = new SortedDictionary<float, List<Vector3>>();
17 SortedDictionary<string, List<Vector3>> filteredHoles = new SortedDictionary<string, List<Vector3>>();
18 SortedDictionary<float, List<Vector3>> HolesWithMidPoint = new SortedDictionary<float, List<Vector3>>();
19 public GameObject Hole;
20 public int HoleSize;
21 public float PercentOfModel;
22 List<Vector3> HoleCoord = new List<Vector3>();
23 List<string> HoleList = new List<string>();
24 List<Vector3> HoleCoordTeszt = new List<Vector3>();
25 Vector3 a = new Vector3();
26 Vector3 b = new Vector3();
27 Vector3 x = new Vector3();
28 Vector3 y = new Vector3();
29 Vector3 TesztX = new Vector3();
30 Vector3 TesztY = new Vector3();
31 Vector3 TesztZ = new Vector3();
32 public Vector3 TesztA = new Vector3();
33 public Vector3 TesztB = new Vector3();
34 public Vector3 TesztC = new Vector3();
35 public float distanceAB;
36 public float distanceBC;
37 public float distanceCA;
38
39 Vector3 PrevTesztX = new Vector3();
40 Vector3 PrevTesztY = new Vector3();
41 Vector3 z = new Vector3();
42 Vector3 c = new Vector3();
43 Bounds size;
44 float biggestSizeOfTheModel;
45 public int t;
46
47 [Button]
48 public void DrawTheTriangles()
49 {
50 var numberOfTriangles = Hole.GetComponent<MeshFilter>().mesh.triangles;
51 var Vertex = Hole.GetComponent<MeshFilter>().mesh.vertices;
52 int p = (t*3);
53
54 a = Vertex[numberOfTriangles[p]];
55 b = Vertex[numberOfTriangles[p + 1]];
56 c = Vertex[numberOfTriangles[p + 2]];
57 distanceAB = Vector3.Distance(a, b) *1000;
58 distanceBC = Vector3.Distance(b, c) * 1000;
59 distanceCA = Vector3.Distance(c, a) * 1000;
60 TesztA = a * 1000;
61 TesztB = b * 1000;
62 TesztC = c * 1000;
63
64
65 }
66
67 [Button]
68 private void GetInfo()
69 {
70 size = Hole.GetComponent<MeshRenderer>().bounds;
71 var numberOfVertex = Hole.GetComponent<MeshFilter>().mesh.vertexCount;
72 var Vertex = Hole.GetComponent<MeshFilter>().mesh.vertices;
73 var numberOfTriangles = Hole.GetComponent<MeshFilter>().mesh.triangles;
74 bool AlreadyAdded = false;
75 int m = 0;
76
77 float maxDistance;
78 Mesh mesh = new Mesh();
79
80
81 float[] arrayNumbers = { size.max.x, size.max.y, size.max.z };
82 biggestSizeOfTheModel = arrayNumbers.Max();
83
84 for (int i = 0; i < numberOfTriangles.Length-2; i+=3)
85 {
86 a = Vertex[numberOfTriangles[i]];
87 b = Vertex[numberOfTriangles[i+1]];
88 c = Vertex[numberOfTriangles[i+2]];
89 float distanceAB = Vector3.Distance(a, b);
90 float distanceBC = Vector3.Distance(b, c);
91 float distanceCA = Vector3.Distance(c, a);
92 dist.Add(distanceAB, new List<Vector3>() { a, b });
93 if (!dist.ContainsKey(distanceBC))
94 {
95 dist.Add(distanceBC, new List<Vector3>() { b, c });
96 }
97 if (!dist.ContainsKey(distanceCA))
98 {
99 dist.Add(distanceCA, new List<Vector3>() { c, a });
100 }
101
102 var v = dist.Keys.Min();
103 x = dist[v][0];
104 y = dist[v][1];
105 TesztX = x * 1000;
106 TesztY = y * 1000;
107 Debug.Log(PrevTesztX);
108 Debug.Log(PrevTesztY);
109 float PercentFloat = PercentOfModel / 100;
110 if (v < biggestSizeOfTheModel * PercentFloat)
111 {
112 var keyValuePair1 = collectHoles.FirstOrDefault(s => s.Value.Contains(x));
113 var keyValuePair2 = collectHoles.FirstOrDefault(s => s.Value.Contains(y));
114 var keyValuePair1List = collectHoles.Where(s => s.Value.Contains(x));
115 var keyValuePair2List = collectHoles.Where(s => s.Value.Contains(y));
116 int countX = collectHoles.Count(s => s.Value.Contains(x));
117 int countY = collectHoles.Count(s => s.Value.Contains(y));
118 if (keyValuePair1.Value != null)
119 {
120 foreach (var item in keyValuePair1List)
121 {
122 if (countX < 2 && countY < 2)
123 {
124
125 if (x.x <= y.x * 1.001f || x.x >= y.x * 0.999f || x.y <= y.y * 1.001f || x.y >= y.y * 0.999f || x.z <= y.z * 1.001f || x.z >= y.z * 0.999f)
126 {
127 var distanceSample = Vector3.Distance(item.Value[0], item.Value[1]);
128 if (v >= distanceSample * 0.98f && v <= distanceSample * 1.02f)
129 {
130 //HoleCoordTeszt.Add(TesztX);
131 //HoleCoordTeszt.Add(TesztY);
132 //PrevTesztX = x * 1000;
133 //PrevTesztY = y * 1000;
134 collectHoles[item.Key].Add(x);
135 collectHoles[item.Key].Add(y);
136 }
137 }
138 }
139 }
140
141
142 }
143 if (keyValuePair2.Value != null)
144 {
145 foreach (var item in keyValuePair2List)
146 {
147 if (countX < 2 && countY < 2)
148 {
149 if (x.x <= y.x * 1.001f || x.x >= y.x * 0.999f || x.y <= y.y * 1.001f || x.y >= y.y * 0.999f || x.z <= y.z * 1.001f || x.z >= y.z * 0.999f)
150 {
151 var distanceSample = Vector3.Distance(item.Value[0], item.Value[1]);
152 if (v >= distanceSample * 0.98f && v <= distanceSample * 1.02f)
153 {
154 //HoleCoordTeszt.Add(TesztX);
155 //HoleCoordTeszt.Add(TesztY);
156 //PrevTesztX = x * 1000;
157 //PrevTesztY = y * 1000;
158 collectHoles[item.Key].Add(x);
159 collectHoles[item.Key].Add(y);
160 }
161 }
162 }
163 }
164
165
166
167
168 }
169 if (keyValuePair1.Value == null && keyValuePair2.Value == null)
170 {
171 //HoleCoordTeszt.Add(TesztX);
172 //HoleCoordTeszt.Add(TesztY);
173 //PrevTesztX = x * 1000;
174 //PrevTesztY = y * 1000;
175 collectHoles.Add("Hole" + m, new List<Vector3>() { x, y });
176 m++;
177 }
178 // if (!dist.ContainsKey(distance))
179 // {
180 // dist.Add(distance, new List<Vector3>() { x, y });
181 // }
182 //if (dist.ContainsKey(distance))
183 //{
184 // dist[distance].Add(x);
185 // dist[distance].Add(y);
186 //}
187 }
188 dist.Clear();
189
190 }
191
192
194 //for (int i = 0; i < Vertex.Length; i++)
195 //{
196 // if (firstPoint)
197 // {
198 // x = Vertex[i];
199 // firstCord = x;
200 // //whereFindHole = i;
201 // }
202 // if (!firstPoint)
203 // {
204 // x = y;
205 // }
206 // if (alreadyFindHole && HoleCoord.Contains(x))
207 // {
208 // int count = HoleCoord.Where(s => s != null && s.Equals(x)).Count();
209 // if (count > 1)
210 // {
211 // firstPoint = true;
212 // continue;
213 // }
214 // }
215
216
217 // for (int j = 0; j < Vertex.Length; j++)
218 // {
219 // y = Vertex[j];
220 // if (x != y && c != y)
221 // {
222
223 // minDistance = Vector3.Distance(x, y);
224 // if (!dist.ContainsKey(minDistance)) ;
225 // {
226 // bool fuckup = false;
227 // try
228 // {
229 // var asd = dist[minDistance];
230
231 // if (asd != null)
232 // {
233 // fuckup = true;
234 // }
235 // }
236 // catch (Exception)
237 // {
238
239 // }
240 // if (!fuckup)
241 // dist.Add(minDistance, new List<Vector3>() { x, y });
242 // }
243
244 // }
245 // }
246 // var v = dist.Keys.Min();
247 // var keyValuePairMin = dist.FirstOrDefault(s => s.Key.Equals(v));
248 // y = keyValuePairMin.Value[1];
249 // var distance = Vector3.Distance(x, y);
250 // dist.Clear();
251 // if (distance < biggestSizeOfTheModel * 0.03f)
252 // {
253 // firstPoint = false;
254 // c = x;
255 // HoleCoord.Add(y);
256 // if (y == firstCord)
257 // {
258 // firstPoint = true;
259 // //i = whereFindHole;
260 // alreadyFindHole = true;
261 // }
262 // var keyValuePair1 = collectHoles.FirstOrDefault(s => s.Value.Contains(x));
263 // var keyValuePair2 = collectHoles.FirstOrDefault(s => s.Value.Contains(y));
264
265 // if (keyValuePair1.Value != null)
266 // {
267 // if (x.x == y.x || x.y == y.y || x.z == y.z)
268 // {
269 // if (distance * 0.98f <= Vector3.Distance(keyValuePair1.Value[0], keyValuePair1.Value[1]) && distance * 1.02f >= Vector3.Distance(keyValuePair1.Value[0], keyValuePair1.Value[1]))
270 // {
271 // collectHoles[keyValuePair1.Key].Add(x);
272 // collectHoles[keyValuePair1.Key].Add(y);
273 // }
274 // else
275 // {
276 // firstPoint = true;
277 // //i = whereFindHole;
278 // }
279
280 // }
281
282 // }
283 // if (keyValuePair2.Value != null)
284 // {
285 // if (x.x == y.x || x.y == y.y || x.z == y.z)
286 // {
287 // if (distance * 0.98f <= Vector3.Distance(keyValuePair2.Value[0], keyValuePair2.Value[1]) && distance * 1.02f >= Vector3.Distance(keyValuePair2.Value[0], keyValuePair2.Value[1]))
288 // {
289 // collectHoles[keyValuePair2.Key].Add(x);
290 // collectHoles[keyValuePair2.Key].Add(y);
291 // }
292 // else
293 // {
294 // firstPoint = true;
295 // //i = whereFindHole;
296 // }
297
298 // }
299
300 // }
301 // if (keyValuePair1.Value == null && keyValuePair2.Value == null)
302 // {
303 // collectHoles.Add("Hole" + m, new List<Vector3>() { x, y });
304 // m++;
305 // }
306 // n++;
307 // }
308 //}
309
311 //foreach (var a in Vertex)
312 //{
313 // x = a;
314
315 // foreach (var b in Vertex)
316 // {
317 // y = b;
318 // if (x != y)
319 // {
320 // var distance = Vector3.Distance(x, y);
321 // if (distance < biggestSizeOfTheModel * 0.02f)
322 // {
323 // var keyValuePair1 = collectHoles.FirstOrDefault(s => s.Value.Contains(x));
324 // var keyValuePair2 = collectHoles.FirstOrDefault(s => s.Value.Contains(y));
325 // int countX = collectHoles.Count(s => s.Value.Contains(x));
326 // int countY = collectHoles.Count(s => s.Value.Contains(y));
327 // if (keyValuePair1.Value != null)
328 // {
329 // if (countX < 2 && countY < 2)
330 // {
331 // if (x.x == y.x || x.y == y.y || x.z == y.z)
332 // {
333 // if (distance * 0.98f <= Vector3.Distance(keyValuePair1.Value[0], keyValuePair1.Value[1]) && distance * 1.02f >= Vector3.Distance(keyValuePair1.Value[0], keyValuePair1.Value[1]))
334 // {
335 // collectHoles[keyValuePair1.Key].Add(x);
336 // collectHoles[keyValuePair1.Key].Add(y);
337 // }
338
339 // }
340 // }
341
342
343 // }
344 // if (keyValuePair2.Value != null)
345 // {
346 // if (countX < 2 && countY < 2)
347 // {
348 // if (x.x == y.x || x.y == y.y || x.z == y.z)
349 // {
350 // if (distance * 0.98f <= Vector3.Distance(keyValuePair2.Value[0], keyValuePair2.Value[1]) && distance * 1.02f >= Vector3.Distance(keyValuePair2.Value[0], keyValuePair2.Value[1]))
351 // {
352 // collectHoles[keyValuePair2.Key].Add(x);
353 // collectHoles[keyValuePair2.Key].Add(y);
354 // }
355
356 // }
357 // }
358
359
360 // }
361 // if (keyValuePair1.Value == null && keyValuePair2.Value == null)
362 // {
363 // collectHoles.Add("Hole" + m, new List<Vector3>() { x, y });
364 // m++;
365 // }
366 // n++;
367 // // if (!dist.ContainsKey(distance))
368 // // {
369 // // dist.Add(distance, new List<Vector3>() { x, y });
370 // // }
371 // //if (dist.ContainsKey(distance))
372 // //{
373 // // dist[distance].Add(x);
374 // // dist[distance].Add(y);
375 // //}
376 // }
377 // }
378
379 // }
380
381 //}
383 foreach (KeyValuePair<string, List<Vector3>> keyValue in collectHoles)
384 {
385 bool isNotHole = false;
386 string key = keyValue.Key;
387 List<Vector3> values = keyValue.Value;
388 AlreadyAdded = false;
389
390 if (values.Count > HoleSize /*&& values.Count<200*/)
391 {
392 for (int j = 0; j < values.Count; j++)
393 {
394 var keyValuePairOld = collectHoles.FirstOrDefault(s => s.Value.Contains(values[j]));
395 var keyValuePairList = collectHoles.Where(s => s.Value.Contains(values[j]));
396 var keyValuePairNew = filteredHoles.FirstOrDefault(s => s.Value.Contains(values[j]));
397
398 if (keyValuePairOld.Value != null)
399 {
400 foreach (var item in keyValuePairList)
401 {
402 if (!HoleList.Contains(item.Key))
403 {
404
405 if (keyValuePairNew.Value == null)
406 {
407 TesztX = values[j] * 1000;
408 Debug.Log(HoleCoordTeszt);
409 Debug.Log(TesztX);
410 if (!AlreadyAdded)
411 {
412 filteredHoles.Add(key, values);
413 AlreadyAdded = true;
414 }
415 filteredHoles[key].AddRange(item.Value);
416 HoleList.Add(item.Key);
417 }
418 if (keyValuePairNew.Value != null)
419 {
420 filteredHoles[keyValuePairNew.Key].AddRange(item.Value);
421 HoleList.Add(item.Key);
422 }
423 }
424 }
425 }
426
427 }
428 }
429 }
431 foreach (KeyValuePair<string, List<Vector3>> keyValue in filteredHoles)
432 {
433 string key = keyValue.Key;
434 List<Vector3> values = keyValue.Value;
435 if (values.Count > HoleSize)
436 {
437 for (int i = 0; i < values.Count; i++)
438 {
439 for (int j = 0; j < values.Count; j++)
440 {
441 if (!HoleCoord.Contains(values[j]))
442 {
443 HoleCoord.Add(values[j]);
444 }
445 maxDistance = Vector3.Distance(values[i], values[j]);
446 if (!maxDistances.ContainsKey(maxDistance))
447 {
448 bool fuckup = false;
449 try
450 {
451 var asd = maxDistances[maxDistance];
452
453 if (asd != null)
454 {
455 fuckup = true;
456 }
457 }
458 catch (Exception)
459 {
460
461 }
462 if (!fuckup)
463 maxDistances.Add(maxDistance, new List<Vector3>() { values[i], values[j] });
464 }
465
466 }
467 }
468 var max = maxDistances.Keys.Max();
469 var xMax = maxDistances[max][0];
470 var yMax = maxDistances[max][1];
471 maxDistances.Clear();
472 Vector3 midPoint = new Vector3((xMax.x + yMax.x) / 2.0f, (xMax.y + yMax.y) / 2.0f, (xMax.z + yMax.z) / 2.0f);
473 var keyValuePairDiam = maxDistances.FirstOrDefault(s => s.Value.Contains(midPoint));
474 GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
475 cylinder.AddComponent<RectTransform>()/*.sizeDelta = new Vector2(max / 2, max * 3)*/;
476 cylinder.transform.position = midPoint;
477 cylinder.transform.parent = Hole.transform;
478
479 newScale(cylinder, max);
480 cylinder.SetActive(false);
481 //cylinder.transform.up = midPoint;
482 //cylinder.transform.forward = values[0];
483 cylinder.transform.up = Vector3.Cross(midPoint - values[0],midPoint - values[5]);
484
485 Plane plane = new Plane(values[0], values[1], values[2]);
486 //if (xMax.x <= yMax.x * 0.999f || xMax.x >= yMax.x * 1.001f)
487 // cylinder.transform.eulerAngles = new Vector3(0, 0, 90);
490 //if (xMax.z <= yMax.z * 0.999f || xMax.z >= yMax.z * 1.001f)
491 // cylinder.transform.eulerAngles = new Vector3(90, 0, 0);
492
493 if (!holeDiameter.ContainsKey(max))
494 {
495 bool fuckup = false;
496 try
497 {
498 var asd = holeDiameter[max];
499
500 if (asd != null)
501 {
502 fuckup = true;
503 }
504 }
505 catch (Exception)
506 {
507
508 }
509 if (!fuckup)
510 holeDiameter.Add(max, new List<Vector3>() { midPoint });
511 if (fuckup)
512 holeDiameter[max].Add(midPoint);
513 Debug.Log(max + ":" + midPoint);
514 }
515 Debug.Log(max + ":" + midPoint);
516 finalHoles.Add(key, values);
517 }
518 }
519 Debug.Log(finalHoles.Count);
520 Debug.Log(HoleCoord.Count);
521 }
522
523
524
525 public void newScale(GameObject theGameObject, float newSize)
526 {
527
528 float sizeX = theGameObject.GetComponent<Renderer>().bounds.size.x;
529 float sizeY = theGameObject.GetComponent<Renderer>().bounds.size.y;
530 float sizeZ = theGameObject.GetComponent<Renderer>().bounds.size.z;
531 Vector3 rescale = theGameObject.transform.localScale;
532
533 rescale.x = newSize * rescale.x / sizeX;
534 rescale.y = newSize * rescale.y / sizeY;
535 rescale.z = newSize * rescale.z / sizeZ;
536
537 theGameObject.transform.localScale = rescale;
538
539 }
540
541 public void OnDrawGizmos()
542 {
543 float c = 0.5f;
544 float d = 0.5f;
545 float e = 0.5f;
546 foreach (KeyValuePair<string, List<Vector3>> keyValue in finalHoles)
547 {
548 string key = keyValue.Key;
549 List<Vector3> values = keyValue.Value;
550 for (int i = 0; i < values.Count - 1; i++)
551 {
552 Gizmos.color = new Color(1, 1, 1);
553 Gizmos.DrawLine(values[i], values[i + 1]);
554 }
555 c += 0.05f;
556 d += 0.02f;
557 e += 0.07f;
558 }
559 //Gizmos.DrawLine(a, b);
560 //Gizmos.DrawLine(b, c);
561 //Gizmos.DrawLine(c, a);
562 }
563
564 }
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
void newScale(GameObject theGameObject, float newSize)
Definition: GetMeshInfo.cs:525
float distanceBC
Definition: GetMeshInfo.cs:36
float distanceAB
Definition: GetMeshInfo.cs:35
Vector3 TesztB
Definition: GetMeshInfo.cs:33
Vector3 TesztA
Definition: GetMeshInfo.cs:32
void OnDrawGizmos()
Definition: GetMeshInfo.cs:541
void DrawTheTriangles()
Definition: GetMeshInfo.cs:48
Vector3 TesztC
Definition: GetMeshInfo.cs:34
float PercentOfModel
Definition: GetMeshInfo.cs:21
float distanceCA
Definition: GetMeshInfo.cs:37
GameObject Hole
Definition: GetMeshInfo.cs:19