line_segment.py
Go to the documentation of this file.
1 # Copyright (c) 2016-2019 The UUV Simulator Authors.
2 # All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 import numpy as np
16 
17 
18 class LineSegment(object):
19  """Line segment class.
20 
21  > *Input arguments*
22 
23  * `p_init` (*type:* `list` or `numpy.array`): Line's starting point
24  * `p_target` (*type:* `list` or `numpy.array`): Line's ending point
25  """
26  def __init__(self, p_init, p_target):
27  if type(p_init) == list:
28  assert len(p_init) == 3, 'Initial segment point must have 3 elements'
29  self._p_init = np.array(p_init)
30  elif type(p_init) == np.ndarray:
31  assert p_init.size == 3, 'Initial segment point must have 3 elements'
32  self._p_init = p_init
33  else:
34  raise TypeError('Initial point is neither a list or an array')
35 
36  if type(p_target) == list:
37  assert len(p_target) == 3, 'Final segment point must have 3 elements'
38  self._p_target = np.array(p_target)
39  elif type(p_target) == np.ndarray:
40  assert p_target.size == 3, 'Final segment point must have 3 elements'
41  self._p_target = p_target
42  else:
43  raise TypeError('Final point is neither a list or an array')
44 
45  assert not np.array_equal(self._p_init, self._p_target), 'Initial and final points are equal'
46 
47  def interpolate(self, u):
48  """Interpolate the Bezier curve using the input parametric variable `u`.
49 
50  > *Input arguments*
51 
52  * `u` (*type:* `float`): Curve parametric input in the interval `[0, 1]`
53 
54  > *Returns*
55 
56  `numpy.array`: 3D point from the Bezier curve
57  """
58  u = max(u, 0)
59  u = min(u, 1)
60  return (1 - u) * self._p_init + u * self._p_target
61 
62  def get_derivative(self, *args):
63  """Compute the derivative of the line segment.
64 
65  > *Returns*
66 
67  `numpy.array`: 3D derivative value from the Bezier curve
68  """
69  return self._p_target - self._p_init
70 
71  def get_length(self):
72  """Get length of the Bezier curve segment.
73 
74  > *Returns*
75 
76  `float`: Length of the curve
77  """
78  return np.linalg.norm(self._p_target - self._p_init)
79 
80  def get_tangent(self):
81  """Compute tangent vector.
82 
83  > *Returns*
84 
85  `numpy.array`: Tangent vector
86  """
87  return (self._p_target - self._p_init) / self.get_length()
88 
89 


uuv_trajectory_control
Author(s):
autogenerated on Thu Jun 18 2020 03:28:42