OpenWalnut  1.4.0
WDataSetDipoles.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <vector>
26 
27 #include "WDataSetDipoles.h"
28 
29 // prototype instance as singleton
30 boost::shared_ptr< WPrototyped > WDataSetDipoles::m_prototype = boost::shared_ptr< WPrototyped >();
31 
32 
34  m_maxMagnitude( 0.0f )
35 {
36 }
37 
38 WDataSetDipoles::WDataSetDipoles( WPosition dipPos, std::vector<float> mags, std::vector<float> times,
39  size_t firstTimeStep, size_t lastTimeStep ) :
40  m_maxMagnitude( 0.0f )
41 {
42  WAssert( mags.size() == times.size(), "There has to be a magnitude for every time and vice versa." );
43  for( size_t id = 0; id < times.size() - 1; ++id )
44  {
45  WAssert( times[id] < times[id+1], "Times need to be ascending." );
46  }
47  addDipole( dipPos, mags, times, firstTimeStep, lastTimeStep );
48 }
49 
51 {
52 }
53 
54 boost::shared_ptr< WPrototyped > WDataSetDipoles::getPrototype()
55 {
56  if( !m_prototype )
57  {
58  m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetDipoles() );
59  }
60 
61  return m_prototype;
62 }
63 
64 size_t WDataSetDipoles::addDipole( WPosition dipPos, std::vector<float> mags, std::vector<float> times,
65  size_t firstTimeStep, size_t lastTimeStep )
66 {
67  Dipole dipole;
68  dipole.m_dipolePosition = dipPos;
69  dipole.m_magnitudes = mags;
70  dipole.m_times = times;
71  dipole.m_firstTimeStep = firstTimeStep;
72  dipole.m_lastTimeStep = lastTimeStep;
73  m_dipoles.push_back( dipole );
74 
75  for( size_t id = 0u; id < mags.size(); ++id )
76  {
77  if( mags[id] > m_maxMagnitude )
78  {
79  m_maxMagnitude = mags[id];
80  }
81  }
82 
83  return m_dipoles.size() - 1;
84 }
85 
87 {
88  return m_dipoles[dipoleId].m_dipolePosition;
89 }
90 
91 float WDataSetDipoles::getStartTime( size_t dipoleId ) const
92 {
93  return m_dipoles[dipoleId].m_times[m_dipoles[dipoleId].m_firstTimeStep];
94 }
95 
96 float WDataSetDipoles::getEndTime( size_t dipoleId ) const
97 {
98  return m_dipoles[dipoleId].m_times[m_dipoles[dipoleId].m_lastTimeStep];
99 }
100 
101 std::vector<float> WDataSetDipoles::getTimes( size_t dipoleId ) const
102 {
103  const Dipole& dipole = m_dipoles[dipoleId];
104  const std::vector<float>::const_iterator& begin = dipole.m_times.begin();
105 
106  return std::vector<float>( begin + dipole.m_firstTimeStep, begin + ( dipole.m_lastTimeStep + 1u ) );
107 }
108 
109 std::vector<float> WDataSetDipoles::getMagnitudes( size_t dipoleId ) const
110 {
111  const Dipole& dipole = m_dipoles[dipoleId];
112  const std::vector<float>::const_iterator& begin = dipole.m_magnitudes.begin();
113 
114  return std::vector<float>( begin + dipole.m_firstTimeStep, begin + ( dipole.m_lastTimeStep + 1u ) );
115 }
116 
118 {
119  return m_dipoles.size();
120 }
121 
123 {
124  return m_maxMagnitude;
125 }
126 
127 float WDataSetDipoles::getMagnitude( float time, size_t dipoleId )
128 {
129  std::vector<float>& times = m_dipoles[dipoleId].m_times;
130  std::vector<float>& magnitudes = m_dipoles[dipoleId].m_magnitudes;
131 
132  if( time < times[0] || time > times.back() )
133  {
134  return 0;
135  }
136  else
137  {
138  size_t upperBoundId = 1u;
139  for( ; upperBoundId < times.size() - 1u; ++upperBoundId )
140  {
141  if( time < times[upperBoundId] )
142  {
143  break;
144  }
145  }
146  float scale = ( time - times[upperBoundId-1] ) / ( times[upperBoundId] - times[upperBoundId-1] );
147  float magnitude = magnitudes[upperBoundId-1] + scale * ( magnitudes[upperBoundId] - magnitudes[upperBoundId-1] );
148  return magnitude;
149  }
150 }