use of org.bimserver.models.ifc2x3tc1.IfcProduct in project BIMserver by opensourceBIM.
the class OfflineGeometryGenerator method generateForAllElements.
public void generateForAllElements() {
try {
serializer.init(model, null, true);
InputStream in = new SerializerInputstream(serializer);
renderEngineModel = renderEngine.openModel(in);
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();
renderEngineModel.setSettings(settings);
renderEngineModel.setFilter(renderEngineFilter);
renderEngineModel.generateGeneralGeometry();
for (IfcProduct ifcProduct : model.getAllWithSubTypes(IfcProduct.class)) {
generateGeometry(ifcProduct);
}
} catch (SerializerException e) {
e.printStackTrace();
} catch (RenderEngineException e) {
e.printStackTrace();
}
}
use of org.bimserver.models.ifc2x3tc1.IfcProduct in project BIMserver by opensourceBIM.
the class ModelColorizer method setColor.
public void setColor(IfcProduct product, double[] color, float transparency) throws IfcModelInterfaceException {
if (!productToStyledItem.containsKey(product) || productToStyledItem.get(product).isEmpty()) {
// There is no style, let's construct a path to a new one
System.out.println("No style for " + product + " " + product.getGlobalId());
IfcProductRepresentation productRepresentation = product.getRepresentation();
if (product.getRepresentation() == null) {
productRepresentation = model.create(IfcProductRepresentation.class);
product.setRepresentation(productRepresentation);
}
IfcRepresentation representation = null;
if (productRepresentation.getRepresentations().isEmpty()) {
representation = model.create(IfcRepresentation.class);
productRepresentation.getRepresentations().add(representation);
} else {
representation = productRepresentation.getRepresentations().get(0);
}
IfcRepresentationItem representationItem = null;
if (representation.getItems().isEmpty()) {
representationItem = model.create(IfcRepresentationItem.class);
representation.getItems().add(representationItem);
} else {
representationItem = representation.getItems().get(0);
}
IfcStyledItem styledItem = model.create(IfcStyledItem.class);
representationItem.getStyledByItem().clear();
representationItem.getStyledByItem().add(styledItem);
createStyle(color, transparency, styledItem);
} else if (productToStyledItem.get(product).size() == 1) {
// There is one style, we can work with that
IfcStyledItem styledItem = productToStyledItem.get(product).iterator().next();
Set<IfcProduct> products = styledItemToProduct.get(styledItem);
if (products.isEmpty()) {
// That's weird, inconsistent with other map
} else if (products.size() == 1) {
// That's probably us, we can alter the Style safely
createStyle(color, transparency, styledItem);
} else {
// Multiple objects are using this style, we have to do some magic
}
} else {
// There are multiple styles, that's a bit much...
System.out.println(productToStyledItem.get(product).size() + " styles for " + product);
}
}
use of org.bimserver.models.ifc2x3tc1.IfcProduct 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