15 using namespace Eigen;
16 using namespace rsvs3d::constants;
24 for (
int ii = 0; ii < ni; ++ii)
26 if (triIn.pointtype[ii] == meshtypes::mesh)
28 veccoord.push_back(&(triRSVS.meshDep->verts.isearch(triIn.pointind[ii])->coord));
30 else if (triIn.pointtype[ii] == meshtypes::snake)
32 veccoord.push_back(&(triRSVS.snakeDep->snakeconn.verts.isearch(triIn.pointind[ii])->coord));
34 else if (triIn.pointtype[ii] == meshtypes::triangulation)
36 veccoord.push_back((triRSVS.trivert.isearch(triIn.pointind[ii])->coord.retPtr()));
47 vector<int> vertsSurf;
48 vector<int> &dvList = dvListMap.vec;
51 for (
int ii = 0; ii < ni; ++ii)
54 if (triIn.pointtype[ii] == meshtypes::snake && objDvMap.find(triIn.pointind[ii]) != __notfound)
56 dvList.push_back(triIn.pointind[ii]);
58 else if (triIn.pointtype[ii] == meshtypes::triangulation && useSurfCentreDeriv)
60 switch (triRSVS.trivert.isearch(triIn.pointind[ii])->parentType)
62 case meshtypes::triangulation: {
64 auto surfCurr = triRSVS.trisurf.isearch(triRSVS.trivert.isearch(triIn.pointind[ii])->parentsurf);
65 int nj = surfCurr->indvert.size();
66 for (
int jj = 0; jj < nj; ++jj)
68 if (surfCurr->typevert[jj] == meshtypes::snake)
70 if (objDvMap.find(surfCurr->indvert[jj]) != __notfound)
72 dvList.push_back(surfCurr->indvert[jj]);
78 case meshtypes::snake: {
81 triRSVS.snakeDep->snakeconn.surfs.isearch(triRSVS.trivert.isearch(triIn.pointind[ii])->parentsurf);
82 vertsSurf = surfCurr->OrderedVerts(&(triRSVS.snakeDep->snakeconn));
83 int nj = vertsSurf.size();
84 for (
int jj = 0; jj < nj; ++jj)
86 if (objDvMap.find(vertsSurf[jj]) != __notfound)
88 dvList.push_back(vertsSurf[jj]);
98 unique(dvListMap.vec);
99 dvListMap.GenerateHash();
112 bool useSurfCentreDeriv,
bool useSurfCentreHessian)
114 std::vector<int> vertsSurf;
115 int nDvAct = dvListMap.vec.size();
116 bool triggerPrint =
false, triggerActive =
false;
117 auto ifisnan0 = [&](
double in) ->
double {
return isnan(in) ? 0.0 : in; };
120 auto cleanup = [&](
double in) ->
double {
return ifisnan0((in)); };
122 static std::ofstream streamout;
123 if (triggerActive && !streamout.is_open())
125 streamout.open(
"dbg/deriv_pos.txt", std::ios::app);
126 std::cout <<
"Debug stream opened";
131 HPos.setZero(nDvAct, nDvAct * 9);
132 dPos.setZero(9, nDvAct);
134 for (
int ii = 0; ii < ni; ++ii)
137 if (triIn.pointtype[ii] == meshtypes::snake && dvListMap.find(triIn.pointind[ii]) != __notfound)
139 auto tempSnax = triRSVS.snakeDep->snaxs.isearch(triIn.pointind[ii]);
140 auto fromVert = triRSVS.meshDep->verts.isearch(tempSnax->fromvert);
141 auto toVert = triRSVS.meshDep->verts.isearch(tempSnax->tovert);
142 int dvSub = dvListMap.find(triIn.pointind[ii]);
143 for (
int jj = 0; jj < 3; ++jj)
145 dPos(ii * 3 + jj, dvSub) += (toVert->coord[jj] - fromVert->coord[jj]);
148 else if (triIn.pointtype[ii] == meshtypes::triangulation && useSurfCentreDeriv)
150 switch (triRSVS.trivert.isearch(triIn.pointind[ii])->parentType)
152 case meshtypes::triangulation: {
153 auto surfCurr = triRSVS.trisurf.isearch(triRSVS.trivert.isearch(triIn.pointind[ii])->parentsurf);
155 SurfaceCentroid_TriangleSurf(*surfCurr, *(triRSVS.meshDep), triRSVS.snakeDep->snakeconn);
157 auto jacCentre = surfCentre.jac_ptr();
158 auto hesCentre = surfCentre.hes_ptr();
159 int count = surfCurr->indvert.size();
160 for (
int j = 0; j < count; ++j)
162 int dvSub = dvListMap.find(surfCurr->indvert[j]);
163 if (dvSub != __notfound && surfCurr->typevert[j] == meshtypes::snake)
165 auto currSnax = triRSVS.snakeDep->snaxs.isearch(surfCurr->indvert[j]);
166 auto fromVert = triRSVS.meshDep->verts.isearch(currSnax->fromvert);
167 auto toVert = triRSVS.meshDep->verts.isearch(currSnax->tovert);
168 for (
int k = 0; k < 3; ++k)
170 for (
int l = 0; l < 3; ++l)
172 dPos(ii * 3 + k, dvSub) +=
173 cleanup(jacCentre[k][j + count * l] * (toVert->coord[l] - fromVert->coord[l]))
182 case meshtypes::snake: {
184 triRSVS.snakeDep->snakeconn.surfs.isearch(triRSVS.trivert.isearch(triIn.pointind[ii])->parentsurf);
185 auto surfCentre = SurfaceCentroid_SnakeSurf(*surfCurr, triRSVS.snakeDep->snakeconn);
187 auto jacCentre = surfCentre.jac_ptr();
188 auto hesCentre = surfCentre.hes_ptr();
189 ConnVertFromConnEdge(triRSVS.snakeDep->snakeconn, surfCurr->edgeind, vertsSurf);
191 size_t count = vertsSurf.size();
192 for (
size_t j = 0; j < count; ++j)
194 int dvSub = dvListMap.find(vertsSurf[j]);
195 auto currSnax = triRSVS.snakeDep->snaxs.isearch(vertsSurf[j]);
196 auto fromVert = triRSVS.meshDep->verts.isearch(currSnax->fromvert);
197 auto toVert = triRSVS.meshDep->verts.isearch(currSnax->tovert);
199 if (dvSub != __notfound)
201 for (
size_t k = 0; k < 3; ++k)
203 for (
size_t l = 0; l < 3; ++l)
205 dPos(ii * 3 + k, dvSub) +=
206 cleanup(jacCentre[k][j + count * l] * (toVert->coord[l] - fromVert->coord[l]))
216 if (useSurfCentreHessian)
218 MatrixXd dPosd(count * 3, count);
219 dPosd.setZero(count * 3, count);
220 for (
size_t j = 0; j < count; ++j)
222 auto currSnax = triRSVS.snakeDep->snaxs.isearch(vertsSurf[j]);
223 auto fromVert = triRSVS.meshDep->verts.isearch(currSnax->fromvert);
224 auto toVert = triRSVS.meshDep->verts.isearch(currSnax->tovert);
225 for (
size_t k = 0; k < 3; ++k)
228 dPosd(j + k * count, j) += (toVert->coord[k] - fromVert->coord[k]);
231 MatrixXd HVal(count * 3, count * 3 * 3), HPosTemp(count, count * 3);
232 ArrayVec2MatrixXd(hesCentre, HVal);
233 for (
size_t j = 0; j < 3; ++j)
235 HPosTemp.block(0, j * count, count, count) =
236 dPosd.transpose() * HVal.block(0, j * count * 3, count * 3, count * 3) * dPosd;
238 for (
size_t j = 0; j < count; ++j)
240 int dvSub = dvListMap.find(vertsSurf[j]);
241 if (dvSub != __notfound)
243 for (
size_t j2 = 0; j2 < count; ++j2)
245 int dvSub2 = dvListMap.find(vertsSurf[j2]);
246 if (dvSub2 != __notfound)
248 for (
size_t k = 0; k < 3; ++k)
250 HPos(dvSub, dvSub2 + (ii * 3 + k) * nDvAct) +=
251 cleanup(HPosTemp(j, j2 + k * count));
263 streamout <<
"triangle " << triIn.index <<
" parent: surf : " << triIn.parentsurf
264 <<
" parent: type : " << triIn.parenttype <<
" pnt-centroid: " << ii << std::endl;
265 streamout <<
"pointind" << std::endl;
266 PrintVector(triIn.pointind, streamout);
267 streamout <<
"" << std::endl;
268 streamout <<
"pointtype" << std::endl;
269 PrintVector(triIn.pointtype, streamout);
270 streamout <<
"" << std::endl;
271 streamout <<
"dPosd:" << std::endl;
272 PrintMatrixFile(dPosd, streamout);
282 streamout <<
"HPos:" << std::endl;
283 PrintMatrixFile(HPos, streamout);
284 streamout <<
"dPos:" << std::endl;
285 PrintMatrixFile(dPos, streamout);
290 const MatrixXd &dConstrPart,
const MatrixXd &HConstrPart,
int constrPos,
293 int nDvAct = dvListMap.vec.size();
295 for (
int kk = 0; kk < nDvAct; ++kk)
297 calc.
dConstr(constrPos, calc.
dvMap.find(dvListMap.vec[kk])) +=
298 triIn.connec.constrinfluence[ii] * dConstrPart(0, kk);
299 calc.dvCallConstr(calc.
dvMap.find(dvListMap.vec[kk]), 0)++;
300 for (
int ll = 0; ll < nDvAct; ++ll)
303 calc.
HConstr(calc.
dvMap.find(dvListMap.vec[ll]), calc.
dvMap.find(dvListMap.vec[kk])) +=
304 triIn.connec.constrinfluence[ii] * HConstrPart(ll, kk) * calc.
lagMult[constrPos]
311 void AssignConstraintDerivativesSparseMath(
RSVScalc &calc,
const triangle &triIn,
313 const MatrixXd &HConstrPart,
int constrPos,
int cellTarg)
315 int nDvAct = dvListMap.vec.size();
318 for (
int kk = 0; kk < nDvAct; ++kk)
320 if (!rsvs3d::utils::IsAproxEqual(dConstrPart(0, kk), 0.0))
322 calc.dConstr_sparse.coeffRef(constrPos, calc.
dvMap.find(dvListMap.vec[kk])) +=
323 triIn.connec.constrinfluence[ii] * dConstrPart(0, kk);
325 calc.dvCallConstr(calc.
dvMap.find(dvListMap.vec[kk]), 0)++;
326 for (
int ll = 0; ll < nDvAct; ++ll)
328 double HconstrLag = HConstrPart(ll, kk) * calc.
lagMult[constrPos];
329 if (!rsvs3d::utils::IsAproxEqual(HconstrLag, 0.0))
332 calc.HConstr_sparse.coeffRef(calc.
dvMap.find(dvListMap.vec[ll]), calc.
dvMap.find(dvListMap.vec[kk])) +=
333 triIn.connec.constrinfluence[ii] * HconstrLag;
Provides the infrastructure for calculation of the RSVS equations.
Performs Volume and Area calculations for the RSVS process.
Provide std::vector container with hashed index mapping.
Class to handle the RSVS calculation.
Eigen::MatrixXd dConstr
Constraint Jacobian, size: [nConstr, nDv].
Eigen::MatrixXd HConstr
Constraint Hessian, size: [nDv, nDv].
Eigen::VectorXd lagMult
Lagrangian multiplier, size: [nConstr, 1].
HashedVector< int, int > dvMap
Maps the snake indices to the position in the design variable vector.
std::vector< const std::vector< double > * > coordlist
Defines a list of coordinates.
Provides various utility functions for the RSVS operation.
Provides a triangulation for snake and meshes.