GCC Code Coverage Report


Directory: ./
File: src/object-map.cc
Date: 2024-09-11 11:37:19
Exec Total Coverage
Lines: 23 58 39.7%
Branches: 8 66 12.1%

Line Branch Exec Source
1 // Copyright (c) 2018, Joseph Mirabel
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // 1. Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27 // DAMAGE.
28
29 #include <hpp/corbaserver/object-map.hh>
30
31 namespace hpp {
32 namespace corbaServer {
33 void ObjectMap::createPolyhedron(const std::string name) {
34 // Check that polyhedron does not already exist.
35 if (nameExists<BothGeomType, ThrowIfItExists>(name)) return;
36 polyhedronMap_[name] = PolyhedronData();
37 }
38
39 // --------------------------------------------------------------------
40
41 void ObjectMap::createBox(const std::string name, value_type x, value_type y,
42 value_type z) {
43 // Check that object does not already exist.
44 if (nameExists<BothGeomType, ThrowIfItExists>(name)) return;
45 shapeMap_[name] = CollisionGeometryPtr_t(new fcl::Box(x, y, z));
46 }
47
48 // --------------------------------------------------------------------
49
50 12 void ObjectMap::createSphere(const std::string name, value_type radius) {
51
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (nameExists<BothGeomType, ThrowIfItExists>(name)) return;
52
2/4
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
12 shapeMap_[name] = CollisionGeometryPtr_t(new fcl::Sphere(radius));
53 }
54
55 // --------------------------------------------------------------------
56
57 void ObjectMap::createCylinder(const std::string name, value_type radius,
58 value_type length) {
59 if (nameExists<BothGeomType, ThrowIfItExists>(name)) return;
60 shapeMap_[name] = CollisionGeometryPtr_t(new fcl::Cylinder(radius, length));
61 }
62
63 // --------------------------------------------------------------------
64
65 std::size_t ObjectMap::addPoint(const std::string name, value_type x,
66 value_type y, value_type z) {
67 // Check that polyhedron exists.
68 if (!nameExists<Polyhedron, ThrowIfItDoesNotExist>(name)) return 0;
69 PolyhedronData& poly = polyhedronMap_[name];
70 poly.pts.push_back(fcl::Vec3f(x, y, z));
71 return poly.pts.size() - 1;
72 }
73
74 // --------------------------------------------------------------------
75
76 std::size_t ObjectMap::addTriangle(const std::string name, std::size_t pt1,
77 std::size_t pt2, std::size_t pt3) {
78 // Check that polyhedron exists.
79 if (!nameExists<Polyhedron, ThrowIfItDoesNotExist>(name)) return 0;
80 PolyhedronData& poly = polyhedronMap_[name];
81 poly.tris.push_back(fcl::Triangle(pt1, pt2, pt3));
82 return poly.tris.size() - 1;
83 }
84
85 // --------------------------------------------------------------------
86
87 12 CollisionGeometryPtr_t ObjectMap::geometry(const std::string name) {
88 12 CollisionGeometryPtr_t geometry;
89 // Check that polyhedron exists.
90
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
12 if (nameExists<Polyhedron, NoThrow>(name)) {
91 const PolyhedronData& poly = polyhedronMap_[name];
92 Polyhedron_t* polyhedron = new Polyhedron_t;
93 geometry.reset(polyhedron);
94 int res = polyhedron->beginModel();
95 if (res != fcl::BVH_OK) {
96 std::ostringstream oss;
97 oss << "fcl BVHReturnCode = " << res;
98 throw hpp::Error(oss.str().c_str());
99 }
100
101 polyhedron->addSubModel(poly.pts, poly.tris);
102 polyhedron->endModel();
103
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
12 } else if (nameExists<Shape, ThrowIfItDoesNotExist>(name)) {
104
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 geometry = shapeMap_[name];
105 }
106 12 return geometry;
107 }
108
109 // --------------------------------------------------------------------
110
111 template <ObjectMap::GeomType geomType, ObjectMap::ThrowType throwType>
112 36 bool ObjectMap::nameExists(const std::string& name) const {
113 36 bool exists = false;
114 switch (geomType) {
115 case Shape:
116 12 exists = (shapeMap_.find(name) != shapeMap_.end());
117 12 break;
118 case Polyhedron:
119 12 exists = (polyhedronMap_.find(name) != polyhedronMap_.end());
120 12 break;
121 case BothGeomType:
122 24 exists = (shapeMap_.find(name) != shapeMap_.end()) ||
123 12 (polyhedronMap_.find(name) != polyhedronMap_.end());
124 12 break;
125 }
126
127 switch (throwType) {
128 case NoThrow:
129 12 return exists;
130 case ThrowIfItExists:
131 12 if (exists) {
132 std::ostringstream oss;
133 oss << "object " << name << " already exists.";
134 throw hpp::Error(oss.str().c_str());
135 }
136 12 return false;
137 case ThrowIfItDoesNotExist:
138 12 if (!exists) {
139 std::ostringstream oss;
140 oss << "object " << name << " does not exist.";
141 throw hpp::Error(oss.str().c_str());
142 }
143 12 return true;
144 }
145 }
146 } // end of namespace corbaServer.
147 } // end of namespace hpp.
148