00001 //---------------------------------------------------------------------------- 00002 /** @file SgPointSetUtil.cpp 00003 See SgPointSetUtil.h. */ 00004 //---------------------------------------------------------------------------- 00005 00006 #include "SgSystem.h" 00007 #include "SgPointSetUtil.h" 00008 00009 #include <iostream> 00010 #include "SgPointSet.h" 00011 #include "SgWrite.h" 00012 00013 using namespace std; 00014 00015 //---------------------------------------------------------------------------- 00016 00017 SgWritePointSet::SgWritePointSet(const SgPointSet& pointSet, string label, 00018 bool writeSize) 00019 : m_writeSize(writeSize), 00020 m_pointSet(pointSet), 00021 m_label(label) 00022 { 00023 } 00024 00025 ostream& SgWritePointSet::Write(ostream& out) const 00026 { 00027 const size_t charPerLine = 60; 00028 int size = m_pointSet.Size(); 00029 if (m_label != "") 00030 out << SgWriteLabel(m_label); 00031 ostringstream buffer; 00032 if (m_writeSize) 00033 buffer << size; 00034 if (size > 0) 00035 { 00036 if (m_writeSize) 00037 buffer << " "; 00038 for (SgSetIterator point(m_pointSet); point; ++point) 00039 { 00040 if (buffer.str().size() > charPerLine) 00041 { 00042 out << buffer.str() << '\n'; 00043 buffer.str(""); 00044 if (m_label != "") 00045 out << SgWriteLabel(""); 00046 } 00047 buffer << SgWritePoint(*point) << ' '; 00048 } 00049 } 00050 out << buffer.str() << '\n'; 00051 return out; 00052 } 00053 00054 ostream& operator<<(ostream& out, const SgWritePointSet& write) 00055 { 00056 return write.Write(out); 00057 } 00058 00059 ostream& operator<<(ostream& out, const SgPointSet& pointSet) 00060 { 00061 return out << SgWritePointSet(pointSet, ""); 00062 } 00063 00064 //---------------------------------------------------------------------------- 00065 00066 ostream& operator<<(ostream& stream, const SgWritePointSetID& w) 00067 { 00068 const SgPointSet& points = w.Points(); 00069 if (points.IsEmpty()) 00070 stream << "(none)"; 00071 else 00072 { 00073 stream << SgWritePoint(points.Center()) 00074 << ", Size " << points.Size(); 00075 } 00076 return stream; 00077 } 00078 00079 //---------------------------------------------------------------------------- 00080 00081 SgReadPointSet::SgReadPointSet(SgPointSet& pointSet) 00082 : m_pointSet(pointSet) 00083 { 00084 } 00085 00086 istream& SgReadPointSet::Read(istream& in) const 00087 { 00088 string pointstr; 00089 int size; 00090 in >> size; 00091 for (int i = 0; i < size; ++i) 00092 { 00093 // @todo Would be nice to have a full set of Read functions 00094 // but in the meanwhile calc the points here... 00095 in >> pointstr; 00096 if (pointstr.size() < 4) // not pass or null move 00097 { 00098 int col = toupper(pointstr[0]) - 'A' + 1; 00099 int row = toupper(pointstr[1]) - '0'; 00100 if (pointstr.size() == 3) 00101 row = row * 10 + pointstr[2] - '0'; 00102 m_pointSet.Include(SgPointUtil::Pt(col, row)); 00103 } 00104 } 00105 return in; 00106 } 00107 00108 istream& operator>>(istream& in, const SgReadPointSet& read) 00109 { 00110 return read.Read(in); 00111 } 00112 00113 istream& operator>>(istream& in, SgPointSet& pointSet) 00114 { 00115 return in >> SgReadPointSet(pointSet); 00116 } 00117 00118 //---------------------------------------------------------------------------- 00119 /* This function could probably be optimized if used in time-critical code */ 00120 void SgPointSetUtil::Rotate(int rotation, SgPointSet& pointSet, int boardSize) 00121 { 00122 SgPointSet newSet; 00123 for (SgSetIterator it(pointSet); it; ++it) 00124 newSet.Include(SgPointUtil::Rotate(rotation, *it, boardSize)); 00125 pointSet = newSet; 00126 } 00127 00128 //----------------------------------------------------------------------------