campcommhdreceiver.cpp 7.66 KB
Newer Older
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
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitt Mnchen
//      Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// 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
31
#define CAMPCOM_FAST_SERIALIZATION

32
33
34
35
#include "campcommhdreceiver.h"

#include "core/datastructures/imagedata.h"
#include "core/datastructures/genericimagerepresentationlocal.h"
36
#include "core/tools/stringutils.h"
37
38
39
40
41
42
43
44
45
46
47
48
49

namespace campvis {
    const std::string CampcomMhdReceiver::loggerCat_ = "CAMPVis.modules.io.CampcomMhdReceiver";

    CampcomMhdReceiver::CampcomMhdReceiver() 
        : AbstractProcessor()
        , p_address("ServerAddress", "Server Address", "127.0.0.1")
        , p_connect("Connect", "Connect to Server", AbstractProcessor::VALID)
        , p_targetImageID("targetImageName", "Target Image ID", "CampcomMhdReceiver.output", DataNameProperty::WRITE)
        , p_imageOffset("ImageOffset", "Image Offset in mm", tgt::vec3(0.f), tgt::vec3(-10000.f), tgt::vec3(10000.f), tgt::vec3(0.1f))
        , p_voxelSize("VoxelSize", "Voxel Size in mm", tgt::vec3(1.f), tgt::vec3(-100.f), tgt::vec3(100.f), tgt::vec3(0.1f))
        , _ccclient(0)
    {
50
51
        _incomingMhd = 0;

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        addProperty(&p_address);
        addProperty(&p_targetImageID);
        addProperty(&p_connect);
        addProperty(&p_imageOffset);
        addProperty(&p_voxelSize);

    }

    CampcomMhdReceiver::~CampcomMhdReceiver() {

    }

    void CampcomMhdReceiver::init() {
        p_connect.s_clicked.connect(this, &CampcomMhdReceiver::onBtnConnectClicked);
    }

    void CampcomMhdReceiver::deinit() {
        p_connect.s_clicked.disconnect(this);
70
71
72
73
        if (_ccclient) {
            _ccclient->disconnect();
            delete _ccclient;
        }
74
75
76
77
        _ccclient = 0;
    }

    void CampcomMhdReceiver::process(DataContainer& data) {
78
79
        validate(INVALID_RESULT);

80
81
        // Get the last received MHD file:
        // Use atomic fetch and store because at the same time CAMPCom may receive another file!
82
83
84
85
        campcom::MHDImageData* mid = _incomingMhd.fetch_and_store(0);
        if (mid == 0)
            return;

86
        // Transform campcom::MHDImageData to campvis::ImageData
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
        int numChannels = 1;
        size_t dimensionality = mid->NDims;
        tgt::svec3 size(1);
        for (int i = 0; i < mid->DimSize.size(); ++i)
            size.elem[i] = mid->DimSize[i];

        WeaklyTypedPointer wtp;
        wtp._pointer = &(mid->imageData.front().front());
        wtp._numChannels = numChannels;

        std::string et = StringUtils::uppercase(mid->elementType);
        if (et == "MET_UCHAR")
            wtp._baseType = WeaklyTypedPointer::UINT8;
        else if (et == "MET_CHAR")
            wtp._baseType = WeaklyTypedPointer::INT8;
        else if (et == "MET_USHORT")
            wtp._baseType = WeaklyTypedPointer::UINT16;
        else if (et == "MET_SHORT")
            wtp._baseType = WeaklyTypedPointer::INT16;
        else if (et == "MET_UINT")
            wtp._baseType = WeaklyTypedPointer::UINT32;
        else if (et == "MET_INT")
            wtp._baseType = WeaklyTypedPointer::INT32;
        else if (et == "MET_FLOAT")
            wtp._baseType = WeaklyTypedPointer::FLOAT;
        else {
            LERROR("Error while parsing MHD header: Unsupported element type: " << et);
            return;
        }

        tgt::vec3 imageOffset(0.f);
        tgt::vec3 voxelSize(1.f);

120
        // all parsing done - lets create the image:
121
122
123
124
        ImageData* image = new ImageData(dimensionality, size, numChannels);
        ImageRepresentationLocal::create(image, wtp);
        image->setMappingInformation(ImageMappingInformation(size, imageOffset + p_imageOffset.getValue(), voxelSize * p_voxelSize.getValue()));
        data.addData(p_targetImageID.getValue(), image);
125
126
127
    }

    void CampcomMhdReceiver::onBtnConnectClicked() {
128
        // CAMPComClient does not support dis-/reconnect. So we have to delete it and recreate it.
129
130
131
132
133
134
        if (_ccclient) {
            _ccclient->disconnect();
            delete _ccclient;
            _ccclient = 0;
        }

135
        // create CAMPComClient and subscribe.
136
        _ccclient = new campcom::CAMPComClient("Campvis", campcom::Device_TestDevice, p_address.getValue());
137
        _ccclient->connect();
138

139
        if (_ccclient->isConnected()) {
140
141
142
            // use ugly boost magic to connect to member function (unfortunately CAMPCom only supports free functions...)
            campcom::DataCallback dc = boost::bind(&CampcomMhdReceiver::ccReceiveImage, this, _1);
            campcom::SuccessCallback sc = std::bind1st(std::mem_fun(&CampcomMhdReceiver::ccSuccessCallback), this);
143
144
145
146
            _ccclient->subscribe(campcom::Type_Image, dc, sc);
        }
        else {
            LWARNING("Could not connect to CAMPCom server.");
147
            delete _ccclient;
148
149
150
151
152
            _ccclient = 0;
        }
    }

    void CampcomMhdReceiver::ccReceiveImage(std::vector<campcom::Byte>& msg) {
153
        // Deserialize payload
154
155
156
        campcom::MHDImageData return_payload;
        campcom::Header header;
        campcom::TypeHandler<campcom::MHDImageData>::deserializePayload(&msg[0], msg.size(), return_payload);
157

158
159
        if (campcom::MHDImageData::isValid(return_payload)) {
            LINFO("New valid MHDImageData received! Pushing it to the DataContainer...");
160
161
162
163
164

            // putting the image into the DataContainer has to be done asynchroneously, because we
            // don't know the DataContainer here... :/
            // So copy the image one more time, but it into _incomingMhd and invalidate the processor.
            // Use atomic fetch and store because at the same time we may convert the last received image!
165
            campcom::MHDImageData* copy = new campcom::MHDImageData(return_payload);
166
167

            // delete old image not yet converted (if present)
168
169
170
171
172
173
174
175
            campcom::MHDImageData* toDelete = _incomingMhd.fetch_and_store(copy);
            delete toDelete;

            invalidate(INVALID_RESULT);
        }
        else {
            LWARNING("New MHDImageData received but it isn't valid!");
        }
176
177
178

    }

179
    void CampcomMhdReceiver::ccSuccessCallback(bool b) {
180
181
182
183
        LINFO("CAMPCom subscribe callback: " << b);
    }

}