WPF HelixTools Mesh From Obj file

82 Views Asked by At

I am fairly new to WPF, and the 3D environment. I made an app, in which I imported a quite big 3D model .obj file. Rendering it, takes between 1 to 2 minutes, meaning that the model itself has a large number of faces/triangles,as it is a solid model. Is there a way in which I can impor the obj file and I can convert it/render it as a surface model?

Thanks in advance!

This is my code :

       ModelImporter importer = new ModelImporter();
       System.Windows.Media.Media3D.Material material = new DiffuseMaterial(new SolidColorBrush(Colors.Beige));
       importer.DefaultMaterial = material;


       mymodels = new Model3DGroup();

       Part1 = importer.Load("3dmodels\\*****.obj");
       mymodels.Children.Add(Part1);


       this.Our_Model = mymodels;
1

There are 1 best solutions below

0
L Chougrani On

obj files are surface models. meaning made up of triangle/quadrangle/etc... i don't think there are tetras/hexas/etc in this format. Anyway, if you have avolumic object and want to render "only" the surface as triangles/quads... you'll need a little bit of work, here is how i do it for tetras:

  • First decompose your tetras :

      private void DecomposeTetra(ModelMesh mesh, uint[] indices)
      {
          AddFaceAdjacency(new Triangle(indices[0], indices[2], indices[1]));
          AddFaceAdjacency(new Triangle(indices[0], indices[3], indices[2]));
          AddFaceAdjacency(new Triangle(indices[0], indices[1], indices[3]));
          AddFaceAdjacency(new Triangle(indices[1], indices[2], indices[3]));
      }
    

then register the face adjacency in a Dictionary<string, Triangle>:

private void AddFaceAdjacency(Triangle t)
    {
        var combi = t.GetKeyCombinations();
        if (facesAdjacency.ContainsAny(combi, out var contained))
            facesAdjacency[contained] = null;
        else
            facesAdjacency.Add(combi[0], t);
    }

where GetKeyCombinations() returns the permutations of indices. and if a face is already registered nullify the face (meaning that this face has an adjacency -> is not on the surface)

finally create your mesh :

        private void CreateVisibleMesh(Mesh mesh)
    {
        float count = facesAdjacency.Count;
        foreach (var pair in facesAdjacency)
        {
            if (pair.Value == null)
                continue;
            mesh.Faces.Add(pair.Value);
        }
        facesAdjacency.Clear();
        facesAdjacency = null;
    }
  • You may need to adapt "CreateVisibleMesh" to fit Helix requirement (adding vertices and indices correctly).

  • This is not necessarilly the most efficient way to go (permutations and stuff) but it does the job. Finally just for helping you :

          private static string[] GetKeyCombinations(this Triangle face)
          => new string[]
          {
               $"{face.VertexIndexes[0]}|{face.VertexIndexes[1]}|{face.VertexIndexes[2]}",
               $"{face.VertexIndexes[1]}|{face.VertexIndexes[2]}|{face.VertexIndexes[0]}",
               $"{face.VertexIndexes[2]}|{face.VertexIndexes[0]}|{face.VertexIndexes[1]}",
    
               $"{face.VertexIndexes[2]}|{face.VertexIndexes[1]}|{face.VertexIndexes[0]}",
               $"{face.VertexIndexes[0]}|{face.VertexIndexes[2]}|{face.VertexIndexes[1]}",
               $"{face.VertexIndexes[1]}|{face.VertexIndexes[0]}|{face.VertexIndexes[2]}",
          };