GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/leaf-node-arrow.cpp Lines: 1 84 1.2 %
Date: 2020-05-14 11:23:33 Branches: 2 106 1.9 %

Line Branch Exec Source
1
//
2
//  leaf-node-arrow.cpp
3
//  gepetto-viewer
4
//
5
//  Created by Pierre Fernbach in june 2015.
6
//  Copyright (c) 2015 LAAS-CNRS. All rights reserved.
7
//
8
9
#include <gepetto/viewer/leaf-node-arrow.h>
10
#include <osg/Version>
11
12
namespace gepetto {
13
namespace viewer {
14
15
    /* Declaration of private function members */
16
17
    void LeafNodeArrow::init ()
18
    {
19
        /* Create Geode for adding ShapeDrawable */
20
        geode_ptr_ = new osg::Geode();
21
22
        resetGeodeContent ();
23
24
        /* Create PositionAttitudeTransform */
25
        this->asQueue()->addChild(geode_ptr_);
26
27
        /* Allow transparency */
28
        geode_ptr_->getOrCreateStateSet()->setMode(GL_BLEND, ::osg::StateAttribute::ON);;
29
30
        addProperty(FloatProperty::create("Radius",
31
              FloatProperty::getterFromMemberFunction(this, &LeafNodeArrow::getRadius),
32
              FloatProperty::setterFromMemberFunction(this, &LeafNodeArrow::setRadius)));
33
        addProperty(FloatProperty::create("Size",
34
              FloatProperty::getterFromMemberFunction(this, &LeafNodeArrow::getSize),
35
              FloatProperty::setterFromMemberFunction(this, &LeafNodeArrow::setSize)));
36
    }
37
38
    void LeafNodeArrow::resetGeodeContent ()
39
    {
40
        if (cylinder_drawable_) geode_ptr_->removeDrawable(cylinder_drawable_);
41
        if (cone_drawable_    ) geode_ptr_->removeDrawable(cone_drawable_);
42
43
         /* create the axis : */
44
        float radiusCyl = getRadius();
45
        float lengthCyl = getSize();
46
47
        /* Create cylinder */
48
        ::osg::CylinderRefPtr cylinder_shape_x_ptr = new ::osg::Cylinder();
49
        cylinder_shape_x_ptr->set(osgVector3(lengthCyl/2.f,0.f,0.f) , radiusCyl ,lengthCyl);
50
        cylinder_shape_x_ptr->setRotation(osgQuat( 0.f , ::osg::X_AXIS , M_PI_2 , ::osg::Y_AXIS , 0.f , ::osg::Z_AXIS ));
51
        /* Create cone */
52
        ::osg::ConeRefPtr cone_shape_x_ptr = new ::osg::Cone();
53
        cone_shape_x_ptr->set(osgVector3(lengthCyl+getRadius(),0.f,0.f) , 2.f * radiusCyl , 4.f * radiusCyl );
54
        cone_shape_x_ptr->setRotation(osgQuat( 0.f , ::osg::X_AXIS , M_PI_2 , ::osg::Y_AXIS , 0.f , ::osg::Z_AXIS ));
55
56
        /* create drawable and add them to geode */
57
        cylinder_drawable_ = new ::osg::ShapeDrawable( cylinder_shape_x_ptr );
58
        cone_drawable_ = new ::osg::ShapeDrawable( cone_shape_x_ptr );
59
        setColor (color_);
60
61
        geode_ptr_->addDrawable(cylinder_drawable_);
62
        geode_ptr_->addDrawable(cone_drawable_);
63
    }
64
65
    LeafNodeArrow::LeafNodeArrow(const std::string& name, const osgVector4& color, float radius, float size):
66
        NodeDrawable(name),
67
        color_(color)
68
    {
69
        setRadius(radius);
70
        setSize(size);
71
72
        init();
73
    }
74
75
    LeafNodeArrow::LeafNodeArrow(const LeafNodeArrow& other) :
76
        NodeDrawable(other.getID())
77
    {
78
        init();
79
      //TODO
80
    }
81
82
    void LeafNodeArrow::initWeakPtr(LeafNodeArrowWeakPtr other_weak_ptr)
83
    {
84
        weak_ptr_ = other_weak_ptr;
85
    }
86
87
    /* End of declaration of private function members */
88
89
    /* Declaration of protected function members */
90
91
    LeafNodeArrowPtr_t LeafNodeArrow::create (const std::string& name , const osgVector4& color ,float radiusCenter, float size)
92
    {
93
        LeafNodeArrowPtr_t shared_ptr(new LeafNodeArrow(name, color, radiusCenter,size));
94
95
        // Add reference to itself
96
        shared_ptr->initWeakPtr(shared_ptr);
97
98
        return shared_ptr;
99
    }
100
101
    LeafNodeArrowPtr_t LeafNodeArrow::create (const std::string& name, const osgVector4& color, float radiusCenter)
102
    {
103
        //const float& size = new float;
104
        //*size = 0;
105
        LeafNodeArrowPtr_t shared_ptr(new LeafNodeArrow(name, color, radiusCenter,0));
106
107
        // Add reference to itself
108
        shared_ptr->initWeakPtr(shared_ptr);
109
110
        return shared_ptr;
111
    }
112
113
    LeafNodeArrowPtr_t LeafNodeArrow::createCopy (LeafNodeArrowPtr_t other)
114
    {
115
        LeafNodeArrowPtr_t shared_ptr(new LeafNodeArrow(*other));
116
117
        // Add reference to itself
118
        shared_ptr->initWeakPtr(shared_ptr);
119
120
        return shared_ptr;
121
    }
122
123
    /* End of declaration of protected function members */
124
125
    /* Declaration of public function members */
126
127
    LeafNodeArrowPtr_t LeafNodeArrow::clone (void) const
128
    {
129
        return LeafNodeArrow::createCopy(weak_ptr_.lock());
130
    }
131
132
    LeafNodeArrowPtr_t LeafNodeArrow::self (void) const
133
    {
134
        return weak_ptr_.lock();
135
    }
136
137
    void LeafNodeArrow::setRadius (const float& radius)
138
    {
139
        radius_=radius;
140
    }
141
142
    float LeafNodeArrow::getRadius() const
143
    {
144
        return radius_;
145
    }
146
147
    void LeafNodeArrow::setSize(const float& size){
148
        size_ = size;
149
    }
150
151
    float LeafNodeArrow::getSize() const
152
    {
153
        return size_;
154
    }
155
156
    void LeafNodeArrow::setColor (const osgVector4& color)
157
    {
158
        cylinder_drawable_->setColor(color);
159
        cone_drawable_->setColor(color);
160
#if OSG_VERSION_GREATER_OR_EQUAL(3, 5, 6)
161
        cylinder_drawable_->build();
162
        cone_drawable_->build();
163
#else
164
        cylinder_drawable_->dirtyDisplayList();
165
        cone_drawable_->dirtyDisplayList();
166
        cylinder_drawable_->dirtyBound();
167
        cone_drawable_->dirtyBound();
168
#endif
169
        color_ = color;
170
        setTransparentRenderingBin (color[3] < Node::TransparencyRenderingBinThreshold);
171
        setDirty();
172
    }
173
174
    void LeafNodeArrow::resize(float radius,float length)
175
    {
176
        if(length != getSize() || radius != getRadius())
177
        { // avoid useless resize
178
            setSize(length);
179
            setRadius(radius);
180
181
            resetGeodeContent ();
182
        }
183
    }
184
185
    LeafNodeArrow::~LeafNodeArrow ()
186
    {
187
        /* Proper deletion of all tree scene */
188
189
        geode_ptr_->removeDrawable(cylinder_drawable_);
190
        geode_ptr_->removeDrawable(cone_drawable_);
191
192
        geode_ptr_ = NULL;
193
        cylinder_drawable_ = NULL;
194
        cone_drawable_ = NULL;
195
196
        weak_ptr_.reset();
197
    }
198
199
    /* End of declaration of public function members */
200
201
} /* namespace viewer */
202

3
} /* namespace gepetto */
203