| 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 |
#include "NodeB.hh" |
| 28 |
#include "../zerror_type/error_out.hh" |
| 29 |
|
| 30 |
NodeB::NodeB() |
| 31 |
{ |
| 32 |
m_left = -1; |
| 33 |
m_right = -1; |
| 34 |
|
| 35 |
m_left_branch = NULL; |
| 36 |
m_right_branch = NULL; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
NodeB::NodeB(const Int4& i, const Int4& j) |
| 41 |
{ |
| 42 |
m_left = i; |
| 43 |
m_right = j; |
| 44 |
|
| 45 |
m_left_branch = NULL; |
| 46 |
m_right_branch = NULL; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
NodeB::NodeB(const NodeB& lhs, const NodeB& rhs) |
| 51 |
{ |
| 52 |
m_left = lhs.Left(); |
| 53 |
m_right = rhs.Left(); |
| 54 |
assert( lhs.Right() == rhs.Right() ); |
| 55 |
m_left_branch = new NodeB(lhs); |
| 56 |
m_right_branch = new NodeB(rhs); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
NodeB::NodeB(const NodeB& rhs) |
| 61 |
{ |
| 62 |
m_left = rhs.m_left; |
| 63 |
m_right = rhs.m_right; |
| 64 |
|
| 65 |
m_left_branch = NULL; |
| 66 |
m_right_branch = NULL; |
| 67 |
if( rhs.m_left_branch != NULL ) m_left_branch = new NodeB(*rhs.m_left_branch); |
| 68 |
if( rhs.m_right_branch != NULL ) |
| 69 |
{ |
| 70 |
if( rhs.m_left_branch == rhs.m_right_branch ) m_right_branch = m_left_branch; |
| 71 |
else m_right_branch = new NodeB(*rhs.m_right_branch); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
NodeB::~NodeB() |
| 77 |
{ |
| 78 |
delete m_left_branch; |
| 79 |
m_left_branch = NULL; |
| 80 |
|
| 81 |
delete m_right_branch; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
NodeB& NodeB::operator=(const NodeB& rhs) |
| 86 |
{ |
| 87 |
if (this != &rhs) |
| 88 |
{ |
| 89 |
m_left = rhs.m_left; |
| 90 |
m_right = rhs.m_right; |
| 91 |
|
| 92 |
this->cutBranch(); |
| 93 |
if( rhs.m_left_branch != NULL ) m_left_branch = new NodeB(*rhs.m_left_branch); |
| 94 |
if( rhs.m_right_branch != NULL ) |
| 95 |
{ |
| 96 |
if( rhs.m_left_branch == rhs.m_right_branch ) m_right_branch = m_left_branch; |
| 97 |
else m_right_branch = new NodeB(*rhs.m_right_branch); |
| 98 |
} |
| 99 |
} |
| 100 |
return *this; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
void NodeB::setBranch(const Int4& index) |
| 105 |
{ |
| 106 |
cutBranch(); |
| 107 |
|
| 108 |
m_left_branch = new NodeB(m_left, index); |
| 109 |
if( m_left == m_right ) m_right_branch = m_left_branch; |
| 110 |
else m_right_branch = new NodeB(m_right, index); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
void NodeB::cutBranch() |
| 115 |
{ |
| 116 |
delete m_left_branch; |
| 117 |
m_left_branch = NULL; |
| 118 |
|
| 119 |
delete m_right_branch; |
| 120 |
m_right_branch = NULL; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
void NodeB::count(std::set<Int4>& index_tray) const |
| 125 |
{ |
| 126 |
if( m_left == m_right ) |
| 127 |
{ |
| 128 |
if( m_left >= 0 ) index_tray.insert(m_left); |
| 129 |
if( IsBud() ) return; |
| 130 |
|
| 131 |
m_left_branch->count(index_tray); |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
if( m_left >= 0 ) index_tray.insert(m_left); |
| 136 |
if( m_right >= 0 ) index_tray.insert(m_right); |
| 137 |
if( IsBud() ) return; |
| 138 |
|
| 139 |
m_left_branch->count(index_tray); |
| 140 |
m_right_branch->count(index_tray); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
void NodeB::putRootBud(const Int4& K3, std::set<Bud>& budtray) const |
| 147 |
{ |
| 148 |
Int4 count = 0; |
| 149 |
Int4 K4 = -1; |
| 150 |
if( !( this->IsBud() ) ) K4 = this->Upper(); |
| 151 |
|
| 152 |
if( K3 < 0 ) count++; |
| 153 |
if( m_left < 0 ) count++; |
| 154 |
if( m_right < 0 ) count++; |
| 155 |
if( K4 < 0 ) count++; |
| 156 |
|
| 157 |
if( count < 2 ) |
| 158 |
{ |
| 159 |
Bud budex; |
| 160 |
budex.setIndex( m_left, m_right, K3, K4 ); |
| 161 |
budtray.insert(budex); |
| 162 |
} |
| 163 |
else if( !( this->IsBud() ) ) |
| 164 |
{ |
| 165 |
m_left_branch->putRootBud(m_right, budtray); |
| 166 |
if( m_left != m_right ) m_right_branch->putRootBud(m_left, budtray); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
void NodeB::putBud(const Int4& K3, std::set<Bud>& budtray) const |
| 172 |
{ |
| 173 |
Int4 count = 0; |
| 174 |
Int4 K4 = -1; |
| 175 |
if( !( this->IsBud() ) ) K4 = this->Upper(); |
| 176 |
|
| 177 |
if( K3 < 0 ) count++; |
| 178 |
if( m_left < 0 ) count++; |
| 179 |
if( m_right < 0 ) count++; |
| 180 |
if( K4 < 0 ) count++; |
| 181 |
|
| 182 |
if( count < 2 ) |
| 183 |
{ |
| 184 |
Bud budex; |
| 185 |
budex.setIndex( m_left, m_right, K3, K4 ); |
| 186 |
budtray.insert(budex); |
| 187 |
} |
| 188 |
if( IsBud() ) return; |
| 189 |
|
| 190 |
m_left_branch->putBud(m_right, budtray); |
| 191 |
if( m_left != m_right ) m_right_branch->putBud(m_left, budtray); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
void NodeB::putQuadraticForm(const VecDat3<Int4>& hkl_left, |
| 196 |
const VecDat3<Int4>& hkl_right, |
| 197 |
multimap<Int4, VecDat3<Int4> >& qindex_hkl) const |
| 198 |
{ |
| 199 |
if( !( this->IsBud() ) ) |
| 200 |
{ |
| 201 |
VecDat3<Int4> diff = hkl_left - hkl_right; |
| 202 |
if( this->Upper() >= 0) |
| 203 |
{ |
| 204 |
qindex_hkl.insert( multimap<Int4, VecDat3<Int4> >::value_type( this->Upper(), diff ) ); |
| 205 |
} |
| 206 |
m_left_branch->putQuadraticForm(hkl_left*(-1), diff, qindex_hkl); |
| 207 |
if( m_left != m_right ) m_right_branch->putQuadraticForm(hkl_right, diff, qindex_hkl); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
void NodeB::print(ostream& os, const Int4& num, |
| 213 |
const Int4& K3, const Double& maxQ) const |
| 214 |
{ |
| 215 |
os.width(10); |
| 216 |
os << this->chToString(); |
| 217 |
|
| 218 |
bool flag_maxQ = false; |
| 219 |
bool set_Q4 = false; |
| 220 |
Double ans = -100.0; |
| 221 |
if( m_left_branch == NULL || m_right_branch == NULL ) |
| 222 |
{ |
| 223 |
if( m_left != -1 && m_right != -1 && K3 != -1 ) |
| 224 |
{ |
| 225 |
ans = ( VCData::putPeakPos(m_left).q |
| 226 |
+ VCData::putPeakPos(m_right).q ) * 2.0 |
| 227 |
- VCData::putPeakPos(K3).q; |
| 228 |
if( ans > maxQ ) flag_maxQ = true; |
| 229 |
set_Q4 = true; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
os.width(5); |
| 234 |
os << " -L- "; |
| 235 |
if( m_left_branch != NULL ) |
| 236 |
{ |
| 237 |
m_left_branch->print(os, num+15, m_right, maxQ); |
| 238 |
} |
| 239 |
else |
| 240 |
{ |
| 241 |
if( flag_maxQ ) os << ">MaxQ"; |
| 242 |
else if( set_Q4 ) os << ans; |
| 243 |
} |
| 244 |
os << endl; |
| 245 |
|
| 246 |
os.width(num+15); |
| 247 |
os << " -R- "; |
| 248 |
if( m_right_branch != NULL ) |
| 249 |
{ |
| 250 |
m_right_branch->print(os, num+15, m_left, maxQ); |
| 251 |
} |
| 252 |
else |
| 253 |
{ |
| 254 |
if( flag_maxQ ) os << ">MaxQ"; |
| 255 |
else if( set_Q4 ) os << ans; |
| 256 |
} |
| 257 |
os << endl; |
| 258 |
} |