| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Software License Agreement (BSD License) | ||
| 3 | * | ||
| 4 | * Copyright (c) 2011-2014, Willow Garage, Inc. | ||
| 5 | * Copyright (c) 2014-2015, Open Source Robotics Foundation | ||
| 6 | * All rights reserved. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * | ||
| 12 | * * Redistributions of source code must retain the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer. | ||
| 14 | * * Redistributions in binary form must reproduce the above | ||
| 15 | * copyright notice, this list of conditions and the following | ||
| 16 | * disclaimer in the documentation and/or other materials provided | ||
| 17 | * with the distribution. | ||
| 18 | * * Neither the name of Open Source Robotics Foundation nor the names of its | ||
| 19 | * contributors may be used to endorse or promote products derived | ||
| 20 | * from this software without specific prior written permission. | ||
| 21 | * | ||
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
| 25 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
| 26 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
| 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 29 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 30 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
| 32 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 33 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | /** \author Jia Pan */ | ||
| 37 | |||
| 38 | #ifndef COAL_GEOMETRIC_SHAPE_TO_BVH_MODEL_H | ||
| 39 | #define COAL_GEOMETRIC_SHAPE_TO_BVH_MODEL_H | ||
| 40 | |||
| 41 | #include "coal/shape/geometric_shapes.h" | ||
| 42 | #include "coal/BVH/BVH_model.h" | ||
| 43 | #include <boost/math/constants/constants.hpp> | ||
| 44 | |||
| 45 | namespace coal { | ||
| 46 | |||
| 47 | /// @brief Generate BVH model from box | ||
| 48 | template <typename BV> | ||
| 49 | 1866 | void generateBVHModel(BVHModel<BV>& model, const Box& shape, | |
| 50 | const Transform3s& pose) { | ||
| 51 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | Scalar a = shape.halfSide[0]; |
| 52 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | Scalar b = shape.halfSide[1]; |
| 53 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | Scalar c = shape.halfSide[2]; |
| 54 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
3732 | std::vector<Vec3s> points(8); |
| 55 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | std::vector<Triangle32> tri_indices(12); |
| 56 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | points[0] = Vec3s(a, -b, c); |
| 57 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | points[1] = Vec3s(a, b, c); |
| 58 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | points[2] = Vec3s(-a, b, c); |
| 59 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | points[3] = Vec3s(-a, -b, c); |
| 60 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | points[4] = Vec3s(a, -b, -c); |
| 61 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | points[5] = Vec3s(a, b, -c); |
| 62 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | points[6] = Vec3s(-a, b, -c); |
| 63 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | points[7] = Vec3s(-a, -b, -c); |
| 64 | |||
| 65 | 1866 | tri_indices[0].set(0, 4, 1); | |
| 66 | 1866 | tri_indices[1].set(1, 4, 5); | |
| 67 | 1866 | tri_indices[2].set(2, 6, 3); | |
| 68 | 1866 | tri_indices[3].set(3, 6, 7); | |
| 69 | 1866 | tri_indices[4].set(3, 0, 2); | |
| 70 | 1866 | tri_indices[5].set(2, 0, 1); | |
| 71 | 1866 | tri_indices[6].set(6, 5, 7); | |
| 72 | 1866 | tri_indices[7].set(7, 5, 4); | |
| 73 | 1866 | tri_indices[8].set(1, 5, 2); | |
| 74 | 1866 | tri_indices[9].set(2, 5, 6); | |
| 75 | 1866 | tri_indices[10].set(3, 7, 0); | |
| 76 | 1866 | tri_indices[11].set(0, 7, 4); | |
| 77 | |||
| 78 |
2/2✓ Branch 1 taken 14928 times.
✓ Branch 2 taken 1866 times.
|
16794 | for (unsigned int i = 0; i < points.size(); ++i) { |
| 79 |
2/4✓ Branch 2 taken 14928 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 14928 times.
✗ Branch 8 not taken.
|
14928 | points[i] = pose.transform(points[i]).eval(); |
| 80 | } | ||
| 81 | |||
| 82 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | model.beginModel(); |
| 83 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | model.addSubModel(points, tri_indices); |
| 84 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | model.endModel(); |
| 85 |
1/2✓ Branch 1 taken 1866 times.
✗ Branch 2 not taken.
|
1866 | model.computeLocalAABB(); |
| 86 | 1866 | } | |
| 87 | |||
| 88 | /// @brief Generate BVH model from sphere, given the number of segments along | ||
| 89 | /// longitude and number of rings along latitude. | ||
| 90 | template <typename BV> | ||
| 91 | 369 | void generateBVHModel(BVHModel<BV>& model, const Sphere& shape, | |
| 92 | const Transform3s& pose, unsigned int seg, | ||
| 93 | unsigned int ring) { | ||
| 94 | 369 | std::vector<Vec3s> points; | |
| 95 | 369 | std::vector<Triangle32> tri_indices; | |
| 96 | |||
| 97 | 369 | Scalar r = shape.radius; | |
| 98 | Scalar phi, phid; | ||
| 99 | 369 | const Scalar pi = boost::math::constants::pi<Scalar>(); | |
| 100 | 369 | phid = pi * 2 / Scalar(seg); | |
| 101 | 369 | phi = 0; | |
| 102 | |||
| 103 | Scalar theta, thetad; | ||
| 104 | 369 | thetad = pi / Scalar(ring + 1); | |
| 105 | 369 | theta = 0; | |
| 106 | |||
| 107 |
2/2✓ Branch 0 taken 5887 times.
✓ Branch 1 taken 369 times.
|
6256 | for (unsigned int i = 0; i < ring; ++i) { |
| 108 | 5887 | Scalar theta_ = theta + thetad * Scalar(i + 1); | |
| 109 |
2/2✓ Branch 0 taken 94077 times.
✓ Branch 1 taken 5887 times.
|
99964 | for (unsigned int j = 0; j < seg; ++j) { |
| 110 |
1/2✓ Branch 1 taken 94077 times.
✗ Branch 2 not taken.
|
94077 | points.push_back(Vec3s(r * sin(theta_) * cos(phi + Scalar(j) * phid), |
| 111 | ✗ | r * sin(theta_) * sin(phi + Scalar(j) * phid), | |
| 112 |
1/2✓ Branch 1 taken 94077 times.
✗ Branch 2 not taken.
|
188154 | r * cos(theta_))); |
| 113 | } | ||
| 114 | } | ||
| 115 |
2/4✓ Branch 1 taken 369 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 369 times.
✗ Branch 5 not taken.
|
369 | points.push_back(Vec3s(0, 0, r)); |
| 116 |
2/4✓ Branch 1 taken 369 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 369 times.
✗ Branch 5 not taken.
|
369 | points.push_back(Vec3s(0, 0, -r)); |
| 117 | |||
| 118 |
2/2✓ Branch 0 taken 5518 times.
✓ Branch 1 taken 369 times.
|
5887 | for (unsigned int i = 0; i < ring - 1; ++i) { |
| 119 |
2/2✓ Branch 0 taken 88190 times.
✓ Branch 1 taken 5518 times.
|
93708 | for (unsigned int j = 0; j < seg; ++j) { |
| 120 | unsigned int a, b, c, d; | ||
| 121 | 88190 | a = i * seg + j; | |
| 122 |
2/2✓ Branch 0 taken 5518 times.
✓ Branch 1 taken 82672 times.
|
88190 | b = (j == seg - 1) ? (i * seg) : (i * seg + j + 1); |
| 123 | 88190 | c = (i + 1) * seg + j; | |
| 124 |
2/2✓ Branch 0 taken 5518 times.
✓ Branch 1 taken 82672 times.
|
88190 | d = (j == seg - 1) ? ((i + 1) * seg) : ((i + 1) * seg + j + 1); |
| 125 |
1/2✓ Branch 2 taken 88190 times.
✗ Branch 3 not taken.
|
88190 | tri_indices.push_back(Triangle32(a, c, b)); |
| 126 |
1/2✓ Branch 2 taken 88190 times.
✗ Branch 3 not taken.
|
88190 | tri_indices.push_back(Triangle32(b, c, d)); |
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 |
2/2✓ Branch 0 taken 5887 times.
✓ Branch 1 taken 369 times.
|
6256 | for (unsigned int j = 0; j < seg; ++j) { |
| 131 | unsigned int a, b; | ||
| 132 | 5887 | a = j; | |
| 133 |
2/2✓ Branch 0 taken 5518 times.
✓ Branch 1 taken 369 times.
|
5887 | b = (j == seg - 1) ? 0 : (j + 1); |
| 134 |
1/2✓ Branch 2 taken 5887 times.
✗ Branch 3 not taken.
|
5887 | tri_indices.push_back(Triangle32(ring * seg, a, b)); |
| 135 | |||
| 136 | 5887 | a = (ring - 1) * seg + j; | |
| 137 |
2/2✓ Branch 0 taken 369 times.
✓ Branch 1 taken 5518 times.
|
5887 | b = (j == seg - 1) ? (ring - 1) * seg : ((ring - 1) * seg + j + 1); |
| 138 |
1/2✓ Branch 2 taken 5887 times.
✗ Branch 3 not taken.
|
5887 | tri_indices.push_back(Triangle32(a, ring * seg + 1, b)); |
| 139 | } | ||
| 140 | |||
| 141 |
2/2✓ Branch 1 taken 94815 times.
✓ Branch 2 taken 369 times.
|
95184 | for (unsigned int i = 0; i < points.size(); ++i) { |
| 142 |
1/2✓ Branch 2 taken 94815 times.
✗ Branch 3 not taken.
|
94815 | points[i] = pose.transform(points[i]); |
| 143 | } | ||
| 144 | |||
| 145 |
1/2✓ Branch 1 taken 369 times.
✗ Branch 2 not taken.
|
369 | model.beginModel(); |
| 146 |
1/2✓ Branch 1 taken 369 times.
✗ Branch 2 not taken.
|
369 | model.addSubModel(points, tri_indices); |
| 147 |
1/2✓ Branch 1 taken 369 times.
✗ Branch 2 not taken.
|
369 | model.endModel(); |
| 148 |
1/2✓ Branch 1 taken 369 times.
✗ Branch 2 not taken.
|
369 | model.computeLocalAABB(); |
| 149 | 369 | } | |
| 150 | |||
| 151 | /// @brief Generate BVH model from sphere | ||
| 152 | /// The difference between generateBVHModel is that it gives the number of | ||
| 153 | /// triangles faces N for a sphere with unit radius. For sphere of radius r, | ||
| 154 | /// then the number of triangles is r * r * N so that the area represented by a | ||
| 155 | /// single triangle is approximately the same.s | ||
| 156 | template <typename BV> | ||
| 157 | 1 | void generateBVHModel(BVHModel<BV>& model, const Sphere& shape, | |
| 158 | const Transform3s& pose, | ||
| 159 | unsigned int n_faces_for_unit_sphere) { | ||
| 160 | 1 | Scalar r = shape.radius; | |
| 161 | 1 | Scalar n_low_bound = | |
| 162 | 1 | std::sqrt((Scalar)n_faces_for_unit_sphere / Scalar(2.)) * r * r; | |
| 163 | 1 | unsigned int ring = (unsigned int)ceil(n_low_bound); | |
| 164 | 1 | unsigned int seg = (unsigned int)ceil(n_low_bound); | |
| 165 | |||
| 166 | 1 | generateBVHModel(model, shape, pose, seg, ring); | |
| 167 | 1 | } | |
| 168 | |||
| 169 | /// @brief Generate BVH model from cylinder, given the number of segments along | ||
| 170 | /// circle and the number of segments along axis. | ||
| 171 | template <typename BV> | ||
| 172 | 369 | void generateBVHModel(BVHModel<BV>& model, const Cylinder& shape, | |
| 173 | const Transform3s& pose, unsigned int tot, | ||
| 174 | unsigned int h_num) { | ||
| 175 | 369 | std::vector<Vec3s> points; | |
| 176 | 369 | std::vector<Triangle32> tri_indices; | |
| 177 | |||
| 178 | 369 | Scalar r = shape.radius; | |
| 179 | 369 | Scalar h = shape.halfLength; | |
| 180 | Scalar phi, phid; | ||
| 181 | 369 | const Scalar pi = boost::math::constants::pi<Scalar>(); | |
| 182 | 369 | phid = pi * 2 / Scalar(tot); | |
| 183 | 369 | phi = 0; | |
| 184 | |||
| 185 | 369 | Scalar hd = 2 * h / Scalar(h_num); | |
| 186 | |||
| 187 |
2/2✓ Branch 0 taken 5932 times.
✓ Branch 1 taken 369 times.
|
6301 | for (unsigned int i = 0; i < tot; ++i) |
| 188 |
1/2✓ Branch 1 taken 5932 times.
✗ Branch 2 not taken.
|
5932 | points.push_back(Vec3s(r * cos(phi + phid * Scalar(i)), |
| 189 |
1/2✓ Branch 1 taken 5932 times.
✗ Branch 2 not taken.
|
11864 | r * sin(phi + phid * Scalar(i)), h)); |
| 190 | |||
| 191 |
2/2✓ Branch 0 taken 5521 times.
✓ Branch 1 taken 369 times.
|
5890 | for (unsigned int i = 0; i < h_num - 1; ++i) { |
| 192 |
2/2✓ Branch 0 taken 88520 times.
✓ Branch 1 taken 5521 times.
|
94041 | for (unsigned int j = 0; j < tot; ++j) { |
| 193 |
1/2✓ Branch 1 taken 88520 times.
✗ Branch 2 not taken.
|
88520 | points.push_back(Vec3s(r * cos(phi + phid * Scalar(j)), |
| 194 | ✗ | r * sin(phi + phid * Scalar(j)), | |
| 195 |
1/2✓ Branch 1 taken 88520 times.
✗ Branch 2 not taken.
|
177040 | h - Scalar(i + 1) * hd)); |
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 |
2/2✓ Branch 0 taken 5932 times.
✓ Branch 1 taken 369 times.
|
6301 | for (unsigned int i = 0; i < tot; ++i) |
| 200 |
1/2✓ Branch 1 taken 5932 times.
✗ Branch 2 not taken.
|
5932 | points.push_back(Vec3s(r * cos(phi + phid * Scalar(i)), |
| 201 |
1/2✓ Branch 1 taken 5932 times.
✗ Branch 2 not taken.
|
11864 | r * sin(phi + phid * Scalar(i)), -h)); |
| 202 | |||
| 203 |
2/4✓ Branch 1 taken 369 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 369 times.
✗ Branch 5 not taken.
|
369 | points.push_back(Vec3s(0, 0, h)); |
| 204 |
2/4✓ Branch 1 taken 369 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 369 times.
✗ Branch 5 not taken.
|
369 | points.push_back(Vec3s(0, 0, -h)); |
| 205 | |||
| 206 |
2/2✓ Branch 0 taken 5932 times.
✓ Branch 1 taken 369 times.
|
6301 | for (unsigned int i = 0; i < tot; ++i) { |
| 207 |
2/2✓ Branch 0 taken 5563 times.
✓ Branch 1 taken 369 times.
|
5932 | Triangle32 tmp((h_num + 1) * tot, i, ((i == tot - 1) ? 0 : (i + 1))); |
| 208 |
1/2✓ Branch 1 taken 5932 times.
✗ Branch 2 not taken.
|
5932 | tri_indices.push_back(tmp); |
| 209 | } | ||
| 210 | |||
| 211 |
2/2✓ Branch 0 taken 5932 times.
✓ Branch 1 taken 369 times.
|
6301 | for (unsigned int i = 0; i < tot; ++i) { |
| 212 | 5932 | Triangle32 tmp((h_num + 1) * tot + 1, | |
| 213 | 5932 | h_num * tot + ((i == tot - 1) ? 0 : (i + 1)), | |
| 214 |
2/2✓ Branch 0 taken 5563 times.
✓ Branch 1 taken 369 times.
|
5932 | h_num * tot + i); |
| 215 |
1/2✓ Branch 1 taken 5932 times.
✗ Branch 2 not taken.
|
5932 | tri_indices.push_back(tmp); |
| 216 | } | ||
| 217 | |||
| 218 |
2/2✓ Branch 0 taken 5890 times.
✓ Branch 1 taken 369 times.
|
6259 | for (unsigned int i = 0; i < h_num; ++i) { |
| 219 |
2/2✓ Branch 0 taken 94452 times.
✓ Branch 1 taken 5890 times.
|
100342 | for (unsigned int j = 0; j < tot; ++j) { |
| 220 | unsigned int a, b, c, d; | ||
| 221 | 94452 | a = j; | |
| 222 |
2/2✓ Branch 0 taken 88562 times.
✓ Branch 1 taken 5890 times.
|
94452 | b = (j == tot - 1) ? 0 : (j + 1); |
| 223 | 94452 | c = j + tot; | |
| 224 |
2/2✓ Branch 0 taken 88562 times.
✓ Branch 1 taken 5890 times.
|
94452 | d = (j == tot - 1) ? tot : (j + 1 + tot); |
| 225 | |||
| 226 | 94452 | unsigned int start = i * tot; | |
| 227 |
1/2✓ Branch 2 taken 94452 times.
✗ Branch 3 not taken.
|
94452 | tri_indices.push_back(Triangle32(start + b, start + a, start + c)); |
| 228 |
1/2✓ Branch 2 taken 94452 times.
✗ Branch 3 not taken.
|
94452 | tri_indices.push_back(Triangle32(start + b, start + c, start + d)); |
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 |
2/2✓ Branch 1 taken 101122 times.
✓ Branch 2 taken 369 times.
|
101491 | for (unsigned int i = 0; i < points.size(); ++i) { |
| 233 |
1/2✓ Branch 2 taken 101122 times.
✗ Branch 3 not taken.
|
101122 | points[i] = pose.transform(points[i]); |
| 234 | } | ||
| 235 | |||
| 236 |
1/2✓ Branch 1 taken 369 times.
✗ Branch 2 not taken.
|
369 | model.beginModel(); |
| 237 |
1/2✓ Branch 1 taken 369 times.
✗ Branch 2 not taken.
|
369 | model.addSubModel(points, tri_indices); |
| 238 |
1/2✓ Branch 1 taken 369 times.
✗ Branch 2 not taken.
|
369 | model.endModel(); |
| 239 |
1/2✓ Branch 1 taken 369 times.
✗ Branch 2 not taken.
|
369 | model.computeLocalAABB(); |
| 240 | 369 | } | |
| 241 | |||
| 242 | /// @brief Generate BVH model from cylinder | ||
| 243 | /// Difference from generateBVHModel: is that it gives the circle split number | ||
| 244 | /// tot for a cylinder with unit radius. For cylinder with larger radius, the | ||
| 245 | /// number of circle split number is r * tot. | ||
| 246 | template <typename BV> | ||
| 247 | 1 | void generateBVHModel(BVHModel<BV>& model, const Cylinder& shape, | |
| 248 | const Transform3s& pose, | ||
| 249 | unsigned int tot_for_unit_cylinder) { | ||
| 250 | 1 | Scalar r = shape.radius; | |
| 251 | 1 | Scalar h = 2 * shape.halfLength; | |
| 252 | |||
| 253 | 1 | const Scalar pi = boost::math::constants::pi<Scalar>(); | |
| 254 | 1 | unsigned int tot = (unsigned int)(Scalar(tot_for_unit_cylinder) * r); | |
| 255 | 1 | Scalar phid = pi * 2 / Scalar(tot); | |
| 256 | |||
| 257 | 1 | Scalar circle_edge = phid * r; | |
| 258 | 1 | unsigned int h_num = (unsigned int)ceil(h / circle_edge); | |
| 259 | |||
| 260 | 1 | generateBVHModel(model, shape, pose, tot, h_num); | |
| 261 | 1 | } | |
| 262 | |||
| 263 | /// @brief Generate BVH model from cone, given the number of segments along | ||
| 264 | /// circle and the number of segments along axis. | ||
| 265 | template <typename BV> | ||
| 266 | 2 | void generateBVHModel(BVHModel<BV>& model, const Cone& shape, | |
| 267 | const Transform3s& pose, unsigned int tot, | ||
| 268 | unsigned int h_num) { | ||
| 269 | 2 | std::vector<Vec3s> points; | |
| 270 | 2 | std::vector<Triangle32> tri_indices; | |
| 271 | |||
| 272 | 2 | Scalar r = shape.radius; | |
| 273 | 2 | Scalar h = shape.halfLength; | |
| 274 | |||
| 275 | Scalar phi, phid; | ||
| 276 | 2 | const Scalar pi = boost::math::constants::pi<Scalar>(); | |
| 277 | 2 | phid = pi * 2 / Scalar(tot); | |
| 278 | 2 | phi = 0; | |
| 279 | |||
| 280 | 2 | Scalar hd = 2 * h / Scalar(h_num); | |
| 281 | |||
| 282 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | for (unsigned int i = 0; i < h_num - 1; ++i) { |
| 283 | 16 | Scalar h_i = h - Scalar(i + 1) * hd; | |
| 284 | 16 | Scalar rh = r * (Scalar(0.5) - h_i / h / 2); | |
| 285 |
2/2✓ Branch 0 taken 440 times.
✓ Branch 1 taken 16 times.
|
456 | for (unsigned int j = 0; j < tot; ++j) { |
| 286 |
1/2✓ Branch 1 taken 440 times.
✗ Branch 2 not taken.
|
440 | points.push_back(Vec3s(rh * cos(phi + phid * Scalar(j)), |
| 287 |
1/2✓ Branch 1 taken 440 times.
✗ Branch 2 not taken.
|
880 | rh * sin(phi + phid * Scalar(j)), h_i)); |
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2 times.
|
62 | for (unsigned int i = 0; i < tot; ++i) |
| 292 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | points.push_back(Vec3s(r * cos(phi + phid * Scalar(i)), |
| 293 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
120 | r * sin(phi + phid * Scalar(i)), -h)); |
| 294 | |||
| 295 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | points.push_back(Vec3s(0, 0, h)); |
| 296 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | points.push_back(Vec3s(0, 0, -h)); |
| 297 | |||
| 298 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2 times.
|
62 | for (unsigned int i = 0; i < tot; ++i) { |
| 299 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 2 times.
|
60 | Triangle32 tmp(h_num * tot, i, (i == tot - 1) ? 0 : (i + 1)); |
| 300 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | tri_indices.push_back(tmp); |
| 301 | } | ||
| 302 | |||
| 303 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2 times.
|
62 | for (unsigned int i = 0; i < tot; ++i) { |
| 304 | 60 | Triangle32 tmp(h_num * tot + 1, | |
| 305 | 60 | (h_num - 1) * tot + ((i == tot - 1) ? 0 : (i + 1)), | |
| 306 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 2 times.
|
60 | (h_num - 1) * tot + i); |
| 307 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | tri_indices.push_back(tmp); |
| 308 | } | ||
| 309 | |||
| 310 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | for (unsigned int i = 0; i < h_num - 1; ++i) { |
| 311 |
2/2✓ Branch 0 taken 440 times.
✓ Branch 1 taken 16 times.
|
456 | for (unsigned int j = 0; j < tot; ++j) { |
| 312 | unsigned int a, b, c, d; | ||
| 313 | 440 | a = j; | |
| 314 |
2/2✓ Branch 0 taken 424 times.
✓ Branch 1 taken 16 times.
|
440 | b = (j == tot - 1) ? 0 : (j + 1); |
| 315 | 440 | c = j + tot; | |
| 316 |
2/2✓ Branch 0 taken 424 times.
✓ Branch 1 taken 16 times.
|
440 | d = (j == tot - 1) ? tot : (j + 1 + tot); |
| 317 | |||
| 318 | 440 | unsigned int start = i * tot; | |
| 319 |
1/2✓ Branch 2 taken 440 times.
✗ Branch 3 not taken.
|
440 | tri_indices.push_back(Triangle32(start + b, start + a, start + c)); |
| 320 |
1/2✓ Branch 2 taken 440 times.
✗ Branch 3 not taken.
|
440 | tri_indices.push_back(Triangle32(start + b, start + c, start + d)); |
| 321 | } | ||
| 322 | } | ||
| 323 | |||
| 324 |
2/2✓ Branch 1 taken 504 times.
✓ Branch 2 taken 2 times.
|
506 | for (unsigned int i = 0; i < points.size(); ++i) { |
| 325 |
1/2✓ Branch 2 taken 504 times.
✗ Branch 3 not taken.
|
504 | points[i] = pose.transform(points[i]); |
| 326 | } | ||
| 327 | |||
| 328 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | model.beginModel(); |
| 329 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | model.addSubModel(points, tri_indices); |
| 330 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | model.endModel(); |
| 331 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | model.computeLocalAABB(); |
| 332 | 2 | } | |
| 333 | |||
| 334 | /// @brief Generate BVH model from cone | ||
| 335 | /// Difference from generateBVHModel: is that it gives the circle split number | ||
| 336 | /// tot for a cylinder with unit radius. For cone with larger radius, the number | ||
| 337 | /// of circle split number is r * tot. | ||
| 338 | template <typename BV> | ||
| 339 | 1 | void generateBVHModel(BVHModel<BV>& model, const Cone& shape, | |
| 340 | const Transform3s& pose, unsigned int tot_for_unit_cone) { | ||
| 341 | 1 | Scalar r = shape.radius; | |
| 342 | 1 | Scalar h = 2 * shape.halfLength; | |
| 343 | |||
| 344 | 1 | const Scalar pi = boost::math::constants::pi<Scalar>(); | |
| 345 | 1 | unsigned int tot = (unsigned int)(Scalar(tot_for_unit_cone) * r); | |
| 346 | 1 | Scalar phid = pi * 2 / Scalar(tot); | |
| 347 | |||
| 348 | 1 | Scalar circle_edge = phid * r; | |
| 349 | 1 | unsigned int h_num = (unsigned int)ceil(h / circle_edge); | |
| 350 | |||
| 351 | 1 | generateBVHModel(model, shape, pose, tot, h_num); | |
| 352 | 1 | } | |
| 353 | |||
| 354 | } // namespace coal | ||
| 355 | |||
| 356 | #endif | ||
| 357 |