1 |
tim |
1232 |
#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 |
tim |
1234 |
//typeid must operate on deferenced, otherwise it will return the type_info of base class |
107 |
|
|
foundResult = callbackMap.find(TypeInfo(typeid(*consPair))); |
108 |
tim |
1232 |
if (foundResult != callbackMap.end()){ |
109 |
|
|
functor = foundResult->second; |
110 |
|
|
return (*functor)(consPair); |
111 |
|
|
} |
112 |
|
|
else{ |
113 |
|
|
//can not found appropriate functor to handle constraint pair in callbackMap |
114 |
|
|
return consPairHandlerFail; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
void ConstraintAlgorithm::registerCallback(const TypeInfo& ti, CallbackFunctor* functor){ |
120 |
|
|
map<TypeInfo, CallbackFunctor*>::iterator foundResult; |
121 |
|
|
|
122 |
|
|
foundResult = callbackMap.find(ti); |
123 |
|
|
if (foundResult == callbackMap.end()) |
124 |
|
|
callbackMap[ti] = functor; |
125 |
|
|
else{ |
126 |
|
|
delete foundResult->second; |
127 |
|
|
foundResult->second = functor; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
} |
131 |
|
|
void ConstraintAlgorithm::unRegister(TypeInfo& ti){ |
132 |
|
|
map<TypeInfo, CallbackFunctor*>::iterator foundResult; |
133 |
|
|
|
134 |
|
|
foundResult = callbackMap.find(ti); |
135 |
|
|
if (foundResult != callbackMap.end()){ |
136 |
|
|
delete foundResult->second; |
137 |
|
|
callbackMap.erase(foundResult); |
138 |
|
|
} |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
//////////////////////////////////////////////////////////////////////////////// |
142 |
|
|
//Implementation of ConsAlgoFramework |
143 |
|
|
//////////////////////////////////////////////////////////////////////////////// |
144 |
|
|
ConsAlgoFramework::ConsAlgoFramework(SimInfo* rhs){ |
145 |
|
|
ceIter = rhs->consMan->creatElementIterator(); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
ConsAlgoFramework::~ConsAlgoFramework(){ |
149 |
|
|
delete ceIter; |
150 |
|
|
} |
151 |
tim |
1234 |
|
152 |
tim |
1232 |
void ConsAlgoFramework::doPreConstraint(){ |
153 |
|
|
ConstraintElement* consElem; |
154 |
|
|
|
155 |
|
|
for(ceIter->first(); !ceIter->isEnd(); ceIter->next()){ |
156 |
|
|
consElem = ceIter->currentItem(); |
157 |
|
|
consElem->saveOldState(); |
158 |
tim |
1234 |
} |
159 |
|
|
} |