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

📄 intersect.java

📁 JAVA3D矩陈的相关类
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    else if(dir.y != 0.0) {      if(pnt.x != ori.x)	return false;      flag = 1;      dist[0] = (pnt.y - ori.y)/dir.y;    }    else if(dir.z != 0.0) {      if((pnt.x != ori.x)||(pnt.y != ori.y))	return false;      flag = 2;      dist[0] = (pnt.z - ori.z)/dir.z;               }    else      return false;    if(dist[0] < 0.0)      return false;    if(flag == 0) {      temp = ori.y + dist[0] * dir.y;      if((pnt.y < (temp - Double.MIN_VALUE)) || (pnt.y > (temp + Double.MIN_VALUE)))	return false;        }        if(flag < 2) {      temp = ori.z + dist[0] * dir.z;      if((pnt.z < (temp - Double.MIN_VALUE)) || (pnt.z > (temp + Double.MIN_VALUE)))	return false;    }    return true;      }  private static boolean rayAndPoly( Point3d coordinates[], 				     PickRay ray, double dist[] ) {        Vector3d vec0 = new Vector3d(); // Edge vector from point 0 to point 1;    Vector3d vec1 = new Vector3d(); // Edge vector from point 0 to point 2 or 3;    Vector3d pNrm = new Vector3d();    double  absNrmX, absNrmY, absNrmZ, pD = 0.0;    Vector3d tempV3d = new Vector3d();    double pNrmDotrDir = 0.0;     int axis, nc, sh, nsh;    Point3d origin = new Point3d();    Vector3d direction = new Vector3d();    Point3d iPnt = new Point3d(); // Point of intersection.        double uCoor[] = new double[4]; // Only need to support up to quad.    double vCoor[] = new double[4];    double tempD;    int i, j;    // Compute plane normal.    for(i=0; i<coordinates.length-1;) {      vec0.x = coordinates[i+1].x - coordinates[i].x;      vec0.y = coordinates[i+1].y - coordinates[i].y;      vec0.z = coordinates[i+1].z - coordinates[i++].z;      if(vec0.length() > 0.0)	break;    }            for(j=i; j<coordinates.length-1; j++) {      vec1.x = coordinates[j+1].x - coordinates[j].x;      vec1.y = coordinates[j+1].y - coordinates[j].y;      vec1.z = coordinates[j+1].z - coordinates[j].z;      if(vec1.length() > 0.0)	break;    }        if(j == (coordinates.length-1)) {      // System.out.println("(1) Degenerated polygon.");      return false;  // Degenerated polygon.    }    /*        System.out.println("Triangle/Quad :");       for(i=0; i<coordinates.length; i++)        System.out.println("P" + i + " " + coordinates[i]);       */    pNrm.cross(vec0,vec1);        if(pNrm.length() == 0.0) {      // System.out.println("(2) Degenerated polygon.");      return false;  // Degenerated polygon.    }    ray.get(origin, direction);    // System.out.println("Ray orgin : " + origin + " dir " + direction);    // Compute plane D.    tempV3d.set((Tuple3d) coordinates[0]);    pD = pNrm.dot(tempV3d);    pNrmDotrDir = pNrm.dot(direction);        // Ray is parallel to plane.     if(pNrmDotrDir== 0.0) {      // System.out.println("Ray is parallel to plane.");      return false;            }    tempV3d.set((Tuple3d) origin);    dist[0] = (pD - pNrm.dot(tempV3d))/ pNrmDotrDir;    // Ray intersects the plane behind the ray's origin.    if(dist[0] < 0.0 ) {      // System.out.println("Ray intersects the plane behind the ray's origin.");      return false;    }    // Now, one thing for sure the ray intersects the plane.    // Find the intersection point.    iPnt.x = origin.x + direction.x * dist[0];    iPnt.y = origin.y + direction.y * dist[0];    iPnt.z = origin.z + direction.z * dist[0];        // System.out.println("dist " + dist[0] + " iPnt : " + iPnt);    // Project 3d points onto 2d plane and apply Jordan curve theorem.     // Note : Area of polygon is not preserve in this projection, but    // it doesn't matter here.         // Find the axis of projection.    absNrmX = Math.abs(pNrm.x);    absNrmY = Math.abs(pNrm.y);    absNrmZ = Math.abs(pNrm.z);    if(absNrmX > absNrmY)      axis = 0;    else       axis = 1;    if(axis == 0) {      if(absNrmX < absNrmZ)	axis = 2;    }        else if(axis == 1) {      if(absNrmY < absNrmZ)	axis = 2;    }            // System.out.println("Normal " + pNrm + " axis " + axis );        for(i=0; i<coordinates.length; i++) {      switch (axis) {      case 0:	uCoor[i] = coordinates[i].y - iPnt.y;	vCoor[i] = coordinates[i].z - iPnt.z;	break;	      case 1:      	uCoor[i] = coordinates[i].x - iPnt.x;	vCoor[i] = coordinates[i].z - iPnt.z;	break;	      case 2:	uCoor[i] = coordinates[i].x - iPnt.x;	vCoor[i] = coordinates[i].y - iPnt.y;	break;            }            // System.out.println("i " + i + " u " + uCoor[i] + " v " + vCoor[i]);     }        // initialize number of crossing, nc.    nc = 0;       if(vCoor[0] < 0.0)      sh = -1;    else       sh = 1;    for(i=0; i<coordinates.length; i++) {      j= i+1;      if(j==coordinates.length)	j=0;            if(vCoor[j] < 0.0)	nsh = -1;      else	nsh = 1;      if(sh != nsh) {	if((uCoor[i] > 0.0) && (uCoor[j] > 0.0)) {	  // This line must cross U+.	  nc++;	}	else if((uCoor[i] > 0.0) || (uCoor[j] > 0.0)) {	  // This line might cross U+. We need to compute intersection on U azis.	  tempD = uCoor[i]-vCoor[i]*(uCoor[j]-uCoor[i])/(vCoor[j]-vCoor[i]);	  if(tempD > 0)	    // This line cross U+.	    nc++;	  	}		sh = nsh;      } // sh != nsh    }    // System.out.println("nc " + nc);       if((nc%2) == 1) {      	// calculate the distance	dist[0] *= direction.length();	// System.out.println("Ray Intersected!");		/* 	   System.out.println("Ray orgin : " + origin + " dir " + direction);	   System.out.println("Triangle/Quad :");	   for(i=0; i<coordinates.length; i++) 	   System.out.println("P" + i + " " + coordinates[i]);	   System.out.println("dist " + dist[0] + " iPnt : " + iPnt);	   */	return true;    }    else {	// System.out.println("Ray Not Intersected!");	return false;    }  }  /**   *  Return true if triangle or quad intersects with segment and the distance is    *  stored in dist[0].   * */       private static boolean segmentAndPoly( Point3d coordinates[], 						  PickSegment segment, 						  double dist[] ) {        Vector3d vec0 = new Vector3d(); // Edge vector from point 0 to point 1;    Vector3d vec1 = new Vector3d(); // Edge vector from point 0 to point 2 or 3;    Vector3d pNrm = new Vector3d();    double  absNrmX, absNrmY, absNrmZ, pD = 0.0;    Vector3d tempV3d = new Vector3d();    Vector3d direction = new Vector3d();    double pNrmDotrDir = 0.0;     int axis, nc, sh, nsh;    Point3d start = new Point3d();     Point3d end = new Point3d();     Point3d iPnt = new Point3d(); // Point of intersection.        double uCoor[] = new double[4]; // Only need to support up to quad.    double vCoor[] = new double[4];    double tempD;    int i, j;    // Compute plane normal.    for(i=0; i<coordinates.length-1;) {      vec0.x = coordinates[i+1].x - coordinates[i].x;      vec0.y = coordinates[i+1].y - coordinates[i].y;      vec0.z = coordinates[i+1].z - coordinates[i++].z;      if(vec0.length() > 0.0)	break;    }            for(j=i; j<coordinates.length-1; j++) {      vec1.x = coordinates[j+1].x - coordinates[j].x;      vec1.y = coordinates[j+1].y - coordinates[j].y;      vec1.z = coordinates[j+1].z - coordinates[j].z;      if(vec1.length() > 0.0)	break;    }        if(j == (coordinates.length-1)) {      // System.out.println("(1) Degenerated polygon.");      return false;  // Degenerated polygon.    }        /*        System.out.println("Triangle/Quad :");       for(i=0; i<coordinates.length; i++)        System.out.println("P" + i + " " + coordinates[i]);       */    pNrm.cross(vec0,vec1);        if(pNrm.length() == 0.0) {      // System.out.println("(2) Degenerated polygon.");      return false;  // Degenerated polygon.    }    // Compute plane D.    tempV3d.set((Tuple3d) coordinates[0]);    pD = pNrm.dot(tempV3d);    segment.get(start, end);    // System.out.println("Segment start : " + start + " end " + end);    direction.x = end.x - start.x;    direction.y = end.y - start.y;    direction.z = end.z - start.z;    pNrmDotrDir = pNrm.dot(direction);        // Segment is parallel to plane.     if(pNrmDotrDir== 0.0) {      // System.out.println("Segment is parallel to plane.");      return false;            }    tempV3d.set((Tuple3d) start);    dist[0] = (pD - pNrm.dot(tempV3d))/ pNrmDotrDir;    // Segment intersects the plane behind the segment's start.    // or exceed the segment's length.    if((dist[0] < 0.0 ) || (dist[0] > 1.0 )) {      // System.out.println("Segment intersects the plane behind the start or exceed end.");      return false;    }    // Now, one thing for sure the segment intersect the plane.    // Find the intersection point.    iPnt.x = start.x + direction.x * dist[0];    iPnt.y = start.y + direction.y * dist[0];    iPnt.z = start.z + direction.z * dist[0];        // System.out.println("dist " + dist[0] + " iPnt : " + iPnt);    // Project 3d points onto 2d plane and apply Jordan curve theorem.     // Note : Area of polygon is not preserve in this projection, but    // it doesn't matter here.         // Find the axis of projection.    absNrmX = Math.abs(pNrm.x);    absNrmY = Math.abs(pNrm.y);    absNrmZ = Math.abs(pNrm.z);    if(absNrmX > absNrmY)      axis = 0;    else       axis = 1;    if(axis == 0) {      if(absNrmX < absNrmZ)	axis = 2;    }        else if(axis == 1) {      if(absNrmY < absNrmZ)	axis = 2;    }            // System.out.println("Normal " + pNrm + " axis " + axis );        for(i=0; i<coordinates.length; i++) {      switch (axis) {      case 0:	uCoor[i] = coordinates[i].y - iPnt.y;	vCoor[i] = coordinates[i].z - iPnt.z;	break;	      case 1:      	uCoor[i] = coordinates[i].x - iPnt.x;	vCoor[i] = coordinates[i].z - iPnt.z;	break;	      case 2:	uCoor[i] = coordinates[i].x - iPnt.x;	vCoor[i] = coordinates[i].y - iPnt.y;	break;            }            // System.out.println("i " + i + " u " + uCoor[i] + " v " + vCoor[i]);     }        // initialize number of crossing, nc.    nc = 0;       if(vCoor[0] < 0.0)      sh = -1;    else       sh = 1;    for(i=0; i<coordinates.length; i++) {      j= i+1;      if(j==coordinates.length)	j=0;            if(vCoor[j] < 0.0)	nsh = -1;      else	nsh = 1;      if(sh != nsh) {	if((uCoor[i] > 0.0) && (uCoor[j] > 0.0)) {	  // This line must cross U+.	  nc++;	}	else if((uCoor[i] > 0.0) || (uCoor[j] > 0.0)) {	  // This line might cross U+. We need to compute intersection on U azis.	  tempD = uCoor[i]-vCoor[i]*(uCoor[j]-uCoor[i])/(vCoor[j]-vCoor[i]);	  if(tempD > 0)	    // This line cross U+.	    nc++;	  	}		sh = nsh;      } // sh != nsh    }    // System.out.println("nc " + nc);       if((nc%2) == 1) {      	// calculate the distance	dist[0] *= direction.length();		// System.out.println("Segment Intersected!");		/* 	   System.out.println("Segment orgin : " + start + " dir " + direction);	   System.out.println("Triangle/Quad :");	   for(i=0; i<coordinates.length; i++) 	   System.out.println("P" + i + " " + coordinates[i]);	   System.out.println("dist " + dist[0] + " iPnt : " + iPnt);	   */	return true;    }    else {	// System.out.println("Segment Not Intersected!");	return false;    }  }    }

⌨️ 快捷键说明

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