GCC Code Coverage Report


Directory: ./
File: src/leaf-node-line.cpp
Date: 2025-04-14 11:18:00
Exec Total Coverage
Lines: 0 148 0.0%
Branches: 0 154 0.0%

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