Search in sources :

Example 1 with GeometryData

use of org.bimserver.models.geometry.GeometryData in project BIMserver by opensourceBIM.

the class GeometryGenerator method returnCachedData.

private void returnCachedData(IfcModelInterface model, GeometryCache geometryCache, DatabaseSession databaseSession, int pid, int rid) throws BimserverDatabaseException {
    EClass productClass = model.getPackageMetaData().getEClass("IfcProduct");
    List<IdEObject> products = model.getAllWithSubTypes(productClass);
    for (IdEObject ifcProduct : products) {
        GeometryCacheEntry geometryCacheEntry = geometryCache.get(ifcProduct.getExpressId());
        if (geometryCacheEntry != null) {
            GeometryData geometryData = databaseSession.create(GeometryPackage.eINSTANCE.getGeometryData(), pid, rid);
            geometryData.setVertices(geometryCacheEntry.getVertices().array());
            geometryData.setNormals(geometryCacheEntry.getNormals().array());
            GeometryInfo geometryInfo = databaseSession.create(GeometryPackage.eINSTANCE.getGeometryInfo(), pid, rid);
            Vector3f min = databaseSession.create(GeometryPackage.eINSTANCE.getVector3f(), pid, rid);
            min.setX(geometryCacheEntry.getGeometryInfo().getMinBounds().getX());
            min.setY(geometryCacheEntry.getGeometryInfo().getMinBounds().getY());
            min.setZ(geometryCacheEntry.getGeometryInfo().getMinBounds().getZ());
            Vector3f max = databaseSession.create(GeometryPackage.eINSTANCE.getVector3f(), pid, rid);
            max.setX(geometryCacheEntry.getGeometryInfo().getMaxBounds().getX());
            max.setY(geometryCacheEntry.getGeometryInfo().getMaxBounds().getY());
            max.setZ(geometryCacheEntry.getGeometryInfo().getMaxBounds().getZ());
            geometryInfo.setMinBounds(min);
            geometryInfo.setMaxBounds(max);
            geometryInfo.setData(geometryData);
            ifcProduct.eSet(ifcProduct.eClass().getEStructuralFeature("geometry"), geometryInfo);
        }
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) IdEObject(org.bimserver.emf.IdEObject) Vector3f(org.bimserver.models.geometry.Vector3f) GeometryInfo(org.bimserver.models.geometry.GeometryInfo) GeometryData(org.bimserver.models.geometry.GeometryData)

Example 2 with GeometryData

use of org.bimserver.models.geometry.GeometryData in project BIMserver by opensourceBIM.

the class ClientIfcModel method processGeometryInputStream.

private void processGeometryInputStream(InputStream inputStream, Map<Long, Long> geometryInfoOidToOid) throws IOException, GeometryException, IfcModelInterfaceException {
    try (LittleEndianDataInputStream dataInputStream = new LittleEndianDataInputStream(inputStream)) {
        boolean done = false;
        while (!done) {
            byte type = dataInputStream.readByte();
            if (type == 0) {
                String protocol = dataInputStream.readUTF();
                if (!protocol.equals("BGS")) {
                    throw new GeometryException("Protocol != BGS (" + protocol + ")");
                }
                byte formatVersion = dataInputStream.readByte();
                if (formatVersion != 11) {
                    throw new GeometryException("Unsupported version " + formatVersion + " / 11");
                }
                int skip = 4 - (7 % 4);
                if (skip != 0 && skip != 4) {
                    dataInputStream.readFully(new byte[skip]);
                }
                for (int i = 0; i < 6; i++) {
                    dataInputStream.readDouble();
                }
            } else if (type == 5) {
                dataInputStream.readFully(new byte[7]);
                // roid
                dataInputStream.readLong();
                long geometryInfoOid = dataInputStream.readLong();
                GeometryInfo geometryInfo = (GeometryInfo) get(geometryInfoOid);
                if (geometryInfo == null) {
                    geometryInfo = create(GeometryInfo.class);
                }
                ((IdEObjectImpl) geometryInfo).setOid(geometryInfoOid);
                ((IdEObjectImpl) geometryInfo).setLoadingState(State.LOADING);
                add(geometryInfoOid, geometryInfo);
                Long ifcProductOid = geometryInfoOidToOid.get(geometryInfoOid);
                if (ifcProductOid == null) {
                    throw new GeometryException("Missing geometry info id: " + geometryInfoOid);
                }
                IdEObject ifcProduct = get(ifcProductOid);
                EStructuralFeature geometryFeature = getPackageMetaData().getEClass("IfcProduct").getEStructuralFeature("geometry");
                ifcProduct.eSet(geometryFeature, geometryInfo);
                org.bimserver.models.geometry.Vector3f minBounds = GeometryFactory.eINSTANCE.createVector3f();
                minBounds.setX(dataInputStream.readDouble());
                minBounds.setY(dataInputStream.readDouble());
                minBounds.setZ(dataInputStream.readDouble());
                org.bimserver.models.geometry.Vector3f maxBounds = GeometryFactory.eINSTANCE.createVector3f();
                maxBounds.setX(dataInputStream.readDouble());
                maxBounds.setY(dataInputStream.readDouble());
                maxBounds.setZ(dataInputStream.readDouble());
                geometryInfo.setMinBounds(minBounds);
                geometryInfo.setMaxBounds(maxBounds);
                byte[] transformation = new byte[16 * 8];
                dataInputStream.readFully(transformation);
                geometryInfo.setTransformation(transformation);
                long geometryDataOid = dataInputStream.readLong();
                GeometryData geometryData = (GeometryData) get(geometryDataOid);
                if (geometryData == null) {
                    geometryData = GeometryFactory.eINSTANCE.createGeometryData();
                    add(geometryDataOid, geometryData);
                }
                geometryInfo.setData(geometryData);
                ((IdEObjectImpl) geometryData).setLoadingState(State.LOADED);
            } else if (type == 3) {
                throw new GeometryException("Parts not supported");
            } else if (type == 1) {
                dataInputStream.readFully(new byte[7]);
                long geometryDataOid = dataInputStream.readLong();
                GeometryData geometryData = (GeometryData) get(geometryDataOid);
                if (geometryData == null) {
                    geometryData = GeometryFactory.eINSTANCE.createGeometryData();
                    add(geometryDataOid, geometryData);
                }
                ((IdEObjectImpl) geometryData).setOid(geometryDataOid);
                ((IdEObjectImpl) geometryData).setLoadingState(State.LOADING);
                int nrIndices = dataInputStream.readInt();
                byte[] indices = new byte[nrIndices * 4];
                dataInputStream.readFully(indices);
                geometryData.setIndices(indices);
                int colorType = dataInputStream.readInt();
                if (colorType == 1) {
                    dataInputStream.readFloat();
                    dataInputStream.readFloat();
                    dataInputStream.readFloat();
                    dataInputStream.readFloat();
                }
                int nrVertices = dataInputStream.readInt();
                byte[] vertices = new byte[nrVertices * 4];
                dataInputStream.readFully(vertices);
                geometryData.setVertices(vertices);
                int nrNormals = dataInputStream.readInt();
                byte[] normals = new byte[nrNormals * 4];
                dataInputStream.readFully(normals);
                geometryData.setNormals(normals);
                int nrMaterials = dataInputStream.readInt();
                byte[] materials = new byte[nrMaterials * 4];
                dataInputStream.readFully(materials);
                geometryData.setMaterials(materials);
                ((IdEObjectImpl) geometryData).setLoadingState(State.LOADED);
            } else if (type == 6) {
                done = true;
            } else {
                throw new GeometryException("Unimplemented type: " + type);
            }
        }
    } catch (EOFException e) {
    // 
    } catch (ObjectAlreadyExistsException e) {
        e.printStackTrace();
    }
}
Also used : IdEObject(org.bimserver.emf.IdEObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) GeometryData(org.bimserver.models.geometry.GeometryData) LittleEndianDataInputStream(com.google.common.io.LittleEndianDataInputStream) GeometryInfo(org.bimserver.models.geometry.GeometryInfo) EOFException(java.io.EOFException) ObjectAlreadyExistsException(org.bimserver.plugins.ObjectAlreadyExistsException)

Example 3 with GeometryData

use of org.bimserver.models.geometry.GeometryData in project GltfSerializers by opensourceBIM.

the class BinaryGltfSerializer2 method addVerticesAccessor.

private int addVerticesAccessor(IfcProduct ifcProduct, int bufferViewIndex, int startPosition, int count) throws SerializerException {
    if (count <= 0) {
        throw new SerializerException("Count <= 0");
    }
    if (count * 12 == 9000) {
        System.out.println();
    }
    GeometryData data = ifcProduct.getGeometry().getData();
    ByteBuffer verticesBuffer = ByteBuffer.wrap(data.getVertices());
    ObjectNode accessor = OBJECT_MAPPER.createObjectNode();
    accessor.put("bufferView", bufferViewIndex);
    accessor.put("byteOffset", startPosition);
    // accessor.put("byteStride", 12);
    accessor.put("componentType", FLOAT);
    accessor.put("count", count);
    accessor.put("type", "VEC3");
    verticesBuffer.order(ByteOrder.LITTLE_ENDIAN);
    float[] min = { Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE };
    float[] max = { -Float.MAX_VALUE, -Float.MAX_VALUE, -Float.MAX_VALUE };
    for (int i = 0; i < verticesBuffer.capacity(); i += 12) {
        for (int j = 0; j < 3; j++) {
            float val = verticesBuffer.getFloat(i + (j * 4));
            if (val > max[j]) {
                max[j] = val;
            }
            if (val < min[j]) {
                min[j] = val;
            }
        }
    }
    ArrayNode minNode = OBJECT_MAPPER.createArrayNode();
    minNode.add(min[0]);
    minNode.add(min[1]);
    minNode.add(min[2]);
    ArrayNode maxNode = OBJECT_MAPPER.createArrayNode();
    maxNode.add(max[0]);
    maxNode.add(max[1]);
    maxNode.add(max[2]);
    accessor.set("min", minNode);
    accessor.set("max", maxNode);
    accessors.add(accessor);
    return accessors.size() - 1;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GeometryData(org.bimserver.models.geometry.GeometryData) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) SerializerException(org.bimserver.plugins.serializers.SerializerException) ByteBuffer(java.nio.ByteBuffer)

Example 4 with GeometryData

use of org.bimserver.models.geometry.GeometryData in project GltfSerializers by opensourceBIM.

the class BinaryGltfSerializer2 method generateSceneAndBody.

private void generateSceneAndBody() throws SerializerException {
    int totalBodyByteLength = 0;
    int totalIndicesByteLength = 0;
    int totalVerticesByteLength = 0;
    int totalNormalsByteLength = 0;
    int totalColorsByteLength = 0;
    int maxIndexValues = 16389;
    for (IfcProduct ifcProduct : model.getAllWithSubTypes(IfcProduct.class)) {
        GeometryInfo geometryInfo = ifcProduct.getGeometry();
        if (!ifcProduct.eClass().getName().equals("IfcOpeningElement") && geometryInfo != null && geometryInfo.getData().getVertices().length > 0) {
            GeometryData data = geometryInfo.getData();
            int nrIndicesBytes = data.getIndices().length;
            totalIndicesByteLength += nrIndicesBytes / 2;
            if (nrIndicesBytes > 4 * maxIndexValues) {
                int nrIndices = nrIndicesBytes / 4;
                totalVerticesByteLength += nrIndices * 3 * 4;
                totalNormalsByteLength += nrIndices * 3 * 4;
                if (data.getMaterials() != null) {
                    totalColorsByteLength += nrIndices * 4 * 4;
                }
            } else {
                totalVerticesByteLength += data.getVertices().length;
                totalNormalsByteLength += data.getNormals().length;
                if (data.getMaterials() != null) {
                    totalColorsByteLength += data.getMaterials().length;
                }
            }
        }
    }
    totalBodyByteLength = totalIndicesByteLength + totalVerticesByteLength + totalNormalsByteLength + totalColorsByteLength;
    body = ByteBuffer.allocate(totalBodyByteLength + materialColorFragmentShaderBytes.length + materialColorVertexShaderBytes.length + vertexColorFragmentShaderBytes.length + vertexColorVertexShaderBytes.length);
    body.order(ByteOrder.LITTLE_ENDIAN);
    ByteBuffer newIndicesBuffer = ByteBuffer.allocate(totalIndicesByteLength);
    newIndicesBuffer.order(ByteOrder.LITTLE_ENDIAN);
    ByteBuffer newVerticesBuffer = ByteBuffer.allocate(totalVerticesByteLength);
    newVerticesBuffer.order(ByteOrder.LITTLE_ENDIAN);
    ByteBuffer newNormalsBuffer = ByteBuffer.allocate(totalNormalsByteLength);
    newNormalsBuffer.order(ByteOrder.LITTLE_ENDIAN);
    ByteBuffer newColorsBuffer = ByteBuffer.allocate(totalColorsByteLength);
    newColorsBuffer.order(ByteOrder.LITTLE_ENDIAN);
    int indicesBufferView = createBufferView(totalIndicesByteLength, 0, ELEMENT_ARRAY_BUFFER, -1);
    int verticesBufferView = createBufferView(totalVerticesByteLength, totalIndicesByteLength, ARRAY_BUFFER, 12);
    int normalsBufferView = createBufferView(totalNormalsByteLength, totalIndicesByteLength + totalVerticesByteLength, ARRAY_BUFFER, 12);
    int colorsBufferView = -1;
    scenesNode.add(createDefaultScene());
    gltfNode.put("scene", 0);
    createModelNode();
    for (IfcProduct ifcProduct : model.getAllWithSubTypes(IfcProduct.class)) {
        GeometryInfo geometryInfo = ifcProduct.getGeometry();
        if (!ifcProduct.eClass().getName().equals("IfcOpeningElement") && geometryInfo != null) {
            ByteBuffer matrixByteBuffer = ByteBuffer.wrap(ifcProduct.getGeometry().getTransformation());
            matrixByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
            DoubleBuffer doubleBuffer = matrixByteBuffer.asDoubleBuffer();
            float[] matrix = new float[16];
            for (int i = 0; i < doubleBuffer.capacity(); i++) {
                matrix[i] = (float) doubleBuffer.get();
            }
            updateExtends(geometryInfo, matrix);
        }
    }
    float[] offsets = getOffsets();
    // This will "normalize" the model by moving it's axis-aligned bounding box center to the 0-point. This will always be the wrong position, but at least the building will be close to the 0-point
    modelTranslation.add(-offsets[0]);
    modelTranslation.add(-offsets[1]);
    modelTranslation.add(-offsets[2]);
    for (IfcProduct ifcProduct : model.getAllWithSubTypes(IfcProduct.class)) {
        GeometryInfo geometryInfo = ifcProduct.getGeometry();
        if (!ifcProduct.eClass().getName().equals("IfcOpeningElement") && geometryInfo != null && geometryInfo.getData().getVertices().length > 0) {
            int startPositionIndices = newIndicesBuffer.position();
            int startPositionVertices = newVerticesBuffer.position();
            int startPositionNormals = newNormalsBuffer.position();
            int startPositionColors = newColorsBuffer.position();
            GeometryData data = geometryInfo.getData();
            ByteBuffer indicesBuffer = ByteBuffer.wrap(data.getIndices());
            indicesBuffer.order(ByteOrder.LITTLE_ENDIAN);
            IntBuffer indicesIntBuffer = indicesBuffer.asIntBuffer();
            ByteBuffer verticesBuffer = ByteBuffer.wrap(data.getVertices());
            verticesBuffer.order(ByteOrder.LITTLE_ENDIAN);
            FloatBuffer verticesFloatBuffer = verticesBuffer.asFloatBuffer();
            ByteBuffer normalsBuffer = ByteBuffer.wrap(data.getNormals());
            normalsBuffer.order(ByteOrder.LITTLE_ENDIAN);
            FloatBuffer normalsFloatBuffer = normalsBuffer.asFloatBuffer();
            FloatBuffer materialsFloatBuffer = null;
            if (data.getMaterials() != null) {
                ByteBuffer materialsBuffer = ByteBuffer.wrap(data.getMaterials());
                materialsBuffer.order(ByteOrder.LITTLE_ENDIAN);
                materialsFloatBuffer = materialsBuffer.asFloatBuffer();
            }
            if (data.getIndices().length > 4 * maxIndexValues) {
                int totalNrIndices = indicesIntBuffer.capacity();
                int nrParts = (totalNrIndices + maxIndexValues - 1) / maxIndexValues;
                ArrayNode primitivesNode = OBJECT_MAPPER.createArrayNode();
                for (int part = 0; part < nrParts; part++) {
                    startPositionIndices = newIndicesBuffer.position();
                    startPositionVertices = newVerticesBuffer.position();
                    startPositionNormals = newNormalsBuffer.position();
                    startPositionColors = newColorsBuffer.position();
                    short indexCounter = 0;
                    int upto = Math.min((part + 1) * maxIndexValues, totalNrIndices);
                    for (int i = part * maxIndexValues; i < upto; i++) {
                        newIndicesBuffer.putShort(indexCounter++);
                    }
                    int[] min = new int[] { 0 };
                    int[] max = new int[] { upto };
                    int nrVertices = upto - part * maxIndexValues;
                    for (int i = part * maxIndexValues; i < upto; i += 3) {
                        int oldIndex1 = indicesIntBuffer.get(i);
                        int oldIndex2 = indicesIntBuffer.get(i + 1);
                        int oldIndex3 = indicesIntBuffer.get(i + 2);
                        newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex1 * 3));
                        newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex1 * 3 + 1));
                        newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex1 * 3 + 2));
                        newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex2 * 3));
                        newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex2 * 3 + 1));
                        newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex2 * 3 + 2));
                        newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex3 * 3));
                        newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex3 * 3 + 1));
                        newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex3 * 3 + 2));
                    }
                    for (int i = part * maxIndexValues; i < upto; i += 3) {
                        int oldIndex1 = indicesIntBuffer.get(i);
                        int oldIndex2 = indicesIntBuffer.get(i + 1);
                        int oldIndex3 = indicesIntBuffer.get(i + 2);
                        newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex1 * 3));
                        newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex1 * 3 + 1));
                        newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex1 * 3 + 2));
                        newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex2 * 3));
                        newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex2 * 3 + 1));
                        newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex2 * 3 + 2));
                        newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex3 * 3));
                        newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex3 * 3 + 1));
                        newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex3 * 3 + 2));
                    }
                    if (materialsFloatBuffer != null) {
                        for (int i = part * maxIndexValues; i < upto; i += 3) {
                            int oldIndex1 = indicesIntBuffer.get(i);
                            int oldIndex2 = indicesIntBuffer.get(i + 1);
                            int oldIndex3 = indicesIntBuffer.get(i + 2);
                            newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex1 * 4));
                            newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex1 * 4 + 1));
                            newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex1 * 4 + 2));
                            newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex1 * 4 + 3));
                            newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex2 * 4));
                            newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex2 * 4 + 1));
                            newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex2 * 4 + 2));
                            newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex2 * 4 + 3));
                            newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex3 * 4));
                            newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex3 * 4 + 1));
                            newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex3 * 4 + 2));
                            newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex3 * 4 + 3));
                        }
                    }
                    ObjectNode primitiveNode = OBJECT_MAPPER.createObjectNode();
                    int indicesAccessor = addIndicesAccessor(ifcProduct, indicesBufferView, startPositionIndices, nrVertices / 3, min, max);
                    int verticesAccessor = addVerticesAccessor(ifcProduct, verticesBufferView, startPositionVertices, nrVertices);
                    int normalsAccessor = addNormalsAccessor(ifcProduct, normalsBufferView, startPositionNormals, nrVertices);
                    int colorAccessor = -1;
                    if (data.getMaterials() != null) {
                        if (colorsBufferView == -1) {
                            colorsBufferView = createBufferView(totalColorsByteLength, totalIndicesByteLength + totalVerticesByteLength + totalNormalsByteLength, ARRAY_BUFFER, 16);
                        }
                        colorAccessor = addColorsAccessor(ifcProduct, colorsBufferView, startPositionColors, 16);
                    }
                    primitivesNode.add(primitiveNode);
                    primitiveNode.put("indices", indicesAccessor);
                    primitiveNode.put("mode", TRIANGLES);
                    ObjectNode attributesNode = OBJECT_MAPPER.createObjectNode();
                    primitiveNode.set("attributes", attributesNode);
                    attributesNode.put("NORMAL", normalsAccessor);
                    attributesNode.put("POSITION", verticesAccessor);
                    if (colorAccessor != -1) {
                        // attributesNode.put("COLOR_0", colorAccessor);
                        primitiveNode.put("material", vertexColorIndex);
                    } else {
                        primitiveNode.put("material", createOrGetMaterial(ifcProduct.eClass().getName(), IfcColors.getDefaultColor(ifcProduct.eClass().getName())));
                    }
                }
                int meshId = addMesh(ifcProduct, primitivesNode);
                int nodeId = addNode(meshId, ifcProduct);
                translationChildrenNode.add(nodeId);
            } else {
                int maxVal = 0;
                for (int i = 0; i < indicesIntBuffer.capacity(); i++) {
                    int index = indicesIntBuffer.get(i);
                    if (index > Short.MAX_VALUE) {
                        throw new SerializerException("Index too large to store as short " + index);
                    }
                    newIndicesBuffer.putShort((short) (index));
                    if (index > maxVal) {
                        maxVal = index;
                    }
                }
                int[] min = new int[] { 0 };
                int[] max = new int[] { maxVal };
                newVerticesBuffer.put(data.getVertices());
                newNormalsBuffer.put(data.getNormals());
                if (data.getMaterials() != null) {
                    newColorsBuffer.put(data.getMaterials());
                }
                int totalNrIndices = indicesIntBuffer.capacity();
                ArrayNode primitivesNode = OBJECT_MAPPER.createArrayNode();
                ObjectNode primitiveNode = OBJECT_MAPPER.createObjectNode();
                int indicesAccessor = addIndicesAccessor(ifcProduct, indicesBufferView, startPositionIndices, totalNrIndices, min, max);
                int verticesAccessor = addVerticesAccessor(ifcProduct, verticesBufferView, startPositionVertices, data.getVertices().length / 12);
                int normalsAccessor = addNormalsAccessor(ifcProduct, normalsBufferView, startPositionNormals, data.getNormals().length / 12);
                int colorAccessor = -1;
                if (data.getMaterials() != null) {
                    if (colorsBufferView == -1) {
                        colorsBufferView = createBufferView(totalColorsByteLength, totalIndicesByteLength + totalVerticesByteLength + totalNormalsByteLength, ARRAY_BUFFER, 16);
                    }
                    colorAccessor = addColorsAccessor(ifcProduct, colorsBufferView, startPositionColors, data.getVertices().length / 12);
                }
                primitivesNode.add(primitiveNode);
                primitiveNode.put("indices", indicesAccessor);
                primitiveNode.put("mode", TRIANGLES);
                ObjectNode attributesNode = OBJECT_MAPPER.createObjectNode();
                primitiveNode.set("attributes", attributesNode);
                attributesNode.put("NORMAL", normalsAccessor);
                attributesNode.put("POSITION", verticesAccessor);
                if (colorAccessor != -1) {
                    attributesNode.put("COLOR_0", colorAccessor);
                    primitiveNode.put("material", vertexColorIndex);
                } else {
                    primitiveNode.put("material", createOrGetMaterial(ifcProduct.eClass().getName(), IfcColors.getDefaultColor(ifcProduct.eClass().getName())));
                }
                int meshId = addMesh(ifcProduct, primitivesNode);
                int nodeId = addNode(meshId, ifcProduct);
                translationChildrenNode.add(nodeId);
            }
        }
    }
    if (newIndicesBuffer.position() != newIndicesBuffer.capacity()) {
        throw new SerializerException("Not all space used");
    }
    if (newVerticesBuffer.position() != newVerticesBuffer.capacity()) {
        throw new SerializerException("Not all space used");
    }
    if (newNormalsBuffer.position() != newNormalsBuffer.capacity()) {
        throw new SerializerException("Not all space used");
    }
    if (newColorsBuffer.position() != newColorsBuffer.capacity()) {
        throw new SerializerException("Not all space used");
    }
    newIndicesBuffer.position(0);
    newVerticesBuffer.position(0);
    newNormalsBuffer.position(0);
    newColorsBuffer.position(0);
    body.put(newIndicesBuffer);
    body.put(newVerticesBuffer);
    body.put(newNormalsBuffer);
    body.put(newColorsBuffer);
    int vertexColorFragmentShaderBufferViewName = createBufferView(vertexColorFragmentShaderBytes.length, body.position(), -1, -1);
    body.put(vertexColorFragmentShaderBytes);
    int vertexColorVertexShaderBufferViewName = createBufferView(vertexColorVertexShaderBytes.length, body.position(), -1, -1);
    body.put(vertexColorVertexShaderBytes);
    int materialColorFragmentShaderBufferViewName = createBufferView(materialColorFragmentShaderBytes.length, body.position(), -1, -1);
    body.put(materialColorFragmentShaderBytes);
    int materialColorVertexShaderBufferViewName = createBufferView(materialColorVertexShaderBytes.length, body.position(), -1, -1);
    body.put(materialColorVertexShaderBytes);
    // gltfNode.set("animations", createAnimations());
    gltfNode.set("asset", createAsset());
    // gltfNode.set("programs", createPrograms());
    gltfNode.put("scene", 0);
    // gltfNode.set("skins", createSkins());
    // gltfNode.set("techniques", createTechniques());
    // createVertexColorShaders(vertexColorFragmentShaderBufferViewName, vertexColorVertexShaderBufferViewName);
    // createMaterialColorShaders(materialColorFragmentShaderBufferViewName, materialColorVertexShaderBufferViewName);
    addBuffer(body.capacity());
// ArrayNode extensions = OBJECT_MAPPER.createArrayNode();
// extensions.add("KHR_binary_glTF");
// gltfNode.set("extensionsUsed", extensions);
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GeometryData(org.bimserver.models.geometry.GeometryData) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) SerializerException(org.bimserver.plugins.serializers.SerializerException) IntBuffer(java.nio.IntBuffer) GeometryInfo(org.bimserver.models.geometry.GeometryInfo) IfcProduct(org.bimserver.models.ifc2x3tc1.IfcProduct) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 5 with GeometryData

use of org.bimserver.models.geometry.GeometryData in project BIMserver by opensourceBIM.

the class IfcTools2D method get2D.

public Area get2D(IfcProduct ifcProduct, double multiplierMillimeters) {
    IfcObjectPlacement objectPlacement = ifcProduct.getObjectPlacement();
    double[] productMatrix = placementToMatrix(objectPlacement);
    // Matrix.dump(productMatrix);
    IfcProductRepresentation representation = ifcProduct.getRepresentation();
    if (representation == null) {
        return null;
    }
    for (IfcRepresentation ifcRepresentation : representation.getRepresentations()) {
        if ("Curve2D".equals(ifcRepresentation.getRepresentationType())) {
        // Skip
        } else {
            if (ifcRepresentation instanceof IfcShapeRepresentation) {
                IfcShapeRepresentation ifcShapeRepresentation = (IfcShapeRepresentation) ifcRepresentation;
                for (IfcRepresentationItem ifcRepresentationItem : ifcShapeRepresentation.getItems()) {
                    Area area = getArea(multiplierMillimeters, productMatrix, ifcRepresentationItem);
                    if (area != null && area.getPathIterator(null).isDone()) {
                        return area;
                    }
                }
            }
        }
    }
    // Fall back to 3D geometry projected from the top
    GeometryInfo geometry = ifcProduct.getGeometry();
    if (geometry != null) {
        GeometryData geometryData = geometry.getData();
        if (geometryData != null) {
            int[] indices = GeometryUtils.toIntegerArray(geometryData.getIndices());
            float[] vertices = GeometryUtils.toFloatArray(geometryData.getVertices());
            float[] matrix = GeometryUtils.toFloatArray(GeometryUtils.toDoubleArray(geometry.getTransformation()));
            Area area = new Area();
            for (int i = 0; i < indices.length; i += 3) {
                int index1 = indices[i + 0];
                int index2 = indices[i + 1];
                int index3 = indices[i + 2];
                float[] a = new float[] { vertices[index1 * 3], vertices[index1 * 3 + 1], vertices[index1 * 3 + 2] };
                float[] b = new float[] { vertices[index2 * 3], vertices[index2 * 3 + 1], vertices[index2 * 3 + 2] };
                float[] c = new float[] { vertices[index3 * 3], vertices[index3 * 3 + 1], vertices[index3 * 3 + 2] };
                float[][] points = new float[][] { a, b, c };
                // if (similar(a[2], b[2], c[2])) {
                boolean first = true;
                Path2D.Float path = new Path2D.Float();
                for (int j = 0; j < 3; j++) {
                    float[] point = points[j];
                    float[] res = new float[4];
                    Matrix.multiplyMV(res, 0, matrix, 0, new float[] { point[0], point[1], point[2], 1 }, 0);
                    // * multiplierMillimeters
                    float x = (float) (res[0]);
                    // * multiplierMillimeters
                    float y = (float) (res[1]);
                    if (first) {
                        path.moveTo(x, y);
                        first = false;
                    } else {
                        path.lineTo(x, y);
                    }
                }
                path.closePath();
                area.add(new Area(path));
            // }
            }
            return area;
        }
    } else {
        LOGGER.info("No geometry generated for " + ifcProduct);
    }
    return null;
}
Also used : IfcRepresentationItem(org.bimserver.models.ifc2x3tc1.IfcRepresentationItem) Path2D(java.awt.geom.Path2D) GeometryData(org.bimserver.models.geometry.GeometryData) IfcObjectPlacement(org.bimserver.models.ifc2x3tc1.IfcObjectPlacement) IfcCartesianPoint(org.bimserver.models.ifc2x3tc1.IfcCartesianPoint) Area(java.awt.geom.Area) IfcProductRepresentation(org.bimserver.models.ifc2x3tc1.IfcProductRepresentation) GeometryInfo(org.bimserver.models.geometry.GeometryInfo) IfcShapeRepresentation(org.bimserver.models.ifc2x3tc1.IfcShapeRepresentation) IfcRepresentation(org.bimserver.models.ifc2x3tc1.IfcRepresentation)

Aggregations

GeometryData (org.bimserver.models.geometry.GeometryData)9 GeometryInfo (org.bimserver.models.geometry.GeometryInfo)6 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ByteBuffer (java.nio.ByteBuffer)4 SerializerException (org.bimserver.plugins.serializers.SerializerException)4 DoubleBuffer (java.nio.DoubleBuffer)2 FloatBuffer (java.nio.FloatBuffer)2 IntBuffer (java.nio.IntBuffer)2 IdEObject (org.bimserver.emf.IdEObject)2 IfcProduct (org.bimserver.models.ifc2x3tc1.IfcProduct)2 LittleEndianDataInputStream (com.google.common.io.LittleEndianDataInputStream)1 Area (java.awt.geom.Area)1 Path2D (java.awt.geom.Path2D)1 EOFException (java.io.EOFException)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)1 Vector3f (org.bimserver.models.geometry.Vector3f)1 IfcCartesianPoint (org.bimserver.models.ifc2x3tc1.IfcCartesianPoint)1