public static Solid FromString(string content) { var lines = content.Trim().Split('\n'); int c = 0; Triangle t = new Triangle(); List<Triangle> triangles = new List<Triangle>(); for (int i = 0; i < lines.Count(); i++) { if (c == 0) { t = new Triangle(); t.MaterialIndex = (int)Convert.ToDecimal(lines[i]); t.Points = new Vector3[3]; } else { var split = lines[i].Split(' '); t.Points[c - 1] = new Vector3() { //X = (float)Convert.ToDecimal(split[0]), // this way won't detected 'E' //Y = (float)Convert.ToDecimal(split[1]), //Z = (float)Convert.ToDecimal(split[2]) X = float.Parse(split[0]), Y = float.Parse(split[1]), Z = float.Parse(split[2]) }; } c++; if (c > 3) { c = 0; Vector3 u = t.Points[1] - t.Points[0]; Vector3 v = t.Points[2] - t.Points[0]; t.Normal = u * v; t.Normal = Vector3.Normalize(t.Normal); triangles.Add(t); } } return new Solid() { triangles = triangles }; }
private static double Intersection(Ray r, Triangle tr) { double[,] a = new double[3, 3] { {tr.Points[0].X - tr.Points[1].X, tr.Points[0].X - tr.Points[2].X, r.direction.X}, {tr.Points[0].Y - tr.Points[1].Y, tr.Points[0].Y - tr.Points[2].Y, r.direction.Y}, {tr.Points[0].Z - tr.Points[1].Z, tr.Points[0].Z - tr.Points[2].Z, r.direction.Z}}; double detA = Determinant(a); double[,] b = new double[3, 3] { {tr.Points[0].X - r.origin.X, tr.Points[0].X - tr.Points[2].X, r.direction.X}, {tr.Points[0].Y - r.origin.Y, tr.Points[0].Y - tr.Points[2].Y, r.direction.Y}, {tr.Points[0].Z - r.origin.Z, tr.Points[0].Z - tr.Points[2].Z, r.direction.Z}}; double detB = Determinant(b) / detA; if (detB < 0) return -1; double[,] y = new double[3, 3] { {tr.Points[0].X - tr.Points[1].X, tr.Points[0].X - r.origin.X, r.direction.X}, {tr.Points[0].Y - tr.Points[1].Y, tr.Points[0].Y - r.origin.Y, r.direction.Y}, {tr.Points[0].Z - tr.Points[1].Z, tr.Points[0].Z - r.origin.Z, r.direction.Z}}; double detY = Determinant(y) / detA; if (detY < 0) return -1; if (detB + detY >= 1) return -1; double[,] t = new double[3, 3] { {tr.Points[0].X - tr.Points[1].X, tr.Points[0].X - tr.Points[2].X, tr.Points[0].X - r.origin.X}, {tr.Points[0].Y - tr.Points[1].Y, tr.Points[0].Y - tr.Points[2].Y, tr.Points[0].Y - r.origin.Y}, {tr.Points[0].Z - tr.Points[1].Z, tr.Points[0].Z - tr.Points[2].Z, tr.Points[0].Z - r.origin.Z}}; double detT = Determinant(t) / detA; if (detT < EPSILON) return -1; return detT; }