OpenWalnut  1.5.0dev
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 <memory>
26 #include <vector>
27 
28 #include "WDataSetDipoles.h"
29 
30 // prototype instance as singleton
31 std::shared_ptr< WPrototyped > WDataSetDipoles::m_prototype = std::shared_ptr< WPrototyped >();
32 
33 
35  m_maxMagnitude( 0.0f )
36 {
37 }
38 
39 WDataSetDipoles::WDataSetDipoles( WPosition dipPos, std::vector<float> mags, std::vector<float> times,
40  size_t firstTimeStep, size_t lastTimeStep ) :
41  m_maxMagnitude( 0.0f )
42 {
43  WAssert( mags.size() == times.size(), "There has to be a magnitude for every time and vice versa." );
44  for( size_t id = 0; id < times.size() - 1; ++id )
45  {
46  WAssert( times[id] < times[id+1], "Times need to be ascending." );
47  }
48  addDipole( dipPos, mags, times, firstTimeStep, lastTimeStep );
49 }
50 
52 {
53 }
54 
55 std::shared_ptr< WPrototyped > WDataSetDipoles::getPrototype()
56 {
57  if( !m_prototype )
58  {
59  m_prototype = std::shared_ptr< WPrototyped >( new WDataSetDipoles() );
60  }
61 
62  return m_prototype;
63 }
64 
65 size_t WDataSetDipoles::addDipole( WPosition dipPos, std::vector<float> mags, std::vector<float> times,
66  size_t firstTimeStep, size_t lastTimeStep )
67 {
68  Dipole dipole;
69  dipole.m_dipolePosition = dipPos;
70  dipole.m_magnitudes = mags;
71  dipole.m_times = times;
72  dipole.m_firstTimeStep = firstTimeStep;
73  dipole.m_lastTimeStep = lastTimeStep;
74  m_dipoles.push_back( dipole );
75 
76  for( size_t id = 0u; id < mags.size(); ++id )
77  {
78  if( mags[id] > m_maxMagnitude )
79  {
80  m_maxMagnitude = mags[id];
81  }
82  }
83 
84  return m_dipoles.size() - 1;
85 }
86 
88 {
89  return m_dipoles[dipoleId].m_dipolePosition;
90 }
91 
92 float WDataSetDipoles::getStartTime( size_t dipoleId ) const
93 {
94  return m_dipoles[dipoleId].m_times[m_dipoles[dipoleId].m_firstTimeStep];
95 }
96 
97 float WDataSetDipoles::getEndTime( size_t dipoleId ) const
98 {
99  return m_dipoles[dipoleId].m_times[m_dipoles[dipoleId].m_lastTimeStep];
100 }
101 
102 std::vector<float> WDataSetDipoles::getTimes( size_t dipoleId ) const
103 {
104  const Dipole& dipole = m_dipoles[dipoleId];
105  const std::vector<float>::const_iterator& begin = dipole.m_times.begin();
106 
107  return std::vector<float>( begin + dipole.m_firstTimeStep, begin + ( dipole.m_lastTimeStep + 1u ) );
108 }
109 
110 std::vector<float> WDataSetDipoles::getMagnitudes( size_t dipoleId ) const
111 {
112  const Dipole& dipole = m_dipoles[dipoleId];
113  const std::vector<float>::const_iterator& begin = dipole.m_magnitudes.begin();
114 
115  return std::vector<float>( begin + dipole.m_firstTimeStep, begin + ( dipole.m_lastTimeStep + 1u ) );
116 }
117 
119 {
120  return m_dipoles.size();
121 }
122 
124 {
125  return m_maxMagnitude;
126 }
127 
128 float WDataSetDipoles::getMagnitude( float time, size_t dipoleId )
129 {
130  std::vector<float>& times = m_dipoles[dipoleId].m_times;
131  std::vector<float>& magnitudes = m_dipoles[dipoleId].m_magnitudes;
132 
133  if( time < times[0] || time > times.back() )
134  {
135  return 0;
136  }
137  else
138  {
139  size_t upperBoundId = 1u;
140  for( ; upperBoundId < times.size() - 1u; ++upperBoundId )
141  {
142  if( time < times[upperBoundId] )
143  {
144  break;
145  }
146  }
147  float scale = ( time - times[upperBoundId-1] ) / ( times[upperBoundId] - times[upperBoundId-1] );
148  float magnitude = magnitudes[upperBoundId-1] + scale * ( magnitudes[upperBoundId] - magnitudes[upperBoundId-1] );
149  return magnitude;
150  }
151 }
Internal class representing one dipole.
WPosition m_dipolePosition
The location of the dipole.
std::vector< float > m_times
Times for the different magnitudes.
std::vector< float > m_magnitudes
The magnitude of the dipole.
size_t m_lastTimeStep
Last time where the magnitude is not 0.
size_t m_firstTimeStep
First time where the magnitude is not 0.
float getStartTime(size_t dipoleId=0u) const
Return first time where the magnitude is not 0.
std::vector< float > getTimes(size_t dipoleId=0u) const
Return the times where the magnitude is not 0.
float getMaxMagnitude() const
Return the biggest magnitude of all dipoles.
~WDataSetDipoles()
Destructs this dataset.
std::vector< Dipole > m_dipoles
List of dipoles representeing this dipoles dataset.
float getMagnitude(float time, size_t dipoleId=0)
Return magnitude of dipole for a given time.
static std::shared_ptr< WPrototyped > m_prototype
The prototype as singleton.
float m_maxMagnitude
Biggest magnitude of all dipoles.
WPosition getPosition(size_t dipoleId=0)
Return position of dipole.
float getEndTime(size_t dipoleId=0u) const
Return last time where the magnitude is not 0.
std::vector< float > getMagnitudes(size_t dipoleId=0u) const
Return the magnitudes where the magnitude is not 0.
WDataSetDipoles()
Creates a new dipole dataset.
size_t getNumberOfDipoles()
Return number of dipoles in this dataset.
static std::shared_ptr< WPrototyped > getPrototype()
Returns a prototype instantiated with the true type of the deriving class.
size_t addDipole(WPosition dipPos, std::vector< float > mags, std::vector< float > times, size_t firstTimeStep, size_t lastTimeStep)
Adds a new dipole with given information and checks consistency of the information.
This only is a 3d double vector.