Search in sources :

Example 1 with MeshData

use of com.ardor3d.scenegraph.MeshData in project energy3d by concord-consortium.

the class TriangleMeshLib method convertNode.

// remove this in the future
static Node convertNode(final Foundation foundation, final Node oldNode) {
    final Node newNode = new Node();
    final List<Mesh> oldMeshes = new ArrayList<Mesh>();
    Util.getMeshes(oldNode, oldMeshes);
    // 0.633 is determined by fitting the length in Energy3D to the length in SketchUp
    final double scale = Scene.getInstance().getAnnotationScale() * 0.633;
    for (final Mesh m : oldMeshes) {
        // an imported mesh doesn't necessarily have the same normal vector (e.g., a cube could be a whole mesh in collada)
        m.setUserData(new UserData(foundation));
        final MeshData md = m.getMeshData();
        switch(md.getIndexMode(0)) {
            case Triangles:
                final FloatBuffer vertexBuffer = md.getVertexBuffer();
                final FloatBuffer normalBuffer = md.getNormalBuffer();
                final FloatBuffer colorBuffer = md.getColorBuffer();
                final int n = (int) Math.round(vertexBuffer.limit() / 9.0);
                for (int i = 0; i < n; i++) {
                    final Mesh mesh = new Mesh("Triangle");
                    final FloatBuffer vb = BufferUtils.createFloatBuffer(9);
                    final int j = i * 9;
                    vb.put(vertexBuffer.get(j)).put(vertexBuffer.get(j + 1)).put(vertexBuffer.get(j + 2));
                    vb.put(vertexBuffer.get(j + 3)).put(vertexBuffer.get(j + 4)).put(vertexBuffer.get(j + 5));
                    vb.put(vertexBuffer.get(j + 6)).put(vertexBuffer.get(j + 7)).put(vertexBuffer.get(j + 8));
                    mesh.getMeshData().setVertexBuffer(vb);
                    final UserData userData = new UserData(foundation);
                    if (normalBuffer != null) {
                        final FloatBuffer nb = BufferUtils.createFloatBuffer(9);
                        nb.put(normalBuffer.get(j)).put(normalBuffer.get(j + 1)).put(normalBuffer.get(j + 2));
                        nb.put(normalBuffer.get(j + 3)).put(normalBuffer.get(j + 4)).put(normalBuffer.get(j + 5));
                        nb.put(normalBuffer.get(j + 6)).put(normalBuffer.get(j + 7)).put(normalBuffer.get(j + 8));
                        mesh.getMeshData().setNormalBuffer(nb);
                        userData.setNormal(new Vector3(nb.get(0), nb.get(1), nb.get(2)));
                    }
                    mesh.setUserData(userData);
                    if (colorBuffer != null) {
                        final FloatBuffer cb = BufferUtils.createFloatBuffer(9);
                        cb.put(colorBuffer.get(j)).put(colorBuffer.get(j + 1)).put(colorBuffer.get(j + 2));
                        cb.put(colorBuffer.get(j + 3)).put(colorBuffer.get(j + 4)).put(colorBuffer.get(j + 5));
                        cb.put(colorBuffer.get(j + 6)).put(colorBuffer.get(j + 7)).put(colorBuffer.get(j + 8));
                        mesh.getMeshData().setColorBuffer(cb);
                    }
                    mesh.getMeshData().setTextureBuffer(BufferUtils.createVector2Buffer(3), 0);
                    mesh.setRenderState(HousePart.offsetState);
                    mesh.getMeshData().setIndexMode(md.getIndexMode(0));
                    mesh.setScale(scale);
                    newNode.attachChild(mesh);
                }
                break;
            case Lines:
                break;
            default:
                System.out.println("*******" + md.getIndexMode(0));
                break;
        }
    }
    return newNode;
}
Also used : MeshData(com.ardor3d.scenegraph.MeshData) UserData(org.concord.energy3d.model.UserData) Node(com.ardor3d.scenegraph.Node) ArrayList(java.util.ArrayList) Mesh(com.ardor3d.scenegraph.Mesh) FloatBuffer(java.nio.FloatBuffer) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3)

Example 2 with MeshData

use of com.ardor3d.scenegraph.MeshData in project energy3d by concord-consortium.

the class Foundation method importCollada.

public Node importCollada(final URL file, final Vector3 position) throws Exception {
    if (importedNodes == null) {
        importedNodes = new ArrayList<Node>();
    }
    if (importedNodeStates == null) {
        importedNodeStates = new ArrayList<NodeState>();
    }
    if (position != null) {
        // when position is null, the cursor is already set to be wait by the loading method
        SceneManager.getInstance().cursorWait(true);
    }
    File sourceFile = new File(file.toURI());
    if (!sourceFile.exists() && Scene.getURL() != null) {
        sourceFile = new File(new File(Scene.getURL().toURI()).getParentFile(), Util.getFileName(file.getPath()).replaceAll("%20", " "));
    }
    if (sourceFile.exists()) {
        final double az = Math.toRadians(getAzimuth());
        final boolean zeroAz = Util.isZero(az);
        // 0.633 is determined by fitting the length in Energy3D to the length in SketchUp
        final double scale = Scene.getInstance().getAnnotationScale() * 0.633;
        final ColladaStorage storage = new ColladaImporter().load(new URLResourceSource(sourceFile.toURI().toURL()));
        final Node originalNode = storage.getScene();
        originalNode.setScale(scale);
        if (position != null) {
            // when position is null, the node uses the position saved in the associated NodeState object
            final NodeState ns = new NodeState();
            ns.setSourceURL(file);
            ns.setAbsolutePosition(position.clone());
            importedNodeStates.add(ns);
            originalNode.setTranslation(position);
            final Vector3 relativePosition = position.subtract(getAbsCenter().multiplyLocal(1, 1, 0), null).addLocal(0, 0, height);
            // why not -getAzimuth()?
            ns.setRelativePosition(zeroAz ? relativePosition : new Matrix3().fromAngles(0, 0, az).applyPost(relativePosition, null));
        }
        // now construct a new node that is a parent of all planar meshes
        final Node newNode = new Node(originalNode.getName());
        final String nodeString = "Node #" + importedNodes.size() + ", Foundation #" + id;
        final List<Mesh> meshes = new ArrayList<Mesh>();
        Util.getMeshes(originalNode, meshes);
        String warnInfo = null;
        final int nodeIndex = importedNodes.size();
        int meshIndex = 0;
        for (final Mesh m : meshes) {
            final ReadOnlyTransform t = m.getWorldTransform();
            final MeshData md = m.getMeshData();
            switch(md.getIndexMode(0)) {
                case Triangles:
                    final List<Mesh> children = TriangleMeshLib.getPlanarMeshes(m);
                    if (!children.isEmpty()) {
                        for (final Mesh s : children) {
                            s.setTransform(t);
                            final UserData ud = new UserData(this, nodeIndex, meshIndex);
                            ud.setNormal((Vector3) s.getUserData());
                            ud.setRenderState(s.getLocalRenderState(StateType.Texture));
                            ud.setTextureBuffer(s.getMeshData().getTextureBuffer(0));
                            s.setUserData(ud);
                            s.setName("Mesh #" + meshIndex + ", " + nodeString);
                            newNode.attachChild(s);
                            meshIndex++;
                        }
                    }
                    break;
                case Lines:
                    break;
                default:
                    warnInfo = md.getIndexMode(0).name();
                    break;
            }
        }
        if (warnInfo != null) {
            JOptionPane.showMessageDialog(MainFrame.getInstance(), "Non-triangular mesh " + warnInfo + " is found.", "Warning", JOptionPane.WARNING_MESSAGE);
        }
        if (newNode.getNumberOfChildren() > 0) {
            importedNodes.add(newNode);
            newNode.setScale(scale);
            newNode.updateWorldTransform(true);
            root.attachChild(newNode);
            createMeshThickness(newNode);
            if (!zeroAz) {
                setRotatedNormalsForImportedMeshes();
            }
            return newNode;
        }
        if (position != null) {
            SceneManager.getInstance().cursorWait(false);
        }
    } else {
        if (position != null) {
            // get rid of the dead nodes no longer linked to files
            for (final Iterator<NodeState> it = importedNodeStates.iterator(); it.hasNext(); ) {
                if (file.equals(it.next().getSourceURL())) {
                    it.remove();
                }
            }
        }
    }
    return null;
}
Also used : Node(com.ardor3d.scenegraph.Node) ColladaImporter(com.ardor3d.extension.model.collada.jdom.ColladaImporter) URLResourceSource(com.ardor3d.util.resource.URLResourceSource) ArrayList(java.util.ArrayList) ReadOnlyTransform(com.ardor3d.math.type.ReadOnlyTransform) Mesh(com.ardor3d.scenegraph.Mesh) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) CullHint(com.ardor3d.scenegraph.hint.CullHint) Matrix3(com.ardor3d.math.Matrix3) MeshData(com.ardor3d.scenegraph.MeshData) ColladaStorage(com.ardor3d.extension.model.collada.jdom.data.ColladaStorage) File(java.io.File)

Example 3 with MeshData

use of com.ardor3d.scenegraph.MeshData in project energy3d by concord-consortium.

the class Foundation method init.

@Override
protected void init() {
    super.init();
    resizeHouseMode = false;
    if (Util.isZero(uValue)) {
        uValue = 0.19;
    }
    if (Util.isZero(volumetricHeatCapacity)) {
        volumetricHeatCapacity = 0.5;
    }
    if (Util.isZero(solarReceiverEfficiency)) {
        solarReceiverEfficiency = 0.2;
    }
    if (Util.isZero(childGridSize)) {
        childGridSize = 2.5;
    }
    if (thermostat == null) {
        thermostat = new Thermostat();
    }
    mesh = new Mesh("Foundation");
    mesh.getMeshData().setVertexBuffer(BufferUtils.createVector3Buffer(6));
    mesh.getMeshData().setNormalBuffer(BufferUtils.createVector3Buffer(6));
    mesh.getMeshData().setTextureBuffer(BufferUtils.createVector2Buffer(6), 0);
    mesh.setRenderState(offsetState);
    mesh.setModelBound(new BoundingBox());
    root.attachChild(mesh);
    if (foundationPolygon == null) {
        foundationPolygon = new FoundationPolygon(this);
    } else {
        foundationPolygon.draw();
    }
    root.attachChild(foundationPolygon.getRoot());
    sideMesh = new Mesh[4];
    for (int i = 0; i < 4; i++) {
        final Mesh mesh_i = new Mesh("Foundation (Side " + i + ")");
        mesh_i.setUserData(new UserData(this));
        mesh_i.setRenderState(offsetState);
        mesh_i.setModelBound(new BoundingBox());
        final MeshData meshData = mesh_i.getMeshData();
        meshData.setVertexBuffer(BufferUtils.createVector3Buffer(6));
        meshData.setNormalBuffer(BufferUtils.createVector3Buffer(6));
        mesh_i.getMeshData().setTextureBuffer(BufferUtils.createVector2Buffer(6), 0);
        root.attachChild(mesh_i);
        sideMesh[i] = mesh_i;
    }
    boundingMesh = new Line("Foundation (Bounding)");
    boundingMesh.getMeshData().setVertexBuffer(BufferUtils.createVector3Buffer(24));
    boundingMesh.setModelBound(new BoundingBox());
    Util.disablePickShadowLight(boundingMesh);
    boundingMesh.getSceneHints().setCullHint(CullHint.Always);
    root.attachChild(boundingMesh);
    outlineMesh = new Line("Foundation (Outline)");
    outlineMesh.getMeshData().setVertexBuffer(BufferUtils.createVector3Buffer(24));
    outlineMesh.setDefaultColor(ColorRGBA.BLACK);
    outlineMesh.setModelBound(new BoundingBox());
    Util.disablePickShadowLight(outlineMesh);
    root.attachChild(outlineMesh);
    linePatternMesh = new Line("Line Pattern");
    linePatternMesh.getMeshData().setVertexBuffer(BufferUtils.createVector3Buffer(2));
    linePatternMesh.setDefaultColor(new ColorRGBA(0, 0, 0, 0.75f));
    linePatternMesh.setModelBound(null);
    final BlendState blendState = new BlendState();
    blendState.setBlendEnabled(true);
    linePatternMesh.setRenderState(blendState);
    linePatternMesh.getSceneHints().setRenderBucketType(RenderBucketType.Transparent);
    Util.disablePickShadowLight(linePatternMesh);
    root.attachChild(linePatternMesh);
    setLinePatternVisible(false);
    final UserData userData = new UserData(this);
    mesh.setUserData(userData);
    boundingMesh.setUserData(userData);
    setLabelOffset(-0.11);
    label = new BMText("Floating Label", "Undefined", FontManager.getInstance().getPartNumberFont(), Align.Center, Justify.Center);
    Util.initHousePartLabel(label);
    label.setFontScale(0.5);
    label.setVisible(false);
    root.attachChild(label);
    azimuthArrow = new Line("Azimuth Arrow");
    azimuthArrow.setLineWidth(2);
    azimuthArrow.setModelBound(null);
    Util.disablePickShadowLight(azimuthArrow);
    azimuthArrow.getMeshData().setVertexBuffer(BufferUtils.createVector3Buffer(6));
    azimuthArrow.setDefaultColor(ColorRGBA.WHITE);
    root.attachChild(azimuthArrow);
    solarReceiver = new Cylinder("Solar Receiver", 10, 10, 10, 0, true);
    solarReceiver.setDefaultColor(ColorRGBA.WHITE);
    solarReceiver.setRenderState(offsetState);
    solarReceiver.setModelBound(new BoundingBox());
    solarReceiver.setVisible(false);
    root.attachChild(solarReceiver);
    selectedMeshOutline = new Line("Outline of Selected Mesh");
    selectedMeshOutline.setLineWidth(2f);
    selectedMeshOutline.setStipplePattern((short) 0xf0f0);
    selectedMeshOutline.setModelBound(null);
    Util.disablePickShadowLight(selectedMeshOutline);
    selectedMeshOutline.getMeshData().setVertexBuffer(BufferUtils.createVector3Buffer(1));
    selectedMeshOutline.setDefaultColor(new ColorRGBA(0f, 0f, 0f, 1f));
    root.attachChild(selectedMeshOutline);
    selectedNodeBoundingBox = new Line("Bounding Box of Selected Mesh");
    selectedNodeBoundingBox.setLineWidth(0.01f);
    selectedNodeBoundingBox.setStipplePattern((short) 0xf0f0);
    selectedNodeBoundingBox.setModelBound(null);
    Util.disablePickShadowLight(selectedNodeBoundingBox);
    selectedNodeBoundingBox.getMeshData().setVertexBuffer(BufferUtils.createVector3Buffer(24));
    selectedNodeBoundingBox.setDefaultColor(new ColorRGBA(1f, 1f, 0f, 1f));
    root.attachChild(selectedNodeBoundingBox);
    updateTextureAndColor();
    if (points.size() == 8) {
        for (int i = 0; i < 4; i++) {
            points.add(new Vector3());
        }
    }
    if (importedNodeStates != null) {
        try {
            for (final Iterator<NodeState> it = importedNodeStates.iterator(); it.hasNext(); ) {
                final NodeState ns = it.next();
                final Node n = importCollada(ns.getSourceURL(), null);
                if (n == null) {
                    it.remove();
                    EventQueue.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                JOptionPane.showMessageDialog(MainFrame.getInstance(), Paths.get(ns.getSourceURL().toURI()).toFile() + " was not found!", "File problem", JOptionPane.ERROR_MESSAGE);
                            } catch (final HeadlessException e) {
                                e.printStackTrace();
                            } catch (final URISyntaxException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } else {
                    final ArrayList<Integer> reversedFaceMeshes = ns.getMeshesWithReversedNormal();
                    if (reversedFaceMeshes != null) {
                        for (final Integer i : reversedFaceMeshes) {
                            Util.reverseFace(Util.getMesh(n, i));
                        }
                    }
                    final ArrayList<Integer> deletedMeshes = ns.getDeletedMeshes();
                    if (deletedMeshes != null && !deletedMeshes.isEmpty()) {
                        final List<Mesh> toDelete = new ArrayList<Mesh>();
                        for (final Integer i : deletedMeshes) {
                            toDelete.add(Util.getMesh(n, i));
                        }
                        for (final Mesh m : toDelete) {
                            n.detachChild(m);
                        }
                    }
                    final HashMap<Integer, ReadOnlyColorRGBA> meshColors = ns.getMeshColors();
                    if (meshColors != null) {
                        for (final Integer i : meshColors.keySet()) {
                            Util.getMesh(n, i).setDefaultColor(meshColors.get(i));
                        }
                    }
                }
            }
        } catch (final Throwable t) {
            BugReporter.report(t);
        }
        setRotatedNormalsForImportedMeshes();
    }
}
Also used : ReadOnlyColorRGBA(com.ardor3d.math.type.ReadOnlyColorRGBA) HeadlessException(java.awt.HeadlessException) Node(com.ardor3d.scenegraph.Node) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) BoundingBox(com.ardor3d.bounding.BoundingBox) OrientedBoundingBox(com.ardor3d.bounding.OrientedBoundingBox) BMText(com.ardor3d.ui.text.BMText) BlendState(com.ardor3d.renderer.state.BlendState) Thermostat(org.concord.energy3d.simulation.Thermostat) Mesh(com.ardor3d.scenegraph.Mesh) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) CullHint(com.ardor3d.scenegraph.hint.CullHint) MeshData(com.ardor3d.scenegraph.MeshData) Line(com.ardor3d.scenegraph.Line) Cylinder(com.ardor3d.scenegraph.shape.Cylinder) ReadOnlyColorRGBA(com.ardor3d.math.type.ReadOnlyColorRGBA) ColorRGBA(com.ardor3d.math.ColorRGBA)

Aggregations

Vector3 (com.ardor3d.math.Vector3)3 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)3 Mesh (com.ardor3d.scenegraph.Mesh)3 MeshData (com.ardor3d.scenegraph.MeshData)3 Node (com.ardor3d.scenegraph.Node)3 ArrayList (java.util.ArrayList)3 CullHint (com.ardor3d.scenegraph.hint.CullHint)2 BoundingBox (com.ardor3d.bounding.BoundingBox)1 OrientedBoundingBox (com.ardor3d.bounding.OrientedBoundingBox)1 ColladaImporter (com.ardor3d.extension.model.collada.jdom.ColladaImporter)1 ColladaStorage (com.ardor3d.extension.model.collada.jdom.data.ColladaStorage)1 ColorRGBA (com.ardor3d.math.ColorRGBA)1 Matrix3 (com.ardor3d.math.Matrix3)1 ReadOnlyColorRGBA (com.ardor3d.math.type.ReadOnlyColorRGBA)1 ReadOnlyTransform (com.ardor3d.math.type.ReadOnlyTransform)1 BlendState (com.ardor3d.renderer.state.BlendState)1 Line (com.ardor3d.scenegraph.Line)1 Cylinder (com.ardor3d.scenegraph.shape.Cylinder)1 BMText (com.ardor3d.ui.text.BMText)1 URLResourceSource (com.ardor3d.util.resource.URLResourceSource)1