/*
 * $B%3%s%T%e!<%?%0%i%U%#%C%/%9FCO@(BI
 *    3 Marching Cube$BJ}$K$h$k#3<!85?^7A$N%o%$%d!<%U%l!<%`I=<((B
 *  Programmed by Yoshizumi TANAKA. 2000/2
 *
 * *Text Box*
 *   border $B!D(B $B%a%?%\!<%k$N6-3&$r7hDj$7$^$9!#(B
 *
 * *Mouse*
 *   $BG$0U$NE@$r%I%i%C%0$9$k$3$H$K$h$j2sE>$7$^$9!#(B
 *
 */

import java.applet.Applet;
import java.awt.*;
import java.util.Vector;
//import java.io.*;

/* **************************************************************: */
public class MarchingCube extends java.applet.Applet{
    final int bit0 = 1;
    final int bit1 = 2;
    final int bit2 = 4;
    final int bit3 = 8;
    final int bit4 = 16;
    final int bit5 = 32;
    final int bit6 = 64;
    final int bit7 = 128;
    final int NI = 64;
    final int NJ = 64;
    final int NK = 64;
    double scale;
    double Border = 0.1;
    Panel p1 = new Panel();
    Label l1;
    TextField t1;
    double cube[][][] = new double[NI][NJ][NK];
    int xedge[];
    int yedge[];
    int topyedge[];
    int zedge[];
    int old6pt, old10pt;
    int vert_index = 0;
    Vector vertex_table = new Vector();
    Vector facet_table = new Vector();

    int width, height;
    int x_origin,y_origin;             //center of screen
    double theta=70., phi=20., R=800.; // $B;kE@0LCV(B
    int idrag=0;
    Point prev = new Point(0, 0);
    Trans henkan = new Trans();
    
    int edge_data[][] = {
        {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {1, 3, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, },
        {1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 8, 10, -1, -1, -1, -1, -1, -1, },
        {0, 2, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1, },
        {2, 3, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1, },
        {2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 2, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 9, 11, -1, -1, -1, -1, -1, -1, },
        {1, 2, 8, 9, 11, -1, -1, -1, -1, -1, -1, -1, },
        {1, 3, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1, },
        {8, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, },
        {4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 4, 7, 8, 9, -1, -1, -1, -1, -1, -1, },
        {1, 3, 4, 7, 9, -1, -1, -1, -1, -1, -1, -1, },
        {1, 2, 4, 7, 8, 10, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 7, 10, -1, -1, -1, -1, -1, },
        {0, 2, 4, 7, 8, 9, 10, -1, -1, -1, -1, -1, },
        {2, 3, 4, 7, 9, 10, -1, -1, -1, -1, -1, -1, },
        {2, 3, 4, 7, 8, 11, -1, -1, -1, -1, -1, -1, },
        {0, 2, 4, 7, 11, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 7, 8, 9, 11, -1, -1, -1, },
        {1, 2, 4, 7, 9, 11, -1, -1, -1, -1, -1, -1, },
        {1, 3, 4, 7, 8, 10, 11, -1, -1, -1, -1, -1, },
        {0, 1, 4, 7, 10, 11, -1, -1, -1, -1, -1, -1, },
        {0, 3, 4, 7, 8, 9, 10, 11, -1, -1, -1, -1, },
        {4, 7, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1, },
        {4, 5, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 4, 5, 8, 9, -1, -1, -1, -1, -1, -1, },
        {0, 1, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, },
        {1, 3, 4, 5, 8, -1, -1, -1, -1, -1, -1, -1, },
        {1, 2, 4, 5, 9, 10, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 5, 8, 9, 10, -1, -1, -1, },
        {0, 2, 4, 5, 10, -1, -1, -1, -1, -1, -1, -1, },
        {2, 3, 4, 5, 8, 10, -1, -1, -1, -1, -1, -1, },
        {2, 3, 4, 5, 9, 11, -1, -1, -1, -1, -1, -1, },
        {0, 2, 4, 5, 8, 9, 11, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 5, 11, -1, -1, -1, -1, -1, },
        {1, 2, 4, 5, 8, 11, -1, -1, -1, -1, -1, -1, },
        {1, 3, 4, 5, 9, 10, 11, -1, -1, -1, -1, -1, },
        {0, 1, 4, 5, 8, 9, 10, 11, -1, -1, -1, -1, },
        {0, 3, 4, 5, 10, 11, -1, -1, -1, -1, -1, -1, },
        {4, 5, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1, },
        {7, 5, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 7, 5, 9, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 7, 5, 8, -1, -1, -1, -1, -1, -1, -1, },
        {1, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, },
        {1, 2, 7, 5, 8, 9, 10, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 7, 5, 9, 10, -1, -1, -1, -1, },
        {0, 2, 7, 5, 8, 10, -1, -1, -1, -1, -1, -1, },
        {2, 3, 7, 5, 10, -1, -1, -1, -1, -1, -1, -1, },
        {2, 3, 7, 5, 8, 9, 11, -1, -1, -1, -1, -1, },
        {0, 2, 7, 5, 9, 11, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 7, 5, 8, 11, -1, -1, -1, -1, },
        {1, 2, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, },
        {1, 3, 7, 5, 8, 9, 10, 11, -1, -1, -1, -1, },
        {0, 1, 7, 5, 9, 10, 11, -1, -1, -1, -1, -1, },
        {0, 3, 7, 5, 8, 10, 11, -1, -1, -1, -1, -1, },
        {7, 5, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, },
        {5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 5, 6, 8, 10, -1, -1, -1, -1, -1, -1, },
        {0, 1, 5, 6, 9, 10, -1, -1, -1, -1, -1, -1, },
        {1, 3, 5, 6, 8, 9, 10, -1, -1, -1, -1, -1, },
        {1, 2, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 5, 6, 8, -1, -1, -1, -1, -1, },
        {0, 2, 5, 6, 9, -1, -1, -1, -1, -1, -1, -1, },
        {2, 3, 5, 6, 8, 9, -1, -1, -1, -1, -1, -1, },
        {2, 3, 5, 6, 10, 11, -1, -1, -1, -1, -1, -1, },
        {0, 2, 5, 6, 8, 10, 11, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 5, 6, 9, 10, 11, -1, -1, -1, },
        {1, 2, 5, 6, 8, 9, 10, 11, -1, -1, -1, -1, },
        {1, 3, 5, 6, 11, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 5, 6, 8, 11, -1, -1, -1, -1, -1, -1, },
        {0, 3, 5, 6, 9, 11, -1, -1, -1, -1, -1, -1, },
        {5, 6, 8, 9, 11, -1, -1, -1, -1, -1, -1, -1, },
        {4, 7, 5, 6, 8, 10, -1, -1, -1, -1, -1, -1, },
        {0, 3, 4, 7, 5, 6, 10, -1, -1, -1, -1, -1, },
        {0, 1, 4, 7, 5, 6, 8, 9, 10, -1, -1, -1, },
        {1, 3, 4, 7, 5, 6, 9, 10, -1, -1, -1, -1, },
        {1, 2, 4, 7, 5, 6, 8, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 7, 5, 6, -1, -1, -1, -1, },
        {0, 2, 4, 7, 5, 6, 8, 9, -1, -1, -1, -1, },
        {2, 3, 4, 7, 5, 6, 9, -1, -1, -1, -1, -1, },
        {2, 3, 4, 7, 5, 6, 8, 10, 11, -1, -1, -1, },
        {0, 2, 4, 7, 5, 6, 10, 11, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 7, 5, 6, 8, 9, 10, 11, },
        {1, 2, 4, 7, 5, 6, 9, 10, 11, -1, -1, -1, },
        {1, 3, 4, 7, 5, 6, 8, 11, -1, -1, -1, -1, },
        {0, 1, 4, 7, 5, 6, 11, -1, -1, -1, -1, -1, },
        {0, 3, 4, 7, 5, 6, 8, 9, 11, -1, -1, -1, },
        {4, 7, 5, 6, 9, 11, -1, -1, -1, -1, -1, -1, },
        {4, 6, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 4, 6, 8, 9, 10, -1, -1, -1, -1, -1, },
        {0, 1, 4, 6, 10, -1, -1, -1, -1, -1, -1, -1, },
        {1, 3, 4, 6, 8, 10, -1, -1, -1, -1, -1, -1, },
        {1, 2, 4, 6, 9, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 6, 8, 9, -1, -1, -1, -1, },
        {0, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1, -1, },
        {2, 3, 4, 6, 8, -1, -1, -1, -1, -1, -1, -1, },
        {2, 3, 4, 6, 9, 10, 11, -1, -1, -1, -1, -1, },
        {0, 2, 4, 6, 8, 9, 10, 11, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 6, 10, 11, -1, -1, -1, -1, },
        {1, 2, 4, 6, 8, 10, 11, -1, -1, -1, -1, -1, },
        {1, 3, 4, 6, 9, 11, -1, -1, -1, -1, -1, -1, },
        {0, 1, 4, 6, 8, 9, 11, -1, -1, -1, -1, -1, },
        {0, 3, 4, 6, 11, -1, -1, -1, -1, -1, -1, -1, },
        {4, 6, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, },
        {7, 6, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 7, 6, 9, 10, -1, -1, -1, -1, -1, -1, },
        {0, 1, 7, 6, 8, 10, -1, -1, -1, -1, -1, -1, },
        {1, 3, 7, 6, 10, -1, -1, -1, -1, -1, -1, -1, },
        {1, 2, 7, 6, 8, 9, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 7, 6, 9, -1, -1, -1, -1, -1, },
        {0, 2, 7, 6, 8, -1, -1, -1, -1, -1, -1, -1, },
        {2, 3, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, },
        {2, 3, 7, 6, 8, 9, 10, 11, -1, -1, -1, -1, },
        {0, 2, 7, 6, 9, 10, 11, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 7, 6, 8, 10, 11, -1, -1, -1, },
        {1, 2, 7, 6, 10, 11, -1, -1, -1, -1, -1, -1, },
        {1, 3, 7, 6, 8, 9, 11, -1, -1, -1, -1, -1, },
        {0, 1, 7, 6, 9, 11, -1, -1, -1, -1, -1, -1, },
        {0, 3, 7, 6, 8, 11, -1, -1, -1, -1, -1, -1, },
        {7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 7, 6, 8, 11, -1, -1, -1, -1, -1, -1, },
        {0, 1, 7, 6, 9, 11, -1, -1, -1, -1, -1, -1, },
        {1, 3, 7, 6, 8, 9, 11, -1, -1, -1, -1, -1, },
        {1, 2, 7, 6, 10, 11, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 7, 6, 8, 10, 11, -1, -1, -1, },
        {0, 2, 7, 6, 9, 10, 11, -1, -1, -1, -1, -1, },
        {2, 3, 7, 6, 8, 9, 10, 11, -1, -1, -1, -1, },
        {2, 3, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 2, 7, 6, 8, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 7, 6, 9, -1, -1, -1, -1, -1, },
        {1, 2, 7, 6, 8, 9, -1, -1, -1, -1, -1, -1, },
        {1, 3, 7, 6, 10, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 7, 6, 8, 10, -1, -1, -1, -1, -1, -1, },
        {0, 3, 7, 6, 9, 10, -1, -1, -1, -1, -1, -1, },
        {7, 6, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1, },
        {4, 6, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 4, 6, 11, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 4, 6, 8, 9, 11, -1, -1, -1, -1, -1, },
        {1, 3, 4, 6, 9, 11, -1, -1, -1, -1, -1, -1, },
        {1, 2, 4, 6, 8, 10, 11, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 6, 10, 11, -1, -1, -1, -1, },
        {0, 2, 4, 6, 8, 9, 10, 11, -1, -1, -1, -1, },
        {2, 3, 4, 6, 9, 10, 11, -1, -1, -1, -1, -1, },
        {2, 3, 4, 6, 8, -1, -1, -1, -1, -1, -1, -1, },
        {0, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 6, 8, 9, -1, -1, -1, -1, },
        {1, 2, 4, 6, 9, -1, -1, -1, -1, -1, -1, -1, },
        {1, 3, 4, 6, 8, 10, -1, -1, -1, -1, -1, -1, },
        {0, 1, 4, 6, 10, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 4, 6, 8, 9, 10, -1, -1, -1, -1, -1, },
        {4, 6, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1, },
        {4, 7, 5, 6, 9, 11, -1, -1, -1, -1, -1, -1, },
        {0, 3, 4, 7, 5, 6, 8, 9, 11, -1, -1, -1, },
        {0, 1, 4, 7, 5, 6, 11, -1, -1, -1, -1, -1, },
        {1, 3, 4, 7, 5, 6, 8, 11, -1, -1, -1, -1, },
        {1, 2, 4, 7, 5, 6, 9, 10, 11, -1, -1, -1, },
        {0, 1, 2, 3, 4, 7, 5, 6, 8, 9, 10, 11, },
        {0, 2, 4, 7, 5, 6, 10, 11, -1, -1, -1, -1, },
        {2, 3, 4, 7, 5, 6, 8, 10, 11, -1, -1, -1, },
        {2, 3, 4, 7, 5, 6, 9, -1, -1, -1, -1, -1, },
        {0, 2, 4, 7, 5, 6, 8, 9, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 7, 5, 6, -1, -1, -1, -1, },
        {1, 2, 4, 7, 5, 6, 8, -1, -1, -1, -1, -1, },
        {1, 3, 4, 7, 5, 6, 9, 10, -1, -1, -1, -1, },
        {0, 1, 4, 7, 5, 6, 8, 9, 10, -1, -1, -1, },
        {0, 3, 4, 7, 5, 6, 10, -1, -1, -1, -1, -1, },
        {4, 7, 5, 6, 8, 10, -1, -1, -1, -1, -1, -1, },
        {5, 6, 8, 9, 11, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 5, 6, 9, 11, -1, -1, -1, -1, -1, -1, },
        {0, 1, 5, 6, 8, 11, -1, -1, -1, -1, -1, -1, },
        {1, 3, 5, 6, 11, -1, -1, -1, -1, -1, -1, -1, },
        {1, 2, 5, 6, 8, 9, 10, 11, -1, -1, -1, -1, },
        {0, 1, 2, 3, 5, 6, 9, 10, 11, -1, -1, -1, },
        {0, 2, 5, 6, 8, 10, 11, -1, -1, -1, -1, -1, },
        {2, 3, 5, 6, 10, 11, -1, -1, -1, -1, -1, -1, },
        {2, 3, 5, 6, 8, 9, -1, -1, -1, -1, -1, -1, },
        {0, 2, 5, 6, 9, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 5, 6, 8, -1, -1, -1, -1, -1, },
        {1, 2, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, },
        {1, 3, 5, 6, 8, 9, 10, -1, -1, -1, -1, -1, },
        {0, 1, 5, 6, 9, 10, -1, -1, -1, -1, -1, -1, },
        {0, 3, 5, 6, 8, 10, -1, -1, -1, -1, -1, -1, },
        {5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {7, 5, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 7, 5, 8, 10, 11, -1, -1, -1, -1, -1, },
        {0, 1, 7, 5, 9, 10, 11, -1, -1, -1, -1, -1, },
        {1, 3, 7, 5, 8, 9, 10, 11, -1, -1, -1, -1, },
        {1, 2, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 7, 5, 8, 11, -1, -1, -1, -1, },
        {0, 2, 7, 5, 9, 11, -1, -1, -1, -1, -1, -1, },
        {2, 3, 7, 5, 8, 9, 11, -1, -1, -1, -1, -1, },
        {2, 3, 7, 5, 10, -1, -1, -1, -1, -1, -1, -1, },
        {0, 2, 7, 5, 8, 10, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 7, 5, 9, 10, -1, -1, -1, -1, },
        {1, 2, 7, 5, 8, 9, 10, -1, -1, -1, -1, -1, },
        {1, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 7, 5, 8, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 7, 5, 9, -1, -1, -1, -1, -1, -1, -1, },
        {7, 5, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, },
        {4, 5, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 4, 5, 10, 11, -1, -1, -1, -1, -1, -1, },
        {0, 1, 4, 5, 8, 9, 10, 11, -1, -1, -1, -1, },
        {1, 3, 4, 5, 9, 10, 11, -1, -1, -1, -1, -1, },
        {1, 2, 4, 5, 8, 11, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 5, 11, -1, -1, -1, -1, -1, },
        {0, 2, 4, 5, 8, 9, 11, -1, -1, -1, -1, -1, },
        {2, 3, 4, 5, 9, 11, -1, -1, -1, -1, -1, -1, },
        {2, 3, 4, 5, 8, 10, -1, -1, -1, -1, -1, -1, },
        {0, 2, 4, 5, 10, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 5, 8, 9, 10, -1, -1, -1, },
        {1, 2, 4, 5, 9, 10, -1, -1, -1, -1, -1, -1, },
        {1, 3, 4, 5, 8, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 4, 5, 8, 9, -1, -1, -1, -1, -1, -1, },
        {4, 5, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {4, 7, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 4, 7, 8, 9, 10, 11, -1, -1, -1, -1, },
        {0, 1, 4, 7, 10, 11, -1, -1, -1, -1, -1, -1, },
        {1, 3, 4, 7, 8, 10, 11, -1, -1, -1, -1, -1, },
        {1, 2, 4, 7, 9, 11, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 7, 8, 9, 11, -1, -1, -1, },
        {0, 2, 4, 7, 11, -1, -1, -1, -1, -1, -1, -1, },
        {2, 3, 4, 7, 8, 11, -1, -1, -1, -1, -1, -1, },
        {2, 3, 4, 7, 9, 10, -1, -1, -1, -1, -1, -1, },
        {0, 2, 4, 7, 8, 9, 10, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 4, 7, 10, -1, -1, -1, -1, -1, },
        {1, 2, 4, 7, 8, 10, -1, -1, -1, -1, -1, -1, },
        {1, 3, 4, 7, 9, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 4, 7, 8, 9, -1, -1, -1, -1, -1, -1, },
        {0, 3, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, },
        {4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {8, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1, },
        {1, 3, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, },
        {1, 2, 8, 9, 11, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 9, 11, -1, -1, -1, -1, -1, -1, },
        {0, 2, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, },
        {2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {2, 3, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1, },
        {0, 2, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 2, 3, 8, 10, -1, -1, -1, -1, -1, -1, },
        {1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {1, 3, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
        {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
    };
    int facet_data[][] = {
      {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {3, 0, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {1, 9, 8, 1, 8, 3, -1, -1, -1, -1, -1, -1, },
      {1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {1, 2, 10, 3, 0, 8, -1, -1, -1, -1, -1, -1, },
      {2, 10, 9, 2, 9, 0, -1, -1, -1, -1, -1, -1, },
      {2, 10, 9, 2, 9, 8, 2, 8, 3, -1, -1, -1, },
      {2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {11, 2, 0, 11, 0, 8, -1, -1, -1, -1, -1, -1, },
      {2, 3, 11, 0, 1, 9, -1, -1, -1, -1, -1, -1, },
      {11, 2, 1, 11, 1, 9, 11, 9, 8, -1, -1, -1, },
      {3, 11, 10, 3, 10, 1, -1, -1, -1, -1, -1, -1, },
      {11, 10, 1, 11, 1, 0, 11, 0, 8, -1, -1, -1, },
      {3, 11, 10, 3, 10, 9, 3, 9, 0, -1, -1, -1, },
      {11, 10, 9, 11, 9, 8, -1, -1, -1, -1, -1, -1, },
      {4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {4, 7, 3, 4, 3, 0, -1, -1, -1, -1, -1, -1, },
      {4, 7, 8, 0, 1, 9, -1, -1, -1, -1, -1, -1, },
      {4, 7, 3, 4, 3, 1, 4, 1, 9, -1, -1, -1, },
      {4, 7, 8, 1, 2, 10, -1, -1, -1, -1, -1, -1, },
      {4, 7, 3, 4, 3, 0, 1, 2, 10, -1, -1, -1, },
      {4, 7, 8, 2, 10, 9, 2, 9, 0, -1, -1, -1, },
      {4, 7, 3, 4, 3, 2, 4, 2, 10, 4, 10, 9, },
      {4, 7, 8, 2, 3, 11, -1, -1, -1, -1, -1, -1, },
      {4, 7, 11, 4, 11, 2, 4, 2, 0, -1, -1, -1, },
      {4, 7, 8, 0, 1, 9, 2, 3, 11, -1, -1, -1, },
      {4, 7, 11, 4, 11, 2, 4, 2, 1, 4, 1, 9, },
      {4, 7, 8, 3, 11, 10, 3, 10, 1, -1, -1, -1, },
      {4, 7, 11, 4, 11, 10, 4, 10, 1, 4, 1, 0, },
      {4, 7, 8, 3, 11, 10, 3, 10, 9, 3, 9, 0, },
      {11, 7, 4, 11, 4, 9, 11, 9, 10, -1, -1, -1, },
      {5, 4, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {5, 4, 9, 3, 0, 8, -1, -1, -1, -1, -1, -1, },
      {5, 4, 0, 5, 0, 1, -1, -1, -1, -1, -1, -1, },
      {5, 4, 8, 5, 8, 3, 5, 3, 1, -1, -1, -1, },
      {5, 4, 9, 1, 2, 10, -1, -1, -1, -1, -1, -1, },
      {5, 4, 9, 3, 0, 8, 1, 2, 10, -1, -1, -1, },
      {5, 4, 0, 5, 0, 2, 5, 2, 10, -1, -1, -1, },
      {5, 4, 8, 5, 8, 3, 5, 3, 2, 5, 2, 10, },
      {5, 4, 9, 2, 3, 11, -1, -1, -1, -1, -1, -1, },
      {5, 4, 9, 11, 2, 0, 11, 0, 8, -1, -1, -1, },
      {5, 4, 0, 5, 0, 1, 2, 3, 11, -1, -1, -1, },
      {5, 4, 8, 5, 8, 11, 5, 11, 2, 5, 2, 1, },
      {5, 4, 9, 3, 11, 10, 3, 10, 1, -1, -1, -1, },
      {5, 4, 9, 11, 10, 1, 11, 1, 0, 11, 0, 8, },
      {5, 4, 0, 5, 0, 3, 5, 3, 11, 5, 11, 10, },
      {5, 10, 11, 5, 11, 8, 5, 8, 4, -1, -1, -1, },
      {9, 5, 7, 9, 7, 8, -1, -1, -1, -1, -1, -1, },
      {9, 5, 7, 9, 7, 3, 9, 3, 0, -1, -1, -1, },
      {5, 7, 8, 5, 8, 0, 5, 0, 1, -1, -1, -1, },
      {5, 7, 3, 5, 3, 1, -1, -1, -1, -1, -1, -1, },
      {9, 5, 7, 9, 7, 8, 1, 2, 10, -1, -1, -1, },
      {9, 5, 7, 9, 7, 3, 9, 3, 0, 1, 2, 10, },
      {5, 7, 8, 5, 8, 0, 5, 0, 2, 5, 2, 10, },
      {7, 5, 10, 7, 10, 2, 7, 2, 3, -1, -1, -1, },
      {9, 5, 7, 9, 7, 8, 2, 3, 11, -1, -1, -1, },
      {9, 5, 7, 9, 7, 11, 9, 11, 2, 9, 2, 0, },
      {5, 7, 8, 5, 8, 0, 5, 0, 1, 2, 3, 11, },
      {11, 7, 5, 11, 5, 1, 11, 1, 2, -1, -1, -1, },
      {9, 5, 7, 9, 7, 8, 3, 11, 10, 3, 10, 1, },
      {11, 7, 5, 11, 5, 10, 0, 1, 9, -1, -1, -1, },
      {11, 7, 5, 11, 5, 10, 3, 0, 8, -1, -1, -1, },
      {11, 7, 5, 11, 5, 10, -1, -1, -1, -1, -1, -1, },
      {6, 5, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {6, 5, 10, 3, 0, 8, -1, -1, -1, -1, -1, -1, },
      {6, 5, 10, 0, 1, 9, -1, -1, -1, -1, -1, -1, },
      {6, 5, 10, 1, 9, 8, 1, 8, 3, -1, -1, -1, },
      {6, 5, 1, 6, 1, 2, -1, -1, -1, -1, -1, -1, },
      {6, 5, 1, 6, 1, 2, 3, 0, 8, -1, -1, -1, },
      {6, 5, 9, 6, 9, 0, 6, 0, 2, -1, -1, -1, },
      {6, 5, 9, 6, 9, 8, 6, 8, 3, 6, 3, 2, },
      {6, 5, 10, 2, 3, 11, -1, -1, -1, -1, -1, -1, },
      {6, 5, 10, 11, 2, 0, 11, 0, 8, -1, -1, -1, },
      {6, 5, 10, 0, 1, 9, 2, 3, 11, -1, -1, -1, },
      {6, 5, 10, 11, 2, 1, 11, 1, 9, 11, 9, 8, },
      {6, 5, 1, 6, 1, 3, 6, 3, 11, -1, -1, -1, },
      {6, 5, 1, 6, 1, 0, 6, 0, 8, 6, 8, 11, },
      {6, 5, 9, 6, 9, 0, 6, 0, 3, 6, 3, 11, },
      {6, 11, 8, 6, 8, 9, 6, 9, 5, -1, -1, -1, },
      {6, 5, 10, 4, 7, 8, -1, -1, -1, -1, -1, -1, },
      {6, 5, 10, 4, 7, 3, 4, 3, 0, -1, -1, -1, },
      {6, 5, 10, 0, 1, 9, 4, 7, 8, -1, -1, -1, },
      {6, 5, 10, 4, 7, 3, 4, 3, 1, 4, 1, 9, },
      {6, 5, 1, 6, 1, 2, 4, 7, 8, -1, -1, -1, },
      {6, 5, 1, 6, 1, 2, 4, 7, 3, 4, 3, 0, },
      {6, 5, 9, 6, 9, 0, 6, 0, 2, 4, 7, 8, },
      {7, 6, 2, 7, 2, 3, 5, 4, 9, -1, -1, -1, },
      {6, 5, 10, 2, 3, 11, 4, 7, 8, -1, -1, -1, },
      {6, 5, 10, 4, 7, 11, 4, 11, 2, 4, 2, 0, },
      {6, 5, 10, 2, 3, 11, 0, 1, 9, 4, 7, 8, },
      {7, 6, 11, 1, 2, 10, 5, 4, 9, -1, -1, -1, },
      {6, 5, 1, 6, 1, 3, 6, 3, 11, 4, 7, 8, },
      {7, 6, 11, 5, 4, 0, 5, 0, 1, -1, -1, -1, },
      {7, 6, 11, 3, 0, 8, 5, 4, 9, -1, -1, -1, },
      {7, 6, 11, 5, 4, 9, -1, -1, -1, -1, -1, -1, },
      {10, 6, 4, 10, 4, 9, -1, -1, -1, -1, -1, -1, },
      {10, 6, 4, 10, 4, 9, 3, 0, 8, -1, -1, -1, },
      {10, 6, 4, 10, 4, 0, 10, 0, 1, -1, -1, -1, },
      {10, 6, 4, 10, 4, 8, 10, 8, 3, 10, 3, 1, },
      {6, 4, 9, 6, 9, 1, 6, 1, 2, -1, -1, -1, },
      {6, 4, 9, 6, 9, 1, 6, 1, 2, 3, 0, 8, },
      {6, 4, 0, 6, 0, 2, -1, -1, -1, -1, -1, -1, },
      {8, 4, 6, 8, 6, 2, 8, 2, 3, -1, -1, -1, },
      {10, 6, 4, 10, 4, 9, 2, 3, 11, -1, -1, -1, },
      {10, 6, 4, 10, 4, 9, 11, 2, 0, 11, 0, 8, },
      {10, 6, 4, 10, 4, 0, 10, 0, 1, 2, 3, 11, },
      {6, 11, 8, 6, 8, 4, 1, 2, 10, -1, -1, -1, },
      {6, 4, 9, 6, 9, 1, 6, 1, 3, 6, 3, 11, },
      {6, 11, 8, 6, 8, 4, 0, 1, 9, -1, -1, -1, },
      {6, 11, 3, 6, 3, 0, 6, 0, 4, -1, -1, -1, },
      {6, 11, 8, 6, 8, 4, -1, -1, -1, -1, -1, -1, },
      {10, 6, 7, 10, 7, 8, 10, 8, 9, -1, -1, -1, },
      {10, 6, 7, 10, 7, 3, 10, 3, 0, 10, 0, 9, },
      {10, 6, 7, 10, 7, 8, 10, 8, 0, 10, 0, 1, },
      {7, 6, 10, 7, 10, 1, 7, 1, 3, -1, -1, -1, },
      {6, 7, 8, 6, 8, 9, 6, 9, 1, 6, 1, 2, },
      {7, 6, 2, 7, 2, 3, 0, 1, 9, -1, -1, -1, },
      {7, 6, 2, 7, 2, 0, 7, 0, 8, -1, -1, -1, },
      {7, 6, 2, 7, 2, 3, -1, -1, -1, -1, -1, -1, },
      {10, 6, 7, 10, 7, 8, 10, 8, 9, 2, 3, 11, },
      {7, 6, 11, 2, 10, 9, 2, 9, 0, -1, -1, -1, },
      {7, 6, 11, 3, 0, 8, 1, 2, 10, -1, -1, -1, },
      {7, 6, 11, 1, 2, 10, -1, -1, -1, -1, -1, -1, },
      {7, 6, 11, 1, 9, 8, 1, 8, 3, -1, -1, -1, },
      {7, 6, 11, 0, 1, 9, -1, -1, -1, -1, -1, -1, },
      {7, 6, 11, 3, 0, 8, -1, -1, -1, -1, -1, -1, },
      {7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {7, 6, 11, 3, 0, 8, -1, -1, -1, -1, -1, -1, },
      {7, 6, 11, 0, 1, 9, -1, -1, -1, -1, -1, -1, },
      {7, 6, 11, 1, 9, 8, 1, 8, 3, -1, -1, -1, },
      {7, 6, 11, 1, 2, 10, -1, -1, -1, -1, -1, -1, },
      {7, 6, 11, 3, 0, 8, 1, 2, 10, -1, -1, -1, },
      {7, 6, 11, 2, 10, 9, 2, 9, 0, -1, -1, -1, },
      {7, 6, 11, 2, 10, 9, 2, 9, 8, 2, 8, 3, },
      {7, 6, 2, 7, 2, 3, -1, -1, -1, -1, -1, -1, },
      {7, 6, 2, 7, 2, 0, 7, 0, 8, -1, -1, -1, },
      {7, 6, 2, 7, 2, 3, 0, 1, 9, -1, -1, -1, },
      {7, 6, 2, 7, 2, 1, 7, 1, 9, 7, 9, 8, },
      {7, 6, 10, 7, 10, 1, 7, 1, 3, -1, -1, -1, },
      {7, 6, 10, 7, 10, 1, 7, 1, 0, 7, 0, 8, },
      {7, 6, 10, 7, 10, 9, 7, 9, 0, 7, 0, 3, },
      {10, 6, 7, 10, 7, 8, 10, 8, 9, -1, -1, -1, },
      {6, 11, 8, 6, 8, 4, -1, -1, -1, -1, -1, -1, },
      {6, 11, 3, 6, 3, 0, 6, 0, 4, -1, -1, -1, },
      {6, 11, 8, 6, 8, 4, 0, 1, 9, -1, -1, -1, },
      {6, 11, 3, 6, 3, 1, 6, 1, 9, 6, 9, 4, },
      {6, 11, 8, 6, 8, 4, 1, 2, 10, -1, -1, -1, },
      {6, 11, 3, 6, 3, 0, 6, 0, 4, 1, 2, 10, },
      {6, 11, 8, 6, 8, 4, 2, 10, 9, 2, 9, 0, },
      {10, 6, 4, 10, 4, 9, 2, 3, 11, -1, -1, -1, },
      {8, 4, 6, 8, 6, 2, 8, 2, 3, -1, -1, -1, },
      {6, 2, 0, 6, 0, 4, -1, -1, -1, -1, -1, -1, },
      {8, 4, 6, 8, 6, 2, 8, 2, 3, 0, 1, 9, },
      {6, 4, 9, 6, 9, 1, 6, 1, 2, -1, -1, -1, },
      {8, 4, 6, 8, 6, 10, 8, 10, 1, 8, 1, 3, },
      {10, 6, 4, 10, 4, 0, 10, 0, 1, -1, -1, -1, },
      {10, 6, 4, 10, 4, 9, 3, 0, 8, -1, -1, -1, },
      {10, 6, 4, 10, 4, 9, -1, -1, -1, -1, -1, -1, },
      {7, 6, 11, 5, 4, 9, -1, -1, -1, -1, -1, -1, },
      {7, 6, 11, 3, 0, 8, 5, 4, 9, -1, -1, -1, },
      {7, 6, 11, 5, 4, 0, 5, 0, 1, -1, -1, -1, },
      {7, 6, 11, 5, 4, 8, 5, 8, 3, 5, 3, 1, },
      {7, 6, 11, 1, 2, 10, 5, 4, 9, -1, -1, -1, },
      {7, 6, 11, 1, 2, 10, 3, 0, 8, 5, 4, 9, },
      {7, 6, 11, 5, 4, 0, 5, 0, 2, 5, 2, 10, },
      {6, 5, 10, 2, 3, 11, 4, 7, 8, -1, -1, -1, },
      {7, 6, 2, 7, 2, 3, 5, 4, 9, -1, -1, -1, },
      {7, 6, 2, 7, 2, 0, 7, 0, 8, 5, 4, 9, },
      {7, 6, 2, 7, 2, 3, 5, 4, 0, 5, 0, 1, },
      {6, 5, 1, 6, 1, 2, 4, 7, 8, -1, -1, -1, },
      {7, 6, 10, 7, 10, 1, 7, 1, 3, 5, 4, 9, },
      {6, 5, 10, 0, 1, 9, 4, 7, 8, -1, -1, -1, },
      {6, 5, 10, 4, 7, 3, 4, 3, 0, -1, -1, -1, },
      {6, 5, 10, 4, 7, 8, -1, -1, -1, -1, -1, -1, },
      {6, 11, 8, 6, 8, 9, 6, 9, 5, -1, -1, -1, },
      {6, 11, 3, 6, 3, 0, 6, 0, 9, 6, 9, 5, },
      {6, 11, 8, 6, 8, 0, 6, 0, 1, 6, 1, 5, },
      {6, 5, 1, 6, 1, 3, 6, 3, 11, -1, -1, -1, },
      {6, 11, 8, 6, 8, 9, 6, 9, 5, 1, 2, 10, },
      {6, 5, 10, 0, 1, 9, 2, 3, 11, -1, -1, -1, },
      {6, 5, 10, 11, 2, 0, 11, 0, 8, -1, -1, -1, },
      {6, 5, 10, 2, 3, 11, -1, -1, -1, -1, -1, -1, },
      {8, 9, 5, 8, 5, 6, 8, 6, 2, 8, 2, 3, },
      {6, 5, 9, 6, 9, 0, 6, 0, 2, -1, -1, -1, },
      {6, 5, 1, 6, 1, 2, 3, 0, 8, -1, -1, -1, },
      {6, 5, 1, 6, 1, 2, -1, -1, -1, -1, -1, -1, },
      {6, 5, 10, 1, 9, 8, 1, 8, 3, -1, -1, -1, },
      {6, 5, 10, 0, 1, 9, -1, -1, -1, -1, -1, -1, },
      {6, 5, 10, 3, 0, 8, -1, -1, -1, -1, -1, -1, },
      {6, 5, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {11, 7, 5, 11, 5, 10, -1, -1, -1, -1, -1, -1, },
      {11, 7, 5, 11, 5, 10, 3, 0, 8, -1, -1, -1, },
      {11, 7, 5, 11, 5, 10, 0, 1, 9, -1, -1, -1, },
      {11, 7, 5, 11, 5, 10, 1, 9, 8, 1, 8, 3, },
      {11, 7, 5, 11, 5, 1, 11, 1, 2, -1, -1, -1, },
      {11, 7, 5, 11, 5, 1, 11, 1, 2, 3, 0, 8, },
      {11, 7, 5, 11, 5, 9, 11, 9, 0, 11, 0, 2, },
      {9, 5, 7, 9, 7, 8, 2, 3, 11, -1, -1, -1, },
      {7, 5, 10, 7, 10, 2, 7, 2, 3, -1, -1, -1, },
      {7, 5, 10, 7, 10, 2, 7, 2, 0, 7, 0, 8, },
      {7, 5, 10, 7, 10, 2, 7, 2, 3, 0, 1, 9, },
      {9, 5, 7, 9, 7, 8, 1, 2, 10, -1, -1, -1, },
      {7, 5, 1, 7, 1, 3, -1, -1, -1, -1, -1, -1, },
      {5, 7, 8, 5, 8, 0, 5, 0, 1, -1, -1, -1, },
      {9, 5, 7, 9, 7, 3, 9, 3, 0, -1, -1, -1, },
      {9, 5, 7, 9, 7, 8, -1, -1, -1, -1, -1, -1, },
      {5, 10, 11, 5, 11, 8, 5, 8, 4, -1, -1, -1, },
      {5, 10, 11, 5, 11, 3, 5, 3, 0, 5, 0, 4, },
      {5, 10, 11, 5, 11, 8, 5, 8, 4, 0, 1, 9, },
      {5, 4, 9, 3, 11, 10, 3, 10, 1, -1, -1, -1, },
      {5, 1, 2, 5, 2, 11, 5, 11, 8, 5, 8, 4, },
      {5, 4, 0, 5, 0, 1, 2, 3, 11, -1, -1, -1, },
      {5, 4, 9, 11, 2, 0, 11, 0, 8, -1, -1, -1, },
      {5, 4, 9, 2, 3, 11, -1, -1, -1, -1, -1, -1, },
      {5, 10, 2, 5, 2, 3, 5, 3, 8, 5, 8, 4, },
      {5, 4, 0, 5, 0, 2, 5, 2, 10, -1, -1, -1, },
      {5, 4, 9, 3, 0, 8, 1, 2, 10, -1, -1, -1, },
      {5, 4, 9, 1, 2, 10, -1, -1, -1, -1, -1, -1, },
      {5, 4, 8, 5, 8, 3, 5, 3, 1, -1, -1, -1, },
      {5, 4, 0, 5, 0, 1, -1, -1, -1, -1, -1, -1, },
      {5, 4, 9, 3, 0, 8, -1, -1, -1, -1, -1, -1, },
      {5, 4, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {11, 7, 4, 11, 4, 9, 11, 9, 10, -1, -1, -1, },
      {11, 7, 4, 11, 4, 9, 11, 9, 10, 3, 0, 8, },
      {11, 7, 4, 11, 4, 0, 11, 0, 1, 11, 1, 10, },
      {4, 7, 8, 3, 11, 10, 3, 10, 1, -1, -1, -1, },
      {11, 7, 4, 11, 4, 9, 11, 9, 1, 11, 1, 2, },
      {4, 7, 8, 0, 1, 9, 2, 3, 11, -1, -1, -1, },
      {4, 7, 11, 4, 11, 2, 4, 2, 0, -1, -1, -1, },
      {4, 7, 8, 2, 3, 11, -1, -1, -1, -1, -1, -1, },
      {7, 4, 9, 7, 9, 10, 7, 10, 2, 7, 2, 3, },
      {4, 7, 8, 2, 10, 9, 2, 9, 0, -1, -1, -1, },
      {4, 7, 3, 4, 3, 0, 1, 2, 10, -1, -1, -1, },
      {4, 7, 8, 1, 2, 10, -1, -1, -1, -1, -1, -1, },
      {4, 7, 3, 4, 3, 1, 4, 1, 9, -1, -1, -1, },
      {4, 7, 8, 0, 1, 9, -1, -1, -1, -1, -1, -1, },
      {4, 7, 3, 4, 3, 0, -1, -1, -1, -1, -1, -1, },
      {4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {11, 8, 9, 11, 9, 10, -1, -1, -1, -1, -1, -1, },
      {3, 11, 10, 3, 10, 9, 3, 9, 0, -1, -1, -1, },
      {11, 10, 1, 11, 1, 0, 11, 0, 8, -1, -1, -1, },
      {3, 11, 10, 3, 10, 1, -1, -1, -1, -1, -1, -1, },
      {11, 2, 1, 11, 1, 9, 11, 9, 8, -1, -1, -1, },
      {2, 3, 11, 0, 1, 9, -1, -1, -1, -1, -1, -1, },
      {11, 2, 0, 11, 0, 8, -1, -1, -1, -1, -1, -1, },
      {2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {2, 10, 9, 2, 9, 8, 2, 8, 3, -1, -1, -1, },
      {2, 10, 9, 2, 9, 0, -1, -1, -1, -1, -1, -1, },
      {1, 2, 10, 3, 0, 8, -1, -1, -1, -1, -1, -1, },
      {1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {1, 9, 8, 1, 8, 3, -1, -1, -1, -1, -1, -1, },
      {0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {3, 0, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
      {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, },
    };
    
    /* **************************************************************: */
    public void init() {
	setBackground(Color.black);
	Dimension appsize = size();

	width = getSize().width;
	height = getSize().height;

        p1.setBackground(new Color(192, 192, 192));
        p1.setForeground(Color.black);
        p1.add(t1 = new TextField("0.1", 3));
        p1.add(l1 = new Label("= border"));
        setLayout(new BorderLayout());
        add("North", p1);

	x_origin = width/2;
	y_origin = height/2;

        henkan.setVRpnt(0.,0.,0.);      // set view refrence point
        henkan.setview(R,theta,phi);    // set viewpoint
	
	setScale(6.0);
	initCube();
    }
    // ------------------------------------------------------------------
    void setScale(double s) {
	scale = s;
    }
    void initCube() {
	for (int i = 0; i < NI; i++)
	    for (int j = 0; j < NJ; j++)
		for (int k = 0; k < NK; k++)
		    cube[i][j][k] = makeDensity(scale*(i-NI/2.), scale*(j-NJ/2.), scale*(k-NK/2.));
    }
    /* *************** $BIA2h4X?t(B *****************************: */
    public void paint(Graphics g) {
	Border = Double.valueOf(t1.getText()).doubleValue();
	g.setColor(Color.white);
	henkan.preper();         // $B:BI8JQ49$N78?t%;%C%H(B
	march();
	drawFacet(g);
    }
    /* ************** Function d = F(x,y,z) *******************: */
    public double makeDensity(double x, double y, double z) {
	double r = Math.sqrt(x*x+y*y+z*z);
	double R = (NI/2.0)*scale*0.9;
	double r_R = r / R;
	double d;
	if (r > R) {
	    d = 0;
	} else {
	    d = (-4*Math.pow(r_R,6)+17*Math.pow(r_R,4)-22*Math.pow(r_R,2)+9)/9;
	}
	return d;
    }
    /* **************************************************************: */
    public boolean mouseDown(Event evt, int x, int y) {
	prev = new Point(x, y);
	repaint();
	return true;
    }
    // ------------------------------------------------------------------
    public boolean mouseDrag(Event ev, int x, int y) {

	int ia,ib;
	ia = x-prev.x; ib=y-prev.y;
	if(ia>8 || ia<-8 || ib>8 || ib<-8) { 
	    idrag=1;
	    theta += ia/2;  phi -= ib/2;
            henkan.setview(R,theta,phi); 
	}  else idrag=0;
	repaint();
	prev.move(x, y);
	return true;
    }
    // ------------------------------------------------------------------
    public boolean mouseUp(Event ev, int x, int y) {
	prev = null;
        idrag=0;
	return true;
    }
    /* ******* Marching Cube main *****************************************: */
    void march() {
	int cube_index;
	xedge = new int[NI*NJ];
	yedge = new int[NI*NJ];
	topyedge = new int[NJ];
	zedge = new int[NJ];

	for (int k = 0; k < NK-1; k++) {
	    if (k == 0) loadxedgeandyedge(0);
	    for (int i = 0; i < NI-1; i++) {
		if (i == 0)  loadzedgeandtopyedge(k);
		for (int j = 0; j < NJ-1; j++) {
		    if (j == 0) loadold10ptandold6pt(k, i);
		    cube_index = 0;
		    if (cube[i][j][k]       > Border) cube_index |= bit0;
		    if (cube[i][j][k+1]     > Border) cube_index |= bit4;
		    if (cube[i][j+1][k]     > Border) cube_index |= bit3;
		    if (cube[i][j+1][k+1]   > Border) cube_index |= bit7;
		    if (cube[i+1][j][k]     > Border) cube_index |= bit1;
		    if (cube[i+1][j][k+1]   > Border) cube_index |= bit5;
		    if (cube[i+1][j+1][k]   > Border) cube_index |= bit2;
		    if (cube[i+1][j+1][k+1] > Border) cube_index |= bit6;
		    if ((cube_index != 0) && (cube_index != 255)) {
			load_facet(cube_index, i, j, k);
		    }
		}
	    }
	}
    }
    void loadxedgeandyedge(int start_contour) {
	for (int offset = 0; offset < NI*NJ; offset++) {
	    xedge[offset] = 0;
	    yedge[offset] = 0;
	}
    }
    void loadzedgeandtopyedge(int k) {
	for (int j = 0; j < NJ; j++) {
	    zedge[j] = 0;
	    yedge[j] = 0;
	}
    }
    void loadold10ptandold6pt(int k, int i) {
	old6pt = 0;
	old10pt = 0;
    }
    void load_facet(int cube_index, int imarch, int jmarch, int kmarch) {
	int edge_table[] = new int[12];
	int vertex_list[];
	int vert_offset = 0, offset, edge, i;
	Point3 pt;

	i = 0;
	while(edge_data[cube_index][i] != -1) {
	    edge = edge_data[cube_index][i];
	    switch (edge) {
	    case 0:
		offset = NJ*imarch+jmarch;
		vert_offset = xedge[offset];
		break;
	    case 1:
		offset = NJ*(imarch+1)+jmarch;
		vert_offset = yedge[offset];
		break;
	    case 2:
		offset = NJ*imarch+jmarch;
		vert_offset = xedge[offset];
		break;
	    case 3:
		offset = NJ*imarch+jmarch;
		vert_offset = yedge[offset];
		break;
	    case 4:
		vert_offset = old6pt;
		offset = NJ*imarch+jmarch;
		xedge[offset] = vert_offset;
		break;
	    case 5:
		pt = contour_intersect(5, imarch, jmarch, kmarch);
		add_vertex(pt);
		topyedge[jmarch] = vert_index;
		vert_offset = vert_index;
		if (imarch == (NI-2)) {
		    offset = NJ*(NI-1)+jmarch;
		    yedge[offset] = vert_index;
		}
		vert_index++;
		break;
	    case 6:
		pt = contour_intersect(6, imarch, jmarch, kmarch);
		add_vertex(pt);
		old6pt = vert_index;
		vert_offset = vert_index;
		if (jmarch == (NJ-2)) {
		    offset = NJ*(NI-1)+NJ-1;
		    xedge[offset] = vert_index;
		}
		vert_index++;
		break;
	    case 7:
		vert_offset = topyedge[jmarch];
		offset = NJ*imarch+jmarch;
		yedge[offset] = vert_offset;
		break;
	    case 8:
		vert_offset = zedge[jmarch];
		break;
	    case 9:
		vert_offset =old10pt;
		zedge[jmarch] = vert_offset;
		break;
	    case 10:
		pt = contour_intersect(10, imarch, jmarch, kmarch);
		add_vertex(pt);
		old10pt = vert_index;
		vert_offset = vert_index;
		if (jmarch == (NJ-2)) {
		    zedge[NJ-1] = vert_offset;
		}
		vert_index++;
		break;
	    case 11:
		vert_offset = zedge[jmarch+1];
		break;
	    }
	    edge_table[edge] = vert_offset;
	    i++;
	}
	i = 0;
	while((i != 12) && (facet_data[cube_index][i] != -1)) {
	    vertex_list = new int[3];
	    for (int j=0; j<3; j++) {
		vertex_list[j] = edge_table[facet_data[cube_index][i+j]];
	    }
	    add_facet(vertex_list);
	    i += 3;
	}
    }
    Point3 contour_intersect(int edge, int imarch, int jmarch, int kmarch) {
	Point3 q = new Point3();
	switch (edge) {
	case 5:
	    q.x = scale*(imarch - NI/2 + 1);
	    q.y = scale*(jmarch - NJ/2 + 0.5);
	    q.z = scale*(kmarch - NK/2 + 1);
	    break;
	case 6:
	    q.x = scale*(imarch - NI/2 + 0.5);
	    q.y = scale*(jmarch - NJ/2 + 1);
	    q.z = scale*(kmarch - NK/2 + 1);
	    break;
	case 10:
	    q.x = scale*(imarch - NI/2 + 1);
	    q.y = scale*(jmarch - NJ/2 + 1);
	    q.z = scale*(kmarch - NK/2 + 0.5);
	    break;
	}
	return q;
    }
    void add_vertex(Point3 pt) {
	vertex_table.addElement(pt);
    }
    void add_facet(int vertex_list[]) {
	facet_table.addElement(vertex_list);
    }
    void drawFacet(Graphics g) {
	int vsize = vertex_table.size();
	Point projected_table[] = new Point[vsize];
	for (int i = 0; i < vsize; i++) {
	    Point q = henkan.project((Point3)vertex_table.elementAt(i));
	    projected_table[i] = q;
	}
	int fsize = facet_table.size();
	for (int i = 0; i < fsize; i++) {
	    int vlist[] = (int [])facet_table.elementAt(i);
	    Point p1 = projected_table[vlist[0]];
	    Point p2 = projected_table[vlist[1]];
	    Point p3 = projected_table[vlist[2]];
	    int p1x = x_origin + p1.x;
	    int p1y = y_origin - p1.y;
	    int p2x = x_origin + p2.x;
	    int p2y = y_origin - p2.y;
	    int p3x = x_origin + p3.x;
	    int p3y = y_origin - p3.y;
	    //	    if (p1.x > 0) g.setColor(Color.red);
	    //	    else g.setColor(Color.blue);
	    g.drawLine(p1x, p1y, p2x, p2y);
	    g.drawLine(p2x, p2y, p3x, p3y);
	    g.drawLine(p3x, p3y, p1x, p1y);
	}
    }
}

// --  Class for perspective transformation ---------
class Trans {
    double  cos_theta=1., cos_phi=1., sin_theta=0., sin_phi=0.;
    Point3 ptViewRef = new Point3(0, 0, 0);   // view reference point
    double theta, phi, R;

    Trans() {
    }
    public void setVRpnt( double x, double y, double z) {
	ptViewRef = new Point3(x,y,z);
    }
    public void setview(double r, double the, double ph) {
	theta=the; phi=ph; R=r;
    }

    //     perspective transformation  
    public void preper() {
	double d2r= Math.PI/180;

	cos_theta = Math.cos(theta*d2r);
	sin_theta = Math.sin(theta*d2r);
	cos_phi = Math.cos(phi*d2r);
	sin_phi = Math.sin(phi*d2r);
    }
    //  Transforms world coordinate-system to viewpoint coordinate-system.
    public Point3 tranfrm(Point3 point) {
	Point3 p1 = new Point3();
	Point3 p2 = new Point3();

	p1.x = point.x - ptViewRef.x;
	p1.y = point.y - ptViewRef.y;
	p1.z = point.z - ptViewRef.z;

    	double xy = cos_theta*p1.x+sin_theta*p1.y; 	
	p2.z =(R-cos_phi*xy-sin_phi*p1.z)/R;
	p2.x = cos_theta*p1.y-sin_theta*p1.x;	
	p2.y = cos_phi * p1.z - sin_phi * xy;

	return p2;
    }
    public Point project(Point3 point) {
	Point3 p1= tranfrm(point);
	return new Point((int)(p1.x/p1.z), (int)(p1.y/p1.z)); 
    }
}

//    3D real point class 
class Point3 {
    public double x, y, z;

    Point3() {
    }

    Point3(double ox, double oy, double oz) {
	x = ox; y = oy; z = oz;
    }
}
