GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/leaf-node-xyzaxis.cpp Lines: 1 130 0.8 %
Date: 2020-05-14 11:23:33 Branches: 2 238 0.8 %

Line Branch Exec Source
1
//
2
//  leaf-node-xyz.cpp
3
//  gepetto-viewer
4
//
5
//  Created by Justin Carpentier, Mathieu Geisert and Pierre Fernbach in april 2015.
6
//  Copyright (c) 2015 LAAS-CNRS. All rights reserved.
7
//
8
9
#include <gepetto/viewer/leaf-node-xyzaxis.h>
10
11
#include <../src/internal/configuration.hh>
12
13
#include <osgUtil/SmoothingVisitor>
14
#include <osgText/Text>
15
#include <osg/CullFace>
16
17
namespace gepetto {
18
namespace viewer {
19
    namespace {
20
      // if rot corresponds to identity, the arrow is along Z.
21
      inline osg::ShapeDrawableRefPtr arrowDrawable (const float& zshift,
22
          const float& lengthCyl, const float& radiusCyl,
23
          const float& lengthCon, const float& radiusCon,
24
          const osgQuat& rot)
25
      {
26
        /* Create cylinder */
27
        ::osg::CylinderRefPtr cylinder = new ::osg::Cylinder();
28
        cylinder->set(rot * osgVector3(0.,0.,zshift + lengthCyl/2.f) , radiusCyl ,lengthCyl);
29
        cylinder->setRotation(rot);
30
        /* Create cone */
31
        ::osg::ConeRefPtr cone = new ::osg::Cone();
32
        cone->set(rot * osgVector3(0.,0.,zshift + lengthCyl+lengthCon/2.f) , lengthCon , radiusCon );
33
        cone->setRotation(rot);
34
35
        osg::ref_ptr<osg::CompositeShape> shape = new osg::CompositeShape;
36
        shape->addChild(cylinder);
37
        shape->addChild(cone);
38
39
        return new ::osg::ShapeDrawable( shape );
40
      }
41
42
      osg::ref_ptr<osgText::Text> label(const char* text, osg::ref_ptr<osgText::Font> font, const osgVector3& pos, const float& charSize)
43
      {
44
            osgVector4 black = osgVector4(0.f, 0.f, 0.f, 1.f);
45
            osg::ref_ptr<osgText::Text> l = new osgText::Text;
46
            l->setText(text);
47
            l->setFont(font);
48
            l->setCharacterSize(charSize);
49
            l->setCharacterSizeMode(osgText::Text::SCREEN_COORDS);
50
            l->setFontResolution(30,30);
51
            l->setAlignment(osgText::Text::LEFT_CENTER);
52
            l->setAxisAlignment(osgText::Text::SCREEN);
53
            l->setColor(black);
54
            l->setPosition(pos);
55
            return l;
56
      }
57
58
      std::string labelGetText(const osgText::TextBase* t) { return t->getText().createUTF8EncodedString(); }
59
60
      inline PropertyPtr_t axisColor(const std::string t, osg::ShapeDrawable* arrow)
61
      {
62
        return Vector4Property::create(t + "AxisColor",
63
                    Vector4Property::getterFromMemberFunction(arrow, &osg::ShapeDrawable::getColor),
64
                    Vector4Property::setterFromMemberFunction(arrow, &osg::ShapeDrawable::setColor));
65
      }
66
67
      inline PropertyPtr_t labelText(const std::string t, osgText::TextBase* text)
68
      {
69
        return StringProperty::create(t + "LabelText",
70
              StringProperty::Getter_t(boost::bind(labelGetText, text)),
71
              StringProperty::setterFromMemberFunction(text, &osgText::TextBase::setText));
72
      }
73
74
      inline PropertyPtr_t labelSize(const std::string t, osgText::TextBase* text)
75
      {
76
        return FloatProperty::create(t + "LabelSize",
77
            FloatProperty::getterFromMemberFunction(text, &osgText::TextBase::getCharacterHeight),
78
            FloatProperty::setterFromMemberFunction(text, &osgText::TextBase::setCharacterSize));
79
      }
80
    }
81
82
    /* Declaration of private function members */
83
84
    void LeafNodeXYZAxis::init ()
85
    {
86
        static osg::ref_ptr<osgText::Font> font = defaultFont();
87
88
        /* Create sphere object */
89
        sphere_ptr_ = new ::osg::Sphere();
90
        sphere_ptr_->setRadius(getRadius());
91
        /* Set ShapeDrawable */
92
        shape_drawable_ptr_ = new ::osg::ShapeDrawable(sphere_ptr_);
93
94
        /* Create Geode for adding ShapeDrawable */
95
        geode_ptr_ = new osg::Geode();
96
        geode_ptr_->addDrawable(shape_drawable_ptr_);
97
        if(sizeAxis_ > 0){  // optimisation of memory consumption : doesn't create the axis instead of creating axis with size "0"
98
             /* create the axis : */
99
            // float radiusCyl = (getRadius()/4.f) * getSizeAxis();
100
            float radiusCyl = getRadius()/4.f;
101
            float lengthCyl = getSizeAxis();
102
            float labelShift1 = lengthCyl + getRadius() + 4 * radiusCyl + getRadius();
103
            float labelShift2 = 0;
104
            // float charSize = 3.f * radiusCyl;
105
            float charSize = 60;
106
107
            osgVector4 blue  = osgVector4(0.f, 0.f, 1.f, 1.f);
108
            osgVector4 green = osgVector4(0.f, 1.f, 0.f, 1.f);
109
            osgVector4 red   = osgVector4(1.f, 0.f, 0.f, 1.f);
110
111
            osg::ref_ptr<osgText::Text> lbl;
112
            osg::ShapeDrawableRefPtr arrow;
113
114
            /* X_AXIS */
115
            /* create drawable and add them to geode */
116
            arrow = arrowDrawable (
117
                getRadius(), lengthCyl, radiusCyl,
118
                2.f * radiusCyl, getRadius(),
119
                osgQuat( 0. , ::osg::X_AXIS , ::osg::PI_2 , ::osg::Y_AXIS , 0. , ::osg::Z_AXIS ));
120
            arrow->setColor(red);
121
122
            lbl = label("", font, osgVector3(labelShift1, 0., labelShift2), charSize);
123
124
            addProperty(axisColor("X", arrow.get()));
125
            addProperty(labelText("X", lbl.get()));
126
            addProperty(labelSize("X", lbl.get()));
127
128
            geode_ptr_->addDrawable(arrow);
129
            geode_ptr_->addDrawable(lbl);
130
131
            /* Y_AXIS */
132
            /* create drawable and add them to geode */
133
            arrow = arrowDrawable (
134
                getRadius(), lengthCyl, radiusCyl,
135
                2.f * radiusCyl, getRadius(),
136
                osgQuat( -::osg::PI_2 , ::osg::X_AXIS , 0. , ::osg::Y_AXIS , 0. , ::osg::Z_AXIS ));
137
            arrow->setColor(green);
138
139
            lbl = label("", font, osgVector3(0., labelShift1, labelShift2), charSize);
140
141
            addProperty(axisColor("Y", arrow.get()));
142
            addProperty(labelText("Y", lbl.get()));
143
            addProperty(labelSize("Y", lbl.get()));
144
145
            geode_ptr_->addDrawable(arrow);
146
            geode_ptr_->addDrawable(lbl);
147
148
            /* Z_AXIS */
149
            /* create drawable and add them to geode */
150
            arrow = arrowDrawable (
151
                getRadius(), lengthCyl, radiusCyl,
152
                2.f * radiusCyl, getRadius(),
153
                osgQuat( 0. , ::osg::X_AXIS , 0. , ::osg::Y_AXIS , 0. , ::osg::Z_AXIS ));
154
            arrow->setColor(blue);
155
156
            lbl = label("", font, osgVector3(labelShift2, 0., labelShift1), charSize);
157
158
            addProperty(axisColor("Z", arrow.get()));
159
            addProperty(labelText("Z", lbl.get()));
160
            addProperty(labelSize("Z", lbl.get()));
161
162
            geode_ptr_->addDrawable(arrow);
163
            geode_ptr_->addDrawable(lbl);
164
        } // if radius > 0 : create axis
165
166
167
        /* Create PositionAttitudeTransform */
168
        this->asQueue()->addChild(geode_ptr_);
169
170
        /* Allow transparency */
171
        geode_ptr_->getOrCreateStateSet()->setMode(GL_BLEND, ::osg::StateAttribute::ON);;
172
    }
173
174
    LeafNodeXYZAxis::LeafNodeXYZAxis(const std::string& name, const osgVector4& color, float radiusCenter, float sizeAxis):
175
        NodeDrawable(name)
176
    {
177
178
        setRadius(radiusCenter);
179
        setSizeAxis(sizeAxis);
180
181
        init();
182
        setColor(color);
183
    }
184
185
    LeafNodeXYZAxis::LeafNodeXYZAxis(const LeafNodeXYZAxis& other) :
186
        NodeDrawable(other)
187
    {
188
        init();
189
      //TODO
190
    }
191
192
    void LeafNodeXYZAxis::initWeakPtr(LeafNodeXYZAxisWeakPtr other_weak_ptr)
193
    {
194
        weak_ptr_ = other_weak_ptr;
195
    }
196
197
    /* End of declaration of private function members */
198
199
    /* Declaration of protected function members */
200
201
    LeafNodeXYZAxisPtr_t LeafNodeXYZAxis::create (const std::string& name , const osgVector4& color ,float radiusCenter, float sizeAxis)
202
    {
203
        LeafNodeXYZAxisPtr_t shared_ptr(new LeafNodeXYZAxis(name, color, radiusCenter,sizeAxis));
204
205
        // Add reference to itself
206
        shared_ptr->initWeakPtr(shared_ptr);
207
208
        return shared_ptr;
209
    }
210
211
    LeafNodeXYZAxisPtr_t LeafNodeXYZAxis::create (const std::string& name, const osgVector4& color, float radiusCenter)
212
    {
213
        //const float& size = new float;
214
        //*size = 0;
215
        LeafNodeXYZAxisPtr_t shared_ptr(new LeafNodeXYZAxis(name, color, radiusCenter,0));
216
217
        // Add reference to itself
218
        shared_ptr->initWeakPtr(shared_ptr);
219
220
        return shared_ptr;
221
    }
222
223
    LeafNodeXYZAxisPtr_t LeafNodeXYZAxis::createCopy (LeafNodeXYZAxisPtr_t other)
224
    {
225
        LeafNodeXYZAxisPtr_t shared_ptr(new LeafNodeXYZAxis(*other));
226
227
        // Add reference to itself
228
        shared_ptr->initWeakPtr(shared_ptr);
229
230
        return shared_ptr;
231
    }
232
233
    /* End of declaration of protected function members */
234
235
    /* Declaration of public function members */
236
237
    LeafNodeXYZAxisPtr_t LeafNodeXYZAxis::clone (void) const
238
    {
239
        return LeafNodeXYZAxis::createCopy(weak_ptr_.lock());
240
    }
241
242
    LeafNodeXYZAxisPtr_t LeafNodeXYZAxis::self (void) const
243
    {
244
        return weak_ptr_.lock();
245
    }
246
247
    void LeafNodeXYZAxis::setRadius (const float& radius)
248
    {
249
        radius_=radius;
250
    }
251
252
    float LeafNodeXYZAxis::getRadius() const
253
    {
254
        return radius_;
255
    }
256
257
    void LeafNodeXYZAxis::setSizeAxis(const float& sizeAxis){
258
        sizeAxis_ = sizeAxis;
259
    }
260
261
    float LeafNodeXYZAxis::getSizeAxis() const
262
    {
263
        return sizeAxis_;
264
    }
265
266
    LeafNodeXYZAxis::~LeafNodeXYZAxis ()
267
    {
268
        /* Proper deletion of all tree scene */
269
        geode_ptr_->removeDrawables(0, geode_ptr_->getNumDrawables());
270
        geode_ptr_ = NULL;
271
        weak_ptr_.reset();
272
    }
273
274
    /* End of declaration of public function members */
275
276
} /* namespace viewer */
277

3
} /* namespace gepetto */
278