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

📄 pickresult.java

📁 JAVA3D矩陈的相关类
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	    generateIntersections();	}	for (int i=0;i<intersections.size();i++) {	    if ((null != (curPi = getIntersection(i))) && 		(null != (curPt = curPi.getPointCoordinatesVW()))) {		curDist = pt.distance (curPt);		if (curDist < minDist) {		    pi = curPi;		    minDist = curDist;		}	    }	}	return pi;    }    /**       Returns String representation       @return string representation of this object      */    public String toString () {	String rt = new String ("PickResult: sgp:"+pickedSceneGraphPath+"\n");	if (pickedNode != null) rt += " node:"+pickedNode;		// TODO: catch cap not set exceptions and return no intersection info	if (intersections == null) {	    generateIntersections();	}	if (intersections.size() > 0) {	    for (int i=0;i<intersections.size();i++) {		rt +="\n";		rt += ((PickIntersection)intersections.get(i)).toString2();	    }	}		return rt;    }    /** Store the geometry for the node in this PickResult */    private void storeGeometry () {	if (pickedNode instanceof Morph) {	    geometryArrays = new GeometryArray[1];	    geometryArrays[0] = 		(GeometryArray) ((Morph)pickedNode).getGeometryArray (0);	} else if (pickedNode instanceof Shape3D) {	    Shape3D shape = ((Shape3D)pickedNode);	    ArrayList geoArrays = new ArrayList();	    for (int k = 0; k < shape.numGeometries(); k++) {		Geometry geometry = shape.getGeometry(k);		if (geometry instanceof CompressedGeometry) {		    Shape3D[] sa = ((CompressedGeometry)geometry).decompress();		    // System.out.println ("Decompressed geometry has "+sa.length+		    //  " Shape3Ds");		    if (sa != null) {			for (int j = 0; j < sa.length; j++) {			    for (int i = 0; i < sa[j].numGeometries(); i++) {				geoArrays.add(sa[j].getGeometry(i));			    }			}		    }		    if (compressGeomShape3Ds == null) {			// just save the new one			compressGeomShape3Ds = sa;		    } else {			// append the the new one on the end of the old array			Shape3D[] save = compressGeomShape3Ds;			int newLength = save.length + sa.length;			compressGeomShape3Ds = new Shape3D[newLength];			System.arraycopy(save, 0, compressGeomShape3Ds, 0,					 save.length);			System.arraycopy(sa, 0, compressGeomShape3Ds, save.length,					 sa.length);		    }		} else if (geometry instanceof GeometryArray) {		    geoArrays.add(geometry);		}	    }	    geometryArrays = new GeometryArray[geoArrays.size()];	    for (int i = 0; i < geoArrays.size(); i++) {		geometryArrays[i] = (GeometryArray) geoArrays.get(i);	    }	} 	if (geometryArrays == null) {	    if (pickedNode instanceof Shape3D) {		Shape3D shape = (Shape3D) pickedNode;	    }	    throw new RuntimeException ("Type of the picked node is not supported");	}    }    /** Get the picked node */    public Node getObject () {	// get node from scenegraphpath	if (pickedNode == null) {	    storeNode ();	}	return pickedNode;    }    /** Set the picked node */    void setObject (Node n) {	pickedNode = n;    }    /** Get the first node of a certain type up the SceneGraphPath       @param flags the type of node we are interested in      @return a Node object      */    public Node getNode (int flags) {	if (pickedNode == null) {	    storeNode ();	}	if ((pickedNode instanceof Shape3D) && ((flags & SHAPE3D) != 0)){	    if (debug) System.out.println("Shape3D found");	    return pickedNode;	} 	else if ((pickedNode instanceof Morph) && ((flags & MORPH) != 0)){	    if (debug) System.out.println("Morph found"); 	    return pickedNode;	}	else {	  	    if (pickedSceneGraphPath == null) {		return null;	    }	    for (int j=pickedSceneGraphPath.nodeCount()-1; j>=0; j--){		Node pNode = pickedSceneGraphPath.getNode(j); 		if (debug) System.out.println("looking at node " + pNode);	    		if ((pNode instanceof Primitive) &&		    ((flags & PRIMITIVE) != 0)){		    if (debug) System.out.println("Primitive found");		    return pNode;		}		else if ((pNode instanceof Link) && ((flags & LINK) != 0)){		    if (debug) System.out.println("Link found");		    return pNode;		}		else if ((pNode instanceof Switch) && ((flags & SWITCH) != 0)){		    if (debug) System.out.println("Switch found");		    return pNode;		}		else if ((pNode instanceof TransformGroup) &&			 ((flags & TRANSFORM_GROUP) != 0)){		    if (debug) System.out.println("xform group found");		    return pNode;		}		else if ((pNode instanceof BranchGroup) &&			 ((flags & BRANCH_GROUP) != 0)){		    if (debug) System.out.println("Branch group found");		    return pNode;		}		else if ((pNode instanceof Group) && ((flags & GROUP) != 0)){		    if (debug) System.out.println("Group found");		    return pNode;		}	     	    }	}	return null; // should not be reached    }    /** Extract the picked node from the SceneGraphPath */    void storeNode () {	if (pickedSceneGraphPath == null) {	    throw new RuntimeException ("SceneGraphPath missing");	}	pickedNode = pickedSceneGraphPath.getObject();    }    /** Fill in the intersections of the Node with the PickShape */    boolean generateIntersections() {	if (geometryArrays == null) { 	    storeGeometry();	}	intersections = new ArrayList();	int hits = 0;	for (int i = 0; i < geometryArrays.length; i++) {	    if (intersect(i, firstIntersectOnly)) {		if (firstIntersectOnly) {		    return true;		} else {		    hits++;		}	    }	}	return (hits > 0);    }    /*  Takes a GeometryArray object, determines what actual type      *  it is (RTTI) and casts it to call the appropriate intersect method.     */    final boolean intersect(int geomIndex, boolean firstpick) {	int offset;	GeometryArray geom = geometryArrays[geomIndex];	int numPts = geom.getVertexCount();	double[] doubleData = null;	float[] floatData = null;	Point3d[] p3dData = null;	Point3f[] p3fData = null;	int vformat = geom.getVertexFormat();	int stride;	boolean retFlag = false;	if ((vformat & GeometryArray.BY_REFERENCE) == 0) {	    doubleData = new double [numPts * 3];	    geom.getCoordinates (0, doubleData);	}	else {	    if ((vformat & GeometryArray.INTERLEAVED) == 0) {		doubleData = geom.getCoordRefDouble();		// If data was set as float then ..		if (doubleData == null) {		    floatData = geom.getCoordRefFloat();		    if (floatData == null) {			p3fData = geom.getCoordRef3f();			if (p3fData == null) {			    p3dData = geom.getCoordRef3d();			}		    }		}	    }	    else {		floatData = geom.getInterleavedVertices();	    }	}	Point3d[] pnts = new Point3d[numPts];		/*	  System.out.println("geomIndex : " + geomIndex);	  System.out.println("numPts : " + numPts);	  System.out.println("firstpick : " + firstpick);	  System.out.println("localToVWorld : ");	  System.out.println(localToVWorld);	*/		if (debug) {	    System.out.println("localToVWorld = " + localToVWorld);	}	if ((vformat & GeometryArray.INTERLEAVED) == 0) {	    if (doubleData != null) {		offset = 0;		for (int i=0; i < numPts; i++) {		    // Need to transform each pnt by localToVWorld.		    pnts[i] = new Point3d();		    pnts[i].x = doubleData[offset++];		    pnts[i].y = doubleData[offset++];		    pnts[i].z = doubleData[offset++];		    localToVWorld.transform(pnts[i]);		}	    }	    else  if (floatData != null) { // by reference and float data is defined ..		offset = 0;		for (int i=0; i < numPts; i++) {		    // Need to transform each pnt by localToVWorld.		    pnts[i] = new Point3d();		    pnts[i].x = floatData[offset++];		    pnts[i].y = floatData[offset++];		    pnts[i].z = floatData[offset++];		    localToVWorld.transform(pnts[i]);		}	    }	    else if (p3fData != null) {		for (int i=0; i < numPts; i++) {		    // Need to transform each pnt by localToVWorld.		    pnts[i] = new Point3d();		    pnts[i].set(p3fData[i]);		    localToVWorld.transform(pnts[i]);		}	    }	    else { // p3dData		for (int i=0; i < numPts; i++) {		    // Need to transform each pnt by localToVWorld.		    pnts[i] = new Point3d();		    pnts[i].set(p3dData[i]);		    localToVWorld.transform(pnts[i]);		}	    }	}	// Its an interleaved type ..	else {	    offset = 0;	    if ((vformat & GeometryArray.COLOR_3) == GeometryArray.COLOR_3) {		offset += 3;	    }	    else if ((vformat & GeometryArray.COLOR_4) == GeometryArray.COLOR_4) {		offset += 4;	    }	    if ((vformat & GeometryArray.NORMALS) != 0)		offset += 3;	    if ((vformat & GeometryArray.TEXTURE_COORDINATE_2) == GeometryArray.TEXTURE_COORDINATE_2) {		offset += 2 * geom.getTexCoordSetCount();	    }	    else if ((vformat & GeometryArray.TEXTURE_COORDINATE_3) == GeometryArray.TEXTURE_COORDINATE_3) {		offset += 3 * geom.getTexCoordSetCount();	    }	    stride = offset + 3; // for the vertices .	    for (int i=0; i < numPts; i++) {		// Need to transform each pnt by localToVWorld.		pnts[i] = new Point3d();		pnts[i].x = floatData[offset];		pnts[i].y = floatData[offset+1];		pnts[i].z = floatData[offset+2];		localToVWorld.transform(pnts[i]);		offset += stride;	    }	}	PickIntersection pi = new PickIntersection(this, geom);	if (geom instanceof PointArray) {	    retFlag = intersectPA ((PointArray)geom, geomIndex, pnts, firstpick, pi);	} else if (geom instanceof IndexedPointArray) {	    pi.iGeom = (IndexedGeometryArray) geom;	    retFlag = intersectIPA ((IndexedPointArray)geom, geomIndex, pnts,				 firstpick, pi);	} else if (geom instanceof LineArray) {	    retFlag = intersectLA ((LineArray)geom, geomIndex, pnts, firstpick, pi);	} else if (geom instanceof LineStripArray) {	    retFlag = intersectLSA ((LineStripArray)geom, geomIndex, pnts, 					 firstpick, pi);	} else if (geom instanceof IndexedLineArray) {	    pi.iGeom = (IndexedGeometryArray) geom;	    retFlag = intersectILA ((IndexedLineArray)geom, geomIndex, pnts,				 firstpick, pi);	} else if (geom instanceof IndexedLineStripArray) {	    pi.iGeom = (IndexedGeometryArray) geom;	    retFlag = intersectILSA ((IndexedLineStripArray)geom, geomIndex, pnts, 				  firstpick, pi);	} else if (geom instanceof TriangleArray) {	    retFlag = intersectTA ((TriangleArray)geom, geomIndex, pnts, 				firstpick, pi);	} else if (geom instanceof TriangleStripArray) {	    retFlag = intersectTSA ((TriangleStripArray)geom, geomIndex, pnts,				 firstpick, pi);	} else if (geom instanceof TriangleFanArray) {	    retFlag = intersectTFA ((TriangleFanArray)geom, geomIndex, pnts,				 firstpick, pi);	} else if (geom instanceof IndexedTriangleArray) {	    pi.iGeom = (IndexedGeometryArray) geom;	    retFlag = intersectITA ((IndexedTriangleArray)geom, geomIndex, pnts,				 firstpick, pi);	} else if (geom instanceof IndexedTriangleStripArray) {	    pi.iGeom = (IndexedGeometryArray) geom;	    retFlag = intersectITSA ((IndexedTriangleStripArray)geom, geomIndex, 				  pnts, firstpick, pi);	} else if (geom instanceof IndexedTriangleFanArray) {	    pi.iGeom = (IndexedGeometryArray) geom;	    retFlag = intersectITFA ((IndexedTriangleFanArray)geom, geomIndex, 				  pnts, firstpick, pi);	} else if (geom instanceof QuadArray) {	    retFlag = intersectQA ((QuadArray)geom, geomIndex, pnts, firstpick, pi);	} else if (geom instanceof IndexedQuadArray) {	    pi.iGeom = (IndexedGeometryArray) geom;	    retFlag = intersectIQA ((IndexedQuadArray)geom, geomIndex, pnts,				 firstpick, pi);	} else {	    throw new RuntimeException ("incorrect class type");	}	return retFlag;    }    /* ==================================================================== */    /*                 INTERSECT METHODS BY PRIMITIVE TYPE                  */    /* ==================================================================== */    boolean intersectPoint(int[] vertidx, int[] coordidx, int geomIndex, 			   Point3d[] pnts, PickIntersection pi) {	// PickIntersection pi = new PickIntersection(this);	Point3d[] point = new Point3d[1];	point[0] = pnts[coordidx[0]];	if (debug) {	    System.out.println("intersect point, point = " + point[0]);	}	boolean intersect = false;	switch(pickShapeType) {	case PICK_SHAPE_RAY:	    intersect = intersectPntAndRay(point[0], pickShapeStart, 					   pickShapeDir, pi);	    break;	case PICK_SHAPE_SEGMENT:	    if (intersectPntAndRay(point[0], pickShapeStart, pickShapeDir, pi)){		if(pi.getDistance() <= 1.0) { // TODO: why 1.0?		    intersect = true;		}	    }	    break;	    /* case PICK_SHAPE_POINT:	       intersect = intersectPntAndPnt(point[0],	       ((PickPoint) pickShape).location );	       break;	       */	case PICK_SHAPE_BOUNDING_BOX:	    intersect = ((BoundingBox)pickShapeBounds).intersect(point[0]);	    pi.setPointCoordinatesVW(point[0]);	    break;	case PICK_SHAPE_BOUNDING_SPHERE:	    intersect = ((BoundingSphere)pickShapeBounds).intersect(point[0]);	    pi.setPointCoordinatesVW(point[0]);	    break;	case PICK_SHAPE_BOUNDING_POLYTOPE:	    intersect = ((BoundingPolytope)pickShapeBounds).intersect(point[0]);	    pi.setPointCoordinatesVW(point[0]);	    break;	case PICK_SHAPE_CYLINDER:	    intersect = intersectCylinder(point[0], (PickCylinder)pickShape,pi);	    break;	case PICK_SHAPE_CONE:	    intersect = intersectCone (point[0], (PickCone)pickShape, pi);

⌨️ 快捷键说明

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