GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/leaf-node-ground.cpp Lines: 1 112 0.9 %
Date: 2020-05-14 11:23:33 Branches: 2 172 1.2 %

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
16
    void LeafNodeGround::init()
17
    {
18
      osgVector3 center = osgVector3( length_ , width_ , 0.f );
19
20
      /* Number of cells on both dimensions */
21
      float nX = floorf( 2.f * length_ / square_length_ );
22
      float nY = floorf( 2.f * width_ / square_width_ );
23
24
      /* According to floor operation, we adapt sizes */
25
      //setSquareLength(2. * length_ / nX);
26
      //setSquareWidth(2. * width_ / nY);
27
28
      /* Setting base vectors */
29
      osgVector3 x_base_square = osgVector3( square_length_ , 0.0f , 0.0f );
30
      osgVector3 y_base_square = osgVector3( 0.0f , square_width_ , 0.0f );
31
32
      /* Allocation of vertices */
33
      ::osg::Vec3ArrayRefPtr vertices_array_ptr = new ::osg::Vec3Array;
34
      colors_array_ptr_ = new ::osg::Vec4Array;
35
36
      for ( int j(0) ; j < (int)nY ; j++ )
37
      {
38
        for ( int i(0) ; i < (int)nX ; i++ )
39
        {
40
          vertices_array_ptr->push_back( - center + x_base_square * ((float) i) + y_base_square * ((float) (j+1)));
41
          vertices_array_ptr->push_back( - center + x_base_square * ((float) i) + y_base_square * ((float) j));
42
          vertices_array_ptr->push_back( - center + x_base_square * ((float) (i+1)) + y_base_square * ((float) j));
43
          vertices_array_ptr->push_back( - center + x_base_square * ((float) (i+1)) + y_base_square * ((float) (j+1)));
44
45
          if ((i+j)%2) {
46
            colors_array_ptr_->push_back(color1_);
47
            colors_array_ptr_->push_back(color1_);
48
            colors_array_ptr_->push_back(color1_);
49
            colors_array_ptr_->push_back(color1_);
50
          }
51
          else {
52
            colors_array_ptr_->push_back(color2_);
53
            colors_array_ptr_->push_back(color2_);
54
            colors_array_ptr_->push_back(color2_);
55
            colors_array_ptr_->push_back(color2_);
56
          }
57
        }
58
      }
59
60
      /* Allocating grid_geometry_ptr_ */
61
      if (!grid_geometry_ptr_.valid()) {
62
        grid_geometry_ptr_ = new ::osg::Geometry;
63
      }
64
65
      grid_geometry_ptr_->setVertexArray(vertices_array_ptr);
66
      grid_geometry_ptr_->setColorArray(colors_array_ptr_);
67
68
      grid_geometry_ptr_->setColorBinding(::osg::Geometry::BIND_PER_VERTEX);
69
70
      /* Define the normal to all quads */
71
      ::osg::Vec3ArrayRefPtr normals_array_ptr = new ::osg::Vec3Array;
72
      normals_array_ptr->push_back( osgVector3(0.0f,0.0f, 1.0f) );
73
      grid_geometry_ptr_->setNormalArray( normals_array_ptr );
74
      grid_geometry_ptr_->setNormalBinding( ::osg::Geometry::BIND_OVERALL );
75
76
      /* Defining type of geometries */
77
      grid_geometry_ptr_->addPrimitiveSet
78
	(new ::osg::DrawArrays (::osg::PrimitiveSet::QUADS, 0,
79
				(GLsizei) vertices_array_ptr->size ()));
80
81
82
        /* Allocating geode_ptr_ */
83
        if (!geode_ptr_.valid()) {
84
            geode_ptr_ = new ::osg::Geode;
85
        }
86
87
        geode_ptr_->addDrawable(grid_geometry_ptr_);
88
89
        //node_osg_ptr_->asGroup()->addChild(geode_ptr_);
90
        asQueue()->addChild(geode_ptr_);
91
92
        /* Apply colors */
93
        setColors(color1_,color2_);
94
95
        if (hasProperty("Color"))
96
          properties_.erase("Color");
97
        addProperty(Vector4Property::create("Color1",
98
              Vector4Property::getterFromMemberFunction(this, &LeafNodeGround::getColor1),
99
              Vector4Property::setterFromMemberFunction(this, &LeafNodeGround::setColor1)));
100
        addProperty(Vector4Property::create("Color2",
101
              Vector4Property::getterFromMemberFunction(this, &LeafNodeGround::getColor2),
102
              Vector4Property::setterFromMemberFunction(this, &LeafNodeGround::setColor2)));
103
104
#ifdef DEBUG
105
        std::cout << getID() << " created" << std::endl;
106
#endif
107
    }
108
109
    void LeafNodeGround::initWeakPtr( const LeafNodeGroundWeakPtr &other_weak_ptr )
110
    {
111
        weak_ptr_ = other_weak_ptr;
112
    }
113
114
    /* End of declaration of private function members */
115
116
    /* Declaration of protected function members */
117
118
119
    LeafNodeGround::LeafNodeGround(const std::string& name, const float &length, const float &width, const float &square_length, const float &square_width, const osgVector4& color1, const osgVector4& color2):
120
        Node(name), length_(length), width_(width), square_length_(square_length), square_width_(square_width), color1_(color1), color2_(color2)
121
        {
122
            init();
123
        }
124
125
    LeafNodeGround::LeafNodeGround(const LeafNodeGround &other) :
126
        Node(other.getID()), length_(other.length_), width_(other.width_), square_length_(other.square_length_), square_width_(other.square_width_), color1_(other.getColor1()), color2_(other.getColor2())
127
        {
128
            init();
129
        }
130
131
    /* End of declaration of protected function members */
132
133
    /* Declaration of public function members */
134
135
    LeafNodeGroundPtr_t LeafNodeGround::create(const std::string& name, const float& length, const float& width)
136
    {
137
138
        LeafNodeGroundPtr_t shared_ptr( new LeafNodeGround(name, length, width, length/10.f, width/10.f, osgVector4(0.,0.,0.,1.), osgVector4(1.,1.,1.,1.)) );
139
140
        // Add reference to itself
141
        shared_ptr->initWeakPtr(shared_ptr);
142
143
        return shared_ptr;
144
    }
145
146
    LeafNodeGroundPtr_t LeafNodeGround::create(const std::string& name, const float &length, const float &width, const float &square_length, const float &square_width)
147
    {
148
      LeafNodeGroundPtr_t shared_ptr( new LeafNodeGround(name, length, width, square_length, square_width, osgVector4(0.,0.,0.,1.), osgVector4(1.,1.,1.,1.)) );
149
150
        // Add reference to itself
151
        shared_ptr->initWeakPtr(shared_ptr);
152
153
        return shared_ptr;
154
    }
155
156
    LeafNodeGroundPtr_t LeafNodeGround::create(const std::string& name)
157
    {
158
      LeafNodeGroundPtr_t shared_ptr( new LeafNodeGround(name, 10.f, 10.f, 1.f, 1.f, osgVector4(0.,0.,0.,1.), osgVector4(1.,1.,1.,1.)) );
159
160
        // Add reference to itself
161
        shared_ptr->initWeakPtr(shared_ptr);
162
163
        return shared_ptr;
164
    }
165
166
    LeafNodeGroundPtr_t LeafNodeGround::createCopy(const LeafNodeGroundPtr_t &other)
167
    {
168
      LeafNodeGroundPtr_t shared_ptr( new LeafNodeGround( *other ) );
169
170
        // Add reference to itself
171
        shared_ptr->initWeakPtr(shared_ptr);
172
173
        return shared_ptr;
174
    }
175
176
    LeafNodeGroundPtr_t LeafNodeGround::clone(void) const
177
    {
178
        return LeafNodeGround::createCopy( weak_ptr_.lock() );
179
    }
180
181
    LeafNodeGroundPtr_t LeafNodeGround::self(void) const
182
    {
183
        return weak_ptr_.lock();
184
    }
185
186
    void LeafNodeGround::setColor(const osgVector4 &color)
187
    {
188
        setColor1(color);
189
    }
190
191
    void LeafNodeGround::setColor1(const osgVector4 &color1)
192
    {
193
        LeafNodeGround::setColors(color1, color2_);
194
    }
195
196
    void LeafNodeGround::setColor2(const osgVector4 &color2)
197
    {
198
        LeafNodeGround::setColors(color1_, color2);
199
    }
200
201
    void LeafNodeGround::setColors(const osgVector4 &color1 , const osgVector4 &color2)
202
    {
203
        color1_ = color1;
204
        color2_ = color2;
205
206
        /* Reset colors array */
207
        colors_array_ptr_.release();
208
        colors_array_ptr_ = new ::osg::Vec4Array;
209
210
        /* Number of cells on both dimensions */
211
        float nX = floorf( 2.f * length_ / square_length_ );
212
        float nY = floorf( 2.f * width_ / square_width_ );
213
214
        /* Set colors */
215
        for ( int j(0) ; j < (int)nY ; j++ )
216
        {
217
          for ( int i(0) ; i < (int)nX ; i++ )
218
          {
219
            if ((i+j)%2) {
220
              colors_array_ptr_->push_back(color1_);
221
              colors_array_ptr_->push_back(color1_);
222
              colors_array_ptr_->push_back(color1_);
223
              colors_array_ptr_->push_back(color1_);
224
            }
225
            else {
226
              colors_array_ptr_->push_back(color2_);
227
              colors_array_ptr_->push_back(color2_);
228
              colors_array_ptr_->push_back(color2_);
229
              colors_array_ptr_->push_back(color2_);
230
            }
231
          }
232
        }
233
234
        /* Apply colors */
235
        grid_geometry_ptr_->setColorArray(colors_array_ptr_);
236
        grid_geometry_ptr_->setColorBinding(::osg::Geometry::BIND_PER_VERTEX);
237
        setTransparentRenderingBin (   color1[3] < Node::TransparencyRenderingBinThreshold
238
                                    || color2[3] < Node::TransparencyRenderingBinThreshold);
239
        setDirty();
240
    }
241
242
    LeafNodeGround::~LeafNodeGround()
243
    {
244
245
      /* Proper deletion of all tree scene */
246
      geode_ptr_->removeDrawable(grid_geometry_ptr_);
247
      grid_geometry_ptr_ = NULL;
248
249
      this->asQueue()->removeChild(geode_ptr_);
250
      geode_ptr_ = NULL;
251
252
      colors_array_ptr_.release();
253
254
      weak_ptr_.reset();
255
256
    }
257
258
    /* End of declaration of public function members */
259
} /* namespace viewer */
260
261

3
}