| 1 |
/* |
| 2 |
* The MIT License |
| 3 |
|
| 4 |
Conograph (powder auto-indexing program) |
| 5 |
|
| 6 |
Copyright (c) <2012> <Ryoko Oishi-Tomiyasu, KEK> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in |
| 16 |
all copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
THE SOFTWARE. |
| 25 |
* |
| 26 |
*/ |
| 27 |
#ifndef _TreeLattice_hh_ |
| 28 |
#define _TreeLattice_hh_ |
| 29 |
// TreeLattice.hh |
| 30 |
|
| 31 |
#include <ostream> |
| 32 |
#include <fstream> |
| 33 |
#include <assert.h> |
| 34 |
|
| 35 |
#include "../RietveldAnalysisTypes.hh" |
| 36 |
#include "NodeB.hh" |
| 37 |
#include "VecDat3.hh" |
| 38 |
|
| 39 |
class TreeLattice |
| 40 |
{ |
| 41 |
private: |
| 42 |
// const Int4 m_data_type; |
| 43 |
|
| 44 |
NodeB* m_root; // K1, K2 |
| 45 |
NodeB* m_root_on_left; // K2, K3 |
| 46 |
NodeB* m_root_on_right; // K3, K1 |
| 47 |
|
| 48 |
bool HeadIsTail; |
| 49 |
|
| 50 |
bool m_is_set_sort_criteria; |
| 51 |
Int4 m_count_Q; |
| 52 |
Double m_detS; |
| 53 |
|
| 54 |
// Returns 2 if HeadIsTail is true. |
| 55 |
// Returns 1 if HeadIsTail is false, and m_root, m_root_on_left, m_root_on_right are not NULL. |
| 56 |
// Returns 0 otherwise. |
| 57 |
// inline Int4 SuperBasis() const; |
| 58 |
|
| 59 |
// Sets m_detS. |
| 60 |
void setAreaSquare(); |
| 61 |
// Sets m_count_Q. |
| 62 |
void setCountOfQ(); |
| 63 |
|
| 64 |
public: |
| 65 |
TreeLattice(); |
| 66 |
TreeLattice(const TreeLattice& rhs); |
| 67 |
~TreeLattice(); |
| 68 |
|
| 69 |
inline const NodeB& Root() const; |
| 70 |
|
| 71 |
inline void setRoot(const Int4&, const Int4&); |
| 72 |
inline void setRoot(const NodeB&, const NodeB&); |
| 73 |
|
| 74 |
inline void setRootEqualUpper(); |
| 75 |
inline void setRootOnLeftBranch(const NodeB& node); |
| 76 |
inline void setRootOnRightBranch(const NodeB& node); |
| 77 |
|
| 78 |
inline void setSortingCriteria() { this->setCountOfQ(); this->setAreaSquare(); m_is_set_sort_criteria = true; }; |
| 79 |
inline Int4 putCountOfQ() const{ assert(m_is_set_sort_criteria); return m_count_Q; }; |
| 80 |
inline const Double& putAreaSquare() const{ assert(m_is_set_sort_criteria); return m_detS; }; |
| 81 |
|
| 82 |
inline void swapBranch(); |
| 83 |
|
| 84 |
TreeLattice& operator=(const TreeLattice& rhs); |
| 85 |
|
| 86 |
void clear(); |
| 87 |
|
| 88 |
// On output, index_tray is sorted into ascending order, any elements are not repeated. |
| 89 |
void putRootBuds(set<Bud>& tray) const; |
| 90 |
void putBud(set<Bud>& tray) const; |
| 91 |
|
| 92 |
void putQuadraticForm(SymMat<VCData>& Q) const; |
| 93 |
// bool putQuadraticForm(SymMat<Double>& Q, multimap<Int4, VecDat3<Int4> >& qindex_hkl) const; |
| 94 |
|
| 95 |
void print(ostream&, const Double& minQ, const Double& maxQ) const; |
| 96 |
|
| 97 |
template <class Iterator> |
| 98 |
static void print(const string&, const Iterator&, const Iterator&); |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
//inline Int4 TreeLattice::SuperBasis() const |
| 103 |
//{ |
| 104 |
// if( HeadIsTail ) return 2; |
| 105 |
// if( m_root != NULL && m_root_on_left != NULL && m_root_on_right != NULL ) return 1; |
| 106 |
// return 0; |
| 107 |
//} |
| 108 |
|
| 109 |
|
| 110 |
inline const NodeB& TreeLattice::Root() const |
| 111 |
{ |
| 112 |
if( m_root == NULL ) |
| 113 |
{ |
| 114 |
throw ZErrorMessage(ZErrorNullPointer, __FILE__, __LINE__, __FUNCTION__); |
| 115 |
} |
| 116 |
return *m_root; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
inline void TreeLattice::setRoot(const Int4& K1, const Int4& K2) |
| 121 |
{ |
| 122 |
this->clear(); |
| 123 |
m_root = new NodeB(K1, K2); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
inline void TreeLattice::setRoot(const NodeB& lhs, const NodeB& rhs) |
| 128 |
{ |
| 129 |
this->clear(); |
| 130 |
assert( lhs.Right() == rhs.Right() ); |
| 131 |
|
| 132 |
m_root = new NodeB(lhs, rhs); |
| 133 |
} |
| 134 |
|
| 135 |
inline void TreeLattice::setRootEqualUpper() |
| 136 |
{ |
| 137 |
delete m_root_on_left; |
| 138 |
m_root_on_left = NULL; |
| 139 |
delete m_root_on_right; |
| 140 |
m_root_on_right = NULL; |
| 141 |
|
| 142 |
if( m_root->IsBud() ) HeadIsTail = false; |
| 143 |
else HeadIsTail = true; |
| 144 |
m_is_set_sort_criteria = false; |
| 145 |
} |
| 146 |
|
| 147 |
inline void TreeLattice::setRootOnLeftBranch(const NodeB& nodex) |
| 148 |
{ |
| 149 |
assert( m_root != NULL ); |
| 150 |
|
| 151 |
delete m_root_on_left; |
| 152 |
m_root_on_left = NULL; |
| 153 |
|
| 154 |
if( nodex.Left() == m_root->Right() ) |
| 155 |
{ |
| 156 |
if( m_root_on_right == NULL ) |
| 157 |
{ |
| 158 |
HeadIsTail = false; |
| 159 |
m_root_on_left = new NodeB(nodex); |
| 160 |
m_root_on_right = new NodeB(m_root_on_left->Right(), m_root->Left()); |
| 161 |
} |
| 162 |
else if( nodex.Right() == m_root_on_right->Left() ) |
| 163 |
{ |
| 164 |
m_root_on_left = new NodeB(nodex); |
| 165 |
} |
| 166 |
else assert( false ); |
| 167 |
} |
| 168 |
else if( nodex.Right() == m_root->Right() ) |
| 169 |
{ |
| 170 |
if( m_root_on_right == NULL ) |
| 171 |
{ |
| 172 |
HeadIsTail = false; |
| 173 |
m_root_on_left = new NodeB(nodex); |
| 174 |
m_root_on_left->swapBranch(); |
| 175 |
m_root_on_right = new NodeB(m_root_on_left->Right(), m_root->Left()); |
| 176 |
} |
| 177 |
else if( nodex.Left() == m_root_on_right->Left() ) |
| 178 |
{ |
| 179 |
m_root_on_left = new NodeB(nodex); |
| 180 |
m_root_on_left->swapBranch(); |
| 181 |
} |
| 182 |
else assert( false ); |
| 183 |
} |
| 184 |
else assert( false ); |
| 185 |
|
| 186 |
m_is_set_sort_criteria = false; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
inline void TreeLattice::setRootOnRightBranch(const NodeB& nodex) |
| 191 |
{ |
| 192 |
assert( m_root != NULL ); |
| 193 |
|
| 194 |
delete m_root_on_right; |
| 195 |
m_root_on_right = NULL; |
| 196 |
|
| 197 |
if( nodex.Right() == m_root->Left() ) |
| 198 |
{ |
| 199 |
if( m_root_on_left == NULL ) |
| 200 |
{ |
| 201 |
HeadIsTail = false; |
| 202 |
m_root_on_right = new NodeB(nodex); |
| 203 |
m_root_on_left = new NodeB(m_root->Right(), m_root_on_right->Left()); |
| 204 |
} |
| 205 |
else if( nodex.Left() == m_root_on_left->Right() ) |
| 206 |
{ |
| 207 |
m_root_on_right = new NodeB(nodex); |
| 208 |
} |
| 209 |
else assert( false ); |
| 210 |
} |
| 211 |
else if( nodex.Left() == m_root->Left() ) |
| 212 |
{ |
| 213 |
if( m_root_on_left == NULL ) |
| 214 |
{ |
| 215 |
HeadIsTail = false; |
| 216 |
m_root_on_right = new NodeB(nodex); |
| 217 |
m_root_on_right->swapBranch(); |
| 218 |
m_root_on_left = new NodeB(m_root->Right(), m_root_on_right->Left()); |
| 219 |
} |
| 220 |
else if( nodex.Right() == m_root_on_left->Right() ) |
| 221 |
{ |
| 222 |
m_root_on_right = new NodeB(nodex); |
| 223 |
m_root_on_right->swapBranch(); |
| 224 |
} |
| 225 |
else assert( false ); |
| 226 |
} |
| 227 |
else assert( false ); |
| 228 |
|
| 229 |
m_is_set_sort_criteria = false; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
inline bool operator<(const TreeLattice& lhs, const TreeLattice& rhs) |
| 234 |
{ |
| 235 |
if( lhs.putCountOfQ() > rhs.putCountOfQ() ) return true; |
| 236 |
if( lhs.putCountOfQ() < rhs.putCountOfQ() ) return false; |
| 237 |
return lhs.putAreaSquare() < rhs.putAreaSquare(); |
| 238 |
} |
| 239 |
|
| 240 |
inline void TreeLattice::swapBranch() |
| 241 |
{ |
| 242 |
if( m_root != NULL ) m_root->swapBranch(); |
| 243 |
swap(m_root_on_left, m_root_on_right); |
| 244 |
|
| 245 |
if( m_root_on_left != NULL ) m_root_on_left->swapBranch(); |
| 246 |
if( m_root_on_right != NULL ) m_root_on_right->swapBranch(); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
template <class Iterator> |
| 252 |
void TreeLattice::print(const string& fname, |
| 253 |
const Iterator& it_begin, const Iterator& it_end) |
| 254 |
{ |
| 255 |
const vector<QData>& qdata = VCData::putPeakQData(); |
| 256 |
const Double maxQ = qdata.rbegin()->q; // *max_element(Qdata.begin(), Qdata.end()); |
| 257 |
const Double minQ = qdata.begin()->q; // *min_element(Qdata.begin(), Qdata.end()); |
| 258 |
|
| 259 |
ofstream ofs(fname.c_str()); |
| 260 |
|
| 261 |
ofs << "** MaxQ = " << maxQ << endl; |
| 262 |
ofs << "** MinQ = " << minQ << endl; |
| 263 |
ofs << "**" << endl; |
| 264 |
|
| 265 |
// Output |
| 266 |
Int4 index = 1; |
| 267 |
for(Iterator it = it_begin; it!=it_end; it++) |
| 268 |
{ |
| 269 |
ofs << "** Tree_" << index++ << endl; |
| 270 |
it->print(ofs, minQ, maxQ); |
| 271 |
ofs << endl; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
#endif |