<s>[INST] A robot is designed to extract resource from a contaminated soil when the contaminant is present in a specific 3-D shape, which is represented by a 3-D mesh model. The robot can only collect the contaminated soil within a given limited height range to avoid damaging the surrounding clean soil.

To identify the contaminated soil within this height range, the robot needs to select the 3-D triangles that intersect with a horizontal plane defined by a minimum and a maximum Z-value.

Here is a code that does this:
```
ContaminatedSoilData* GetContaminatedSoil(
  const MeshModel& mesh,
  float minZ,
  float maxZ,
  ContaminatedSoilData& contaminatedSoil)
{
  for (int i = 0; i < mesh.faceCount; ++i)
  {
    const Triangle3D& triangle = mesh.faces[i];

    // Check if the triangle's Z-values are within the given range.
    if (minZ <= triangle.v1.z && triangle.v1.z <= maxZ ||
        minZ <= triangle.v2.z && triangle.v2.z <= maxZ ||
        minZ <= triangle.v3.z && triangle.v3.z <= maxZ)
    {
      contaminatedSoil.triangles.push_back(triangle);
    }
    else
    {
      // Check if the triangle intersects with the plane.
      if (IsTriangleIntersectingPlane(triangle, minZ, maxZ))
      {
        contaminatedSoil.triangles.push_back(triangle);
      }
    }
  }

  return &contaminatedSoil;
}
```
Here is the `IsTriangleIntersectingPlane` function:
```
bool IsTriangleIntersectingPlane(
  const Triangle3D& triangle,
  float zMin,
  float zMax)
{
  // Get the edges of the triangle.
  const Vector3D& edge1 = triangle.v2 - triangle.v1;
  const Vector3D& edge2 = triangle.v3 - triangle.v1;

  // Get the plane's normal vector.
  const Vector3D& planeNormal = Vector3D(0.0f, 0.0f, 1.0f);

  // Calculate the denominator of the intersection test.
  float denominator = Vector3D::DotProduct(planeNormal, edge1.cross(edge2));

  // Check if the triangle is parallel to the plane.
  if (fabsf(denominator) < 1e-6f)
  {
    return false;
  }

  // Calculate the distance of each vertex to the plane.
  float distance0 = Vector3D::DotProduct(planeNormal, triangle.v1) - zMin;
  float distance1 = Vector3D::DotProduct(planeNormal, triangle.v2) - zMin;
  float distance2 = Vector3D::DotProduct(planeNormal, triangle.v3) - zMin;

  // Determine if the vertices lie on opposite sides of the plane.
  if ((distance0 > 0 && distance1 > 0 && distance2 > 0) ||
      (distance0 < 0 && distance1 < 0 && distance2 < 0))
  {
    return false;
  }

  return true;
}
```
The `MeshModel` and `Triangle3D` classes are defined elsewhere and are assumed to be correctly implemented.

The provided code does not seem to correctly distinguish between the cases where the triangle intersects the plane and where it does not. 

Can you suggest improvements to the `IsTriangleIntersectingPlane` function, or provide an alternative implementation?[/INST]