Skip to content

Commit

Permalink
Add simple example of how to use the library.
Browse files Browse the repository at this point in the history
  • Loading branch information
speps committed Nov 17, 2014
1 parent 2c3840e commit 1d83cb0
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 18 deletions.
24 changes: 6 additions & 18 deletions LibTessDotNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,26 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Misc", "Misc", "{1E6E1C79-6
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TessExample", "TessExample\TessExample.csproj", "{BB1005DC-C911-4E65-B3B8-7316357FD254}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5EE39029-873A-45A0-9259-2198BF8729F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5EE39029-873A-45A0-9259-2198BF8729F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5EE39029-873A-45A0-9259-2198BF8729F4}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{5EE39029-873A-45A0-9259-2198BF8729F4}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{5EE39029-873A-45A0-9259-2198BF8729F4}.Debug|x86.ActiveCfg = Debug|Any CPU
{5EE39029-873A-45A0-9259-2198BF8729F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5EE39029-873A-45A0-9259-2198BF8729F4}.Release|Any CPU.Build.0 = Release|Any CPU
{5EE39029-873A-45A0-9259-2198BF8729F4}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{5EE39029-873A-45A0-9259-2198BF8729F4}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{5EE39029-873A-45A0-9259-2198BF8729F4}.Release|x86.ActiveCfg = Release|Any CPU
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Debug|Any CPU.ActiveCfg = Debug|x86
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Debug|Any CPU.Build.0 = Debug|x86
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Debug|Mixed Platforms.Build.0 = Debug|x86
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Debug|x86.ActiveCfg = Debug|x86
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Debug|x86.Build.0 = Debug|x86
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Release|Any CPU.ActiveCfg = Release|x86
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Release|Any CPU.Build.0 = Release|x86
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Release|Mixed Platforms.ActiveCfg = Release|x86
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Release|Mixed Platforms.Build.0 = Release|x86
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Release|x86.ActiveCfg = Release|x86
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Release|x86.Build.0 = Release|x86
{BB1005DC-C911-4E65-B3B8-7316357FD254}.Debug|Any CPU.ActiveCfg = Debug|x86
{BB1005DC-C911-4E65-B3B8-7316357FD254}.Debug|Any CPU.Build.0 = Debug|x86
{BB1005DC-C911-4E65-B3B8-7316357FD254}.Release|Any CPU.ActiveCfg = Release|x86
{BB1005DC-C911-4E65-B3B8-7316357FD254}.Release|Any CPU.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
67 changes: 67 additions & 0 deletions TessExample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Drawing;

namespace TessExample
{
class Program
{
// The data array contains 4 values, it's the associated data of the vertices that resulted in an intersection.
private static object VertexCombine(LibTessDotNet.Vec3 position, object[] data, float[] weights)
{
// Fetch the vertex data.
var colors = new Color[] { (Color)data[0], (Color)data[1], (Color)data[2], (Color)data[3] };
// Interpolate with the 4 weights.
var rgba = new float[] {
colors[0].R * weights[0] + colors[1].R * weights[1] + colors[2].R * weights[2] + colors[3].R * weights[3],
colors[0].G * weights[0] + colors[1].G * weights[1] + colors[2].G * weights[2] + colors[3].G * weights[3],
colors[0].B * weights[0] + colors[1].B * weights[1] + colors[2].B * weights[2] + colors[3].B * weights[3],
colors[0].A * weights[0] + colors[1].A * weights[1] + colors[2].A * weights[2] + colors[3].A * weights[3]
};
// Return interpolated data for the new vertex.
return Color.FromArgb((int)rgba[3], (int)rgba[0], (int)rgba[1], (int)rgba[2]);
}

static void Main(string[] args)
{
// Example input data in the form of a star that intersects itself.
var inputData = new float[] { 0.0f, 3.0f, -1.0f, 0.0f, 1.6f, 1.9f, -1.6f, 1.9f, 1.0f, 0.0f };

// Create an instance of the tessellator. Can be reused.
var tess = new LibTessDotNet.Tess();

// Construct the contour from inputData.
// A polygon can be composed of multiple contours which are all tessellated at the same time.
int numPoints = inputData.Length / 2;
var contour = new LibTessDotNet.ContourVertex[numPoints];
for (int i = 0; i < numPoints; i++)
{
// NOTE : Z is here for convenience if you want to keep a 3D vertex position throughout the tessellation process but only X and Y are important.
contour[i].Position = new LibTessDotNet.Vec3 { X = inputData[i * 2], Y = inputData[i * 2 + 1], Z = 0.0f };
// Data can contain any per-vertex data, here a constant color.
contour[i].Data = Color.Azure;
}
// Add the contour with a specific orientation, use "Original" if you want to keep the input orientation.
tess.AddContour(contour, LibTessDotNet.ContourOrientation.Clockwise);

// Tessellate!
// The winding rule determines how the different contours are combined together.
// See http://www.glprogramming.com/red/chapter11.html (section "Winding Numbers and Winding Rules") for more information.
// If you want triangles as output, you need to use "Polygons" type as output and 3 vertices per polygon.
tess.Tessellate(LibTessDotNet.WindingRule.EvenOdd, LibTessDotNet.ElementType.Polygons, 3, VertexCombine);

// Same call but the last callback is optional. Data will be null because no interpolated data would have been generated.
//tess.Tessellate(LibTessDotNet.WindingRule.EvenOdd, LibTessDotNet.ElementType.Polygons, 3); // Some vertices will have null Data in this case.

Console.WriteLine("Output triangles:");
int numTriangles = tess.ElementCount;
for (int i = 0; i < numTriangles; i++)
{
var v0 = tess.Vertices[tess.Elements[i * 3]].Position;
var v1 = tess.Vertices[tess.Elements[i * 3 + 1]].Position;
var v2 = tess.Vertices[tess.Elements[i * 3 + 2]].Position;
Console.WriteLine("#{0} ({1:F1},{2:F1}) ({3:F1},{4:F1}) ({5:F1},{6:F1})", i, v0.X, v0.Y, v1.X, v1.Y, v2.X, v2.Y);
}
Console.ReadLine();
}
}
}
36 changes: 36 additions & 0 deletions TessExample/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TessExample")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TessExample")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("55f70c57-4ac9-4166-86b8-938b24f1e44e")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
59 changes: 59 additions & 0 deletions TessExample/TessExample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{BB1005DC-C911-4E65-B3B8-7316357FD254}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TessExample</RootNamespace>
<AssemblyName>TessExample</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\Build\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\Build\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LibTessDotNet\LibTessDotNet.csproj">
<Project>{5EE39029-873A-45A0-9259-2198BF8729F4}</Project>
<Name>LibTessDotNet</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

0 comments on commit 1d83cb0

Please sign in to comment.