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