0

Importing Mesh & Calculating Normals

Anonymous 10 years ago 0
This discussion was imported from CodePlex

alexalthauser wrote at 2012-04-07 02:36:

I am loading a model with borrowed code from the Helix Modelviewer example. The imported 3D model is called _currentModel. How would I go about getting the normals from the vertices in this new model?

Thanks,

-Alex


objo wrote at 2012-04-08 01:13:

you have to traverse the Model3D hierarchy and find all GeometryModel3D objects. Then read the Normals property from the MeshGeometry3D objects referenced in the Geometry properties. See the HelixToolkit.Wpf.Visual3DHelper.Traverse methods, this should make it very easy to traverse the model. 


alexalthauser wrote at 2012-04-12 01:57:

I'm trying to use the traverse method, but I'm not sure how it works. I tried:

Visual3DHelper.Traverse<T>(model3d);

with no success.  is there a tutorial somewhere?

So far I have the importer working like a charm, but I need to get to the vertex, triangle indexes, and normal information. It's strange that such useful data wouldn't be readily accessible from an imported model. I suppose there's a good reason for it?


alexalthauser wrote at 2012-04-12 21:12:

I came across this code while searching the web, it seems to do the trick.

GeometryModel3D gm3d = ModelVisualObjects.Content as GeometryModel3D;
MeshGeometry3D mesh = gm3d.Geometry as MeshGeometry3D;


objo wrote at 2012-04-14 00:05:

Right, you'll get the mesh as you show in the last posting.

You need to pass an Action<GeometryModel3D,Transform3D> to the Traverse methods, the following example should show how you could access the Positions collection of all meshes in the model:

int countVertices = 0;
Visual3DHelper.TraverseModel<GeometryModel3D>(yourModel, (geometryModel,transform) => 
  { 
    var mesh = (MeshGeometry3D)geometryModel.Geometry;
    countVertices += mesh.Positions.Count; 
  });

alexalthauser wrote at 2012-04-14 00:31:

I spoke too soon. Actually,

GeometryModel3D gm3d = ModelVisualObjects.Content as GeometryModel3D;
MeshGeometry3D mesh = gm3d.Geometry as MeshGeometry3D;

returns null.

 

// Open document
                string filename = dlg.FileName;
                // Load 3DModel
                CurrentModel = ModelImporter.Load(filename); //loads as a modelgroupobject
                modelgroupobjects.Children.Add(CurrentModel);
                ModelVisualObjects.Content = modelgroupobjects;
                
                GeometryModel3D gm3d = ModelVisualObjects.Content as GeometryModel3D;
                MeshGeometry3D mesh = gm3d.Geometry as MeshGeometry3D;

and I tried the code you posted

int countVertices = 0;
                Visual3DHelper.TraverseModel(CurrentModel, (geometryModel, transform) =>
                {
                    var mesh = geometryModel.Content as GeometryModel3D;
                    countVertices += mesh.Positions.Count;
                }); 

and I get an error underlying Visual3DHelper.TraverseModel that states: Error 2 The type arguments for method 'HelixToolkit.Visual3DHelper.TraverseModel (System.Windows.Media.Media3D.Model3D, System.Action)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

I appreciate your help.


objo wrote at 2012-04-14 00:49:

Sorry for the bugs in the example (I corrected the code).

Here is a unit test that works:

        [Test]
        public void Load_Test_ValidNumberOfVertices()
        {
            var model = ModelImporter.Load(@"Models\obj\test.obj");
            int countVertices = 0;
            Visual3DHelper.TraverseModel<GeometryModel3D>(
                model,
                (geometryModel, transform) =>
                {
                    var mesh = (MeshGeometry3D)geometryModel.Geometry;
                    countVertices += mesh.Positions.Count;
                });

            Assert.AreEqual(8, countVertices);
        }

alexalthauser wrote at 2012-04-14 01:13:

Works Great, Thanks!

              ( o )o)
             ( o )o )o)
           (o( ~~~~~~~~~o
           ( )' ~~~~~~~~'
           ( )|)        |-.
             o|         |-. \
             o|         |  \ \
              |         |
             o|         |  / /
              |         |." "
              |         |- '
              .=========.