GCC Code Coverage Report


Directory: ./
File: src/leaf-node-ground.cpp
Date: 2024-12-20 15:53:58
Exec Total Coverage
Lines: 0 129 0.0%
Branches: 0 192 0.0%

Line Branch Exec Source
1 //
2 // leaf-node-ground.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-ground.h>
10
11 namespace gepetto {
12 namespace viewer {
13
14 /* Declaration of private function members */
15 void LeafNodeGround::init() {
16 for (auto* n : {&nX_, &nY_}) {
17 n->min = 2;
18 n->step = 2;
19 n->callback(boost::bind(&LeafNodeGround::updateVertices, this));
20 }
21 for (auto* size : {&square_size_x_, &square_size_y_}) {
22 size->min = 0.000001f;
23 size->step = 0.1f;
24 size->adaptiveDecimal = true;
25 size->callback(boost::bind(&LeafNodeGround::updateVertices, this));
26 }
27
28 /* Allocation of vertices */
29 vertices_array_ptr_ = new ::osg::Vec3Array;
30 colors_array_ptr_ = new ::osg::Vec4Array;
31
32 /* Allocating grid_geometry_ptr_ */
33 if (!grid_geometry_ptr_.valid()) {
34 grid_geometry_ptr_ = new ::osg::Geometry;
35 }
36
37 grid_geometry_ptr_->setVertexArray(vertices_array_ptr_);
38 grid_geometry_ptr_->setColorArray(colors_array_ptr_);
39
40 grid_geometry_ptr_->setColorBinding(::osg::Geometry::BIND_PER_VERTEX);
41
42 /* Define the normal to all quads */
43 ::osg::Vec3ArrayRefPtr normals_array_ptr = new ::osg::Vec3Array;
44 normals_array_ptr->push_back(osgVector3(0.0f, 0.0f, 1.0f));
45 grid_geometry_ptr_->setNormalArray(normals_array_ptr);
46 grid_geometry_ptr_->setNormalBinding(::osg::Geometry::BIND_OVERALL);
47
48 /* Defining type of geometries */
49 draw_array_ptr_ = new osg::DrawArrays(::osg::PrimitiveSet::QUADS, 0, 0);
50 grid_geometry_ptr_->addPrimitiveSet(draw_array_ptr_);
51
52 updateVertices();
53
54 /* Allocating geode_ptr_ */
55 if (!geode_ptr_.valid()) {
56 geode_ptr_ = new ::osg::Geode;
57 }
58
59 geode_ptr_->addDrawable(grid_geometry_ptr_);
60
61 // node_osg_ptr_->asGroup()->addChild(geode_ptr_);
62 asQueue()->addChild(geode_ptr_);
63
64 /* Apply colors */
65 setColors(color1_, color2_);
66
67 if (hasProperty("Color")) properties_.erase("Color");
68 addProperty(Vector4Property::create("Color1",
69 Vector4Property::getterFromMemberFunction(
70 this, &LeafNodeGround::getColor1),
71 Vector4Property::setterFromMemberFunction(
72 this, &LeafNodeGround::setColor1)));
73 addProperty(Vector4Property::create("Color2",
74 Vector4Property::getterFromMemberFunction(
75 this, &LeafNodeGround::getColor2),
76 Vector4Property::setterFromMemberFunction(
77 this, &LeafNodeGround::setColor2)));
78 addProperty(&nX_);
79 addProperty(&nY_);
80 addProperty(&square_size_x_);
81 addProperty(&square_size_y_);
82 }
83
84 void LeafNodeGround::updateVertices() {
85 float sx = square_size_x_.value;
86 float sy = square_size_y_.value;
87 int nx = nX_.value;
88 int ny = nY_.value;
89
90 osgVector3 corner = osgVector3(-(float)nx * sx / 2, -(float)ny * sy / 2, 0.f);
91
92 /* Allocation of vertices */
93 vertices_array_ptr_->resize(nx * ny * 4);
94 colors_array_ptr_->resize(nx * ny * 4);
95
96 int k = 0;
97 for (int j(0); j < ny; j++) {
98 for (int i(0); i < nx; i++) {
99 vertices_array_ptr_->at(k + 0) =
100 corner + osgVector3(sx * (float)i, sy * float(j + 1), 0.f);
101 vertices_array_ptr_->at(k + 1) =
102 corner + osgVector3(sx * (float)i, sy * (float)j, 0.f);
103 vertices_array_ptr_->at(k + 2) =
104 corner + osgVector3(sx * float(i + 1), sy * (float)j, 0.f);
105 vertices_array_ptr_->at(k + 3) =
106 corner + osgVector3(sx * float(i + 1), sy * float(j + 1), 0.f);
107
108 if ((i + j) % 2) {
109 colors_array_ptr_->at(k + 0) = color1_;
110 colors_array_ptr_->at(k + 1) = color1_;
111 colors_array_ptr_->at(k + 2) = color1_;
112 colors_array_ptr_->at(k + 3) = color1_;
113 } else {
114 colors_array_ptr_->at(k + 0) = color2_;
115 colors_array_ptr_->at(k + 1) = color2_;
116 colors_array_ptr_->at(k + 2) = color2_;
117 colors_array_ptr_->at(k + 3) = color2_;
118 }
119 k += 4;
120 }
121 }
122
123 grid_geometry_ptr_->setColorBinding(::osg::Geometry::BIND_PER_VERTEX);
124
125 /* Defining type of geometries */
126 draw_array_ptr_->setCount((GLsizei)vertices_array_ptr_->size());
127 grid_geometry_ptr_->dirtyDisplayList();
128 setDirty();
129 }
130
131 void LeafNodeGround::initWeakPtr(const LeafNodeGroundWeakPtr& other_weak_ptr) {
132 weak_ptr_ = other_weak_ptr;
133 }
134
135 /* End of declaration of private function members */
136
137 /* Declaration of protected function members */
138
139 LeafNodeGround::LeafNodeGround(const std::string& name, int nX, int nY,
140 float square_length, float square_width,
141 const osgVector4& color1,
142 const osgVector4& color2)
143 : Node(name),
144 square_size_x_("CellSizeX"),
145 square_size_y_("CellSizeY"),
146 nX_("NbCellsX"),
147 nY_("NbCellsY"),
148 color1_(color1),
149 color2_(color2) {
150 nX_.value = nX;
151 nY_.value = nY;
152 square_size_x_.value = square_length;
153 square_size_y_.value = square_width;
154 init();
155 }
156
157 /* End of declaration of protected function members */
158
159 /* Declaration of public function members */
160
161 LeafNodeGroundPtr_t LeafNodeGround::create(const std::string& name, int nX,
162 int nY, float square_length,
163 float square_width) {
164 LeafNodeGroundPtr_t shared_ptr(new LeafNodeGround(
165 name, nX, nY, square_length, square_width, osgVector4(0., 0., 0., 1.),
166 osgVector4(1., 1., 1., 1.)));
167
168 // Add reference to itself
169 shared_ptr->initWeakPtr(shared_ptr);
170
171 return shared_ptr;
172 }
173
174 LeafNodeGroundPtr_t LeafNodeGround::create(const std::string& name) {
175 return create(name, 10, 10, 1.f, 1.f);
176 }
177
178 LeafNodeGroundPtr_t LeafNodeGround::self(void) const {
179 return weak_ptr_.lock();
180 }
181
182 void LeafNodeGround::setColor(const osgVector4& color) { setColor1(color); }
183
184 void LeafNodeGround::setColor1(const osgVector4& color1) {
185 LeafNodeGround::setColors(color1, color2_);
186 }
187
188 void LeafNodeGround::setColor2(const osgVector4& color2) {
189 LeafNodeGround::setColors(color1_, color2);
190 }
191
192 void LeafNodeGround::setColors(const osgVector4& color1,
193 const osgVector4& color2) {
194 color1_ = color1;
195 color2_ = color2;
196
197 /* Set colors */
198 int k = 0;
199 for (int j(0); j < nY_.value; j++) {
200 for (int i(0); i < nX_.value; i++) {
201 if ((i + j) % 2) {
202 colors_array_ptr_->at(k + 0) = color1_;
203 colors_array_ptr_->at(k + 1) = color1_;
204 colors_array_ptr_->at(k + 2) = color1_;
205 colors_array_ptr_->at(k + 3) = color1_;
206 } else {
207 colors_array_ptr_->at(k + 0) = color2_;
208 colors_array_ptr_->at(k + 1) = color2_;
209 colors_array_ptr_->at(k + 2) = color2_;
210 colors_array_ptr_->at(k + 3) = color2_;
211 }
212 k += 4;
213 }
214 }
215
216 /* Apply colors */
217 setTransparentRenderingBin(
218 color1[3] < Node::TransparencyRenderingBinThreshold ||
219 color2[3] < Node::TransparencyRenderingBinThreshold);
220 setDirty();
221 }
222
223 LeafNodeGround::~LeafNodeGround() {
224 /* Proper deletion of all tree scene */
225 geode_ptr_->removeDrawable(grid_geometry_ptr_);
226 grid_geometry_ptr_ = NULL;
227
228 this->asQueue()->removeChild(geode_ptr_);
229 geode_ptr_ = NULL;
230
231 colors_array_ptr_.release();
232
233 weak_ptr_.reset();
234 }
235
236 /* End of declaration of public function members */
237 } /* namespace viewer */
238
239 } // namespace gepetto
240