## toxi.geom.mesh Class Terrain

```java.lang.Object
toxi.geom.mesh.Terrain
```

`public class Terrainextends java.lang.Object`

Implementation of a 2D grid based heightfield with basic intersection features and conversion to `TriangleMesh`. The terrain is always located in the XZ plane with the positive Y axis as up vector.

Constructor Summary
```Terrain(int width, int depth, float scale)```
Constructs a new and initially flat terrain of the given size in the XZ plane, centred around the world origin.

Method Summary
` int` `getDepth()`

` float` ```getHeightAtCell(int x, int z)```

` float` ```getHeightAtPoint(float x, float z)```
Computes the elevation of the terrain at the given 2D world coordinate (based on current terrain scale).
` int` `getWidth()`

` IsectData3D` ```intersectAtPoint(float x, float z)```
Computes the 3D position (with elevation) and normal vector at the given 2D location in the terrain.
` Terrain` `setElevation(float[] elevation)`
Sets the elevation of all cells to those of the given array values.
` Terrain` ```setHeightAtCell(int x, int z, float h)```
Sets the elevation for a single given grid cell.
` Mesh3D` `toMesh()`

` Mesh3D` `toMesh(float groundLevel)`

` Mesh3D` `toMesh(Mesh3D mesh)`
Creates a `TriangleMesh` instance of the terrain surface or adds its geometry to an existing mesh.
` Mesh3D` ```toMesh(Mesh3D mesh, float groundLevel)```
Creates a `TriangleMesh` instance of the terrain and constructs side panels and a bottom plane to form a fully enclosed mesh volume, e.g.

Methods inherited from class java.lang.Object
`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### Terrain

```public Terrain(int width,
int depth,
float scale)```
Constructs a new and initially flat terrain of the given size in the XZ plane, centred around the world origin.

Parameters:
`width` -
`depth` -
`scale` -
Method Detail

### getDepth

`public int getDepth()`
Returns:
number of grid cells along the Z axis.

### getHeightAtCell

```public float getHeightAtCell(int x,
int z)```
Parameters:
`x` -
`z` -
Returns:
the elevation at grid point

### getHeightAtPoint

```public float getHeightAtPoint(float x,
float z)```
Computes the elevation of the terrain at the given 2D world coordinate (based on current terrain scale).

Parameters:
`x` - scaled world coord x
`z` - scaled world coord z
Returns:
interpolated elevation

### getWidth

`public int getWidth()`
Returns:
number of grid cells along the X axis.

### intersectAtPoint

```public IsectData3D intersectAtPoint(float x,
float z)```
Computes the 3D position (with elevation) and normal vector at the given 2D location in the terrain. The position is in scaled world coordinates based on the given terrain scale. The returned data is encapsulated in a `IsectData3D` instance.

Parameters:
`x` -
`z` -
Returns:
intersection data parcel

### setElevation

`public Terrain setElevation(float[] elevation)`
Sets the elevation of all cells to those of the given array values.

Parameters:
`elevation` - array of height values
Returns:
itself

### setHeightAtCell

```public Terrain setHeightAtCell(int x,
int z,
float h)```
Sets the elevation for a single given grid cell.

Parameters:
`x` -
`z` -
`h` - new elevation value
Returns:
itself

### toMesh

`public Mesh3D toMesh()`

### toMesh

`public Mesh3D toMesh(float groundLevel)`

### toMesh

`public Mesh3D toMesh(Mesh3D mesh)`
Creates a `TriangleMesh` instance of the terrain surface or adds its geometry to an existing mesh.

Parameters:
`mesh` -
Returns:
mesh instance

### toMesh

```public Mesh3D toMesh(Mesh3D mesh,
float groundLevel)```
Creates a `TriangleMesh` instance of the terrain and constructs side panels and a bottom plane to form a fully enclosed mesh volume, e.g. suitable for CNC fabrication or 3D printing. The bottom plane will be created at the given ground level (can also be negative) and the sides are extended downward to that level too.

Parameters:
`mesh` - existing mesh or null
`groundLevel` -
Returns:
mesh