📄 picktool.java
字号:
obj[i] = sgp[i].getObject(); pr[i] = new PickResult (sgp[i], pickShape); if (obj[i] instanceof Shape3D) { found[i] = ((Shape3D) obj[i]).intersect(sgp[i], pickShape); } else if (obj[i] instanceof Morph) { found[i] = ((Morph) obj[i]).intersect(sgp[i], pickShape); } if (found[i] == true) cnt++; } if (cnt == 0) return null; // no match PickResult[] newpr = new PickResult[cnt]; cnt = 0; // reset for reuse. for(i=0; i<sgp.length; i++) { if (found[i] == true) pr[cnt++] = pr[i]; } return pr; } private PickResult[] pickGeomAllSorted(PickShape pickShape) { SceneGraphPath[] sgp = null; Node[] obj = null; int i, cnt=0; double[] dist = new double[1]; // First pass if (pickRootBG != null) { sgp = pickRootBG.pickAll(pickShape); } else if (pickRootL != null) { sgp = pickRootL.pickAll(pickShape); } if (sgp == null) return null; // no match /* System.out.println ("PickTool.pickGeomAllSorted: bounds " + "picking found "+sgp.length+" nodes"); */ // Second pass, check to see if geometries intersected boolean[] found = new boolean [sgp.length]; double[] distArr = new double[sgp.length]; obj = new Node [sgp.length]; PickResult[] pr = new PickResult [sgp.length]; for (i=0; i<sgp.length; i++) { obj[i] = sgp[i].getObject(); pr[i] = new PickResult (sgp[i], pickShape); if (obj[i] instanceof Shape3D) { found[i] = ((Shape3D)obj[i]).intersect(sgp[i], pickShape, dist); distArr[i] = dist[0]; } else if (obj[i] instanceof Morph) { found[i] = ((Morph)obj[i]).intersect(sgp[i], pickShape, dist); distArr[i] = dist[0]; } if (found[i] == true) cnt++; } if (cnt == 0) return null; // no match PickResult[] npr = new PickResult [cnt]; double[] distance = new double [cnt]; cnt = 0; // reset for reuse. for(i=0; i<sgp.length; i++) { if (found[i] == true) { distance[cnt] = distArr[i]; npr[cnt++] = pr[i]; } } if (cnt > 1) { return sortPickResults (npr, distance); } else { // Don't have to sort if only one item return npr; } } private PickResult pickGeomAny (PickShape pickShape) { Node obj = null; int i; SceneGraphPath[] sgpa = null; if (pickRootBG != null) { sgpa = pickRootBG.pickAll(pickShape); } else if (pickRootL != null) { sgpa = pickRootL.pickAll(pickShape); } if (sgpa == null) return null; // no match for(i=0; i<sgpa.length; i++) { obj = sgpa[i].getObject(); PickResult pr = new PickResult(sgpa[i], pickShape); if(obj instanceof Shape3D) { if(((Shape3D) obj).intersect(sgpa[i], pickShape)) { return pr; } } else if (obj instanceof Morph) { if(((Morph) obj).intersect(sgpa[i], pickShape)){ return pr; } } } return null; } private PickResult pickGeomClosest(PickShape pickShape) { // System.out.println("pickGeomCloset -- Geometry based picking"); PickResult[] pr = pickGeomAllSorted(pickShape); if (pr == null) { return null; } else { return pr[0]; } } // ================================================================ // NEW METHODS, return additional information // ================================================================ private PickResult[] pickGeomAllIntersect(PickShape pickShape) { SceneGraphPath[] sgp = null; Node obj[] = null; int i, cnt=0; // First pass if (pickRootBG != null) { sgp = pickRootBG.pickAll(pickShape); } else if (pickRootL != null) { sgp = pickRootL.pickAll(pickShape); } if (sgp == null) return null; // no match // Second pass, check to see if geometries intersected boolean found[] = new boolean [sgp.length]; PickResult[] pr = new PickResult[sgp.length]; for (i=0; i<sgp.length; i++) { pr[i] = new PickResult (sgp[i], pickShape); if (pr[i].numIntersections() > 0) { found[i] = true; cnt++; } } if (cnt == 0) return null; // no match PickResult[] newpr = new PickResult[cnt]; cnt = 0; // reset for reuse. for(i=0; i<sgp.length; i++) { if(found[i] == true) pr[cnt++] = pr[i]; } return pr; } private PickResult[] pickGeomAllSortedIntersect (PickShape pickShape) { SceneGraphPath[] sgp = null; Node[] obj = null; int i, cnt=0; double[] dist = new double[1]; // First pass if (pickRootBG != null) { sgp = pickRootBG.pickAll(pickShape); } else if (pickRootL != null) { sgp = pickRootL.pickAll(pickShape); } if (sgp == null) return null; // no match // System.out.println ("PickTool.pickGeomAllSortedIntersect: bounds " + // " picking found "+sgp.length+" nodes"); // Second pass, check to see if geometries intersected boolean[] found = new boolean[sgp.length]; double[] distArr = new double[sgp.length]; PickResult[] pr = new PickResult[sgp.length]; for (i=0; i<sgp.length; i++) { pr[i] = new PickResult(sgp[i], pickShape); int numIntersection = pr[i].numIntersections(); if (numIntersection > 0) { // System.out.println ("numIntersection " + numIntersection); found[i] = true; double minDist; double tempDist; int minIndex; boolean needToSwap = false; minDist = pr[i].getIntersection(0).getDistance(); minIndex = 0; for(int j=1; j<numIntersection; j++) { // System.out.println ("Distance " + pr[i].getIntersection(j).getDistance()); //System.out.println ("Geom Index " + pr[i].getIntersection(j).getGeometryArrayIndex()); tempDist = pr[i].getIntersection(j).getDistance(); if(minDist > tempDist) { minDist = tempDist; minIndex = j; needToSwap = true; } } //Swap if necc. if(needToSwap) { // System.out.println ("Swap is needed"); PickIntersection pi0 = pr[i].getIntersection(0); PickIntersection piMin = pr[i].getIntersection(minIndex); pr[i].intersections.set(0, piMin); pr[i].intersections.set(minIndex, pi0); } distArr[i] = pr[i].getIntersection(0).getDistance(); cnt++; } } // System.out.println ("PickTool.pickGeomAllSortedIntersect: geometry intersect check " // + " cnt " + cnt); if (cnt == 0) return null; // no match PickResult[] npr = new PickResult[cnt]; double[] distance = new double[cnt]; cnt = 0; // reset for reuse. for(i=0; i<sgp.length; i++) { if(found[i] == true) { distance[cnt] = distArr[i]; npr[cnt++] = pr[i]; } } if (cnt > 1) { return sortPickResults (npr, distance); } else { // Don't have to sort if only one item return npr; } } private PickResult pickGeomClosestIntersect(PickShape pickShape) { PickResult[] pr = pickGeomAllSortedIntersect(pickShape); /* System.out.println ("PickTool.pickGeomClosestIntersect: pr.length " + pr.length); for(int i=0;i<pr.length;i++) { System.out.println ("pr["+i+"] " + pr[i]); } */ if (pr == null) { return null; } else { return pr[0]; } } private PickResult pickGeomAnyIntersect(PickShape pickShape) { Node obj = null; int i; SceneGraphPath[] sgpa = null; if (pickRootBG != null) { sgpa = pickRootBG.pickAll(pickShape); } else if (pickRootL != null) { sgpa = pickRootL.pickAll(pickShape); } if (sgpa == null) return null; // no match for(i=0; i<sgpa.length; i++) { PickResult pr = new PickResult(sgpa[i], pickShape); pr.setFirstIntersectOnly(true); if (pr.numIntersections() > 0) { return pr; } } return null; } // ================================================================ // Sort Methods // ================================================================ private PickResult[] sortPickResults (PickResult[] pr, double[] dist) { int[] pos = new int [pr.length]; PickResult[] prsorted = new PickResult [pr.length]; // Initialize position array for (int i=0; i<pr.length; i++) { pos[i]=i; } // Do sort quicksort (0, dist.length-1, dist, pos); // Create new array for (int i=0; i<pr.length; i++) { prsorted[i] = pr[pos[i]]; } return prsorted; } private final void quicksort( int l, int r, double[] dist, int[] pos) { int p,i,j; double tmp,k; i = l; j = r; k = dist[(l+r) / 2]; do { while (dist[i]<k) i++; while (k<dist[j]) j--; if (i<=j) { tmp = dist[i]; dist[i] =dist[j]; dist[j] = tmp; p=pos[i]; pos[i]=pos[j]; pos[j]=p; i++; j--; } } while (i<=j); if (l<j) quicksort(l, j, dist, pos); if (l<r) quicksort(i, r, dist, pos); }} // PickTool
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -