1 /* 2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.javafx.scene; 27 28 import javafx.geometry.Point2D; 29 import javafx.geometry.Point3D; 30 import javafx.scene.Node; 31 import javafx.scene.SubScene; 32 33 /** 34 * Utility class for helper methods needed by scene. 35 */ 36 public class SceneUtils { 37 38 /** 39 * Translates point from inner subScene coordinates to scene coordinates. 40 */ 41 public static Point3D subSceneToScene(SubScene subScene, Point3D point) { 42 43 Node n = subScene; 44 while(n != null) { 45 // flatten the coords - project them by subScene's camera 46 final Point2D projection = CameraHelper.project( 47 SubSceneHelper.getEffectiveCamera(subScene), point); 48 // transform to scene/outer-subScene coords 49 point = n.localToScene(projection.getX(), projection.getY(), 0.0); 50 n = NodeHelper.getSubScene(n); 51 } 52 53 return point; 54 } 55 56 /** 57 * Translates point from scene coordinates to subScene local coordinates. 58 */ 59 public static Point2D sceneToSubScenePlane(SubScene subScene, Point2D point) { 60 61 // compute pick ray intersection with the subScene, recursively 62 // over all parent subScenes 63 point = computeSubSceneCoordinates(point.getX(), point.getY(), subScene); 64 65 return point; 66 } 67 68 /** 69 * Computes subScene local intersection point from the given scene mouse 70 * coordinates. Works recursively over all outer subScenes. 71 */ 72 private static Point2D computeSubSceneCoordinates( 73 double x, double y, SubScene subScene) { 74 SubScene outer = NodeHelper.getSubScene(subScene); 75 76 if (outer == null) { 77 return CameraHelper.pickNodeXYPlane( 78 SceneHelper.getEffectiveCamera(subScene.getScene()), 79 subScene, x, y); 80 } else { 81 Point2D coords = computeSubSceneCoordinates(x, y, outer); 82 if (coords != null) { 83 coords = CameraHelper.pickNodeXYPlane( 84 SubSceneHelper.getEffectiveCamera(outer), 85 subScene, coords.getX(), coords.getY()); 86 } 87 return coords; 88 } 89 } 90 91 // This method is currently not used, but its code may be useful 92 // for the future introducion of public 3D picking and in particular 93 // 3D screenToLocal functionality. 94 // /** 95 // * Translates 2D scene coordinates to 3D local coordinates of a node. 96 // * 97 // * If the node contains (is rendered at) the given flat coordinates, 98 // * the returned point represents the intersection of the node and a 99 // * ray cast by the given coordinates; otherwise it represents 100 // * the intersection point of the ray and the projection plane. 101 // */ 102 // public Point3D sceneToLocal3D(Node n, double sceneX, double sceneY) { 103 // Scene scene = n.getScene(); 104 // if (scene == null) { 105 // return null; 106 // } 107 // 108 // Point2D pt = new Point2D(screenX, screenY); 109 // final SubScene subScene = NodeHelper.getSubScene(n); 110 // if (subScene != null) { 111 // pt = SceneUtils.sceneToSubScenePlane(subScene, pt); 112 // if (pt == null) { 113 // return null; 114 // } 115 // } 116 // 117 // // compute pick ray 118 // final Camera cam = subScene != null 119 // ? SubSceneHelper.getEffectiveCamera(subScene) 120 // : SceneHelper.getEffectiveCamera(scene); 121 // final PickRay pickRay = cam.computePickRay(pt.getX(), pt.getY(), null); 122 // 123 // // convert it to node's local pickRay 124 // final Affine3D localToSceneTx = new Affine3D(); 125 // TransformHelper.apply(n.getLocalToSceneTransform(), localToSceneTx); 126 // try { 127 // Vec3d origin = pickRay.getOriginNoClone(); 128 // Vec3d direction = pickRay.getDirectionNoClone(); 129 // localToSceneTx.inverseTransform(origin, origin); 130 // localToSceneTx.inverseDeltaTransform(direction, direction); 131 // } catch (NoninvertibleTransformException e) { 132 // return null; 133 // } 134 // 135 // // compute the intersection 136 // final PickResultChooser result = new PickResultChooser(); 137 // NodeHelper.computeIntersects(this, pickRay, result); 138 // if (result.getIntersectedNode() == n) { 139 // return result.getIntersectedPoint(); 140 // } 141 // 142 // // there is none, use point on projection plane instead 143 // final Point3D ppIntersect = CameraAccess.getCameraAccess().pickProjectPlane(cam, pt.getX(), pt.getY()); 144 // return n.sceneToLocal(ppIntersect); 145 // } 146 }