GCC Code Coverage Report


Directory: ./
File: src/leaf-node-xyzaxis.cpp
Date: 2024-08-14 11:04:57
Exec Total Coverage
Lines: 0 133 0.0%
Branches: 0 234 0.0%

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