Back to index

wims  3.65+svn20090927
Package Functions | Package Attributes | Static Package Attributes | Private Member Functions
Model3D Class Reference

The representation of a 3D model. More...

Collaboration diagram for Model3D:
Collaboration graph
[legend]

List of all members.

Package Functions

 Model3D ()
 Model3D (InputStream is) throws IOException, FileFormatException
 Create a 3D model by parsing an input stream.
int addVert (float x, float y, float z)
 Add a vertex to this model.
void add (int p1, int p2)
 Add a line from vertex p1 to vertex p2.
void transform ()
 Transform all the points in this model.
void compress ()
 eliminate duplicate lines
void paint (Graphics g)
 Paint this model to a graphics context.
void findBB ()
 Find the bounding box of this model.

Package Attributes

float vert []
int tvert []
int nvert
int maxvert
int con []
int ncon
int maxcon
boolean transformed
Matrix3D mat
float xmin
float xmax
float ymin
float ymax
float zmin
float zmax

Static Package Attributes

static Color gr []

Private Member Functions

void quickSort (int a[], int left, int right)
void swap (int a[], int i, int j)

Detailed Description

The representation of a 3D model.

Definition at line 50 of file ThreeD.java.


Constructor & Destructor Documentation

Model3D.Model3D ( ) [inline, package]

Definition at line 61 of file ThreeD.java.

               {
       mat = new Matrix3D ();
       mat.xrot(20);
       mat.yrot(30);
    }

Here is the call graph for this function:

Model3D.Model3D ( InputStream  is) throws IOException, FileFormatException [inline, package]

Create a 3D model by parsing an input stream.

Definition at line 67 of file ThreeD.java.

                                                                     {
       this();
       StreamTokenizer st = new StreamTokenizer(is);
       st.eolIsSignificant(true);
       st.commentChar('#');
scan:
       while (true) {
           switch (st.nextToken()) {
             default:
              break scan;
             case StreamTokenizer.TT_EOL:
              break;
             case StreamTokenizer.TT_WORD:
              if ("v".equals(st.sval)) {
                  double x = 0, y = 0, z = 0;
                  if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
                     x = st.nval;
                     if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
                         y = st.nval;
                         if (st.nextToken() == StreamTokenizer.TT_NUMBER)
                            z = st.nval;
                     }
                  }
                  addVert((float) x, (float) y, (float) z);
                  while (st.ttype != StreamTokenizer.TT_EOL &&
                         st.ttype != StreamTokenizer.TT_EOF)
                     st.nextToken();
              } else if ("f".equals(st.sval) || "fo".equals(st.sval) || "l".equals(st.sval)) {
                  int start = -1;
                  int prev = -1;
                  int n = -1;
                  while (true)
                     if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
                         n = (int) st.nval;
                         if (prev >= 0)
                            add(prev - 1, n - 1);
                         if (start < 0)
                            start = n;
                         prev = n;
                     } else if (st.ttype == '/')
                         st.nextToken();
                     else
                         break;
                  if (start >= 0)
                     add(start - 1, prev - 1);
                  if (st.ttype != StreamTokenizer.TT_EOL)
                     break scan;
              } else {
                  while (st.nextToken() != StreamTokenizer.TT_EOL
                         && st.ttype != StreamTokenizer.TT_EOF);
              }
           }
       }
       is.close();
       if (st.ttype != StreamTokenizer.TT_EOF)
           throw new FileFormatException(st.toString());
    }

Here is the call graph for this function:


Member Function Documentation

void Model3D.add ( int  p1,
int  p2 
) [inline, package]

Add a line from vertex p1 to vertex p2.

Definition at line 145 of file ThreeD.java.

                             {
       int i = ncon;
       if (p1 >= nvert || p2 >= nvert)
           return;
       if (i >= maxcon)
           if (con == null) {
              maxcon = 100;
              con = new int[maxcon];
           } else {
              maxcon *= 2;
              int nv[] = new int[maxcon];
              System.arraycopy(con, 0, nv, 0, con.length);
              con = nv;
           }
       if (p1 > p2) {
           int t = p1;
           p1 = p2;
           p2 = t;
       }
       con[i] = (p1 << 16) | p2;
       ncon = i + 1;
    }

Here is the caller graph for this function:

int Model3D.addVert ( float  x,
float  y,
float  z 
) [inline, package]

Add a vertex to this model.

Definition at line 126 of file ThreeD.java.

                                           {
       int i = nvert;
       if (i >= maxvert)
           if (vert == null) {
              maxvert = 100;
              vert = new float[maxvert * 3];
           } else {
              maxvert *= 2;
              float nv[] = new float[maxvert * 3];
              System.arraycopy(vert, 0, nv, 0, vert.length);
              vert = nv;
           }
       i *= 3;
       vert[i] = x;
       vert[i + 1] = y;
       vert[i + 2] = z;
       return nvert++;
    }

Here is the caller graph for this function:

void Model3D.compress ( ) [inline, package]

eliminate duplicate lines

Definition at line 242 of file ThreeD.java.

                    {
       int limit = ncon;
       int c[] = con;
       quickSort(con, 0, ncon - 1);
       int d = 0;
       int pp1 = -1;
       for (int i = 0; i < limit; i++) {
           int p1 = c[i];
           if (pp1 != p1) {
              c[d] = p1;
              d++;
           }
           pp1 = p1;
       }
       ncon = d;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void Model3D.findBB ( ) [inline, package]

Find the bounding box of this model.

Definition at line 301 of file ThreeD.java.

                  {
       if (nvert <= 0)
           return;
       float v[] = vert;
       float xmin = v[0], xmax = xmin;
       float ymin = v[1], ymax = ymin;
       float zmin = v[2], zmax = zmin;
       for (int i = nvert * 3; (i -= 3) > 0;) {
           float x = v[i];
           if (x < xmin)
              xmin = x;
           if (x > xmax)
              xmax = x;
           float y = v[i + 1];
           if (y < ymin)
              ymin = y;
           if (y > ymax)
              ymax = y;
           float z = v[i + 2];
           if (z < zmin)
              zmin = z;
           if (z > zmax)
              zmax = z;
       }
       this.xmax = xmax;
       this.xmin = xmin;
       this.ymax = ymax;
       this.ymin = ymin;
       this.zmax = zmax;
       this.zmin = zmin;
    }

Here is the caller graph for this function:

void Model3D.paint ( Graphics  g) [inline, package]

Paint this model to a graphics context.

It uses the matrix associated with this model to map from model space to screen space. The next version of the browser should have double buffering, which will make this much nicer

Definition at line 265 of file ThreeD.java.

                           {
       if (vert == null || nvert <= 0)
           return;
       transform();
       if (gr == null) {
           gr = new Color[16];
           for (int i = 0; i < 16; i++) {
              int grey = (int) (170*(1-Math.pow(i/15.0, 2.3)));
              gr[i] = new Color(grey, grey, grey);
           }
       }
       int lg = 0;
       int lim = ncon;
       int c[] = con;
       int v[] = tvert;
       if (lim <= 0 || nvert <= 0)
           return;
       for (int i = 0; i < lim; i++) {
           int T = c[i];
           int p1 = ((T >> 16) & 0xFFFF) * 3;
           int p2 = (T & 0xFFFF) * 3;
           int grey = v[p1 + 2] + v[p2 + 2];
           if (grey < 0)
              grey = 0;
           if (grey > 15)
              grey = 15;
           if (grey != lg) {
              lg = grey;
              g.setColor(gr[grey]);
           }
           g.drawLine(v[p1], v[p1 + 1],
                     v[p2], v[p2 + 1]);
       }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void Model3D.quickSort ( int  a[],
int  left,
int  right 
) [inline, private]

Definition at line 179 of file ThreeD.java.

   {
      int leftIndex = left;
      int rightIndex = right;
      int partionElement;
      if ( right > left)
      {

         /* Arbitrarily establishing partition element as the midpoint of
          * the array.
          */
         partionElement = a[ ( left + right ) / 2 ];

         // loop through the array until indices cross
         while( leftIndex <= rightIndex )
         {
            /* find the first element that is greater than or equal to 
             * the partionElement starting from the leftIndex.
             */
            while( ( leftIndex < right ) && ( a[leftIndex] < partionElement ) )
               ++leftIndex;

            /* find an element that is smaller than or equal to 
             * the partionElement starting from the rightIndex.
             */
            while( ( rightIndex > left ) && 
                   ( a[rightIndex] > partionElement ) )
               --rightIndex;

            // if the indexes have not crossed, swap
            if( leftIndex <= rightIndex ) 
            {
               swap(a, leftIndex, rightIndex);
               ++leftIndex;
               --rightIndex;
            }
         }

         /* If the right index has not reached the left side of array
          * must now sort the left partition.
          */
         if( left < rightIndex )
            quickSort( a, left, rightIndex );

         /* If the left index has not reached the right side of array
          * must now sort the right partition.
          */
         if( leftIndex < right )
            quickSort( a, leftIndex, right );

      }
   }

Here is the call graph for this function:

Here is the caller graph for this function:

void Model3D.swap ( int  a[],
int  i,
int  j 
) [inline, private]

Definition at line 232 of file ThreeD.java.

   {
      int T;
      T = a[i]; 
      a[i] = a[j];
      a[j] = T;
   }

Here is the caller graph for this function:

void Model3D.transform ( ) [inline, package]

Transform all the points in this model.

Definition at line 168 of file ThreeD.java.

                     {
       if (transformed || nvert <= 0)
           return;
       if (tvert == null || tvert.length < nvert * 3)
           tvert = new int[nvert*3];
       mat.transform(vert, tvert, nvert);
       transformed = true;
    }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

int Model3D.con[] [package]

Definition at line 54 of file ThreeD.java.

Color Model3D.gr[] [static, package]

Definition at line 259 of file ThreeD.java.

Matrix3D Model3D.mat [package]

Definition at line 57 of file ThreeD.java.

int Model3D.maxcon [package]

Definition at line 55 of file ThreeD.java.

int Model3D.maxvert [package]

Definition at line 53 of file ThreeD.java.

int Model3D.ncon [package]

Definition at line 55 of file ThreeD.java.

int Model3D.nvert [package]

Definition at line 53 of file ThreeD.java.

Definition at line 56 of file ThreeD.java.

int Model3D.tvert[] [package]

Definition at line 52 of file ThreeD.java.

float Model3D.vert[] [package]

Definition at line 51 of file ThreeD.java.

float Model3D.xmax [package]

Definition at line 59 of file ThreeD.java.

float Model3D.xmin [package]

Definition at line 59 of file ThreeD.java.

float Model3D.ymax [package]

Definition at line 59 of file ThreeD.java.

float Model3D.ymin [package]

Definition at line 59 of file ThreeD.java.

float Model3D.zmax [package]

Definition at line 59 of file ThreeD.java.

float Model3D.zmin [package]

Definition at line 59 of file ThreeD.java.


The documentation for this class was generated from the following file: