]> Creatis software - bbtk.git/blob - kernel/src/bbtkBlackBoxDescriptor.cxx
#3202 BBTK Feature New Normal - fast algorithm for ImageBoundaries box
[bbtk.git] / kernel / src / bbtkBlackBoxDescriptor.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbtkBlackBoxDescriptor.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.24 $
34 =========================================================================*/
35
36
37
38 /**
39  *  \file 
40  *  \brief Class bbtk::BlackBoxDescriptor : (abstract) describes a BlackBox (name, description, author) and is able to create an instance of it.
41  */
42 #include "bbtkBlackBoxDescriptor.h"
43 #include "bbtkMessageManager.h"
44 #include "bbtkPackage.h"
45 #include "bbtkUtilities.h"
46 #include "bbtkAtomicBlackBoxDescriptor.h"
47 #include "bbtkWxBlackBox.h"
48 // JFGA 12/12/2014 Adding libraries for Wt
49 //#include "bbtkWtBlackBox.h"
50
51 namespace bbtk
52 {
53
54   typedef Package::Pointer PackagePointer;
55
56 #define bbtkDMessage(key,level,mess) \
57   bbtkMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
58 #define bbtkDDebugMessage(key,level,mess)       \
59   bbtkDebugMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
60
61
62   //=========================================================================
63   /// Default ctor
64   BlackBoxDescriptor::BlackBoxDescriptor()  
65     : mTypeName("Unknown"), 
66       mDescription(""), 
67       mAuthor(""),
68       mCategory(""),
69       mKind(STANDARD),
70           mTypeOfScript(TS_BINARY),
71       mPackage()
72   {
73     bbtkDDebugMessage("object",4,
74                      "==> BlackBoxDescriptor()"<<std::endl);
75     bbtkDDebugMessage("object",4,
76                      "<== BlackBoxDescriptor()"<<std::endl);
77   }
78   //=========================================================================
79
80   //=========================================================================
81   /// Default ctor
82   BlackBoxDescriptor::BlackBoxDescriptor(const BlackBoxDescriptor&)  
83   {
84           mScriptFileName="";
85   }
86   //=========================================================================
87   /// Dtor
88   BlackBoxDescriptor::~BlackBoxDescriptor()
89   {
90     bbtkDDebugMessage("object",4,
91                      "==> ~BlackBoxDescriptor()"
92                      <<std::endl);
93
94     // deletes all I/O descriptors
95     InputDescriptorMapType::iterator i;
96     for (i=mInput.begin(); i!=mInput.end(); ++i) delete i->second;
97     OutputDescriptorMapType::iterator o;
98     for (o=mOutput.begin(); o!=mOutput.end(); ++o) delete o->second;
99
100     bbtkDDebugMessage("object",4,
101                       "<== ~BlackBoxDescriptor()"
102                       <<std::endl);
103   }
104   //=========================================================================
105
106   /*
107   //=======================================================================
108   /// Release
109   void BlackBoxDescriptor::Release(BlackBoxDescriptor::WeakPointer desc)
110   {
111     bbtkMessage("object",2,"==> BlackBoxDescriptor::Release('"
112                 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
113     long c = desc.use_count();
114     bbtkMessage("object",3," - ref count = "<<c<<std::endl);
115     // If only one ref 
116     if ((c == 1) && (desc.lock()->mPackage))
117       {
118         bbtkMessage("object",2," --> No more instance alive = releasing from package"<<std::endl);
119         
120         Package::WeakPointer pack = desc.lock()->mPackage;
121         Package::ReleaseBlackBoxDescriptor(pack,desc);
122       }
123     else 
124       {
125         bbtkMessage("object",2," --> Still some instances alive = Keeping it alive"<<std::endl);
126       }    
127     bbtkMessage("object",2,"<== BlackBoxDescriptor::Release('"
128                 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
129  
130   }
131   //=========================================================================
132   */
133
134   /*
135   //=========================================================================
136   /// Dtor
137   void BlackBoxDescriptor::UnReference()
138   {
139     bbtkDDebugMessageInc("kernel",1,
140                         "BlackBoxDescriptor::UnReference() ["
141                         <<mTypeName<<"] #"<<mRefCount-1<<std::endl);
142     mRefCount--;
143     if (mRefCount<=0) 
144       {
145         bbtkDDebugMessage("kernel",1,"--> Destructing BlackBoxDescriptor ["<<mTypeName<<"]"<<std::endl);
146         delete this;
147       }
148   }
149   //=========================================================================
150   */
151   //=========================================================================
152   /// Check
153   void BlackBoxDescriptor::Check(bool) const
154   {
155     
156   }
157   //=========================================================================
158
159   //=========================================================================
160   /// Adds the string to the BlackBox description
161   void BlackBoxDescriptor::AddToDescription( const std::string& s, bool clear)
162   {
163     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToDescription(\""
164                       <<s<<"\")"<<std::endl);
165     if (clear) mDescription = s; 
166     else mDescription += s;
167    }
168   //=========================================================================
169
170   //=========================================================================   
171   /// Adds the string to the BlackBox author list
172   void BlackBoxDescriptor::AddToAuthor( const std::string& s, bool clear)
173   {
174     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToAuthor(\""
175                       <<s<<"\")"
176                       <<std::endl);
177     if (clear) mAuthor = s;
178     else mAuthor += s;
179   }
180   //=========================================================================
181
182   //=========================================================================  
183   /// Adds the string to the BlackBox category list
184   void BlackBoxDescriptor::AddToCategory( const std::string& s, bool clear)
185   {
186     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToCategory(\""
187                       <<s<<"\")"
188                       <<std::endl);  
189     if (clear) mCategory = s;
190     else mCategory += s;
191     mCategory += ";";
192   }  
193   //=========================================================================
194   
195   //=========================================================================
196   const BlackBoxDescriptor::InputDescriptor* 
197   BlackBoxDescriptor::GetInputDescriptor(const std::string & name) const
198   {
199     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetInputDescriptor('"
200                       <<name<<"')"<<std::endl);
201
202     InputDescriptorMapType::const_iterator i;
203     i = mInput.find(name);
204     if ( i == mInput.end() ) 
205     {
206             bbtkError("input '"<<name<<"' does not exist");
207     }
208
209     return i->second;
210   }
211   //=========================================================================
212
213   //=========================================================================
214   const BlackBoxDescriptor::OutputDescriptor* 
215   BlackBoxDescriptor::GetOutputDescriptor(const std::string & name) const
216   {
217     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetOutputDescriptor('"
218                       <<name<<"')"<<std::endl);
219
220     OutputDescriptorMapType::const_iterator i;
221     i = mOutput.find(name);
222     if ( i == mOutput.end() ) 
223     {
224       bbtkError("output '"<<name<<"' does not exist");
225     }
226
227     return i->second;
228   }
229   //=========================================================================
230
231   //=========================================================================
232   void BlackBoxDescriptor::GetHelp(bool full) const
233   {
234     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetHelp()"
235                       <<std::endl);
236     
237     bbtkMessage("help",1,"Black Box <"<<GetFullTypeName()<<">"<<std::endl);
238     bbtkMessage("help",1," "              <<GetDescription()<<std::endl);
239     bbtkMessage("help",1," By : "         <<GetAuthor()     <<std::endl);
240     bbtkMessage("help",1," Categories : " <<GetCategory()    <<std::endl);
241     if (mInput.size()) 
242       bbtkMessage("help",1," * Inputs : "<<std::endl);
243     else 
244       bbtkMessage("help",1," * No inputs"<<std::endl);
245     InputDescriptorMapType::const_iterator i;
246     unsigned int namelmax = 0;
247     unsigned int typelmax = 0;
248     unsigned int natlmax = 0;
249     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
250     {
251            if (i->second->GetName().size()>namelmax) 
252              namelmax = i->second->GetName().size();
253            if (i->second->GetHumanTypeName().size()>typelmax) 
254              typelmax = i->second->GetHumanTypeName().size();
255            if (i->second->GetNature().size()>natlmax) 
256              natlmax = i->second->GetNature().size();
257     }
258     OutputDescriptorMapType::const_iterator o;
259     for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
260     {
261            if (o->second->GetName().size()>namelmax) 
262              namelmax = o->second->GetName().size();
263            if (o->second->GetHumanTypeName().size()>typelmax) 
264              typelmax = o->second->GetHumanTypeName().size();
265            if (o->second->GetNature().size()>natlmax) 
266              natlmax = o->second->GetNature().size();
267     }
268     //
269     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
270     {
271            std::string name(i->second->GetName());
272            name += "'";
273            name.append(1+namelmax-name.size(),' ');
274            std::string type(i->second->GetHumanTypeName());
275            type += ">";
276            type.append(1+typelmax-type.size(),' ');
277            std::string nature(i->second->GetNature());
278            nature += "]";
279            nature.append(1+natlmax-nature.size(),' ');
280            bbtkMessage("help",1,
281                        "    '"<<name
282                        <<" <"<<type
283                        <<" ["<<nature
284                        <<" : "<<i->second->GetDescription()<<std::endl);
285     }
286     if (mOutput.size()) 
287       bbtkMessage("help",1," * Outputs : "<<std::endl);
288     else 
289       bbtkMessage("help",1," * No outputs"<<std::endl);
290     for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
291     {
292            std::string name(o->second->GetName());
293            name += "'";
294            name.append(1+namelmax-name.size(),' ');
295            std::string type(o->second->GetHumanTypeName());
296            type += ">";
297            type.append(1+typelmax-type.size(),' ');
298            std::string nature(o->second->GetNature());
299            nature += "]";
300            nature.append(1+natlmax-nature.size(),' ');
301            bbtkMessage("help",1,
302                     "    '"<<name
303                        <<" <"<<type
304                        <<" ["<<nature
305                        <<" : "<<o->second->GetDescription()<<std::endl);
306       }
307    
308   
309   }
310   //=========================================================================
311    
312   //=========================================================================
313   /// Returns the full name of the **TYPE** of the black box (+package name)
314   std::string BlackBoxDescriptor::GetFullTypeName() const
315   {
316     if (GetPackage()!=0) return GetPackage()->GetName() + "::" + mTypeName;
317     return "::" + mTypeName;
318   }
319   //=========================================================================
320          void BlackBoxDescriptor::GetBoxesInside (NodeTreeC& tree, int cont){
321                 
322         }
323   //=========================================================================
324   void BlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s, 
325                                             int detail, int level,
326                                             const std::string& output_dir,
327                                             bool relative_link )
328   {
329     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::InsertHtmlHelp()"
330                       <<std::endl);
331     
332     //-------------
333     // General info 
334     std::string name = GetTypeName();
335     Utilities::html_format(name);
336
337     (s) << "<p><hr>\n";
338     (s) << "<a name=\""<<name<<"\"></a>\n";
339     (s) << //"Top:&nbsp;
340       "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
341     // (s) << "Previous:&nbsp;<a rel="previous" accesskey="p" href="#dir">(dir)</a>,
342     // (s) << "Up:&nbsp;<a rel="up" accesskey="u" href="#dir">(dir)</a>
343     (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
344
345
346     std::string descr = GetDescription();
347     //Utilities::html_format(descr);
348     std::string author = GetAuthor();
349     Utilities::html_format(author);
350
351     std::vector<std::string> categories;
352     // Split the category string 
353     std::string delimiters = ";,";
354     Utilities::SplitString(GetCategory(),
355                            delimiters,categories);
356
357     
358     (s) << "<p><TABLE cellspacing=0  cellpadding=3>\n";
359     (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> " 
360       << descr << "</TD></TR>\n";
361     (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  " 
362       << author << "</TD></TR>\n";
363     (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  ";
364     std::vector<std::string>::iterator ci;
365     for (ci=categories.begin(); ci!=categories.end(); ++ci)
366       {
367         s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci 
368           << "</a>&nbsp;\n";
369       }
370     s << "</TD></TR>\n";      
371
372     (s) << "<TR><TD style='vertical-align: top;'><b> To use it </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  include " 
373         << GetPackage()->GetName() << "</TD></TR>\n";
374     (s) << "</TABLE>\n";
375
376     //-------------
377     // Graph
378     //i->second->InsertHTMLGraph( &s , detail,level,dir);
379     
380     //-------------
381     // Inputs
382     std::string titlecol("#BBBBFF");
383     std::string usercol("#FFFFFF");
384     std::string ubbcol("#DDFFFF");
385     std::string wxbbcol("#EEFFFF");
386 // JFGA 12/12/2014 Adding new variable 
387 //    std::string wtbbcol("#CCFFFF");
388
389     //  (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
390     (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
391     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
392       <<"\">Inputs</TD></TR>\n";
393
394     std::vector<std::string> user_defined;
395     std::vector<std::string> ubb_defined;
396     std::vector<std::string> wxbb_defined;
397
398     const BlackBoxDescriptor::InputDescriptorMapType& imap = 
399       GetInputDescriptorMap();
400     InputDescriptorMapType::const_iterator in;
401     for ( in = imap.begin();  in != imap.end(); ++in ) 
402     {
403       // Skips system-defined inputs
404       std::string col(usercol);
405       int iotype = 0;
406       if (in->second->GetCreatorTypeInfo() == 
407           typeid(AtomicBlackBoxDescriptor))
408         {
409           col = ubbcol; 
410           iotype = 1;
411         }
412 #ifdef USE_WXWIDGETS
413       else if (in->second->GetCreatorTypeInfo() == 
414                typeid(WxBlackBoxDescriptor))
415         {
416           col = wxbbcol; 
417           iotype = 2;
418         }
419 #endif
420
421       std::string name(in->second->GetName());
422       Utilities::html_format(name);
423       
424       std::string type("<");
425       type += in->second->GetTypeName();    
426       type += ">";
427       Utilities::html_format(type);
428       
429       std::string descr(in->second->GetDescription());
430       //Utilities::html_format(descr);
431
432 /*EED 10/11/2009
433       std::string out = 
434         "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
435         +"\"><B><PRE> "+name+" </PRE></B></TD>"
436         + "<TD style='vertical-align: top;' bgcolor=\""+col
437         +"\"><I><PRE> "+type+" </PRE></I></TD>"
438         + "<TD style='vertical-align: top;' bgcolor=\""+col
439         +"\">"+descr+"</TD></TR>\n";
440 */      
441
442       std::string out = 
443         "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
444         +"\"><B><PRE> "+name+" </PRE></B></TD>"
445         + "<TD style='vertical-align: top;' bgcolor=\""+col
446         +"\"><I><PRE> "+descr+" </PRE></I></TD>"
447         + "<TD style='vertical-align: top;' bgcolor=\""+col
448         +"\">"+type+"</TD></TR>\n";
449
450       if (iotype==0) user_defined.push_back(out);
451       else if (iotype==1) ubb_defined.push_back(out);
452       else if (iotype==2) wxbb_defined.push_back(out);
453       
454     }
455
456     std::vector<std::string>::iterator hi;
457     for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
458     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
459     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
460
461     user_defined.clear();
462     ubb_defined.clear();
463     wxbb_defined.clear();
464
465     //-------------
466     // Outputs
467     //  (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
468     //  (s) << "<TABLE border=1 cellspacing=0>\n";
469     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
470       <<"\">Outputs</TD></TR>\n";
471     
472     const BlackBoxDescriptor::OutputDescriptorMapType& omap = 
473       GetOutputDescriptorMap();
474     
475     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
476     
477     for ( o = omap.begin();  o != omap.end(); ++o ) 
478       {
479         std::string col(usercol);
480         int iotype = 0;
481         if (o->second->GetCreatorTypeInfo() == 
482             typeid(AtomicBlackBoxDescriptor))
483           {
484             col = ubbcol; 
485             iotype = 1;
486           }
487 #ifdef USE_WXWIDGETS
488         else if (o->second->GetCreatorTypeInfo() == 
489                  typeid(WxBlackBoxDescriptor))
490           {
491             col = wxbbcol; 
492             iotype = 2;
493           }
494 #endif
495         
496         std::string name(o->second->GetName());
497         Utilities::html_format(name);
498         
499         std::string type("<");
500         type += o->second->GetTypeName();    
501         type += ">";
502         Utilities::html_format(type);
503         
504         std::string descr(o->second->GetDescription());
505         //Utilities::html_format(descr);
506         
507 /*EED 10/11/2009
508         std::string out = 
509           "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
510           +"\"><B><PRE> "+name+" </PRE></B></TD>"
511           + "<TD style='vertical-align: top;' bgcolor=\""+col
512           +"\"><I><PRE> "+type+" </PRE></I></TD>"
513           + "<TD style='vertical-align: top;' bgcolor=\""+col
514           +"\">"+descr+"</TD></TR>\n";
515 */
516         std::string out = 
517           "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
518           +"\"><B><PRE> "+name+" </PRE></B></TD>"
519           + "<TD style='vertical-align: top;' bgcolor=\""+col
520           +"\"><I><PRE> "+descr+" </PRE></I></TD>"
521           + "<TD style='vertical-align: top;' bgcolor=\""+col
522           +"\">"+type+"</TD></TR>\n";
523
524         if (iotype==0) user_defined.push_back(out);
525         else if (iotype==1) ubb_defined.push_back(out);
526         else if (iotype==2) wxbb_defined.push_back(out);
527         
528       }
529     
530     for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
531     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
532     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
533
534     (s) << "</TABLE>\n";
535     
536     //------------
537     // End
538
539
540    }
541   //=========================================================================
542  
543         
544         //==========================================================================
545         void BlackBoxDescriptor::SetTypeOfScript_Application()
546         {
547                 SetTypeOfScript(TS_SCRIPT_APPLICATION);
548         }        
549         //==========================================================================
550         
551                 
552         //==========================================================================
553         bool BlackBoxDescriptor::IsTypeOfScript_Application()
554         {
555                 return (GetTypeOfScript()==TS_SCRIPT_APPLICATION);
556         }
557         //==========================================================================
558
559         
560         //==========================================================================
561         bool BlackBoxDescriptor::IsTypeOfScript_ComplexBox()
562         {
563                 return (GetTypeOfScript()==TS_SCRIPT_COMPLEXBOX);
564         }
565         //==========================================================================
566         
567 }