UsTK : Ultrasound ToolKit  version 2.0.1 under development (2024-05-21)
usNeedleModelBaseTip.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ustk software.
4  * Copyright (C) 2016 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ustk with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * This software was developed at:
17  * Inria Rennes - Bretagne Atlantique
18  * Campus Universitaire de Beaulieu
19  * 35042 Rennes Cedex
20  * France
21  *
22  * If you have questions regarding the use of this file, please contact
23  * Inria at ustk@inria.fr
24  *
25  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  *
28  * Author:
29  * Jason Chevrie
30  *
31  *****************************************************************************/
32 
33 #include <visp3/ustk_needle_modeling/usNeedleModelBaseTip.h>
34 
35 #include <visp3/core/vpException.h>
36 #include <visp3/core/vpExponentialMap.h>
37 
38 usNeedleModelBaseTip::usNeedleModelBaseTip() : m_basePose(), m_worldMbase(), m_tipPose(), m_worldMtip() { }
39 
41  : m_basePose(needle.m_basePose), m_worldMbase(needle.m_worldMbase), m_tipPose(needle.m_tipPose),
42  m_worldMtip(needle.m_worldMtip)
43 { }
44 
46 
48 {
49  m_basePose = needle.m_basePose;
50  m_worldMbase = needle.m_worldMbase;
51  m_tipPose = needle.m_tipPose;
52  m_worldMtip = needle.m_worldMtip;
53 
54  return (*this);
55 }
56 
58 
59 vpPoseVector usNeedleModelBaseTip::getBasePose() const { return m_basePose; }
60 
61 vpHomogeneousMatrix usNeedleModelBaseTip::getWorldMbase() const { return m_worldMbase; }
62 
63 vpColVector usNeedleModelBaseTip::getBasePosition() const { return vpColVector(m_basePose.getTranslationVector()); }
64 
66 {
67  vpColVector v(3);
68 
69  v[0] = m_worldMbase[0][2];
70  v[1] = m_worldMbase[1][2];
71  v[2] = m_worldMbase[2][2];
72 
73  return v;
74 }
75 
76 vpPoseVector usNeedleModelBaseTip::getTipPose() const { return m_tipPose; }
77 vpHomogeneousMatrix usNeedleModelBaseTip::getWorldMtip() const { return m_worldMtip; }
78 
79 vpColVector usNeedleModelBaseTip::getTipPosition() const { return vpColVector(m_tipPose.getTranslationVector()); }
80 
82 {
83  vpColVector v(3);
84 
85  v[0] = m_worldMtip[0][2];
86  v[1] = m_worldMtip[1][2];
87  v[2] = m_worldMtip[2][2];
88 
89  return v;
90 }
91 
92 void usNeedleModelBaseTip::setBasePose(double tx, double ty, double tz, double thetax, double thetay, double thetaz)
93 {
94  m_worldMbase.buildFrom(tx, ty, tz, thetax, thetay, thetaz);
95  m_basePose[0] = tx;
96  m_basePose[1] = ty;
97  m_basePose[2] = tz;
98  m_basePose[3] = thetax;
99  m_basePose[4] = thetay;
100  m_basePose[5] = thetaz;
101 }
102 
103 void usNeedleModelBaseTip::setBasePose(const vpPoseVector &pose)
104 {
105  m_basePose = pose;
106  m_worldMbase.buildFrom(m_basePose);
107 }
108 
109 void usNeedleModelBaseTip::setBasePose(const vpHomogeneousMatrix &Hpose)
110 {
111  m_worldMbase = Hpose;
112  m_basePose.buildFrom(m_worldMbase);
113 }
114 
115 void usNeedleModelBaseTip::setTipPose(double tx, double ty, double tz, double thetax, double thetay, double thetaz)
116 {
117  m_worldMtip.buildFrom(tx, ty, tz, thetax, thetay, thetaz);
118  m_tipPose[0] = tx;
119  m_tipPose[1] = ty;
120  m_tipPose[2] = tz;
121  m_tipPose[3] = thetax;
122  m_tipPose[4] = thetay;
123  m_tipPose[5] = thetaz;
124 }
125 
126 void usNeedleModelBaseTip::setTipPose(const vpPoseVector &pose)
127 {
128  m_tipPose = pose;
129  m_worldMtip.buildFrom(m_tipPose);
130 }
131 
132 void usNeedleModelBaseTip::setTipPose(const vpHomogeneousMatrix &Hpose)
133 {
134  m_worldMtip = Hpose;
135  m_tipPose.buildFrom(m_worldMtip);
136 }
137 
138 void usNeedleModelBaseTip::moveBase(const vpColVector &control, double time)
139 {
140  if (control.size() != 6)
141  throw vpException(
142  vpException::dimensionError,
143  "usNeedleModelBaseTip::moveBase(const vpColVector&, double): invalid control vector dimension, should be 6");
144  if (time <= std::numeric_limits<double>::epsilon())
145  return;
146  if (control.frobeniusNorm() <= std::numeric_limits<double>::epsilon())
147  return;
148 
149  vpHomogeneousMatrix Hmotion = vpExponentialMap::direct(control, time);
150 
151  this->moveBase(Hmotion);
152 }
153 
154 void usNeedleModelBaseTip::moveBase(double tx, double ty, double tz, double thetax, double thetay, double thetaz)
155 {
156  vpHomogeneousMatrix Hmotion(tx, ty, tz, thetax, thetay, thetaz);
157 
158  this->moveBase(Hmotion);
159 }
160 
161 void usNeedleModelBaseTip::moveBase(const vpPoseVector &pose)
162 {
163  vpHomogeneousMatrix Hmotion(pose);
164 
165  this->moveBase(Hmotion);
166 }
167 
168 void usNeedleModelBaseTip::moveBase(const vpColVector &v)
169 {
170  if (v.size() != 6)
171  throw vpException(vpException::dimensionError,
172  "usNeedleModelBaseTip::moveBase(const vpColVector): invalid vector dimension, should be 6");
173  vpHomogeneousMatrix Hmotion(v[0], v[1], v[2], v[3], v[4], v[5]);
174 
175  this->moveBase(Hmotion);
176 }
177 
178 void usNeedleModelBaseTip::moveBase(const vpHomogeneousMatrix &Hmotion)
179 {
180  // Move base
181  // Translation and rotation are expressed in base frame
182 
183  this->setBasePose(m_worldMbase * Hmotion);
184 }
185 
186 void usNeedleModelBaseTip::moveBaseWorldFrame(const vpColVector &control, double time)
187 {
188  if (control.size() != 6)
189  throw vpException(vpException::dimensionError, "usNeedleModelBaseTip::usNeedleModelBaseWorldFrame(const "
190  "vpColVector&, double): invalid control vector dimension, should be "
191  "6");
192  if (time <= std::numeric_limits<double>::epsilon())
193  return;
194  if (control.frobeniusNorm() <= std::numeric_limits<double>::epsilon())
195  return;
196 
197  vpHomogeneousMatrix Hmotion = vpExponentialMap::direct(control, time);
198 
199  this->moveBaseWorldFrame(Hmotion);
200 }
201 
202 void usNeedleModelBaseTip::moveBaseWorldFrame(double tx, double ty, double tz, double thetax, double thetay,
203  double thetaz)
204 {
205  vpHomogeneousMatrix Hmotion(tx, ty, tz, thetax, thetay, thetaz);
206 
207  this->moveBaseWorldFrame(Hmotion);
208 }
209 
210 void usNeedleModelBaseTip::moveBaseWorldFrame(const vpPoseVector &pose)
211 {
212  vpHomogeneousMatrix Hmotion(pose);
213 
214  this->moveBaseWorldFrame(Hmotion);
215 }
216 
218 {
219  if (v.size() != 6)
220  throw vpException(
221  vpException::dimensionError,
222  "usNeedleModelBaseTip::moveBaseWorldFrame(const vpColVector&): invalid vector dimension, should be 6");
223  vpHomogeneousMatrix Hmotion(v[0], v[1], v[2], v[3], v[4], v[5]);
224 
225  this->moveBaseWorldFrame(Hmotion);
226 }
227 
228 void usNeedleModelBaseTip::moveBaseWorldFrame(const vpHomogeneousMatrix &Hmotion)
229 {
230  // Move base
231  // Translation is expressed in world frame,
232  // Rotation is expressed in world frame but is done around the base position
233 
234  vpHomogeneousMatrix Hrotation0(m_worldMbase);
235 
236  Hrotation0[0][3] = 0;
237  Hrotation0[1][3] = 0;
238  Hrotation0[2][3] = 0;
239 
240  vpHomogeneousMatrix HmotionBaseFrame = Hrotation0.inverse() * Hmotion * Hrotation0;
241 
242  this->moveBase(HmotionBaseFrame);
243 }
244 
245 void usNeedleModelBaseTip::moveTip(const vpColVector &control, double time)
246 {
247  if (control.size() != 6)
248  throw vpException(
249  vpException::dimensionError,
250  "usNeedleModelBaseTip::moveTip(const vpColVector&, double): invalid control vector dimension, should be 6");
251  if (time <= std::numeric_limits<double>::epsilon())
252  return;
253  if (control.frobeniusNorm() <= std::numeric_limits<double>::epsilon())
254  return;
255 
256  vpHomogeneousMatrix Hmotion = vpExponentialMap::direct(control, time);
257 
258  this->moveTip(Hmotion);
259 }
260 
261 void usNeedleModelBaseTip::moveTip(double tx, double ty, double tz, double thetax, double thetay, double thetaz)
262 {
263  vpHomogeneousMatrix Hmotion(tx, ty, tz, thetax, thetay, thetaz);
264 
265  return this->moveTip(Hmotion);
266 }
267 
268 void usNeedleModelBaseTip::moveTip(const vpPoseVector &pose)
269 {
270  vpHomogeneousMatrix Hmotion(pose);
271 
272  return this->moveTip(Hmotion);
273 }
274 
275 void usNeedleModelBaseTip::moveTip(const vpColVector &v)
276 {
277  if (v.size() != 6)
278  throw vpException(vpException::dimensionError,
279  "usNeedleModelBaseTip::MoveTip(const vpColVector&): invalid vector dimension, should be 6");
280  vpHomogeneousMatrix Hmotion(v[0], v[1], v[2], v[3], v[4], v[5]);
281 
282  return this->moveTip(Hmotion);
283 }
284 
285 void usNeedleModelBaseTip::moveTip(const vpHomogeneousMatrix &Hmotion)
286 {
287  // Move base
288  // Translation and rotation are expressed in base frame
289 
290  return this->setTipPose(m_worldMtip * Hmotion);
291 }
292 
293 void usNeedleModelBaseTip::moveTipWorldFrame(const vpColVector &control, double time)
294 {
295  if (control.size() != 6)
296  throw vpException(vpException::dimensionError, "usNeedleModelBaseTip::moveTipWorldFrame(const vpColVector&, "
297  "double): invalid control vector dimension, should be 6");
298  if (time <= std::numeric_limits<double>::epsilon())
299  return;
300  if (control.frobeniusNorm() <= std::numeric_limits<double>::epsilon())
301  return;
302 
303  vpHomogeneousMatrix Hmotion = vpExponentialMap::direct(control, time);
304 
305  this->moveTipWorldFrame(Hmotion);
306 }
307 
308 void usNeedleModelBaseTip::moveTipWorldFrame(double tx, double ty, double tz, double thetax, double thetay,
309  double thetaz)
310 {
311  vpHomogeneousMatrix Hmotion(tx, ty, tz, thetax, thetay, thetaz);
312 
313  this->moveTipWorldFrame(Hmotion);
314 }
315 
316 void usNeedleModelBaseTip::moveTipWorldFrame(const vpPoseVector &pose)
317 {
318  vpHomogeneousMatrix Hmotion(pose);
319 
320  this->moveTipWorldFrame(Hmotion);
321 }
322 
323 void usNeedleModelBaseTip::moveTipWorldFrame(const vpColVector &v)
324 {
325  if (v.size() != 6)
326  throw vpException(
327  vpException::dimensionError,
328  "usNeedleModelBaseTip::moveTipWorldFrame(const vpColVector&): invalid vector dimension, should be 6");
329  vpHomogeneousMatrix Hmotion(v[0], v[1], v[2], v[3], v[4], v[5]);
330 
331  this->moveTipWorldFrame(Hmotion);
332 }
333 
334 void usNeedleModelBaseTip::moveTipWorldFrame(const vpHomogeneousMatrix &Hmotion)
335 {
336  // Move tip
337  // Translation is expressed in world frame,
338  // Rotation is expressed in world frame but is done around the tip position
339 
340  vpHomogeneousMatrix Hrotation0(m_worldMtip);
341 
342  Hrotation0[0][3] = 0;
343  Hrotation0[1][3] = 0;
344  Hrotation0[2][3] = 0;
345 
346  vpHomogeneousMatrix HmotionBaseFrame = Hrotation0.inverse() * Hmotion * Hrotation0;
347 
348  this->moveTip(HmotionBaseFrame);
349 }
350 
351 std::ostream &operator<<(std::ostream &s, const usNeedleModelBaseTip &needle)
352 {
353  s << "usNeedleModelBaseTip\n";
354  for (int i = 0; i < 6; i++)
355  s << needle.m_basePose[i] << " ";
356  s << '\n';
357  for (int i = 0; i < 4; i++) {
358  for (int j = 0; j < 4; j++)
359  s << needle.m_worldMbase[i][j] << " ";
360  s << '\n';
361  }
362  for (int i = 0; i < 6; i++)
363  s << needle.m_tipPose[i] << " ";
364  s << '\n';
365  for (int i = 0; i < 4; i++) {
366  for (int j = 0; j < 4; j++)
367  s << needle.m_worldMtip[i][j] << " ";
368  s << '\n';
369  }
370 
371  s.flush();
372  return s;
373 }
374 
375 std::istream &operator>>(std::istream &s, usNeedleModelBaseTip &needle)
376 {
377  std::string c;
378  s >> c;
379  if (c != "usNeedleModelBaseTip") {
380  vpException e(vpException::ioError, "Stream does not contain usNeedleModelBaseTip data");
381  throw e;
382  }
383  for (int i = 0; i < 6; i++)
384  s >> needle.m_basePose[i];
385  for (int i = 0; i < 4; i++)
386  for (int j = 0; j < 4; j++)
387  s >> needle.m_worldMbase[i][j];
388  for (int i = 0; i < 6; i++)
389  s >> needle.m_tipPose[i];
390  for (int i = 0; i < 4; i++)
391  for (int j = 0; j < 4; j++)
392  s >> needle.m_worldMtip[i][j];
393  return s;
394 }
395 
396 std::ostream &operator<<=(std::ostream &s, const usNeedleModelBaseTip &needle)
397 {
398  s.write("usNeedleModelBaseTip", 21);
399  for (int i = 0; i < 6; i++)
400  s.write((char *)&(needle.m_basePose[i]), sizeof(double));
401  for (int i = 0; i < 4; i++)
402  for (int j = 0; j < 4; j++)
403  s.write((char *)&(needle.m_worldMbase[i][j]), sizeof(double));
404  for (int i = 0; i < 6; i++)
405  s.write((char *)&(needle.m_tipPose[i]), sizeof(double));
406  for (int i = 0; i < 4; i++)
407  for (int j = 0; j < 4; j++)
408  s.write((char *)&(needle.m_worldMtip[i][j]), sizeof(double));
409 
410  s.flush();
411  return s;
412 }
413 
414 std::istream &operator>>=(std::istream &s, usNeedleModelBaseTip &needle)
415 {
416  char c[21];
417  s.read(c, 21);
418  if (strcmp(c, "usNeedleModelBaseTip")) {
419  vpException e(vpException::ioError, "Stream does not contain usNeedleModelBaseTip data");
420  throw e;
421  }
422  for (int i = 0; i < 6; i++)
423  s.read((char *)&(needle.m_basePose[i]), sizeof(double));
424  for (int i = 0; i < 4; i++)
425  for (int j = 0; j < 4; j++)
426  s.read((char *)&(needle.m_worldMbase[i][j]), sizeof(double));
427  for (int i = 0; i < 6; i++)
428  s.read((char *)&(needle.m_tipPose[i]), sizeof(double));
429  for (int i = 0; i < 4; i++)
430  for (int j = 0; j < 4; j++)
431  s.read((char *)&(needle.m_worldMtip[i][j]), sizeof(double));
432  return s;
433 }
vpPoseVector getBasePose() const
Parameters setters and getters.
void setBasePose(double tx, double ty, double tz, double thetax, double thetay, double thetaz)
Command of the needle.
void moveTipWorldFrame(const vpColVector &command, double time)
vpColVector getTipDirection() const
vpColVector getTipPosition() const
vpHomogeneousMatrix getWorldMtip() const
void moveTip(const vpColVector &control, double time)
vpHomogeneousMatrix getWorldMbase() const
vpPoseVector getTipPose() const
vpColVector getBaseDirection() const
virtual usNeedleModelBaseTip * clone() const
usNeedleModelBaseTip()
Constructors, destructors.
vpHomogeneousMatrix m_worldMtip
void moveBaseWorldFrame(const vpColVector &command, double time)
vpColVector getBasePosition() const
void setTipPose(double tx, double ty, double tz, double thetax, double thetay, double thetaz)
void moveBase(const vpColVector &control, double time)
vpHomogeneousMatrix m_worldMbase
virtual usNeedleModelBaseTip & operator=(const usNeedleModelBaseTip &needle)