UsTK : Ultrasound ToolKit  version 2.0.1 under development (2024-12-17)
usOrientedPlane3D.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_core/usOrientedPlane3D.h>
34 
35 #include <visp3/core/vpException.h>
36 #include <visp3/core/vpRotationMatrix.h>
37 #include <visp3/core/vpThetaUVector.h>
38 
39 usOrientedPlane3D::usOrientedPlane3D() : m_direction(3, 0), m_pose(0, 0, 0, 0, 0, 0) {}
40 
42  : m_direction(plane.m_direction), m_pose(plane.m_pose)
43 {
44 }
45 
46 usOrientedPlane3D::usOrientedPlane3D(const vpPoseVector &pose) : m_direction(3, 0), m_pose(pose) { }
47 
48 usOrientedPlane3D::usOrientedPlane3D(const vpColVector &p, const vpColVector &d) : m_direction(3, 0), m_pose(0, 0, 0, 0, 0, 0)
49 {
50  this->setPosition(p);
51  this->setDirection(d);
52 }
53 
55 
57 {
58  m_direction = plane.m_direction;
59  m_pose = plane.m_pose;
60 
61  return *this;
62 }
63 
64 void usOrientedPlane3D::setPose(const vpPoseVector &pose)
65 {
66  m_pose = pose;
67  m_direction = ((vpMatrix)vpRotationMatrix(pose)).getCol(2, 0, 3).normalize();
68 }
69 
70 vpPoseVector usOrientedPlane3D::getPose() const { return m_pose; }
71 
72 void usOrientedPlane3D::setPosition(const vpColVector &P)
73 {
74  if (P.size() == 3) {
75  for (int i = 0; i < 3; i++)
76  m_pose[i] = P[i];
77  }
78 }
79 
80 vpColVector usOrientedPlane3D::getPosition() const
81 {
82  vpColVector P(3);
83 
84  for (int i = 0; i < 3; i++)
85  P[i] = m_pose[i];
86 
87  return P;
88 }
89 
90 void usOrientedPlane3D::setDirection(const vpColVector &D)
91 {
92  if (D.size() != 3)
93  return;
94 
95  if (D.frobeniusNorm() > std::numeric_limits<double>::epsilon()) {
96  if (m_direction.frobeniusNorm() == 0)
97  m_direction[2] = 1;
98  double cos = vpColVector::dotProd(m_direction, D);
99  vpColVector u = vpColVector::crossProd(m_direction, D);
100  double sin = u.frobeniusNorm();
101  double theta = atan2(sin, cos);
102  u = theta * u.normalize();
103 
104  vpThetaUVector tu(vpRotationMatrix(u[0], u[1], u[2]) * vpRotationMatrix(m_pose));
105  for (int i = 0; i < 3; i++)
106  m_pose[3 + i] = tu[i];
107  m_direction = D;
108  m_direction.normalize();
109  } else {
110  m_direction = 0;
111  for (int i = 3; i < 6; i++)
112  m_pose[i] = 0;
113  }
114 }
115 
116 vpColVector usOrientedPlane3D::getDirection() const { return m_direction; }
117 
118 void usOrientedPlane3D::moveInLocalFrame(const vpHomogeneousMatrix &H)
119 {
120  this->setPose(vpPoseVector(vpHomogeneousMatrix(m_pose) * H));
121 }
122 
123 void usOrientedPlane3D::moveInLocalFrame(double x, double y, double z, double tx, double ty, double tz)
124 {
125  this->moveInLocalFrame(vpHomogeneousMatrix(x, y, z, tx, ty, tz));
126 }
127 
128 void usOrientedPlane3D::moveInWorldFrame(const vpHomogeneousMatrix &H)
129 {
130  this->setPose(vpPoseVector(H * vpHomogeneousMatrix(m_pose)));
131 }
132 
133 void usOrientedPlane3D::moveInWorldFrame(double x, double y, double z, double tx, double ty, double tz)
134 {
135  this->moveInWorldFrame(vpHomogeneousMatrix(x, y, z, tx, ty, tz));
136 }
137 
138 std::ostream &operator<<(std::ostream &s, const usOrientedPlane3D &plane)
139 {
140  s << "usOrientedPlane3D\n";
141  s << plane.m_pose[0];
142  for (int i = 1; i < 6; i++)
143  s << " " << plane.m_pose[i];
144  s << '\n';
145  s.flush();
146  return s;
147 }
148 
149 std::istream &operator>>(std::istream &s, usOrientedPlane3D &plane)
150 {
151  std::string c;
152  s >> c;
153  if (c != "usOrientedPlane3D") {
154  vpException e(vpException::ioError,
155  "operator>>=(std::istream&, usOrientedPlane3D&): Stream does not contain usOrientedPlane3D data");
156  throw e;
157  }
158  vpPoseVector pose;
159  for (int i = 0; i < 6; i++)
160  s >> pose[i];
161  s.get();
162  plane.setPose(pose);
163  return s;
164 }
165 
166 std::ostream &operator<<=(std::ostream &s, const usOrientedPlane3D &plane)
167 {
168  s.write("usOrientedPlane3D", 18);
169  for (int i = 0; i < 6; i++)
170  s.write((char *)&(plane.m_pose[i]), sizeof(double));
171  s.flush();
172  return s;
173 }
174 
175 std::istream &operator>>=(std::istream &s, usOrientedPlane3D &plane)
176 {
177  char c[18];
178  s.read(c, 18);
179  if (strcmp(c, "usOrientedPlane3D")) {
180  vpException e(vpException::ioError,
181  "operator>>=(std::istream&, usOrientedPlane3D&): Stream does not contain usOrientedPlane3D data");
182  throw e;
183  }
184  vpPoseVector pose;
185  for (int i = 0; i < 3; i++)
186  s.read((char *)&(pose[i]), sizeof(double));
187  plane.setPose(pose);
188  return s;
189 }
vpColVector getDirection() const
vpColVector m_direction
void setDirection(const vpColVector &D)
void moveInLocalFrame(const vpHomogeneousMatrix &H)
vpColVector getPosition() const
void moveInWorldFrame(const vpHomogeneousMatrix &H)
void setPose(const vpPoseVector &pose)
Parameters setters and getters.
vpPoseVector getPose() const
const usOrientedPlane3D & operator=(const usOrientedPlane3D &plane)
void setPosition(const vpColVector &P)
usOrientedPlane3D()
Constructors, destructor.