EMAN2
emobject.cpp
Go to the documentation of this file.
1/*
2 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
3 * Probable contributor: Liwei Peng (what dates?)
4 * Contributing author: David Woolford 06/11/2007
5 *
6 * Copyright (c) 2000-2007 Baylor College of Medicine
7 *
8 * This software is issued under a joint BSD/GNU license. You may use the
9 * source code in this file under either license. However, note that the
10 * complete EMAN2 and SPARX software packages have some GPL dependencies,
11 * so you are responsible for compliance with the licenses of these packages
12 * if you opt to use BSD licensing. The warranty disclaimer below holds
13 * in either instance.
14 *
15 * This complete copyright notice must be included in any revised version of the
16 * source code. Additional authorship citations may be added, but existing
17 * author citations must be preserved.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 *
33 * */
34
35#include "emobject.h"
36#include <cmath>
37#ifdef WIN32
38#define M_PI 3.14159265358979323846f
39#endif
40
41#include <algorithm>
42// using copy
43
44#include "util.h"
45
46using namespace EMAN;
47
48#include <iostream>
49using std::cout;
50using std::cerr;
51using std::endl;
52
53const float EMConsts::I2G = (float) (4.0 / (M_PI*M_PI));
54const float EMConsts::I3G = (float) (6.4 / (M_PI*M_PI));
55const float EMConsts::I4G = (float) (8.8 / (M_PI*M_PI));
56const float EMConsts::I5G = (float) (10.4 / (M_PI*M_PI));
57// This stolen from wikipedia.org
58const double EMConsts::pi = 3.141592653589793238462643383279502884197169399;
59const double EMConsts::deg2rad = pi/180.0;
60const double EMConsts::rad2deg = 180.0/pi;
61
62
63#include <sstream>
64using std::stringstream;
65
66#include "transform.h"
67#include "ctf.h"
68
69#ifdef MEMDEBUG
70set<EMObject*> allemobjlist;
71#endif
72
73
74// Static init
75map< EMObject::ObjectType, string> EMObject::type_registry = init();;
76
77//-------------------------------EMObjectTypes-----------------------------------------
78map< EMObject::ObjectType, string> EMObject::init()
79{
80 map< EMObject::ObjectType, string> mymap;
81 static bool first_construction = true;
82 if ( first_construction )
83 {
84 // Initialize the the type registry once and for all
85 mymap[BOOL] = "BOOL";
86 mymap[SHORT] = "SHORT";
87 mymap[INT] = "INT";
88 mymap[UNSIGNEDINT] = "UNSIGNEDINT";
89 mymap[FLOAT] = "FLOAT";
90 mymap[DOUBLE] = "DOUBLE";
91 mymap[STRING] = "STRING";
92 mymap[EMDATA] = "EMDATA";
93 mymap[XYDATA] = "XYDATA";
94 mymap[INTARRAY] = "INTARRAY";
95 mymap[FLOATARRAY] = "FLOATARRAY";
96 mymap[STRINGARRAY] = "STRINGARRAY";
97 mymap[TRANSFORM] = "TRANSFORM";
98 mymap[CTF] = "CTF";
99 mymap[FLOAT_POINTER] = "FLOAT_POINTER";
100 mymap[INT_POINTER] = "INT_POINTER";
101 mymap[UNKNOWN] = "UNKNOWN";
102 mymap[VOID_POINTER] = "VOID_POINTER";
103 mymap[TRANSFORMARRAY] = "TRANSFORMARRAY";
104 first_construction = false;
105 }
106
107 return mymap;
108}
109
110//-------------------------------EMObject--------------------------------------------
111
113{
114 cout << "The address of my type is " << &type << endl;
115 cout << " Now printing the enumerated values in type_registry " << endl;
116 for( map< ObjectType, string>::const_iterator it = type_registry.begin(); it != type_registry.end(); ++it )
117 {
118 cout << it->first << " " << it->second << endl;
119 }
120 cout << "My type is " << to_str(type) << " and its enumerated value is " << type << endl;
121 cout << "The address of the static type registry is " << &type_registry <<", it should be same for all EMObjects" << endl;
122}
123
125#ifdef MEMDEBUG
126 allemobjlist.erase(this);
127 printf(" -(%6d) %p\n",(int)allemobjlist.size(),this);
128#endif
129}
130
132 d(0), type(UNKNOWN)
133{
134#ifdef MEMDEBUG
135 allemobjlist.insert(this);
136 printf(" +(%6d) %p\n",(int)allemobjlist.size(),this);
137#endif
138}
139
140EMObject::EMObject(bool boolean) :
141 b(boolean), type(BOOL)
142{
143#ifdef MEMDEBUG
144 allemobjlist.insert(this);
145 printf(" +(%6d) %p\n",(int)allemobjlist.size(),this);
146#endif
147}
148
150 si(sint), type(SHORT)
151{
152#ifdef MEMDEBUG
153 allemobjlist.insert(this);
154 printf(" +(%6d) %p\n",(int)allemobjlist.size(),this);
155#endif
156}
157
159 n(num), type(INT)
160{
161#ifdef MEMDEBUG
162 allemobjlist.insert(this);
163 printf(" +(%6d) %p\n",(int)allemobjlist.size(),this);
164#endif
165}
166
167EMObject::EMObject(unsigned int num) :
168 ui(num), type(UNSIGNEDINT)
169{
170#ifdef MEMDEBUG
171 allemobjlist.insert(this);
172 printf(" +(%6d) %p\n",(int)allemobjlist.size(),this);
173#endif
174}
175
177 type(FLOAT)
178{
179
180#ifdef MEMDEBUG
181 allemobjlist.insert(this);
182 printf(" +(%6d) %p\n",(int)allemobjlist.size(),this);
183#endif
184 if(Util::goodf(&ff)) {
185 f = ff;
186 }
187 else{
188 f = 0.0f;
189 }
190}
191
193 type(DOUBLE)
194{
195
196#ifdef MEMDEBUG
197 allemobjlist.insert(this);
198 printf(" +(%6d) %p\n",(int)allemobjlist.size(),this);
199#endif
200 if(Util::goodf(&dd)) {
201 d = dd;
202 }
203 else{
204 d = 0.0;
205 }
206}
207
208EMObject::EMObject(const char *s) :
209 str(s), type(STRING)
210{
211#ifdef MEMDEBUG
212 allemobjlist.insert(this);
213 printf(" +(%6d) %p c*\n",(int)allemobjlist.size(),this);
214#endif
215}
216
217EMObject::EMObject(const string & s) :
218 str(s), type(STRING)
219{
220#ifdef MEMDEBUG
221 allemobjlist.insert(this);
222 printf(" +(%6d) %p s\n",(int)allemobjlist.size(),this);
223#endif
224}
225
227 fp(f), type(FLOAT_POINTER)
228{
229#ifdef MEMDEBUG
230 allemobjlist.insert(this);
231 printf(" +(%6d) %p f*\n",(int)allemobjlist.size(),this);
232#endif
233}
234
236 ip(i), type(INT_POINTER)
237{
238#ifdef MEMDEBUG
239 allemobjlist.insert(this);
240 printf(" +(%6d) %p i*\n",(int)allemobjlist.size(),this);
241#endif
242}
243
245 vp(v), type(VOID_POINTER)
246{
247#ifdef MEMDEBUG
248 allemobjlist.insert(this);
249 printf(" +(%6d) %p v*\n",(int)allemobjlist.size(),this);
250#endif
251}
252
254 emdata(em), type(EMDATA)
255{
256#ifdef MEMDEBUG
257 allemobjlist.insert(this);
258 printf(" +(%6d) %p emd\n",(int)allemobjlist.size(),this);
259#endif
260}
261
263 xydata(xy), type(XYDATA)
264{
265#ifdef MEMDEBUG
266 allemobjlist.insert(this);
267 printf(" +(%6d) %p xyd\n",(int)allemobjlist.size(),this);
268#endif
269}
270
272 farray(t->get_matrix()), type(TRANSFORM)
273{
274#ifdef MEMDEBUG
275 allemobjlist.insert(this);
276 printf(" +(%6d) %p xf\n",(int)allemobjlist.size(),this);
277#endif
278}
279
281 str(ctf->to_string()), type(CTF)
282{
283#ifdef MEMDEBUG
284 allemobjlist.insert(this);
285 printf(" +(%6d) %p ctf\n",(int)allemobjlist.size(),this);
286#endif
287}
288
289EMObject::EMObject(const vector< int >& v ) :
290 iarray(v), type(INTARRAY)
291{
292#ifdef MEMDEBUG
293 allemobjlist.insert(this);
294 printf(" +(%6d) %p ia\n",(int)allemobjlist.size(),this);
295#endif
296}
297
298EMObject::EMObject(const vector < float >&v) :
299 farray(v), type(FLOATARRAY)
300{
301#ifdef MEMDEBUG
302 allemobjlist.insert(this);
303 printf(" +(%6d) %p fa\n",(int)allemobjlist.size(),this);
304#endif
305}
306
307EMObject::EMObject(const vector <string>& sarray) :
308 strarray(sarray), type(STRINGARRAY)
309{
310#ifdef MEMDEBUG
311 allemobjlist.insert(this);
312 printf(" +(%6d) %p sa\n",(int)allemobjlist.size(),this);
313#endif
314}
315
316EMObject::EMObject(const vector <Transform>& tarray) :
317 transformarray(tarray), type(TRANSFORMARRAY)
318{
319#ifdef MEMDEBUG
320 allemobjlist.insert(this);
321 printf(" +(%6d) %p xfa\n",(int)allemobjlist.size(),this);
322#endif
323}
324
325EMObject::operator bool () const
326{
327 if (type == BOOL) {
328 return b;
329 }
330 else if (type == SHORT) {
331 return si != 0;
332 }
333 else if (type == INT) {
334 return n != 0;
335 }
336 else if (type == UNSIGNEDINT) {
337 return ui != 0;
338 }
339 else if (type == FLOAT) {
340 return f != 0;
341 }
342 else if (type == DOUBLE) {
343 return d != 0;
344 }
345 else if (type == EMDATA) {
346 return emdata != 0;
347 }
348 else if (type == XYDATA) {
349 return xydata != 0;
350 }
351 else if (type == FLOAT_POINTER) {
352 return fp != 0;
353 }
354 else if (type == INT_POINTER) {
355 return ip != 0;
356 }
357 else if (type == VOID_POINTER) {
358 return vp != 0;
359 }
360// else if (type == TRANSFORM) {
361// return transform != 0;
362// }
363 // It seemed unconventional to return a boolean for the stl objects
364 else {
365 if (type != UNKNOWN) {
366 throw TypeException("Cannot convert to bool this data type ",
367 get_object_type_name(type));
368 }
369 }
370 return 0;
371}
372
373EMObject::operator short () const
374{
375 if (type == SHORT) {
376 return si;
377 }
378 else {
379 if (type != UNKNOWN) {
380 throw TypeException("Cannot convert to int this data type ",
381 get_object_type_name(type));
382 }
383 }
384 return 0;
385}
386
387EMObject::operator int () const
388{
389 if (type == INT) {
390 return n;
391 }
392 else if (type == SHORT) {
393 return si;
394 }
395 else if (type == UNSIGNEDINT) {
396 return (int) ui;
397 }
398 else if (type == FLOAT) {
399 return (int) f;
400 }
401 else if (type == DOUBLE) {
402 return (int) d;
403 }
404 else if (type == BOOL) {
405 return b?1:0;
406 }
407 else {
408 if (type != UNKNOWN) {
409 throw TypeException("Cannot convert to int this data type ",
410 get_object_type_name(type));
411 }
412 }
413 return 0;
414}
415
416EMObject::operator unsigned int () const
417{
418 if (type == UNSIGNEDINT) {
419 return (unsigned int) ui;
420 }
421 else {
422 if (type != UNKNOWN) {
423 throw TypeException("Cannot convert to int this data type ",
424 get_object_type_name(type));
425 }
426 }
427 return 0;
428}
429
430EMObject::operator float () const
431{
432 if (type == BOOL) {
433 return b?1.0f:0.0f;
434 }
435 else if (type == FLOAT) {
436 return f;
437 }
438 else if (type == SHORT) {
439 return si;
440 }
441 else if (type == INT) {
442 return (float) n;
443 }
444 else if (type == UNSIGNEDINT) {
445 return (float) ui;
446 }
447 else if (type == DOUBLE) {
448 return (float) d;
449 }
450 else if (type == STRING) {
451 return (float)atof(str.c_str());
452 }
453 else {
454 if (type != UNKNOWN) {
455 throw TypeException("Cannot convert to float from this data type",
456 get_object_type_name(type));
457 }
458 }
459
460 return 0;
461}
462
463EMObject::operator double () const
464{
465 if (type == BOOL) {
466 return b?1.0:0.0;
467 }
468 else if (type == DOUBLE) {
469 return d;
470 }
471 else if (type == SHORT) {
472 return si;
473 }
474 else if (type == INT) {
475 return (double) n;
476 }
477 else if (type == UNSIGNEDINT) {
478 return (double) ui;
479 }
480 else if (type == FLOAT) {
481 return (double) f;
482 }
483 else {
484 if (type != UNKNOWN) {
485 throw TypeException("Cannot convert to double from this data type",
486 get_object_type_name(type));
487 }
488 }
489 return 0;
490}
491
492EMObject::operator int * () const
493{
494 if (type != INT_POINTER)
495 {
496 if (type != UNKNOWN)
497 throw TypeException("Cannot convert to float pointer from this data type",
498 get_object_type_name(type));
499
500 return 0;
501 }
502
503 return ip;
504}
505
506
507EMObject::operator float * () const
508{
509 if (type != FLOAT_POINTER)
510 {
511 if (type != UNKNOWN)
512 throw TypeException("Cannot convert to float pointer from this data type",
513 get_object_type_name(type));
514
515 return 0;
516 }
517
518 return fp;
519}
520
521// MAYBE REMOVE? FIXME
522EMObject::operator void * () const
523{
524 if (type == VOID_POINTER) return vp;
525 else if (type == FLOAT_POINTER) return (void *)fp;
526 else if (type == INT_POINTER) return (void *)ip;
527 else if (type == EMDATA) return (void *) emdata;
528 else if (type == XYDATA) return (void *) xydata;
529// else if (type == TRANSFORM) return (void *) transform;
530 else throw TypeException("Cannot convert to void pointer from this data type", get_object_type_name(type));
531}
532
533EMObject::operator const char * () const
534{
535 if (type != STRING && type != CTF) {
536 stringstream ss;
537 string return_string;
538 if ( type == INT )
539 {
540 ss << n;
541 ss >> return_string;
542 return return_string.c_str();
543 }
544 if ( type == UNSIGNEDINT )
545 {
546 ss << ui;
547 ss >> return_string;
548 return return_string.c_str();
549 }
550 else
551 if ( type == FLOAT )
552 {
553 ss << f;
554 ss >> return_string;
555 return return_string.c_str();
556 }
557 else
558 if ( type == DOUBLE )
559 {
560 ss << d;
561 ss >> return_string;
562 return return_string.c_str();
563 }
564 else if (type != UNKNOWN) {
565 throw TypeException("Cannot convert to string from this data type",
566 get_object_type_name(type));
567 }
568
569 return "";
570 }
571 return str.c_str();
572}
573
574EMObject::operator EMData * () const
575{
576 if (type != EMDATA) {
577 if (type != UNKNOWN) {
578 throw TypeException("Cannot convert to EMData* from this data type",
579 get_object_type_name(type));
580 }
581 return 0;
582 }
583 return emdata;
584}
585
586EMObject::operator XYData * () const
587{
588 if (type != XYDATA) {
589 if (type != UNKNOWN) {
590 throw TypeException("Cannot convert to XYData* from this data type",
591 get_object_type_name(type));
592 }
593 return 0;
594 }
595 return xydata;
596}
597
598EMObject::operator Transform* () const
599{
600 if(type != TRANSFORM) {
601 if(type != UNKNOWN) {
602 throw TypeException("Cannot convert to TRANSFORM* from this data type",
603 get_object_type_name(type));
604 }
605 }
606 Transform * transform = new Transform();
607 transform->set_matrix(farray);
608 return transform;
609}
610
611EMObject::operator Ctf* () const
612{
613/* if(type != CTF) {
614 if(type != CTF) {
615 throw TypeException("Cannot convert to TRANSFORM* from this data type",
616 get_object_type_name(type));
617 }
618 }*/
619 Ctf * ctf = 0;
620 if(str[0] == 'O') {
621 ctf = new EMAN1Ctf();
622 ctf->from_string(str);
623 }
624 else if(str[0] == 'E') {
625 ctf = new EMAN2Ctf();
626 ctf->from_string(str);
627 }
628 return ctf;
629}
630
631EMObject::operator vector<int>() const
632{
633 if( type != INTARRAY )
634 {
635 if( type != UNKNOWN ) {
636 throw TypeException("Cannot convert to vector<int> from this data type", get_object_type_name(type) );
637 }
638 return vector<int>();
639 }
640 return iarray;
641}
642
643EMObject::operator vector < float > () const
644{
645 if (type != FLOATARRAY) {
646 if (type != UNKNOWN) {
647 throw TypeException("Cannot convert to vector<float> from this data type",
648 get_object_type_name(type));
649 }
650 return vector < float >();
651 }
652 return farray;
653}
654
655EMObject::operator vector<string> () const
656{
657 if (type != STRINGARRAY) {
658 if (type != UNKNOWN) {
659 throw TypeException("Cannot convert to vector<string> from this data type",
660 get_object_type_name(type));
661 }
662 return vector<string>();
663 }
664 return strarray;
665}
666
667EMObject::operator vector<Transform> () const
668{
669 if(type != TRANSFORMARRAY) {
670 if (type != UNKNOWN) {
671 throw TypeException("Cannot convert to vector<string> from this data type",
672 get_object_type_name(type));
673 }
674 return vector<Transform>();
675 }
676 return transformarray;
677}
678
680{
681 return (type == UNKNOWN);
682}
683
684string EMObject::to_str() const
685{
686 return to_str(type);
687}
688
689string EMObject::to_str(ObjectType argtype) const
690{
691 if (argtype == STRING) {
692 return str;
693 }
694 else {
695 char tmp_str[32];
696 if (argtype == BOOL) {
697 if (b)
698 sprintf(tmp_str, "true");
699 else
700 sprintf(tmp_str, "false");
701 }
702 else if (argtype == SHORT) {
703 sprintf(tmp_str, "%hd", si);
704 }
705 else if (argtype == INT) {
706 sprintf(tmp_str, "%d", n);
707 }
708 else if (argtype == UNSIGNEDINT) {
709 sprintf(tmp_str, "%d", ui);
710 }
711 else if (argtype == FLOAT) {
712 sprintf(tmp_str, "%f", f);
713 }
714 else if (argtype == DOUBLE) {
715 sprintf(tmp_str, "%f", d);
716 }
717 else if (argtype == EMDATA) {
718 sprintf(tmp_str, "EMDATA");
719 }
720 else if (argtype == FLOAT_POINTER) {
721 sprintf(tmp_str, "FLOAT_POINTER");
722 }
723 else if (argtype == INT_POINTER) {
724 sprintf(tmp_str, "INT_POINTER");
725 }
726 else if (argtype == VOID_POINTER) {
727 sprintf(tmp_str, "VOID_POINTER");
728 }
729 else if (argtype == XYDATA) {
730 sprintf(tmp_str, "XYDATA");
731 }
732 else if (argtype == INTARRAY) {
733 sprintf(tmp_str, "INTARRAY");
734 }
735 else if (argtype == FLOATARRAY) {
736 sprintf(tmp_str, "FLOATARRAY");
737 }
738 else if (argtype == STRINGARRAY) {
739 sprintf(tmp_str, "STRINGARRAY");
740 }
741 else if (argtype == TRANSFORM) {
742 sprintf(tmp_str, "TRANSFORM");
743 }
744 else if (argtype == TRANSFORMARRAY) {
745 sprintf(tmp_str, "TRANSFORMARRAY");
746 }
747 else if (argtype == CTF) {
748 sprintf(tmp_str, "CTF");
749 }
750 else if (argtype == UNKNOWN) {
751 sprintf(tmp_str, "UNKNOWN");
752 }
753 else {
754 LOGERR("No such EMObject defined");
755 throw NotExistingObjectException("EMObject", "unknown type");
756 }
757 return string(tmp_str);
758 }
759}
760
762{
763 if ( type_registry.find(t) != type_registry.end() )
764 return type_registry[t];
765 else
766 LOGERR("No such EMObject defined");
767 throw NotExistingObjectException("EMObject", "unknown type");
768}
769
770bool EMAN::operator==(const EMObject &e1, const EMObject & e2)
771{
772
773 if (e1.type != e2.type) {
774 return false;
775 }
776
777 switch (e1.type) {
778 case EMObject::BOOL:
779 return (e1.b == e2.b);
780 break;
781 case EMObject::SHORT:
782 return (e1.si == e2.si);
783 break;
784 case EMObject::INT:
785 return (e1.n == e2.n);
786 break;
788 return (e1.ui == e2.ui);
789 break;
790 case EMObject::FLOAT:
791 return (e1.f == e2.f);
792 break;
793 case EMObject::DOUBLE:
794 return (e1.d == e2.d);
795 break;
796 case EMObject::CTF:
797 case EMObject::STRING:
798 return (e1.str == e2.str);
799 break;
801 return (e1.fp == e2.fp);
802 break;
804 return (e1.ip == e2.ip);
805 break;
807 return (e1.vp == e2.vp);
808 break;
809 case EMObject::EMDATA:
810 return (e1.emdata == e2.emdata);
811 break;
812 case EMObject::XYDATA:
813 return (e1.xydata == e2.xydata);
814 break;
817 if (e1.farray.size() == e2.farray.size()) {
818 for (size_t i = 0; i < e1.farray.size(); i++) {
819 if (e1.farray[i] != e2.farray[i]) {
820 return false;
821 }
822 }
823 return true;
824 }
825 else {
826 return false;
827 }
828 break;
830 if (e1.iarray.size() == e2.iarray.size()) {
831 for (size_t i = 0; i < e1.iarray.size(); i++) {
832 if (e1.iarray[i] != e2.iarray[i]) {
833 return false;
834 }
835 }
836 return true;
837 }
838 break;
840 if (e1.strarray.size() == e2.strarray.size()) {
841 for (size_t i = 0; i < e1.strarray.size(); i++) {
842 if (e1.strarray[i] != e2.strarray[i]) {
843 return false;
844 }
845 }
846 return true;
847 }
848 else {
849 return false;
850 }
851 break;
853 if (e1.transformarray.size() == e2.transformarray.size()) {
854 for (size_t i = 0; i < e1.transformarray.size(); i++) {
855 if (e1.transformarray[i] != e2.transformarray[i]) {
856 return false;
857 }
858 }
859 }
860 break;
862 // UNKNOWN really means "no type" and if two objects both have
863 // type UNKNOWN they really are the same
864 return (e1.type == e2.type);
865 break;
866 default:
867 return false;
868 break;
869 }
870 return false;
871}
872
873bool EMAN::operator!=(const EMObject &e1, const EMObject & e2)
874{
875 return !(e1 == e2);
876}
877
878// Copy constructor
880{
881 // init isn't necessary because that must have already called it!
882 *this = that;
883#ifdef MEMDEBUG
884 allemobjlist.insert(this);
885 printf(" +(%6d) %p\n",(int)allemobjlist.size(),this);
886#endif
887}
888
889
890// Assignment operator - - copies only the variable associated with the type of the argument.
891// It would be possible just to do a dumb copy of everything, but that seems opposed to
892// the concept of an EMObject, which is always of a single type.
894{
895
896// This test breaks assignment when either the current or assigned values are (float)nan
897// it's also somewhat inherently stupid, since testing for equivalence may cost as much
898// as assignment.
899// if ( *this != that )
900 {
901 // First store the type of the input, At first I forgot to do this and it was a very
902 // difficult bug to track down
903 type = that.type;
904
905// if ( type != this_type ) throw
906
907
908 switch (type)
909 {
910 case BOOL:
911 b = that.b;
912 break;
913 case SHORT:
914 si = that.si;
915 break;
916 case INT:
917 n = that.n;
918 break;
919 case UNSIGNEDINT:
920 ui = that.ui;
921 break;
922 case FLOAT:
923 f = that.f;
924 break;
925 case DOUBLE:
926 d = that.d;
927 break;
928 case CTF:
929 case STRING:
930 str = that.str;
931 break;
932 case FLOAT_POINTER:
933 // Warning - Pointer address copy.
934 fp = that.fp;
935 break;
936 case INT_POINTER:
937 // Warning - Pointer address copy.
938 ip = that.ip;
939 break;
940 case VOID_POINTER:
941 // Warning - Pointer address copy.
942 vp = that.vp;
943 break;
944 case EMDATA:
945 // Warning - Pointer address copy.
946 emdata = that.emdata;
947 break;
948 case XYDATA:
949 // Warning - Pointer address copy.
950 xydata = that.xydata;
951 break;
952 case TRANSFORM:
953 case FLOATARRAY:
954 farray = that.farray;
955 break;
956 case INTARRAY:
957 iarray = that.iarray;
958 break;
959 case STRINGARRAY:
960 strarray = that.strarray;
961 break;
962 case TRANSFORMARRAY:
964 break;
965 case UNKNOWN:
966 // This is possible, nothing should happen
967 // The EMObject's default constructor has been called and
968 // as yet has no type - doing nothing is exactly as the
969 // the assignment operator should work.
970 break;
971 default:
972 LOGERR("No such EMObject defined");
973 throw NotExistingObjectException("EMObject", "unknown type");
974 break;
975 }
976 }
977// else
978// {
979// cerr << "Warning - attempt to assign EMObject onto itself. No action taken" << endl;
980// cerr << "My type is " << get_object_type_name(type) << endl;
981// }
982
983 return *this;
984}
985
986//-------------------------------TypeDict--------------------------------------------
987
989{
990 map < string, string >::iterator p;
991 for (p = type_dict.begin(); p != type_dict.end(); p++) {
992 printf("\t%s %s %s\n",
993 p->first.c_str(), p->second.c_str(), desc_dict[p->first].c_str());
994 }
995}
996
997//-------------------------------Dict--------------------------------------------
998
999Dict::Dict(const Dict& that)
1000{
1001 *this = that;
1002}
1003
1005{
1006 if ( this != &that )
1007 {
1008 dict.clear();
1009 copy(that.begin(), that.end(), inserter(dict, dict.begin()));
1010 // or use this
1011 // dict.insert( that.begin(), that.end());
1012 }
1013 else
1014 {
1015 cerr << "Warning - attempted to assign a Dict object to itself. No action taken" << endl;
1016 }
1017
1018 return *this;
1019}
1020
1021bool EMAN::operator==(const Dict& d1, const Dict& d2)
1022{
1023 // Just make use of map's version of operator==
1024 return (d1.dict == d2.dict);
1025}
1026
1027bool EMAN::operator!=(const Dict& d1, const Dict& d2)
1028{
1029 return !(d1 == d2);
1030}
1031
1032void Dict::update(const Dict& that)
1033{
1034 for (Dict::const_iterator p=that.begin(); p!=that.end(); p++) {
1035 dict[p->first]=p->second;
1036 }
1037}
1038
1039
1040// Iterator support
1041// This is just a wrapper, everything is inherited from the map<string,EMObject>::iterator
1042// so the interface is the same as you would expect
1043// iterator support added by d.woolford May 2007
1044
1046{
1047 return iterator( dict.begin() );
1048}
1049
1051{
1052 return const_iterator( (map < string, EMObject >::const_iterator) dict.begin() );
1053}
1054
1055// Wraps map.find(const string& key)
1056Dict::iterator Dict::find( const string& key )
1057{
1058 return iterator( dict.find(key) );
1059}
1060
1062{
1063 return iterator( dict.end() );
1064}
1065
1067{
1068 return const_iterator( (map < string, EMObject >::const_iterator)dict.end() );
1069}
1070
1071Dict::const_iterator Dict::find( const string& key ) const
1072{
1073 return const_iterator( (map < string, EMObject >::const_iterator)dict.find(key) );
1074}
1075
1076//
1077// iterator
1078//
1079Dict::iterator::iterator( map< string, EMObject >::iterator parent_it ) :
1080 map< string, EMObject >::iterator( parent_it )
1081{
1082}
1083
1084
1086 map < string, EMObject >::iterator( that )
1087{
1088}
1089
1090
1092{
1093 if( this != &that )
1094 {
1095 map < string, EMObject >::iterator::operator=( that );
1096 }
1097 return *this;
1098}
1099
1100//
1101// const_iterator
1102//
1103
1104Dict::const_iterator::const_iterator( const map < string, EMObject >::const_iterator parent_it ) :
1105 map< string, EMObject >::const_iterator( parent_it )
1106{
1107}
1108
1110 map< string, EMObject >::const_iterator(it)
1111{
1112}
1113
1115 map< string, EMObject >::const_iterator(it)
1116{
1117}
1118
1120{
1121 if( this != &that )
1122 {
1123 map < string, EMObject >::const_iterator::operator=( that );
1124 }
1125 return *this;
1126}
1127
1128EMObject Dict::get_ci(const string & key) const
1129{
1130 string lower_key = Util::str_to_lower(key);
1131
1132 for (map < string, EMObject >::const_iterator it = dict.begin(); it != dict.end(); ++it ) {
1133 string lower = Util::str_to_lower(it->first);
1134 if (lower == lower_key) return it->second;
1135 }
1136
1137 throw NotExistingObjectException("EMObject", "Nonexisting key (" + key + ") in Dict");
1138}
1139
1140bool Dict::has_key_ci(const string & key) const
1141{
1142 string lower_key = Util::str_to_lower(key);
1143
1144 for (map < string, EMObject >::const_iterator it = dict.begin(); it != dict.end(); ++it ) {
1145 string lower = Util::str_to_lower(it->first);
1146 if (lower == lower_key) return true;
1147 }
1148 return false;
1149}
Ctf is the base class for all CTF model.
Definition: ctf.h:60
virtual int from_string(const string &ctf)=0
Const iterator support for the Dict object This is just a wrapper, everything is inherited from the m...
Definition: emobject.h:674
const_iterator(const map< string, EMObject >::const_iterator parent_it)
Definition: emobject.cpp:1104
const_iterator & operator=(const const_iterator &that)
Definition: emobject.cpp:1119
Non const iterator support for the Dict object This is just a wrapper, everything is inherited from t...
Definition: emobject.h:653
iterator & operator=(const iterator &that)
Definition: emobject.cpp:1091
iterator(map< string, EMObject >::iterator parent_it)
Definition: emobject.cpp:1079
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
bool has_key_ci(const string &key) const
Ask the Dictionary if it as a particular key in a case insensitive way.
Definition: emobject.cpp:1140
iterator end()
Definition: emobject.cpp:1061
Dict & operator=(const Dict &that)
Assignment operator Copies all elements in dict.
Definition: emobject.cpp:1004
iterator begin()
Definition: emobject.cpp:1045
iterator find(const string &key)
Definition: emobject.cpp:1056
map< string, EMObject > dict
Definition: emobject.h:642
EMObject get_ci(const string &key) const
Get the EMObject corresponding to the particular key using case insensitivity.
Definition: emobject.cpp:1128
void update(const Dict &that)
Update replaces values from that into this without otherwise altering this.
Definition: emobject.cpp:1032
EMAN1Ctf is the CTF model used in EMAN1.
Definition: ctf.h:120
EMAN2Ctf is the default CTF model used in EMAN2.
Definition: ctf.h:237
EMData stores an image's data and defines core image processing routines.
Definition: emdata.h:82
EMObject is a wrapper class for types including int, float, double, etc as defined in ObjectType.
Definition: emobject.h:123
vector< Transform > transformarray
Definition: emobject.h:277
EMObject & operator=(const EMObject &that)
Assigment operator copies pointer locations (emdata, xydata, transform) - does not take ownership dee...
Definition: emobject.cpp:893
string str
Definition: emobject.h:273
string to_str() const
Calls to_str( this->type)
Definition: emobject.cpp:684
bool is_null() const
Checks to see if the EMObject is interpretable This basically equates to checking to see if the type ...
Definition: emobject.cpp:679
EMObject()
Constructors for each type More types could be added, but all of the accompanying functions would hav...
Definition: emobject.cpp:131
ObjectType type
Definition: emobject.h:278
~EMObject()
Desctructor Does not free pointers.
Definition: emobject.cpp:124
EMData * emdata
Definition: emobject.h:269
void printInfo() const
A debug function that prints as much information as possibe to cout.
Definition: emobject.cpp:112
unsigned int ui
Definition: emobject.h:263
static string get_object_type_name(ObjectType t)
Get an ObjectType as a string statically Can be accessed without the instantiation of a class object.
Definition: emobject.cpp:761
vector< string > strarray
Definition: emobject.h:276
vector< float > farray
Definition: emobject.h:275
vector< int > iarray
Definition: emobject.h:274
float * fp
Definition: emobject.h:266
XYData * xydata
Definition: emobject.h:270
static map< ObjectType, string > type_registry
Definition: emobject.h:287
A Transform object is a somewhat specialized object designed specifically for EMAN2/Sparx storage of ...
Definition: transform.h:75
void set_matrix(const vector< float > &v)
Set the transformation matrix using a vector.
Definition: transform.cpp:147
map< string, string > type_dict
Definition: emobject.h:356
map< string, string > desc_dict
Definition: emobject.h:357
static string str_to_lower(const string &s)
Return a lower case version of the argument string.
Definition: util.cpp:283
static int goodf(const float *p_f)
Check whether a number is a good float.
Definition: util.h:1112
XYData defines a 1D (x,y) data set.
Definition: xydata.h:47
EMData * copy() const
This file is a part of "emdata.h", to use functions in this file, you should "#include "emdata....
vector< pair< float, int > > vp
#define NotExistingObjectException(objname, desc)
Definition: exception.h:130
#define TypeException(desc, type)
Definition: exception.h:261
#define LOGERR
Definition: log.h:51
E2Exception class.
Definition: aligner.h:40
bool operator!=(const EMObject &e1, const EMObject &e2)
Definition: emobject.cpp:873
bool operator==(const EMObject &e1, const EMObject &e2)
Definition: emobject.cpp:770