Click here to Skip to main content
15,885,757 members
Articles / Desktop Programming / WPF

Fun with WPF 3D and Delaunay Triangulation

Rate me:
Please Sign up or sign in to vote.
4.96/5 (24 votes)
18 Mar 2011CPOL9 min read 87.5K   6.2K   43  
Computing, rendering, and animating the Delaunay triangulation of a random set of 3D points.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Threading.Tasks;

namespace DelaunayWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // set up the trackball
            var trackball = new Wpf3DTools.Trackball();
            trackball.EventSource = background;
            viewport.Camera.Transform = trackball.Transform;
            light.Transform = trackball.RotateTransform;
        }

        RandomTriangulation triangulation;

        void Create()
        {
            if (triangulation != null) viewport.Children.Remove(triangulation);

            int count;
            if (!int.TryParse(numPoints.Text, out count) && count < 10) count = 100;

            triangulation = RandomTriangulation.Create(count, 10);
            viewport.Children.Add(triangulation);

            infoText.Text = string.Format("{0} tetrahedrons", triangulation.Count);
        }
        
        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            Create();
        }

        private void GenerateButton_Click(object sender, RoutedEventArgs e)
        {
            Create();
        }

        private void ExpandButton_Click(object sender, RoutedEventArgs e)
        {
            triangulation.Expand();
        }
       
        private void ExpandRandomButton_Click(object sender, RoutedEventArgs e)
        {
            triangulation.ExpandRandom();
        }

        private void CollapseButton_Click(object sender, RoutedEventArgs e)
        {
            triangulation.Collapse();
        }

        private void PulseButton_Click(object sender, RoutedEventArgs e)
        {
            triangulation.Pulse();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Czech Republic Czech Republic
I have a master's degree in computer science and currently I am a PhD student. My main research interest is in computational chemistry (sort of), although I am more interested in the computational than the chemical aspects of the field.

Comments and Discussions