Search in sources :

Example 16 with PrimitivePickResults

use of com.ardor3d.intersection.PrimitivePickResults in project energy3d by concord-consortium.

the class SolarRadiation method computeOnRack.

// TODO: we probably should handle the radiation heat map visualization on the rack using a coarse grid and the energy calculation using a fine grid
private void computeOnRack(final int minute, final ReadOnlyVector3 directionTowardSun, final Rack rack) {
    if (rack.getTracker() != SolarPanel.NO_TRACKER) {
        final Calendar calendar = Heliodon.getInstance().getCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, (int) ((double) minute / (double) SolarRadiation.MINUTES_OF_DAY * 24.0));
        calendar.set(Calendar.MINUTE, minute % 60);
        rack.draw();
    }
    if (!rack.isMonolithic()) {
        return;
    }
    final ReadOnlyVector3 normal = rack.getNormal();
    if (normal == null) {
        throw new RuntimeException("Normal is null");
    }
    int nx = Scene.getInstance().getRackNx();
    int ny = Scene.getInstance().getRackNy();
    final Mesh drawMesh = rack.getRadiationMesh();
    final Mesh collisionMesh = (Mesh) rack.getRadiationCollisionSpatial();
    MeshDataStore data = onMesh.get(drawMesh);
    if (data == null) {
        data = initMeshTextureDataOnRectangle(drawMesh, nx, ny);
    }
    final ReadOnlyVector3 offset = directionTowardSun.multiply(1, null);
    final double dot = normal.dot(directionTowardSun);
    double directRadiation = 0;
    if (dot > 0) {
        directRadiation += calculateDirectRadiation(directionTowardSun, normal);
    }
    final double indirectRadiation = calculateDiffuseAndReflectedRadiation(directionTowardSun, normal);
    final FloatBuffer vertexBuffer = drawMesh.getMeshData().getVertexBuffer();
    // (0, 0)
    final Vector3 p0 = new Vector3(vertexBuffer.get(3), vertexBuffer.get(4), vertexBuffer.get(5));
    // (1, 0)
    final Vector3 p1 = new Vector3(vertexBuffer.get(6), vertexBuffer.get(7), vertexBuffer.get(8));
    // (0, 1)
    final Vector3 p2 = new Vector3(vertexBuffer.get(0), vertexBuffer.get(1), vertexBuffer.get(2));
    // this is the longer side (supposed to be y)
    final double d10 = p1.distance(p0);
    // this is the shorter side (supposed to be x)
    final double d20 = p2.distance(p0);
    final Vector3 p10 = p1.subtract(p0, null).normalizeLocal();
    final Vector3 p20 = p2.subtract(p0, null).normalizeLocal();
    // generate the heat map first. this doesn't affect the energy calculation, it just shows the distribution of solar radiation on the rack.
    // x and y must be swapped to have correct heat map texture, because nx represents rows and ny columns as we call initMeshTextureDataOnRectangle(mesh, nx, ny)
    double xSpacing = d10 / nx;
    double ySpacing = d20 / ny;
    Vector3 u = p10;
    Vector3 v = p20;
    final int iMinute = minute / Scene.getInstance().getTimeStep();
    for (int x = 0; x < nx; x++) {
        for (int y = 0; y < ny; y++) {
            if (EnergyPanel.getInstance().isCancelled()) {
                throw new CancellationException();
            }
            final Vector3 u2 = u.multiply(xSpacing * (x + 0.5), null);
            final Vector3 v2 = v.multiply(ySpacing * (y + 0.5), null);
            final ReadOnlyVector3 p = drawMesh.getWorldTransform().applyForward(p0.add(v2, null).addLocal(u2)).addLocal(offset);
            final Ray3 pickRay = new Ray3(p, directionTowardSun);
            // assuming that indirect (ambient or diffuse) radiation can always reach a grid point
            double radiation = indirectRadiation;
            if (dot > 0) {
                final PickResults pickResults = new PrimitivePickResults();
                for (final Spatial spatial : collidables) {
                    if (spatial != collisionMesh) {
                        PickingUtil.findPick(spatial, pickRay, pickResults, false);
                        if (pickResults.getNumber() != 0) {
                            break;
                        }
                    }
                }
                if (pickResults.getNumber() == 0) {
                    radiation += directRadiation;
                }
            }
            data.dailySolarIntensity[x][y] += radiation;
        }
    }
    // now do the calculation to get the total energy generated by the cells
    final double airTemperature = Weather.getInstance().getOutsideTemperatureAtMinute(dailyAirTemperatures[1], dailyAirTemperatures[0], minute);
    // system efficiency
    double syseff;
    // output at a cell center
    double output;
    // cell temperature
    double tcell;
    final SolarPanel panel = rack.getSolarPanel();
    if (Scene.getInstance().isRackModelExact()) {
        // exactly model each solar cell on each solar panel
        final int[] rc = rack.getSolarPanelRowAndColumnNumbers();
        // numbers of solar panels in x and y directions
        final int nxPanels = rc[0];
        final int nyPanels = rc[1];
        // numbers of solar cells on each panel in x and y directions
        int nxCells, nyCells;
        if (panel.isRotated()) {
            nxCells = panel.getNumberOfCellsInY();
            nyCells = panel.getNumberOfCellsInX();
        } else {
            nxCells = panel.getNumberOfCellsInX();
            nyCells = panel.getNumberOfCellsInY();
        }
        nx = nxCells * rc[0];
        ny = nyCells * rc[1];
        // get the area of a solar cell. 60 converts the unit of timeStep from minute to kWh
        final double a = panel.getPanelWidth() * panel.getPanelHeight() * Scene.getInstance().getTimeStep() / (panel.getNumberOfCellsInX() * panel.getNumberOfCellsInY() * 60.0);
        // swap the x and y back to correct order
        xSpacing = d20 / nx;
        ySpacing = d10 / ny;
        u = p20;
        v = p10;
        if (cellOutputs == null || cellOutputs.length != nx || cellOutputs[0].length != ny) {
            cellOutputs = new double[nx][ny];
        }
        // calculate the solar radiation first without worrying about the underlying cell wiring and distributed efficiency
        for (int x = 0; x < nx; x++) {
            for (int y = 0; y < ny; y++) {
                if (EnergyPanel.getInstance().isCancelled()) {
                    throw new CancellationException();
                }
                final Vector3 u2 = u.multiply(xSpacing * (x + 0.5), null);
                final Vector3 v2 = v.multiply(ySpacing * (y + 0.5), null);
                final ReadOnlyVector3 p = drawMesh.getWorldTransform().applyForward(p0.add(v2, null).addLocal(u2)).addLocal(offset);
                final Ray3 pickRay = new Ray3(p, directionTowardSun);
                // assuming that indirect (ambient or diffuse) radiation can always reach a grid point
                double radiation = indirectRadiation;
                if (dot > 0) {
                    final PickResults pickResults = new PrimitivePickResults();
                    for (final Spatial spatial : collidables) {
                        if (spatial != collisionMesh) {
                            PickingUtil.findPick(spatial, pickRay, pickResults, false);
                            if (pickResults.getNumber() != 0) {
                                break;
                            }
                        }
                    }
                    if (pickResults.getNumber() == 0) {
                        radiation += directRadiation;
                    }
                }
                cellOutputs[x][y] = radiation * a;
            }
        }
        // Tcell = Tair + (NOCT - 20) / 80 * R, where the unit of R is mW/cm^2
        final double noctFactor = (panel.getNominalOperatingCellTemperature() - 20.0) * 100.0 / (a * 80.0);
        // now consider cell wiring and distributed efficiency. TODO: This is very inaccurate. The output depends on both cell wiring and panel wiring.
        switch(// the ideal case that probably doesn't exist in reality
        panel.getShadeTolerance()) {
            case SolarPanel.HIGH_SHADE_TOLERANCE:
                for (int x = 0; x < nx; x++) {
                    for (int y = 0; y < ny; y++) {
                        output = cellOutputs[x][y];
                        tcell = airTemperature + output * noctFactor;
                        syseff = panel.getSystemEfficiency(tcell);
                        rack.getSolarPotential()[iMinute] += output * syseff;
                    }
                }
                break;
            case // assuming that all the cells on a panel are connected in series and all panels are connected in parallel
            SolarPanel.NO_SHADE_TOLERANCE:
                double min = Double.MAX_VALUE;
                for (int ix = 0; ix < nxPanels; ix++) {
                    // panel by panel
                    for (int iy = 0; iy < nyPanels; iy++) {
                        min = Double.MAX_VALUE;
                        for (int jx = 0; jx < nxCells; jx++) {
                            // cell by cell on each panel
                            for (int jy = 0; jy < nyCells; jy++) {
                                output = cellOutputs[ix * nxCells + jx][iy * nyCells + jy];
                                tcell = airTemperature + output * noctFactor;
                                syseff = panel.getSystemEfficiency(tcell);
                                output *= syseff;
                                if (output < min) {
                                    min = output;
                                }
                            }
                        }
                        rack.getSolarPotential()[iMinute] += min * nxCells * nyCells;
                    }
                }
                break;
            case // assuming each panel uses a diode bypass to connect two columns of cells
            SolarPanel.PARTIAL_SHADE_TOLERANCE:
                for (int ix = 0; ix < nxPanels; ix++) {
                    // panel by panel
                    for (int iy = 0; iy < nyPanels; iy++) {
                        min = Double.MAX_VALUE;
                        if (panel.isRotated()) {
                            // landscape: nxCells = 10, nyCells = 6
                            for (int jy = 0; jy < nyCells; jy++) {
                                // cell by cell on each panel
                                if (jy % 2 == 0) {
                                    // reset min every two columns of cells
                                    min = Double.MAX_VALUE;
                                }
                                for (int jx = 0; jx < nxCells; jx++) {
                                    output = cellOutputs[ix * nxCells + jx][iy * nyCells + jy];
                                    tcell = airTemperature + output * noctFactor;
                                    syseff = panel.getSystemEfficiency(tcell);
                                    output *= syseff;
                                    if (output < min) {
                                        min = output;
                                    }
                                }
                                if (jy % 2 == 1) {
                                    rack.getSolarPotential()[iMinute] += min * 2 * nxCells;
                                }
                            }
                        } else {
                            // portrait: nxCells = 6, nyCells = 10
                            for (int jx = 0; jx < nxCells; jx++) {
                                // cell by cell on each panel
                                if (jx % 2 == 0) {
                                    // reset min every two columns of cells
                                    min = Double.MAX_VALUE;
                                }
                                for (int jy = 0; jy < nyCells; jy++) {
                                    output = cellOutputs[ix * nxCells + jx][iy * nyCells + jy];
                                    tcell = airTemperature + output * noctFactor;
                                    syseff = panel.getSystemEfficiency(tcell);
                                    output *= syseff;
                                    if (output < min) {
                                        min = output;
                                    }
                                }
                                if (jx % 2 == 1) {
                                    rack.getSolarPotential()[iMinute] += min * 2 * nyCells;
                                }
                            }
                        }
                    }
                }
                break;
        }
    } else {
        // for simulation speed, approximate rack model doesn't compute panel by panel and cell by cell
        ySpacing = xSpacing = Scene.getInstance().getRackCellSize() / Scene.getInstance().getAnnotationScale();
        // swap the x and y back to correct order
        nx = Math.max(2, (int) (d20 / xSpacing));
        ny = Math.max(2, (int) (d10 / ySpacing));
        // nx*ny*60: dividing the total rack area by nx*ny gets the unit cell area of the nx*ny grid; 60 converts the unit of timeStep from minute to kWh
        final double a = rack.getRackWidth() * rack.getRackHeight() * Scene.getInstance().getTimeStep() / (nx * ny * 60.0);
        u = p20;
        v = p10;
        if (cellOutputs == null || cellOutputs.length != nx || cellOutputs[0].length != ny) {
            cellOutputs = new double[nx][ny];
        }
        // calculate the solar radiation first without worrying about the underlying cell wiring and distributed efficiency
        for (int x = 0; x < nx; x++) {
            for (int y = 0; y < ny; y++) {
                if (EnergyPanel.getInstance().isCancelled()) {
                    throw new CancellationException();
                }
                final Vector3 u2 = u.multiply(xSpacing * (x + 0.5), null);
                final Vector3 v2 = v.multiply(ySpacing * (y + 0.5), null);
                final ReadOnlyVector3 p = drawMesh.getWorldTransform().applyForward(p0.add(v2, null).addLocal(u2)).addLocal(offset);
                final Ray3 pickRay = new Ray3(p, directionTowardSun);
                // assuming that indirect (ambient or diffuse) radiation can always reach a grid point
                double radiation = indirectRadiation;
                if (dot > 0) {
                    final PickResults pickResults = new PrimitivePickResults();
                    for (final Spatial spatial : collidables) {
                        if (spatial != collisionMesh) {
                            PickingUtil.findPick(spatial, pickRay, pickResults, false);
                            if (pickResults.getNumber() != 0) {
                                break;
                            }
                        }
                    }
                    if (pickResults.getNumber() == 0) {
                        radiation += directRadiation;
                    }
                }
                cellOutputs[x][y] = radiation * a;
            }
        }
        // Tcell = Tair + (NOCT - 20) / 80 * R, where the unit of R is mW/cm^2
        final double noctFactor = (panel.getNominalOperatingCellTemperature() - 20.0) * 100.0 / (a * 80.0);
        // now consider cell wiring and distributed efficiency. TODO: This is very inaccurate. The output depends on both cell wiring and panel wiring.
        switch(panel.getShadeTolerance()) {
            case // the ideal case that probably doesn't exist in reality
            SolarPanel.HIGH_SHADE_TOLERANCE:
                for (int x = 0; x < nx; x++) {
                    for (int y = 0; y < ny; y++) {
                        output = cellOutputs[x][y];
                        tcell = airTemperature + output * noctFactor;
                        syseff = panel.getSystemEfficiency(tcell);
                        rack.getSolarPotential()[iMinute] += output * syseff;
                    }
                }
                break;
            case SolarPanel.NO_SHADE_TOLERANCE:
                double min = Double.MAX_VALUE;
                for (int x = 0; x < nx; x++) {
                    for (int y = 0; y < ny; y++) {
                        output = cellOutputs[x][y];
                        tcell = airTemperature + output * noctFactor;
                        syseff = panel.getSystemEfficiency(tcell);
                        output *= syseff;
                        if (output < min) {
                            min = output;
                        }
                    }
                }
                rack.getSolarPotential()[iMinute] += min * ny * nx;
                break;
            case SolarPanel.PARTIAL_SHADE_TOLERANCE:
                for (int x = 0; x < nx; x++) {
                    min = Double.MAX_VALUE;
                    for (int y = 0; y < ny; y++) {
                        output = cellOutputs[x][y];
                        tcell = airTemperature + output * noctFactor;
                        syseff = panel.getSystemEfficiency(tcell);
                        output *= syseff;
                        if (output < min) {
                            min = output;
                        }
                    }
                    rack.getSolarPotential()[iMinute] += min * ny;
                }
                break;
        }
    }
}
Also used : Calendar(java.util.Calendar) Mesh(com.ardor3d.scenegraph.Mesh) FloatBuffer(java.nio.FloatBuffer) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) CullHint(com.ardor3d.scenegraph.hint.CullHint) TPoint(org.poly2tri.triangulation.point.TPoint) Point(org.poly2tri.geometry.primitives.Point) Ray3(com.ardor3d.math.Ray3) PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) CancellationException(java.util.concurrent.CancellationException) Spatial(com.ardor3d.scenegraph.Spatial) SolarPanel(org.concord.energy3d.model.SolarPanel) PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) PickResults(com.ardor3d.intersection.PickResults)

Example 17 with PrimitivePickResults

use of com.ardor3d.intersection.PrimitivePickResults in project energy3d by concord-consortium.

the class Wall method findRoofIntersection.

public ReadOnlyVector3 findRoofIntersection(final ReadOnlyVector3 p, final ReadOnlyVector3 direction, final double offset) {
    if (roof == null) {
        return p;
    }
    final Vector3 origin = new Vector3(p.getX(), p.getY(), direction.equals(Vector3.UNIT_Z) ? 0 : p.getZ());
    final PickResults pickResults = new PrimitivePickResults();
    PickingUtil.findPick(roof.getRoofPartsRoot(), new Ray3(origin, direction), pickResults, false);
    if (pickResults.getNumber() > 0) {
        return pickResults.getPickData(0).getIntersectionRecord().getIntersectionPoint(0).add(direction.multiply(roof.getOverhangLength() > 0.05 ? offset : 0, null), null);
    } else {
        return p;
    }
}
Also used : PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) PickResults(com.ardor3d.intersection.PickResults) Ray3(com.ardor3d.math.Ray3)

Example 18 with PrimitivePickResults

use of com.ardor3d.intersection.PrimitivePickResults in project energy3d by concord-consortium.

the class HousePart method computeNormalAndKeepOnSurface.

protected ReadOnlyVector3 computeNormalAndKeepOnSurface() {
    if (container == null) {
        return null;
    }
    if (container instanceof Rack) {
        final Rack rack = (Rack) container;
        final PickResults pickResults = new PrimitivePickResults();
        final Ray3 ray = new Ray3(getAbsPoint(0).multiplyLocal(1, 1, 0), Vector3.UNIT_Z);
        PickingUtil.findPick(container.getCollisionSpatial(), ray, pickResults, false);
        if (pickResults.getNumber() != 0) {
            final PickData pickData = pickResults.getPickData(0);
            final Vector3 p = pickData.getIntersectionRecord().getIntersectionPoint(0);
            points.get(0).setZ(p.getZ());
        } else {
            if (rack.getBaseHeight() < Math.abs(0.5 * rack.getRackHeight() / Scene.getInstance().getAnnotationScale() * Math.sin(Math.toRadians(rack.getTiltAngle())))) {
                final Ray3 ray2 = new Ray3(getAbsPoint(0).multiplyLocal(1, 1, 0), Vector3.NEG_UNIT_Z);
                PickingUtil.findPick(container.getCollisionSpatial(), ray2, pickResults, false);
                if (pickResults.getNumber() != 0) {
                    final PickData pickData = pickResults.getPickData(0);
                    final Vector3 p = pickData.getIntersectionRecord().getIntersectionPoint(0);
                    points.get(0).setZ(p.getZ());
                }
            }
        }
        return rack.getNormal();
    } else if (container instanceof Roof) {
        final Roof roof = (Roof) container;
        final int[] editPointToRoofIndex = new int[points.size()];
        final PickResults pickResults = new PrimitivePickResults();
        for (int i = 0; i < points.size(); i++) {
            pickResults.clear();
            final Ray3 ray = new Ray3(getAbsPoint(i).multiplyLocal(1, 1, 0), Vector3.UNIT_Z);
            for (final Spatial roofPart : roof.getRoofPartsRoot().getChildren()) {
                if (roofPart.getSceneHints().getCullHint() != CullHint.Always) {
                    PickingUtil.findPick(((Node) roofPart).getChild(0), ray, pickResults, false);
                    if (pickResults.getNumber() != 0) {
                        break;
                    }
                }
            }
            if (pickResults.getNumber() != 0) {
                final PickData pickData = pickResults.getPickData(0);
                final Vector3 p = pickData.getIntersectionRecord().getIntersectionPoint(0);
                points.get(i).setZ(p.getZ());
                final UserData userData = (UserData) ((Spatial) pickData.getTarget()).getUserData();
                final int roofPartIndex = userData.getEditPointIndex();
                editPointToRoofIndex[i] = roofPartIndex;
            }
            // find roofPart with most edit points on it
            containerRoofIndex = editPointToRoofIndex[0];
            if (points.size() > 1) {
                containerRoofIndex = 0;
                final Map<Integer, Integer> counts = new HashMap<Integer, Integer>(points.size());
                for (final int roofIndex : editPointToRoofIndex) {
                    counts.put(roofIndex, counts.get(roofIndex) == null ? 1 : counts.get(roofIndex) + 1);
                }
                int highestCount = 0;
                for (final int roofIndex : editPointToRoofIndex) {
                    if (counts.get(roofIndex) > highestCount) {
                        highestCount = counts.get(roofIndex);
                        containerRoofIndex = roofIndex;
                    }
                }
            }
        }
        return (ReadOnlyVector3) roof.getRoofPartsRoot().getChild(containerRoofIndex).getUserData();
    } else if (container instanceof Foundation) {
        final Foundation foundation = (Foundation) container;
        final List<Node> nodes = foundation.getImportedNodes();
        if (nodes != null) {
            final Map<Vector3, ReadOnlyVector3> intersections = new HashMap<Vector3, ReadOnlyVector3>();
            final PickResults pickResults = new PrimitivePickResults();
            for (final Node n : nodes) {
                for (final Spatial s : n.getChildren()) {
                    if (s instanceof Mesh) {
                        final Mesh m = (Mesh) s;
                        pickResults.clear();
                        PickingUtil.findPick(m, new Ray3(getAbsPoint(0).multiplyLocal(1, 1, 0), Vector3.UNIT_Z), pickResults, false);
                        if (pickResults.getNumber() > 0) {
                            intersections.put(pickResults.getPickData(0).getIntersectionRecord().getIntersectionPoint(0), ((UserData) m.getUserData()).getNormal());
                        }
                    }
                }
            }
            if (!intersections.isEmpty()) {
                double zmax = -Double.MAX_VALUE;
                ReadOnlyVector3 normal = null;
                for (final Vector3 v : intersections.keySet()) {
                    if (v.getZ() > zmax) {
                        zmax = v.getZ();
                        normal = intersections.get(v);
                    }
                }
                if (normal != null) {
                    pickedNormal = normal;
                    return normal;
                }
            }
        }
    }
    return container.getNormal();
}
Also used : HashMap(java.util.HashMap) Node(com.ardor3d.scenegraph.Node) Mesh(com.ardor3d.scenegraph.Mesh) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) PickData(com.ardor3d.intersection.PickData) Ray3(com.ardor3d.math.Ray3) PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Spatial(com.ardor3d.scenegraph.Spatial) PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) PickResults(com.ardor3d.intersection.PickResults) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

PickResults (com.ardor3d.intersection.PickResults)18 PrimitivePickResults (com.ardor3d.intersection.PrimitivePickResults)18 Ray3 (com.ardor3d.math.Ray3)18 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)16 Spatial (com.ardor3d.scenegraph.Spatial)15 Vector3 (com.ardor3d.math.Vector3)13 CullHint (com.ardor3d.scenegraph.hint.CullHint)11 Point (org.poly2tri.geometry.primitives.Point)11 TPoint (org.poly2tri.triangulation.point.TPoint)11 Mesh (com.ardor3d.scenegraph.Mesh)10 CancellationException (java.util.concurrent.CancellationException)10 FloatBuffer (java.nio.FloatBuffer)8 Calendar (java.util.Calendar)6 Node (com.ardor3d.scenegraph.Node)5 Foundation (org.concord.energy3d.model.Foundation)4 HousePart (org.concord.energy3d.model.HousePart)4 Vector2 (com.ardor3d.math.Vector2)3 ReadOnlyVector2 (com.ardor3d.math.type.ReadOnlyVector2)3 Roof (org.concord.energy3d.model.Roof)3 Window (org.concord.energy3d.model.Window)3