@@ -617,31 +617,23 @@ void VisualModelImpl::applyUVTransformation()
617617
618618void VisualModelImpl::applyTranslation (const SReal dx, const SReal dy, const SReal dz)
619619{
620- const Coord d ((Real)dx,(Real)dy,(Real)dz);
621-
622- Data< VecCoord >* d_x = this ->write (core::vec_id::write_access::position);
623- VecCoord &x = *d_x->beginEdit ();
624-
625- for (std::size_t i = 0 ; i < x.size (); i++)
620+ constexpr auto computeTranslation = [](auto id, auto & vm, const auto & d)
626621 {
627- x[i] += d;
628- }
629-
630- d_x->endEdit ();
631-
632- if (d_initRestPositions.getValue ())
633- {
634- VecCoord& restPositions = *(m_restPositions.beginEdit ());
635-
636- for (std::size_t i = 0 ; i < restPositions.size (); i++)
622+ auto positions = sofa::helper::getWriteOnlyAccessor (*vm.write (id));
623+
624+ for (auto & p : positions)
637625 {
638- restPositions[i] += d;
626+ p += d;
639627 }
628+ };
629+
630+ const Coord d ((Real)dx,(Real)dy,(Real)dz);
631+ computeTranslation (core::vec_id::write_access::position, *this , d);
640632
641- m_restPositions.endEdit ();
633+ if (d_initRestPositions.getValue ())
634+ {
635+ computeTranslation (core::vec_id::write_access::restPosition, *this , d);
642636 }
643-
644- updateVisual (sofa::core::visual::visualparams::defaultInstance ());
645637}
646638
647639void VisualModelImpl::applyRotation (const SReal rx, const SReal ry, const SReal rz)
@@ -652,60 +644,44 @@ void VisualModelImpl::applyRotation(const SReal rx, const SReal ry, const SReal
652644
653645void VisualModelImpl::applyRotation (const Quat<SReal> q)
654646{
655- Data< VecCoord >* d_x = this ->write (core::vec_id::write_access::position);
656- VecCoord &x = *d_x->beginEdit ();
657-
658- for (std::size_t i = 0 ; i < x.size (); i++)
659- {
660- x[i] = q.rotate (x[i]);
661- }
662-
663- d_x->endEdit ();
664-
665- if (d_initRestPositions.getValue ())
647+ constexpr auto computeRotation = [](auto id, auto & vm, const auto & q)
666648 {
667- VecCoord& restPositions = *(m_restPositions. beginEdit ( ));
649+ auto positions = sofa::helper::getWriteOnlyAccessor (*vm. write (id ));
668650
669- for (std:: size_t i = 0 ; i < restPositions. size (); i++ )
651+ for (auto & p : positions )
670652 {
671- restPositions[i] = q.rotate (restPositions[i] );
653+ p = q.rotate (p );
672654 }
655+ };
656+
657+ computeRotation (core::vec_id::write_access::position, *this , q);
673658
674- m_restPositions.endEdit ();
659+ if (d_initRestPositions.getValue ())
660+ {
661+ computeRotation (core::vec_id::write_access::restPosition, *this , q);
675662 }
676-
677- updateVisual (sofa::core::visual::visualparams::defaultInstance ());
678663}
679664
680665void VisualModelImpl::applyScale (const SReal sx, const SReal sy, const SReal sz)
681666{
682- Data< VecCoord >* d_x = this ->write (core::vec_id::write_access::position);
683- VecCoord &x = *d_x->beginEdit ();
684-
685- for (std::size_t i = 0 ; i < x.size (); i++)
686- {
687- x[i][0 ] *= (Real)sx;
688- x[i][1 ] *= (Real)sy;
689- x[i][2 ] *= (Real)sz;
690- }
691-
692- d_x->endEdit ();
693-
694- if (d_initRestPositions.getValue ())
667+ constexpr auto computeScale = [](auto id, auto & vm, const auto sx, const auto sy, const auto sz)
695668 {
696- VecCoord& restPositions = *(m_restPositions. beginEdit ( ));
669+ auto positions = sofa::helper::getWriteOnlyAccessor (*vm. write (id ));
697670
698- for (std:: size_t i = 0 ; i < restPositions. size (); i++ )
671+ for (auto & p : positions )
699672 {
700- restPositions[i] [0 ] *= (Real)sx;
701- restPositions[i] [1 ] *= (Real)sy;
702- restPositions[i] [2 ] *= (Real)sz;
673+ p [0 ] *= (Real)sx;
674+ p [1 ] *= (Real)sy;
675+ p [2 ] *= (Real)sz;
703676 }
704-
705- m_restPositions.endEdit ();
677+ };
678+
679+ computeScale (core::vec_id::write_access::position, *this , sx, sy, sz);
680+
681+ if (d_initRestPositions.getValue ())
682+ {
683+ computeScale (core::vec_id::write_access::restPosition, *this , sx, sy, sz);
706684 }
707-
708- updateVisual (sofa::core::visual::visualparams::defaultInstance ());
709685}
710686
711687void VisualModelImpl::applyUVTranslation (const Real dU, const Real dV)
@@ -790,9 +766,15 @@ void VisualModelImpl::init()
790766 return ;
791767 }
792768
793- applyScale (d_scale.getValue ()[0 ], d_scale.getValue ()[1 ], d_scale.getValue ()[2 ]);
794- applyRotation (d_rotation.getValue ()[0 ], d_rotation.getValue ()[1 ], d_rotation.getValue ()[2 ]);
795- applyTranslation (d_translation.getValue ()[0 ], d_translation.getValue ()[1 ], d_translation.getValue ()[2 ]);
769+ const auto & scale = d_scale.getValue ();
770+ const auto & rotation = d_rotation.getValue ();
771+ const auto & translation = d_translation.getValue ();
772+
773+ applyScale (scale[0 ], scale[1 ], scale[2 ]);
774+ applyRotation (rotation[0 ], rotation[1 ], rotation[2 ]);
775+ applyTranslation (translation[0 ], translation[1 ], translation[2 ]);
776+
777+ updateVisual (sofa::core::visual::visualparams::defaultInstance ());
796778
797779 d_translation.setValue (Vec3Real ());
798780 d_rotation.setValue (Vec3Real ());
@@ -1184,6 +1166,7 @@ void VisualModelImpl::computeBBox(const core::ExecParams*, bool)
11841166 {
11851167 bbox.include (v);
11861168 }
1169+
11871170 this ->f_bbox .setValue (bbox);
11881171}
11891172
0 commit comments