Select triangles inside a mesh

The attached project shows how to select the triangles inside a mesh.

It creates a derived MyMesh object, overrides 2 methods of the Model (ProcessSelectionVisibleOnly, GetVisibleEntitiesFromBackBuffer) and listens to the model1.SelectionChanged event to compile the selected triangle's data.

Note:

By using the following class it's possible to highlight the triangles dynamically when moving the mouse.

 


class MyModel : Model
    {
        private bool processTriangles = false;


        public override void ProcessSelectionVisibleOnly(Rectangle selectionBox, bool firstOnly, bool invert, SelectionChangedEventArgs eventArgs, bool selectableOnly = true, bool temporarySelection = false)
        {

            processTriangles = true;
            base.ProcessSelectionVisibleOnly(selectionBox, firstOnly, invert, eventArgs, selectableOnly, temporarySelection);
            processTriangles = false;
        }
        protected override int[] GetVisibleEntitiesFromBackBuffer(Viewport viewport, byte[] rgbValues, int stride, int bpp, Rectangle selectionBox,
            bool firstOnly)
        {
            // Need to pass the selectionEntityMode

            if (!processTriangles)

                return base.GetVisibleEntitiesFromBackBuffer(viewport, rgbValues, stride, bpp, selectionBox, firstOnly);

            // Reads the visible triangles from the back buffer and selects them
            for (int i = 0; i < Entities.Count; i++)
            {
                if (Entities[i] is MyMesh)
                {
                    MyMesh myMesh = Entities[i] as MyMesh;
                    int[] indices = base.GetVisibleEntitiesFromBackBuffer(viewport, rgbValues, stride, bpp, selectionBox, firstOnly);

                    // Add the previously selected triangles to the list
                    if (addTrianglesToSelection)
                    {
                        var l = new List(myMesh.SelectedTriangles);
                        l.AddRange(indices);
                        indices = l.ToArray();
                    }

                    // Select the triangles
                    myMesh.SelectTriangles(indices);
                    break;
                }
            }

            return new int[0];
        }
        private bool addTrianglesToSelection = true;

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (ActionMode != actionType.None)
                return;
            processTriangles = true;
            addTrianglesToSelection = true;
            GetEntityUnderMouseCursor(e.Location);

            processTriangles = false;

            for (int i = 0; i < Entities.Count; i++)
            {
                var m = ((MyMesh)Entities[i]);
                if (m.needsCompileSelected)

                    m.CompileSelected(renderContext);
            }

            addTrianglesToSelection = false;

            Invalidate();
        }

    }

Previous versions of this article: Eyeshot 8Eyeshot 9

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.