char SegmentTriangleIntersection::SegTriInt( tPointi T, tPointd q, tPointd r, tPointd p )
{
    int code = '?';
    int m = -1;

    code = SegPlaneInt( T, q, r, p, &m );
    //printf("SegPlaneInt code=%c, m=%d; p=(%lf,%lf,%lf)\n", code,m,p[X],p[Y],p[Z]);

    if      ( code == '0')
        return '0';
    else if ( code == 'q')
        return InTri3D( T, m, q );
    else if ( code == 'r')
        return InTri3D( T, m, r );
    else if ( code == 'p' )
        return InPlane( T, m, q, r, p );
    else if ( code == '1' )
        return SegTriCross( T, q, r );
    else /* Error */
        return code;
}
char    SegTriInt( tPointi T, tPointd q, tPointd r, tPointd p )
{
    int code = '?';
    int m = -1;

    code = SegPlaneInt( T, q, r, p, &m );


    if      ( code == '0')
       return '0';
    else if ( code == 'q')
       return InTri3D( T, m, q );
    else if ( code == 'r')
       return InTri3D( T, m, r );
    else if ( code == 'p' )
       return InPlane( T, m, q, r, p );
    else if ( code == '1' )
       return SegTriCross( T, q, r );
    else /* Error */
       return code;
}
// return whether the segment intersects a tri edge
// not complete! the c1 == '0' and c2 == '0' case is not handled!
char	InPlane( tPointi T, int m, tPointd q, tPointd r, tPointd p)
{
  tPointd tmp;
  int c1,c2;
  c1 = InTri3D(T,m,q);
  c2 = InTri3D(T,m,r);

  // yes
  if(c1 == 'E' && c2 == 'E') return 'f';
  if(c1 == 'E' && c2 == 'F') return 'f';
  if(c1 == 'F' && c2 == 'E') return 'f';
  if(c1 == 'F' && c2 == 'F') return 'f';
  if(c1 == '0' && c2 == 'F') return 'f';
  if(c1 == 'F' && c2 == '0') return 'f';

  // no
  if(c1 == 'V' && c2 == 'V') return '0';

  // maybe
  if(c1 == '0' && (c2 == 'V' || c2 == 'E')) {
    tmp[0] = r[0] + 0.001*(p[0]-r[0]);
    tmp[1] = r[1] + 0.001*(p[1]-r[1]);
    tmp[2] = r[2] + 0.001*(p[2]-r[2]);

    if(InTri3D(T,m,tmp) == 'F') return 'f';
    else return '0';
  }

  if((c1 == 'V' || c1 == 'E') && c2 == '0') {
    tmp[0] = p[0] + 0.001*(r[0]-p[0]);
    tmp[1] = p[1] + 0.001*(r[1]-p[1]);
    tmp[2] = p[2] + 0.001*(r[2]-p[2]);

    if(InTri3D(T,m,tmp) == 'F') return 'f';
    else return '0';
  }

  return '0';
}