Version

Triangulating Data

Purpose

This topic provides information on how triangulation of data points works as well as how it can improve the performance of rendering data in the XamDataChart™ control when using Scatter Area Series or Scatter Contour Series.

Understanding Triangulation

Introduction

Triangulation is a process of triangulating data points with the same values based on their X and Y locations. Consider the following simplified scenario of triangulating data:

The following is a screenshot of (-130, 30), (-130, 50), (-100, 50), (-100, 30), (-70, 40) locations plotted using the ScatterAreaSeries.

DataChart Triangulating Data 1.png

For the above five points, there are three triangles and their triangle vertex indices are as follows: (0, 1, 2), (0, 2, 3), (2, 3, 4).

The following image shows these triangles and their vertex indices.

DataChart Triangulating Data 2.png

Triangulation Source

In the XamDataChart control, the TriangulationSource class represents the data source for creating, loading, and saving triangulation. A complete data structure for TriangulationSource consist of two properties that are listed in the following table:

Property Name Property Type Description

ObservableCollection<TriangulationSourcePointRecord>

Gets or sets a collection of TriangulationSourcePointRecord objects that store points combined with numeric values from which triangulation is created.

ObservableCollection<Triangle>

Gets or sets a collection of Triangle objects that store three indices with each one corresponding to a point in the Points collection.

The TriangulationSourcePointRecord class represents one of the triangle points in triangulation. The following table lists key properties of the TriangulationSourcePointRecord class.

Property Name Property Type Description

Point

Gets or sets a point that represents a two dimensional (2-D) location of one of triangle points.

Value

float

Gets or sets a value associated with a one of triangle points.

The Triangle class represents a record with three integer values or triangle vertex indices. Each integer is an index which corresponds to a point in the Points collection. The following table lists key properties of the Triangle class.

Property Name Property Type Description

V1

integer

Gets or sets an index of the first vertex of a triangle.

V2

integer

Gets or sets an index of the second vertex of a triangle.

V3

integer

Gets or sets an index of the third vertex of a triangle.

Triangulation Files

Triangulation data can also be stored in triangulation files that use the Intermediate Triangular Irregular Network Format (or ITF for short). For more information on this file format and its specifications, please visit this website.

Triangulation Methods

The TriangulationSource class provides methods for creating triangulation, saving and loading triangulation data to and from ITF files.

The following table lists key methods in the TriangulationSource class for working with triangulation data

Method Name Description

A method for creating triangulation of data.

A method for saving triangulation of data to binary ITF file.

A method for loading triangulation of data from binary ITF file.

Note
Note:

A detailed explanation of each of these methods along with some code examples follow at end of this topic.

Pre-Triangulation Process

In the XamDataChart control, the following types of series that can use pre-triangulated data or perform triangulation of data at runtime if no triangulation source is specified:

Type of Geographic Series Description

This series draws a colored surface based on a triangulation of point data (X and Y locations) with numeric values assigned to each point. Refer to the Scatter Area Series topic for more information about this series.

This series draws colored contour lines based on a triangulation of point data (X and Y locations) with numeric values assigned to each point. Refer to the Scatter Contour Series topic for more information about this series.

These types of series provide built-in data triangulation that is automatically performed on items in the ItemsSource if no triangulation is set to the TrianglesSource property. However, computing triangulation can be a very time-consuming process, so the runtime performance will be better when specifying a TriangulationSource for this property, especially when a large number of data items are present. Therefore, you should avoid this computation at run-time by “pre-triangulating” the data and providing the triangulation.

The following table list the main steps of pre-triangulation process:

Triangulation Process Description

Creates triangulation data as a TriangulationSource object from shape files using the ShapefileConverter class.

Saves triangulation data to an ITF file that can be deployed with your application.

Loads triangulation data from an ITF file stores it as a TriangulationSource object.

After creating a TriangulationSource and completing the pre-triangulation process you can bind it to the ScatterAreaSeries or ScatterContourSeries through the TrianglesSource property. Below is an example of what that might look like:

In XAML:

<ig:ScatterAreaSeries ItemsSource="{Binding Path=Source.Points}"
                      TrianglesSource="{Binding Path=Source.Triangles}"
                      .../>

In C#:

public TriangulationSource Source { get; set; }

// After TriangulationSource is created pass it to the series.
var scatterAreaSeries = new ScatterAreaSeries();
scatterAreaSeries.ItemsSource = Source.Points;
scatterAreaSeries.TrianglesSource = Source.Triangles;

In Visual Basic:

Private Dim _source As TriangulationSource
Public Property TriangulationSource Source
    Get
        Return _source
    End Get
    Set
        _source = value
    End Set
End Property

' After TriangulationSource is created pass it to the series.
Dim scatterAreaSeries As New ScatterAreaSeries()
scatterAreaSeries.ItemsSource = Source.Points
scatterAreaSeries.TrianglesSource = Source.Triangles

Creating Triangulation from Shape Files

Overview

The TriangulationSource class provides the Create method for creating triangulation of data. This static method creates a TriangulationSource using delegates to get the Points and Triangles to create a triangulation.

Example

The following code demonstrates how to create triangulation from shape files by implementing a handler for ImportCompleted event of the ShapefileConverter class and passing delegates to get Points and Fields collections to the Create method of the TriangulationSource class.

Note
Note:

This code example uses a shape file that contains precipitation data from NOAA website and it assumes that you already know how to add shape files to your project and use it with the ShapefileConverter. Also, stores values associated with data points in “Globvalue” data column of the database (DBF) file, but the name of the data column containing values might be different in your shape file, in which case you will need to update the key passed in the Fields collections of the ShapefileConverter.

In C#:

using Infragistics.Controls.Charts;
using Infragistics.Controls.Maps;
using System.ComponentModel;

var converter = new ShapefileConverter();
converter.ImportCompleted += OnShapeFileConverterImportCompleted;
converter.ShapefileSource= new Uri("nws_precip_1day_observed_20110419.shp", System.UriKind.Relative);
converter.DatabaseSource = new Uri("nws_precip_1day_observed_20110419.dbf", System.UriKind.Relative);

void OnShapeFileConverterImportCompleted(object sender, AsyncCompletedEventArgs e)
{
    TriangulationSource triangulationSource = TriangulationSource.Create(converter.Count,
        (i) => converter[i].Points[0][0],
        (i) => Convert.ToSingle(converter[i].Fields["Globvalue"]));
}

In Visual Basic:

Imports Infragistics.Controls.Charts
Imports Infragistics.Controls.Maps
Imports System.ComponentModel

Private WithEvents converter As New ShapefileConverter()

converter.ShapefileSource = New Uri("nws_precip_1day_observed_20110419.shp", System.UriKind.RelativeOrAbsolute)
converter.DatabaseSource = New Uri("nws_precip_1day_observed_20110419.dbf", System.UriKind.RelativeOrAbsolute)

Private Sub OnShapeFileConverterImportCompleted(sender As Object, e As AsyncCompletedEventArgs) Handles converter.ImportCompleted
      Dim triangulationSource As TriangulationSource = TriangulationSource.Create(converter.Count,
                    Function(i) converter(i).Points(0)(0),
                    Function(i) Convert.ToSingle(converter(i).Fields("Globvalue")))
End Sub

Saving Triangulation to an ITF file

Overview

The TriangulationSource class provides the SaveItf method for saving triangulation of data. This static method saves a triangulation to binary ITF file that you can deploy with your application and use later for loading triangulation data.

Example

The following code demonstrates how to save triangulation data to an ITF file by providing an IsolatedStorageFileStream to the SaveItf method of the TriangulationSource class.

Note
Note:

This code example assumes an approved request for allocating appropriate space for size of the ITF file in isolated storage.

In C#:

using (IsolatedStorageFile iso = IsolatedStorageProvider.GetIsolatedStorageFile())
{
    string filePath = "TriangulatedFile.itf";
    using (var stream = new IsolatedStorageFileStream(filePath, FileMode.Create, iso))
    {
        triangulationSource.SaveItf(stream);
        stream.Close();
    }
}

In Visual Basic:

Using iso As IsolatedStorageFile = IsolatedStorageProvider.GetIsolatedStorageFile()
      Dim filePath As String = "TriangulatedFile.itf"
      Using stream = New IsolatedStorageFileStream(filePath, FileMode.Create, iso)
            triangulationSource.SaveItf(stream)
            stream.Close()
      End Using
End Using

Loading Triangulation from an ITF file

Overview

Similar to saving triangulation method, the TriangulationSource class also provides the LoadItf method for loading triangulation of data. This static method loads a triangulation data from binary ITF file.

Example

The following code demonstrates how to load triangulation from an ITF file by providing an IsolatedStorageFileStream of an ITF file and passing it to the TriangulationSource class’ LoadItf method.

Note
Note:

This code example assumes the ITF file already exists and is accessible in isolated storage.

In C#:

TriangulationSource triangulationSource;
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForAssembly();
{
    string filePath = "TriangulatedFile.itf";
    if (iso.FileExists(filePath))
    {
        using (var stream = new IsolatedStorageFileStream(filePath, FileMode.Open, iso))
        {
            triangulationSource = TriangulationSource.LoadItf(stream);
            stream.Close();
        }
    }
}

In Visual Basic:

Dim triangulationSource As TriangulationSource
Using iso As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly()
      Dim filePath As String = "TriangulatedFile.itf"
      If iso.FileExists(filePath) Then
            Using stream = New IsolatedStorageFileStream(filePath, FileMode.Open, iso)
                  triangulationSource = TriangulationSource.LoadItf(stream)
                  stream.Close()
            End Using
      End If
End Using

Using the ItfConverter method is an alternative for loading triangulation data from an ITF file as demonstrated in the following code:

In XAML:

<ig:ItfConverter x:Key="itfConverter"
                 Source="TriangulatedFile.itf" >
</ig:ItfConverter>

In C#:

var itfConverter = new ItfConverter();
itfConverter.Source = new Uri("TriangulatedFile.itf", UriKind.RelativeOrAbsolute);
TriangulationSource triangulationSource = itfConverter.TriangulationSource;

In Visual Basic:

Dim itfConverter = New ItfConverter()
itfConverter.Source = New Uri("TriangulatedFile.itf", UriKind.RelativeOrAbsolute)
Dim triangulationSource As TriangulationSource = itfConverter.TriangulationSource

Related Content

The following topics provide additional information related to this topic.

Topic Purpose

This topic provides information on how to use the ScatterAreaSeries element in the XamDataChart control.

This topic provides information on how to use the ScatterContourSeries element in the XamDataChart control.