2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
27 /*=========================================================================
29 Program: instantHandler
30 Module: $RCSfile: InstantHandler_Txx.h,v $
32 Date: $Date: 2012/11/15 12:12:26 $
33 Version: $Revision: 1.2 $
34 Objective: Handles the change of instant among the concept already inscribed
35 Authot: Monica Maria Lozano Romero
40 This software is distributed WITHOUT ANY WARRANTY; without even
41 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
42 PURPOSE. See the above copyright notice for more information.
45 =========================================================================*/
54 //#include "Instant.h"
61 //====== LIFECYCLE ========
64 InstantHandler<P>::InstantHandler(SomeEnvironment<P>* environment)throw (ViewerNDimensionsException)
68 this->hasPrevious=true;
69 this->actualConcept=NULL;
74 this->environment=environment;
75 this->actualInstant=new Instant(environment->getNumberOfConcepts());
79 this->environment=NULL;
80 this->actualInstant=NULL;
81 throw new ViewerNDimensionsException("ERROR ENVIRONMENT NULL");
87 InstantHandler<P>::~InstantHandler()
90 //deleting conceptsToChange
91 size=getSizeConceptsToChange();
94 if(conceptsToChange[i])
95 delete conceptsToChange[i];
97 this->conceptsToChange.clear();
98 //deleting actual concept
99 if(actualInstant) delete actualInstant;
105 //====== OPERATIONS =======
107 * Adds a concept to handled in the instant, it means, when
108 * the next instant is used we are going to change this concepts in the instant
110 * the calls to this functions are made in order of position, where the last position (it means
111 * the higher position defined) is the concept thats is going to handled the real time
112 * THE LAST POSITION HANDLEDS THE REAL TIME DOESNT MATTER THE CONCEPT
114 * The user wants to reproduce doctors, pacients and time, and positions given are:
115 * 0=reproduce doctors, 1= reproduce pacients, 2= time, then 1,2 are going to be reproduced
116 * by PLUS PLUS, and time by REAL TIME
117 * @param nameConcept, name of the concept
118 * @param mode, mode of change
119 * @param position, position of reproduction
120 * @return true if succesful, false otherwise
123 bool InstantHandler<P>::addConceptToHandled(std::string nameConcept,int mode,int position) throw (ViewerNDimensionsException)
127 //creating the concept
128 ConceptInstantHandler* conceptInstantHandler=new ConceptInstantHandler(nameConcept,mode,position);
129 //looking for its index
130 int indexInInstant=environment->getIndexConcept(nameConcept);
131 if(indexInInstant!=-1)
132 conceptInstantHandler->setIndexInInstant(indexInInstant);
134 throw new ViewerNDimensionsException("ERROR CONCEPT ISN'T IN ENVIRONMENT");
135 //setting it maximums
136 int maximumIndex=environment->getSizeConcept(nameConcept);
138 conceptInstantHandler->setMaximumIndex(maximumIndex);
140 throw new ViewerNDimensionsException("ERROR CONCEPT ISN'T IN ENVIRONMENT");
142 this->conceptsToChange.push_back(conceptInstantHandler);
143 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
144 actualInstant->setConcept(indexInInstant,conceptInstantHandler->geActualIndex());
155 * Next instant of the concepts to change
157 * conceptsToChange is ordered by the position of the concepts handled
162 void InstantHandler<P>::nextInstant() throw (ViewerNDimensionsException)
166 bool hasNextIndex=actualConcept->hasNextIndex();
167 int position=actualConcept->getPosition();
170 actualConcept->nextIndex();
171 actualInstant->setConcept(actualConcept->getIndexInInstant(),actualConcept->geActualIndex());
176 while( (position-1)>=0 && !hasNextIndex)
178 actualConcept=conceptsToChange[position-1];
179 hasNextIndex=actualConcept->hasNextIndex();
180 position=actualConcept->getPosition();
184 actualConcept->nextIndex();
185 actualInstant->setConcept(actualConcept->getIndexInInstant(),actualConcept->geActualIndex());
187 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
190 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
197 throw new ViewerNDimensionsException("ERROR THERE ISN'T CONCEPTS TO HANDLED IN THE PLAYER");
202 * Previous instant of the actual concept
207 void InstantHandler<P>::previousInstant()throw (ViewerNDimensionsException)
211 bool hasPreviousIndex=actualConcept->hasPreviousIndex();
212 int position=actualConcept->getPosition();
215 actualConcept->previousIndex();
216 actualInstant->setConcept(actualConcept->getIndexInInstant(),actualConcept->geActualIndex());
221 while( (position-1)>=0 && !hasPreviousIndex)
223 actualConcept=conceptsToChange[position-1];
224 hasPreviousIndex=actualConcept->hasPreviousIndex();
225 position=actualConcept->getPosition();
229 actualConcept->previousIndex();
230 actualInstant->setConcept(actualConcept->getIndexInInstant(),actualConcept->geActualIndex());
232 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
235 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
242 throw new ViewerNDimensionsException("ERROR THERE ISN'T CONCEPTS TO HANDLED IN THE PLAYER");
246 * Removes all the concept handled
249 void InstantHandler<P>::removeAllConcepts()
252 size=getSizeConceptsToChange();
255 if(conceptsToChange[i])
256 delete conceptsToChange[i];
258 conceptsToChange.clear();
264 * Removes the concept with the name given
265 * @param nameConcept, name of the concept to remove
266 * WARNING the user know wich concept is erasing, also knows the
267 * mode of other concepts, if the user is deleting the concept that
268 * handled the real time, should change the mode to other concept that exist
271 void InstantHandler<P>::removeConceptToHandled(std::string nameConcept)
274 size=getSizeConceptsToChange();
275 std::vector<ConceptInstantHandler*>::iterator iteratorConcepts=conceptsToChange.begin();
276 ConceptInstantHandler* concepti;
277 std::string nameConcepti;
278 bool isConcept=false;
279 for(i=0;i<size && !isConcept;i++)
281 concepti=conceptsToChange[i];
282 nameConcepti=concepti->getName();
283 if(nameConcepti.compare(nameConcept)==0)
285 if(actualConcept==concepti)
287 conceptsToChange.erase(iteratorConcepts);
288 actualConcept=conceptsToChange[size-1];
296 * sets all the concept to handled in 0
299 void InstantHandler<P>::resetConceptsHandled()
302 size=getSizeConceptsToChange();
303 ConceptInstantHandler* concepti;
304 int indexInInstant=-1;
307 concepti=conceptsToChange[i];
308 concepti->setActualIndex(0);
309 indexInInstant=concepti->getIndexInInstant();
310 actualInstant->setConcept(indexInInstant,0);
320 void InstantHandler<P>::pause()
322 time_t seconds=time(NULL);
323 pauseTime=seconds*1000;
326 //====== INQUIRY =========
329 * get the actual instant fot the concept identified by the name
332 Instant* InstantHandler<P>::getActualInstant()throw (ViewerNDimensionsException)
334 if(this->actualInstant)
335 return this->actualInstant;
337 throw new ViewerNDimensionsException("ERROR ACTUAL INSTANT IS NOT SETTED");
342 * If the actual concept has next instant, it means, if the index of the concept
343 * in the instant is in the maximum value
344 * @param nameConcept, concept for which we need to know if we are
345 * in its maximum value
346 * @return true if IS NOT in the maximum, false otherwise
349 bool InstantHandler<P>::hasNextInstant()
357 * If the concept has previous instant, it means, if the index of the concept
358 * in the instant is positive
359 * @param nameConcept, concept for which we need to know if we are
361 * @return true if IS NOT in zero, false otherwise
364 bool InstantHandler<P>::hasPreviousInstant()
369 * returns the number con conceptsToHandled
372 int InstantHandler<P>::getSizeConceptsToChange()
374 return conceptsToChange.size();
377 * returns the number of possibles instants of the handler
378 * it depends of the concepts added in conceptsTochange
381 int InstantHandler<P>::getNumOfInstants()
383 int i,size,numInstants=1;
384 size=getSizeConceptsToChange();
385 ConceptInstantHandler* concepti;
388 concepti=conceptsToChange[i];
389 numInstants*=concepti->getMaximumIndex();
396 * Returns the names of the concepts handled and its modes
400 void InstantHandler<P>::getConceptsInfo(std::vector<std::string>& namesConcepts,std::vector<int>& modes)
403 size=getSizeConceptsToChange();
404 ConceptInstantHandler* concepti;
407 concepti=conceptsToChange[i];
408 namesConcepts.push_back(concepti->getName());
409 modes.push_back(concepti->getMode());
413 * Returns the name of the concept that handled the real time
417 void InstantHandler<P>::getTimeConcept(std::string& nameConcept)
419 ConceptInstantHandler* concept=conceptsToChange[getSizeConceptsToChange()-1];
420 nameConcept=concept->getName();
423 //====== ACCESS ==========
425 * Sets the environment that is going to be handled
426 * @param environment, a pointer to the environmen
430 void InstantHandler<P>::setEnvironment(SomeEnvironment<P>* environment)
432 this->environment=environment;
435 * Sets the actual instant of the instant handler
438 void InstantHandler<P>::setActualInstant(Instant* instant)
441 actualInstant=new Instant(instant->getInstant());
445 size=actualInstant->getSize();
447 actualInstant->setConcept(i,instant->getIndexInConcept(i));
449 setConceptsToChange();
453 * Sets the time of reproduction to the concept
454 * that depends of the real time of the reproduction
456 * @param timeReproduction
459 void InstantHandler<P>::setTimeReproduction(double time)
461 if(getSizeConceptsToChange()>0)
463 ConceptInstantHandler* concept=conceptsToChange[getSizeConceptsToChange()-1];
464 concept->setTimeReproduction(time);
468 * Sets the actual time of the reproduction,
469 * is the real time (seconds)
473 void InstantHandler<P>::setActualTime(double actualTime)
475 ConceptInstantHandler* concept=conceptsToChange[getSizeConceptsToChange()-1];
476 concept->setActualTime(actualTime);
477 this->actualTime=actualTime;
480 * Set the mode of a concept that's being handled
483 void InstantHandler<P>::setModeOfConcept(std::string nameConcept,int mode)
486 size=getSizeConceptsToChange();
487 ConceptInstantHandler* concepti;
488 std::string nameConcepti;
489 bool isConcept=false;
490 for(i=0;i<size && !isConcept;i++)
492 concepti=conceptsToChange[i];
493 nameConcepti=concepti->getName();
494 if(nameConcepti.compare(nameConcept)==0)
497 concepti->setMode(mode);
506 void InstantHandler<P>::set()
508 if(getSizeConceptsToChange()!=0)
509 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
512 * Set the increase Value for changing the instant
515 void InstantHandler<P>::setIncreaseValue(int increase)
518 size=conceptsToChange.size();
519 ConceptInstantHandler* concepti;
522 concepti=conceptsToChange[i];
523 concepti->setIncreaseValue(increase);
528 * Set the decrease Value for changing the instant
531 void InstantHandler<P>::setDecreaseValue(int decrease)
534 size=conceptsToChange.size();
535 ConceptInstantHandler* concepti;
538 concepti=conceptsToChange[i];
539 concepti->setDecreaseValue(decrease);
543 //====== PRIVATE METHODS=========
546 * Sets if the instant has next or not
550 void InstantHandler<P>::setHasNext()
552 int i, sizeConceptsToChange;
553 ConceptInstantHandler* concepti;
555 sizeConceptsToChange=getSizeConceptsToChange();
556 for(i=0;i<sizeConceptsToChange && !hasNext;i++)
558 concepti=conceptsToChange[i];
559 if(concepti->hasNextIndex())
566 * Sets if the instant has previous or not
569 void InstantHandler<P>::setHasPrevious()
571 int i, sizeConceptsToChange;
572 ConceptInstantHandler* concepti;
574 sizeConceptsToChange=getSizeConceptsToChange();
575 for(i=0;i<sizeConceptsToChange && !hasPrevious;i++)
577 concepti=conceptsToChange[i];
578 if(concepti->hasPreviousIndex())
583 * actualize the actual index of the concepts handled
586 void InstantHandler<P>::setConceptsToChange()
588 int i,sizeConceptsToChange, indexInInstant,indexInConcept;
589 sizeConceptsToChange=getSizeConceptsToChange();
590 ConceptInstantHandler* concepti;
591 for(i=0;i<sizeConceptsToChange;i++)
593 concepti=conceptsToChange[i];
594 indexInInstant=concepti->getIndexInInstant();
595 indexInConcept=actualInstant->getIndexInConcept(indexInInstant);
596 concepti->setActualIndex(indexInConcept);
600 * resets all the next concepts to zero
603 void InstantHandler<P>::resetNextConcepts()
606 index=actualConcept->getPosition();
607 size=getSizeConceptsToChange();
608 ConceptInstantHandler* concepti;
609 for(i=index+1;i<size;i++)
611 concepti=conceptsToChange[i];
612 concepti->setActualIndex(0);
613 concepti->resetInitialTime();
614 actualInstant->setConcept(concepti->getIndexInInstant(),0);
619 * Sets the next index of concepts to handled to the maximum value
622 void InstantHandler<P>::setNextConcepts()
625 index=actualConcept->getPosition();
626 size=getSizeConceptsToChange();
627 ConceptInstantHandler* concepti;
628 for(i=index+1;i<size;i++)
630 concepti=conceptsToChange[i];
631 concepti->setActualIndex(concepti->getMaximumIndex()-1);
632 //concepti->resetInitialTime();
633 actualInstant->setConcept(concepti->getIndexInInstant(),concepti->geActualIndex());
637 * this method locates the new actual concept
638 * @param next, if has to locate the actual concept to the first concept that has next
639 * next=true, else search for the first concept that has previous
642 void InstantHandler<P>::toLocateActualConcept(bool next)
644 int size=getSizeConceptsToChange();
650 bool hasNextIndex=actualConcept->hasNextIndex();
651 int position=actualConcept->getPosition();
652 while( (position-1)>=0 && !hasNextIndex)
654 actualConcept=conceptsToChange[position-1];
655 hasNextIndex=actualConcept->hasNextIndex();
656 position=actualConcept->getPosition();
662 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
669 bool hasPreviousIndex=actualConcept->hasPreviousIndex();
670 int position=actualConcept->getPosition();
671 while( (position-1)>=0 && !hasPreviousIndex)
673 actualConcept=conceptsToChange[position-1];
674 hasPreviousIndex=actualConcept->hasPreviousIndex();
675 position=actualConcept->getPosition();
681 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];