35 namespace RestFrames {
40 int Jigsaw::m_class_key = 0;
43 const std::string& stitle,
44 int Nchild,
int Ndependancy)
46 m_Nchild(Nchild), m_Ndeps(Ndependancy)
50 m_ParentStatePtr =
nullptr;
51 for(
int i = 0; i < m_Nchild; i++){
55 for(
int i = 0; i < m_Ndeps; i++){
57 m_DependancyStates.push_back(
StateList());
62 m_Type = kVanillaJigsaw;
79 for(
int i = 0; i < m_Nchild; i++){
80 m_ChildFrames[i].Clear();
81 m_ChildStates[i].Clear();
84 for(
int i = 0; i < m_Ndeps; i++){
85 m_DependancyStates[i].Clear();
86 m_DependancyFrames[i].Clear();
89 m_DependancyJigsaws.Clear();
93 return m_Type == kInvisibleJigsaw;
97 return m_Type == kCombinatoricJigsaw;
103 output +=
" Type: Invisible \n";
105 output +=
" Type: Combinatoric \n";
116 if(*m_GroupPtr == group){
119 Group* groupPtr = m_GroupPtr;
120 m_GroupPtr =
nullptr;
125 m_GroupPtr =
nullptr;
139 return SetBody(
false);
144 bool Jigsaw::CanResolve(
const State& state)
const {
145 if(!state)
return false;
146 return CanResolve(state.GetListFrames());
149 void Jigsaw::SetParentState(){
153 void Jigsaw::SetParentState(State& state){
157 if(m_ParentStatePtr){
158 if(*m_ParentStatePtr == state)
161 m_ParentStatePtr =
nullptr;
165 m_ParentStatePtr =
nullptr;
167 m_ParentStatePtr = &state;
172 State
const& Jigsaw::GetParentState()
const {
174 return *m_ParentStatePtr;
179 int Jigsaw::GetNChildren()
const {
183 int Jigsaw::GetNDependancyStates()
const {
187 State& Jigsaw::GetChildState(
int i)
const {
188 return m_ChildStates[i];
191 StateList
const& Jigsaw::GetChildStates()
const {
192 return m_ChildStates;
195 StateList
const& Jigsaw::GetDependancyStates(
int i)
const {
196 if(i < 0 || i >= m_Ndeps)
198 return m_DependancyStates[i];
203 for(
int i = 0; i < m_Nchild; i++)
209 if(i < 0 || i >= m_Nchild)
211 return m_ChildFrames[i];
215 if(i < 0 || i >= m_Ndeps)
217 return m_DependancyFrames[i];
220 bool Jigsaw::InitializeTree(){
222 return SetMind(
false);
224 if(!GetParentState()){
226 m_Log <<
"Unable to initialize Jigsaw. ";
227 m_Log <<
"No parent State set." << LogEnd;
228 return SetMind(
false);
231 if(!CanResolve(GetParentState())){
233 m_Log <<
"Unable to resolve input parent State. ";
235 m_Log <<
" Frames (requested): " << Log(GetParentState().GetListFrames());
237 return SetMind(
false);
240 for(
int i = 0; i < m_Nchild; i++){
241 m_ChildStates[i].Clear();
242 m_ChildStates[i].SetParentJigsaw(*
this);
246 return SetMind(
true);
249 bool Jigsaw::InitializeAnalysis(){
251 return SetMind(
false);
254 return SetMind(
false);
257 const LabRecoFrame& lab_frame =
258 static_cast<const LabRecoFrame&
>(
GetGroup().GetLabFrame());
259 StateList states = lab_frame.GetTreeStates();
260 GroupList groups = lab_frame.GetListGroups();
262 int Ngroup = groups.GetN();
263 std::vector<ConstRestFrameList> group_frames;
264 for(
int i = 0; i < Ngroup; i++)
265 group_frames.push_back(ConstRestFrameList());
267 for(
int d = 0; d < m_Ndeps; d++){
268 m_DependancyStates[d].Clear();
270 for(
int i = 0; i < Ngroup; i++)
271 group_frames[i].
Clear();
273 int Nf = m_DependancyFrames[d].GetN();
274 for(
int f = 0; f < Nf; f++){
275 const RestFrame& frame = m_DependancyFrames[d][f];
277 bool no_group =
true;
278 for(
int g = 0; g < Ngroup; g++){
279 if(groups[g].ContainsFrame(frame)){
280 group_frames[g].Add(frame);
287 State& state = states.GetFrame(frame);
290 m_Log <<
"Cannot find State corresponding to frame: " << std::endl;
291 m_Log << Log(frame) <<
" " << Log(states) << LogEnd;
292 return SetMind(
false);
295 m_Log <<
"Adding dependancy State for index " << d;
296 m_Log <<
" corresponding to frame:";
297 m_Log << Log(frame) << LogEnd;
298 m_DependancyStates[d] += state;
301 for(
int g = 0; g < Ngroup; g++){
302 if(group_frames[g].GetN() == 0)
continue;
303 StateList group_states = groups[g].GetChildStates(group_frames[g]);
304 if(group_states.GetN() == 0){
305 m_Log <<
"Cannot find States in Group:" << std::endl;
306 m_Log <<
" Frames:" << std::endl <<
" ";
307 m_Log << Log(group_frames[g]) << std::endl;
308 m_Log <<
" Group:" << std::endl;
309 m_Log << Log(groups.Get(g)) << LogEnd;
310 return SetMind(
false);
313 m_Log <<
"Sucessfully found dependancy States for index " << d << std::endl;
314 int Ns = group_states.GetN();
315 m_Log <<
" Frames:" << std::endl <<
" ";
316 m_Log << Log(group_frames[g]) << std::endl;
317 m_Log <<
" States:" << std::endl;
318 for(
int s = 0; s < Ns; s++){
319 m_Log <<
" state " << s <<
": ";
320 m_Log << Log(group_states[s].GetListFrames()) << std::endl;
323 m_DependancyStates[d] += group_states;
327 return SetMind(
true);
330 bool Jigsaw::InitializeDependancyJigsaws(){
334 m_DependancyJigsaws.Clear();
337 FillJigsawDependancies(jigsaws);
339 m_DependancyJigsaws.Add(jigsaws);
344 void Jigsaw::AddChildFrame(
const RestFrame& frame,
int i){
346 if(!frame.IsRecoFrame())
return;
347 if(i < 0 || i >= m_Nchild)
return;
351 m_ChildFrames[i] += frame;
354 void Jigsaw::AddDependancyFrame(
const RestFrame& frame,
int i){
356 if(!frame.IsRecoFrame())
return;
357 if(i < 0 || i >= m_Ndeps)
return;
361 m_DependancyFrames[i] += frame;
369 for(
int i = 0; i < m_Nchild; i++)
370 m_ChildFrames[i].Remove(frame);
372 for(
int i = 0; i < m_Ndeps; i++)
373 m_DependancyFrames[i].Remove(frame);
377 int N = frames.GetN();
378 for(
int i = 0; i < N; i++)
382 bool Jigsaw::IsSoundBody()
const {
383 if(RFBase::IsSoundBody())
386 for(
int i = 0; i < m_Nchild-1; i++){
387 for(
int j = i+1; j < m_Nchild; j++){
388 if(m_ChildFrames[i].Intersection(m_ChildFrames[j]).GetN() > 0){
390 m_Log <<
"Child frames are repeated between ";
391 m_Log <<
"more than one output index: ";
392 m_Log << Log(m_ChildFrames[i].Intersection(m_ChildFrames[j]));
394 return SetBody(
false);
398 for(
int i = 0; i < m_Nchild; i++){
399 if(m_ChildFrames[i].GetN() == 0){
401 m_Log <<
"No child frames at index ";
402 m_Log << i << LogEnd;
403 return SetBody(
false);
407 return SetBody(
true);
410 bool Jigsaw::DependsOnJigsaw(
const Jigsaw& jigsaw)
const {
411 return m_DependancyJigsaws.Contains(jigsaw);
414 void Jigsaw::FillJigsawDependancies(JigsawList& jigsaws)
const {
415 if(jigsaws.Contains(*
this))
return;
416 jigsaws.Add((
Jigsaw&)(*m_This));
421 for(
int i = 0; i < m_Ndeps; i++){
422 int M = m_DependancyStates[i].GetN();
423 for(
int j = 0; j < M; j++){
424 Jigsaw& jigsaw = m_DependancyStates[i][j].GetParentJigsaw();
425 jigsaw.FillJigsawDependancies(jigsaws);
430 void Jigsaw::FillGroupJigsawDependancies(JigsawList& jigsaws)
const {
431 if(jigsaws.Contains(*
this))
return;
432 jigsaws.Add((
Jigsaw&)(*m_This));
435 m_ParentStatePtr->
GetParentJigsaw().FillGroupJigsawDependancies(jigsaws);
438 void Jigsaw::FillGroupJigsawDependants(JigsawList& jigsaws)
const {
439 if(jigsaws.Contains(*
this))
return;
440 jigsaws.Add((
Jigsaw&)(*m_This));
442 for(
int i = 0; i < m_Nchild; i++)
443 GetChildState(i).
GetChildJigsaw().FillGroupJigsawDependants(jigsaws);