Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: Do Mär 28, 2024 16:15

Foren-Übersicht » Programmierung » Mathematik-Forum
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 1 Beitrag ] 
Autor Nachricht
 Betreff des Beitrags: Ein paar Volumen Helper
BeitragVerfasst: Mi Jun 20, 2012 19:48 
Offline
DGL Member
Benutzeravatar

Registriert: Mo Nov 08, 2010 18:41
Beiträge: 769
Programmiersprache: Gestern
Grad ein paar kleine Helper für nen Modelcompiler geschrieben, nicht ganz schick aber dafür lesbar :P
vielleicht hilfts ja dem ein oder anderen

Code:
  1.  
  2.  
  3.         internal static List<Single> getPolyDistanceToPlane(List<Vector3> poly, Vector4 plane)
  4.         {
  5.             List<Single> dists = new List<Single>();
  6.             for (Int32 k = 0; k < poly.Count; k++)
  7.             {
  8.                 Single dot = Vector3.Dot(poly[k], plane.Xyz) - plane.W;
  9.                 dists.Add(dot);
  10.             }
  11.             return dists;
  12.         }
  13.  
  14.         internal static List<PlaneSide> getPolySideToPlane(List<Vector3> poly, Vector4 plane)
  15.         {
  16.             List<PlaneSide> sides = new List<PlaneSide>();
  17.             List<Single> dists = getPolyDistanceToPlane(poly,plane);
  18.             foreach (Single dot in dists)
  19.             {
  20.                 if (dot > Single.Epsilon)
  21.                 {
  22.                     sides.Add(PlaneSide.front);
  23.                 }
  24.                 else if (-dot > Single.Epsilon)
  25.                 {
  26.                     sides.Add(PlaneSide.back);
  27.                 }
  28.                 else
  29.                 {
  30.                     sides.Add(PlaneSide.on);
  31.                 }
  32.             }
  33.             return sides;
  34.         }
  35.  
  36.         internal static List<Vector3> intersectPolyByPlane(List<Vector3> poly, Vector4 plane)
  37.         {
  38.  
  39.             List<PlaneSide> sides = getPolySideToPlane(poly, plane);
  40.             List<Single> dists = getPolyDistanceToPlane(poly, plane);
  41.  
  42.             if ((from cc in sides where cc == PlaneSide.front select cc).Count() == 0 &&
  43.                 (from cc in sides where cc == PlaneSide.back select cc).Count() == 0)
  44.             {
  45.                 return poly;
  46.             }
  47.             if ((from cc in sides where cc == PlaneSide.front select cc).Count() == 0)
  48.             {
  49.  
  50.                 return null;
  51.             }
  52.             if ((from cc in sides where cc == PlaneSide.back select cc).Count() == 0)
  53.             {
  54.  
  55.                 return poly;
  56.             }
  57.             List<Vector3> output = new List<Vector3>();
  58.             for (Int32 i = 0; i < poly.Count; i++)
  59.             {
  60.                 Vector3 p1 = poly[i];
  61.                 if (sides[i] == PlaneSide.on)
  62.                 {
  63.                     output.Add(p1);
  64.                     continue;
  65.                 }
  66.                 if (sides[i] == PlaneSide.front)
  67.                 {
  68.                     output.Add(p1);
  69.                 }
  70.                 if (sides[(i + 1) % sides.Count] == PlaneSide.on || sides[(i + 1) % sides.Count] == sides[i])
  71.                 {
  72.                     continue;
  73.                 }
  74.                 Vector3 p2 = poly[(i + 1) % poly.Count];
  75.                 Single dot = dists[i] / (dists[i] - dists[(i + 1) % dists.Count]);
  76.                 Vector3 mid = new Vector3(0, 0, 0);
  77.                 if (Math.Abs(plane.X - 1) < Single.Epsilon)
  78.                 {
  79.                     mid.X = plane.W;
  80.                 }
  81.                 else if (Math.Abs(plane.X + 1) < Single.Epsilon)
  82.                 {
  83.                     mid.X = -plane.W;
  84.                 }
  85.                 else
  86.                 {
  87.                     mid.X = p1.X + dot * (p2.X - p1.X);
  88.                 }
  89.  
  90.                 if (Math.Abs(plane.Y - 1) < Single.Epsilon)
  91.                 {
  92.                     mid.Y = plane.W;
  93.                 }
  94.                 else if (Math.Abs(plane.Y + 1) < Single.Epsilon)
  95.                 {
  96.                     mid.Y = -plane.W;
  97.                 }
  98.                 else
  99.                 {
  100.                     mid.Y = p1.Y + dot * (p2.Y - p1.Y);
  101.                 }
  102.  
  103.                 if (Math.Abs(plane.Z - 1) < Single.Epsilon)
  104.                 {
  105.                     mid.Z = plane.W;
  106.                 }
  107.                 else if (Math.Abs(plane.Z + 1) < Single.Epsilon)
  108.                 {
  109.                     mid.Z = -plane.W;
  110.                 }
  111.                 else
  112.                 {
  113.                     mid.Z = p1.Z + dot * (p2.Z - p1.Z);
  114.                 }
  115.                 output.Add(mid);
  116.             }
  117.             return output;
  118.         }
  119.  
  120.         internal static List<Vector3> createPolyByPlane(Vector4 plane)
  121.         {
  122.             var max = Double.MinValue;
  123.             var vup = new Vector3(0, 0, 0);
  124.             if (Math.Abs(plane.X) > max)
  125.             {
  126.                 max = Math.Abs(plane.X);
  127.                 vup.Z = 1;
  128.             }
  129.             if (Math.Abs(plane.Y) > max)
  130.             {
  131.                 max = Math.Abs(plane.Y);
  132.                 vup.Z = 1;
  133.             }
  134.             if (Math.Abs(plane.Z) > max)
  135.             {
  136.                 vup.X = 1;
  137.             }
  138.  
  139.             vup = (plane.Xyz * -Vector3.Dot(vup, plane.Xyz)) + vup;
  140.             vup.Normalize();
  141.             Vector3 org = plane.Xyz * plane.W;
  142.             Vector3 vright = Vector3.Cross(vup, plane.Xyz);
  143.  
  144.  
  145.  
  146.             vup *= 64000;
  147.             vright *= 64000;
  148.  
  149.             return new List<Vector3>(new[] { org - vright + vup, org + vright + vup, org + vright - vup, org - vright - vup });
  150.         }
  151.  
  152.         internal static List<Vector3> createPolyByPlaneAndVolume(Vector4 plane, List<Vector4> volumeData)
  153.         {
  154.             List<Vector3> winding = createPolyByPlane(plane);
  155.             for (Int32 j = 0; j < volumeData.Count; j++)
  156.             {
  157.                 if (volumeData[j].Equals(plane))
  158.                 {
  159.                     continue;
  160.  
  161.                 }
  162.                 winding = intersectPolyByPlane(winding, volumeData[j]);
  163.                 if (winding == null)
  164.                 {
  165.                     break;
  166.                 }
  167.             }
  168.             if (winding != null)
  169.             {
  170.                 if (winding.Count >= 3)
  171.                 {
  172.  
  173.                     return winding;
  174.  
  175.                 }
  176.             }
  177.             return null;
  178.         }
  179.  
  180.         internal static List<Vector3> getVolumePoints(List<Vector4> volumeData)
  181.         {
  182.             List<Vector3> output = new List<Vector3>();
  183.             for (Int32 i = 0; i < volumeData.Count; i++)
  184.             {
  185.                 List<Vector3> vecs = createPolyByPlaneAndVolume(volumeData[i], volumeData);
  186.                 if (vecs != null)
  187.                 {
  188.                     if (vecs.Count >= 3)
  189.                     {
  190.                         output.AddRange(vecs);
  191.                     }
  192.                 }
  193.                
  194.             }
  195.             return output;
  196.         } 

_________________
Meine Homepage


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 1 Beitrag ] 
Foren-Übersicht » Programmierung » Mathematik-Forum


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 11 Gäste


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
cron
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.058s | 17 Queries | GZIP : On ]