GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/leaf-node-line.cpp Lines: 1 145 0.7 %
Date: 2020-05-14 11:23:33 Branches: 2 146 1.4 %

Line Branch Exec Source
1
//
2
//  leaf-node-line.cpp
3
//  gepetto-viewer
4
//
5
//  Created by Justin Carpentier, Mathieu Geisert in November 2014.
6
//  Copyright (c) 2014 LAAS-CNRS. All rights reserved.
7
//
8
9
#include <gepetto/viewer/leaf-node-line.h>
10
11
#include <osg/Point>
12
#include <osg/CullFace>
13
#include <osg/LineWidth>
14
#include <osgUtil/SmoothingVisitor>
15
#include <gepetto/viewer/node.h>
16
17
namespace gepetto {
18
namespace viewer {
19
    int getNodeMode (LeafNodeLine* node) { return node->getMode(); }
20
    void setNodeMode (LeafNodeLine* node, const int& v) { node->setMode((GLenum)v); }
21
22
    void setNodePointSize (LeafNodeLine* node, const float& size)
23
    {
24
      ::osg::GeometryRefPtr geom = node->geometry ();
25
      osg::Point* point = static_cast<osg::Point*>(
26
          geom->getOrCreateStateSet()->getAttribute(osg::StateAttribute::POINT));
27
      point->setSize(size);
28
      geom->dirtyDisplayList();
29
    }
30
31
    void setNodeLineWidth (LeafNodeLine* node, const float& width)
32
    {
33
      ::osg::GeometryRefPtr geom = node->geometry ();
34
      osg::LineWidth* linewidth = static_cast<osg::LineWidth*>(
35
        geom->getOrCreateStateSet()->getAttribute(osg::StateAttribute::LINEWIDTH));
36
      linewidth->setWidth(width);
37
      geom->dirtyDisplayList();
38
    }
39
40
41
    /* Declaration of private function members */
42
    void LeafNodeLine::init ()
43
    {
44
        /* Init the beam as a Geometry */
45
        beam_ptr_ = new ::osg::Geometry();
46
47
        /* Define points of the beam */
48
        points_ptr_ = new ::osg::Vec3Array(2);
49
50
        /* Define the color */
51
        color_ptr_ = new ::osg::Vec4Array(1);
52
53
        beam_ptr_->setVertexArray(points_ptr_.get());
54
        beam_ptr_->setColorArray(color_ptr_.get(), ::osg::Array::BIND_OVERALL);
55
        drawArray_ptr_ = new osg::DrawArrays(GL_LINE_STRIP,0,2);
56
        beam_ptr_->addPrimitiveSet(drawArray_ptr_.get());
57
58
        /* Create Geode for adding ShapeDrawable */
59
        geode_ptr_ = new osg::Geode();
60
        geode_ptr_->addDrawable(beam_ptr_);
61
62
        /* Add geode to the queue */
63
        this->asQueue()->addChild(geode_ptr_);
64
65
        /* Allow transparency */
66
        geode_ptr_->getOrCreateStateSet()->setMode(GL_BLEND, ::osg::StateAttribute::ON);
67
68
        /* No light influence by default */
69
        setLightingMode(LIGHT_INFLUENCE_OFF);
70
71
        /* Set a default line width */
72
        osg::LineWidth* linewidth = new osg::LineWidth();
73
        linewidth->setWidth(1.0f);
74
        beam_ptr_->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
75
76
        osg::Point* point = new osg::Point(3.f);
77
        beam_ptr_->getOrCreateStateSet()->setAttribute(point, osg::StateAttribute::ON);
78
79
        addProperty(FloatProperty::create("PointSize",
80
              FloatProperty::getterFromMemberFunction(point, &osg::Point::getSize),
81
              FloatProperty::Setter_t(boost::bind(setNodePointSize, this, _1))));
82
        addProperty(FloatProperty::create("LineWidth",
83
              FloatProperty::getterFromMemberFunction(linewidth, &osg::LineWidth::getWidth),
84
              FloatProperty::Setter_t(boost::bind(setNodeLineWidth, this, _1))));
85
        addProperty(EnumProperty::create("ImmediateMode", glImmediateModeEnum(),
86
              EnumProperty::Getter_t(boost::bind(getNodeMode, this)),
87
              EnumProperty::Setter_t(boost::bind(setNodeMode, this, _1))));
88
        addProperty(Vector4Property::create("Color", this,
89
              &LeafNodeLine::getColor, &LeafNodeLine::setColor));
90
    }
91
92
    LeafNodeLine::LeafNodeLine (const std::string& name, const osgVector3& start_point, const osgVector3& end_point) :
93
        Node (name)
94
    {
95
        init ();
96
        setStartPoint(start_point);
97
        setEndPoint(end_point);
98
        setColor(osgVector4(1.,1.,1.,1.));
99
    }
100
101
    LeafNodeLine::LeafNodeLine (const std::string& name, const osgVector3& start_point, const osgVector3& end_point, const osgVector4& color) :
102
        Node (name)
103
    {
104
        init ();
105
        setStartPoint(start_point);
106
        setEndPoint(end_point);
107
        setColor(color);
108
    }
109
110
    LeafNodeLine::LeafNodeLine (const std::string& name, const ::osg::Vec3ArrayRefPtr& points, const osgVector4& color) :
111
        Node (name)
112
    {
113
        init ();
114
        setPoints(points);
115
        setColor(color);
116
    }
117
118
    LeafNodeLine::LeafNodeLine (const LeafNodeLine& other) :
119
        Node (other)
120
    {
121
        init();
122
        setPoints (other.points_ptr_);
123
        setColor(other.getColor());
124
    }
125
126
    void LeafNodeLine::initWeakPtr (LeafNodeLineWeakPtr other_weak_ptr )
127
    {
128
        weak_ptr_ = other_weak_ptr;
129
    }
130
131
    /* End of declaration of private function members */
132
133
    /* Declaration of protected function members */
134
    LeafNodeLinePtr_t LeafNodeLine::create (const std::string& name, const osgVector3& start_point, const osgVector3& end_point)
135
    {
136
        LeafNodeLinePtr_t shared_ptr(new LeafNodeLine (name, start_point, end_point));
137
138
        // Add reference to itself
139
        shared_ptr->initWeakPtr (shared_ptr);
140
141
        return shared_ptr;
142
    }
143
144
    LeafNodeLinePtr_t LeafNodeLine::create (const std::string& name, const osgVector3& start_point, const osgVector3& end_point, const osgVector4& color)
145
    {
146
        LeafNodeLinePtr_t shared_ptr(new LeafNodeLine (name, start_point, end_point, color));
147
148
        // Add reference to itself
149
        shared_ptr->initWeakPtr (shared_ptr);
150
151
        return shared_ptr;
152
    }
153
154
    LeafNodeLinePtr_t LeafNodeLine::create (const std::string& name, const ::osg::Vec3ArrayRefPtr& points, const osgVector4& color)
155
    {
156
        LeafNodeLinePtr_t shared_ptr(new LeafNodeLine (name, points, color));
157
158
        // Add reference to itself
159
        shared_ptr->initWeakPtr (shared_ptr);
160
161
        return shared_ptr;
162
    }
163
164
    LeafNodeLinePtr_t LeafNodeLine::createCopy (LeafNodeLinePtr_t other)
165
    {
166
        LeafNodeLinePtr_t shared_ptr(new LeafNodeLine (*other));
167
168
        // Add reference to itself
169
        shared_ptr->initWeakPtr (shared_ptr);
170
171
        return shared_ptr;
172
    }
173
174
    /* End of declaration of protected function members */
175
176
    /* Declaration of public function members */
177
178
    LeafNodeLinePtr_t LeafNodeLine::clone (void) const
179
    {
180
        return LeafNodeLine::createCopy(weak_ptr_.lock());
181
    }
182
183
    LeafNodeLinePtr_t LeafNodeLine::self (void) const
184
    {
185
        return weak_ptr_.lock();
186
    }
187
188
    void LeafNodeLine::setStartPoint (const osgVector3& start_point)
189
    {
190
        points_ptr_->at(0) = start_point;
191
        beam_ptr_->dirtyDisplayList();
192
        setDirty();
193
    }
194
195
    osgVector3 LeafNodeLine::getStartPoint() const
196
    {
197
        return points_ptr_->at(0);
198
    }
199
200
    void LeafNodeLine::setEndPoint (const osgVector3& end_point)
201
    {
202
        points_ptr_->at(1) = end_point;
203
        beam_ptr_->dirtyDisplayList();
204
        setDirty();
205
    }
206
207
    osgVector3 LeafNodeLine::getEndPoint() const
208
    {
209
        return points_ptr_->at(1);
210
    }
211
212
    void LeafNodeLine::setMode (const GLenum& mode)
213
    {
214
      drawArray_ptr_->set (mode, 0, (GLsizei)points_ptr_->size ());
215
      osgUtil::SmoothingVisitor::smooth( *beam_ptr_ );
216
      beam_ptr_->dirtyDisplayList();
217
      setDirty();
218
    }
219
220
    GLenum LeafNodeLine::getMode () const
221
    {
222
      return drawArray_ptr_->getMode();
223
    }
224
225
    void LeafNodeLine::setPoints (const osgVector3& start_point, const osgVector3& end_point)
226
    {
227
        setStartPoint(start_point);
228
        setEndPoint(end_point);
229
    }
230
231
    void LeafNodeLine::setPoints (const ::osg::Vec3ArrayRefPtr& points)
232
    {
233
        bool sizeChanged = (points->size() != points_ptr_->size());
234
        points_ptr_ = points;
235
        beam_ptr_->setVertexArray (points_ptr_.get ());
236
        if (sizeChanged)
237
          setPointsSubset (0, points->size());
238
        else {
239
          beam_ptr_->dirtyDisplayList();
240
          setDirty();
241
        }
242
    }
243
244
    void LeafNodeLine::setPointsSubset (const int first, const std::size_t count)
245
    {
246
        if (first + count > points_ptr_->size())
247
          throw std::invalid_argument ("Invalid range of points in LeafNodeLine::setPointsSubset");
248
        drawArray_ptr_->setFirst (first);
249
        drawArray_ptr_->setCount ((GLsizei)count);
250
        beam_ptr_->dirtyDisplayList();
251
        setDirty();
252
    }
253
254
    void LeafNodeLine::setColor (const osgVector4& color)
255
    {
256
        color_ptr_->at(0) = color;
257
        beam_ptr_->setColorArray(color_ptr_.get(), ::osg::Array::BIND_OVERALL);
258
        beam_ptr_->dirtyDisplayList();
259
        setTransparentRenderingBin (color[3] < Node::TransparencyRenderingBinThreshold);
260
        setDirty();
261
    }
262
263
    void LeafNodeLine::setColors (const ::osg::Vec4ArrayRefPtr & colors)
264
    {
265
      color_ptr_ = colors;
266
      beam_ptr_->setColorArray(color_ptr_.get(), ::osg::Array::BIND_PER_VERTEX);
267
      beam_ptr_->dirtyDisplayList();
268
      bool transparent = false;
269
      for (std::size_t i = 0; i < colors->size(); ++i) {
270
        if (colors->at(i)[3] < Node::TransparencyRenderingBinThreshold) {
271
          transparent = true;
272
          break;
273
        }
274
      }
275
      setTransparentRenderingBin (transparent);
276
      setDirty();
277
    }
278
279
    // void LeafNodeLine::setLineStipple (const GLint factor, const GLushort pattern)
280
    // {
281
      // osg::LineStipple* stipple = static_cast<osg::LineStipple*>(
282
        // beam_ptr_->getOrCreateStateSet()->getAttribute(osg::StateAttribute::LINESTIPPLE));
283
      // stipple->setFactor(factor);
284
      // stipple->setPattern(pattern);
285
      // beam_ptr_->dirtyDisplayList();
286
    // }
287
288
    LeafNodeLine::~LeafNodeLine()
289
    {
290
        /* Proper deletion of all tree scene */
291
        geode_ptr_->removeDrawable(beam_ptr_);
292
        //std::cout << "Beam ref count " << beam_ptr_->referenceCount() << std::endl;
293
        beam_ptr_ = NULL;
294
295
        this->asQueue()->removeChild(geode_ptr_);
296
        //std::cout << "Geode ref count " << geode_ptr_->referenceCount() << std::endl;
297
        geode_ptr_ = NULL;
298
299
        weak_ptr_.reset();
300
        //std::cout << "Destruction of line " << getID() << std::endl;
301
    }
302
303
    /* End of declaration of public function members */
304
305
} /* namespace viewer */
306
307

3
} /* namespace gepetto */