Search in sources :

Example 1 with OctreeNode

use of gaiasky.util.tree.OctreeNode in project gaiasky by langurmonkey.

the class NaturalCamera method notify.

@Override
public void notify(final Event event, Object source, final Object... data) {
    switch(event) {
        case FOCUS_CHANGE_CMD:
            setTrackingObject(null, null);
            // Check the type of the parameter: IFocus or String
            IFocus focus = null;
            // Center focus or not
            boolean centerFocus = !Settings.settings.runtime.openVr;
            if (data.length > 1)
                centerFocus = (Boolean) data[1];
            if (data[0] instanceof String) {
                SceneGraphNode sgn = GaiaSky.instance.sceneGraph.getNode((String) data[0]);
                if (sgn instanceof IFocus) {
                    focus = (IFocus) sgn;
                    diverted = !centerFocus;
                }
            } else if (data[0] instanceof IFocus) {
                focus = (IFocus) data[0];
                diverted = !centerFocus;
            }
            setFocus(focus);
            checkFocus();
            break;
        case FOV_CHANGED_CMD:
            boolean checkMax = source instanceof Actor;
            float fov = MathUtilsd.clamp((float) data[0], Constants.MIN_FOV, checkMax ? Constants.MAX_FOV : 179f);
            for (PerspectiveCamera cam : cameras) {
                cam.fieldOfView = fov;
            }
            fovFactor = camera.fieldOfView / 40f;
            if (parent.current == this) {
                EventManager.publish(Event.FOV_CHANGE_NOTIFICATION, this, fov, fovFactor);
            }
            break;
        case CUBEMAP_CMD:
            boolean state = (boolean) data[0];
            CubemapProjection p = (CubemapProjection) data[1];
            if (p.isPlanetarium() && state && !Settings.settings.runtime.openVr) {
                fovBackup = GaiaSky.instance.cameraManager.getCamera().fieldOfView;
            }
            break;
        case CAMERA_POS_CMD:
            synchronized (updateLock) {
                pos.set((double[]) data[0]);
                posinv.set(pos).scl(-1d);
            }
            break;
        case CAMERA_DIR_CMD:
            synchronized (updateLock) {
                direction.set((double[]) data[0]).nor();
            }
            break;
        case CAMERA_UP_CMD:
            synchronized (updateLock) {
                up.set((double[]) data[0]).nor();
            }
            break;
        case CAMERA_PROJECTION_CMD:
            synchronized (updateLock) {
                // Position
                pos.set((double[]) data[0]);
                posinv.set(pos).scl(-1d);
                // Direction
                direction.set((double[]) data[1]).nor();
                // Up
                up.set((double[]) data[2]).nor();
                // Change projection flag
                projectionFlag = true;
            }
            break;
        case CAMERA_FWD:
            synchronized (updateLock) {
                addForwardForce((double) data[0]);
            }
            break;
        case CAMERA_ROTATE:
            synchronized (updateLock) {
                addRotateMovement((double) data[0], (double) data[1], false, true);
            }
            break;
        case CAMERA_TURN:
            synchronized (updateLock) {
                addRotateMovement((double) data[0], (double) data[1], true, true);
            }
            break;
        case CAMERA_PAN:
            break;
        case CAMERA_ROLL:
            synchronized (updateLock) {
                addRoll((double) data[0], Settings.settings.scene.camera.cinematic);
            }
            break;
        case CAMERA_STOP:
            synchronized (updateLock) {
                stopTotalMovement();
            }
            break;
        case CAMERA_CENTER:
            synchronized (updateLock) {
                diverted = false;
            }
            break;
        case GO_TO_OBJECT_CMD:
            if (this.focus != null) {
                final IFocus f = this.focus;
                GaiaSky.postRunnable(() -> {
                    setTrackingObject(null, null);
                    // Position camera near focus
                    stopTotalMovement();
                    f.getAbsolutePosition(aux1b);
                    pos.set(aux1b);
                    double dx = 0d;
                    double dy = f.getSize() / 4d;
                    double dz = -f.getSize() * 4d;
                    if (Settings.settings.runtime.openVr) {
                        dz = -dz;
                    }
                    pos.add(dx, dy, dz);
                    posinv.set(pos).scl(-1d);
                    direction.set(aux1b).sub(pos).nor();
                    up.set(direction.x, direction.z, -direction.y).nor();
                    rotate(up, 0.01);
                    updatePerspectiveCamera();
                });
            }
            break;
        case ORIENTATION_LOCK_CMD:
            synchronized (updateLock) {
                previousOrientationAngle = 0;
            }
            break;
        case FREE_MODE_COORD_CMD:
            synchronized (updateLock) {
                double ra = (Double) data[0];
                double dec = (Double) data[1];
                double dist = 1e12d * Constants.PC_TO_U;
                aux1.set(MathUtilsd.degRad * ra, MathUtilsd.degRad * dec, dist);
                Coordinates.sphericalToCartesian(aux1, aux2);
                freeTargetPos.set(aux2);
                facingFocus = false;
                freeTargetOn = true;
            }
            break;
        case FOCUS_NOT_AVAILABLE:
            if (getMode().isFocus()) {
                boolean found = false;
                if (data[0] instanceof IFocus) {
                    focus = (IFocus) data[0];
                    found = isFocus(focus);
                } else if (data[0] instanceof OctreeWrapper) {
                    OctreeWrapper octree = (OctreeWrapper) data[0];
                    OctreeNode octant = this.focus.getOctant();
                    if (octant != null && octant.getRoot() == octree.root) {
                        found = true;
                    }
                } else if (data[0] instanceof GenericCatalog) {
                    GenericCatalog gc = (GenericCatalog) data[0];
                    if (gc.children != null && gc.children.contains((SceneGraphNode) this.focus, true)) {
                        found = true;
                    }
                }
                if (found) {
                    // Set camera  free
                    EventManager.publish(Event.CAMERA_MODE_CMD, this, CameraMode.FREE_MODE);
                }
            }
            break;
        case TOGGLE_VISIBILITY_CMD:
            if (getMode().isFocus()) {
                ComponentType ct = ComponentType.getFromKey((String) data[0]);
                if (this.focus != null && ct != null && this.focus.getCt().isEnabled(ct)) {
                    // Set camera  free
                    EventManager.publish(Event.CAMERA_MODE_CMD, this, CameraMode.FREE_MODE);
                }
            }
            break;
        case CAMERA_CENTER_FOCUS_CMD:
            synchronized (updateLock) {
                setCenterFocus((Boolean) data[0]);
            }
            break;
        case CONTROLLER_CONNECTED_INFO:
            Settings.settings.controls.gamepad.addControllerListener(controllerListener, (String) data[0]);
            break;
        case CONTROLLER_DISCONNECTED_INFO:
            // Nothing
            break;
        case NEW_DISTANCE_SCALE_FACTOR:
            synchronized (updateLock) {
                DIST_A = 0.1 * Constants.PC_TO_U;
                DIST_B = 5.0 * Constants.KPC_TO_U;
                DIST_C = 5000.0 * Constants.MPC_TO_U;
            }
            break;
        case CAMERA_TRACKING_OBJECT_CMD:
            final IFocus newTrackingObject = (IFocus) data[0];
            final String newTrackingName = (String) data[1];
            synchronized (updateLock) {
                this.setTrackingObject(newTrackingObject, newTrackingName != null ? newTrackingName.toLowerCase() : null);
            }
            break;
        default:
            break;
    }
}
Also used : ComponentType(gaiasky.render.ComponentTypes.ComponentType) OctreeNode(gaiasky.util.tree.OctreeNode) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) Actor(com.badlogic.gdx.scenes.scene2d.Actor) CubemapProjection(gaiasky.util.gdx.contrib.postprocess.effects.CubemapProjections.CubemapProjection) OctreeWrapper(gaiasky.scenegraph.octreewrapper.OctreeWrapper)

Example 2 with OctreeNode

use of gaiasky.util.tree.OctreeNode in project gaiasky by langurmonkey.

the class StreamingOctreeLoader method addToQueue.

public void addToQueue(OctreeNode octant) {
    // Add only if there is room.
    if (!loadingPaused) {
        if (toLoadQueue.size() >= LOAD_QUEUE_MAX_SIZE) {
            OctreeNode out = toLoadQueue.poll();
            out.setStatus(LoadStatus.NOT_LOADED);
        }
        toLoadQueue.add(octant);
        octant.setStatus(LoadStatus.QUEUED);
    }
}
Also used : OctreeNode(gaiasky.util.tree.OctreeNode)

Example 3 with OctreeNode

use of gaiasky.util.tree.OctreeNode in project gaiasky by langurmonkey.

the class StreamingOctreeLoader method loadOctants.

/**
 * Loads the objects of the given octants.
 *
 * @param octants       The list holding the octants to load.
 * @param octreeWrapper The octree wrapper.
 * @param abort         State variable that will be set to true if an abort is called.
 *
 * @return The actual number of loaded octants.
 *
 * @throws IOException When any of the octants' files fail to load.
 */
public int loadOctants(final Array<OctreeNode> octants, final AbstractOctreeWrapper octreeWrapper, final AtomicBoolean abort) throws IOException {
    int loaded = 0;
    if (octants.size > 0) {
        int i = 0;
        OctreeNode octant = octants.get(0);
        while (i < octants.size && !abort.get()) {
            if (loadOctant(octant, octreeWrapper, true))
                loaded++;
            i += 1;
            octant = octants.get(i);
        }
        flushLoadedIds();
        if (abort.get()) {
            // We aborted, roll back status of rest of octants
            for (int j = i; j < octants.size; j++) {
                octants.get(j).setStatus(LoadStatus.NOT_LOADED);
            }
        }
    }
    return loaded;
}
Also used : OctreeNode(gaiasky.util.tree.OctreeNode)

Example 4 with OctreeNode

use of gaiasky.util.tree.OctreeNode in project gaiasky by langurmonkey.

the class OctreeGeneratorRun method run.

public void run() {
    try {
        if (outFolder == null) {
            outFolder = System.getProperty("java.io.tmpdir");
        }
        if (!outFolder.endsWith("/"))
            outFolder += "/";
        Path outPath = Path.of(outFolder);
        Files.createDirectories(outPath);
        // Assets location
        String ASSETS_LOC = Settings.ASSETS_LOC;
        Gdx.files = new Lwjgl3Files();
        // Add notification watch
        new ConsoleLogger();
        // Initialize number format
        NumberFormatFactory.initialize(new DesktopNumberFormatFactory());
        // Initialize date format
        DateFormatFactory.initialize(new DesktopDateFormatFactory());
        // Initialize i18n
        I18n.initialize(Path.of(ASSETS_LOC, "i18n/gsbundle"));
        // Initialize configuration
        Path dummyv = Path.of(ASSETS_LOC, "data/dummyversion");
        if (!Files.exists(dummyv)) {
            dummyv = Path.of(ASSETS_LOC, "dummyversion");
        }
        SettingsManager.initialize(new FileInputStream(Path.of(ASSETS_LOC, "conf/config.yaml").toFile()), new FileInputStream(dummyv.toFile()));
        // Parallelism
        if (parallelism > 0) {
            System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", String.valueOf(parallelism));
        }
        logger.info("Parallelism set to " + ForkJoinPool.commonPool().getParallelism());
        OctreeNode root = generateOctree();
        if (root != null) {
            // Save arguments and structure
            StringBuffer argStr = new StringBuffer();
            for (int i = 0; i < arguments.length; i++) {
                argStr.append(arguments[i]).append(" ");
            }
            try (PrintStream out = new PrintStream(new FileOutputStream(outFolder + "log"))) {
                out.print(argStr);
                out.println();
                out.println();
                for (MessageBean msg : NotificationsInterface.getHistorical()) {
                    out.println(msg.toString());
                }
            }
        }
    } catch (Exception e) {
        logger.error(e);
    }
}
Also used : Path(java.nio.file.Path) MessageBean(gaiasky.interafce.MessageBean) Lwjgl3Files(com.badlogic.gdx.backends.lwjgl3.Lwjgl3Files) OctreeNode(gaiasky.util.tree.OctreeNode) DesktopDateFormatFactory(gaiasky.desktop.format.DesktopDateFormatFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConsoleLogger(gaiasky.interafce.ConsoleLogger) DesktopNumberFormatFactory(gaiasky.desktop.format.DesktopNumberFormatFactory)

Example 5 with OctreeNode

use of gaiasky.util.tree.OctreeNode in project gaiasky by langurmonkey.

the class OctreeGeneratorMag method generateOctree.

@Override
public OctreeNode generateOctree(List<IParticleRecord> catalog) {
    root = IOctreeGenerator.startGeneration(catalog, params);
    // Holds all octree nodes indexed by id
    LongMap<OctreeNode> idMap = new LongMap<>();
    idMap.put(root.pageId, root);
    // Contains the list of objects for each node
    Map<OctreeNode, List<IParticleRecord>> objMap = new HashMap<>();
    int catalogSize = catalog.size();
    int catalogIndex = 0;
    for (int level = 0; level <= 19; level++) {
        logger.info("Generating level " + level + " (" + (catalog.size() - catalogIndex) + " stars left)");
        while (catalogIndex < catalogSize) {
            // Add stars to nodes until we reach max part
            IParticleRecord sb = catalog.get(catalogIndex++);
            double x = sb.x();
            double y = sb.y();
            double z = sb.z();
            Long nodeId = getPositionOctantId(x, y, z, level);
            if (!idMap.containsKey(nodeId)) {
                // Create octant and parents if necessary
                OctreeNode octant = createOctant(nodeId, x, y, z, level);
                // Add to idMap
                idMap.put(octant.pageId, octant);
            }
            // Add star to node
            OctreeNode octant = idMap.get(nodeId);
            int addedNum = addStarToNode(sb, octant, objMap);
            if (addedNum >= params.maxPart) {
                // On to next level!
                break;
            }
        }
        if (catalogIndex >= catalog.size()) {
            // All stars added -> FINISHED
            break;
        }
    }
    if (params.postprocess) {
        logger.info("Post-processing octree: childcount=" + params.childCount + ", parentcount=" + params.parentCount);
        long mergedNodes = 0;
        long mergedObjects = 0;
        // We merge low-count nodes (<= childcount) with parents, if parents' count is <= parentcount
        Object[] nodes = objMap.keySet().toArray();
        // Sort by descending depth
        Arrays.sort(nodes, (node1, node2) -> {
            OctreeNode n1 = (OctreeNode) node1;
            OctreeNode n2 = (OctreeNode) node2;
            return Integer.compare(n2.depth, n1.depth);
        });
        int n = objMap.size();
        for (int i = n - 1; i >= 0; i--) {
            OctreeNode current = (OctreeNode) nodes[i];
            if (current.numChildren() == 0 && current.parent != null && objMap.containsKey(current) && objMap.containsKey(current.parent)) {
                List<IParticleRecord> childArr = objMap.get(current);
                List<IParticleRecord> parentArr = objMap.get(current.parent);
                if (childArr.size() <= params.childCount && parentArr.size() <= params.parentCount) {
                    // Merge children nodes with parent nodes, remove children
                    parentArr.addAll(childArr);
                    objMap.remove(current);
                    current.remove();
                    mergedNodes++;
                    mergedObjects += childArr.size();
                }
            }
        }
        logger.info("POSTPROCESS STATS:");
        logger.info("    Merged nodes:    " + mergedNodes);
        logger.info("    Merged objects:  " + mergedObjects);
    }
    // Tree is ready, create star groups
    Set<OctreeNode> nodes = objMap.keySet();
    for (OctreeNode node : nodes) {
        List<IParticleRecord> list = objMap.get(node);
        StarGroup sg = new StarGroup();
        sg.setData(list, false);
        node.add(sg);
        sg.octant = node;
        sg.octantId = node.pageId;
    }
    root.updateCounts();
    return root;
}
Also used : LongMap(com.badlogic.gdx.utils.LongMap) OctreeNode(gaiasky.util.tree.OctreeNode) StarGroup(gaiasky.scenegraph.StarGroup) IParticleRecord(gaiasky.scenegraph.particle.IParticleRecord)

Aggregations

OctreeNode (gaiasky.util.tree.OctreeNode)15 IParticleRecord (gaiasky.scenegraph.particle.IParticleRecord)4 Array (com.badlogic.gdx.utils.Array)2 MetadataBinaryIO (gaiasky.data.octreegen.MetadataBinaryIO)2 OctreeWrapper (gaiasky.scenegraph.octreewrapper.OctreeWrapper)2 Vector3d (gaiasky.util.math.Vector3d)2 Lwjgl3Files (com.badlogic.gdx.backends.lwjgl3.Lwjgl3Files)1 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)1 Actor (com.badlogic.gdx.scenes.scene2d.Actor)1 LongMap (com.badlogic.gdx.utils.LongMap)1 IStarGroupDataProvider (gaiasky.data.group.IStarGroupDataProvider)1 STILDataProvider (gaiasky.data.group.STILDataProvider)1 IStarGroupIO (gaiasky.data.octreegen.IStarGroupIO)1 StarBrightnessComparator (gaiasky.data.octreegen.StarBrightnessComparator)1 StarGroupBinaryIO (gaiasky.data.octreegen.StarGroupBinaryIO)1 IOctreeGenerator (gaiasky.data.octreegen.generator.IOctreeGenerator)1 OctreeGeneratorMag (gaiasky.data.octreegen.generator.OctreeGeneratorMag)1 OctreeGeneratorParams (gaiasky.data.octreegen.generator.OctreeGeneratorParams)1 HipNames (gaiasky.data.util.HipNames)1 DesktopDateFormatFactory (gaiasky.desktop.format.DesktopDateFormatFactory)1