      SUBROUTINE CONVEX(X,Y,Z,VAL,IFVISO)
      DIMENSION X(4),Y(4),Z(4),VAL(4),V(3,3),VN(3)
      DO I=1,3
        V(1,I) = X(I+1)-X(1)
        V(2,I) = Y(I+1)-Y(1)
        V(3,I) = Z(I+1)-Z(1)
      ENDDO
      VN(1) = (V(2,1)*V(3,2)-V(2,2)*V(3,1))
      VN(2) = (V(1,2)*V(3,1)-V(1,1)*V(3,2))
      VN(3) = (V(1,1)*V(2,2)-V(2,1)*V(1,2))
      DET1 = VN(1)*(V(2,2)*V(3,3)-V(2,3)*V(3,2))
     &     + VN(2)*(V(1,3)*V(3,2)-V(1,2)*V(3,3))
     &     + VN(3)*(V(1,2)*V(2,3)-V(2,2)*V(1,3))
      IF (DET1.LT.0.) THEN
        DET2 = VN(1)*(V(2,3)*V(3,1)-V(2,1)*V(3,3))
     &       + VN(2)*(V(1,1)*V(3,3)-V(1,3)*V(3,1))
     &       + VN(3)*(V(1,3)*V(2,1)-V(2,3)*V(1,1))
        IF (DET2.LT.0.) THEN
          CALL ECHR(X(3),X(4))
          CALL ECHR(Y(3),Y(4))
          CALL ECHR(Z(3),Z(4))
          IF (IFVISO.NE.0) CALL ECHR(VAL(3),VAL(4))
        ELSE
          CALL ECHR(X(3),X(2))
          CALL ECHR(Y(3),Y(2))
          CALL ECHR(Z(3),Z(2))
          IF (IFVISO.NE.0) CALL ECHR(VAL(3),VAL(2))
        ENDIF
      ENDIF
      END
