File: LineSegmentShape.cpp

package info (click to toggle)
dart 6.12.1%2Bdfsg4-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 57,000 kB
  • sloc: cpp: 269,461; python: 3,911; xml: 1,273; sh: 404; makefile: 30
file content (399 lines) | stat: -rw-r--r-- 12,919 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * Copyright (c) 2011-2021, The DART development contributors
 * All rights reserved.
 *
 * The list of contributors can be found at:
 *   https://github.com/dartsim/dart/blob/master/LICENSE
 *
 * This file is provided under the following "BSD-style" License:
 *   Redistribution and use in source and binary forms, with or
 *   without modification, are permitted provided that the following
 *   conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 *   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 *   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 *   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *   POSSIBILITY OF SUCH DAMAGE.
 */

#include "dart/dynamics/LineSegmentShape.hpp"

#include "dart/common/Console.hpp"
#include "dart/math/Geometry.hpp"

namespace dart {
namespace dynamics {

//==============================================================================
const Eigen::Vector3d LineSegmentShape::mDummyVertex = Eigen::Vector3d::Zero();

//==============================================================================
LineSegmentShape::LineSegmentShape(float _thickness)
  : Shape(LINE_SEGMENT), mThickness(_thickness)
{
  if (_thickness <= 0.0f)
  {
    dtwarn << "[LineSegmentShape::LineSegmentShape] Attempting to set "
           << "non-positive thickness. We set the thickness to 1.0f instead."
           << std::endl;
    mThickness = 1.0f;
  }

  mVariance = DYNAMIC_VERTICES;
}

//==============================================================================
LineSegmentShape::LineSegmentShape(
    const Eigen::Vector3d& _v1, const Eigen::Vector3d& _v2, float _thickness)
  : Shape(), mThickness(_thickness)
{
  if (_thickness <= 0.0f)
  {
    dtwarn << "[LineSegmentShape::LineSegmentShape] Attempting to set "
           << "non-positive thickness. We set the thickness to 1.0f instead."
           << std::endl;
    mThickness = 1.0f;
  }

  addVertex(_v1);
  addVertex(_v2);
  mVariance = DYNAMIC_VERTICES;
}

//==============================================================================
const std::string& LineSegmentShape::getType() const
{
  return getStaticType();
}

//==============================================================================
const std::string& LineSegmentShape::getStaticType()
{
  static const std::string type("LineSegmentShape");
  return type;
}

//==============================================================================
void LineSegmentShape::setThickness(float _thickness)
{
  if (_thickness <= 0.0f)
  {
    dtwarn << "[LineSegmentShape::setThickness] Attempting to set non-positive "
           << "thickness. We set the thickness to 1.0f instead." << std::endl;
    mThickness = 1.0f;
    return;
  }

  mThickness = _thickness;
}

//==============================================================================
float LineSegmentShape::getThickness() const
{
  return mThickness;
}

//==============================================================================
std::size_t LineSegmentShape::addVertex(const Eigen::Vector3d& _v)
{
  std::size_t parent = mVertices.size();
  if (parent > 0)
    return addVertex(_v, parent - 1);

  mVertices.push_back(_v);
  return 0;
}

//==============================================================================
std::size_t LineSegmentShape::addVertex(
    const Eigen::Vector3d& _v, std::size_t _parent)
{
  std::size_t index = mVertices.size();
  mVertices.push_back(_v);

  if (_parent > mVertices.size())
  {
    if (mVertices.size() == 0)
      dtwarn << "[LineSegmentShape::addVertex(const Eigen::Vector3d&, "
                "std::size_t)] "
             << "Attempting to add a vertex to be a child of vertex #"
             << _parent << ", but no vertices exist yet. No connection will be "
             << "created for the new vertex yet.\n";
    else
      dtwarn << "[LineSegmentShape::addVertex(const Eigen::Vector3d&, "
                "std::size_t)] "
             << "Attempting to add a vertex to be a child of vertex #"
             << _parent << ", but the vertex indices only go up to "
             << mVertices.size() - 1
             << ". No connection will be created for the "
             << "new vertex yet.\n";
  }
  else
  {
    mConnections.push_back(Eigen::Vector2i(_parent, index));
  }

  return index;
}

//==============================================================================
void LineSegmentShape::removeVertex(std::size_t _idx)
{
  if (_idx >= mVertices.size())
  {
    if (mVertices.size() == 0)
      dtwarn << "[LineSegmentShape::removeVertex] Attempting to remove vertex #"
             << _idx << ", but "
             << "this LineSegmentShape contains no vertices. "
             << "No vertex will be removed.\n";
    else
      dtwarn << "[LineSegmentShape::removeVertex] Attempting to remove vertex #"
             << _idx << ", but "
             << "vertex indices only go up to #" << mVertices.size() - 1 << ". "
             << "No vertex will be removed.\n";

    return;
  }

  mVertices.erase(mVertices.begin() + _idx);
}

//==============================================================================
void LineSegmentShape::setVertex(std::size_t _idx, const Eigen::Vector3d& _v)
{
  if (_idx >= mVertices.size())
  {
    if (mVertices.size() == 0)
      dtwarn << "[LineSegmentShape::setVertex] Attempting to set vertex #"
             << _idx << ", but "
             << "no vertices exist in this LineSegmentShape yet.\n";
    else
      dtwarn << "[LineSegmentShape::setVertex] Attempting to set vertex #"
             << _idx << ", but "
             << "the vertices of this LineSegmentShape only go up to #"
             << mVertices.size() - 1 << ".\n";

    return;
  }
  mVertices[_idx] = _v;
}

//==============================================================================
const Eigen::Vector3d& LineSegmentShape::getVertex(std::size_t _idx) const
{
  if (_idx < mVertices.size())
    return mVertices[_idx];

  if (mVertices.empty())
  {
    dtwarn << "[LineSegmentShape::getVertex] Requested vertex #" << _idx
           << ", but no vertices currently exist in this LineSegmentShape\n";
  }
  else
  {
    dtwarn << "[LineSegmentShape::getVertex] Requested vertex #" << _idx
           << ", but vertex indices currently only go up to "
           << mVertices.size() - 1 << "\n";
  }

  return mDummyVertex;
}

//==============================================================================
const std::vector<Eigen::Vector3d>& LineSegmentShape::getVertices() const
{
  return mVertices;
}

//==============================================================================
void LineSegmentShape::addConnection(std::size_t _idx1, std::size_t _idx2)
{
  if (_idx1 >= mVertices.size() || _idx2 >= mVertices.size())
  {
    if (mVertices.size() == 0)
      dtwarn << "[LineSegmentShape::createConnection] Attempted to create a "
             << "connection between vertex #" << _idx1 << " and vertex #"
             << _idx2
             << ", but no vertices exist for this LineSegmentShape yet. "
             << "No connection will be made for these non-existent vertices.\n";
    else
      dtwarn << "[LineSegmentShape::createConnection] Attempted to create a "
             << "connection between vertex #" << _idx1 << " and vertex #"
             << _idx2 << ", but the vertices only go up to #"
             << mVertices.size() << ". "
             << "No connection will be made for these non-existent vertices.\n";

    return;
  }

  mConnections.push_back(Eigen::Vector2i(_idx1, _idx2));
}

//==============================================================================
void LineSegmentShape::removeConnection(
    std::size_t _vertexIdx1, std::size_t _vertexIdx2)
{
  // Search through all connections to remove any that match the request
  common::aligned_vector<Eigen::Vector2i>::iterator it = mConnections.begin();
  while (it != mConnections.end())
  {
    const Eigen::Vector2i c = (*it);
    if ((c[0] == (int)_vertexIdx1 && c[1] == (int)_vertexIdx2)
        || (c[0] == (int)_vertexIdx2 && c[1] == (int)_vertexIdx1))
    {
      // Erase this iterator, but not before stepping it forward to the next
      // iterator in the sequence.
      mConnections.erase(it++);
    }
    else
      ++it;
  }
}

//==============================================================================
void LineSegmentShape::removeConnection(std::size_t _connectionIdx)
{
  if (_connectionIdx >= mConnections.size())
  {
    if (mConnections.size() == 0)
      dtwarn
          << "[LineSegmentShape::removeConnection(std::size_t)] Attempting to "
          << "remove connection #" << _connectionIdx << ", but "
          << "no connections exist yet. "
          << "No connection will be removed.\n";
    else
      dtwarn
          << "[LineSegmentShape::removeConnection(std::size_t)] Attempting to "
          << "remove connection #" << _connectionIdx << ", but "
          << "connection indices only go up to #" << mConnections.size() - 1
          << ". "
          << "No connection will be removed.\n";

    return;
  }

  mConnections.erase(mConnections.begin() + _connectionIdx);
}

//==============================================================================
const common::aligned_vector<Eigen::Vector2i>&
LineSegmentShape::getConnections() const
{
  return mConnections;
}

//==============================================================================
Eigen::Matrix3d LineSegmentShape::computeInertia(double _mass) const
{
  Eigen::Matrix3d inertia = Eigen::Matrix3d::Zero();

  double totalLength = 0;
  for (const Eigen::Vector2i& c : mConnections)
  {
    const Eigen::Vector3d& v0 = mVertices[c[0]];
    const Eigen::Vector3d& v1 = mVertices[c[1]];

    totalLength += (v1 - v0).norm();
  }

  for (const Eigen::Vector2i& c : mConnections)
  {
    const Eigen::Vector3d& v0 = mVertices[c[0]];
    const Eigen::Vector3d& v1 = mVertices[c[1]];

    double radius = 1e-6;
    double height = (v1 - v0).norm();

    double partialMass = _mass * height / totalLength;

    Eigen::Matrix3d partialInertia = Eigen::Matrix3d::Zero();
    partialInertia(0, 0)
        = partialMass * (3.0 * radius * radius + height * height) / 12.0;
    partialInertia(1, 1) = partialInertia(0, 0);
    partialInertia(2, 2) = 0.5 * _mass * radius * radius;

    Eigen::Vector3d v = v1 - v0;
    Eigen::Vector3d axis;
    double angle;
    if (v.norm() == 0)
    {
      angle = 0;
      axis = Eigen::Vector3d::UnitX();
    }
    else
    {
      v.normalize();
      Eigen::Vector3d axis = Eigen::Vector3d::UnitZ().cross(v);
      if (axis.norm() == 0)
      {
        angle = 0;
        axis = Eigen::Vector3d::UnitX();
      }
      else
      {
        axis.normalize();
        angle = acos(Eigen::Vector3d::UnitZ().dot(v));
      }
    }

    Eigen::AngleAxisd R(angle, axis);
    Eigen::Vector3d center = (v1 + v0) / 2.0;
    inertia += R.matrix()
               * math::parallelAxisTheorem(partialInertia, center, partialMass)
               * R.matrix().transpose();
  }

  return inertia;
}

//==============================================================================
void LineSegmentShape::updateBoundingBox() const
{
  if (mVertices.empty())
  {
    mBoundingBox.setMin(Eigen::Vector3d::Zero());
    mBoundingBox.setMax(Eigen::Vector3d::Zero());
    mIsBoundingBoxDirty = false;
    return;
  }

  Eigen::Vector3d min
      = Eigen::Vector3d::Constant(std::numeric_limits<double>::infinity());
  Eigen::Vector3d max
      = Eigen::Vector3d::Constant(-std::numeric_limits<double>::infinity());

  for (const auto& vertex : mVertices)
  {
    min = min.cwiseMin(vertex);
    max = max.cwiseMax(vertex);
  }

  mBoundingBox.setMin(min);
  mBoundingBox.setMax(max);

  mIsBoundingBoxDirty = false;
}

//==============================================================================
void LineSegmentShape::updateVolume() const
{
  mVolume = 0.0;
  mIsVolumeDirty = false;
}

} // namespace dynamics
} // namespace dart