@@ -32,14 +32,14 @@ class LegacyShapeMatrix : public vnl_matrix<double>, public Observer {
3232 /* * Method for creation through the object factory. */
3333 itkNewMacro (Self)
3434
35- /* * Run-time type information (and related methods). */
36- itkTypeMacro (LegacyShapeMatrix, Observer)
37-
38- /* * Callbacks that may be defined by a subclass. If a subclass defines one
39- of these callback methods, the corresponding flag in m_DefinedCallbacks
40- should be set to true so that the ParticleSystem will know to register
41- the appropriate event with this method. */
42- virtual void DomainAddEventCallback (Object*, const itk::EventObject& e) {
35+ /* * Run-time type information (and related methods). */
36+ itkTypeMacro (LegacyShapeMatrix, Observer)
37+
38+ /* * Callbacks that may be defined by a subclass. If a subclass defines one
39+ of these callback methods, the corresponding flag in m_DefinedCallbacks
40+ should be set to true so that the ParticleSystem will know to register
41+ the appropriate event with this method. */
42+ virtual void DomainAddEventCallback (Object*, const itk::EventObject& e) {
4343 const ParticleDomainAddEvent& event = dynamic_cast <const ParticleDomainAddEvent&>(e);
4444 unsigned int d = event.GetDomainIndex ();
4545
@@ -67,16 +67,28 @@ class LegacyShapeMatrix : public vnl_matrix<double>, public Observer {
6767 const typename ParticleSystem::PointType pos = ps->GetTransformedPosition (idx, d);
6868
6969 int numRows = 0 ;
70- for (int i = 0 ; i < m_DomainsPerShape; i++) numRows += VDimension * ps->GetNumberOfParticles (i);
70+ for (int i = 0 ; i < m_DomainsPerShape; i++) {
71+ numRows += VDimension * ps->GetNumberOfParticles (i);
72+ }
7173
72- if (numRows > this ->rows ()) this ->ResizeMatrix (numRows, this ->cols ());
74+ if (numRows > this ->rows ()) {
75+ this ->ResizeMatrix (numRows, this ->cols ());
76+ }
7377
7478 unsigned int k = 0 ;
7579 int dom = d % m_DomainsPerShape;
76- for (int i = 0 ; i < dom; i++) k += VDimension * ps->GetNumberOfParticles (i);
80+ for (int i = 0 ; i < dom; i++) {
81+ k += VDimension * ps->GetNumberOfParticles (i);
82+ }
7783 k += idx * VDimension;
7884
79- for (unsigned int i = 0 ; i < VDimension; i++) this ->operator ()(i + k, d / m_DomainsPerShape) = pos[i];
85+ for (unsigned int i = 0 ; i < VDimension; i++) {
86+ if (i + k >= this ->rows ()) {
87+ throw std::runtime_error (
88+ " PositionSetEventCallback: index out of bounds! Different number of particles per shape?" );
89+ }
90+ this ->operator ()(i + k, d / m_DomainsPerShape) = pos[i];
91+ }
8092 }
8193
8294 virtual void PositionSetEventCallback (Object* o, const itk::EventObject& e) {
@@ -89,10 +101,18 @@ class LegacyShapeMatrix : public vnl_matrix<double>, public Observer {
89101
90102 unsigned int k = 0 ;
91103 int dom = d % m_DomainsPerShape;
92- for (int i = 0 ; i < dom; i++) k += VDimension * ps->GetNumberOfParticles (i);
104+ for (int i = 0 ; i < dom; i++) {
105+ k += VDimension * ps->GetNumberOfParticles (i);
106+ }
93107 k += idx * VDimension;
94108
95- for (unsigned int i = 0 ; i < VDimension; i++) this ->operator ()(i + k, d / m_DomainsPerShape) = pos[i];
109+ for (unsigned int i = 0 ; i < VDimension; i++) {
110+ if (i + k >= this ->rows ()) {
111+ throw std::runtime_error (
112+ " PositionSetEventCallback: index out of bounds! Different number of particles per shape?" );
113+ }
114+ this ->operator ()(i + k, d / m_DomainsPerShape) = pos[i];
115+ }
96116 }
97117
98118 virtual void PositionRemoveEventCallback (Object*, const itk::EventObject&) {
0 commit comments