/usr/include/sofa/gpu/cuda/CudaBarycentricMapping.inl is in libsofa1-dev 1.0~beta4-10ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 | /******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Modules *
* *
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_GPU_CUDA_CUDABARYCENTRICMAPPING_INL
#define SOFA_GPU_CUDA_CUDABARYCENTRICMAPPING_INL
#include "CudaBarycentricMapping.h"
#include <sofa/component/mapping/BarycentricMapping.inl>
namespace sofa
{
namespace gpu
{
namespace cuda
{
extern "C"
{
void RegularGridMapperCuda3f_apply(unsigned int size, const unsigned int* gridsize, const void* map, void* out, const void* in);
void RegularGridMapperCuda3f_applyJ(unsigned int size, const unsigned int* gridsize, const void* map, void* out, const void* in);
void RegularGridMapperCuda3f_applyJT(unsigned int insize, unsigned int maxNOut, const unsigned int* gridsize, const void* mapT, void* out, const void* in);
void RegularGridMapperCuda3f1_apply(unsigned int size, const unsigned int* gridsize, const void* map, void* out, const void* in);
void RegularGridMapperCuda3f1_applyJ(unsigned int size, const unsigned int* gridsize, const void* map, void* out, const void* in);
void RegularGridMapperCuda3f1_applyJT(unsigned int insize, unsigned int maxNOut, const unsigned int* gridsize, const void* mapT, void* out, const void* in);
void RegularGridMapperCuda3f_3f1_apply(unsigned int size, const unsigned int* gridsize, const void* map, void* out, const void* in);
void RegularGridMapperCuda3f_3f1_applyJ(unsigned int size, const unsigned int* gridsize, const void* map, void* out, const void* in);
void RegularGridMapperCuda3f_3f1_applyJT(unsigned int insize, unsigned int maxNOut, const unsigned int* gridsize, const void* mapT, void* out, const void* in);
void RegularGridMapperCuda3f1_3f_apply(unsigned int size, const unsigned int* gridsize, const void* map, void* out, const void* in);
void RegularGridMapperCuda3f1_3f_applyJ(unsigned int size, const unsigned int* gridsize, const void* map, void* out, const void* in);
void RegularGridMapperCuda3f1_3f_applyJT(unsigned int insize, unsigned int maxNOut, const unsigned int* gridsize, const void* mapT, void* out, const void* in);
void MeshMapperCuda3f_apply(unsigned int size, unsigned int maxN, const void* map, void* out, const void* in);
void MeshMapperCuda3f1_apply(unsigned int size, unsigned int maxN, const void* map, void* out, const void* in);
void MeshMapperCuda3f_3f1_apply(unsigned int size, unsigned int maxN, const void* map, void* out, const void* in);
void MeshMapperCuda3f1_3f_apply(unsigned int size, unsigned int maxN, const void* map, void* out, const void* in);
void MeshMapperCuda3f_applyPEq(unsigned int size, unsigned int maxN, const void* map, void* out, const void* in);
void MeshMapperCuda3f1_applyPEq(unsigned int size, unsigned int maxN, const void* map, void* out, const void* in);
void MeshMapperCuda3f_3f1_applyPEq(unsigned int size, unsigned int maxN, const void* map, void* out, const void* in);
void MeshMapperCuda3f1_3f_applyPEq(unsigned int size, unsigned int maxN, const void* map, void* out, const void* in);
}
} // namespace cuda
} // namespace gpu
namespace component
{
namespace mapping
{
using namespace gpu::cuda;
////////////////////////////////////////////////////////////
////////// RegularGridTopology //////////
////////////////////////////////////////////////////////////
template <typename VecIn, typename VecOut>
void BarycentricMapperRegularGridTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::clear(int reserve)
{
map.clear();
if (reserve>0) map.reserve(reserve);
}
template <typename VecIn, typename VecOut>
int BarycentricMapperRegularGridTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::addPointInCube(int cubeIndex, const Real* baryCoords)
{
map.resize(map.size()+1);
CubeData& data = map[map.size()-1];
//data.in_index = cubeIndex;
#ifdef SOFA_NEW_HEXA
data.in_index = topology->getHexaCopy(cubeIndex)[0];
#else
data.in_index = topology->getCubeCopy(cubeIndex)[0];
#endif
data.baryCoords[0] = baryCoords[0];
data.baryCoords[1] = baryCoords[1];
data.baryCoords[2] = baryCoords[2];
maxNOut = 0; // mapT must be recomputed
return map.size()-1;
}
template <typename VecIn, typename VecOut>
void BarycentricMapperRegularGridTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::init(const typename Out::VecCoord& out, const typename In::VecCoord& /*in*/)
{
int outside = 0;
clear(out.size());
for (unsigned int i=0;i<out.size();i++)
{
Vector3 coefs;
int cube = topology->findCube(Vector3(out[i]), coefs[0], coefs[1], coefs[2]);
if (cube==-1)
{
++outside;
cube = topology->findNearestCube(Vector3(out[i]), coefs[0], coefs[1], coefs[2]);
}
Vec<3,Real> baryCoords = coefs;
addPointInCube(cube, baryCoords.ptr());
}
}
template <typename VecIn, typename VecOut>
void BarycentricMapperRegularGridTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::calcMapT()
{
if (!map.empty() && maxNOut == 0)
{
const unsigned int insize = topology->getNbPoints();
const unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
// compute mapT
const int nx = gridsize[0];
const int nxny = gridsize[0]*gridsize[1];
const int shift[8] = { 0, 1, nx, 1+nx, nxny, 1+nxny, nx+nxny, 1+nx+nxny };
std::vector<int> nout(insize);
for (unsigned int i=0;i<map.size();i++)
{
int index0 = map[i].in_index;
for (int j=0;j<8;j++)
nout[index0+shift[j]]++;
}
for (unsigned int i=0;i<insize;i++)
if (nout[i] > maxNOut) maxNOut = nout[i];
int nbloc = (insize+BSIZE-1)/BSIZE;
std::cout << "CudaBarycentricMapping: mapT with "<<maxNOut<<" entries per DOF and "<<nbloc<<" blocs."<<std::endl;
mapT.resize(nbloc*(BSIZE*maxNOut));
for (unsigned int i=0;i<mapT.size();i++)
mapT[i] = std::make_pair(-1,0.0f);
nout.clear();
nout.resize(insize);
for (unsigned int i=0;i<map.size();i++)
{
int index0 = map[i].in_index;
for (int j=0;j<8;j++)
{
int index = index0+shift[j];
int num = nout[index]++;
int b = (index / BSIZE); index -= b*BSIZE;
float f;
f = (j&1)?(map[i].baryCoords[0]):(1-map[i].baryCoords[0]);
f *= (j&2)?(map[i].baryCoords[1]):(1-map[i].baryCoords[1]);
f *= (j&4)?(map[i].baryCoords[2]):(1-map[i].baryCoords[2]);
//std::cout << "mapT["<<b<<"*"<<maxNOut*BSIZE<<"+"<<num<<"*"<<BSIZE<<"+"<<index<<"] = < "<<i<<", "<<f<<">"<<std::endl;
mapT[(maxNOut*b+num)*BSIZE+index] = std::make_pair(i,f);
}
}
}
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3fTypes,CudaVec3fTypes>::apply( Out::VecCoord& out, const In::VecCoord& in )
{
unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
out.fastResize(map.size());
RegularGridMapperCuda3f_apply(map.size(), gridsize, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3fTypes,CudaVec3fTypes>::applyJ( Out::VecDeriv& out, const In::VecDeriv& in )
{
unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
out.fastResize(map.size());
RegularGridMapperCuda3f_applyJ(map.size(), gridsize, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3fTypes,CudaVec3fTypes>::applyJT( In::VecDeriv& out, const Out::VecDeriv& in )
{
if (map.size() == 0) return;
calcMapT();
unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
unsigned int insize = out.size();
RegularGridMapperCuda3f_applyJT(insize, maxNOut, gridsize, mapT.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3fTypes,CudaVec3fTypes>::applyJT( In::VecConst& /*out*/, const Out::VecConst& /*in*/ )
{
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3fTypes,CudaVec3fTypes>::draw( const Out::VecCoord& /*out*/, const In::VecCoord& /*in*/)
{
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3f1Types,CudaVec3f1Types>::apply( Out::VecCoord& out, const In::VecCoord& in )
{
unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
out.fastResize(map.size());
RegularGridMapperCuda3f1_apply(map.size(), gridsize, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3f1Types,CudaVec3f1Types>::applyJ( Out::VecDeriv& out, const In::VecDeriv& in )
{
unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
out.fastResize(map.size());
RegularGridMapperCuda3f1_applyJ(map.size(), gridsize, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3f1Types,CudaVec3f1Types>::applyJT( In::VecDeriv& out, const Out::VecDeriv& in )
{
calcMapT();
if (map.size() == 0) return;
unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
unsigned int insize = out.size();
RegularGridMapperCuda3f1_applyJT(insize, maxNOut, gridsize, mapT.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3f1Types,CudaVec3f1Types>::applyJT( In::VecConst& /*out*/, const Out::VecConst& /*in*/ )
{
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3f1Types,CudaVec3f1Types>::draw( const Out::VecCoord& /*out*/, const In::VecCoord& /*in*/)
{
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3f1Types,CudaVec3fTypes>::apply( Out::VecCoord& out, const In::VecCoord& in )
{
unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
out.fastResize(map.size());
RegularGridMapperCuda3f1_3f_apply(map.size(), gridsize, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3f1Types,CudaVec3fTypes>::applyJ( Out::VecDeriv& out, const In::VecDeriv& in )
{
unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
out.fastResize(map.size());
RegularGridMapperCuda3f1_3f_applyJ(map.size(), gridsize, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3f1Types,CudaVec3fTypes>::applyJT( In::VecDeriv& out, const Out::VecDeriv& in )
{
calcMapT();
if (map.size() == 0) return;
unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
unsigned int insize = out.size();
RegularGridMapperCuda3f1_3f_applyJT(insize, maxNOut, gridsize, mapT.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3f1Types,CudaVec3fTypes>::applyJT( In::VecConst& /*out*/, const Out::VecConst& /*in*/ )
{
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3f1Types,CudaVec3fTypes>::draw( const Out::VecCoord& /*out*/, const In::VecCoord& /*in*/)
{
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3fTypes,CudaVec3f1Types>::apply( Out::VecCoord& out, const In::VecCoord& in )
{
unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
out.fastResize(map.size());
RegularGridMapperCuda3f_3f1_apply(map.size(), gridsize, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3fTypes,CudaVec3f1Types>::applyJ( Out::VecDeriv& out, const In::VecDeriv& in )
{
unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
out.fastResize(map.size());
RegularGridMapperCuda3f_3f1_applyJ(map.size(), gridsize, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3fTypes,CudaVec3f1Types>::applyJT( In::VecDeriv& out, const Out::VecDeriv& in )
{
calcMapT();
if (map.size() == 0) return;
unsigned int gridsize[3] = { topology->getNx(), topology->getNy(), topology->getNz() };
unsigned int insize = out.size();
RegularGridMapperCuda3f_3f1_applyJT(insize, maxNOut, gridsize, mapT.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3fTypes,CudaVec3f1Types>::applyJT( In::VecConst& /*out*/, const Out::VecConst& /*in*/ )
{
}
template<>
void BarycentricMapperRegularGridTopology<CudaVec3fTypes,CudaVec3f1Types>::draw( const Out::VecCoord& /*out*/, const In::VecCoord& /*in*/)
{
}
////////////////////////////////////////////////////////////
////////// BaseMeshTopology //////////
////////////////////////////////////////////////////////////
template <typename VecIn, typename VecOut>
void BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::resizeMap(int size2, int maxNIn2)
{
if (maxNIn2 < maxNIn) maxNIn2 = maxNIn;
map.resize(((size2+BSIZE-1)/BSIZE)*maxNIn2);
if (maxNIn2 > maxNIn)
{
int n = (size+BSIZE-1)/BSIZE;
for (int b=n-1;b>0;--b)
for (int j=maxNIn-1;j>=0;--j)
{
// copy old data
map[b*maxNIn2+j]=map[b*maxNIn+j];
// clear other data
for (int i=0;i<BSIZE;++i)
{
map[b*maxNIn+j].d[i].i = 0;
map[b*maxNIn+j].d[i].val = 0.0f;
}
}
}
size = size2;
maxNIn = maxNIn2;
}
template <typename VecIn, typename VecOut>
void BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::setMap(int outIndex, int j, int inIndex, Real val)
{
int b = outIndex / BSIZE;
outIndex = outIndex % BSIZE;
map[b*maxNIn+j].d[outIndex].i = inIndex+1;
map[b*maxNIn+j].d[outIndex].val = val;
}
template <typename VecIn, typename VecOut>
void BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::clear(int reserve)
{
map.clear(); if (reserve>0) map.reserve((reserve+BSIZE-1)/BSIZE*maxNIn);
size = 0;
}
template <typename VecIn, typename VecOut>
int BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::addPointInLine(const int lineIndex, const SReal* baryCoords)
{
unsigned int i0 = size;
resizeMap(i0+1,2);
core::componentmodel::topology::BaseMeshTopology::Line e = topology->getLine(lineIndex);
setMap(i0,0,e[0],(Real)(1-baryCoords[0]));
setMap(i0,1,e[1],(Real)(baryCoords[0]));
return i0;
}
template <typename VecIn, typename VecOut>
int BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::addPointInTriangle(const int triangleIndex, const SReal* baryCoords)
{
unsigned int i0 = size;
resizeMap(i0+1,3);
core::componentmodel::topology::BaseMeshTopology::Triangle e = topology->getTriangle(triangleIndex);
setMap(i0,0,e[0],(Real)(1-baryCoords[0]-baryCoords[1]));
setMap(i0,1,e[1],(Real)(baryCoords[0]));
setMap(i0,2,e[2],(Real)(baryCoords[1]));
return i0;
}
template <typename VecIn, typename VecOut>
int BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::addPointInQuad(const int quadIndex, const SReal* baryCoords)
{
unsigned int i0 = size;
resizeMap(i0+1,4);
core::componentmodel::topology::BaseMeshTopology::Quad e = topology->getQuad(quadIndex);
setMap(i0,0,e[0],(Real)((1-baryCoords[0])*(1-baryCoords[1])));
setMap(i0,1,e[1],(Real)(( baryCoords[0])*(1-baryCoords[1])));
setMap(i0,2,e[3],(Real)((1-baryCoords[0])*( baryCoords[1])));
setMap(i0,3,e[2],(Real)(( baryCoords[0])*( baryCoords[1])));
return i0;
}
template <typename VecIn, typename VecOut>
int BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::addPointInTetra(const int tetraIndex, const SReal* baryCoords)
{
unsigned int i0 = size;
resizeMap(i0+1,4);
core::componentmodel::topology::BaseMeshTopology::Tetra e = topology->getTetra(tetraIndex);
setMap(i0,0,e[0],(Real)(1-baryCoords[0]-baryCoords[1]-baryCoords[2]));
setMap(i0,1,e[1],(Real)(baryCoords[0]));
setMap(i0,2,e[2],(Real)(baryCoords[1]));
setMap(i0,3,e[3],(Real)(baryCoords[2]));
return i0;
}
template <typename VecIn, typename VecOut>
int BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::addPointInCube(const int cubeIndex, const SReal* baryCoords)
{
unsigned int i0 = size;
resizeMap(i0+1,8);
#ifdef SOFA_NEW_HEXA
core::componentmodel::topology::BaseMeshTopology::Hexa e = topology->getHexa(cubeIndex);
setMap(i0,0,e[0],(Real)((1-baryCoords[0])*(1-baryCoords[1])*(1-baryCoords[2])));
setMap(i0,1,e[1],(Real)(( baryCoords[0])*(1-baryCoords[1])*(1-baryCoords[2])));
setMap(i0,2,e[3],(Real)((1-baryCoords[0])*( baryCoords[1])*(1-baryCoords[2])));
setMap(i0,3,e[2],(Real)(( baryCoords[0])*( baryCoords[1])*(1-baryCoords[2])));
setMap(i0,4,e[4],(Real)((1-baryCoords[0])*(1-baryCoords[1])*( baryCoords[2])));
setMap(i0,5,e[5],(Real)(( baryCoords[0])*(1-baryCoords[1])*( baryCoords[2])));
setMap(i0,6,e[7],(Real)((1-baryCoords[0])*( baryCoords[1])*( baryCoords[2])));
setMap(i0,7,e[6],(Real)(( baryCoords[0])*( baryCoords[1])*( baryCoords[2])));
#else
core::componentmodel::topology::BaseMeshTopology::Cube e = topology->getCube(cubeIndex);
setMap(i0,0,e[0],(Real)((1-baryCoords[0])*(1-baryCoords[1])*(1-baryCoords[2])));
setMap(i0,1,e[1],(Real)(( baryCoords[0])*(1-baryCoords[1])*(1-baryCoords[2])));
setMap(i0,2,e[2],(Real)((1-baryCoords[0])*( baryCoords[1])*(1-baryCoords[2])));
setMap(i0,3,e[3],(Real)(( baryCoords[0])*( baryCoords[1])*(1-baryCoords[2])));
setMap(i0,4,e[4],(Real)((1-baryCoords[0])*(1-baryCoords[1])*( baryCoords[2])));
setMap(i0,5,e[5],(Real)(( baryCoords[0])*(1-baryCoords[1])*( baryCoords[2])));
setMap(i0,6,e[6],(Real)((1-baryCoords[0])*( baryCoords[1])*( baryCoords[2])));
setMap(i0,7,e[7],(Real)(( baryCoords[0])*( baryCoords[1])*( baryCoords[2])));
#endif
return i0;
}
template <typename VecIn, typename VecOut>
int BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::createPointInLine(const typename Out::Coord& p, int lineIndex, const typename In::VecCoord* points)
{
SReal baryCoords[1];
const topology::MeshTopology::Line& elem = topology->getLine(lineIndex);
const typename In::Coord p0 = (*points)[elem[0]];
const typename In::Coord pA = (*points)[elem[1]] - p0;
typename In::Coord pos = p - p0;
baryCoords[0] = ((pos*pA)/pA.norm2());
return this->addPointInLine(lineIndex, baryCoords);
}
template <typename VecIn, typename VecOut>
int BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::createPointInTriangle(const typename Out::Coord& p, int triangleIndex, const typename In::VecCoord* points)
{
SReal baryCoords[2];
const topology::MeshTopology::Triangle& elem = topology->getTriangle(triangleIndex);
const typename In::Coord p0 = (*points)[elem[0]];
const typename In::Coord pA = (*points)[elem[1]] - p0;
const typename In::Coord pB = (*points)[elem[2]] - p0;
typename In::Coord pos = p - p0;
// First project to plane
typename In::Coord normal = cross(pA, pB);
Real norm2 = normal.norm2();
pos -= normal*((pos*normal)/norm2);
baryCoords[0] = (Real)sqrt(cross(pB, pos).norm2() / norm2);
baryCoords[1] = (Real)sqrt(cross(pA, pos).norm2() / norm2);
return this->addPointInTriangle(triangleIndex, baryCoords);
}
template <typename VecIn, typename VecOut>
int BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::createPointInQuad(const typename Out::Coord& p, int quadIndex, const typename In::VecCoord* points)
{
SReal baryCoords[2];
const topology::MeshTopology::Quad& elem = topology->getQuad(quadIndex);
const typename In::Coord p0 = (*points)[elem[0]];
const typename In::Coord pA = (*points)[elem[1]] - p0;
const typename In::Coord pB = (*points)[elem[3]] - p0;
typename In::Coord pos = p - p0;
Mat<3,3,typename In::Real> m,mt,base;
m[0] = pA;
m[1] = pB;
m[2] = cross(pA, pB);
mt.transpose(m);
base.invert(mt);
const typename In::Coord base0 = base[0];
const typename In::Coord base1 = base[1];
baryCoords[0] = base0 * pos;
baryCoords[1] = base1 * pos;
return this->addPointInQuad(quadIndex, baryCoords);
}
template <typename VecIn, typename VecOut>
void BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::init(const typename Out::VecCoord& out, const typename In::VecCoord& in)
{
int outside = 0;
const topology::MeshTopology::SeqTetras& tetras = topology->getTetras();
#ifdef SOFA_NEW_HEXA
const topology::MeshTopology::SeqHexas& cubes = topology->getHexas();
#else
const topology::MeshTopology::SeqCubes& cubes = topology->getCubes();
#endif
const topology::MeshTopology::SeqTriangles& triangles = topology->getTriangles();
const topology::MeshTopology::SeqQuads& quads = topology->getQuads();
sofa::helper::vector<Matrix3> bases;
sofa::helper::vector<Vector3> centers;
clear(out.size()); // reserve space for mapping
if (tetras.empty() && cubes.empty())
{ // no 3D elements -> map on 2D elements
int c0 = triangles.size();
bases.resize(triangles.size()+quads.size());
centers.resize(triangles.size()+quads.size());
for (unsigned int t = 0; t < triangles.size(); t++)
{
Mat3x3d m,mt;
m[0] = in[triangles[t][1]]-in[triangles[t][0]];
m[1] = in[triangles[t][2]]-in[triangles[t][0]];
m[2] = cross(m[0],m[1]);
mt.transpose(m);
bases[t].invert(mt);
centers[t] = (in[triangles[t][0]]+in[triangles[t][1]]+in[triangles[t][2]])/3;
}
for (unsigned int c = 0; c < quads.size(); c++)
{
Mat3x3d m,mt;
m[0] = in[quads[c][1]]-in[quads[c][0]];
m[1] = in[quads[c][3]]-in[quads[c][0]];
m[2] = cross(m[0],m[1]);
mt.transpose(m);
bases[c0+c].invert(mt);
centers[c0+c] = (in[quads[c][0]]+in[quads[c][1]]+in[quads[c][2]]+in[quads[c][3]])*0.25;
}
for (unsigned int i=0;i<out.size();i++)
{
Vector3 pos = out[i];
Vector3 coefs;
int index = -1;
double distance = 1e10;
for (unsigned int t = 0; t < triangles.size(); t++)
{
Vec3d v = bases[t] * (pos - in[triangles[t][0]]);
double d = std::max(std::max(-v[0],-v[1]),std::max((v[2]<0?-v[2]:v[2])-0.01,v[0]+v[1]-1));
if (d>0) d = (pos-centers[t]).norm2();
if (d<distance) { coefs = v; distance = d; index = t; }
}
for (unsigned int c = 0; c < quads.size(); c++)
{
Vec3d v = bases[c0+c] * (pos - in[quads[c][0]]);
double d = std::max(std::max(-v[0],-v[1]),std::max(std::max(v[1]-1,v[0]-1),std::max(v[2]-0.01,-v[2]-0.01)));
if (d>0) d = (pos-centers[c0+c]).norm2();
if (d<distance) { coefs = v; distance = d; index = c0+c; }
}
if (distance>0)
{
++outside;
}
if (index < c0)
addPointInTriangle(index, coefs.ptr());
else
addPointInQuad(index-c0, coefs.ptr());
}
}
else
{
int c0 = tetras.size();
bases.resize(tetras.size()+cubes.size());
centers.resize(tetras.size()+cubes.size());
for (unsigned int t = 0; t < tetras.size(); t++)
{
Mat3x3d m,mt;
m[0] = in[tetras[t][1]]-in[tetras[t][0]];
m[1] = in[tetras[t][2]]-in[tetras[t][0]];
m[2] = in[tetras[t][3]]-in[tetras[t][0]];
mt.transpose(m);
bases[t].invert(mt);
centers[t] = (in[tetras[t][0]]+in[tetras[t][1]]+in[tetras[t][2]]+in[tetras[t][3]])*0.25;
//std::cout << "Tetra "<<t<<" center="<<centers[t]<<" base="<<m<<std::endl;
}
for (unsigned int c = 0; c < cubes.size(); c++)
{
Mat3x3d m,mt;
m[0] = in[cubes[c][1]]-in[cubes[c][0]];
#ifdef SOFA_NEW_HEXA
m[1] = in[cubes[c][3]]-in[cubes[c][0]];
#else
m[1] = in[cubes[c][2]]-in[cubes[c][0]];
#endif
m[2] = in[cubes[c][4]]-in[cubes[c][0]];
mt.transpose(m);
bases[c0+c].invert(mt);
centers[c0+c] = (in[cubes[c][0]]+in[cubes[c][1]]+in[cubes[c][2]]+in[cubes[c][3]]+in[cubes[c][4]]+in[cubes[c][5]]+in[cubes[c][6]]+in[cubes[c][7]])*0.125;
}
for (unsigned int i=0;i<out.size();i++)
{
Vector3 pos = out[i];
Vector3 coefs;
int index = -1;
double distance = 1e10;
for (unsigned int t = 0; t < tetras.size(); t++)
{
Vector3 v = bases[t] * (pos - in[tetras[t][0]]);
double d = std::max(std::max(-v[0],-v[1]),std::max(-v[2],v[0]+v[1]+v[2]-1));
if (d>0) d = (pos-centers[t]).norm2();
if (d<distance) { coefs = v; distance = d; index = t; }
}
for (unsigned int c = 0; c < cubes.size(); c++)
{
Vector3 v = bases[c0+c] * (pos - in[cubes[c][0]]);
double d = std::max(std::max(-v[0],-v[1]),std::max(std::max(-v[2],v[0]-1),std::max(v[1]-1,v[2]-1)));
if (d>0) d = (pos-centers[c0+c]).norm2();
if (d<distance) { coefs = v; distance = d; index = c0+c; }
}
if (distance>0)
{
++outside;
}
if (index < c0)
addPointInTetra(index, coefs.ptr());
else
addPointInCube(index-c0, coefs.ptr());
}
}
std::cout << "CUDA: BarycentricMapperMeshTopology: map initialized, "<<size<<" output points, max "<<maxNIn<<" inputs points per output, "<<map.size()*BSIZE<<" contributions total."<<std::endl;
}
template <typename VecIn, typename VecOut>
void BarycentricMapperMeshTopology<gpu::cuda::CudaVectorTypes<VecIn,VecIn,float>, gpu::cuda::CudaVectorTypes<VecOut,VecOut,float> >::calcMapT()
{
if (size > 0 && maxNOut == 0)
{
// compute mapT
std::vector<int> nout;
int nb = (size+BSIZE-1)/BSIZE;
for (int b=0;b<nb;b++)
{
for (int j=0;j<maxNIn;j++)
{
int n = (b<nb-1) ? BSIZE : size-b*BSIZE;
for (int i=0;i<n;i++)
{
int index = map[b*maxNIn+j].d[i].i-1;
std::cout << "map["<<b<<"*"<<maxNIn<<"+"<<j<<"].index["<<i<<"]="<<index<<std::endl;
if (index >= 0)
{
if ((unsigned)index >= nout.size()) nout.resize(index+1);
nout[index]++;
}
}
}
}
insize = nout.size();
for (int i=0;i<insize;i++)
if (nout[i] > maxNOut) maxNOut = nout[i];
int nbloc = (insize+BSIZE-1)/BSIZE;
std::cout << "CudaBarycentricMapping: mapT with "<<maxNOut<<" entries per DOF and "<<nbloc<<" blocs."<<std::endl;
mapT.clear();
mapT.resize(nbloc*maxNOut);
//for (unsigned int i=0;i<mapT.size();i++)
// mapT[i] = std::make_pair(-1,0.0f);
nout.clear();
nout.resize(insize);
for (int b=0;b<nb;b++)
{
for (int j=0;j<maxNIn;j++)
{
int n = (b<nb-1) ? BSIZE : size-b*BSIZE;
for (int i=0;i<n;i++)
{
int index = map[b*maxNIn+j].d[i].i-1;
float val = (float) map[b*maxNIn+j].d[i].val;
if (index >= 0)
{
int num = nout[index]++;
int bo = (index / BSIZE); index -= b*BSIZE;
mapT[bo*maxNOut+num].d[index].i = b*BSIZE+i;
mapT[bo*maxNOut+num].d[index].val = val;
}
}
}
}
}
}
template<>
void BarycentricMapperMeshTopology<CudaVec3fTypes,CudaVec3fTypes>::apply( Out::VecCoord& out, const In::VecCoord& in )
{
out.fastResize(size);
MeshMapperCuda3f_apply(size, maxNIn, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperMeshTopology<CudaVec3fTypes,CudaVec3fTypes>::applyJ( Out::VecDeriv& out, const In::VecDeriv& in )
{
out.fastResize(size);
MeshMapperCuda3f_apply(size, maxNIn, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperMeshTopology<CudaVec3fTypes,CudaVec3fTypes>::applyJT( In::VecDeriv& out, const Out::VecDeriv& in )
{
if (size == 0) return;
calcMapT();
MeshMapperCuda3f_applyPEq(insize, maxNOut, mapT.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperMeshTopology<CudaVec3fTypes,CudaVec3fTypes>::applyJT( In::VecConst& /*out*/, const Out::VecConst& /*in*/ )
{
}
template<>
void BarycentricMapperMeshTopology<CudaVec3fTypes,CudaVec3fTypes>::draw( const Out::VecCoord& /*out*/, const In::VecCoord& /*in*/)
{
}
template<>
void BarycentricMapperMeshTopology<CudaVec3f1Types,CudaVec3f1Types>::apply( Out::VecCoord& out, const In::VecCoord& in )
{
out.fastResize(size);
MeshMapperCuda3f1_apply(size, maxNIn, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperMeshTopology<CudaVec3f1Types,CudaVec3f1Types>::applyJ( Out::VecDeriv& out, const In::VecDeriv& in )
{
out.fastResize(size);
MeshMapperCuda3f1_apply(size, maxNIn, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperMeshTopology<CudaVec3f1Types,CudaVec3f1Types>::applyJT( In::VecDeriv& out, const Out::VecDeriv& in )
{
if (size == 0) return;
calcMapT();
MeshMapperCuda3f1_applyPEq(insize, maxNOut, mapT.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperMeshTopology<CudaVec3f1Types,CudaVec3f1Types>::applyJT( In::VecConst& /*out*/, const Out::VecConst& /*in*/ )
{
}
template<>
void BarycentricMapperMeshTopology<CudaVec3f1Types,CudaVec3f1Types>::draw( const Out::VecCoord& /*out*/, const In::VecCoord& /*in*/)
{
}
template<>
void BarycentricMapperMeshTopology<CudaVec3f1Types,CudaVec3fTypes>::apply( Out::VecCoord& out, const In::VecCoord& in )
{
out.fastResize(size);
MeshMapperCuda3f1_3f_apply(size, maxNIn, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperMeshTopology<CudaVec3f1Types,CudaVec3fTypes>::applyJ( Out::VecDeriv& out, const In::VecDeriv& in )
{
out.fastResize(size);
MeshMapperCuda3f1_3f_apply(size, maxNIn, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperMeshTopology<CudaVec3f1Types,CudaVec3fTypes>::applyJT( In::VecDeriv& out, const Out::VecDeriv& in )
{
if (size == 0) return;
calcMapT();
MeshMapperCuda3f_3f1_applyPEq(insize, maxNOut, mapT.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperMeshTopology<CudaVec3f1Types,CudaVec3fTypes>::applyJT( In::VecConst& /*out*/, const Out::VecConst& /*in*/ )
{
}
template<>
void BarycentricMapperMeshTopology<CudaVec3f1Types,CudaVec3fTypes>::draw( const Out::VecCoord& /*out*/, const In::VecCoord& /*in*/)
{
}
template<>
void BarycentricMapperMeshTopology<CudaVec3fTypes,CudaVec3f1Types>::apply( Out::VecCoord& out, const In::VecCoord& in )
{
out.fastResize(size);
MeshMapperCuda3f_3f1_apply(size, maxNIn, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperMeshTopology<CudaVec3fTypes,CudaVec3f1Types>::applyJ( Out::VecDeriv& out, const In::VecDeriv& in )
{
out.fastResize(size);
MeshMapperCuda3f_3f1_apply(size, maxNIn, map.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperMeshTopology<CudaVec3fTypes,CudaVec3f1Types>::applyJT( In::VecDeriv& out, const Out::VecDeriv& in )
{
if (size == 0) return;
calcMapT();
MeshMapperCuda3f1_3f_applyPEq(insize, maxNOut, mapT.deviceRead(), out.deviceWrite(), in.deviceRead());
}
template<>
void BarycentricMapperMeshTopology<CudaVec3fTypes,CudaVec3f1Types>::applyJT( In::VecConst& /*out*/, const Out::VecConst& /*in*/ )
{
}
template<>
void BarycentricMapperMeshTopology<CudaVec3fTypes,CudaVec3f1Types>::draw( const Out::VecCoord& /*out*/, const In::VecCoord& /*in*/)
{
}
} // namespace mapping
} // namespace component
} // namespace sofa
#endif
|