Search in sources :

Example 16 with PluginConfiguration

use of org.bimserver.plugins.PluginConfiguration in project BIMserver by opensourceBIM.

the class GeometryRunner method run.

@Override
public void run() {
    long start = System.nanoTime();
    job.setStartNanos(start);
    try {
        HashMapVirtualObject next = objectProvider.next();
        Query query = new Query("Double buffer query " + eClass.getName(), this.streamingGeometryGenerator.packageMetaData);
        QueryPart queryPart = query.createQueryPart();
        while (next != null) {
            queryPart.addOid(next.getOid());
            if (this.streamingGeometryGenerator.packageMetaData.getEClass("IfcProduct").isSuperTypeOf(next.eClass())) {
                job.addObject(next.getOid(), next.eClass().getName());
            }
            next = objectProvider.next();
        }
        objectProvider = new QueryObjectProvider(databaseSession, this.streamingGeometryGenerator.bimServer, query, Collections.singleton(queryContext.getRoid()), this.streamingGeometryGenerator.packageMetaData);
        StreamingSerializer ifcSerializer = ifcSerializerPlugin.createSerializer(new PluginConfiguration());
        RenderEngine renderEngine = null;
        byte[] bytes = null;
        try {
            final Set<HashMapVirtualObject> objects = new HashSet<>();
            ObjectProviderProxy proxy = new ObjectProviderProxy(objectProvider, new ObjectListener() {

                @Override
                public void newObject(HashMapVirtualObject next) {
                    if (eClass.isSuperTypeOf(next.eClass())) {
                        if (next.eGet(GeometryRunner.this.streamingGeometryGenerator.representationFeature) != null) {
                            objects.add(next);
                        }
                    }
                }
            });
            ifcSerializer.init(proxy, null, null, this.streamingGeometryGenerator.bimServer.getPluginManager(), this.streamingGeometryGenerator.packageMetaData);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            IOUtils.copy(ifcSerializer.getInputStream(), baos);
            bytes = baos.toByteArray();
            InputStream in = new ByteArrayInputStream(bytes);
            Map<Integer, String> notFoundObjects = new HashMap<>();
            Set<Range> reusableGeometryData = new HashSet<>();
            Map<Long, Q> productToData = new HashMap<>();
            try {
                if (!objects.isEmpty()) {
                    renderEngine = renderEnginePool.borrowObject();
                    try (RenderEngineModel renderEngineModel = renderEngine.openModel(in, bytes.length)) {
                        renderEngineModel.setSettings(renderEngineSettings);
                        renderEngineModel.setFilter(renderEngineFilter);
                        try {
                            renderEngineModel.generateGeneralGeometry();
                        } catch (RenderEngineException e) {
                            if (e.getCause() instanceof java.io.EOFException) {
                                if (objects.isEmpty() || eClass.getName().equals("IfcAnnotation")) {
                                // SKIP
                                } else {
                                    StreamingGeometryGenerator.LOGGER.error("Error in " + eClass.getName(), e);
                                }
                            }
                        }
                        OidConvertingSerializer oidConvertingSerializer = (OidConvertingSerializer) ifcSerializer;
                        Map<Long, Integer> oidToEid = oidConvertingSerializer.getOidToEid();
                        Map<Long, double[]> matrices = new HashMap<>();
                        for (HashMapVirtualObject ifcProduct : objects) {
                            if (!this.streamingGeometryGenerator.running) {
                                return;
                            }
                            if (ifcProduct.get("GlobalId").equals("1sZn$z_i91bPOCQJOkOibU")) {
                                System.out.println();
                            }
                            Integer expressId = oidToEid.get(ifcProduct.getOid());
                            try {
                                RenderEngineInstance renderEngineInstance = renderEngineModel.getInstanceFromExpressId(expressId);
                                RenderEngineGeometry geometry = renderEngineInstance.generateGeometry();
                                boolean translate = true;
                                if (geometry != null && geometry.getNrIndices() > 0) {
                                    VirtualObject geometryInfo = new HashMapVirtualObject(queryContext, GeometryPackage.eINSTANCE.getGeometryInfo());
                                    WrappedVirtualObject minBounds = new HashMapWrappedVirtualObject(GeometryPackage.eINSTANCE.getVector3f());
                                    WrappedVirtualObject maxBounds = new HashMapWrappedVirtualObject(GeometryPackage.eINSTANCE.getVector3f());
                                    minBounds.set("x", Double.POSITIVE_INFINITY);
                                    minBounds.set("y", Double.POSITIVE_INFINITY);
                                    minBounds.set("z", Double.POSITIVE_INFINITY);
                                    maxBounds.set("x", -Double.POSITIVE_INFINITY);
                                    maxBounds.set("y", -Double.POSITIVE_INFINITY);
                                    maxBounds.set("z", -Double.POSITIVE_INFINITY);
                                    geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_MinBounds(), minBounds);
                                    geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_MaxBounds(), maxBounds);
                                    WrappedVirtualObject minBoundsUntranslated = new HashMapWrappedVirtualObject(GeometryPackage.eINSTANCE.getVector3f());
                                    WrappedVirtualObject maxBoundsUntranslated = new HashMapWrappedVirtualObject(GeometryPackage.eINSTANCE.getVector3f());
                                    minBoundsUntranslated.set("x", Double.POSITIVE_INFINITY);
                                    minBoundsUntranslated.set("y", Double.POSITIVE_INFINITY);
                                    minBoundsUntranslated.set("z", Double.POSITIVE_INFINITY);
                                    maxBoundsUntranslated.set("x", -Double.POSITIVE_INFINITY);
                                    maxBoundsUntranslated.set("y", -Double.POSITIVE_INFINITY);
                                    maxBoundsUntranslated.set("z", -Double.POSITIVE_INFINITY);
                                    geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_MinBoundsUntranslated(), minBoundsUntranslated);
                                    geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_MaxBoundsUntranslated(), maxBoundsUntranslated);
                                    geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_Area(), renderEngineInstance.getArea());
                                    geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_Volume(), renderEngineInstance.getVolume());
                                    VirtualObject geometryData = new HashMapVirtualObject(queryContext, GeometryPackage.eINSTANCE.getGeometryData());
                                    int[] indices = geometry.getIndices();
                                    geometryData.setAttribute(GeometryPackage.eINSTANCE.getGeometryData_Indices(), GeometryUtils.intArrayToByteArray(indices));
                                    float[] vertices = geometry.getVertices();
                                    geometryData.setAttribute(GeometryPackage.eINSTANCE.getGeometryData_Vertices(), GeometryUtils.floatArrayToByteArray(vertices));
                                    geometryData.setAttribute(GeometryPackage.eINSTANCE.getGeometryData_Normals(), GeometryUtils.floatArrayToByteArray(geometry.getNormals()));
                                    geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_PrimitiveCount(), indices.length / 3);
                                    job.setTrianglesGenerated(indices.length / 3);
                                    job.getReport().incrementTriangles(indices.length / 3);
                                    Set<Color4f> usedColors = new HashSet<>();
                                    if (geometry.getMaterialIndices() != null && geometry.getMaterialIndices().length > 0) {
                                        boolean hasMaterial = false;
                                        float[] vertex_colors = new float[vertices.length / 3 * 4];
                                        for (int i = 0; i < geometry.getMaterialIndices().length; ++i) {
                                            int c = geometry.getMaterialIndices()[i];
                                            for (int j = 0; j < 3; ++j) {
                                                int k = indices[i * 3 + j];
                                                if (c > -1) {
                                                    hasMaterial = true;
                                                    Color4f color = new Color4f();
                                                    for (int l = 0; l < 4; ++l) {
                                                        float val = geometry.getMaterials()[4 * c + l];
                                                        vertex_colors[4 * k + l] = val;
                                                        color.set(l, val);
                                                    }
                                                    usedColors.add(color);
                                                }
                                            }
                                        }
                                        if (usedColors.size() == 1) {
                                            WrappedVirtualObject color = new HashMapWrappedVirtualObject(GeometryPackage.eINSTANCE.getVector4f());
                                            Color4f firstColor = usedColors.iterator().next();
                                            color.set("x", firstColor.getR());
                                            color.set("y", firstColor.getG());
                                            color.set("z", firstColor.getB());
                                            color.set("w", firstColor.getA());
                                            geometryData.setAttribute(GeometryPackage.eINSTANCE.getGeometryData_Color(), color);
                                            hasMaterial = false;
                                        }
                                        if (hasMaterial) {
                                            geometryData.setAttribute(GeometryPackage.eINSTANCE.getGeometryData_Materials(), GeometryUtils.floatArrayToByteArray(vertex_colors));
                                        }
                                    }
                                    double[] productTranformationMatrix = new double[16];
                                    if (translate && renderEngineInstance.getTransformationMatrix() != null) {
                                        productTranformationMatrix = renderEngineInstance.getTransformationMatrix();
                                    } else {
                                        Matrix.setIdentityM(productTranformationMatrix, 0);
                                    }
                                    geometryInfo.setReference(GeometryPackage.eINSTANCE.getGeometryInfo_Data(), geometryData.getOid(), 0);
                                    long size = this.streamingGeometryGenerator.getSize(geometryData);
                                    for (int i = 0; i < indices.length; i++) {
                                        this.streamingGeometryGenerator.processExtends(geometryInfo, productTranformationMatrix, vertices, indices[i] * 3, generateGeometryResult);
                                        this.streamingGeometryGenerator.processExtendsUntranslated(geometryInfo, vertices, indices[i] * 3, generateGeometryResult);
                                    }
                                    double[] mibu = new double[] { (double) minBoundsUntranslated.eGet(GeometryPackage.eINSTANCE.getVector3f_X()), (double) minBoundsUntranslated.eGet(GeometryPackage.eINSTANCE.getVector3f_Y()), (double) minBoundsUntranslated.eGet(GeometryPackage.eINSTANCE.getVector3f_Z()), 1d };
                                    double[] mabu = new double[] { (double) maxBoundsUntranslated.eGet(GeometryPackage.eINSTANCE.getVector3f_X()), (double) maxBoundsUntranslated.eGet(GeometryPackage.eINSTANCE.getVector3f_Y()), (double) maxBoundsUntranslated.eGet(GeometryPackage.eINSTANCE.getVector3f_Z()), 1d };
                                    if (reuseGeometry) {
                                        int hash = this.streamingGeometryGenerator.hash(geometryData);
                                        float[] firstVertex = new float[] { vertices[indices[0]], vertices[indices[0] + 1], vertices[indices[0] + 2] };
                                        float[] lastVertex = new float[] { vertices[indices[indices.length - 1] * 3], vertices[indices[indices.length - 1] * 3 + 1], vertices[indices[indices.length - 1] * 3 + 2] };
                                        Range range = new Range(firstVertex, lastVertex);
                                        if (this.streamingGeometryGenerator.hashes.containsKey(hash)) {
                                            geometryInfo.setReference(GeometryPackage.eINSTANCE.getGeometryInfo_Data(), this.streamingGeometryGenerator.hashes.get(hash), 0);
                                            this.streamingGeometryGenerator.bytesSavedByHash.addAndGet(size);
                                        } else if (geometryReused) {
                                            boolean found = false;
                                            // }
                                            if (!found) {
                                                range.setGeometryDataOid(geometryData.getOid());
                                                reusableGeometryData.add(range);
                                                geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_Area(), renderEngineInstance.getArea());
                                                geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_Volume(), renderEngineInstance.getVolume());
                                                geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_PrimitiveCount(), indices.length / 3);
                                                productToData.put(ifcProduct.getOid(), new Q(geometryData.getOid(), renderEngineInstance.getArea(), renderEngineInstance.getVolume(), indices.length / 3, size, mibu, mabu));
                                                geometryData.save();
                                            }
                                        } else {
                                            // }
                                            if (geometryReused) {
                                                range.setGeometryDataOid(geometryData.getOid());
                                                reusableGeometryData.add(range);
                                                productToData.put(ifcProduct.getOid(), new Q(geometryData.getOid(), renderEngineInstance.getArea(), renderEngineInstance.getVolume(), indices.length / 3, size, mibu, mabu));
                                            }
                                            this.streamingGeometryGenerator.hashes.put(hash, geometryData.getOid());
                                            geometryData.save();
                                        // sizes.put(size, ifcProduct);
                                        }
                                    } else {
                                        geometryData.save();
                                    }
                                    calculateObb(geometryInfo, productTranformationMatrix, indices, vertices, generateGeometryResult);
                                    this.streamingGeometryGenerator.setTransformationMatrix(geometryInfo, productTranformationMatrix);
                                    matrices.put(ifcProduct.getOid(), productTranformationMatrix);
                                    geometryInfo.save();
                                    this.streamingGeometryGenerator.totalBytes.addAndGet(size);
                                    ifcProduct.setReference(this.streamingGeometryGenerator.geometryFeature, geometryInfo.getOid(), 0);
                                    ifcProduct.saveOverwrite();
                                // Doing a sync here because probably
                                // writing large amounts of data, and db
                                // only syncs every 100.000 writes by
                                // default
                                // databaseSession.getKeyValueStore().sync();
                                } else {
                                // TODO
                                }
                            } catch (EntityNotFoundException e) {
                                // e.printStackTrace();
                                // As soon as we find a representation that
                                // is not Curve2D, then we should show a
                                // "INFO" message in the log to indicate
                                // there could be something wrong
                                boolean ignoreNotFound = eClass.getName().equals("IfcAnnotation");
                                // }
                                if (!ignoreNotFound) {
                                    // LOGGER.warn("Entity not found " +
                                    // ifcProduct.eClass().getName() + " " +
                                    // (expressId) + "/" +
                                    // ifcProduct.getOid());
                                    notFoundObjects.put(expressId, ifcProduct.eClass().getName());
                                }
                            } catch (BimserverDatabaseException | RenderEngineException e) {
                                StreamingGeometryGenerator.LOGGER.error("", e);
                            }
                        }
                        if (geometryReused && map != null) {
                            long firstKey = map.keySet().iterator().next();
                            ProductDef masterProductDef = map.get(firstKey);
                            for (long key : map.keySet()) {
                                if (key != firstKey) {
                                    ProductDef productDef = map.get(key);
                                    HashMapVirtualObject ifcProduct = productDef.getObject();
                                    Q q = productToData.get(productDef.getMasterOid());
                                    if (q != null) {
                                        VirtualObject geometryInfo = new HashMapVirtualObject(queryContext, GeometryPackage.eINSTANCE.getGeometryInfo());
                                        WrappedVirtualObject minBounds = new HashMapWrappedVirtualObject(GeometryPackage.eINSTANCE.getVector3f());
                                        WrappedVirtualObject maxBounds = new HashMapWrappedVirtualObject(GeometryPackage.eINSTANCE.getVector3f());
                                        double[] mibu = q.getMibu();
                                        double[] mabu = q.getMibu();
                                        double[] mibt = new double[4];
                                        double[] mabt = new double[4];
                                        // TODO
                                        Matrix.multiplyMV(mibt, 0, productDef.getProductMatrix(), 0, mibu, 0);
                                        Matrix.multiplyMV(mabt, 0, productDef.getProductMatrix(), 0, mabu, 0);
                                        minBounds.set("x", mibt[0]);
                                        minBounds.set("y", mibt[1]);
                                        minBounds.set("z", mibt[2]);
                                        maxBounds.set("x", mabt[0]);
                                        maxBounds.set("y", mabt[1]);
                                        maxBounds.set("z", mabt[2]);
                                        geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_MinBounds(), minBounds);
                                        geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_MaxBounds(), maxBounds);
                                        WrappedVirtualObject minBoundsUntranslated = new HashMapWrappedVirtualObject(GeometryPackage.eINSTANCE.getVector3f());
                                        WrappedVirtualObject maxBoundsUntranslated = new HashMapWrappedVirtualObject(GeometryPackage.eINSTANCE.getVector3f());
                                        minBoundsUntranslated.set("x", mibu[0]);
                                        minBoundsUntranslated.set("y", mibu[1]);
                                        minBoundsUntranslated.set("z", mibu[2]);
                                        maxBoundsUntranslated.set("x", mabu[0]);
                                        maxBoundsUntranslated.set("y", mabu[1]);
                                        maxBoundsUntranslated.set("z", mabu[2]);
                                        geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_MinBoundsUntranslated(), minBoundsUntranslated);
                                        geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_MaxBoundsUntranslated(), maxBoundsUntranslated);
                                        geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_Area(), q.getArea());
                                        geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_Volume(), q.getVolume());
                                        geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_PrimitiveCount(), q.getNrPrimitives());
                                        this.streamingGeometryGenerator.bytesSavedByMapping.addAndGet(q.getSize());
                                        this.streamingGeometryGenerator.totalBytes.addAndGet(q.getSize());
                                        double[] inverted = Matrix.identity();
                                        if (!Matrix.invertM(inverted, 0, masterProductDef.getMappingMatrix(), 0)) {
                                            System.out.println("No inverse");
                                        }
                                        double[] finalMatrix = Matrix.identity();
                                        double[] totalTranformationMatrix = Matrix.identity();
                                        Matrix.multiplyMM(finalMatrix, 0, productDef.getMappingMatrix(), 0, inverted, 0);
                                        Matrix.multiplyMM(totalTranformationMatrix, 0, productDef.getProductMatrix(), 0, finalMatrix, 0);
                                        if (matrices.containsKey(ifcProduct.getOid())) {
                                            if (!Arrays.equals(matrices.get(ifcProduct.getOid()), totalTranformationMatrix)) {
                                                System.out.println("Not the same " + ifcProduct.get("GlobalId"));
                                                Matrix.dump(matrices.get(ifcProduct.getOid()));
                                                System.out.println();
                                                Matrix.dump(totalTranformationMatrix);
                                            }
                                        }
                                        geometryInfo.setReference(GeometryPackage.eINSTANCE.getGeometryInfo_Data(), q.getOid(), 0);
                                        // for (int i = 0; i <
                                        // indices.length; i++) {
                                        // processExtends(geometryInfo,
                                        // productTranformationMatrix,
                                        // vertices, indices[i] * 3,
                                        // generateGeometryResult);
                                        // processExtendsUntranslated(geometryInfo,
                                        // vertices, indices[i] * 3,
                                        // generateGeometryResult);
                                        // }
                                        // calculateObb(geometryInfo,
                                        // productTranformationMatrix,
                                        // indices, vertices,
                                        // generateGeometryResult);
                                        this.streamingGeometryGenerator.setTransformationMatrix(geometryInfo, totalTranformationMatrix);
                                        geometryInfo.save();
                                        // totalBytes.addAndGet(size);
                                        ifcProduct.setReference(this.streamingGeometryGenerator.geometryFeature, geometryInfo.getOid(), 0);
                                        ifcProduct.saveOverwrite();
                                    }
                                }
                            }
                        }
                    }
                }
            } finally {
                if (renderEngine != null) {
                    renderEnginePool.returnObject(renderEngine);
                }
                try {
                    if (!notFoundObjects.isEmpty()) {
                        int debugId = writeDebugFile(bytes, false, notFoundObjects);
                        job.setException(new Exception("Missing objects in model (" + Joiner.on(", ").join(notFoundObjects.keySet()) + ")"), debugId);
                    } else if (writeOutputFiles) {
                        int debugId = writeDebugFile(bytes, false, null);
                        job.setDebugFile(debugId);
                    }
                    in.close();
                } catch (Throwable e) {
                } finally {
                }
                this.streamingGeometryGenerator.jobsDone.incrementAndGet();
                this.streamingGeometryGenerator.updateProgress();
            }
        } catch (Exception e) {
            StreamingGeometryGenerator.LOGGER.error("", e);
            int debugId = writeDebugFile(bytes, true, null);
            job.setException(e, debugId);
        // LOGGER.error("Original query: " + originalQuery, e);
        }
    } catch (Exception e) {
        StreamingGeometryGenerator.LOGGER.error("", e);
    // LOGGER.error("Original query: " + originalQuery, e);
    }
    long end = System.nanoTime();
    job.setEndNanos(end);
}
Also used : Query(org.bimserver.database.queries.om.Query) HashMapWrappedVirtualObject(org.bimserver.shared.HashMapWrappedVirtualObject) HashMap(java.util.HashMap) QueryPart(org.bimserver.database.queries.om.QueryPart) RenderEngineGeometry(org.bimserver.plugins.renderengine.RenderEngineGeometry) RenderEngineModel(org.bimserver.plugins.renderengine.RenderEngineModel) HashMapWrappedVirtualObject(org.bimserver.shared.HashMapWrappedVirtualObject) VirtualObject(org.bimserver.shared.VirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) WrappedVirtualObject(org.bimserver.shared.WrappedVirtualObject) QueryObjectProvider(org.bimserver.database.queries.QueryObjectProvider) RenderEngineException(org.bimserver.plugins.renderengine.RenderEngineException) HashSet(java.util.HashSet) OidConvertingSerializer(org.bimserver.plugins.serializers.OidConvertingSerializer) StreamingSerializer(org.bimserver.plugins.serializers.StreamingSerializer) EntityNotFoundException(org.bimserver.plugins.renderengine.EntityNotFoundException) Range(org.bimserver.Range) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectListener(org.bimserver.ObjectListener) Q(org.bimserver.Q) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) PluginConfiguration(org.bimserver.plugins.PluginConfiguration) RenderEngine(org.bimserver.plugins.renderengine.RenderEngine) HashMapWrappedVirtualObject(org.bimserver.shared.HashMapWrappedVirtualObject) WrappedVirtualObject(org.bimserver.shared.WrappedVirtualObject) Color4f(org.bimserver.Color4f) ObjectProviderProxy(org.bimserver.ObjectProviderProxy) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileNotFoundException(java.io.FileNotFoundException) EntityNotFoundException(org.bimserver.plugins.renderengine.EntityNotFoundException) IOException(java.io.IOException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) RenderEngineException(org.bimserver.plugins.renderengine.RenderEngineException) RenderEngineInstance(org.bimserver.plugins.renderengine.RenderEngineInstance) ProductDef(org.bimserver.ProductDef)

Example 17 with PluginConfiguration

use of org.bimserver.plugins.PluginConfiguration in project BIMserver by opensourceBIM.

the class StreamingGeometryGenerator method generateGeometry.

@SuppressWarnings("unchecked")
public GenerateGeometryResult generateGeometry(long uoid, final DatabaseSession databaseSession, QueryContext queryContext) throws BimserverDatabaseException, GeometryGeneratingException {
    GenerateGeometryResult generateGeometryResult = new GenerateGeometryResult();
    packageMetaData = queryContext.getPackageMetaData();
    productClass = packageMetaData.getEClass("IfcProduct");
    geometryFeature = productClass.getEStructuralFeature("geometry");
    representationFeature = productClass.getEStructuralFeature("Representation");
    representationsFeature = packageMetaData.getEClass("IfcProductDefinitionShape").getEStructuralFeature("Representations");
    itemsFeature = packageMetaData.getEClass("IfcShapeRepresentation").getEStructuralFeature("Items");
    mappingSourceFeature = packageMetaData.getEClass("IfcMappedItem").getEStructuralFeature("MappingSource");
    GregorianCalendar now = new GregorianCalendar();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    debugIdentifier = dateFormat.format(now.getTime());
    long start = System.nanoTime();
    String pluginName = "";
    if (queryContext.getPackageMetaData().getSchema() == Schema.IFC4) {
        pluginName = "org.bimserver.ifc.step.serializer.Ifc4StepStreamingSerializerPlugin";
    } else if (queryContext.getPackageMetaData().getSchema() == Schema.IFC2X3TC1) {
        pluginName = "org.bimserver.ifc.step.serializer.Ifc2x3tc1StepStreamingSerializerPlugin";
    } else {
        throw new GeometryGeneratingException("Unknown schema " + queryContext.getPackageMetaData().getSchema());
    }
    reuseGeometry = bimServer.getServerSettingsCache().getServerSettings().isReuseGeometry();
    optimizeMappedItems = bimServer.getServerSettingsCache().getServerSettings().isOptimizeMappedItems();
    report.setStart(new GregorianCalendar());
    report.setIfcSchema(queryContext.getPackageMetaData().getSchema());
    report.setMaxPerFile(maxObjectsPerFile);
    report.setUseMappingOptimization(optimizeMappedItems);
    report.setReuseGeometry(reuseGeometry);
    try {
        final StreamingSerializerPlugin ifcSerializerPlugin = (StreamingSerializerPlugin) bimServer.getPluginManager().getPlugin(pluginName, true);
        if (ifcSerializerPlugin == null) {
            throw new UserException("No IFC serializer found");
        }
        User user = (User) databaseSession.get(uoid, org.bimserver.database.OldQuery.getDefault());
        UserSettings userSettings = user.getUserSettings();
        report.setUserName(user.getName());
        report.setUserUserName(user.getUsername());
        RenderEnginePluginConfiguration renderEngine = null;
        if (eoid != -1) {
            renderEngine = databaseSession.get(eoid, OldQuery.getDefault());
        } else {
            renderEngine = userSettings.getDefaultRenderEngine();
        }
        if (renderEngine == null) {
            throw new UserException("No default render engine has been selected for this user");
        }
        renderEngineName = renderEngine.getName();
        int availableProcessors = Runtime.getRuntime().availableProcessors();
        report.setAvailableProcessors(availableProcessors);
        int maxSimultanousThreads = Math.min(bimServer.getServerSettingsCache().getServerSettings().getRenderEngineProcesses(), availableProcessors);
        if (maxSimultanousThreads < 1) {
            maxSimultanousThreads = 1;
        }
        final RenderEngineSettings settings = new RenderEngineSettings();
        settings.setPrecision(Precision.SINGLE);
        settings.setIndexFormat(IndexFormat.AUTO_DETECT);
        settings.setGenerateNormals(true);
        settings.setGenerateTriangles(true);
        settings.setGenerateWireFrame(false);
        final RenderEngineFilter renderEngineFilter = new RenderEngineFilter();
        RenderEnginePool renderEnginePool = bimServer.getRenderEnginePools().getRenderEnginePool(packageMetaData.getSchema(), renderEngine.getPluginDescriptor().getPluginClassName(), new PluginConfiguration(renderEngine.getSettings()));
        report.setRenderEngineName(renderEngine.getName());
        report.setRenderEnginePluginVersion(renderEngine.getPluginDescriptor().getPluginBundleVersion().getVersion());
        try (RenderEngine engine = renderEnginePool.borrowObject()) {
            report.setRenderEngineVersion(engine.getVersion());
        }
        ThreadPoolExecutor executor = new ThreadPoolExecutor(maxSimultanousThreads, maxSimultanousThreads, 24, TimeUnit.HOURS, new ArrayBlockingQueue<Runnable>(10000000));
        JsonQueryObjectModelConverter jsonQueryObjectModelConverter = new JsonQueryObjectModelConverter(packageMetaData);
        String queryNameSpace = "validifc";
        if (packageMetaData.getSchema() == Schema.IFC4) {
            queryNameSpace = "ifc4stdlib";
        }
        Include objectPlacement = jsonQueryObjectModelConverter.getDefineFromFile(queryNameSpace + ":ObjectPlacement");
        // TODO these are cached, so f'ing em up by doing this...
        objectPlacement.makeDirectRecursive(new HashSet<>());
        Set<EClass> classes = null;
        if (queryContext.getOidCounters() != null) {
            classes = queryContext.getOidCounters().keySet();
        } else {
            classes = packageMetaData.getEClasses();
        }
        for (EClass eClass : classes) {
            if (packageMetaData.getEClass("IfcProduct").isSuperTypeOf(eClass)) {
                Query query2 = new Query(eClass.getName() + "Main query", packageMetaData);
                QueryPart queryPart2 = query2.createQueryPart();
                queryPart2.addType(eClass, false);
                Include representationInclude = queryPart2.createInclude();
                representationInclude.addType(eClass, false);
                representationInclude.addFieldDirect("Representation");
                Include representationsInclude = representationInclude.createInclude();
                representationsInclude.addType(packageMetaData.getEClass("IfcProductRepresentation"), true);
                representationsInclude.addFieldDirect("Representations");
                Include itemsInclude = representationsInclude.createInclude();
                itemsInclude.addType(packageMetaData.getEClass("IfcShapeRepresentation"), false);
                itemsInclude.addFieldDirect("Items");
                Include mappingSourceInclude = itemsInclude.createInclude();
                mappingSourceInclude.addType(packageMetaData.getEClass("IfcMappedItem"), false);
                mappingSourceInclude.addFieldDirect("MappingSource");
                mappingSourceInclude.addFieldDirect("MappingTarget");
                Include representationMap = mappingSourceInclude.createInclude();
                representationMap.addType(packageMetaData.getEClass("IfcRepresentationMap"), false);
                Include targetInclude = mappingSourceInclude.createInclude();
                targetInclude.addType(packageMetaData.getEClass("IfcCartesianTransformationOperator3D"), false);
                targetInclude.addFieldDirect("Axis1");
                targetInclude.addFieldDirect("Axis2");
                targetInclude.addFieldDirect("Axis3");
                targetInclude.addFieldDirect("LocalOrigin");
                queryPart2.addInclude(objectPlacement);
                Map<Long, Map<Long, ProductDef>> representationMapToProduct = new HashMap<>();
                QueryObjectProvider queryObjectProvider2 = new QueryObjectProvider(databaseSession, bimServer, query2, Collections.singleton(queryContext.getRoid()), packageMetaData);
                HashMapVirtualObject next = queryObjectProvider2.next();
                while (next != null) {
                    if (next.eClass() == eClass) {
                        HashMapVirtualObject representation = next.getDirectFeature(representationFeature);
                        if (representation != null) {
                            List<HashMapVirtualObject> representations = representation.getDirectListFeature(representationsFeature);
                            if (representations != null) {
                                for (HashMapVirtualObject representationItem : representations) {
                                    String representationIdentifier = (String) representationItem.get("RepresentationIdentifier");
                                    if (representationIdentifier.equals("Body") || representationIdentifier.equals("Facetation")) {
                                        List<HashMapVirtualObject> items = representationItem.getDirectListFeature(itemsFeature);
                                        for (HashMapVirtualObject item : items) {
                                            report.addRepresentationItem(item.eClass().getName());
                                            HashMapVirtualObject mappingTarget = item.getDirectFeature(packageMetaData.getEReference("IfcMappedItem", "MappingTarget"));
                                            double[] mappingMatrix = Matrix.identity();
                                            double[] productMatrix = Matrix.identity();
                                            if (mappingTarget != null) {
                                                HashMapVirtualObject axis1 = mappingTarget.getDirectFeature(packageMetaData.getEReference("IfcCartesianTransformationOperator", "Axis1"));
                                                HashMapVirtualObject axis2 = mappingTarget.getDirectFeature(packageMetaData.getEReference("IfcCartesianTransformationOperator", "Axis2"));
                                                HashMapVirtualObject axis3 = mappingTarget.getDirectFeature(packageMetaData.getEReference("IfcCartesianTransformationOperator", "Axis3"));
                                                HashMapVirtualObject localOrigin = mappingTarget.getDirectFeature(packageMetaData.getEReference("IfcCartesianTransformationOperator", "LocalOrigin"));
                                                double[] a1 = null;
                                                double[] a2 = null;
                                                double[] a3 = null;
                                                if (axis3 != null) {
                                                    List<Double> list = (List<Double>) axis3.get("DirectionRatios");
                                                    a3 = new double[] { list.get(0), list.get(1), list.get(2) };
                                                } else {
                                                    a3 = new double[] { 0, 0, 1, 1 };
                                                    Vector.normalize(a3);
                                                }
                                                if (axis1 != null) {
                                                    List<Double> list = (List<Double>) axis1.get("DirectionRatios");
                                                    a1 = new double[] { list.get(0), list.get(1), list.get(2) };
                                                    Vector.normalize(a1);
                                                } else {
                                                    // if (a3[0] == 1 && a3[1] == 0 && a3[2] == 0) {
                                                    a1 = new double[] { 1, 0, 0, 1 };
                                                // } else {
                                                // a1 = new double[]{0, 1, 0, 1};
                                                // }
                                                }
                                                double[] xVec = Vector.scalarProduct(Vector.dot(a1, a3), a3);
                                                double[] xAxis = Vector.subtract(a1, xVec);
                                                Vector.normalize(xAxis);
                                                if (axis2 != null) {
                                                    List<Double> list = (List<Double>) axis2.get("DirectionRatios");
                                                    a2 = new double[] { list.get(0), list.get(1), list.get(2) };
                                                    Vector.normalize(a2);
                                                } else {
                                                    a2 = new double[] { 0, 1, 0, 1 };
                                                }
                                                double[] tmp = Vector.scalarProduct(Vector.dot(a2, a3), a3);
                                                double[] yAxis = Vector.subtract(a2, tmp);
                                                tmp = Vector.scalarProduct(Vector.dot(a2, xAxis), xAxis);
                                                yAxis = Vector.subtract(yAxis, tmp);
                                                Vector.normalize(yAxis);
                                                a2 = yAxis;
                                                a1 = xAxis;
                                                List<Double> t = (List<Double>) localOrigin.get("Coordinates");
                                                mappingMatrix = new double[] { a1[0], a1[1], a1[2], 0, a2[0], a2[1], a2[2], 0, a3[0], a3[1], a3[2], 0, t.get(0).doubleValue(), t.get(1).doubleValue(), t.get(2).doubleValue(), 1 };
                                            }
                                            HashMapVirtualObject placement = next.getDirectFeature(packageMetaData.getEReference("IfcProduct", "ObjectPlacement"));
                                            if (placement != null) {
                                                productMatrix = placementToMatrix(placement);
                                            }
                                            HashMapVirtualObject mappingSource = item.getDirectFeature(mappingSourceFeature);
                                            if (mappingSource != null) {
                                                Map<Long, ProductDef> map = representationMapToProduct.get(mappingSource.getOid());
                                                if (map == null) {
                                                    map = new LinkedHashMap<>();
                                                    representationMapToProduct.put(mappingSource.getOid(), map);
                                                }
                                                ProductDef pd = new ProductDef(next.getOid());
                                                pd.setObject(next);
                                                pd.setProductMatrix(productMatrix);
                                                pd.setMappingMatrix(mappingMatrix);
                                                map.put(next.getOid(), pd);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    next = queryObjectProvider2.next();
                }
                Set<Long> done = new HashSet<>();
                for (Long repMapId : representationMapToProduct.keySet()) {
                    Map<Long, ProductDef> map = representationMapToProduct.get(repMapId);
                    if (map.size() > 1) {
                        Query query = new Query("Reuse query " + eClass.getName(), packageMetaData);
                        QueryPart queryPart = query.createQueryPart();
                        queryPart.addType(eClass, false);
                        long masterOid = map.values().iterator().next().getOid();
                        for (ProductDef pd : map.values()) {
                            done.add(pd.getOid());
                            if (!optimizeMappedItems) {
                                queryPart.addOid(pd.getOid());
                            } else {
                                pd.setMasterOid(masterOid);
                            }
                        }
                        if (optimizeMappedItems) {
                            queryPart.addOid(masterOid);
                        }
                        LOGGER.debug("Running " + map.size() + " objects in one batch because of reused geometry " + (eClass.getName()));
                        processX(databaseSession, queryContext, generateGeometryResult, ifcSerializerPlugin, settings, renderEngineFilter, renderEnginePool, executor, eClass, query, queryPart, true, map, map.size());
                    }
                }
                Query query3 = new Query("Remaining " + eClass.getName(), packageMetaData);
                QueryPart queryPart3 = query3.createQueryPart();
                queryPart3.addType(eClass, false);
                Include include3 = queryPart3.createInclude();
                include3.addType(eClass, false);
                include3.addFieldDirect("Representation");
                Include rInclude = include3.createInclude();
                rInclude.addType(packageMetaData.getEClass("IfcProductRepresentation"), false);
                rInclude.addFieldDirect("Representations");
                Include representationsInclude2 = rInclude.createInclude();
                representationsInclude2.addType(packageMetaData.getEClass("IfcShapeModel"), false);
                queryObjectProvider2 = new QueryObjectProvider(databaseSession, bimServer, query3, Collections.singleton(queryContext.getRoid()), packageMetaData);
                next = queryObjectProvider2.next();
                while (next != null) {
                    if (next.eClass() == eClass && !done.contains(next.getOid())) {
                        HashMapVirtualObject representation = next.getDirectFeature(representationFeature);
                        if (representation != null) {
                            List<HashMapVirtualObject> list = representation.getDirectListFeature(packageMetaData.getEReference("IfcProductRepresentation", "Representations"));
                            boolean goForIt = goForIt(list);
                            if (goForIt) {
                                Query query = new Query("Main " + eClass.getName(), packageMetaData);
                                QueryPart queryPart = query.createQueryPart();
                                queryPart.addType(eClass, false);
                                int x = 1;
                                queryPart.addOid(next.getOid());
                                while (next != null && x < maxObjectsPerFile) {
                                    next = queryObjectProvider2.next();
                                    if (next != null) {
                                        if (next.eClass() == eClass && !done.contains(next.getOid())) {
                                            representation = next.getDirectFeature(representationFeature);
                                            if (representation != null) {
                                                list = representation.getDirectListFeature(packageMetaData.getEReference("IfcProductRepresentation", "Representations"));
                                                boolean goForIt2 = goForIt(list);
                                                if (goForIt2) {
                                                    queryPart.addOid(next.getOid());
                                                    x++;
                                                }
                                            }
                                        }
                                    }
                                }
                                processX(databaseSession, queryContext, generateGeometryResult, ifcSerializerPlugin, settings, renderEngineFilter, renderEnginePool, executor, eClass, query, queryPart, false, null, x);
                            }
                        }
                    }
                    next = queryObjectProvider2.next();
                }
            }
        }
        // for (Long l : counters.keySet()) {
        // LOGGER.info(databaseSession.getEClassForOid(l).getName() + "(" + l + "): " + counters.get(l));
        // }
        allJobsPushed = true;
        executor.shutdown();
        executor.awaitTermination(24, TimeUnit.HOURS);
        long end = System.nanoTime();
        long total = totalBytes.get() - (bytesSavedByHash.get() + bytesSavedByTransformation.get() + bytesSavedByMapping.get());
        LOGGER.info("Rendertime: " + Formatters.nanosToString(end - start) + ", " + "Reused (by hash): " + Formatters.bytesToString(bytesSavedByHash.get()) + ", Reused (by transformation): " + Formatters.bytesToString(bytesSavedByTransformation.get()) + ", Reused (by mapping): " + Formatters.bytesToString(bytesSavedByMapping.get()) + ", Total: " + Formatters.bytesToString(totalBytes.get()) + ", Final: " + Formatters.bytesToString(total));
    } catch (Exception e) {
        running = false;
        LOGGER.error("", e);
        report.setEnd(new GregorianCalendar());
        throw new GeometryGeneratingException(e);
    }
    report.setEnd(new GregorianCalendar());
    try {
        writeDebugFile();
    } catch (IOException e) {
        LOGGER.debug("", e);
    }
    return generateGeometryResult;
}
Also used : User(org.bimserver.models.store.User) RenderEnginePool(org.bimserver.renderengine.RenderEnginePool) JsonQueryObjectModelConverter(org.bimserver.database.queries.om.JsonQueryObjectModelConverter) OldQuery(org.bimserver.database.OldQuery) Query(org.bimserver.database.queries.om.Query) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) QueryPart(org.bimserver.database.queries.om.QueryPart) Include(org.bimserver.database.queries.om.Include) GeometryGeneratingException(org.bimserver.GeometryGeneratingException) EClass(org.eclipse.emf.ecore.EClass) QueryObjectProvider(org.bimserver.database.queries.QueryObjectProvider) RenderEnginePluginConfiguration(org.bimserver.models.store.RenderEnginePluginConfiguration) PluginConfiguration(org.bimserver.plugins.PluginConfiguration) List(java.util.List) UserException(org.bimserver.shared.exceptions.UserException) RenderEngine(org.bimserver.plugins.renderengine.RenderEngine) HashSet(java.util.HashSet) UserSettings(org.bimserver.models.store.UserSettings) GregorianCalendar(java.util.GregorianCalendar) IOException(java.io.IOException) StreamingSerializerPlugin(org.bimserver.plugins.serializers.StreamingSerializerPlugin) QueryException(org.bimserver.database.queries.om.QueryException) GeometryGeneratingException(org.bimserver.GeometryGeneratingException) UserException(org.bimserver.shared.exceptions.UserException) IOException(java.io.IOException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) RenderEnginePluginConfiguration(org.bimserver.models.store.RenderEnginePluginConfiguration) RenderEngineFilter(org.bimserver.plugins.renderengine.RenderEngineFilter) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) GenerateGeometryResult(org.bimserver.GenerateGeometryResult) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) AtomicLong(java.util.concurrent.atomic.AtomicLong) ProductDef(org.bimserver.ProductDef) RenderEngineSettings(org.bimserver.plugins.renderengine.RenderEngineSettings) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SimpleDateFormat(java.text.SimpleDateFormat) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 18 with PluginConfiguration

use of org.bimserver.plugins.PluginConfiguration in project BIMserver by opensourceBIM.

the class LongDownloadAction method init.

public void init(Thread thread) {
    super.init(thread);
    if (getBimServer().getServerSettingsCache().getServerSettings().getCacheOutputFiles() && getBimServer().getDiskCacheManager().contains(downloadParameters)) {
        return;
    }
    ObjectIDM objectIDM = null;
    if (downloadParameters.getUseObjectIDM()) {
        session = getBimServer().getDatabase().createSession();
        try {
            SerializerPluginConfiguration serializerPluginConfiguration = session.get(StorePackage.eINSTANCE.getSerializerPluginConfiguration(), downloadParameters.getSerializerOid(), OldQuery.getDefault());
            if (serializerPluginConfiguration != null) {
                ObjectIDMPluginConfiguration objectIdm = serializerPluginConfiguration.getObjectIDM();
                if (objectIdm != null) {
                    ObjectIDMPlugin objectIDMPlugin = getBimServer().getPluginManager().getObjectIDMByName(objectIdm.getPluginDescriptor().getPluginClassName(), true);
                    if (objectIDMPlugin != null) {
                        objectIDM = objectIDMPlugin.getObjectIDM(new PluginConfiguration());
                    }
                }
            }
        } catch (BimserverDatabaseException e) {
            LOGGER.error("", e);
        } finally {
            session.close();
        }
    }
    session = getBimServer().getDatabase().createSession();
    switch(downloadParameters.getDownloadType()) {
        case DOWNLOAD_BY_NEW_JSON_QUERY:
            action = new DownloadByNewJsonQueryDatabaseAction(getBimServer(), session, accessMethod, downloadParameters.getRoids(), downloadParameters.getJsonQuery(), downloadParameters.getSerializerOid(), getAuthorization());
            break;
        case DOWNLOAD_PROJECTS:
            action = new DownloadProjectsDatabaseAction(getBimServer(), session, accessMethod, downloadParameters.getRoids(), downloadParameters.getSerializerOid(), getAuthorization(), objectIDM);
            break;
        case DOWNLOAD_COMPARE:
            action = new DownloadCompareDatabaseAction(getBimServer(), session, accessMethod, downloadParameters.getRoids(), downloadParameters.getModelCompareIdentifier(), downloadParameters.getCompareType(), getAuthorization(), objectIDM);
            break;
    }
    action.addProgressListener(this);
}
Also used : DownloadProjectsDatabaseAction(org.bimserver.database.actions.DownloadProjectsDatabaseAction) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ObjectIDMPluginConfiguration(org.bimserver.models.store.ObjectIDMPluginConfiguration) DownloadCompareDatabaseAction(org.bimserver.database.actions.DownloadCompareDatabaseAction) DownloadByNewJsonQueryDatabaseAction(org.bimserver.database.actions.DownloadByNewJsonQueryDatabaseAction) SerializerPluginConfiguration(org.bimserver.models.store.SerializerPluginConfiguration) SerializerPluginConfiguration(org.bimserver.models.store.SerializerPluginConfiguration) ObjectIDMPluginConfiguration(org.bimserver.models.store.ObjectIDMPluginConfiguration) PluginConfiguration(org.bimserver.plugins.PluginConfiguration) ObjectIDMPlugin(org.bimserver.plugins.objectidms.ObjectIDMPlugin) ObjectIDM(org.bimserver.plugins.objectidms.ObjectIDM)

Aggregations

PluginConfiguration (org.bimserver.plugins.PluginConfiguration)18 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)9 IfcModelInterface (org.bimserver.emf.IfcModelInterface)7 UserException (org.bimserver.shared.exceptions.UserException)7 IOException (java.io.IOException)6 DatabaseSession (org.bimserver.database.DatabaseSession)6 SerializerPluginConfiguration (org.bimserver.models.store.SerializerPluginConfiguration)6 PackageMetaData (org.bimserver.emf.PackageMetaData)5 Revision (org.bimserver.models.store.Revision)5 User (org.bimserver.models.store.User)5 UserSettings (org.bimserver.models.store.UserSettings)5 PluginException (org.bimserver.shared.exceptions.PluginException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Deserializer (org.bimserver.plugins.deserializers.Deserializer)4 DeserializerPlugin (org.bimserver.plugins.deserializers.DeserializerPlugin)4 FileNotFoundException (java.io.FileNotFoundException)3 OldQuery (org.bimserver.database.OldQuery)3 BasicIfcModel (org.bimserver.ifc.BasicIfcModel)3 PluginManager (org.bimserver.plugins.PluginManager)3 ServerException (org.bimserver.shared.exceptions.ServerException)3