abstractpipeline.cpp 6.8 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
5
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
8
9
//      Chair for Computer Aided Medical Procedures
//      Technische Universitt Mnchen
//      Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
schultezub's avatar
schultezub committed
10
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

30
#include "abstractpipeline.h"
schultezub's avatar
schultezub committed
31

schultezub's avatar
schultezub committed
32
#include "tgt/exception.h"
schultezub's avatar
schultezub committed
33
#include "core/pipeline/abstractprocessor.h"
34
35
#include "core/tools/job.h"
#include "core/tools/simplejobprocessor.h"
36
37
#include <ctime>

38

schultezub's avatar
schultezub committed
39
40
namespace campvis {
    const std::string AbstractPipeline::loggerCat_ = "CAMPVis.core.datastructures.AbstractPipeline";
41

42
    AbstractPipeline::AbstractPipeline() 
43
        : HasPropertyCollection()
schultezub's avatar
   
schultezub committed
44
    {
45
        _enabled = true;
schultezub's avatar
   
schultezub committed
46
47
48
49
50
    }

    AbstractPipeline::~AbstractPipeline() {
    }

51
    void AbstractPipeline::init() {
schultezub's avatar
schultezub committed
52
53
        initAllProperties();

54
        // initialize all processors:
schultezub's avatar
schultezub committed
55
56
57
        for (std::vector<AbstractProcessor*>::iterator it = _processors.begin(); it != _processors.end(); ++it) {
            try {
                (*it)->init();
schultezub's avatar
schultezub committed
58
                (*it)->s_invalidated.connect(this, &AbstractPipeline::onProcessorInvalidated);
schultezub's avatar
schultezub committed
59
60
61
62
63
            }
            catch (tgt::Exception& e) {
                LERROR("Caught Exception during initialization of processor: " << e.what());
            }
        }
64
    }
schultezub's avatar
schultezub committed
65

66
    void AbstractPipeline::deinit() {
schultezub's avatar
schultezub committed
67
68
        deinitAllProperties();

69
70
71
        // deinitialize all processors:
        for (std::vector<AbstractProcessor*>::iterator it = _processors.begin(); it != _processors.end(); ++it) {
            try {
schultezub's avatar
schultezub committed
72
                (*it)->s_invalidated.disconnect(this);
73
74
75
76
77
78
                (*it)->deinit();
            }
            catch (tgt::Exception& e) {
                LERROR("Caught Exception during deinitialization of processor: " << e.what());
            }
        }
79
80
81

        // clear DataContainer
        _data.clear();
82
83
    }

schultezub's avatar
schultezub committed
84
    void AbstractPipeline::onPropertyChanged(const AbstractProperty* prop) {
85
        HasPropertyCollection::onPropertyChanged(prop);
schultezub's avatar
schultezub committed
86
87
    }

88
    void AbstractPipeline::onProcessorInvalidated(AbstractProcessor* processor) {
89
        if (processor->getEnabled())
90
            SimpleJobProc.enqueueJob(makeJob(this, &AbstractPipeline::executeProcessor, processor, false));
schultezub's avatar
schultezub committed
91
92
93
94
95
96
    }

    const DataContainer& AbstractPipeline::getDataContainer() const {
        return _data;
    }

97
98
99
100
    DataContainer& AbstractPipeline::getDataContainer() {
        return _data;
    }

101
    void AbstractPipeline::executeProcessor(AbstractProcessor* processor, bool unlockInExtraThred) {
102
103
        tgtAssert(processor != 0, "Processor must not be 0.");

104
105
106
107
108
109
110
111
        if (processor->getEnabled() && !processor->isLocked()) {
            // update properties if they're invalid
            if (processor->hasInvalidProperties()) {
                processor->updateProperties(_data);
#if CAMPVIS_DEBUG
                if (processor->hasInvalidProperties())
                    LDEBUG("Processor " << processor->getName() << " still has INVALID_PROPERTIES level. Did you forget to validate the processor in updateProperties()?");
#endif
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
            // execute processor if needed
            if (processor->hasInvalidResult()) {
                processor->lockProcessor();
                clock_t startTime = clock();

                try {
                    processor->process(_data);
                }
                catch (std::exception& e) {
                    LERROR("Caught unhandled exception while executing processor " << processor->getName() << ": " << e.what());
                }
                catch (...) {
                    LERROR("Caught unhandled exception while executing processor " << processor->getName() << ": unknown exception");
                }

                if (processor->getClockExecutionTime()) {
                    clock_t endTime = clock();
                    LDEBUG("Executed processor " << processor->getName() << " duration: " << (endTime - startTime));
                }

                // Unlocking processors might be expensive, since a long chain of invalidations might be started
                // -> do this in another thread...
                if (unlockInExtraThred)
                    SimpleJobProc.enqueueJob(makeJob(processor, &AbstractProcessor::unlockProcessor));
                else
                    processor->unlockProcessor();
140
            }
141
        }
142
143
    }

144
145
146
147
    const std::vector<AbstractProcessor*>& AbstractPipeline::getProcessors() const {
        return _processors;
    }

148
149
150
151
152
153
154
155
    bool AbstractPipeline::getEnabled() const {
        return _enabled;
    }

    void AbstractPipeline::setEnabled(bool enabled) {
        _enabled = enabled;
    }

156
    void AbstractPipeline::addProcessor(AbstractProcessor* processor) {
schultezub's avatar
schultezub committed
157
        tgtAssert(processor != 0, "Processor must not be 0.")
158
159
        _processors.push_back(processor);
    }
160

161
162
163
164
165
166
167
168
169
170
    void AbstractPipeline::lockAllProcessors() {
        for (std::vector<AbstractProcessor*>::iterator it = _processors.begin(); it != _processors.end(); ++it)
            (*it)->lockProcessor();
    }

    void AbstractPipeline::unlockAllProcessors() {
        for (std::vector<AbstractProcessor*>::iterator it = _processors.begin(); it != _processors.end(); ++it)
            (*it)->unlockProcessor();
    }

171

172
}