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