File: Monitor.cpp

package info (click to toggle)
camitk 4.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 427,532 kB
  • sloc: cpp: 79,494; xml: 1,176; sh: 1,137; ansic: 142; makefile: 107; perl: 84; sed: 20
file content (272 lines) | stat: -rw-r--r-- 7,915 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2018 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/
// Monitor includes
#include "Monitor.h"

// Tools includes
#include "Tools.h"
#include "AtomIterator.h"

#include <sstream>

using namespace std;

// -------------------- constructor --------------------
Monitor::Monitor(mml::Monitor* m, MonitoringManager* monitoringManager, Monitor::type type): mappedObject(m) {
    valueType = type;
    startAt = timeParameter2double(m->startAt());
    stopAt = timeParameter2double(m->stopAt());
    index = (int) m->index();
    target = m->target();
    dx = 0;
    dy = 0;
    dz = 0;
    if (m->dx().present()) {
        dx = m->dx().get();
    }
    if (m->dy().present()) {
        dx = m->dy().get();
    }
    if (m->dz().present()) {
        dx = m->dz().get();
    }
    this->monitoringManager = monitoringManager;

    switch (valueType) {
        case Monitor::SCALAR:
            dimension = 1;
            break;
        case Monitor::SCALARSET:
            dimension = 1;
            break;
        case Monitor::VECTORSET:
            dimension = 3;
            break;
        case Monitor::MATRIX_33SET:
            dimension = 9;
            break;
    }

    //add references
    mml::Monitor::reference_sequence& it(m->reference());
    for (mml::Monitor::reference_iterator i(it.begin()); i != it.end(); ++i) {
        references.push_back(new Reference(*i, monitoringManager));
    }

    // how to store monitors data?
    // strategy here is:
    //	- 1) for monitors used to calculate data (during simulation): store indexes and monitored values in vectors, so that insertion (at every single step) is quick (O(1)) (no search needed)
    //	- 2) for monitors created from MMlout: store data in a map between indexes and corresponding values, so that research is quick (O(log(n))) (insertion is done once for all)
    //
    if (mappedObject->indexes().present() && mappedObject->data().present()) {
        // monitor used for output and there are indexes
        std::string ind = mappedObject->indexes().get();
        std::string data = mappedObject->data().get();
        istringstream issInd(ind, istringstream::in);
        istringstream issData(data, istringstream::in);


        while (!issInd.eof() && !issData.eof()) {
            int ind;
            issInd >> ind;
            for (int i = 0; i < dimension; i++) {
                double d;
                issData >> d;
                indexToValuesMap[ind].push_back(d);

            }
        }

    }
    else if (mappedObject->data().present()) {
        // global output monitors, there is no index, data are stored in index 0
        std::string data = mappedObject->data().get();
        istringstream issData(data, istringstream::in);


        while (!issData.eof()) {
            for (int i = 0; i < dimension; i++) {
                double d;
                issData >> d;
                indexToValuesMap[0].push_back(d);
            }
        }
    }

    // we also add data in vectors to use output monitors in "replay" mode
    //TODO this should be somewhat merged with previous
    if (!m->indexes().present()) {
        // mmlIn monitor, no data in target indexes
        AtomIterator iter = AtomIterator(monitoringManager->getInitPml(), target);
        for (iter.begin(); !iter.end(); iter.next()) {
            indexes.push_back(iter.currentAtom()->getIndex());
        }
    }

    // add data if exist
    if (m->data().present()) {
        std::string s = m->data().get();
        istringstream iss(s, istringstream::in);
        while (!iss.eof()) {
            double d;
            iss >> d;
            values.push_back(d);
        }
    }

}

// -------------------- destructor --------------------
Monitor::~Monitor() {
    for (unsigned int i = 0; i < references.size(); i++) {
        delete references[i];
    }
}

// // -------------------- write --------------------
// void Monitor::write(){
//   // write indexes
//   string sInd = "";
//   string sData= "";
//
//   std::map<int,std::vector<double> >::iterator it;
//   for (it=indexToValuesMap.begin();it!=indexToValuesMap.end();it++){
//     ostringstream ssInd;
//     ssInd << it->first;
//     sInd = sInd + ssInd.str() + " ";
//
//     ostringstream ssData;
//     for(int i=0;i<dimension;i++){
//       ssData << it->second[i];
//       sData = sData + ssData.str() + " ";
//     }
//   }
//   mappedObject->indexes(sInd);
//   mappedObject->data(sData);
// }

// -------------------- write --------------------
void Monitor::write() {
    // write indexes
    string s = "";
    for (unsigned int i = 0; i < indexes.size(); i++) {
        ostringstream ss;
        ss << indexes[i];
        s = s + ss.str() + " ";
    }
    mappedObject->indexes(s);

    // write data
    s = "";
    for (unsigned int i = 0; i < values.size(); i++) {
        ostringstream ss;
        ss << values[i];
        s = s + ss.str() + " ";
    }
    mappedObject->data(s);
}

// -------------------- getReferenceName --------------------
std::string Monitor::getReferenceName() {
    std::string s = "";
    for (unsigned int i = 0; i < references.size(); i++) {
        s += references[i]->toString();
    }
    return s;
}

// -------------------- getIndex --------------------
int Monitor::getIndex() {
    return index;
}


// -------------------- getStartAt --------------------
double Monitor::getStartAt() {
    return startAt;
}

// -------------------- getStopAt --------------------
double Monitor::getStopAt() {
    return stopAt;
}

// -------------------- getTargetName --------------------
std::string Monitor::getTargetName() {
    return target;
}

// -------------------- getValueType --------------------
Monitor::type Monitor::getValueType() {
    return valueType;
}

// -------------------- getValuesOfIndex --------------------
bool Monitor::getValuesOfIndex(int i, double values[]) {
    std::map<int, std::vector<double> >::iterator it;
    it = indexToValuesMap.find(i);
    if (it != indexToValuesMap.end()) {
        for (int j = 0; j < dimension; j++) {
            values[j] = it->second[j];
        }
        return true;
    }
    return false;
}

// -------------------- getIndex --------------------
int Monitor::getIndexOfValues(const unsigned int i) {
    if (i < indexes.size()) {
        return indexes[i];
    }
    else {
        return -1;
    }
}

// -------------------- getValue --------------------
double Monitor::getValue(const unsigned int i) {
    if (i < values.size()) {
        return values[i];
    }
    else {
        return 0.0;
    }
}

// -------------------- getNumberOfIndex --------------------
unsigned int Monitor::getNumberOfIndex() {
    if (indexes.size() > 0) {
        return indexes.size();
    }
    else {
        return indexToValuesMap.size();
    }
}

// -------------------- getNumberOfValues --------------------
unsigned int Monitor::getNumberOfValues() {
    return values.size();
}