QModelIndex DynamicTreeModel::parent( const QModelIndex &index ) const { if ( !index.isValid() ) return QModelIndex(); qint64 searchId = index.internalId(); qint64 parentId = findParentId( searchId ); // Will never happen for valid index, but what the hey... if ( parentId <= 0 ) return QModelIndex(); qint64 grandParentId = findParentId( parentId ); if ( grandParentId < 0 ) grandParentId = 0; int column = 0; QList<qint64> childList = m_childItems.value( grandParentId ).at( column ); int row = childList.indexOf( parentId ); return createIndex( row, column, reinterpret_cast<void *>( parentId ) ); }
QModelIndex DynamicTreeModel::index(int row, int column, const QModelIndex &parent) const { // if (column != 0) // return QModelIndex(); if ( column < 0 || row < 0 ) return QModelIndex(); QList<QList<qint64> > childIdColumns = m_childItems.value(parent.internalId()); const qint64 grandParent = findParentId(parent.internalId()); if (grandParent >= 0) { QList<QList<qint64> > parentTable = m_childItems.value(grandParent); if (parent.column() >= parentTable.size()) qFatal("%s: parent.column() must be less than parentTable.size()", Q_FUNC_INFO); QList<qint64> parentSiblings = parentTable.at(parent.column()); if (parent.row() >= parentSiblings.size()) qFatal("%s: parent.row() must be less than parentSiblings.size()", Q_FUNC_INFO); } if (childIdColumns.size() == 0) return QModelIndex(); if (column >= childIdColumns.size()) return QModelIndex(); QList<qint64> rowIds = childIdColumns.at(column); if ( row >= rowIds.size()) return QModelIndex(); qint64 id = rowIds.at(row); return createIndex(row, column, reinterpret_cast<void *>(id)); }
bool saveKdTreeToDisk (const string filename, const KdTree <T>& kdtree) { if (typeid_data_type_map_inverse.size () == 0) { for (auto element : typeid_data_type_map) { typeid_data_type_map_inverse.insert (make_pair (element.second, element.first)); } } string data_type = typeid (T).name (); cout << "data type " << data_type << endl; uint8_t c_data_type = 0; BEGIN_STRING_SWITCH_(data_type) CASE_(typeid_data_type_map_inverse["float"]) cout << "Data type : float\n"; c_data_type = static_cast <std::uint8_t> (DataType::FLOAT); CASE_(typeid_data_type_map_inverse["double"]) cout << "Data type : double\n"; c_data_type = static_cast <std::uint8_t> (DataType::DOUBLE); CASE_(typeid_data_type_map_inverse["int"]) cout << "Data type : int\n"; c_data_type = static_cast <std::uint8_t> (DataType::INT); CASE_(typeid_data_type_map_inverse["long"]) cout << "Data type : long\n"; c_data_type = static_cast <std::uint8_t> (DataType::LONG); DEFAULT_ cout << "Data type : unknown\n"; return false; END_STRING_SWITCH_ cout << "Writing to file " << filename << endl; ofstream ofile (filename.c_str (), ios::binary); ofile.write (reinterpret_cast <char*> (&c_data_type), sizeof (c_data_type)); PointSetSharedPtr <T> point_set = kdtree.getPointSet (); size_t n_dimensions = 0; if (point_set) { n_dimensions = point_set->dimensions; } ofile.write (reinterpret_cast <char*> (&n_dimensions), sizeof (n_dimensions)); size_t n_points = 0; if (point_set) { n_points = point_set->size (); } ofile.write (reinterpret_cast <char*> (&n_points), sizeof (n_points)); for (const DataPoint <T>& data_point : point_set->points) { for (const T& val : data_point.data) { ofile.write (reinterpret_cast <const char*> (&val), sizeof (val)); } } const KdTreeNodeSharedPtr <T> root = kdtree.getRootNode (); std::queue <KdTreeNodeSharedPtr <T> > node_queue; node_queue.push (root); int node_cnt = 0; std::queue <int> node_id_queue; node_id_queue.push (node_cnt); std::map <int, ParentChildInfo> child_parent_map; while (not node_queue.empty ()) { const KdTreeNodeSharedPtr <T> node = node_queue.front (); KdTreeDiskNode <T> disk_node; int node_id = node_id_queue.front (); ParentChildInfo parent_child_info = findParentId (node_id, child_parent_map); int parent_id = parent_child_info.parent_id; if (parent_id == -2) { cout << "\nError saving node to disk .. \n"; return false; } disk_node.node_id = node_id; disk_node.parent_id = parent_id; disk_node.left_right = parent_child_info.left_right; //cout << "Node " << disk_node.node_id << " parent " << disk_node.parent_id << " "; //cout << (char) std::toupper (disk_node.left_right) << "\n"; disk_node.leaf_node = (node->leaf_node != NULL); if (!disk_node.leaf_node) { disk_node.sort_dimension = node->sort_dimension; disk_node.threshold = node->threshold; } else { disk_node.index = node->leaf_node->index; disk_node.threshold = -1; } //if (!disk_node.leaf_node) { // node->printCondition (); //} //else { // cout << kdtree.getPointSet ()->points[node->leaf_node->index]; // cout << "\t"; //} ofile.write (reinterpret_cast <char*> (&disk_node), sizeof (disk_node)); //cout << "Writing node " << disk_node << " : offset " << ofile.tellp () << " "; //cout << disk_node.node_id << " " << disk_node.parent_id << " leaf " << disk_node.leaf_node << endl; if (node->left) { node_queue.push (node->left); node_cnt++; node_id_queue.push (node_cnt); ParentChildInfo parent_child_info; parent_child_info.parent_id = node_id; parent_child_info.left_right = 'l'; child_parent_map.insert (make_pair (node_cnt, parent_child_info)); } if (node->right) { node_queue.push (node->right); node_cnt++; node_id_queue.push (node_cnt); ParentChildInfo parent_child_info; parent_child_info.parent_id = node_id; parent_child_info.left_right = 'r'; child_parent_map.insert (make_pair (node_cnt, parent_child_info)); } node_queue.pop (); node_id_queue.pop (); } cout << "Number of kdtree nodes written " << node_cnt << endl; ofile.close (); cout << "\n"; return true; }