// вхождение точки в треугольник public static bool PointInTriangle(TPoint point, TTriangle triangle, double EPS) { TPolygon poly = new TPolygon(3); poly.Dots[0].X = triangle.APoint.X; poly.Dots[0].Y = triangle.APoint.Y; poly.Dots[1].X = triangle.BPoint.X; poly.Dots[1].Y = triangle.BPoint.Y; poly.Dots[2].X = triangle.CPoint.X; poly.Dots[2].Y = triangle.CPoint.Y; return(PointInPolygon(point, poly, EPS)); }
public static void TestTriangleInitializationWithIncorrectSides() { try { TTriangle t = new TTriangle(100, 1, 1); } catch (Exception e) { Console.WriteLine("✅ TestTriangleInitializationWithIncorrectSides passed"); return; } Console.WriteLine("🚫 TestTriangleInitializationWithIncorrectSides FAILED"); }
public static void TestTriangleInitializationWithNegativeSide() { try { TTriangle t = new TTriangle(-1, 1, 1); } catch (Exception e) { Console.WriteLine("✅ TestTriangleInitializationWithNegativeSide passed"); return; } Console.WriteLine("🚫 TestTriangleInitializationWithNegativeSide FAILED"); }
public static void TestTriangleSetIncorrectValue() { try { TTriangle t = new TTriangle(1, 1, 1); t.SetA(100); } catch (Exception e) { Console.WriteLine("✅ TestTriangleSetIncorrectValue passed"); return; } Console.WriteLine("🚫 TestTriangleSetIncorrectValue FAILED"); }
public static void TestTriangleGetArea() { double expected = 6; TTriangle t = new TTriangle(3, 4, 5); double actual = t.GetArea(); if (Math.Abs(expected - actual) < 0.0001) { Console.WriteLine("✅ TestTriangleGetArea passed"); } else { Console.WriteLine("🚫 TestTriangleGetArea FAILED"); } }
public static void TestTriangleGetPerimeter() { long expected = 3; TTriangle t = new TTriangle(1, 1, 1); double actual = t.GetPerimeter(); if (Math.Abs(expected - actual) < 0.0001) { Console.WriteLine("✅ TestTriangleGetPerimeter passed"); } else { Console.WriteLine("🚫 TestTriangleGetPerimeter FAILED"); } }
// вхождение точки в треугольник public static bool PointInTriangle(TPoint point, TTriangle triangle) { return(PointInTriangle(point, triangle, MaxError)); }
public void Read(byte[] data) { int pos = 0; // read the data header CmpParser.ParseUInt32(data, ref pos); //MeshType CmpParser.ParseUInt32(data, ref pos); //SurfaceType MeshCount = CmpParser.ParseUInt16(data, ref pos); NumRefVertices = CmpParser.ParseUInt16(data, ref pos); FlexibleVertexFormat = (D3DFVF)CmpParser.ParseUInt16(data, ref pos); VertexCount = CmpParser.ParseUInt16(data, ref pos); // the FVF defines what fields are included for each vertex /*switch (FlexibleVertexFormat) * { * case D3DFVF.XYZ: * case D3DFVF.XYZ | D3DFVF.NORMAL: * case D3DFVF.XYZ | D3DFVF.TEX1: * case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.TEX1: * case D3DFVF.XYZ | D3DFVF.DIFFUSE | D3DFVF.TEX1: * case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.DIFFUSE | D3DFVF.TEX1: * case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.TEX2: * case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.DIFFUSE | D3DFVF.TEX2: * break; * default: * throw new Exception(string.Format("FVF 0x{0:X} not supported.", FlexibleVertexFormat)); * }*/ // read the mesh headers int triangleStartOffset = 0; Meshes = new TMeshHeader[MeshCount]; for (int i = 0; i < MeshCount; ++i) { TMeshHeader mesh = new TMeshHeader(); CmpParser.ParseUInt32(data, ref pos); //MaterialId mesh.StartVertex = CmpParser.ParseUInt16(data, ref pos); mesh.EndVertex = CmpParser.ParseUInt16(data, ref pos); mesh.NumRefVertices = CmpParser.ParseUInt16(data, ref pos); CmpParser.ParseUInt16(data, ref pos); //Padding mesh.TriangleStart = triangleStartOffset; triangleStartOffset += mesh.NumRefVertices; Meshes[i] = mesh; } // read the triangle data int triangleCount = NumRefVertices / 3; Triangles = new TTriangle[triangleCount]; for (int i = 0; i < triangleCount; ++i) { TTriangle triangle = new TTriangle(); triangle.Vertex1 = CmpParser.ParseUInt16(data, ref pos); triangle.Vertex3 = CmpParser.ParseUInt16(data, ref pos); triangle.Vertex2 = CmpParser.ParseUInt16(data, ref pos); Triangles[i] = triangle; } // read the vertex data try { Vertices = new TVertex[VertexCount]; for (int i = 0; i < VertexCount; ++i) { TVertex vertex = new TVertex(); vertex.FVF = FlexibleVertexFormat; vertex.Position = CmpParser.ParsePoint3D(data, ref pos); if ((FlexibleVertexFormat & D3DFVF.Normal) == D3DFVF.Normal) { vertex.Normal = CmpParser.ParseVector3D(data, ref pos); } if ((FlexibleVertexFormat & D3DFVF.Diffuse) == D3DFVF.Diffuse) { CmpParser.ParseUInt32(data, ref pos); //Diffuse } if ((FlexibleVertexFormat & D3DFVF.Tex1) == D3DFVF.Tex1) { CmpParser.ParseFloat(data, ref pos); //S CmpParser.ParseFloat(data, ref pos); //T } if ((FlexibleVertexFormat & D3DFVF.Tex2) == D3DFVF.Tex2) { CmpParser.ParseFloat(data, ref pos); //S CmpParser.ParseFloat(data, ref pos); //T CmpParser.ParseFloat(data, ref pos); //U CmpParser.ParseFloat(data, ref pos); //V } if ((FlexibleVertexFormat & D3DFVF.Tex4) == D3DFVF.Tex4) { CmpParser.ParseFloat(data, ref pos); //S CmpParser.ParseFloat(data, ref pos); //T CmpParser.ParseFloat(data, ref pos); //TangentX CmpParser.ParseFloat(data, ref pos); //TangentY CmpParser.ParseFloat(data, ref pos); //TangentZ CmpParser.ParseFloat(data, ref pos); //BinormalX CmpParser.ParseFloat(data, ref pos); //BinormalY CmpParser.ParseFloat(data, ref pos); //BinormalZ } if ((FlexibleVertexFormat & D3DFVF.Tex5) == D3DFVF.Tex5) { CmpParser.ParseFloat(data, ref pos); //S CmpParser.ParseFloat(data, ref pos); //T CmpParser.ParseFloat(data, ref pos); //U CmpParser.ParseFloat(data, ref pos); //V CmpParser.ParseFloat(data, ref pos); //TangentX CmpParser.ParseFloat(data, ref pos); //TangentY CmpParser.ParseFloat(data, ref pos); //TangentZ CmpParser.ParseFloat(data, ref pos); //BinormalX CmpParser.ParseFloat(data, ref pos); //BinormalY CmpParser.ParseFloat(data, ref pos); //BinormalZ } Vertices[i] = vertex; } } catch { //MessageBox.Show("Header has more vertices then data", "Error"); } }
public VMeshData(byte[] data) { int pos = 0; this.MeshType = Utilities.GetDWord(data, ref pos); this.SurfaceType = Utilities.GetDWord(data, ref pos); this.NumMeshes = Utilities.GetWord(data, ref pos); this.NumRefVertices = Utilities.GetWord(data, ref pos); this.FlexibleVertexFormat = Utilities.GetWord(data, ref pos); this.NumVertices = Utilities.GetWord(data, ref pos); switch (this.FlexibleVertexFormat) { case 0x102: case 0x112: case 2: case 0x12: case 0x142: case 0x152: case 530: case 0x252: { int num2 = 0; for (int i = 0; i < this.NumMeshes; i++) { TMeshHeader item = new TMeshHeader { MaterialId = Utilities.GetDWord(data, ref pos), StartVertex = Utilities.GetWord(data, ref pos), EndVertex = Utilities.GetWord(data, ref pos), NumRefVertices = Utilities.GetWord(data, ref pos), Padding = Utilities.GetWord(data, ref pos), TriangleStart = num2 }; num2 += item.NumRefVertices; this.Meshes.Add(item); } int num4 = this.NumRefVertices / 3; int StasrtAdress = pos; for (int j = 0; j < num4; j++) { TTriangle triangle = new TTriangle { Vertex1 = Utilities.GetWord(data, ref pos), Vertex2 = Utilities.GetWord(data, ref pos), Vertex3 = Utilities.GetWord(data, ref pos) }; this.Triangles.Add(triangle); } try { int SuperBuferPos = pos; for (int i = 0; i < pos; i++) { Result.SuperBuffer[i] = data[i]; } for (int k = 0; k < this.NumVertices; k++) { for (int i = 0; i < 12; i++) { Result.SuperBuffer[SuperBuferPos + i] = data[pos + i]; } SuperBuferPos += 12; TVertex vertex = new TVertex { FVF = this.FlexibleVertexFormat, X = Utilities.GetFloat(data, ref pos), Y = Utilities.GetFloat(data, ref pos), Z = Utilities.GetFloat(data, ref pos) }; if ((this.FlexibleVertexFormat & 0x10) == 0x10) { vertex.NormalX = Utilities.GetFloat(data, ref pos); vertex.NormalY = Utilities.GetFloat(data, ref pos); vertex.NormalZ = Utilities.GetFloat(data, ref pos); } if ((this.FlexibleVertexFormat & 0x40) == 0x40) { vertex.Diffuse = Utilities.GetDWord(data, ref pos); } if (DiffuseManger.diffuseDictionary.ContainsKey(k)) { byte[] temp = DiffuseManger.diffuseDictionary[k]; for (int i = 0; i < 4; i++) { Result.SuperBuffer[SuperBuferPos + i] = temp[i]; } } SuperBuferPos += 4; if ((this.FlexibleVertexFormat & 0x100) == 0x100) { for (int i = 0; i < 8; i++) { Result.SuperBuffer[SuperBuferPos + i] = data[pos + i]; } SuperBuferPos += 8; vertex.S = Utilities.GetFloat(data, ref pos); vertex.T = Utilities.GetFloat(data, ref pos); } if ((this.FlexibleVertexFormat & 0x200) == 0x200) { vertex.S = Utilities.GetFloat(data, ref pos); vertex.T = Utilities.GetFloat(data, ref pos); vertex.U = Utilities.GetFloat(data, ref pos); vertex.V = Utilities.GetFloat(data, ref pos); } this.Vertices.Add(vertex); DiffuseManger.Capacity = SuperBuferPos; } } catch { MessageBox.Show("Header has more vertices than data", "Error"); } return; } } throw new Exception($"FVF 0x{this.FlexibleVertexFormat:X} not supported."); }
/// <summary> /// Decode the VMeshData /// </summary> /// <param name="data"></param> public VMeshData(byte[] data) { int pos = 0; // Read the data header. MeshType = Utilities.GetDWord(data, ref pos); SurfaceType = Utilities.GetDWord(data, ref pos); NumMeshes = Utilities.GetWord(data, ref pos); NumRefVertices = Utilities.GetWord(data, ref pos); FlexibleVertexFormat = Utilities.GetWord(data, ref pos); NumVertices = Utilities.GetWord(data, ref pos); // The FVF defines what fields are included for each vertex. switch (FlexibleVertexFormat) { case 0x02: case 0x12: case 0x102: case 0x112: case 0x142: case 0x152: case 0x212: case 0x252: case 0x412: case 0x512: break; default: throw new Exception(String.Format("FVF 0x{0:X} not supported.", FlexibleVertexFormat)); } // Read the mesh headers. int triangleStartOffset = 0; for (int count = 0; count < NumMeshes; count++) { TMeshHeader item = new TMeshHeader(); item.MaterialId = Utilities.GetDWord(data, ref pos); item.StartVertex = Utilities.GetWord(data, ref pos); item.EndVertex = Utilities.GetWord(data, ref pos); item.NumRefVertices = Utilities.GetWord(data, ref pos); item.Padding = Utilities.GetWord(data, ref pos); item.TriangleStart = triangleStartOffset; triangleStartOffset += item.NumRefVertices; Meshes.Add(item); } // Read the triangle data int num_triangles = NumRefVertices / 3; for (int count = 0; count < num_triangles; count++) { TTriangle item = new TTriangle(); item.Vertex1 = Utilities.GetWord(data, ref pos); item.Vertex2 = Utilities.GetWord(data, ref pos); item.Vertex3 = Utilities.GetWord(data, ref pos); Triangles.Add(item); } // Read the vertex data. try { for (int count = 0; count < NumVertices; count++) { TVertex item = new TVertex(); item.FVF = FlexibleVertexFormat; item.X = Utilities.GetFloat(data, ref pos); item.Y = Utilities.GetFloat(data, ref pos); item.Z = Utilities.GetFloat(data, ref pos); if ((FlexibleVertexFormat & D3DFVF_NORMAL) == D3DFVF_NORMAL) { item.NormalX = Utilities.GetFloat(data, ref pos); item.NormalY = Utilities.GetFloat(data, ref pos); item.NormalZ = Utilities.GetFloat(data, ref pos); } if ((FlexibleVertexFormat & D3DFVF_DIFFUSE) == D3DFVF_DIFFUSE) { item.Diffuse = Utilities.GetDWord(data, ref pos); } if ((FlexibleVertexFormat & D3DFVF_TEX1) == D3DFVF_TEX1) { item.S = Utilities.GetFloat(data, ref pos); item.T = Utilities.GetFloat(data, ref pos); } if ((FlexibleVertexFormat & D3DFVF_TEX2) == D3DFVF_TEX2) { item.S = Utilities.GetFloat(data, ref pos); item.T = Utilities.GetFloat(data, ref pos); item.U = Utilities.GetFloat(data, ref pos); item.V = Utilities.GetFloat(data, ref pos); } if ((FlexibleVertexFormat & D3DFVF_TEX4) == D3DFVF_TEX4) { item.S = Utilities.GetFloat(data, ref pos); item.T = Utilities.GetFloat(data, ref pos); item.TangentX = Utilities.GetFloat(data, ref pos); item.TangentY = Utilities.GetFloat(data, ref pos); item.TangentZ = Utilities.GetFloat(data, ref pos); item.BinormalX = Utilities.GetFloat(data, ref pos); item.BinormalY = Utilities.GetFloat(data, ref pos); item.BinormalZ = Utilities.GetFloat(data, ref pos); } if ((FlexibleVertexFormat & D3DFVF_TEX5) == D3DFVF_TEX5) { item.S = Utilities.GetFloat(data, ref pos); item.T = Utilities.GetFloat(data, ref pos); item.U = Utilities.GetFloat(data, ref pos); item.V = Utilities.GetFloat(data, ref pos); item.TangentX = Utilities.GetFloat(data, ref pos); item.TangentY = Utilities.GetFloat(data, ref pos); item.TangentZ = Utilities.GetFloat(data, ref pos); item.BinormalX = Utilities.GetFloat(data, ref pos); item.BinormalY = Utilities.GetFloat(data, ref pos); item.BinormalZ = Utilities.GetFloat(data, ref pos); } Vertices.Add(item); } } catch { MessageBox.Show("Header has more vertices than data", "Error"); } }