⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 model3d.java

📁 The view238 application is a simple tool for graphically displaying STEP-NC toolpaths. It is a Java
💻 JAVA
字号:
/* $RCSfile: $
 * $Revision: $ $Date: $
 * Auth:  (jfritz@steptools.com)
 * 
 * 	Copyright (c) 1991-2006 by 
 * 	STEP Tools Inc., Troy, New York
 * 	All Rights Reserved
 * 
 * 	This software is furnished under a license and may be used and
 * 	copied only in accordance with the terms of such license and with
 * 	the inclusion of the above copyright notice.  This software and
 * 	accompanying written materials or any other copies thereof may
 * 	not be provided or otherwise made available to any other person.
 * 	No title to or ownership of the software is hereby transferred.
 * 
 * 		----------------------------------------
 */

/*
 * @(#)ThreeD.java	1.14 04/07/26
 * 
 * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 * 
 * -Redistribution in binary form must reproduce the above copyright notice, 
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL 
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */
/* This code was derived from the Java wireframe demo applet */

package com.steptools.view238;

import java.awt.*;
import javax.swing.ListSelectionModel;

/** The representation of a 3D model */
class Model3D {
    double vert[];
    int tvert[];
    int nvert, maxvert;
    int con[];
    int sel_group[];
    int ncon, maxcon;
    boolean transformed;
    Matrix3D mat;
    ListSelectionModel lsm;

    double xmin, xmax, ymin, ymax, zmin, zmax;

    Model3D (ListSelectionModel l) {
	mat = new Matrix3D ();
	mat.xrot(20);
	mat.yrot(30);
	lsm = l;
    }

    
    /** Create a 3D model by parsing an input stream */
//     Model3D (InputStream is) throws IOException {
//       this();
//       StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(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());
//     }

    /** Add a vertex to this model */
    int addVert(double x, double y, double z) {
	int i = nvert;
	if (i >= maxvert)
	    if (vert == null) {
		maxvert = 100;
		vert = new double[maxvert * 3];
	    } else {
		maxvert *= 2;
		double nv[] = new double[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++;
    }
    
    /** Add a line from vertex p1 to vertex p2 */
    void add(int p1, int p2, int vgrp) {
	int i = ncon;
	if (p1 >= nvert || p2 >= nvert)
	    return;
	if (i >= maxcon)
	    if (con == null) {
		maxcon = 100;
		con = new int[maxcon];
		sel_group = new int[maxcon];
	    } else { 
		maxcon *= 2;
		int nv[] = new int[maxcon];
		System.arraycopy(con, 0, nv, 0, con.length);		
		con = nv;

		int[] grp = new int[maxcon];
		System.arraycopy(sel_group, 0, grp, 0, sel_group.length);
		sel_group = grp;
		
	    }
	if (p1 > p2) {
	    int t = p1;
	    p1 = p2;
	    p2 = t;
	}
	con[i] = (p1 << 16) | p2;
	sel_group[i] = vgrp;
	ncon = i + 1;
	if (i > 1<<16) {
	    throw new RuntimeException ("Too many lines");
	}
    }
    
    /** Transform all the points in this model */
    void transform() {
	if (transformed || nvert <= 0)
	    return;
	if (tvert == null || tvert.length < nvert * 3)
	    tvert = new int[nvert*3];
	mat.transform(vert, tvert, nvert);
	transformed = true;
    }

   /* Quick Sort implementation
    */
   private void quickSort(int a[], int left, int right)
   {
      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 );

      }
   }

   private void swap(int a[], int i, int j)
   {
      int T;
      T = a[i];
      a[i] = a[j];
      a[j] = T;
   }


    /** eliminate duplicate lines */
    void compress() {
	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;
    }

    static Color gr[];

    /** 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 */
    void paint(Graphics g) {
	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 grp = sel_group[i];

	    if (!lsm.isSelectedIndex(grp))
		continue;
	    
	    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]);
	}
    }

    /** Find the bounding box of this model */
    void findBB() {
	if (nvert <= 0)
	    return;
	double v[] = vert;
	double xmin = v[0], xmax = xmin;
	double ymin = v[1], ymax = ymin;
	double zmin = v[2], zmax = zmin;
	for (int i = nvert * 3; (i -= 3) > 0;) {
	    double x = v[i];
	    if (x < xmin)
		xmin = x;
	    if (x > xmax)
		xmax = x;
	    double y = v[i + 1];
	    if (y < ymin)
		ymin = y;
	    if (y > ymax)
		ymax = y;
	    double 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;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -