13,860,478 members
alternative version

#### Stats

17.1K views
14 bookmarked
Posted 20 Dec 2013
Licenced CPOL

# Triangle List Generator for Sphere

, 20 Dec 2013
This article demonstrates a simple and fast method for generating a list of regular triangles that form a unit sphere surface
..

## Introduction

WPF lacks built-in 3D shapes such as sphere, cylinder and cube. In result, those who intend to add such shapes to their 3D scene have no choice rather than generating their mesh through additional coding. As I've already googled and searched through CodeProject for a simple solution for generating a sphere in WPF, I didn't find a clear code that I could rely on for a serious project. Some of them too long others with limitations and ambiguity.  The code that I submit here generates a triangle list and their normal vectors for a sphere with unit radius which can be then moved and resized by simple calculations. It’s written in VB.Net and can be easily converted to other .Net based languages.

## Using the code

### Triangle Structure

For better manipulation of `Trianlges `and their `Normals`, the following structure is developed.

```Public Structure Triangle
Dim Point1 As Point3D
Dim Point2 As Point3D
Dim Point3 As Point3D
Dim Normal As Vector3D
End Structure
```

Algorithm

The points in space should be located in such a way that they all have equal distance from center of sphere and also the enumerator of such points must be able to keep a record of neighboring points. This can be accomplished by converting the Cartesian coordinate to Spherical.

The following shows how the parameters r, θ and ɸ replace the Cartesian X, Y and Z.

As far as r is supposed to be unit it can be omitted. Figure below shows how the enumerator passes through points on sphere surface. It’s shown that ɸ enumerator can be used to find adjacent points on certain Z surface while θ enumerator iterates through all Z surfaces to generate the sphere surface points.

A closer look onto the sphere surface reveals how the triangles can be generated using the algorithm. As it’s shown each θ iteration represents a point which may be common in 8 triangles. In order to prevent generating additional triangles, the algorithm locates two triangles on ɸ iteration forward and θ level backward as shown below.

Now it's time to write the code.

### Generating Sphere Points

The CreateTriangleListForSphere function returns an `ArrayList` that contains `Triangles`. In this function, At first, the following code generates the points on surface of sphere and adds them to `PointList()()` Array. The `Density `parameters determines the number of ɸ for sphere.

```
For tita As Integer = 0 To Density
Dim vtita As Double = tita * (Math.PI / Density)
For nphi As Integer = -Density To Density
Dim vphi As Double = nphi * (Math.PI / Density)
PointList(tita)(nphi + Density).X = Math.Sin(vtita) * Math.Cos(vphi)
PointList(tita)(nphi + Density).Y = Math.Sin(vtita) * Math.Sin(vphi)
PointList(tita)(nphi + Density).Z = Math.Cos(vtita)
Next
Next```

### Generating Triangles

Then, iteration below connects the point lists according to described algorithm. `Triangle1 `and `Triangle2 `are respective triangles explained and illustrated already. As far as sphere is located in origin the `Normal `vectors can be suggested easily as the location of points on sphere surface.

```Dim TriangleList As New ArrayList

For n_tita As Integer = 1 To PointList.GetLength(0) - 1
For n_phi As Integer = 0 To PointList(n_tita).GetLength(0) - 2

Dim Triangle1, Triangle2 As Triangle

Triangle1.Point1 = PointList(n_tita)(n_phi)
Triangle1.Point2 = PointList(n_tita)(n_phi + 1)
Triangle1.Point3 = PointList(n_tita - 1)(n_phi)
Triangle1.Normal = New Vector3D(Triangle1.Point1.X, Triangle1.Point1.Y, Triangle1.Point1.Z)

Triangle2.Point1 = PointList(n_tita)(n_phi + 1)
Triangle2.Point2 = PointList(n_tita - 1)(n_phi + 1)
Triangle2.Point3 = PointList(n_tita - 1)(n_phi)
Triangle2.Normal = New Vector3D(Triangle1.Point1.X, Triangle1.Point1.Y, Triangle1.Point1.Z)

Next
Next
```

## Performance

1) In order to enhance the application performance, the triangle list can be saved on hard drive and then
loaded in memory instead of calling the CreateTrianlgeListForSphere function each time a sphere is needed.<o:p />

2) For bigger projects it’d better to use triangle strips rather than an `ArrayList `of `T<code>riangles`. That will reduce use of memory, CPU and GPU significantly.

3) Two loops above can be easily merged for best performance.

<o:p />

## Application

Adding a custom sphere requires calling CreateTriangleListForSphere and then resize and move the `Triangle `points: `Point1`, `Point2` and `Point3`. The `Normal `vector remains intact.

## Share

 Engineer United Kingdom
Amir Emamjomeh is a senior petroleum reservoir engineer and also programmer who has been involved in several industrial and research projects proposed by the petroleum industry of Iran since 2006. His programming experiences include C, C++, C# , VB.Net, MATLAB and Visual FORTRAN.

## You may also be interested in...

 First Prev Next
 Thanks Member 1124602820-Nov-14 5:00 Member 11246028 20-Nov-14 5:00
 Re: Thanks Amir Emamjomeh12-Jan-15 0:55 Amir Emamjomeh 12-Jan-15 0:55
 Interesting Meshack Musundi4-Jan-14 5:31 Meshack Musundi 4-Jan-14 5:31
 Hexagon bling23-Dec-13 8:35 bling 23-Dec-13 8:35
 Re: Hexagon The_Inventor23-Dec-13 17:05 The_Inventor 23-Dec-13 17:05
 Re: Hexagon bling24-Dec-13 6:09 bling 24-Dec-13 6:09
 Re: Hexagon The_Inventor24-Dec-13 14:53 The_Inventor 24-Dec-13 14:53
 Last Visit: 15-Feb-19 9:35     Last Update: 15-Feb-19 9:35 Refresh 1