|
| 1 | +/******************************************************************************** |
| 2 | + * * |
| 3 | + * This file is part of IfcOpenShell. * |
| 4 | + * * |
| 5 | + * IfcOpenShell is free software: you can redistribute it and/or modify * |
| 6 | + * it under the terms of the Lesser GNU General Public License as published by * |
| 7 | + * the Free Software Foundation, either version 3.0 of the License, or * |
| 8 | + * (at your option) any later version. * |
| 9 | + * * |
| 10 | + * IfcOpenShell is distributed in the hope that it will be useful, * |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | + * Lesser GNU General Public License for more details. * |
| 14 | + * * |
| 15 | + * You should have received a copy of the Lesser GNU General Public License * |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | + * * |
| 18 | + ********************************************************************************/ |
| 19 | + |
| 20 | +#include "ColladaSerializer.h" |
| 21 | + |
| 22 | +void ColladaSerializer::ColladaExporter::ColladaGeometries::addFloatSource(const std::string& mesh_id, const std::string& suffix, const std::vector<float>& floats, const char* coords /* = "XYZ" */) { |
| 23 | + COLLADASW::FloatSource source(mSW); |
| 24 | + source.setId(mesh_id + suffix); |
| 25 | + source.setArrayId(mesh_id + suffix + COLLADASW::LibraryGeometries::ARRAY_ID_SUFFIX); |
| 26 | + source.setAccessorStride(strlen(coords)); |
| 27 | + source.setAccessorCount(floats.size() / 3); |
| 28 | + for (unsigned int i = 0; i < source.getAccessorStride(); ++i) { |
| 29 | + source.getParameterNameList().push_back(std::string(1, coords[i])); |
| 30 | + } |
| 31 | + source.prepareToAppendValues(); |
| 32 | + for (std::vector<float>::const_iterator it = floats.begin(); it != floats.end(); ++it) { |
| 33 | + source.appendValues(*it); |
| 34 | + } |
| 35 | + source.finish(); |
| 36 | +} |
| 37 | + |
| 38 | +void ColladaSerializer::ColladaExporter::ColladaGeometries::write(const std::string mesh_id, const std::vector<float>& positions, const std::vector<float>& normals, const std::vector<int>& indices) { |
| 39 | + openMesh(mesh_id); |
| 40 | + |
| 41 | + addFloatSource(mesh_id, COLLADASW::LibraryGeometries::POSITIONS_SOURCE_ID_SUFFIX, positions); |
| 42 | + if (!normals.empty()) { |
| 43 | + // The normals vector can be empty for example when the WELD_VERTICES setting is used. |
| 44 | + // IfcOpenShell does not provide them with multiple face normals collapsed into a single vertex. |
| 45 | + addFloatSource(mesh_id, COLLADASW::LibraryGeometries::NORMALS_SOURCE_ID_SUFFIX, normals); |
| 46 | + } |
| 47 | + |
| 48 | + COLLADASW::VerticesElement vertices(mSW); |
| 49 | + vertices.setId(mesh_id + COLLADASW::LibraryGeometries::VERTICES_ID_SUFFIX ); |
| 50 | + vertices.getInputList().push_back(COLLADASW::Input(COLLADASW::InputSemantic::POSITION, "#" + mesh_id + COLLADASW::LibraryGeometries::POSITIONS_SOURCE_ID_SUFFIX)); |
| 51 | + vertices.add(); |
| 52 | + |
| 53 | + COLLADASW::Triangles triangles(mSW); |
| 54 | + triangles.setCount(indices.size() / 3); |
| 55 | + int offset = 0; |
| 56 | + triangles.getInputList().push_back(COLLADASW::Input(COLLADASW::InputSemantic::VERTEX,"#" + mesh_id + COLLADASW::LibraryGeometries::VERTICES_ID_SUFFIX, offset++ ) ); |
| 57 | + if (!normals.empty()) { |
| 58 | + triangles.getInputList().push_back(COLLADASW::Input(COLLADASW::InputSemantic::NORMAL,"#" + mesh_id + COLLADASW::LibraryGeometries::NORMALS_SOURCE_ID_SUFFIX, offset++ ) ); |
| 59 | + } |
| 60 | + triangles.prepareToAppendValues(); |
| 61 | + for (auto it = indices.begin(); it != indices.end(); ++it) { |
| 62 | + const auto& idx = *it; |
| 63 | + if (!normals.empty()) { |
| 64 | + triangles.appendValues(idx, idx); |
| 65 | + } else { |
| 66 | + triangles.appendValues(idx); |
| 67 | + } |
| 68 | + } |
| 69 | + triangles.finish(); |
| 70 | + |
| 71 | + closeMesh(); |
| 72 | + closeGeometry(); |
| 73 | +} |
| 74 | + |
| 75 | +void ColladaSerializer::ColladaExporter::ColladaGeometries::close() { |
| 76 | + closeLibrary(); |
| 77 | +} |
| 78 | + |
| 79 | +void ColladaSerializer::ColladaExporter::ColladaScene::add(const std::string& node_id, const std::string& node_name, const std::string& geom_id, const std::string& material_id, const std::vector<float>& matrix) { |
| 80 | + if (!scene_opened) { |
| 81 | + openVisualScene(scene_id); |
| 82 | + scene_opened = true; |
| 83 | + } |
| 84 | + |
| 85 | + COLLADASW::Node node(mSW); |
| 86 | + node.setNodeId(node_id); |
| 87 | + node.setNodeName(node_name); |
| 88 | + node.setType(COLLADASW::Node::DEFAULT); |
| 89 | + |
| 90 | + // The matrix attribute of an entity is basically a 4x3 representation of its ObjectPlacement. |
| 91 | + // Note that this placement is absolute, ie it is multiplied with all parent placements. |
| 92 | + double matrix_array[4][4] = { |
| 93 | + {matrix[0], matrix[3], matrix[6], matrix[ 9]}, |
| 94 | + {matrix[1], matrix[4], matrix[7], matrix[10]}, |
| 95 | + {matrix[2], matrix[5], matrix[8], matrix[11]}, |
| 96 | + { 0, 0, 0, 1} |
| 97 | + }; |
| 98 | + |
| 99 | + node.start(); |
| 100 | + node.addMatrix(matrix_array); |
| 101 | + |
| 102 | + COLLADASW::InstanceGeometry instanceGeometry(mSW); |
| 103 | + instanceGeometry.setUrl ("#" + geom_id); |
| 104 | + COLLADASW::InstanceMaterial material ("ColorMaterial", "#" + material_id); |
| 105 | + instanceGeometry.getBindMaterial().getInstanceMaterialList().push_back(material); |
| 106 | + instanceGeometry.add(); |
| 107 | + node.end(); |
| 108 | +} |
| 109 | + |
| 110 | +void ColladaSerializer::ColladaExporter::ColladaScene::write() { |
| 111 | + if (scene_opened) { |
| 112 | + closeVisualScene(); |
| 113 | + closeLibrary(); |
| 114 | + |
| 115 | + COLLADASW::Scene scene (mSW, COLLADASW::URI ("#" + scene_id)); |
| 116 | + scene.add(); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +void ColladaSerializer::ColladaExporter::ColladaMaterials::ColladaEffects::write(const SurfaceStyle& style) { |
| 121 | + openEffect(style.Name() + "-fx"); |
| 122 | + COLLADASW::EffectProfile effect(mSW); |
| 123 | + effect.setShaderType(COLLADASW::EffectProfile::LAMBERT); |
| 124 | + effect.setDiffuse(COLLADASW::ColorOrTexture(COLLADASW::Color(style.Diffuse().R(),style.Diffuse().G(),style.Diffuse().B()))); |
| 125 | + addEffectProfile(effect); |
| 126 | +} |
| 127 | + |
| 128 | +void ColladaSerializer::ColladaExporter::ColladaMaterials::ColladaEffects::close() { |
| 129 | + closeLibrary(); |
| 130 | +} |
| 131 | + |
| 132 | +void ColladaSerializer::ColladaExporter::ColladaMaterials::add(const SurfaceStyle& style) { |
| 133 | + if (!contains(style.Name())) { |
| 134 | + effects.write(style); |
| 135 | + surface_styles.push_back(style); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +bool ColladaSerializer::ColladaExporter::ColladaMaterials::contains(const std::string& name) { |
| 140 | + for (auto it = surface_styles.begin(); it != surface_styles.end(); ++it) { |
| 141 | + if (it->Name() == name) return true; |
| 142 | + } |
| 143 | + return false; |
| 144 | +} |
| 145 | + |
| 146 | +void ColladaSerializer::ColladaExporter::ColladaMaterials::write() { |
| 147 | + effects.close(); |
| 148 | + for (auto it = surface_styles.begin(); it != surface_styles.end(); ++it) { |
| 149 | + const std::string& material_name = it->Name(); |
| 150 | + openMaterial(material_name); |
| 151 | + addInstanceEffect("#" + material_name + "-fx"); |
| 152 | + closeLibrary(); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +void ColladaSerializer::ColladaExporter::startDocument() { |
| 157 | + stream.startDocument(); |
| 158 | + |
| 159 | + COLLADASW::Asset asset(&stream); |
| 160 | + asset.getContributor().mAuthoringTool = std::string("IfcOpenShell ") + IFCOPENSHELL_VERSION; |
| 161 | + // TODO: Get the appropriate unit from the IFC file. |
| 162 | + asset.setUnit("meter", 1.0); |
| 163 | + asset.setUpAxisType(COLLADASW::Asset::Z_UP); |
| 164 | + asset.add(); |
| 165 | +} |
| 166 | + |
| 167 | +void ColladaSerializer::ColladaExporter::writeTesselated(const std::string& type, int obj_id, const std::vector<float>& matrix, const std::vector<float>& vertices, const std::vector<float>& normals, const std::vector<int>& indices) { |
| 168 | + if (!materials.contains(type)) materials.add(GetDefaultMaterial(type)); |
| 169 | + deferreds.push_back(DeferedObject(type, obj_id, matrix, vertices, normals, indices)); |
| 170 | +} |
| 171 | + |
| 172 | +void ColladaSerializer::ColladaExporter::endDocument() { |
| 173 | + // In fact due the XML based nature of Collada and its dependency on library nodes, |
| 174 | + // only at this point all objects are written to the stream. |
| 175 | + materials.write(); |
| 176 | + for (auto it = deferreds.begin(); it != deferreds.end(); ++it) { |
| 177 | + std::stringstream ss; ss << "object" << it->obj_id; |
| 178 | + const std::string object_id = ss.str(); |
| 179 | + geometries.write(object_id, it->vertices, it->normals, it->indices); |
| 180 | + } |
| 181 | + geometries.close(); |
| 182 | + for (auto it = deferreds.begin(); it != deferreds.end(); ++it) { |
| 183 | + std::stringstream ss; ss << "object" << it->obj_id; |
| 184 | + const std::string object_id = ss.str(); |
| 185 | + scene.add(object_id, object_id, object_id, it->type, it->matrix); |
| 186 | + } |
| 187 | + scene.write(); |
| 188 | + stream.endDocument(); |
| 189 | +} |
| 190 | + |
| 191 | +bool ColladaSerializer::ready() { |
| 192 | + return true; |
| 193 | +} |
| 194 | + |
| 195 | +void ColladaSerializer::writeHeader() { |
| 196 | + exporter.startDocument(); |
| 197 | +} |
| 198 | + |
| 199 | +void ColladaSerializer::writeTesselated(const IfcGeomObjects::IfcGeomObject* o) { |
| 200 | + exporter.writeTesselated(o->type, o->id, o->matrix, o->mesh->verts, o->mesh->normals, o->mesh->faces); |
| 201 | +} |
| 202 | + |
| 203 | +void ColladaSerializer::finalize() { |
| 204 | + exporter.endDocument(); |
| 205 | +} |
| 206 | + |
0 commit comments