bool V_NeuronSWC_list::reverse() { int oldnseg = seg.size(); bool res = false; //must first decompose to simple segments { seg = merge_V_NeuronSWC_list(*this).decompose(); } for (int i=0; i<seg.size(); i++) { res = reverse_V_NeuronSWC_inplace(seg[i]); if (!res) break; } // keep old segment number if (oldnseg==1) { merge(); } return res; }
V_NeuronSWC segmentPruning(V_NeuronSWC in_swc) { // map swc's index --> vector's index & in/out link // code from function of "decompose_V_NeuronSWC" by RZC // construct link map, Link_Map link_map = get_link_map(in_swc); /*qDebug*/("link_map is created."); vector <V_NeuronSWC> out_swc_segs; out_swc_segs.clear(); // nchild as processed counter for (V3DLONG i=0; i<in_swc.row.size(); i++) { V_NeuronSWC_unit & cur_node = in_swc.row[i]; Node_Link & nodelink = link_map[V3DLONG(cur_node.n)]; cur_node.nchild = nodelink.nlink; //qDebug("#%d nlink = %d, in %d, out %d", V3DLONG(cur_node.n), nodelink.nlink, nodelink.in_link.size(), nodelink.out_link.size()); } for (;;) { // check is all nodes processed V3DLONG n_removed = 0; for (V3DLONG i=0; i<in_swc.row.size(); i++) { V_NeuronSWC_unit & cur_node = in_swc.row[i]; if (cur_node.nchild <=0) // is removed node ////// count down to 0 n_removed ++; } if (n_removed >= in_swc.row.size()) { //qDebug("split_V_NeuronSWC_segs is done."); break; //over, all nodes have been labeled to remove } // find a tip/out-branch/pure-out point as start point V3DLONG istart = -1; V3DLONG n_left = 0; V3DLONG i_left = -1; for (V3DLONG i=0; i<in_swc.row.size(); i++) { V_NeuronSWC_unit & cur_node = in_swc.row[i]; Node_Link & nodelink = link_map[V3DLONG(cur_node.n)]; if (cur_node.nchild <=0) continue; //skip removed point n_left++; //left valid point i_left = i; if ((nodelink.nlink ==1 && nodelink.in_link.size()==0) // tip point (include single point) || (nodelink.nlink >2 && nodelink.out_link.size() >0) // out-branch point || (nodelink.nlink ==2 && nodelink.in_link.size()==0)) // pure-out point { istart = i; qDebug("start from #%d", V3DLONG(cur_node.n)); break; //find a start point } } if (istart <0) //not find a start point { if (n_left) { qDebug("split_V_NeuronSWC_segs cann't find start point (left %d points)", n_left); istart = i_left; } else { qDebug("split_V_NeuronSWC_segs finished."); break; } } // extract a simple segment V_NeuronSWC new_seg; new_seg.clear(); //qDebug("decompose_V_NeuronSWC_segs: segment from node #%d", j); float segLen = 0; bool isTip = false; V3DLONG inext = istart; for (V3DLONG n=1; inext>=0; n++) { V_NeuronSWC_unit & cur_node = in_swc.row[inext]; Node_Link & nodelink = link_map[V3DLONG(cur_node.n)]; //qDebug(" link #%d", V3DLONG(cur_node.n)); V_NeuronSWC_unit new_node = cur_node; new_node.n = n; new_node.parent = n+1; // link order as original order new_seg.row.push_back(new_node); if (nodelink.nlink ==1 && nodelink.in_link.size()==0) { // tip point isTip = true; cur_node.nchild --; break; } if(cur_node.parent < 0 ) // root point //////////////////////////// { //qDebug("decompose_V_NeuronSWC_segs: segment end at root #%d", V3DLONG(cur_node.n)); cur_node.nchild --; break; //over, a simple segment } else if (n>1 && nodelink.nlink >2) // branch point (in link_map) /////////// { //qDebug("decompose_V_NeuronSWC_segs: segment end at branch #%d", V3DLONG(cur_node.n)); cur_node.nchild --; break; //over, a simple segment } else if (n>1 && inext==istart) // i_left point (a loop) /////////// { //qDebug("decompose_V_NeuronSWC_segs: segment end at branch #%d", V3DLONG(cur_node.n)); cur_node.nchild --; break; //over, a simple segment } else //(nodelink.nlink==2) // path node /////////////////////////////////////////////// { //qDebug("decompose_V_NeuronSWC_segs: node #%d", j); V3DLONG parent = cur_node.parent; inext = link_map[parent].i; //// next point in seg cur_node.nchild = -1; // label to remove } } if ( isTip ) { // measure length segLen = 0; float preX = new_seg.row.at(0).x; float preY = new_seg.row.at(0).y; float preZ = new_seg.row.at(0).z; for (int i = 1; i < new_seg.row.size(); i++ ) { float curX = new_seg.row.at(i).x; float curY = new_seg.row.at(i).y; float curZ = new_seg.row.at(i).z; segLen += sqrt( (preX - curX)*(preX - curX) + (preY - curY)*(preY - curY) +(preZ - curZ)*(preZ - curZ) ); } // compare the length and radius of parent branch point if ( segLen < 2 * in_swc.row[istart].r ) { continue; } } if (new_seg.row.size()>0 )//>=2)//? single point { new_seg.row[new_seg.row.size()-1].parent = -1; // set segment end char buf[10]; new_seg.name = sprintf(buf,"%d", out_swc_segs.size()+1); new_seg.b_linegraph=true; //donot forget to do this //#################### out_swc_segs.push_back(new_seg); } } // merge the segments V_NeuronSWC_list swcList; swcList.seg = out_swc_segs; V_NeuronSWC out_swc = merge_V_NeuronSWC_list (swcList); return out_swc; }
void V_NeuronSWC_list::merge() { V_NeuronSWC new_seg = merge_V_NeuronSWC_list(*this); clear(); append(new_seg); }
void swc_to_segments(V3DPluginCallback &callback, QWidget *parent, int method_code) { NeuronTree neuron; QString filename; QStringList filenames; /////////////////////////////////////////////////////// if (method_code ==1) { filenames = QFileDialog::getOpenFileNames(0, 0,"","Supported file (*.swc)" ";;Neuron structure(*.swc)",0,0); if(filenames.isEmpty()) { v3d_msg("You don't have any SWC file open in the main window."); return; } NeuronSWC *p_cur=0; QDir dir; for (V3DLONG i = 0; i < filenames.size();i++)//////re-search the bounding box { filename = filenames[i]; QString swcname = QFileInfo(filename).fileName(); QString curFilePath = QFileInfo(filename).path(); qDebug()<<"filenames"<<"name"<<"path"<<filename<<swcname<<curFilePath; dir=QFileInfo(filename).dir(); QString dirname= swcname + "_seg"; dir.mkdir(dirname); if (filename.size()>0) { neuron = readSWC_file(filename); //V3DLONG seg_id, nodeinseg_id; QList <NeuronSWC> listNeuron; QString file= neuron.file; //QList<NeuronSWC> & qlist = neuron.listNeuron; V_NeuronSWC_list NeuronList = NeuronTree__2__V_NeuronSWC_list(&neuron); V_NeuronSWC seg = merge_V_NeuronSWC_list(NeuronList); seg.name = NeuronList.name; seg.file = NeuronList.file; QString swcfile; for(int n =0; n < NeuronList.seg.size(); n++) { NeuronTree SS; QList <NeuronSWC> listNeuron; QHash <int, int> hashNeuron; listNeuron.clear(); hashNeuron.clear(); for (V3DLONG i=0;i< NeuronList.seg.at(n).row.size();i++) { NeuronSWC v; v.n = NeuronList.seg.at(n).row.at(i).data[0]; v.type = NeuronList.seg.at(n).row.at(i).data[1]; v.x = NeuronList.seg.at(n).row.at(i).data[2]; v.y = NeuronList.seg.at(n).row.at(i).data[3]; v.z = NeuronList.seg.at(n).row.at(i).data[4]; v.r = NeuronList.seg.at(n).row.at(i).data[5]; v.pn = NeuronList.seg.at(n).row.at(i).data[6]; v.seg_id = seg.row.at(n).seg_id; v.nodeinseg_id = seg.row.at(n).nodeinseg_id; listNeuron.append(v); hashNeuron.insert(v.n, listNeuron.size()-1); } SS.n = -1; SS.color = XYZW(seg.color_uc[0],seg.color_uc[1],seg.color_uc[2],seg.color_uc[3]); SS.on = true; SS.listNeuron = listNeuron; SS.hashNeuron = hashNeuron; SS.name = seg.name.c_str(); SS.file = seg.file.c_str(); swcfile.sprintf("%d",n); QString curImageName1 = curFilePath +"/"+swcname + "_seg" +"/" + swcname + swcfile +".swc"; qDebug()<<"curImage"<<curImageName1; writeSWC_file(curImageName1,SS); //printf("NeuronListsize=%ld qlistsize=%ld num=%ld\n",a,qlist.size(),num); //printf("name=%ls fila=%ls n=%ld\n",name,file,n); } } else { v3d_msg("You don't have any SWC file open in the main window."); return; } } } }