Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members Related Pages

# BlumSkeleton.cpp

Go to the documentation of this file.

    00001
    00035 #include "Debug.h"
    00036 #include "BlumSkeleton.h"
    00037 #include <LEDA/d_array.h>
    00038
    00039 void McGillSkeleton::ReadNodesAndEdges(istream& is, DDSkeleton& dds)
    00040 {
    00041         DDSNode* n;
    00042         FluxPoint fp;
    00043         leda_d_array<long, DDSNode*> map;
    00044         int i, j, size, fp_count;
    00045         double lt1, lt2, rt1, rt2;
    00046         void *key, *keyN1, *keyN2;
    00047
    00048         is.read((char*) &size, sizeof(size));
    00049
    00050         for (i = 0; i < size; i++)
    00051         {
    00052                 is.read((char*) &key, sizeof(key));
    00053                 is.read((char*) &fp, sizeof(fp));
    00054
    00055                 n =  new DDSNode(fp);
    00056
    00057                 map[(long)key] = n;
    00058                 dds.addNode(n);
    00059         }
    00060
    00061         is.read((char*) &size, sizeof(size));
    00062
    00063         for (i = 0; i < size; i++)
    00064         {
    00065                 is.read((char*) &keyN1, sizeof(keyN1));
    00066                 is.read((char*) &keyN2, sizeof(keyN2));
    00067
    00068                 is.read((char*) &lt1, sizeof(lt1));
    00069                 is.read((char*) &lt2, sizeof(lt2));
    00070                 is.read((char*) &rt1, sizeof(rt1));
    00071                 is.read((char*) &rt2, sizeof(rt2));
    00072
    00073                 is.read((char*) &fp_count, sizeof(fp_count));
    00074                 FluxPointList fpl;
    00075
    00076                 for (j = 0; j < fp_count; j++)
    00077                 {
    00078                         is.read((char*) &fp, sizeof(fp));
    00079                         fpl.push_back(fp);
    00080                 }
    00081
    00082                 DDSEdge* e = new DDSEdge(&dds, fpl, map[(long)keyN1], map[(long)keyN2]);
    00083
    00084                 if (lt1 != -1 || lt2 != -1) e->setLeftSegment(lt1, lt2);
    00085                 if (rt1 != -1 || rt2 != -1) e->setRightSegment(rt1, rt2);
    00086
    00087                 dds.addEdge(e);
    00088         }
    00089 }
    00090
    00091 void McGillSkeleton::WriteNodesAndEdges(ostream& os, DDSkeleton& dds)
    00092 {
    00093         DDSNodeVect::iterator I;
    00094         DDSEdgeVect::iterator II;
    00095         FluxPointList::iterator III;
    00096
    00097         DDSNodeVect& nodes = dds.getNodes();
    00098         DDSEdgeVect& edges = dds.getEdges();
    00099
    00100         DDSNode *n, *n1, *n2;
    00101         DDSEdge *e;
    00102         FluxPoint fp;
    00103
    00104         int size = nodes.size();
    00105         os.write((char*) &size, sizeof(size));
    00106
    00107         for(I = nodes.begin(); I != nodes.end(); I++)
    00108         {
    00109         n = *I;
    00110                 os.write((char*) &n, sizeof(n));
    00111                 os.write((char*) &n->fp, sizeof(n->fp));
    00112         }
    00113
    00114         size = edges.size();
    00115         os.write((char*) &size, sizeof(size));
    00116
    00117         for(II = edges.begin(); II != edges.end(); II++)
    00118         {
    00119                 e = *II;
    00120                 n1 = e->getN1();
    00121                 n2 = e->getN2();
    00122                 os.write((char*) &n1, sizeof(n1));
    00123                 os.write((char*) &n2, sizeof(n2));
    00124                 os.write((char*) &e->left_t1, sizeof(e->left_t1));
    00125                 os.write((char*) &e->left_t2, sizeof(e->left_t2));
    00126                 os.write((char*) &e->right_t1, sizeof(e->right_t1));
    00127                 os.write((char*) &e->right_t2, sizeof(e->right_t2));
    00128
    00129                 FluxPointList& fpl = e->getFluxPoints();
    00130
    00131                 size = fpl.size();
    00132                 os.write((char*) &size, sizeof(size));
    00133
    00134                 for(III = fpl.begin(); III != fpl.end(); III++)
    00135                 {
    00136                         fp = *III;
    00137                         os.write((char*) &fp, sizeof(fp));
    00138                 }
    00139         }
    00140
    00141 }
    00142
    00143 DDSkeleton* McGillSkeleton::ReadDDSkeleton(istream& is)
    00144 {
    00145         LineSeg* pLineSeg;
    00146         bool closed;
    00147         double xmin, xmax, ymin, ymax;
    00148         int size;
    00149         double startLength;
    00150         Point startPt, endPt;
    00151
    00152         //Read Contour
    00153         is.read((char*) &xmin, sizeof(xmin));
    00154         is.read((char*) &xmax, sizeof(xmax));
    00155         is.read((char*) &ymin, sizeof(ymin));
    00156         is.read((char*) &ymax, sizeof(ymax));
    00157
    00158         std::vector<CurveSeg*> segments;
    00159         is.read((char*) &size, sizeof(size));
    00160
    00161     for(int i = 0; i < size; i++)
    00162         {
    00163                 is.read((char*) &startLength, sizeof(startLength));
    00164                 is.read((char*) &startPt, sizeof(startPt));
    00165                 is.read((char*) &endPt, sizeof(endPt));
    00166
    00167                 pLineSeg = new LineSeg(startPt, endPt, startLength);
    00168                 segments.push_back(pLineSeg);
    00169         }
    00170
    00171         is.read((char*) &closed, sizeof(closed));
    00172         DiscreteSegCurve c(segments, closed);
    00173
    00174         SimpleShape ss(&c, xmin, xmax, ymin, ymax);
    00175         DistanceTransform dt(&ss);
    00176         DivergenceMap dm(dt);
    00177         DDSkeleton* dds = new DDSkeleton(dm);
    00178
    00179         // Read all nodes and edges
    00180         ReadNodesAndEdges(is, *dds);
    00181
    00182         return dds;
    00183 }
    00184
    00185 void McGillSkeleton::WriteDDSkeleton(ostream& os, DDSkeleton& dds)
    00186 {
    00187         SimpleShape* ss = (SimpleShape*)dds.getShape();
    00188         Curve* p = *(ss->getCurves()->begin());
    00189         DiscreteSegCurve* c = dynamic_cast<DiscreteSegCurve*>(p);
    00190         LineSeg* pLineSeg;
    00191         double xmin, xmax, ymin, ymax;
    00192         bool closed;
    00193
    00194         ASSERT(c);
    00195
    00196         //Read Contour
    00197         ss->getBounds(&xmin, &xmax, &ymin, &ymax);
    00198         os.write((char*) &xmin, sizeof(xmin));
    00199         os.write((char*) &xmax, sizeof(xmax));
    00200         os.write((char*) &ymin, sizeof(ymin));
    00201         os.write((char*) &ymax, sizeof(ymax));
    00202
    00203         int size = c->segments.size();
    00204         os.write((char*) &size, sizeof(size));
    00205
    00206         std::vector<CurveSeg*>::iterator I;
    00207         for(I = c->segments.begin(); I != c->segments.end(); I++)
    00208         {
    00209                 pLineSeg = dynamic_cast<LineSeg*>(*I);
    00210                 ASSERT(pLineSeg);
    00211
    00212                 os.write((char*) &pLineSeg->startLength, sizeof(pLineSeg->startLength));
    00213                 os.write((char*) &pLineSeg->startPt, sizeof(pLineSeg->startPt));
    00214                 os.write((char*) &pLineSeg->endPt, sizeof(pLineSeg->endPt));
    00215         }
    00216
    00217         closed = c->isClosed();
    00218         os.write((char*) &closed, sizeof(closed));
    00219
    00220         // Read all nodes and edges
    00221         WriteNodesAndEdges(os, dds);
    00222 }
    00223