private Vector <double> CalculateNormal(ModelSpace.Point3D a, ModelSpace.Point3D b, ModelSpace.Point3D c) { Vector <double> v1 = Vector <double> .Build.DenseOfArray(new double[] { a.dim[0], a.dim[1], a.dim[2] }); Vector <double> v2 = Vector <double> .Build.DenseOfArray(new double[] { b.dim[0], b.dim[1], b.dim[2] }); Vector <double> v3 = Vector <double> .Build.DenseOfArray(new double[] { c.dim[0], c.dim[1], c.dim[2] }); Vector <double> A = v2 - v1; Vector <double> B = v3 - v1; Vector <double> normal = Vector <double> .Build.DenseOfArray(new double[] { A[1] * B[2] - A[2] * B[1], A[2] * B[0] - A[0] * B[2], A[0] * B[1] - A[2] * B[0] }); return(normal.Normalize(1)); }
private Model LoadOffFile(string filename) { CultureInfo Culture = (CultureInfo)CultureInfo.CurrentCulture.Clone(); Culture.NumberFormat.CurrencyDecimalSeparator = "."; StreamReader reader = new StreamReader(filename); int verticesCount = 0, facesCount = 0, edgesCount = 0; List <ModelSpace.Point3D> vertices = new List <ModelSpace.Point3D>(); List <ModelSpace.Polygon> faces = new List <ModelSpace.Polygon>(); while (!reader.EndOfStream) { string line = reader.ReadLine().ToLower(); if (line.Trim() == string.Empty || line.StartsWith("#")) { continue; } string[] vals = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (line == "off") { continue; } else { verticesCount = int.Parse(vals[0]); facesCount = int.Parse(vals[1]); edgesCount = int.Parse(vals[2]); break; } } while (!reader.EndOfStream) { string line = reader.ReadLine().ToLower(); if (line.Trim() == string.Empty || line.StartsWith("#")) { continue; } var vals = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); float x = float.Parse(vals[0], NumberStyles.Any, Culture); float y = float.Parse(vals[1], NumberStyles.Any, Culture); float z = float.Parse(vals[2], NumberStyles.Any, Culture); ModelSpace.Point3D newPoint = new ModelSpace.Point3D(); newPoint.dim = Vector <float> .Build.DenseOfArray(new float[] { x, y, z, 1 }); vertices.Add(newPoint); --verticesCount; if (verticesCount == 0) { break; } } ModelSpace.Point3D[] vertex = vertices.ToArray(); while (!reader.EndOfStream) { string line = reader.ReadLine().ToLower(); if (line.Trim() == string.Empty || line.StartsWith("#")) { continue; } var vals = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); //wyliczenie wektora normalnego Vector <double> normal = CalculateNormal(vertex[int.Parse(vals[1])], vertex[int.Parse(vals[2])], vertex[int.Parse(vals[3])]); //stworzenie pojedyńczej ściany faces.Add(new ModelSpace.Polygon(int.Parse(vals[1]), int.Parse(vals[2]), int.Parse(vals[3]), Vector <float> .Build.DenseOfArray(new float[] { (int)normal[0], (int)normal[1], (int)normal[2], 1 }))); } return(new Model(vertex, faces, (int)image.Width, (int)image.Height, scene)); }