1 |
#include "ConstraintAlgorithm.hpp" |
2 |
#include "ConstraintPair.hpp" |
3 |
#include "SimInfo.hpp" |
4 |
#include "ConstraintManager.hpp" |
5 |
|
6 |
//////////////////////////////////////////////////////////////////////////////// |
7 |
//Implementation of ConstraintAlgorithm |
8 |
//////////////////////////////////////////////////////////////////////////////// |
9 |
ConstraintAlgorithm::ConstraintAlgorithm(SimInfo* rhs){ |
10 |
info = rhs; |
11 |
cpIter = info->consMan->creatPairIterator(); |
12 |
ceIter = info->consMan->creatElementIterator(); |
13 |
} |
14 |
|
15 |
ConstraintAlgorithm::~ConstraintAlgorithm(){ |
16 |
map<TypeInfo, CallbackFunctor*>::iterator callbackIter; |
17 |
|
18 |
//destroy callbackMap |
19 |
for(callbackIter = callbackMap.begin(); callbackIter != callbackMap.end(); ++callbackIter){ |
20 |
delete callbackIter->second; |
21 |
} |
22 |
callbackMap.erase(callbackMap.begin(), callbackMap.end()); |
23 |
|
24 |
//destroy iterators of constraint element and constraint pair |
25 |
delete cpIter; |
26 |
delete ceIter; |
27 |
} |
28 |
|
29 |
void ConstraintAlgorithm::doConstrain(){ |
30 |
const int maxConsIteration = 30; |
31 |
bool done; |
32 |
int iteration; |
33 |
int maxIteration; |
34 |
double tolerance; |
35 |
ConstraintElement* consElem; |
36 |
ConstraintPair* consPair; |
37 |
int exeStatus; |
38 |
|
39 |
|
40 |
|
41 |
for(ceIter->first(); !ceIter->isEnd(); ceIter->next()){ |
42 |
consElem = ceIter->currentItem(); |
43 |
consElem->setMoved(true); |
44 |
consElem->setMoving(false); |
45 |
} |
46 |
|
47 |
done = false; |
48 |
iteration = 0; |
49 |
|
50 |
//main loop of constraint algorithm |
51 |
while(!done && iteration < maxConsIteration){ |
52 |
done = true; |
53 |
|
54 |
//loop over every constraint pair |
55 |
for(cpIter->first(); !cpIter->isEnd(); cpIter->next()){ |
56 |
|
57 |
consPair = cpIter->currentItem(); |
58 |
|
59 |
//dispatch constraint algorithm |
60 |
if(consPair->isMoved()) |
61 |
exeStatus = doConstrainPair(consPair); |
62 |
|
63 |
switch(exeStatus){ |
64 |
case consFail: |
65 |
cerr << "ConstraintAlgorithm::doConstrain() Error: Constraint Fail" << endl; |
66 |
break; |
67 |
case consSuccess: |
68 |
//constrain the pair by moving two elements |
69 |
done = false; |
70 |
consPair->firstElem->setMoving(true); |
71 |
consPair->secondElem->setMoving(true); |
72 |
break; |
73 |
case consAlready: |
74 |
//current pair is already constrained, do not need to move the elements |
75 |
break; |
76 |
case consPairHandlerFail: |
77 |
//can not found call back functor for constraint pair |
78 |
cerr << "ConstraintAlgorithm::doConstrain() Error: can not found callback functor for constraint pair " << endl; |
79 |
break; |
80 |
case consElemHandlerFail: |
81 |
//can not found callback functor for constraint element |
82 |
cerr << "ConstraintAlgorithm::doConstrain() Error: can not found callback functor for constraint element " << endl; |
83 |
break; |
84 |
default: |
85 |
cerr << "ConstraintAlgorithm::doConstrain() Error: unrecognized status" << endl; |
86 |
break; |
87 |
} |
88 |
}//end for(iter->first()) |
89 |
|
90 |
for(ceIter->first(); !ceIter->isEnd(); ceIter->next()){ |
91 |
consElem = ceIter->currentItem(); |
92 |
consElem->setMoved(consElem->getMoving()); |
93 |
consElem->setMoving(false); |
94 |
} |
95 |
|
96 |
iteration++; |
97 |
}//end while |
98 |
|
99 |
} |
100 |
|
101 |
|
102 |
int ConstraintAlgorithm::doConstrainPair(ConstraintPair* consPair){ |
103 |
map<TypeInfo, CallbackFunctor*>::iterator foundResult; |
104 |
CallbackFunctor* functor; |
105 |
|
106 |
foundResult = callbackMap.find(typeid(consPair)); |
107 |
if (foundResult != callbackMap.end()){ |
108 |
functor = foundResult->second; |
109 |
return (*functor)(consPair); |
110 |
} |
111 |
else{ |
112 |
//can not found appropriate functor to handle constraint pair in callbackMap |
113 |
return consPairHandlerFail; |
114 |
} |
115 |
|
116 |
} |
117 |
|
118 |
void ConstraintAlgorithm::registerCallback(const TypeInfo& ti, CallbackFunctor* functor){ |
119 |
map<TypeInfo, CallbackFunctor*>::iterator foundResult; |
120 |
|
121 |
foundResult = callbackMap.find(ti); |
122 |
if (foundResult == callbackMap.end()) |
123 |
callbackMap[ti] = functor; |
124 |
else{ |
125 |
delete foundResult->second; |
126 |
foundResult->second = functor; |
127 |
} |
128 |
|
129 |
} |
130 |
void ConstraintAlgorithm::unRegister(TypeInfo& ti){ |
131 |
map<TypeInfo, CallbackFunctor*>::iterator foundResult; |
132 |
|
133 |
foundResult = callbackMap.find(ti); |
134 |
if (foundResult != callbackMap.end()){ |
135 |
delete foundResult->second; |
136 |
callbackMap.erase(foundResult); |
137 |
} |
138 |
} |
139 |
|
140 |
//////////////////////////////////////////////////////////////////////////////// |
141 |
//Implementation of ConsAlgoFramework |
142 |
//////////////////////////////////////////////////////////////////////////////// |
143 |
ConsAlgoFramework::ConsAlgoFramework(SimInfo* rhs){ |
144 |
ceIter = rhs->consMan->creatElementIterator(); |
145 |
} |
146 |
|
147 |
ConsAlgoFramework::~ConsAlgoFramework(){ |
148 |
delete ceIter; |
149 |
} |
150 |
void ConsAlgoFramework::doPreConstraint(){ |
151 |
ConstraintElement* consElem; |
152 |
|
153 |
for(ceIter->first(); !ceIter->isEnd(); ceIter->next()){ |
154 |
consElem = ceIter->currentItem(); |
155 |
consElem->saveOldState(); |
156 |
} |
157 |
} |