Geometry.cpp 25 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
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2012 UJF-Grenoble 1, 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$
 ****************************************************************************/
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#include "Geometry.h"
#include "Log.h"

//-- vtk stuff
#include <vtkUnstructuredGrid.h>
#include <vtkStructuredGrid.h>
#include <vtkDataSetMapper.h>
#include <vtkDataSet.h>
#include <vtkActor.h>
#include <vtkTexture.h>
#include <vtkGlyph3D.h>
#include <vtkTransform.h>
#include <vtkTubeFilter.h>
#include <vtkCastToConcrete.h>
#include <vtkProperty.h>
#include <vtkCallbackCommand.h>
#include <vtkTextProperty.h>
#include <vtkSphereSource.h>
#include <vtkAlgorithmOutput.h>
#include <vtkPointData.h>
#include <vtkTextMapper.h>
#include <vtkActor2D.h>
48
#include <vtkViewport.h>
49
#include <vtkMath.h>
50
51
52
53
54

namespace camitk {
//------------------------------- constructor ----------------------------------------
Geometry::Geometry(QString label, vtkSmartPointer<vtkPointSet> pointSetPtr, const RenderingModes renderingModes) {

55
56
57
58
59
60
61
62
63
    this->label = label;

    // the Geometry takes control of the vtkPointSet
    // We need to use a deepcopy because of some strange behaviour of mingw
    // (In mingw variable seems to become out of scope when going from one DLL to another one,
    // which generates a loss of smartness in the vtkSmartPointer)
    // TODO use a MeshUtil class for typeOf?
    if (pointSetPtr->IsA("vtkPolyData")) {
        this->pointSet = vtkSmartPointer<vtkPolyData>::New();
64
65
    }
    else
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
        if (pointSetPtr->IsA("vtkUnstructuredGrid")) {
            this->pointSet = vtkSmartPointer<vtkUnstructuredGrid>::New();
        }
        else
            if (pointSetPtr->IsA("vtkStructuredGrid")) {
                this->pointSet = vtkSmartPointer<vtkStructuredGrid>::New();
            }
    // Disconnect the previous Vtk Pipeline without memcopy
    // (equivalent of ITK DisconnectPipeline()...)
    // Note : this (i.e disconnect/deepcopy/shallowcopy) should not have to be done
    // in the components...
    this->pointSet->ShallowCopy(pointSetPtr);

    this->renderingModes = renderingModes;

    // vtk stuff
    // By default, nothing is created.
    // The vtkThings are only created when required (lazy initialization design-pattern)
    surfaceActor = NULL;
    wireframeActor = NULL;
    pointsActor = NULL;
    tube = NULL;
    texture = NULL;

    // display options
    enhancedModes = InterfaceGeometry::Normal;

    //-- init color
    for (unsigned int i = 0; i < 3;i++)
        surfaceColor[i] = 0.8;
    surfaceColor[3] = 1.0;

    wireframeColor[0] = 1.0;
    wireframeColor[1] = 0.0;
    wireframeColor[2] = 0.0;
    wireframeColor[3] = 1.0;

    pointsColor[0] = 0.0;
    pointsColor[1] = 0.0;
    pointsColor[2] = 1.0;
    pointsColor[3] = 1.0;

    alphaShaded = 0.2;

    //-- init generic filter and dataOutput and customPipelineOuput
    concreteData = vtkSmartPointer<vtkCastToConcrete>::New();
    concreteData->SetInput(this->pointSet);
    dataOutput = concreteData->GetOutputPort();
    customPipelineOutput = dataOutput;

    //-- init mapper
    mapper = vtkSmartPointer<vtkDataSetMapper>::New();
    mapper->SetInputConnection(customPipelineOutput);
    mapper->ImmediateModeRenderingOn();
    mapper->SetResolveCoincidentTopologyToPolygonOffset();
121
122
123
124

    //-- update glyph type size
    setGlyphType(InterfaceGeometry::Sphere);

125
126
127
128
}

//------------------------------- destructor ----------------------------------------
Geometry::~Geometry() {
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    // no memory was allocated, thanks to vtkSmartPointers...

    // just remove the extra prop from the visualization
    // warning: this has to be duplicate of code from the removeProp(..) method
    // as the removal is needed here in the middle of the loop.
    QMutableMapIterator<QString, vtkSmartPointer<vtkProp> > i(extraProp);
    while (i.hasNext()) {
        i.next();
        // remove the prop from any renderer/consummers
        vtkSmartPointer<vtkProp> prop = extraProp.value(i.key());
        prop->VisibilityOff();
        for (int ic = 0;ic < prop->GetNumberOfConsumers();ic++) {
            vtkViewport *viewer = vtkViewport::SafeDownCast(prop->GetConsumer(ic));
            if (viewer)
                viewer->RemoveViewProp(prop);
        }
        // remove it from the maps
        i.remove();
    }
148

149
150
151
152
}

//------------------------------- setPointSet ----------------------------------------
void Geometry::setPointSet(vtkSmartPointer<vtkPointSet> ps) {
153
154
155
156
157
158
159
160
    if (pointSet)
        // Disconnect the previous Vtk Pipeline without memcopy
        // (equivalent of ITK DisconnectPipeline()...)
        // Note : this (i.e disconnect/deepcopy/shallowcopy) should not have to be done
        // in the components...
        pointSet->ShallowCopy(ps);
    else
        pointSet = ps;
161
162
163

    //-- update glyph type size
    setGlyphType(InterfaceGeometry::Sphere);
164
165
166
167
168
}


//------------------------------- setDataConnection ----------------------------------------
void Geometry::setDataConnection(vtkSmartPointer<vtkAlgorithmOutput> outputPort) {
169
170
171
172
    if (outputPort == NULL)
        customPipelineOutput = dataOutput;
    else
        customPipelineOutput = outputPort;
173

174
175
176
177
178
    if (tube) {
        tube->SetInputConnection(customPipelineOutput);
    }
    else
        mapper->SetInputConnection(customPipelineOutput);
179
180
181
182
183
184

}


//------------------------------- getActor ----------------------------------------
vtkSmartPointer<vtkActor> Geometry::getActor(RenderingModes mode) {
185
186
    if (enhancedModes & InterfaceGeometry::Hidden)
        return NULL;
187

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    if ((renderingModes  & InterfaceGeometry::Surface) && (mode & InterfaceGeometry::Surface)) {
        if (!surfaceActor) {
            // This actor does not exist yet; create it.
            surfaceActor = vtkSmartPointer<vtkActor>::New();
            surfaceActor->SetMapper(mapper);
            setActorColor(InterfaceGeometry::Surface, surfaceColor);
            if (texture)
                surfaceActor->SetTexture(texture);

            // surface light
            surfaceActor->GetProperty()->SetInterpolationToPhong();
            surfaceActor->GetProperty()->SetDiffuse(.9);
            surfaceActor->GetProperty()->SetSpecular(.1);
            surfaceActor->GetProperty()->SetAmbient(.1);
            surfaceActor->GetProperty()->SetSpecularColor(.4, .4, .4);
            surfaceActor->GetProperty()->SetSpecularPower(100);

            // picking
            surfaceActor->PickableOn();
        }
        return surfaceActor;
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
    else
        if ((renderingModes & InterfaceGeometry::Wireframe) && (mode & InterfaceGeometry::Wireframe)) {
            if (!wireframeActor) {
                // This actor doesn't exist yet; create it.
                wireframeActor = vtkSmartPointer<vtkActor>::New();
                wireframeActor->SetMapper(mapper);
                wireframeActor->GetProperty()->SetRepresentationToWireframe();
                wireframeActor->GetProperty()->SetLineWidth(1.0);
                setActorColor(InterfaceGeometry::Wireframe, wireframeColor);

                // picking
                wireframeActor->PickableOn();
            }
            return wireframeActor;
        }
        else
            if ((renderingModes & InterfaceGeometry::Points) && (mode & InterfaceGeometry::Points)) {
                if (!pointsActor) {
                    // This actor doesn't exist yet; create it.
                    pointsActor = vtkSmartPointer<vtkActor>::New();
                    pointsActor->SetMapper(mapper);
                    pointsActor->GetProperty()->SetRepresentationToPoints();
                    setActorColor(InterfaceGeometry::Points, pointsColor);

                    // picking
                    pointsActor->PickableOn();
                }
                return pointsActor;
            }
            else
                return NULL;
}

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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

//------------------------------- buildGlyph ----------------------------------------
void Geometry::buildGlyph(const GlyphTypes type) {
    if (!extraProp.contains("glyph")) {
        // the glyph attached to this Geometry
        vtkSmartPointer<vtkGlyph3D> glyph3D = vtkSmartPointer<vtkGlyph3D>::New();

        // the glyph geometry
        vtkSmartPointer<vtkPolyDataAlgorithm> glyphGeom;
        switch (type) {
        case Sphere: {
            // create sphere
            vtkSmartPointer<vtkSphereSource> sphereGeom = vtkSmartPointer<vtkSphereSource>::New();
            sphereGeom->SetRadius(glyphSize);
            glyphGeom = sphereGeom;
            break;
        }
        default:
            break;
        }
        // connect the glyph
        glyphGeom->Update();
        glyph3D->SetSourceConnection(glyphGeom->GetOutputPort());

        // check tubing pipeline
        if (tube)
            glyph3D->SetInputConnection(tube->GetOutputPort());
        else
            glyph3D->SetInputConnection(customPipelineOutput);

        // create a mapper and actor for the glyph
        vtkSmartPointer<vtkDataSetMapper> glyphActorMapper = vtkSmartPointer<vtkDataSetMapper>::New();
        glyphActorMapper->SetInputConnection(glyph3D->GetOutputPort());
        vtkSmartPointer<vtkActor> glyphActor = vtkSmartPointer<vtkActor>::New();
        glyphActor->SetMapper(glyphActorMapper);

        // compute color
        double rgb[4], hsv[3];
        getActorColor(Surface, rgb);
        // if the color is not defined (i.e. black) or gray -> full red
        if ((rgb[0] == 0.0 && rgb[1] == 0.0 && rgb[2] == 0.0) || (rgb[0] == rgb[1] && rgb[1] == rgb[2]))
            rgb[0] = 1.0;
        else {
            // compute complementary color
            vtkMath::RGBToHSV(rgb, hsv);
            hsv[0] = int(hsv[0] + 180.0) % 360;
            double newV = hsv[2]*(hsv[1]-1.0) + 1.0;
            hsv[1] = hsv[1]*hsv[2]/newV;
            hsv[2] = newV;
            vtkMath::HSVToRGB(hsv, rgb);
            // reduce intensity (too bright is not nice)
            for (unsigned int i = 0;i < 3;i++) {
                if (rgb[i] >= 1.0)
                    rgb[i] = 1.0;
                if (rgb[i] >= 0.9)
                    rgb[i] *= .8;
            }
        }
        glyphActor->GetProperty()->SetColor(rgb);
        glyphActor->SetVisibility(false);
        // insert the glyph in the visualization pipeline, as an extraprop
        addProp("glyph", glyphActor);
305
    }
306
307
}

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//------------------------------- buildLabel ----------------------------------------
void Geometry::buildLabel() {
    if (!extraProp.contains("label")) {
        vtkSmartPointer<vtkTextProperty> tprop = vtkSmartPointer<vtkTextProperty>::New();
        tprop->SetFontSize(14);
        tprop->SetFontFamilyToArial();
        tprop->SetJustificationToLeft();
        tprop->BoldOn();
        tprop->ItalicOn();
        tprop->ShadowOn();

        labelActorMapper = vtkSmartPointer<vtkTextMapper>::New();
        labelActorMapper->SetTextProperty(tprop);

        vtkSmartPointer<vtkActor2D> labelActor = vtkSmartPointer<vtkActor2D>::New();
        labelActor->GetPositionCoordinate()->SetCoordinateSystemToWorld();
        labelActor->SetMapper(labelActorMapper);
        labelActor->SetVisibility(false);
        addProp("label", labelActor);
    }
328

329
330
331
332
333
334
    updateLabel();
}

//------------------------------- updateLabel ----------------------------------------
void Geometry::updateLabel() {
    if (extraProp.contains("label")) {
335
336
337
338
339
340
341
342
343
344
345
346
        //-- update the text
        labelActorMapper->SetInput(label.toStdString().c_str());

        //-- update the position
        double position[3] = {0.0, 0.0, 0.0};

        // compute average position of all the points
        for (int i = 0;i < pointSet->GetNumberOfPoints(); i++) {
            double pos[3];
            pointSet->GetPoint(i, pos);
            for (unsigned j = 0;j < 3;j++)
                position[j] += pos[j];
347
        }
348
349
350
        for (unsigned j = 0;j < 3;j++)
            position[j] /= pointSet->GetNumberOfPoints();

351
        position[1] += glyphSize;
352
353
354

        vtkActor2D::SafeDownCast(extraProp.value("label"))->GetPositionCoordinate()->SetValue(position[0], position[1], position[2]);
    }
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
}


//------------------------------- addProp ----------------------------------------
bool Geometry::addProp(const QString& name, vtkSmartPointer< vtkProp > prop ) {
    if (!extraProp.contains(name)) {
        extraProp.insert(name, prop);
        return true;
    }
    else
        return false;
}

//------------------------------- getProp ----------------------------------------
vtkSmartPointer< vtkProp > Geometry::getProp(const QString& name) {
    // check for extra prop lazy instanciation
    if (name == "glyph")
        buildGlyph(Sphere);
    else
        if (name == "label")
            buildLabel();

    if (extraProp.contains(name)) {
378
379
380
        return extraProp.value(name);
    }
    else
381
382
383
        return NULL;
}

384
385
386
387
//------------------------------- getNumberOfProp ----------------------------------------
unsigned int Geometry::getNumberOfProp() const {
    return extraProp.values().size();
}
388

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//------------------------------- getProp ----------------------------------------
vtkSmartPointer< vtkProp > Geometry::getProp(unsigned int index) {
    return extraProp.values().at(index);
}

//------------------------------- removeProp ----------------------------------------
bool Geometry::removeProp(const QString& name) {
    if (extraProp.contains(name)) {
        // remove the prop from any renderer/consummers
        vtkSmartPointer<vtkProp> prop = extraProp.value(name);
        prop->VisibilityOff();
        for (int i = 0;i < prop->GetNumberOfConsumers();i++) {
            vtkViewport *viewer = vtkViewport::SafeDownCast(prop->GetConsumer(i));
            if (viewer)
                viewer->RemoveViewProp(prop);
        }
        // remove it from the maps
        extraProp.remove(name);
        return true;
    }
    else
        return false;
411
412
413
414
}

//------------------------------- setPointPosition ----------------------------------------
void Geometry::setPointPosition(const unsigned int orderNumber, const double x, const double y, const double z) {
415
416
417
    pointSet->GetPoints()->SetPoint(orderNumber, x, y, z);
    pointSet->Modified();
    pointSet->Update();
418
419
420
421
}

//------------------------------- setActorColor ----------------------------------------
void Geometry::setActorColor(const RenderingModes mode, double color[4]) {
422
423
424
425
426
427
428
    if (mode & InterfaceGeometry::Surface) {
        for (unsigned int i = 0;i < 4; i++)
            surfaceColor[i] = color[i];
        if (surfaceActor) {
            surfaceActor->GetProperty()->SetColor(color);
            surfaceActor->GetProperty()->SetOpacity(color[3]);
        }
429
    }
430
431
432
433
434
435
436
437
438

    if (mode & InterfaceGeometry::Wireframe) {
        for (unsigned int i = 0;i < 4; i++)
            wireframeColor[i] = color[i];
        if (wireframeActor) {
            wireframeActor->GetProperty()->SetColor(color);
            wireframeActor->GetProperty()->SetEdgeColor(color);
            wireframeActor->GetProperty()->SetOpacity(color[3]);
        }
439
    }
440
441
442
443
444
445
446
447

    if (mode & InterfaceGeometry::Points) {
        for (unsigned int i = 0;i < 4; i++)
            pointsColor[i] = color[i];
        if (pointsActor) {
            pointsActor->GetProperty()->SetColor(color);
            pointsActor->GetProperty()->SetOpacity(color[3]);
        }
448
449
450
    }
}

451
//------------------------------- setActorColor ----------------------------------------
452
void Geometry::setActorColor(const RenderingModes mode, const double r, const double g, const double b) {
453
454
455
456
457
458
    double color[4];
    color[0] = r;
    color[1] = g;
    color[2] = b;
    color[3] = getActorOpacity(mode);
    setActorColor(mode, color);
459
460
461
462
}

//------------------------------- getActorColor ----------------------------------------
void Geometry::getActorColor(const RenderingModes mode, double color[4]) {
463
464
465
    if (mode & InterfaceGeometry::Surface) {
        for (unsigned int i = 0;i < 4;i++)
            color[i] = surfaceColor[i];
466
467
    }
    else
468
469
470
471
472
473
474
475
476
477
478
479
480
        if (mode & InterfaceGeometry::Wireframe) {
            for (unsigned int i = 0;i < 4;i++)
                color[i] = wireframeColor[i];
        }
        else
            if (mode & InterfaceGeometry::Points) {
                for (unsigned int i = 0;i < 4;i++)
                    color[i] = pointsColor[i];
            }
            else {
                for (unsigned int i = 0;i < 4;i++)
                    color[i] = 0.0;
            }
481
482
483
484
}

//------------------------------- setColor ----------------------------------------
void Geometry::setColor(const double r, const double g, const double b) {
485
    setActorColor(InterfaceGeometry::Surface | InterfaceGeometry::Wireframe | InterfaceGeometry::Points, r, g, b);
486
487
488
489
}

//------------------------------- setColor ----------------------------------------
void Geometry::setColor(const double r, const double g, const double b, const double a) {
490
491
492
493
494
    double color[4];
    color[0] = r;
    color[1] = g;
    color[2] = b;
    color[3] = a;
495

496
    setActorColor(InterfaceGeometry::Surface | InterfaceGeometry::Wireframe | InterfaceGeometry::Points, color);
497
498
499
500
}

//------------------------------- setActorOpacity ----------------------------------------
void Geometry::setActorOpacity(const RenderingModes mode, const double alpha) {
501
502
503
504
505
506
507
508
    // opacity value must be bounded between 0 and 1
    double opacity = alpha;

    if (alpha < 0.0)
        opacity = 0.0;
    else
        if (alpha > 1.0)
            opacity = 1.0;
509

510
511
512
513
514
    if (mode & InterfaceGeometry::Surface) {
        surfaceColor[3] = opacity;
        if (surfaceActor)
            surfaceActor->GetProperty()->SetOpacity(opacity);
    }
515

516
517
518
519
520
    if (mode & InterfaceGeometry::Wireframe) {
        wireframeColor[3] = opacity;
        if (wireframeActor)
            wireframeActor->GetProperty()->SetOpacity(opacity);
    }
521

522
523
524
525
526
    if (mode & InterfaceGeometry::Points) {
        pointsColor[3] = opacity;
        if (pointsActor)
            pointsActor->GetProperty()->SetOpacity(opacity);
    }
527

528
    pointSet->Modified(); // force an update on the vtk pipeline
529
530
531
532
533

}

//------------------------------- getActorOpacity ----------------------------------------
double Geometry::getActorOpacity(const RenderingModes mode) const {
534
535
    if (mode & InterfaceGeometry::Surface) {
        return surfaceColor[3];
536
537
    }
    else
538
539
540
541
542
543
544
545
546
        if (mode & InterfaceGeometry::Wireframe) {
            return wireframeColor[3];
        }
        else
            if (mode & InterfaceGeometry::Points) {
                return pointsColor[3];
            }
            else
                return 0.0;
547
548
549
550
}

//------------------------------- setOpacity ----------------------------------------
void Geometry::setOpacity(const double alpha) {
551
    setActorOpacity(InterfaceGeometry::Surface | InterfaceGeometry::Wireframe | InterfaceGeometry::Points, alpha);
552
553
554
555
}

//------------------------------- setTexture ----------------------------------------
void Geometry::setTexture(vtkSmartPointer<vtkTexture> texture) {
556
557
    this->texture = texture;
    mapper->SetScalarVisibility(0);
558

559
560
561
    // texture is applied only on surface actor, not wireframe nor points one
    if (surfaceActor)
        surfaceActor->SetTexture(texture);
562
563
}

564
565
// -------------------- setGlyphType --------------------
void Geometry::setGlyphType(const GlyphTypes type, const double size) {
566

567
568
    // update size
    if (type == NoGlyph) {
569
        getProp("glyph")->VisibilityOff();
570
571
572
        // update size to 0
        glyphSize = 0.0;
        // nothing else to do!
573
    }
574
    else {
575
576
        // check size
        if (size == 0.0) {
577
578
            // glyph is a not NoGlyph and size is zero, use a default value
            glyphSize = getBoundingRadius() / 100.0;
579
        }
580
581
        else {
            glyphSize = size; // can not be 0.0
582
        }
583
584
        // build the new glyph (get it ready)
        buildGlyph(type);
585
586
587
588
589
590
    }

}

// -------------------- setEnhancedModes --------------------
void Geometry::setEnhancedModes(const EnhancedModes emode) {
591
592
593
594
595
596
597
598
599
600
601
602
    // set hightlighted
    if (!(enhancedModes & InterfaceGeometry::Highlighted) && (emode & InterfaceGeometry::Highlighted)) {
        // backup opacity
        oldAlphaSurface = surfaceColor[3];
        oldAlphaWireframe = wireframeColor[3];
        oldAlphaPoints = pointsColor[3];

        // Force opacity: set actors completely opaque (opacity = 1.0)
        setOpacity(1.0);

        // display the object in wireframe as well
        renderingModes |= InterfaceGeometry::Wireframe;
603
604
    }

605
606
607
608
609
610
611
612
613
614
615
    // unset hightlighted
    if ((enhancedModes & InterfaceGeometry::Highlighted) && !(emode & InterfaceGeometry::Highlighted)) {
        // restore the original alpha values
        setActorOpacity(InterfaceGeometry::Surface, oldAlphaSurface);
        setActorOpacity(InterfaceGeometry::Wireframe, oldAlphaWireframe);
        setActorOpacity(InterfaceGeometry::Points, oldAlphaPoints);

        // if before the highlight, the wireframe isn't visible
        //      we restore the last display
        renderingModes ^= InterfaceGeometry::Wireframe;
    }
616

617
618
619
620
621
622
623
624
625
    // set shaded
    if (!(enhancedModes & InterfaceGeometry::Shaded) && (emode & InterfaceGeometry::Shaded)) {
        // If this object is highlighted, unhighlight it
        if (enhancedModes & InterfaceGeometry::Highlighted) {
            setActorOpacity(InterfaceGeometry::Surface, oldAlphaSurface);
            setActorOpacity(InterfaceGeometry::Wireframe, oldAlphaWireframe);
            setActorOpacity(InterfaceGeometry::Points, oldAlphaPoints);
            renderingModes ^= InterfaceGeometry::Wireframe;
        }
626

627
628
629
630
        // backup opacity
        oldAlphaSurface = surfaceColor[3];
        oldAlphaWireframe = wireframeColor[3];
        oldAlphaPoints = pointsColor[3];
631

632
633
634
635
636
637
638
639
640
641
642
643
        // Force opacity: set actors to be relatively transparent
        setOpacity(alphaShaded);
    }

    if ((enhancedModes & InterfaceGeometry::Shaded) && !(emode & InterfaceGeometry::Shaded)) {
        setActorOpacity(InterfaceGeometry::Surface, oldAlphaSurface);
        setActorOpacity(InterfaceGeometry::Wireframe, oldAlphaWireframe);
        setActorOpacity(InterfaceGeometry::Points, oldAlphaPoints);
    }

    // update the state
    enhancedModes = emode;
644
645
646
647
}

// -------------------- setLinesAsTubes --------------------
void Geometry::setLinesAsTubes(bool isTubes) {
648
649
650
651
652
653
654
655
656
657
658
659
660
    // only works on polydata that contains only lines
    vtkSmartPointer<vtkPolyData> polyData = vtkPolyData::SafeDownCast(pointSet);

    if (polyData && isTubes && !tube) {
        // check if the tube filter is really needed!
        if (polyData->GetNumberOfVerts() == 0 && polyData->GetNumberOfLines() > 0 && polyData->GetNumberOfPolys() == 0 && polyData->GetNumberOfStrips() == 0) {
            //-- create the tube filter
            tube = vtkSmartPointer<vtkTubeFilter>::New();
            tube->SetVaryRadiusToVaryRadiusOff();
            tube->SetRadius(pointSet->GetLength() / 40);  //maybe radius proportions should be defined better
            tube->SetNumberOfSides(5);
            //-- update the pipeline
            tube->SetInputConnection(customPipelineOutput);
661
            mapper->SetInputConnection(tube->GetOutputPort());
662
663
664
        }
    }
    else {
665
        mapper->SetInputConnection(customPipelineOutput);
666
        tube = NULL;
667
668
669
670
671
672
    }

}

// -------------------- getBounds --------------------
void Geometry::getBounds(double bounds[6]) {
673
    pointSet->GetBounds(bounds);
674
675
676
677
}

// -------------------- getBoundingRadius --------------------
double Geometry::getBoundingRadius() {
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
    double radius = 0.0;
    if (pointSet->GetNumberOfPoints() == 1) {
        //-- update the position
        double pos[3] = {0.0, 0.0, 0.0};
        pointSet->GetPoint(0, pos);
        // radius = position
        radius = sqrt(pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2]);
    }
    else {
        double bb[6];
        getBounds(bb);
        double xLength, yLength, zLength;
        xLength = fabs(bb[1] - bb[0]);
        yLength = fabs(bb[3] - bb[2]);
        zLength = fabs(bb[5] - bb[4]);
        radius = sqrt(xLength * xLength + yLength * yLength + zLength * zLength) / 2.0;
    }
    return radius;
696
697
698
699
}

// -------------------- setPointData --------------------
void Geometry::setPointData(vtkSmartPointer<vtkDataArray> da) {
700
    pointSet->GetPointData()->SetScalars(da);
701
702
703
704
705
706
707
708
709
710
    if (da) {
        pointSet->GetPointData()->SetActiveScalars(da->GetName());
        // rescale colors
        mapper->SetScalarVisibility(1);
        double range[2] = {0,1};
        pointSet->GetPointData()->GetScalars()->GetRange(range);
        mapper->SetScalarRange(range);
    }
    // make sure the change is taking into account
    pointSet->Modified();
711
    pointSet->Update();
712
    mapper->Update();
713
714
715
716
}

}

717
718