Skip to content

Commit 1d83cb0

Browse files
committed
Add simple example of how to use the library.
1 parent 2c3840e commit 1d83cb0

File tree

4 files changed

+168
-18
lines changed

4 files changed

+168
-18
lines changed

LibTessDotNet.sln

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,26 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Misc", "Misc", "{1E6E1C79-6
1111
README.md = README.md
1212
EndProjectSection
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TessExample", "TessExample\TessExample.csproj", "{BB1005DC-C911-4E65-B3B8-7316357FD254}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
17-
Debug|Mixed Platforms = Debug|Mixed Platforms
18-
Debug|x86 = Debug|x86
1919
Release|Any CPU = Release|Any CPU
20-
Release|Mixed Platforms = Release|Mixed Platforms
21-
Release|x86 = Release|x86
2220
EndGlobalSection
2321
GlobalSection(ProjectConfigurationPlatforms) = postSolution
2422
{5EE39029-873A-45A0-9259-2198BF8729F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2523
{5EE39029-873A-45A0-9259-2198BF8729F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
26-
{5EE39029-873A-45A0-9259-2198BF8729F4}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
27-
{5EE39029-873A-45A0-9259-2198BF8729F4}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
28-
{5EE39029-873A-45A0-9259-2198BF8729F4}.Debug|x86.ActiveCfg = Debug|Any CPU
2924
{5EE39029-873A-45A0-9259-2198BF8729F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
3025
{5EE39029-873A-45A0-9259-2198BF8729F4}.Release|Any CPU.Build.0 = Release|Any CPU
31-
{5EE39029-873A-45A0-9259-2198BF8729F4}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
32-
{5EE39029-873A-45A0-9259-2198BF8729F4}.Release|Mixed Platforms.Build.0 = Release|Any CPU
33-
{5EE39029-873A-45A0-9259-2198BF8729F4}.Release|x86.ActiveCfg = Release|Any CPU
3426
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Debug|Any CPU.ActiveCfg = Debug|x86
3527
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Debug|Any CPU.Build.0 = Debug|x86
36-
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
37-
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Debug|Mixed Platforms.Build.0 = Debug|x86
38-
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Debug|x86.ActiveCfg = Debug|x86
39-
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Debug|x86.Build.0 = Debug|x86
4028
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Release|Any CPU.ActiveCfg = Release|x86
4129
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Release|Any CPU.Build.0 = Release|x86
42-
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Release|Mixed Platforms.ActiveCfg = Release|x86
43-
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Release|Mixed Platforms.Build.0 = Release|x86
44-
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Release|x86.ActiveCfg = Release|x86
45-
{AA3C13F4-2628-43D5-BAAE-751AE013C865}.Release|x86.Build.0 = Release|x86
30+
{BB1005DC-C911-4E65-B3B8-7316357FD254}.Debug|Any CPU.ActiveCfg = Debug|x86
31+
{BB1005DC-C911-4E65-B3B8-7316357FD254}.Debug|Any CPU.Build.0 = Debug|x86
32+
{BB1005DC-C911-4E65-B3B8-7316357FD254}.Release|Any CPU.ActiveCfg = Release|x86
33+
{BB1005DC-C911-4E65-B3B8-7316357FD254}.Release|Any CPU.Build.0 = Release|x86
4634
EndGlobalSection
4735
GlobalSection(SolutionProperties) = preSolution
4836
HideSolutionNode = FALSE

TessExample/Program.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Drawing;
3+
4+
namespace TessExample
5+
{
6+
class Program
7+
{
8+
// The data array contains 4 values, it's the associated data of the vertices that resulted in an intersection.
9+
private static object VertexCombine(LibTessDotNet.Vec3 position, object[] data, float[] weights)
10+
{
11+
// Fetch the vertex data.
12+
var colors = new Color[] { (Color)data[0], (Color)data[1], (Color)data[2], (Color)data[3] };
13+
// Interpolate with the 4 weights.
14+
var rgba = new float[] {
15+
colors[0].R * weights[0] + colors[1].R * weights[1] + colors[2].R * weights[2] + colors[3].R * weights[3],
16+
colors[0].G * weights[0] + colors[1].G * weights[1] + colors[2].G * weights[2] + colors[3].G * weights[3],
17+
colors[0].B * weights[0] + colors[1].B * weights[1] + colors[2].B * weights[2] + colors[3].B * weights[3],
18+
colors[0].A * weights[0] + colors[1].A * weights[1] + colors[2].A * weights[2] + colors[3].A * weights[3]
19+
};
20+
// Return interpolated data for the new vertex.
21+
return Color.FromArgb((int)rgba[3], (int)rgba[0], (int)rgba[1], (int)rgba[2]);
22+
}
23+
24+
static void Main(string[] args)
25+
{
26+
// Example input data in the form of a star that intersects itself.
27+
var inputData = new float[] { 0.0f, 3.0f, -1.0f, 0.0f, 1.6f, 1.9f, -1.6f, 1.9f, 1.0f, 0.0f };
28+
29+
// Create an instance of the tessellator. Can be reused.
30+
var tess = new LibTessDotNet.Tess();
31+
32+
// Construct the contour from inputData.
33+
// A polygon can be composed of multiple contours which are all tessellated at the same time.
34+
int numPoints = inputData.Length / 2;
35+
var contour = new LibTessDotNet.ContourVertex[numPoints];
36+
for (int i = 0; i < numPoints; i++)
37+
{
38+
// 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.
39+
contour[i].Position = new LibTessDotNet.Vec3 { X = inputData[i * 2], Y = inputData[i * 2 + 1], Z = 0.0f };
40+
// Data can contain any per-vertex data, here a constant color.
41+
contour[i].Data = Color.Azure;
42+
}
43+
// Add the contour with a specific orientation, use "Original" if you want to keep the input orientation.
44+
tess.AddContour(contour, LibTessDotNet.ContourOrientation.Clockwise);
45+
46+
// Tessellate!
47+
// The winding rule determines how the different contours are combined together.
48+
// See http://www.glprogramming.com/red/chapter11.html (section "Winding Numbers and Winding Rules") for more information.
49+
// If you want triangles as output, you need to use "Polygons" type as output and 3 vertices per polygon.
50+
tess.Tessellate(LibTessDotNet.WindingRule.EvenOdd, LibTessDotNet.ElementType.Polygons, 3, VertexCombine);
51+
52+
// Same call but the last callback is optional. Data will be null because no interpolated data would have been generated.
53+
//tess.Tessellate(LibTessDotNet.WindingRule.EvenOdd, LibTessDotNet.ElementType.Polygons, 3); // Some vertices will have null Data in this case.
54+
55+
Console.WriteLine("Output triangles:");
56+
int numTriangles = tess.ElementCount;
57+
for (int i = 0; i < numTriangles; i++)
58+
{
59+
var v0 = tess.Vertices[tess.Elements[i * 3]].Position;
60+
var v1 = tess.Vertices[tess.Elements[i * 3 + 1]].Position;
61+
var v2 = tess.Vertices[tess.Elements[i * 3 + 2]].Position;
62+
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);
63+
}
64+
Console.ReadLine();
65+
}
66+
}
67+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("TessExample")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("TessExample")]
13+
[assembly: AssemblyCopyright("Copyright © 2014")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("55f70c57-4ac9-4166-86b8-938b24f1e44e")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

TessExample/TessExample.csproj

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{BB1005DC-C911-4E65-B3B8-7316357FD254}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>TessExample</RootNamespace>
12+
<AssemblyName>TessExample</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>..\Build\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>..\Build\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Drawing" />
40+
</ItemGroup>
41+
<ItemGroup>
42+
<Compile Include="Program.cs" />
43+
<Compile Include="Properties\AssemblyInfo.cs" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<ProjectReference Include="..\LibTessDotNet\LibTessDotNet.csproj">
47+
<Project>{5EE39029-873A-45A0-9259-2198BF8729F4}</Project>
48+
<Name>LibTessDotNet</Name>
49+
</ProjectReference>
50+
</ItemGroup>
51+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
52+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
53+
Other similar extension points exist, see Microsoft.Common.targets.
54+
<Target Name="BeforeBuild">
55+
</Target>
56+
<Target Name="AfterBuild">
57+
</Target>
58+
-->
59+
</Project>

0 commit comments

Comments
 (0)