use of org.bimserver.models.geometry.GeometryData in project BIMserver by opensourceBIM.
the class OfflineGeometryGenerator method generateGeometry.
private GenerateGeometryResult generateGeometry(IfcProduct ifcProduct) {
GenerateGeometryResult generateGeometryResult = new GenerateGeometryResult();
if (ifcProduct.getRepresentation() != null && ifcProduct.getRepresentation().getRepresentations().size() != 0) {
try {
RenderEngineInstance renderEngineInstance = renderEngineModel.getInstanceFromExpressId(ifcProduct.getExpressId());
RenderEngineGeometry geometry = renderEngineInstance.generateGeometry();
boolean translate = true;
if (geometry != null && geometry.getNrIndices() > 0) {
GeometryInfo geometryInfo = null;
geometryInfo = GeometryFactory.eINSTANCE.createGeometryInfo();
geometryInfo.setMinBounds(createVector3f(model.getPackageMetaData(), model, Double.POSITIVE_INFINITY));
geometryInfo.setMaxBounds(createVector3f(model.getPackageMetaData(), model, -Double.POSITIVE_INFINITY));
try {
double area = renderEngineInstance.getArea();
geometryInfo.setArea(area);
double volume = renderEngineInstance.getVolume();
if (volume < 0d) {
volume = -volume;
}
geometryInfo.setVolume(volume);
// EStructuralFeature guidFeature = ifcProduct.eClass().getEStructuralFeature("GlobalId");
// String guid = (String) ifcProduct.eGet(guidFeature);
// System.out.println(guid + ": " + "Area: " + area + ", Volume: " + volume);
} catch (UnsupportedOperationException e) {
}
GeometryData geometryData = null;
geometryData = GeometryFactory.eINSTANCE.createGeometryData();
geometryData.setIndices(intArrayToByteArray(geometry.getIndices()));
geometryData.setVertices(floatArrayToByteArray(geometry.getVertices()));
geometryData.setMaterialIndices(intArrayToByteArray(geometry.getMaterialIndices()));
geometryData.setNormals(floatArrayToByteArray(geometry.getNormals()));
geometryInfo.setPrimitiveCount(geometry.getIndices().length / 3);
if (geometry.getMaterialIndices() != null && geometry.getMaterialIndices().length > 0) {
boolean hasMaterial = false;
float[] vertex_colors = new float[geometry.getVertices().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 = geometry.getIndices()[i * 3 + j];
if (c > -1) {
hasMaterial = true;
for (int l = 0; l < 4; ++l) {
vertex_colors[4 * k + l] = geometry.getMaterials()[4 * c + l];
}
}
}
}
if (hasMaterial) {
geometryData.setMaterials(floatArrayToByteArray(vertex_colors));
}
}
double[] tranformationMatrix = new double[16];
Matrix.setIdentityM(tranformationMatrix, 0);
if (translate && renderEngineInstance.getTransformationMatrix() != null) {
tranformationMatrix = renderEngineInstance.getTransformationMatrix();
}
for (int i = 0; i < geometry.getIndices().length; i++) {
processExtends(geometryInfo, tranformationMatrix, geometry.getVertices(), geometry.getIndices()[i] * 3, generateGeometryResult);
}
geometryInfo.setData(geometryData);
// long length = (geometryData.getIndices() != null ? geometryData.getIndices().length : 0) +
// (geometryData.getVertices() != null ? geometryData.getVertices().length : 0) +
// (geometryData.getNormals() != null ? geometryData.getNormals().length : 0) +
// (geometryData.getMaterials() != null ? geometryData.getMaterials().length : 0) +
// (geometryData.getMaterialIndices() != null ? geometryData.getMaterialIndices().length : 0);
setTransformationMatrix(geometryInfo, tranformationMatrix);
int hash = hash(geometryData);
if (hashes.containsKey(hash)) {
geometryInfo.setData(hashes.get(hash));
} else {
hashes.put(hash, geometryData);
}
ifcProduct.setGeometry(geometryInfo);
}
} catch (EntityNotFoundException e) {
// 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 = true;
// }
if (!ignoreNotFound) {
LOGGER.info("Entity not found " + ifcProduct.eClass().getName() + " " + ifcProduct.getExpressId() + "/" + ifcProduct.getOid());
}
} catch (BimserverDatabaseException | RenderEngineException e) {
LOGGER.error("", e);
} catch (IfcModelInterfaceException e) {
LOGGER.error("", e);
}
}
return generateGeometryResult;
}
use of org.bimserver.models.geometry.GeometryData in project BIMserver by opensourceBIM.
the class GeometrySimplifier method getMatchingGeometry.
public Set<GeometryData> getMatchingGeometry(IfcProduct ifcProduct, GeometryData geometryData) {
Set<GeometryData> result = new HashSet<>();
Map<Integer, Set<GeometryData>> ofType = data.get(ifcProduct.eClass());
if (ofType != null) {
Set<GeometryData> set = ofType.get(geometryData.getVertices().length);
if (set != null) {
for (GeometryData d : set) {
if (d != ifcProduct.getGeometry().getData()) {
if (matchExactlyTheSame(geometryData, d)) {
result.add(d);
} else if (matchSameOrder(geometryData, d)) {
result.add(d);
// } else if (matchTotalDistance(geometryData, d)) {
// return d;
}
}
}
}
}
return result;
}
use of org.bimserver.models.geometry.GeometryData in project GltfSerializers by opensourceBIM.
the class BinaryGltfSerializer method addVerticesAccessor.
private String addVerticesAccessor(IfcProduct ifcProduct, String bufferViewName, int startPosition, int count) throws SerializerException {
if (count <= 0) {
throw new SerializerException("Count <= 0");
}
String accessorName = "accessor_vertex_" + (accessorCounter++);
GeometryData data = ifcProduct.getGeometry().getData();
ByteBuffer verticesBuffer = ByteBuffer.wrap(data.getVertices());
ObjectNode accessor = OBJECT_MAPPER.createObjectNode();
accessor.put("bufferView", bufferViewName);
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);
double[] min = { Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE };
double[] max = { -Double.MAX_VALUE, -Double.MAX_VALUE, -Double.MAX_VALUE };
for (int i = 0; i < verticesBuffer.capacity(); i += 3) {
for (int j = 0; j < 3; j++) {
double val = verticesBuffer.get(i + j);
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.set(accessorName, accessor);
return accessorName;
}
use of org.bimserver.models.geometry.GeometryData in project GltfSerializers by opensourceBIM.
the class BinaryGltfSerializer 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);
String indicesBufferView = createBufferView(totalIndicesByteLength, 0, ELEMENT_ARRAY_BUFFER);
String verticesBufferView = createBufferView(totalVerticesByteLength, totalIndicesByteLength, ARRAY_BUFFER);
String normalsBufferView = createBufferView(totalNormalsByteLength, totalIndicesByteLength + totalVerticesByteLength, ARRAY_BUFFER);
String colorsBufferView = null;
scenesNode.set("defaultScene", createDefaultScene());
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 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();
String indicesAccessor = addIndicesAccessor(ifcProduct, indicesBufferView, startPositionIndices, nrVertices / 3);
String verticesAccessor = addVerticesAccessor(ifcProduct, verticesBufferView, startPositionVertices, nrVertices);
String normalsAccessor = addNormalsAccessor(ifcProduct, normalsBufferView, startPositionNormals, nrVertices);
String colorAccessor = null;
if (data.getMaterials() != null) {
if (colorsBufferView == null) {
colorsBufferView = createBufferView(totalColorsByteLength, totalIndicesByteLength + totalVerticesByteLength + totalNormalsByteLength, ARRAY_BUFFER);
}
colorAccessor = addColorsAccessor(ifcProduct, colorsBufferView, startPositionColors, nrVertices * 4 / 3);
}
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 != null) {
attributesNode.put("COLOR", colorAccessor);
primitiveNode.put("material", VERTEX_COLOR_MATERIAL);
} else {
primitiveNode.put("material", createOrGetMaterial(ifcProduct.eClass().getName(), IfcColors.getDefaultColor(ifcProduct.eClass().getName())));
}
}
String meshName = addMesh(ifcProduct, primitivesNode);
String nodeName = addNode(meshName, ifcProduct);
translationChildrenNode.add(nodeName);
} else {
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));
}
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();
String indicesAccessor = addIndicesAccessor(ifcProduct, indicesBufferView, startPositionIndices, totalNrIndices);
String verticesAccessor = addVerticesAccessor(ifcProduct, verticesBufferView, startPositionVertices, data.getVertices().length / 4);
String normalsAccessor = addNormalsAccessor(ifcProduct, normalsBufferView, startPositionNormals, data.getNormals().length / 4);
String colorAccessor = null;
if (data.getMaterials() != null) {
if (colorsBufferView == null) {
colorsBufferView = createBufferView(totalColorsByteLength, totalIndicesByteLength + totalVerticesByteLength + totalNormalsByteLength, ARRAY_BUFFER);
}
colorAccessor = addColorsAccessor(ifcProduct, colorsBufferView, startPositionColors, data.getVertices().length / 4);
}
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 != null) {
attributesNode.put("COLOR", colorAccessor);
primitiveNode.put("material", VERTEX_COLOR_MATERIAL);
} else {
primitiveNode.put("material", createOrGetMaterial(ifcProduct.eClass().getName(), IfcColors.getDefaultColor(ifcProduct.eClass().getName())));
}
String meshName = addMesh(ifcProduct, primitivesNode);
String nodeName = addNode(meshName, ifcProduct);
translationChildrenNode.add(nodeName);
}
}
}
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);
String vertexColorFragmentShaderBufferViewName = createBufferView(vertexColorFragmentShaderBytes.length, body.position());
body.put(vertexColorFragmentShaderBytes);
String vertexColorVertexShaderBufferViewName = createBufferView(vertexColorVertexShaderBytes.length, body.position());
body.put(vertexColorVertexShaderBytes);
String materialColorFragmentShaderBufferViewName = createBufferView(materialColorFragmentShaderBytes.length, body.position());
body.put(materialColorFragmentShaderBytes);
String materialColorVertexShaderBufferViewName = createBufferView(materialColorVertexShaderBytes.length, body.position());
body.put(materialColorVertexShaderBytes);
gltfNode.set("animations", createAnimations());
gltfNode.set("asset", createAsset());
gltfNode.set("programs", createPrograms());
gltfNode.put("scene", "defaultScene");
gltfNode.set("skins", createSkins());
gltfNode.set("techniques", createTechniques());
createVertexColorShaders(vertexColorFragmentShaderBufferViewName, vertexColorVertexShaderBufferViewName);
createMaterialColorShaders(materialColorFragmentShaderBufferViewName, materialColorVertexShaderBufferViewName);
addBuffer("binary_glTF", "arraybuffer", body.capacity());
ArrayNode extensions = OBJECT_MAPPER.createArrayNode();
extensions.add("KHR_binary_glTF");
gltfNode.set("extensionsUsed", extensions);
}
Aggregations