2323 ****************************************************************************/
2424#pragma once
2525
26- #include < InfinyToolkit/CenterLineForceField .h>
26+ #include < InfinyToolkit/ProximityOscillatorConstraint .h>
2727#include < sofa/type/vector.h>
2828#include < sofa/defaulttype/VecTypes.h>
2929#include < sofa/defaulttype/RigidTypes.h>
@@ -36,25 +36,15 @@ namespace sofa::infinytoolkit
3636{
3737
3838template <class DataTypes >
39- CenterLineForceField <DataTypes>::CenterLineForceField ()
39+ ProximityOscillatorConstraint <DataTypes>::ProximityOscillatorConstraint ()
4040 : d_positions(initData(&d_positions, " position" , " List of coordinates points" ))
41- , d_restPositions(initData(&d_restPositions, " restPositions" , " List of rest coordinates points" ))
42- , d_outputPositions(initData(&d_outputPositions, " outputPositions" , " List of output coordinates points" ))
41+ , d_outputPositions(initData(&d_outputPositions, " outputPositions" , " List of output coordinates points" ))
4342 , d_centers(initData(&d_centers, " centers" , " List of center coordinates points" ))
44- , d_force(initData(&d_force, 1 .0_sreal, " force" , " Applied force to all points to simulate maximum compression." ))
45- , d_uniformForce(initData(&d_uniformForce, bool (false ), " uniformForce" , " If true, will apply the same force at each vertex otherwise will apply force proportional to the distance to the barycenter" ))
4643 , d_pace(initData(&d_pace, 1 .0_sreal, " pace" , " Time to perform a full Pace (deflate + inflate). Same scale as the simulation time." ))
47-
48- , d_stiffness(initData(&d_stiffness, 1000 .0_sreal, " stiffness" , " Time to perform a full Pace (deflate + inflate). Same scale as the simulation time." ))
49-
50-
51- , d_refreshBaryRate(initData(&d_refreshBaryRate, (unsigned int )(0 ), " refreshBaryRate" , " To recompute barycenter every X pace. 0 by default == no refresh" ))
52- , p_showForce(initData(&p_showForce, bool (false ), " showForce" , " Parameter to display the force direction" ))
53- , d_syncRealTime(initData(&d_syncRealTime, false , " syncRealTime" , " Synchronize with the real time instead of simulation time." ))
54- , d_frequency(initData(&d_frequency, 1 .0_sreal, " frequency" , " Frequency at which the full deflate+inflate is done, in Hz, i.e x/sec. Used if pace is not set." ))
44+ , d_amplitude(initData(&d_amplitude, 0 .8_sreal, " amplitude" , " Amplitude of the oscillation" ))
45+ , p_showMotion(initData(&p_showMotion, bool (false ), " showMotion" , " Parameter to display the force direction" ))
5546 , m_startTime()
5647{
57- addInput (&d_restPositions);
5848 addInput (&d_positions);
5949 addInput (&d_centers);
6050
@@ -64,7 +54,7 @@ CenterLineForceField<DataTypes>::CenterLineForceField()
6454
6555
6656template <class DataTypes >
67- void CenterLineForceField <DataTypes>::init()
57+ void ProximityOscillatorConstraint <DataTypes>::init()
6858{
6959 size_t nbPoints = d_positions.getValue ().size ();
7060 if (nbPoints == 0 )
@@ -74,18 +64,13 @@ void CenterLineForceField<DataTypes>::init()
7464 return ;
7565 }
7666
77- if (!d_pace.isSet () && !d_frequency. isSet () )
67+ if (!d_pace.isSet ())
7868 {
79- msg_error () << " Neither Pace nor frequency are set. Won't be able to compute pace." ;
69+ msg_error () << " Pace is not set. Won't be able to compute pace." ;
8070 sofa::core::objectmodel::BaseObject::d_componentState.setValue (sofa::core::objectmodel::ComponentState::Invalid);
8171 return ;
8272 }
8373
84- if (d_pace.isSet () && d_frequency.isSet ())
85- {
86- msg_warning () << " Pace and frequency are both set. Will take into account the frequency value." ;
87- }
88-
8974 computeDistribution ();
9075
9176 m_startTime = std::chrono::system_clock::now ();
@@ -95,40 +80,25 @@ void CenterLineForceField<DataTypes>::init()
9580
9681
9782template <class DataTypes >
98- void CenterLineForceField<DataTypes>::computeBarycenter()
99- {
100- sofa::helper::ReadAccessor< core::objectmodel::Data< VecCoord > > _x = d_positions;
101-
102- m_bary = Coord (0.0 , 0.0 , 0.0 );
103- size_t nbPoints = _x.size ();
104- for (size_t i = 0 ; i < nbPoints; ++i)
105- {
106- m_bary += _x[i];
107- }
108-
109- m_bary /= nbPoints;
110- }
111-
112-
113-
114- template <class DataTypes >
115- void CenterLineForceField<DataTypes>::computeDistribution()
83+ void ProximityOscillatorConstraint<DataTypes>::computeDistribution()
11684{
11785 sofa::helper::ReadAccessor< core::objectmodel::Data< VecCoord > > _x = d_positions;
11886 sofa::helper::ReadAccessor< core::objectmodel::Data< VecCoord > > _centers = d_centers;
119- // m_centersOrdered.resize(_centers.size());
87+
12088 m_centersOrdered = _centers;
12189 std::sort (m_centersOrdered.begin (), m_centersOrdered.end (),
12290 [](const Coord& a, const Coord& b) {
12391 return a[1 ] > b[1 ];
12492 });
12593
126- for ( size_t i = 0 ; i < _centers. size (); ++i )
94+ if (f_printLog. getValue () )
12795 {
128- std::cout << i << " : " << _centers[i] << " | " << m_centersOrdered[i] << std::endl;
96+ for (size_t i = 0 ; i < _centers.size (); ++i)
97+ {
98+ std::cout << i << " : " << _centers[i] << " | " << m_centersOrdered[i] << std::endl;
99+ }
129100 }
130101
131-
132102 m_distribution.resize (_x.size ());
133103
134104 Coord pt2;
@@ -147,7 +117,7 @@ void CenterLineForceField<DataTypes>::computeDistribution()
147117
148118
149119template <class DataTypes >
150- void CenterLineForceField <DataTypes>::doUpdate()
120+ void ProximityOscillatorConstraint <DataTypes>::doUpdate()
151121{
152122 if (sofa::core::objectmodel::BaseObject::d_componentState.getValue () != sofa::core::objectmodel::ComponentState::Valid)
153123 return ;
@@ -157,59 +127,28 @@ void CenterLineForceField<DataTypes>::doUpdate()
157127 sofa::helper::ReadAccessor< core::objectmodel::Data< VecCoord > > _centers = d_centers;
158128
159129 outX.resize (inX.size ());
160- Real pacePercent = 1 .0_sreal;
161- Real time = 1 .0_sreal;
162-
163- const Real pace = (!d_frequency.isSet ()) ? d_pace.getValue () : Real (1.0 / d_frequency.getValue ());
164-
165- if (d_syncRealTime.getValue ())
166- {
167- using namespace std ::chrono;
130+ Real time = this ->getContext ()->getTime ();
168131
169- std::chrono::duration<Real> duration = system_clock::now () - m_startTime;
170- time = duration.count ();
171- }
172- else
173- {
174- time = this ->getContext ()->getTime ();
175- }
132+ const Real pace = d_pace.getValue ();
176133
177134 // we apply a force proportional to the pace rate. 0 Force at start of pace, 0 at end, F at half pace
178- pacePercent = std::fmod (time, pace) / pace;
179- // std::cout << "std::fmod(time, pace): " << std::fmod(time, pace) << std::endl;
180- // std::cout << "std::fmod(time, pace)/2*pace: " << std::fmod(time, pace)/(2*pace) << std::endl;
181- // std::cout << "time: " << time << " | pace: " << pace << " | pacePercent: " << pacePercent << std::endl;
182-
135+ const Real pacePercent = std::fmod (time, pace) / pace;
136+
183137 if (time >= nextStart) // at every pace/2 we start moving a new center and stop previous one
184138 {
185139 centerStart++;
186140 centerDone++;
187-
188- // if (centerCurrent == 4)
189- {
190- /* centerDone++;
191- centerCurrent = 0;*/
192- std::cout << " Center " << centerDone << " done at time " << time << std::endl;
193- }
194-
195- // centerDone++;
196- std::cout << " Center " << centerStart << " started at time " << time << std::endl;
197-
198141 nextStart += pace / length;
199142 }
200143
201144 // G ---- X -- X0
202145
203- const Real amplitude = 0.8 ;
146+ const Real amplitude = d_amplitude. getValue () ;
204147 const Real frequency = pace;
205148
206- // std::cout << "oscillation: " << oscillation << std::endl;
207- // std::cout << "centerStart: " << centerStart << " | " << centerDone << " / " << _centers.size() << " centers done." << std::endl;
208149 for (size_t i = 0 ; i < inX.size (); ++i)
209150 {
210151 int centerId = m_distribution[i];
211-
212-
213152 if (centerId > centerStart || centerId <= centerDone )
214153 {
215154 outX[i] = inX[i];
@@ -226,35 +165,22 @@ void CenterLineForceField<DataTypes>::doUpdate()
226165 Real oscillation = amplitude * std::cos (pacePercent * 2.0 * M_PI - omega);
227166 oscillation = amplitude - (oscillation + amplitude) / 2 .0_sreal; // normalize between 0 and 0.9
228167
229- // cos: [-1; 1] 1 -1 1
230- // [0; 2] 2 0 2
231- // [0; 1] 1 0 1
232- // [1; 0] 0 1 0
233-
234- // 0.9 -0.9 0.9
235- // 1.8 0 1.8
236- // 0.9 0 0.9
237- // 0 0.9 0
238-
239168 outX[i] = p0 + dir * oscillation;
240169 }
241-
242-
243170
244171 if (centerDone >= int (m_centersOrdered.size ()) && pacePercent >= 0.99 )
245172 {
246- std::cout << " centerDone " << centerDone << " | " << m_centersOrdered.size () << std::endl;
173+ if (f_printLog.getValue ())
174+ std::cout << " centerDone " << centerDone << " | " << m_centersOrdered.size () << std::endl;
247175
248176 centerDone = -4 ;
249177 centerStart = 0 ;
250178 nextStart = time + pace / length;
251179 }
252-
253-
254180}
255181
256182template <class DataTypes >
257- void CenterLineForceField <DataTypes>::handleEvent(sofa::core::objectmodel::Event* event)
183+ void ProximityOscillatorConstraint <DataTypes>::handleEvent(sofa::core::objectmodel::Event* event)
258184{
259185 // std::cout << "event" << std::endl;
260186 if (simulation::AnimateBeginEvent::checkEventType (event))
@@ -265,9 +191,9 @@ void CenterLineForceField<DataTypes>::handleEvent(sofa::core::objectmodel::Event
265191
266192
267193template <class DataTypes >
268- void CenterLineForceField <DataTypes>::draw(const core::visual::VisualParams* vparams)
194+ void ProximityOscillatorConstraint <DataTypes>::draw(const core::visual::VisualParams* vparams)
269195{
270- if (/* !vparams->displayFlags().getShowForceFields() || */ !p_showForce .getValue ()) {
196+ if (!p_showMotion .getValue ()) {
271197 return ;
272198 }
273199 const auto stateLifeCycle = vparams->drawTool ()->makeStateLifeCycle ();
0 commit comments