Elements.cpp
Go to the documentation of this file.
1 #include "Elements.h"
2 
3 #include <SimoxUtility/math/convert/rpy_to_mat3f.h>
4 #include <SimoxUtility/math/normal/normal_to_mat4.h>
5 #include <SimoxUtility/math/pose/transform.h>
6 
10 
11 namespace armarx::viz
12 {
13 
14  struct Convert
15  {
16  static Eigen::Quaternionf
17  directionToQuaternion(Eigen::Vector3f dir)
18  {
19  dir = dir.normalized();
20  Eigen::Vector3f naturalDir = Eigen::Vector3f::UnitY();
21  Eigen::Vector3f cross = naturalDir.cross(dir);
22  float dot = naturalDir.dot(dir);
23  float angle = std::acos(dot);
24  if (cross.squaredNorm() < 1.0e-12)
25  {
26  // Directions are almost colinear ==> Do no rotation
27  cross = Eigen::Vector3f::UnitX();
28  if (dot < 0)
29  {
30  angle = M_PI;
31  }
32  else
33  {
34  angle = 0.0f;
35  }
36  }
37  Eigen::Vector3f axis = cross.normalized();
38  Eigen::Quaternionf ori(Eigen::AngleAxisf(angle, axis));
39 
40  return ori;
41  }
42  };
43 
44  const std::string Object::DefaultObjectsPackage =
46  const std::string Object::DefaultRelativeObjectsDirectory =
48 
49  Object&
50  Object::fileByObjectFinder(const std::string& objectID,
51  const std::string& objectsPackage,
52  const std::string& relativeObjectsDirectory)
53  {
54  return this->fileByObjectFinder(
55  armarx::ObjectID(objectID), objectsPackage, relativeObjectsDirectory);
56  }
57 
58  Object&
60  const std::string& objectsPackage,
61  const std::string& relativeObjectsDirectory)
62  {
63  ObjectInfo info(objectsPackage, "", relativeObjectsDirectory, objectID);
65  return this->file(file.package, file.relativePath);
66  }
67 
68  Object&
69  Object::alpha(float alpha)
70  {
71  if (alpha < 1)
72  {
73  overrideColor(simox::Color::white().with_alpha(alpha));
74  }
75  return *this;
76  }
77 
78  Box&
79  Box::set(const simox::OrientedBoxBase<float>& b)
80  {
81  size(b.dimensions());
82  return pose(b.transformation_centered());
83  }
84 
85  Box&
86  Box::set(const simox::OrientedBoxBase<double>& b)
87  {
88  size(b.dimensions<float>());
89  return pose(b.transformation_centered<float>());
90  }
91 
92  Cylinder&
93  Cylinder::fromTo(Eigen::Vector3f from, Eigen::Vector3f to)
94  {
95  position((to + from) / 2);
96  orientation(Convert::directionToQuaternion((to - from).normalized()));
97  height((to - from).norm());
98 
99  return *this;
100  }
101 
102  Cylinder&
103  Cylinder::direction(Eigen::Vector3f direction)
104  {
106 
107  return *this;
108  }
109 
110  Arrow&
111  Arrow::direction(Eigen::Vector3f dir)
112  {
114  }
115 
116  ArrowCircle&
117  ArrowCircle::normal(Eigen::Vector3f dir)
118  {
119  Eigen::Vector3f naturalDir = Eigen::Vector3f::UnitZ();
120  Eigen::Vector3f cross = naturalDir.cross(dir);
121  float angle = std::acos(naturalDir.dot(dir));
122  if (cross.squaredNorm() < 1.0e-12f)
123  {
124  // Directions are almost colinear ==> Angle is either 0 or 180 deg
125  cross = Eigen::Vector3f::UnitX();
126  // Keep angle
127  }
128  Eigen::Vector3f axis = cross.normalized();
129  Eigen::Quaternionf ori(Eigen::AngleAxisf(angle, axis));
130 
131  return orientation(ori);
132  }
133 
134  Polygon&
135  Polygon::points(const std::vector<Eigen::Vector3f>& ps)
136  {
137  auto& points = data_->points;
138  points.clear();
139  points.reserve(ps.size());
140  for (auto& p : ps)
141  {
142  points.push_back(armarx::Vector3f{p.x(), p.y(), p.z()});
143  }
144 
145  return *this;
146  }
147 
148  Polygon&
149  Polygon::plane(Eigen::Hyperplane3f plane, Eigen::Vector3f at, Eigen::Vector2f size)
150  {
151  const Eigen::Quaternionf ori =
152  Eigen::Quaternionf::FromTwoVectors(Eigen::Vector3f::UnitZ(), plane.normal());
153  return this->plane(plane.projection(at), ori, size);
154  }
155 
156  Polygon&
157  Polygon::plane(Eigen::Vector3f center, Eigen::Quaternionf orientation, Eigen::Vector2f size)
158  {
159  const Eigen::Vector3f x = 0.5f * size.x() * (orientation * Eigen::Vector3f::UnitX());
160  const Eigen::Vector3f y = 0.5f * size.y() * (orientation * Eigen::Vector3f::UnitY());
161 
162  addPoint(center + x + y);
163  addPoint(center - x + y);
164  addPoint(center - x - y);
165  addPoint(center + x - y);
166 
167  return *this;
168  }
169 
170  Polygon&
171  Polygon::circle(Eigen::Vector3f center,
172  Eigen::Vector3f normal,
173  float radius,
174  std::size_t tessellation)
175  {
176  const Eigen::Matrix4f pose = simox::math::normal_pos_to_mat4(normal, center);
177  ARMARX_CHECK_GREATER_EQUAL(tessellation, 3);
178 
179  const float angle = 2 * M_PI / tessellation;
180  const Eigen::Matrix3f rot = simox::math::rpy_to_mat3f(0, 0, angle);
181 
182  Eigen::Vector3f lastLocalPoint = Eigen::Vector3f::UnitX() * radius;
183  addPoint(simox::math::transform_position(pose, lastLocalPoint));
184  while (--tessellation)
185  {
186  const Eigen::Vector3f localPoint = rot * lastLocalPoint;
187  addPoint(simox::math::transform_position(pose, localPoint));
188  lastLocalPoint = localPoint;
189  }
190  return *this;
191  }
192 
193 
194 } // namespace armarx::viz
armarx::viz::Convert
Definition: Elements.cpp:14
armarx::viz::Polygon::points
Polygon & points(std::vector< Eigen::Vector3f > const &ps)
Definition: Elements.cpp:135
armarx::ObjectID
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition: ObjectID.h:10
armarx::ObjectFinder::DefaultObjectsPackageName
static const std::string DefaultObjectsPackageName
Definition: ObjectFinder.h:26
armarx::viz::Cylinder::fromTo
Cylinder & fromTo(Eigen::Vector3f from, Eigen::Vector3f to)
Definition: Elements.cpp:93
armarx::viz::Object::fileByObjectFinder
Object & fileByObjectFinder(const armarx::ObjectID &objectID, const std::string &objectsPackage=DefaultObjectsPackage, const std::string &relativeObjectsDirectory=DefaultRelativeObjectsDirectory)
Set the file so it could be found using armarx::ObjectFinder (also on remote machine).
Definition: Elements.cpp:59
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::viz::Object::file
Object & file(std::string const &project, std::string const &filename)
Definition: Elements.h:340
armarx::view_selection::skills::direction::state::from
state::Type from(Eigen::Vector3f targetPosition)
Definition: LookDirection.cpp:194
armarx::viz::Arrow
Definition: Elements.h:196
armarx::PackageFileLocation
Definition: ObjectInfo.h:22
armarx::viz::Cylinder::height
Cylinder & height(float h)
Definition: Elements.h:84
armarx::viz::ArrowCircle::normal
ArrowCircle & normal(Eigen::Vector3f dir)
Definition: Elements.cpp:117
armarx::viz::ElementOps< Polygon, data::ElementPolygon >::data_
IceInternal::Handle< data::ElementPolygon > data_
Definition: ElementOps.h:315
armarx::viz::Object::alpha
Object & alpha(float alpha)
Definition: Elements.cpp:69
armarx::viz::Convert::directionToQuaternion
static Eigen::Quaternionf directionToQuaternion(Eigen::Vector3f dir)
Definition: Elements.cpp:17
armarx::viz::Object::DefaultRelativeObjectsDirectory
static const std::string DefaultRelativeObjectsDirectory
Definition: Elements.h:335
Elements.h
armarx::viz::ElementOps< Cylinder, data::ElementCylinder >::position
Cylinder & position(float x, float y, float z)
Definition: ElementOps.h:136
armarx::viz::Cylinder::direction
Cylinder & direction(Eigen::Vector3f direction)
Definition: Elements.cpp:103
armarx::viz::ElementOps< Box, data::ElementBox >::pose
Eigen::Matrix4f pose() const
Definition: ElementOps.h:200
armarx::viz::Object
Definition: Elements.h:332
armarx::viz::Polygon::addPoint
Polygon & addPoint(Eigen::Vector3f p)
Definition: Elements.h:302
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::viz::Polygon::clear
Polygon & clear()
Definition: Elements.h:265
armarx::viz::Cylinder
Definition: Elements.h:71
Eigen::Hyperplane3f
Hyperplane< float, 3 > Hyperplane3f
Definition: Elements.h:34
ObjectID.h
armarx::viz::ElementOps< Cylinder, data::ElementCylinder >::orientation
Cylinder & orientation(Eigen::Quaternionf const &ori)
Definition: ElementOps.h:152
armarx::viz::Arrow::direction
Arrow & direction(Eigen::Vector3f dir)
Definition: Elements.cpp:111
armarx::viz::Polygon::plane
Polygon & plane(Eigen::Hyperplane3f plane, Eigen::Vector3f at, Eigen::Vector2f size)
Add points representing a plane as rectangle.
Definition: Elements.cpp:149
cross
Point cross(const Point &x, const Point &y)
Definition: point.hpp:35
armarx::viz::Box
Definition: Elements.h:47
ObjectInfo.h
armarx::viz::Polygon
Definition: Elements.h:260
armarx::viz::Object::overrideColor
Object & overrideColor(Color c)
Definition: Elements.h:388
ARMARX_CHECK_GREATER_EQUAL
#define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs)
This macro evaluates whether lhs is greater or equal (>=) rhs and if it turns out to be false it will...
Definition: ExpressionException.h:123
armarx::viz::Polygon::circle
Polygon & circle(Eigen::Vector3f center, Eigen::Vector3f normal, float radius, std::size_t tessellation=64)
Definition: Elements.cpp:171
armarx::ObjectInfo::simoxXML
PackageFileLocation simoxXML() const
Definition: ObjectInfo.cpp:109
armarx::PackageFileLocation::package
std::string package
Name of the ArmarX package.
Definition: ObjectInfo.h:25
armarx::Quaternion< float, 0 >
armarx::viz::Box::size
Box & size(Eigen::Vector3f const &s)
Definition: Elements.h:52
armarx::view_selection::skills::direction::state::center
state::Type center(state::Type previous)
Definition: LookDirection.cpp:233
dot
double dot(const Point &x, const Point &y)
Definition: point.hpp:57
angle
double angle(const Point &a, const Point &b, const Point &c)
Definition: point.hpp:109
armarx::viz::Object::DefaultObjectsPackage
static const std::string DefaultObjectsPackage
Definition: Elements.h:334
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:649
armarx::viz::Box::set
Box & set(const simox::OrientedBoxBase< float > &b)
Definition: Elements.cpp:79
armarx::viz
This file is part of ArmarX.
Definition: ArVizStorage.cpp:418
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::ObjectFinder::DefaultObjectsDirectory
static const std::string DefaultObjectsDirectory
Definition: ObjectFinder.h:27
armarx::ObjectInfo
Accessor for the object files.
Definition: ObjectInfo.h:36
ObjectFinder.h
norm
double norm(const Point &a)
Definition: point.hpp:102
armarx::viz::ArrowCircle
Definition: Elements.h:229