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
11namespace armarx::viz
12{
13
14 struct Convert
15 {
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 =
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&
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
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&
104 {
106
107 return *this;
108 }
109
110 Arrow&
111 Arrow::direction(Eigen::Vector3f dir)
112 {
114 }
115
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
#define M_PI
Definition MathTools.h:17
static const std::string DefaultObjectsPackageName
static const std::string DefaultObjectsDirectory
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition ObjectID.h:11
Accessor for the object files.
Definition ObjectInfo.h:37
PackageFileLocation simoxXML() const
Polygon & pose(Eigen::Matrix4f const &pose)
Definition ElementOps.h:176
IceInternal::Handle< data::ElementPolygon > data_
Definition ElementOps.h:315
Cylinder & position(float x, float y, float z)
Definition ElementOps.h:136
Cylinder & orientation(Eigen::Quaternionf const &ori)
Definition ElementOps.h:152
#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...
Quaternion< float, 0 > Quaternionf
Hyperplane< float, 3 > Hyperplane3f
Definition Elements.h:34
This file is part of ArmarX.
This file offers overloads of toIce() and fromIce() functions for STL container types.
double norm(const Point &a)
Definition point.hpp:102
Point cross(const Point &x, const Point &y)
Definition point.hpp:35
double angle(const Point &a, const Point &b, const Point &c)
Definition point.hpp:109
double dot(const Point &x, const Point &y)
Definition point.hpp:57
std::string package
Name of the ArmarX package.
Definition ObjectInfo.h:25
ArrowCircle & normal(Eigen::Vector3f dir)
Definition Elements.cpp:117
Arrow & direction(Eigen::Vector3f dir)
Definition Elements.cpp:111
Box & set(const simox::OrientedBoxBase< float > &b)
Definition Elements.cpp:79
Box & size(Eigen::Vector3f const &s)
Definition Elements.h:52
static Eigen::Quaternionf directionToQuaternion(Eigen::Vector3f dir)
Definition Elements.cpp:17
Cylinder & direction(Eigen::Vector3f direction)
Definition Elements.cpp:103
Cylinder & height(float h)
Definition Elements.h:84
Cylinder & fromTo(Eigen::Vector3f from, Eigen::Vector3f to)
Definition Elements.cpp:93
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
Object & overrideColor(Color c)
Definition Elements.h:388
Object & file(std::string const &project, std::string const &filename)
Definition Elements.h:340
static const std::string DefaultObjectsPackage
Definition Elements.h:334
static const std::string DefaultRelativeObjectsDirectory
Definition Elements.h:335
Object & alpha(float alpha)
Definition Elements.cpp:69
Polygon & addPoint(Eigen::Vector3f p)
Definition Elements.h:302
Polygon & circle(Eigen::Vector3f center, Eigen::Vector3f normal, float radius, std::size_t tessellation=64)
Definition Elements.cpp:171
Polygon & points(std::vector< Eigen::Vector3f > const &ps)
Definition Elements.cpp:135
Polygon & plane(Eigen::Hyperplane3f plane, Eigen::Vector3f at, Eigen::Vector2f size)
Add points representing a plane as rectangle.
Definition Elements.cpp:149