contrib/mul/mmn/mmn_triplet.h
Go to the documentation of this file.
00001 #ifndef mmn_triplet_h_
00002 #define mmn_triplet_h_
00003 //:
00004 // \file
00005 // \brief Representation of three vertex indices, eg a triangle
00006 // \author Tim Cootes
00007 
00008 
00009 #include <vsl/vsl_binary_io.h>
00010 #include <vcl_iostream.h>
00011 #include <vcl_vector.h>
00012 
00013 //: Representation of three vertex indices, eg a triangle
00014 //  For comparison, order not important.
00015 class mmn_triplet
00016 {
00017  public:
00018   unsigned v1,v2,v3;
00019 
00020   mmn_triplet() :v1(999999),v2(999999),v3(999999) {}
00021   mmn_triplet(unsigned i1, unsigned i2, unsigned i3)
00022     : v1(i1),v2(i2),v3(i3) {}
00023 
00024   //: Return smallest node index
00025   unsigned min_v() const
00026   {
00027     if (v1<v2) return v1<v3?v1:v3;
00028     else       return v2<v3?v2:v3;
00029   }
00030 
00031   //: Return middle node index
00032   unsigned mid_v() const
00033   {
00034     if (v1<v2)
00035     {
00036       if (v2<v3) return v2;          // 1-2-3
00037       else       return v3<v1?v1:v3; // 3-1-2 or 1-3-2
00038     }
00039     if (v1<v3) return v1;          // 2-1-3
00040     else       return v3<v2?v2:v3; // 3-2-1 or 2-3-1
00041  }
00042 
00043   //: Return largest node index
00044   unsigned max_v() const
00045   {
00046     if (v1>v2) return v1>v3?v1:v3;
00047     else       return v2>v3?v2:v3;
00048   }
00049 };
00050 
00051 inline bool operator==(const mmn_triplet& t1, const mmn_triplet& t2)
00052 {
00053   if (t1.v1==t2.v1)
00054   {
00055     if (t1.v2==t2.v2) return t1.v3==t2.v3;  // Order 1-2-3
00056     if (t1.v2==t2.v3) return t1.v3==t2.v2;  // Order 1-3-2
00057     return false;
00058   }
00059   if (t1.v1==t2.v2)
00060   {
00061     if (t1.v2==t2.v1) return t1.v3==t2.v3;  // Order 2-1-3
00062     if (t1.v2==t2.v3) return t1.v3==t2.v1;  // Order 3-1-2
00063     return false;
00064   }
00065   if (t1.v1==t2.v3)
00066   {
00067     if (t1.v2==t2.v1) return t1.v3==t2.v2;  // Order 2-3-1
00068     if (t1.v2==t2.v2) return t1.v3==t2.v1;  // Order 3-2-1
00069     return false;
00070   }
00071   return false;
00072 }
00073 
00074 inline bool operator!=(const mmn_triplet& t1, const mmn_triplet& t2)
00075 { return !(t1==t2); }
00076 
00077 inline vcl_ostream& operator<<(vcl_ostream& os, const mmn_triplet& t)
00078 {
00079   return os<<'{'<<t.v1<<','<<t.v2<<','<<t.v3<<'}';
00080 }
00081 
00082 inline void vsl_b_write(vsl_b_ostream& bfs, const mmn_triplet& t)
00083 {
00084   vsl_b_write(bfs,t.v1);
00085   vsl_b_write(bfs,t.v2);
00086   vsl_b_write(bfs,t.v3);
00087 }
00088 
00089 inline void vsl_b_read(vsl_b_istream& bfs, mmn_triplet& t)
00090 {
00091   vsl_b_read(bfs,t.v1);
00092   vsl_b_read(bfs,t.v2);
00093   vsl_b_read(bfs,t.v3);
00094 }
00095 
00096 inline void vsl_b_write(vsl_b_ostream& bfs, const vcl_vector<mmn_triplet>& a)
00097 {
00098   short version_no = 1;
00099   vsl_b_write(bfs,version_no);
00100   vsl_b_write(bfs,a.size());
00101   for (unsigned i=0;i<a.size();++i) vsl_b_write(bfs,a[i]);
00102 }
00103 
00104 inline void vsl_b_read(vsl_b_istream& bfs, vcl_vector<mmn_triplet>& a)
00105 {
00106   if (!bfs) return;
00107   short version;
00108   vsl_b_read(bfs,version);
00109   unsigned n;
00110   switch (version)
00111   {
00112     case (1):
00113       vsl_b_read(bfs,n);
00114       a.resize(n);
00115       for (unsigned i=0;i<n;++i) vsl_b_read(bfs,a[i]);
00116       break;
00117     default:
00118       vcl_cerr << "I/O ERROR: vsl_b_read(bfs,vector<mmn_triplet>)\n"
00119                << "           Unknown version number "<< version << '\n';
00120       bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00121       return;
00122   }
00123 }
00124 
00125 
00126 #endif // mmn_triplet_h_