Merge branch 'feature/hierarchy' of https://github.com/mitmadness/IfcOpenShell into feature/hierarchy

# Conflicts:
#	src/ifcconvert/ColladaSerializer.cpp
This commit is contained in:
Balleux Benjamin
2017-04-19 15:09:59 +02:00
3 changed files with 88 additions and 28 deletions
+51 -19
View File
@@ -220,22 +220,38 @@ void ColladaSerializer::ColladaExporter::ColladaScene::add(
node.end(); node.end();
} }
void ColladaSerializer::ColladaExporter::ColladaScene::addParent(const std::string& node_name){ void ColladaSerializer::ColladaExporter::ColladaScene::addParent(const IfcGeom::Element<real_t>& parent){
if (!scene_opened) { if (!scene_opened) {
openVisualScene(scene_id); openVisualScene(scene_id);
scene_opened = true; scene_opened = true;
} }
const std::vector<real_t> matrix = parent.transformation().matrix().data();
double matrix_array[4][4] = {
{ (double)matrix[0], (double)matrix[3], (double)matrix[6], (double)matrix[9] },
{ (double)matrix[1], (double)matrix[4], (double)matrix[7], (double)matrix[10] },
{ (double)matrix[2], (double)matrix[5], (double)matrix[8], (double)matrix[11] },
{ 0, 0, 0, 1 }
};
matrix_array[0][3] += serializer->settings().offset[0];
matrix_array[1][3] += serializer->settings().offset[1];
matrix_array[2][3] += serializer->settings().offset[2];
const std::string id = "representation-" + boost::lexical_cast<std::string>(parent.id());
current_node = new COLLADASW::Node(mSW); current_node = new COLLADASW::Node(mSW);
current_node->setNodeId(node_name); current_node->setNodeId(id);
current_node->setNodeName(node_name); current_node->setNodeName(parent.name());
current_node->addMatrix(matrix_array);
current_node->setType(COLLADASW::Node::NODE); current_node->setType(COLLADASW::Node::NODE);
current_node->start(); current_node->start();
} }
void ColladaSerializer::ColladaExporter::ColladaScene::closeParent(){ void ColladaSerializer::ColladaExporter::ColladaScene::closeParent(){
if (current_node != NULL) { current_node->end(); } if (current_node != NULL) { std::cout << "current node is not null\n"; current_node->end(); }
} }
void ColladaSerializer::ColladaExporter::ColladaScene::write() { void ColladaSerializer::ColladaExporter::ColladaScene::write() {
@@ -339,7 +355,7 @@ void ColladaSerializer::ColladaExporter::write(const IfcGeom::TriangulationEleme
deferreds.push_back( deferreds.push_back(
DeferredObject(name, representation_id, o->type(), o->transformation().matrix().data(), mesh.verts(), mesh.normals(), DeferredObject(name, representation_id, o->type(), o->transformation().matrix().data(), mesh.verts(), mesh.normals(),
mesh.faces(), mesh.edges(), mesh.material_ids(), mesh.materials(), material_references, mesh.uvs(), "") mesh.faces(), mesh.edges(), mesh.material_ids(), mesh.materials(), material_references, mesh.uvs())
); );
} }
@@ -361,20 +377,24 @@ void ColladaSerializer::ColladaExporter::write(const IfcGeom::TriangulationEleme
material_references.push_back(material_name); material_references.push_back(material_name);
} }
const std::string parent_name = (serializer->settings().get(SerializerSettings::USE_ELEMENT_HIERARCHY) ? DeferredObject defered = (serializer->settings().get(SerializerSettings::USE_ELEMENT_HIERARCHY) ?
parent->name() : "");
deferreds.push_back(
DeferredObject(name, representation_id, o->type(), o->transformation().matrix().data(), mesh.verts(), mesh.normals(), DeferredObject(name, representation_id, o->type(), o->transformation().matrix().data(), mesh.verts(), mesh.normals(),
mesh.faces(), mesh.edges(), mesh.material_ids(), mesh.materials(), material_references, mesh.uvs(), parent_name) mesh.faces(), mesh.edges(), mesh.material_ids(), mesh.materials(), material_references, mesh.uvs(), *parent) :
); DeferredObject(name, representation_id, o->type(), o->transformation().matrix().data(), mesh.verts(), mesh.normals(),
mesh.faces(), mesh.edges(), mesh.material_ids(), mesh.materials(), material_references, mesh.uvs()));
deferreds.push_back(defered);
} }
void ColladaSerializer::ColladaExporter::endDocument() { void ColladaSerializer::ColladaExporter::endDocument() {
// In fact due the XML based nature of Collada and its dependency on library nodes, // In fact due the XML based nature of Collada and its dependency on library nodes,
// only at this point all objects are written to the stream. // only at this point all objects are written to the stream.
std::cout << "starting end document\n";
materials.write(); materials.write();
std::set<std::string> geometries_written; std::set<std::string> geometries_written;
std::cout << "sorting defered list...\n";
std::sort(deferreds.begin(), deferreds.end()); std::sort(deferreds.begin(), deferreds.end());
std::cout << "done\n";
for (std::vector<DeferredObject>::const_iterator it = deferreds.begin(); it != deferreds.end(); ++it) { for (std::vector<DeferredObject>::const_iterator it = deferreds.begin(); it != deferreds.end(); ++it) {
if (geometries_written.find(it->representation_id) != geometries_written.end()) { if (geometries_written.find(it->representation_id) != geometries_written.end()) {
continue; continue;
@@ -383,27 +403,39 @@ void ColladaSerializer::ColladaExporter::endDocument() {
geometries.write(it->representation_id, it->type, it->vertices, it->normals, it->faces, it->edges, it->material_ids, it->materials, it->uvs); geometries.write(it->representation_id, it->type, it->vertices, it->normals, it->faces, it->edges, it->material_ids, it->materials, it->uvs);
} }
geometries.close(); geometries.close();
std::string parent_name = ""; int parent_id = -1;
bool is_parent_empty = true; bool is_parent_empty = true;
for (std::vector<DeferredObject>::const_iterator it = deferreds.begin(); it != deferreds.end(); ++it) { for (std::vector<DeferredObject>::const_iterator it = deferreds.begin(); it != deferreds.end(); ++it)
{
const std::string object_name = it->unique_id; const std::string object_name = it->unique_id;
//std::cout << "working on " << it->unique_id;
if (parent_name != it->parent) //std::cout << (it->parent == NULL ? " parent is null\n" : "parent is not null\n");
if (it->parent != NULL && parent_id != it->parent->id())
{ {
std::cout << "parent description : \n --- Type : " << it->parent->type() << " \n --- Name : " << it->parent->name() << "\n";
//std::cout << "parent is not null. Id : " << it->parent->id() << '\n';
//std::cout << "test if parent is empty ... \n";
if (!is_parent_empty) if (!is_parent_empty)
{ {
//std::cout << "close parent1 ... \n";
scene.closeParent(); scene.closeParent();
//std::cout << "done\n";
} }
parent_name = it->parent; //std::cout << "get parent id .. \n";
scene.addParent(parent_name); parent_id = it->parent->id();
//std::cout << "add parent to scene .. \n";
scene.addParent(*it->parent);
//std::cout << "done \n";
is_parent_empty = false; is_parent_empty = false;
} }
//std::cout << "add node to scene ... \n";
/// @todo redundant information using ID as both ID and Name, maybe omit Name or allow specifying what would be used as the name /// @todo redundant information using ID as both ID and Name, maybe omit Name or allow specifying what would be used as the name
scene.add(object_name, object_name, it->representation_id, it->material_references, it->matrix); scene.add(object_name, object_name, it->representation_id, it->material_references, it->matrix);
//std::cout << "done \n";
} }
std::cout << "close parent2 : \n";
scene.closeParent(); if (!is_parent_empty) scene.closeParent();
std::cout << "write : \n";
scene.write(); scene.write();
stream.endDocument(); stream.endDocument();
} }
+34 -6
View File
@@ -89,7 +89,7 @@ private:
{} {}
void add(const std::string& node_id, const std::string& node_name, const std::string& geom_name, void add(const std::string& node_id, const std::string& node_name, const std::string& geom_name,
const std::vector<std::string>& material_ids, const std::vector<real_t>& matrix); const std::vector<std::string>& material_ids, const std::vector<real_t>& matrix);
void addParent(const std::string& node_name); void addParent(const IfcGeom::Element<real_t>& parent);
void closeParent(); void closeParent();
void write(); void write();
ColladaSerializer *serializer; ColladaSerializer *serializer;
@@ -127,7 +127,14 @@ private:
class DeferredObject { class DeferredObject {
friend bool operator < (const DeferredObject & def_obj1, const DeferredObject & def_obj2) friend bool operator < (const DeferredObject & def_obj1, const DeferredObject & def_obj2)
{ {
return (def_obj1.parent < def_obj2.parent ? true : false); const IfcGeom::Element<real_t>* parent1 = def_obj1.parent;
const IfcGeom::Element<real_t>* parent2 = def_obj2.parent;
if (parent1 == NULL || parent2 == NULL)
{
return (def_obj1.unique_id < def_obj2.unique_id ? true : false);
}
else return parent1->name() < parent2->name() ? true : false;
} }
public: public:
std::string unique_id, representation_id, type; std::string unique_id, representation_id, type;
@@ -140,11 +147,11 @@ private:
std::vector<IfcGeom::Material> materials; std::vector<IfcGeom::Material> materials;
std::vector<std::string> material_references; std::vector<std::string> material_references;
std::vector<real_t> uvs; std::vector<real_t> uvs;
std::string parent; const IfcGeom::Element<real_t>* parent;
DeferredObject(const std::string& unique_id, const std::string& representation_id, const std::string& type, const std::vector<real_t>& matrix, DeferredObject(const std::string& unique_id, const std::string& representation_id, const std::string& type, const std::vector<real_t>& matrix,
const std::vector<real_t>& vertices, const std::vector<real_t>& normals, const std::vector<int>& faces, const std::vector<real_t>& vertices, const std::vector<real_t>& normals, const std::vector<int>& faces,
const std::vector<int>& edges, const std::vector<int>& material_ids, const std::vector<IfcGeom::Material>& materials, const std::vector<int>& edges, const std::vector<int>& material_ids, const std::vector<IfcGeom::Material>& materials,
const std::vector<std::string>& material_references, const std::vector<real_t>& uvs, const std::string& parent) const std::vector<std::string>& material_references, const std::vector<real_t>& uvs, const IfcGeom::Element<real_t>& _parent)
: unique_id(unique_id) : unique_id(unique_id)
, representation_id(representation_id) , representation_id(representation_id)
, type(type) , type(type)
@@ -157,8 +164,29 @@ private:
, materials(materials) , materials(materials)
, material_references(material_references) , material_references(material_references)
, uvs(uvs) , uvs(uvs)
, parent(parent) {
{} parent = &_parent;
}
DeferredObject(const std::string& unique_id, const std::string& representation_id, const std::string& type, const std::vector<real_t>& matrix,
const std::vector<real_t>& vertices, const std::vector<real_t>& normals, const std::vector<int>& faces,
const std::vector<int>& edges, const std::vector<int>& material_ids, const std::vector<IfcGeom::Material>& materials,
const std::vector<std::string>& material_references, const std::vector<real_t>& uvs)
: unique_id(unique_id)
, representation_id(representation_id)
, type(type)
, matrix(matrix)
, vertices(vertices)
, normals(normals)
, faces(faces)
, edges(edges)
, material_ids(material_ids)
, materials(materials)
, material_references(material_references)
, uvs(uvs)
{
parent = NULL;
}
}; };
COLLADABU::NativeString filename; COLLADABU::NativeString filename;
COLLADASW::StreamWriter stream; COLLADASW::StreamWriter stream;