private static Shapes.SimpleMesh FlipShape(Shapes.IMesh mesh) { Shapes.SimpleMesh modifiedMesh = Shapes.SimpleMesh.CreateFrom(mesh); uint[] positiveValues = new uint[3]; foreach (Vector3 vector in modifiedMesh.Vertices) { for (int i = 2; i >= 0; i--) { if (vector[i] >= 0) { positiveValues[i]++; } } } // Most mass should be on the left/ in the negative space. uint half = modifiedMesh.VertexCount >> 1; Vector3 flip = new Vector3(); for (int i = 2; i >= 0; i--) { flip[i] = (positiveValues[i] > half) ? -1 : 1; } // Flip the direction of vertices if more mass is on the positive side. if (flip != Vector3.One) { for (uint i = 0; i < modifiedMesh.VertexCount; i++) { modifiedMesh.SetVertex(i, modifiedMesh.GetVertex(i) * flip); } } return(modifiedMesh); }
/// <summary> /// Refines a mesh and overwrites its file. /// </summary> /// <param name="mesh">The mesh.</param> /// <param name="file">The fileinfo.</param> public void RefineMesh(Shapes.IMesh mesh, FileInfo file) { if (mesh == null) { throw new ArgumentNullException(nameof(mesh)); } if (file == null) { throw new ArgumentNullException(nameof(file)); } if (!file.Exists) { throw new ArgumentException(Resources.EX_FileNotLoad, file.FullName); } Shapes.SimpleMesh transformed = ScaleShape( FlipShape( AlignShape( CenterShape(mesh) ) ) ); transformed.IsNormalised = true; Settings.FileManager.Write(file.FullName, transformed); }
private static Shapes.SimpleMesh CenterShape(Shapes.IMesh mesh) { uint vertices = mesh.VertexCount; Vector3 center = FindBaryCenter(mesh); Vector3[] points = new Vector3[vertices]; for (uint i = 0; i < vertices; i++) { points[i] = mesh.GetVertex(i) - center; } Shapes.SimpleMesh modifiedMesh = Shapes.SimpleMesh.CreateFrom(mesh); modifiedMesh.Vertices = points; return(modifiedMesh); }
private static Shapes.SimpleMesh AlignShape(Shapes.SimpleMesh mesh) { // Prepare matrix of all the vectors to present to PCA. double[][] matrix = new double[mesh.VertexCount][]; for (uint i = 0; i < mesh.VertexCount; i++) { matrix[i] = mesh.GetVertex(i).AsArrayD(); } // Call PCA using The Accord library. PrincipalComponentAnalysis pca = new PrincipalComponentAnalysis(PrincipalComponentMethod.Center, false, 3); MultivariateLinearRegression regression = pca.Learn(matrix); double[][] transformed = regression.Transform(matrix); Vector3[] vectors = transformed.Vectorize(); // Provide the new positions into the mesh. Shapes.SimpleMesh simple = Shapes.SimpleMesh.CreateFrom(mesh); simple.Vertices = vectors; return(simple); }
private static Shapes.SimpleMesh ScaleShape(Shapes.IMesh mesh) { IBoundingBox bb = mesh.GetBoundingBox(); float min = NumberUtil.Min(bb.MinX, bb.MinY, bb.MinZ); float max = NumberUtil.Max(bb.MaxX, bb.MaxY, bb.MaxZ); float dif = (MAX_VALUE - MIN_VALUE) / (max - min); Vector3 minVector = new Vector3(min, min, min); Vector3 minExpVector = new Vector3(MIN_VALUE, MIN_VALUE, MIN_VALUE); Vector3[] points = new Vector3[mesh.VertexCount]; for (int i = points.Length - 1; i >= 0; i--) { points[i] = (mesh.GetVertex((uint)i) - minVector) * dif + minExpVector; } Shapes.SimpleMesh modifiedMesh = Shapes.SimpleMesh.CreateFrom(mesh); modifiedMesh.Vertices = points; return(modifiedMesh); }