Importing Mesh & Calculating Normals

Apr 7, 2012 at 12:36 AM
Edited Apr 7, 2012 at 12:43 AM

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

Coordinator
Apr 7, 2012 at 11:13 PM

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. 

Apr 11, 2012 at 11:57 PM
Edited Apr 12, 2012 at 12:17 AM

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?

Apr 12, 2012 at 7:12 PM

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;

Coordinator
Apr 13, 2012 at 10:05 PM
Edited Apr 13, 2012 at 10:48 PM

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; 
  });
Apr 13, 2012 at 10:31 PM
Edited Apr 13, 2012 at 10:34 PM

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.

Coordinator
Apr 13, 2012 at 10:49 PM
Edited Apr 13, 2012 at 10:49 PM

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);
        }
Apr 13, 2012 at 11:13 PM

Works Great, Thanks!

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