38 int Group::m_class_key = 0;
40 Group::Group(
const std::string& sname,
41 const std::string& stitle)
42 : RFBase(sname, stitle, Group::m_class_key++)
44 m_Type = kVanillaGroup;
45 m_GroupStatePtr =
nullptr;
46 m_Log.SetSource(
"Group "+GetName());
49 Group::Group() : RFBase() {
50 m_Type = kVanillaGroup;
51 m_Log.SetSource(
"Group "+GetName());
58 Group& Group::Empty(){
59 return InvisibleGroup::Empty();
65 m_GroupStatePtr =
nullptr;
71 m_StatesToResolve.Clear();
72 m_JigsawsToUse.Clear();
75 bool Group::IsInvisibleGroup()
const{
76 return m_Type == kInvisibleGroup;
79 bool Group::IsCombinatoricGroup()
const{
80 return m_Type == kCombinatoricGroup;
83 void Group::AddFrame(RestFrame& frame){
86 if(!frame.IsRecoFrame())
return;
89 static_cast<ReconstructionFrame&
>(frame).SetGroup(*
this);
93 void Group::AddFrames(
const RestFrameList& frames){
94 int N = frames.GetN();
95 for(
int i = 0; i < N; i++)
99 void Group::AddJigsaw(Jigsaw& jigsaw){
103 if(!jigsaw.GetGroup().IsEmpty()){
104 if(jigsaw.GetGroup() == *
this)
106 Group& group = jigsaw.GetGroup();
108 group.RemoveJigsaw(jigsaw);
113 if(m_JigsawsToUse.Add(jigsaw))
114 jigsaw.SetGroup(*
this);
117 void Group::RemoveFrame(RestFrame& frame){
118 if(!m_Frames.Contains(frame))
123 static_cast<ReconstructionFrame&
>(frame).SetGroup();
124 m_Frames.Remove(frame);
127 void Group::RemoveFrames(){
128 int N = m_Frames.GetN();
129 for(
int i = N-1; i >= 0; i--){
130 RemoveFrame(m_Frames[i]);
134 void Group::RemoveJigsaw(Jigsaw& jigsaw){
135 if(!m_Jigsaws.Contains(jigsaw) &&
136 !m_JigsawsToUse.Contains(jigsaw))
142 m_Jigsaws.Remove(jigsaw);
143 m_JigsawsToUse.Remove(jigsaw);
146 void Group::RemoveJigsaws(){
147 int N = m_Jigsaws.GetN();
148 for(
int i = N-1; i >= 0; i--){
149 RemoveJigsaw(m_Jigsaws[i]);
151 N = m_JigsawsToUse.GetN();
152 for(
int i = N-1; i >= 0; i--){
153 RemoveJigsaw(m_JigsawsToUse[i]);
157 bool Group::ContainsFrame(
const RestFrame& frame)
const {
158 return m_Frames.Contains(frame);
161 int Group::GetNFrames()
const {
162 return m_Frames.GetN();
165 const RestFrameList& Group::GetListFrames()
const {
169 const JigsawList& Group::GetListJigsaws()
const {
173 State& Group::GetParentState()
const {
175 return *m_GroupStatePtr;
177 return State::Empty();
180 bool Group::InitializeAnalysis(){
182 m_Log <<
"Initializing Group for analysis...";
185 m_GroupStatePtr = &InitializeParentState();
186 m_GroupStatePtr->AddFrames(m_Frames);
188 if(!ResolveUnknowns()){
190 m_Log <<
"Unable to resolve unknowns associated with ";
191 m_Log <<
"Frames in this Group with available Jigsaws";
193 return SetBody(
false);
197 m_Log <<
"...Done initializing group for analysis" << LogEnd;
199 return SetMind(
true);
202 bool Group::ResolveUnknowns(){
203 m_JigsawsToUse += m_Jigsaws;
206 int Njigsaw = m_JigsawsToUse.GetN();
207 for(
int i = 0; i < Njigsaw; i++){
208 if(m_JigsawsToUse[i].GetNChildren() == 1){
209 m_JigsawsToUse[i].RemoveFrames(m_Frames);
210 int N = GetNFrames();
211 for(
int j = 0; j < N; j++)
212 m_JigsawsToUse[i].AddChildFrame(m_Frames[j]);
217 m_StatesToResolve.Clear();
218 m_States.Add(*m_GroupStatePtr);
219 m_StatesToResolve.Add(*m_GroupStatePtr);
221 while(m_StatesToResolve.GetN() > 0){
222 State& state = m_StatesToResolve.Get(0);
223 if(!ResolveState(state)){
224 if(state.GetNFrames() != 1){
226 m_Log <<
"Cannot find Jigsaw to Resolve State for frames:";
227 m_Log << std::endl <<
" " << Log(state.GetListFrames());
231 m_StatesToResolve.Remove(state);
237 bool Group::ResolveState(
const State& state){
238 Jigsaw* jigsawSolutionPtr =
nullptr;
240 int N = m_JigsawsToUse.GetN();
241 for(
int i = 0; i < N; i++){
242 Jigsaw* jigsawPtr = &m_JigsawsToUse[i];
243 if(jigsawPtr->CanResolve(state)){
244 if(!jigsawSolutionPtr){
245 jigsawSolutionPtr = jigsawPtr;
248 if(jigsawPtr->GetNChildren() <= jigsawSolutionPtr->GetNChildren())
249 if((jigsawPtr->GetNChildren() < jigsawSolutionPtr->GetNChildren()) ||
250 (jigsawPtr->GetNDependancyStates() < jigsawSolutionPtr->GetNDependancyStates()))
251 jigsawSolutionPtr = jigsawPtr;
254 if(!jigsawSolutionPtr)
258 m_Log <<
"Found Jigsaw to resolve State:" << std::endl;
259 m_Log <<
" Frames:" << std::endl <<
" ";
260 m_Log << Log(state.GetListFrames()) << std::endl;
261 m_Log <<
" Jigsaw:" << Log(jigsawSolutionPtr);
264 if(!InitializeJigsaw(*jigsawSolutionPtr))
267 m_JigsawsToUse.Remove(*jigsawSolutionPtr);
271 bool Group::InitializeJigsaw(Jigsaw& jigsaw){
272 State& state = m_StatesToResolve[0];
273 jigsaw.SetParentState(state);
275 if(!jigsaw.InitializeTree()){
277 m_Log <<
"Unable to initialize Jigsaw:";
278 m_Log << Log(jigsaw) << LogEnd;
282 m_States += jigsaw.GetChildStates();
283 m_StatesToResolve -= state;
284 m_StatesToResolve += jigsaw.GetChildStates();
290 int Group::GetNChildStates()
const {
291 return m_States.GetN();
294 State& Group::GetChildState(
int i)
const {
295 if(i < 0 || i > GetNChildStates()-1)
296 return State::Empty();
301 State& Group::GetChildState(
const RestFrame& frame)
const {
302 if(!frame)
return State::Empty();
303 int Ns = GetNChildStates();
304 for(
int i = Ns-1; i >= 0; i--)
305 if(m_States[i].IsFrame(frame))
308 return State::Empty();
311 StateList Group::GetChildStates(
const RestFrameList& frames)
const {
315 int Ns = m_States.GetN();
316 for(
int i = 0; i < Ns; i++){
317 RestFrameList iframes = m_States[i].GetListFrames();
318 if(frames.Contains(iframes)){
319 int Nsol = states.GetN();
321 for(
int j = 0; j < Nsol; j++){
323 RestFrameList jframes = states[j].GetListFrames();
324 if(iframes.Contains(jframes)){
325 states.Remove(states[j]);
329 if(jframes.Contains(iframes)){
334 if(isnew) states.Add(m_States[i]);
338 RestFrameList match_frames;
340 for(
int i = 0; i < Ns; i++)
341 match_frames += states[i].GetListFrames();
343 if(!(frames == match_frames)){
345 m_Log <<
"Unable to find States corresponding to Frames: " << std::endl;
346 m_Log << Log(frames) << LogEnd;
353 RestFrame
const& Group::GetLabFrame()
const {
354 if(m_Frames.GetN() < 1)
355 return RestFrame::Empty();
357 return m_Frames[0].GetLabFrame();
bool IsEmpty() const
Checks whether this is default (empty) instance of class.
virtual void Clear()
Clears Group of all connections to other objects.