Gazebo Rendering

API Reference

9.0.0
BaseGizmoVisual.hh
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019 Open Source Robotics Foundation
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 *
16 */
17
18#ifndef GZ_RENDERING_BASE_BASEGIZMOVISUAL_HH_
19#define GZ_RENDERING_BASE_BASEGIZMOVISUAL_HH_
20
21#include <map>
22#include <string>
24#include <gz/utils/SuppressWarning.hh>
25
31
32namespace gz
33{
34 namespace rendering
35 {
36 inline namespace GZ_RENDERING_VERSION_NAMESPACE {
37 //
38 /* \class BaseGizmoVisual BaseGizmoVisuall.hh \
39 * gz/rendering/base/BaseGizmoVisual.hh
40 */
42 template <class T>
44 public virtual GizmoVisual,
45 public virtual T
46 {
48 protected: BaseGizmoVisual();
49
51 public: virtual ~BaseGizmoVisual();
52
53 // Documentation inherited
54 public: virtual void Init() override;
55
56 // Documentation inherited
57 public: virtual void PreRender() override;
58
59 // Documentation inherited
60 public: virtual void SetTransformMode(TransformMode _mode) override;
61
62 // Documentation inherited
63 public: virtual TransformMode Mode() const override;
64
65 // Documentation inherited
66 public: virtual void SetActiveAxis(const math::Vector3d &_axis) override;
67
68 // Documentation inherited
69 public: virtual math::Vector3d ActiveAxis() const override;
70
71 // Documentation inherited
72 public: virtual TransformAxis AxisById(unsigned int _id) const override;
73
74 // Documentation inherited
75 public: virtual VisualPtr ChildByAxis(unsigned int _axis) const override;
76
77 // Documentation inherited
78 public: virtual void LookAt(const math::Vector3d &_pos,
79 const math::Quaterniond &_rot) override;
80
82 public: virtual void Reset();
83
85 protected: void CreateMaterials();
86
88 protected: void CreateTranslationVisual();
89
91 protected: void CreateRotationVisual();
92
94 protected: void CreateScaleVisual();
95
97 protected: TransformMode mode = TransformMode::TM_NONE;
98
99 GZ_UTILS_WARN_IGNORE__DLL_INTERFACE_MISSING
102
105
108
110 protected: bool modeDirty = false;
111
113 protected: math::Vector3d axis = math::Vector3d::Zero;
114
117 GZ_UTILS_WARN_RESUME__DLL_INTERFACE_MISSING
118
120 protected: enum AxisMaterial
121 {
133 AM_HANDLE
134 };
135
137 private: friend class BaseScene;
138 };
139
141 template <class T>
145
147 template <class T>
151
153 template <class T>
155 {
156 T::Init();
157
158 this->CreateMaterials();
159 this->CreateTranslationVisual();
160 this->CreateRotationVisual();
161 this->CreateScaleVisual();
162
163 for (auto v : this->visuals)
164 v.second->SetVisible(false);
165 }
166
168 template <class T>
170 {
171 this->visuals[TransformAxis::TA_TRANSLATION_X]->SetMaterial(
172 this->materials[AM_X], false);
173 this->visuals[TransformAxis::TA_TRANSLATION_Y]->SetMaterial(
174 this->materials[AM_Y], false);
175 this->visuals[TransformAxis::TA_TRANSLATION_Z]->SetMaterial(
176 this->materials[AM_Z], false);
177 this->visuals[TransformAxis::TA_ROTATION_X]->SetMaterial(
178 this->materials[AM_X], false);
179 this->visuals[TransformAxis::TA_ROTATION_Y]->SetMaterial(
180 this->materials[AM_Y], false);
181 this->visuals[TransformAxis::TA_ROTATION_Z]->SetMaterial(
182 this->materials[AM_Z], false);
183 this->visuals[TransformAxis::TA_SCALE_X]->SetMaterial(
184 this->materials[AM_X], false);
185 this->visuals[TransformAxis::TA_SCALE_Y]->SetMaterial(
186 this->materials[AM_Y], false);
187 this->visuals[TransformAxis::TA_SCALE_Z]->SetMaterial(
188 this->materials[AM_Z], false);
189
190 this->handles[TransformAxis::TA_TRANSLATION_X]->SetMaterial(
191 this->materials[AM_HANDLE], false);
192 this->handles[TransformAxis::TA_TRANSLATION_Y]->SetMaterial(
193 this->materials[AM_HANDLE], false);
194 this->handles[TransformAxis::TA_TRANSLATION_Z]->SetMaterial(
195 this->materials[AM_HANDLE], false);
196
197 this->handles[TransformAxis::TA_ROTATION_X]->SetMaterial(
198 this->materials[AM_HANDLE], false);
199 this->handles[TransformAxis::TA_ROTATION_Y]->SetMaterial(
200 this->materials[AM_HANDLE], false);
201 this->handles[TransformAxis::TA_ROTATION_Z]->SetMaterial(
202 this->materials[AM_HANDLE], false);
203
204 this->handles[TransformAxis::TA_SCALE_X]->SetMaterial(
205 this->materials[AM_HANDLE], false);
206 this->handles[TransformAxis::TA_SCALE_Y]->SetMaterial(
207 this->materials[AM_HANDLE], false);
208 this->handles[TransformAxis::TA_SCALE_Z]->SetMaterial(
209 this->materials[AM_HANDLE], false);
210
211 for (auto v : this->visuals)
212 v.second->SetVisible(false);
213 }
214
216 template <class T>
218 {
219 if (!this->modeDirty)
220 return;
221
222 this->Reset();
223
224 if (this->mode == TransformMode::TM_NONE)
225 {
226 this->modeDirty = false;
227 return;
228 }
229
230 if (this->mode & TransformMode::TM_TRANSLATION)
231 {
232 this->visuals[TransformAxis::TA_TRANSLATION_X]->SetVisible(true);
233 this->visuals[TransformAxis::TA_TRANSLATION_Y]->SetVisible(true);
234 this->visuals[TransformAxis::TA_TRANSLATION_Z]->SetVisible(true);
235 this->visuals[TransformAxis::TA_TRANSLATION_Z << 1]->SetVisible(true);
236 if (this->axis.X() > 0)
237 {
238 this->visuals[TransformAxis::TA_TRANSLATION_X]->SetMaterial(
239 this->materials[AM_ACTIVE], false);
240 this->handles[TransformAxis::TA_TRANSLATION_X]->SetMaterial(
241 this->materials[AM_HANDLE], false);
242 }
243 if (this->axis.Y() > 0)
244 {
245 this->visuals[TransformAxis::TA_TRANSLATION_Y]->SetMaterial(
246 this->materials[AM_ACTIVE], false);
247 this->handles[TransformAxis::TA_TRANSLATION_Y]->SetMaterial(
248 this->materials[AM_HANDLE], false);
249 }
250 if (this->axis.Z() > 0)
251 {
252 this->visuals[TransformAxis::TA_TRANSLATION_Z]->SetMaterial(
253 this->materials[AM_ACTIVE], false);
254 this->handles[TransformAxis::TA_TRANSLATION_Z]->SetMaterial(
255 this->materials[AM_HANDLE], false);
256 }
257 }
258 else if (this->mode & TransformMode::TM_ROTATION)
259 {
260 this->visuals[TransformAxis::TA_ROTATION_X]->SetVisible(true);
261 this->visuals[TransformAxis::TA_ROTATION_Y]->SetVisible(true);
262 this->visuals[TransformAxis::TA_ROTATION_Z]->SetVisible(true);
263 this->visuals[TransformAxis::TA_ROTATION_Z << 1]->SetVisible(true);
264 if (this->axis.X() > 0)
265 {
266 this->visuals[TransformAxis::TA_ROTATION_X]->SetMaterial(
267 this->materials[AM_ACTIVE], false);
268 this->handles[TransformAxis::TA_ROTATION_X]->SetMaterial(
269 this->materials[AM_HANDLE], false);
270 }
271 if (this->axis.Y() > 0)
272 {
273 this->visuals[TransformAxis::TA_ROTATION_Y]->SetMaterial(
274 this->materials[AM_ACTIVE], false);
275 this->handles[TransformAxis::TA_ROTATION_Y]->SetMaterial(
276 this->materials[AM_HANDLE], false);
277 }
278 if (this->axis.Z() > 0)
279 {
280 this->visuals[TransformAxis::TA_ROTATION_Z]->SetMaterial(
281 this->materials[AM_ACTIVE], false);
282 this->handles[TransformAxis::TA_ROTATION_Z]->SetMaterial(
283 this->materials[AM_HANDLE], false);
284 }
285 }
286 else if (this->mode & TransformMode::TM_SCALE)
287 {
288 this->visuals[TransformAxis::TA_SCALE_X]->SetVisible(true);
289 this->visuals[TransformAxis::TA_SCALE_Y]->SetVisible(true);
290 this->visuals[TransformAxis::TA_SCALE_Z]->SetVisible(true);
291 if (this->axis.X() > 0)
292 {
293 this->visuals[TransformAxis::TA_SCALE_X]->SetMaterial(
294 this->materials[AM_ACTIVE], false);
295 this->handles[TransformAxis::TA_SCALE_X]->SetMaterial(
296 this->materials[AM_HANDLE], false);
297 }
298 if (this->axis.Y() > 0)
299 {
300 this->visuals[TransformAxis::TA_SCALE_Y]->SetMaterial(
301 this->materials[AM_ACTIVE], false);
302 this->handles[TransformAxis::TA_SCALE_Y]->SetMaterial(
303 this->materials[AM_HANDLE], false);
304 }
305 if (this->axis.Z() > 0)
306 {
307 this->visuals[TransformAxis::TA_SCALE_Z]->SetMaterial(
308 this->materials[AM_ACTIVE], false);
309 this->handles[TransformAxis::TA_SCALE_Z]->SetMaterial(
310 this->materials[AM_HANDLE], false);
311 }
312 }
313
314 this->modeDirty = false;
315 }
316
318 template <class T>
320 {
321 if (this->mode == _mode)
322 return;
323
324 this->mode = _mode;
325 // clear active axis when mode changes
326 this->axis = math::Vector3d::Zero;
327 this->modeDirty = true;
328 }
329
331 template <class T>
333 {
334 return this->mode;
335 }
336
338 template <class T>
340 {
341 if (this->axis == _axis)
342 return;
343
344 this->axis = _axis;
345 this->modeDirty = true;
346 }
347
349 template <class T>
351 {
352 return this->axis;
353 }
354
356 template <class T>
358 {
359 for (auto v : this->visuals)
360 {
361 // each axis visual has a child handle so also check children for
362 // matching id
363 if (v.second->Id() == _id || v.second->ChildById(_id))
364 return static_cast<TransformAxis>(v.first);
365 }
367 }
368
369
371 template <class T>
373 {
374 MaterialPtr xMat = this->Scene()->Material("Default/TransRed")->Clone();
375 MaterialPtr yMat = this->Scene()->Material("Default/TransGreen")->Clone();
376 MaterialPtr zMat = this->Scene()->Material("Default/TransBlue")->Clone();
377 MaterialPtr activeMat =
378 this->Scene()->Material("Default/TransYellow")->Clone();
379
380 // disable depth checking and writing, make them overlays
381 xMat->SetDepthWriteEnabled(false);
382 xMat->SetDepthCheckEnabled(false);
383 yMat->SetDepthWriteEnabled(false);
384 yMat->SetDepthCheckEnabled(false);
385 zMat->SetDepthWriteEnabled(false);
386 zMat->SetDepthCheckEnabled(false);
387 activeMat->SetDepthWriteEnabled(false);
388 activeMat->SetDepthCheckEnabled(false);
389
390 MaterialPtr oMat = this->Scene()->Material("GizmoGray");
391 if (!oMat)
392 {
393 oMat = this->Scene()->CreateMaterial("GizmoGray");
394 oMat->SetAmbient(0.5, 0.5, 0.5);
395 oMat->SetDiffuse(0.5, 0.5, 0.5);
396 oMat->SetEmissive(0.5, 0.5, 0.5);
397 oMat->SetTransparency(0.5);
398 oMat->SetCastShadows(false);
399 oMat->SetReceiveShadows(false);
400 oMat->SetLightingEnabled(false);
401 oMat->SetDepthWriteEnabled(false);
402 oMat->SetDepthCheckEnabled(false);
403 }
404
405 MaterialPtr handleMat = this->Scene()->Material("GizmoHandle");
406 if (!handleMat)
407 {
408 handleMat = this->Scene()->CreateMaterial("GizmoHandle");
409 handleMat->SetAmbient(0.0, 0.0, 0.0);
410 handleMat->SetDiffuse(0.0, 0.0, 0.0);
411 handleMat->SetEmissive(0.0, 0.0, 0.0);
412 handleMat->SetTransparency(1.0);
413 handleMat->SetCastShadows(false);
414 handleMat->SetReceiveShadows(false);
415 handleMat->SetLightingEnabled(false);
416 handleMat->SetDepthWriteEnabled(false);
417 handleMat->SetDepthCheckEnabled(false);
418 }
419
420 this->materials[AM_X] = xMat;
421 this->materials[AM_Y] = yMat;
422 this->materials[AM_Z] = zMat;
423 this->materials[AM_ACTIVE] = activeMat;
424 this->materials[AM_O] = oMat;
425 this->materials[AM_HANDLE] = handleMat;
426 }
428 template <class T>
430 {
431 VisualPtr transVis = this->Scene()->CreateVisual();
432
433 // trans x
434 VisualPtr transXVis = this->Scene()->CreateVisual();
435 VisualPtr transShaftXVis = this->Scene()->CreateVisual();
436 transShaftXVis->AddGeometry(this->Scene()->CreateCylinder());
437 transShaftXVis->SetOrigin(0, 0, 0.5);
438 transShaftXVis->SetLocalPosition(0, 0, 0.5);
439 transShaftXVis->SetLocalScale(0.02, 0.02, 0.45);
440 transXVis->AddChild(transShaftXVis);
441
442 VisualPtr transHeadXVis = this->Scene()->CreateVisual();
443 transHeadXVis->AddGeometry(this->Scene()->CreateCone());
444 transHeadXVis->SetOrigin(0, 0, -0.5);
445 transHeadXVis->SetLocalPosition(0, 0, 0.5);
446 transHeadXVis->SetLocalScale(0.07, 0.07, 0.2);
447 transXVis->AddChild(transHeadXVis);
448
449 transXVis->SetMaterial(this->materials[AM_X], false);
450 transXVis->SetLocalRotation(0, GZ_PI * 0.5, 0);
451 transVis->AddChild(transXVis);
452
453 // trans y
454 VisualPtr transYVis = this->Scene()->CreateVisual();
455 VisualPtr transShaftYVis = this->Scene()->CreateVisual();
456 transShaftYVis->AddGeometry(this->Scene()->CreateCylinder());
457 transShaftYVis->SetOrigin(0, 0, 0.5);
458 transShaftYVis->SetLocalPosition(0, 0, 0.5);
459 transShaftYVis->SetLocalScale(0.02, 0.02, 0.45);
460 transYVis->AddChild(transShaftYVis);
461
462 VisualPtr transHeadYVis = this->Scene()->CreateVisual();
463 transHeadYVis->AddGeometry(this->Scene()->CreateCone());
464 transHeadYVis->SetOrigin(0, 0, -0.5);
465 transHeadYVis->SetLocalPosition(0, 0, 0.5);
466 transHeadYVis->SetLocalScale(0.07, 0.07, 0.2);
467 transYVis->AddChild(transHeadYVis);
468
469 transYVis->SetMaterial(this->materials[AM_Y], false);
470 transYVis->SetLocalRotation(-GZ_PI * 0.5, 0, 0);
471 transVis->AddChild(transYVis);
472
473 // trans z
474 VisualPtr transZVis = this->Scene()->CreateVisual();
475 VisualPtr transShaftZVis = this->Scene()->CreateVisual();
476 transShaftZVis->AddGeometry(this->Scene()->CreateCylinder());
477 transShaftZVis->SetOrigin(0, 0, 0.5);
478 transShaftZVis->SetLocalPosition(0, 0, 0.5);
479 transShaftZVis->SetLocalScale(0.02, 0.02, 0.45);
480 transZVis->AddChild(transShaftZVis);
481
482 VisualPtr transHeadZVis = this->Scene()->CreateVisual();
483 transHeadZVis->AddGeometry(this->Scene()->CreateCone());
484 transHeadZVis->SetOrigin(0, 0, -0.5);
485 transHeadZVis->SetLocalPosition(0, 0, 0.5);
486 transHeadZVis->SetLocalScale(0.07, 0.07, 0.2);
487 transZVis->AddChild(transHeadZVis);
488
489 transZVis->SetMaterial(this->materials[AM_Z], false);
490 transVis->AddChild(transZVis);
491
492 // trans origin
493 VisualPtr transOrigin = this->Scene()->CreateVisual();
494 transOrigin->AddGeometry(this->Scene()->CreateSphere());
495 transOrigin->SetLocalScale(0.05, 0.05, 0.05);
496 transOrigin->SetMaterial(this->materials[AM_O], false);
497 transVis->AddChild(transOrigin);
498
499 this->visuals[TransformAxis::TA_TRANSLATION_X] = transXVis;
500 this->visuals[TransformAxis::TA_TRANSLATION_Y] = transYVis;
501 this->visuals[TransformAxis::TA_TRANSLATION_Z] = transZVis;
502 // Store the translation origin visual in this->visuals using a key
503 // that's not already occupied by the TransformAxis enum
504 this->visuals[TransformAxis::TA_TRANSLATION_Z << 1] = transOrigin;
505
506 // translation handles
507 VisualPtr transHandleXVis = this->Scene()->CreateVisual();
508 transHandleXVis->AddGeometry(this->Scene()->CreateCylinder());
509 transHandleXVis->SetLocalPosition(0, 0, 0.35);
510 transHandleXVis->SetLocalScale(0.11, 0.11, 0.7);
511 transHandleXVis->SetMaterial(this->materials[AM_HANDLE], false);
512 transXVis->AddChild(transHandleXVis);
513
514 VisualPtr transHandleYVis = this->Scene()->CreateVisual();
515 transHandleYVis->AddGeometry(this->Scene()->CreateCylinder());
516 transHandleYVis->SetLocalPosition(0, 0, 0.35);
517 transHandleYVis->SetLocalScale(0.11, 0.11, 0.7);
518 transHandleYVis->SetMaterial(this->materials[AM_HANDLE], false);
519 transYVis->AddChild(transHandleYVis);
520
521 VisualPtr transHandleZVis = this->Scene()->CreateVisual();
522 transHandleZVis->AddGeometry(this->Scene()->CreateCylinder());
523 transHandleZVis->SetLocalPosition(0, 0, 0.35);
524 transHandleZVis->SetLocalScale(0.11, 0.11, 0.7);
525 transHandleZVis->SetMaterial(this->materials[AM_HANDLE], false);
526 transZVis->AddChild(transHandleZVis);
527
528 this->handles[TransformAxis::TA_TRANSLATION_X] = transHandleXVis;
529 this->handles[TransformAxis::TA_TRANSLATION_Y] = transHandleYVis;
530 this->handles[TransformAxis::TA_TRANSLATION_Z] = transHandleZVis;
531
532 this->AddChild(transVis);
533 }
534
536 template <class T>
538 {
540 std::string rotMeshName = "gizmo_rotate";
541 if (!meshMgr->HasMesh(rotMeshName))
542 meshMgr->CreateTube(rotMeshName, 1.0f, 1.02f, 0.02f, 1, 64, GZ_PI);
543
544 std::string rotFullMeshName = "gizmo_rotate_full";
545 if (!meshMgr->HasMesh(rotFullMeshName))
546 {
547 meshMgr->CreateTube(rotFullMeshName, 1.0f, 1.02f, 0.02f, 1, 64,
548 2 * GZ_PI);
549 }
550
551 std::string rotHandleMeshName = "gizmo_rotate_handle";
552 if (!meshMgr->HasMesh(rotHandleMeshName))
553 {
554 meshMgr->CreateTube(rotHandleMeshName, 0.95f, 1.07f, 0.1f, 1, 64,
555 GZ_PI);
556 }
557
558 VisualPtr rotVis = this->Scene()->CreateVisual();
559
560 // rotation x
561 VisualPtr rotXVis = this->Scene()->CreateVisual();
562 rotXVis->AddGeometry(this->Scene()->CreateMesh(rotMeshName));
563 rotXVis->SetLocalRotation(0, GZ_PI * 0.5, 0);
564 rotXVis->SetLocalScale(0.5, 0.5, 0.5);
565 rotXVis->SetMaterial(this->materials[AM_X], false);
566 rotVis->AddChild(rotXVis);
567
568 // rotation y
569 VisualPtr rotYVis = this->Scene()->CreateVisual();
570 rotYVis->AddGeometry(this->Scene()->CreateMesh(rotMeshName));
571 rotYVis->SetLocalRotation(GZ_PI * 0.5, 0, 0);
572 rotYVis->SetLocalScale(0.5, 0.5, 0.5);
573 rotYVis->SetMaterial(this->materials[AM_Y], false);
574 rotVis->AddChild(rotYVis);
575
576 // rotation z
577 VisualPtr rotZVis = this->Scene()->CreateVisual();
578 rotZVis->AddGeometry(this->Scene()->CreateMesh(rotMeshName));
579 rotZVis->SetLocalScale(0.5, 0.5, 0.5);
580 rotZVis->SetMaterial(this->materials[AM_Z], false);
581 rotVis->AddChild(rotZVis);
582
583 // rotation origin
584 VisualPtr rotFullVis = this->Scene()->CreateVisual();
585 rotFullVis->AddGeometry(this->Scene()->CreateMesh(rotFullMeshName));
586 rotFullVis->SetLocalScale(0.5, 0.5, 0.5);
587 rotFullVis->SetMaterial(this->materials[AM_O], false);
588 rotVis->AddChild(rotFullVis);
589
590 this->visuals[TransformAxis::TA_ROTATION_X] = rotXVis;
591 this->visuals[TransformAxis::TA_ROTATION_Y] = rotYVis;
592 this->visuals[TransformAxis::TA_ROTATION_Z] = rotZVis;
593 // Store the full rotation visual in this->visuals using a key
594 // that's not already occupied by the TransformAxis enum
595 this->visuals[TransformAxis::TA_ROTATION_Z << 1] = rotFullVis;
596
597 // rotation handles
598 VisualPtr rotHandleXVis = this->Scene()->CreateVisual();
599 rotHandleXVis->AddGeometry(this->Scene()->CreateMesh(rotHandleMeshName));
600 rotHandleXVis->SetMaterial(this->materials[AM_HANDLE], false);
601 rotXVis->AddChild(rotHandleXVis);
602
603 VisualPtr rotHandleYVis = this->Scene()->CreateVisual();
604 rotHandleYVis->AddGeometry(this->Scene()->CreateMesh(rotHandleMeshName));
605 rotHandleYVis->SetMaterial(this->materials[AM_HANDLE], false);
606 rotYVis->AddChild(rotHandleYVis);
607
608 VisualPtr rotHandleZVis = this->Scene()->CreateVisual();
609 rotHandleZVis->AddGeometry(this->Scene()->CreateMesh(rotHandleMeshName));
610 rotHandleZVis->SetMaterial(this->materials[AM_HANDLE], false);
611 rotZVis->AddChild(rotHandleZVis);
612
613 this->handles[TransformAxis::TA_ROTATION_X] = rotHandleXVis;
614 this->handles[TransformAxis::TA_ROTATION_Y] = rotHandleYVis;
615 this->handles[TransformAxis::TA_ROTATION_Z] = rotHandleZVis;
616
617 this->AddChild(rotVis);
618 }
619
621 template <class T>
623 {
624 VisualPtr scaleVis = this->Scene()->CreateVisual();
625
626 // scale x
627 VisualPtr scaleXVis = this->Scene()->CreateVisual();
628 VisualPtr scaleShaftXVis = this->Scene()->CreateVisual();
629 scaleShaftXVis->AddGeometry(this->Scene()->CreateCylinder());
630 scaleShaftXVis->SetOrigin(0, 0, 0.5);
631 scaleShaftXVis->SetLocalPosition(0, 0, 0.5);
632 scaleShaftXVis->SetLocalScale(0.02, 0.02, 0.5);
633 scaleXVis->AddChild(scaleShaftXVis);
634
635 VisualPtr scaleHeadXVis = this->Scene()->CreateVisual();
636 scaleHeadXVis->AddGeometry(this->Scene()->CreateBox());
637 scaleHeadXVis->SetOrigin(0, 0, -0.5);
638 scaleHeadXVis->SetLocalPosition(0, 0, 0.5);
639 scaleHeadXVis->SetLocalScale(0.07, 0.07, 0.07);
640 scaleXVis->AddChild(scaleHeadXVis);
641
642 scaleXVis->SetMaterial(this->materials[AM_X], false);
643 scaleXVis->SetLocalRotation(0, GZ_PI * 0.5, 0);
644 scaleVis->AddChild(scaleXVis);
645
646 // scale y
647 VisualPtr scaleYVis = this->Scene()->CreateVisual();
648 VisualPtr scaleShaftYVis = this->Scene()->CreateVisual();
649 scaleShaftYVis->AddGeometry(this->Scene()->CreateCylinder());
650 scaleShaftYVis->SetOrigin(0, 0, 0.5);
651 scaleShaftYVis->SetLocalPosition(0, 0, 0.5);
652 scaleShaftYVis->SetLocalScale(0.02, 0.02, 0.5);
653 scaleYVis->AddChild(scaleShaftYVis);
654
655 VisualPtr scaleHeadYVis = this->Scene()->CreateVisual();
656 scaleHeadYVis->AddGeometry(this->Scene()->CreateBox());
657 scaleHeadYVis->SetOrigin(0, 0, -0.5);
658 scaleHeadYVis->SetLocalPosition(0, 0, 0.5);
659 scaleHeadYVis->SetLocalScale(0.07, 0.07, 0.07);
660 scaleYVis->AddChild(scaleHeadYVis);
661
662 scaleYVis->SetMaterial(this->materials[AM_Y], false);
663 scaleYVis->SetLocalRotation(-GZ_PI * 0.5, 0, 0);
664 scaleVis->AddChild(scaleYVis);
665
666 // scale z
667 VisualPtr scaleZVis = this->Scene()->CreateVisual();
668 VisualPtr scaleShaftZVis = this->Scene()->CreateVisual();
669 scaleShaftZVis->AddGeometry(this->Scene()->CreateCylinder());
670 scaleShaftZVis->SetOrigin(0, 0, 0.5);
671 scaleShaftZVis->SetLocalPosition(0, 0, 0.5);
672 scaleShaftZVis->SetLocalScale(0.02, 0.02, 0.5);
673 scaleZVis->AddChild(scaleShaftZVis);
674
675 VisualPtr scaleHeadZVis = this->Scene()->CreateVisual();
676 scaleHeadZVis->AddGeometry(this->Scene()->CreateBox());
677 scaleHeadZVis->SetOrigin(0, 0, -0.5);
678 scaleHeadZVis->SetLocalPosition(0, 0, 0.5);
679 scaleHeadZVis->SetLocalScale(0.07, 0.07, 0.07);
680 scaleZVis->AddChild(scaleHeadZVis);
681
682 scaleZVis->SetMaterial(this->materials[AM_Z], false);
683 scaleVis->AddChild(scaleZVis);
684
685 this->visuals[TransformAxis::TA_SCALE_X] = scaleXVis;
686 this->visuals[TransformAxis::TA_SCALE_Y] = scaleYVis;
687 this->visuals[TransformAxis::TA_SCALE_Z] = scaleZVis;
688
689 // scale handles
690 VisualPtr scaleHandleXVis = this->Scene()->CreateVisual();
691 scaleHandleXVis->AddGeometry(this->Scene()->CreateCylinder());
692 scaleHandleXVis->SetLocalPosition(0, 0, 0.285);
693 scaleHandleXVis->SetLocalScale(0.11, 0.11, 0.57);
694 scaleHandleXVis->SetMaterial(this->materials[AM_HANDLE], false);
695 scaleXVis->AddChild(scaleHandleXVis);
696
697 VisualPtr scaleHandleYVis = this->Scene()->CreateVisual();
698 scaleHandleYVis->AddGeometry(this->Scene()->CreateCylinder());
699 scaleHandleYVis->SetLocalPosition(0, 0, 0.285);
700 scaleHandleYVis->SetLocalScale(0.11, 0.11, 0.57);
701 scaleHandleYVis->SetMaterial(this->materials[AM_HANDLE], false);
702 scaleYVis->AddChild(scaleHandleYVis);
703
704 VisualPtr scaleHandleZVis = this->Scene()->CreateVisual();
705 scaleHandleZVis->AddGeometry(this->Scene()->CreateCylinder());
706 scaleHandleZVis->SetLocalPosition(0, 0, 0.285);
707 scaleHandleZVis->SetLocalScale(0.11, 0.11, 0.57);
708 scaleHandleZVis->SetMaterial(this->materials[AM_HANDLE], false);
709 scaleZVis->AddChild(scaleHandleZVis);
710
711 this->handles[TransformAxis::TA_SCALE_X] = scaleHandleXVis;
712 this->handles[TransformAxis::TA_SCALE_Y] = scaleHandleYVis;
713 this->handles[TransformAxis::TA_SCALE_Z] = scaleHandleZVis;
714
715 this->AddChild(scaleVis);
716 }
717
719 template <class T>
721 {
722 auto it = this->visuals.find(_axis);
723 if (it != this->visuals.end())
724 return it->second;
725
726 return VisualPtr();
727 }
728
730 template <class T>
732 const math::Quaterniond &_rot)
733 {
734 math::Vector3d dir = _pos - this->WorldPosition();
735 dir = dir.Normalize();
736 dir = _rot.RotateVectorReverse(dir);
737 math::Vector3d xRot(atan2(-dir.Y(), dir.Z()), 0, 0);
738 math::Vector3d xRotOffset(0, -GZ_PI * 0.5, 0);
739 this->visuals[TransformAxis::TA_ROTATION_X]->SetWorldRotation(
740 _rot * math::Quaterniond(xRot) * math::Quaterniond(xRotOffset));
741
742 math::Vector3d yRot(0, atan2(dir.X(), dir.Z()), 0);
743 math::Vector3d yRotOffset(GZ_PI * 0.5, -GZ_PI * 0.5, 0);
744 this->visuals[TransformAxis::TA_ROTATION_Y]->SetWorldRotation(
745 _rot * math::Quaterniond(yRot) * math::Quaterniond(yRotOffset));
746
747 math::Vector3d zRot(0, 0, atan2(dir.Y(), dir.X()));
748 this->visuals[TransformAxis::TA_ROTATION_Z]->SetWorldRotation(
749 _rot * math::Quaterniond(zRot));
750
751 math::Matrix4d lookAt;
752 lookAt = lookAt.LookAt(_pos, this->WorldPosition());
753 math::Vector3d circleRotOffset(0, GZ_PI * 0.5, 0);
754 this->visuals[TransformAxis::TA_ROTATION_Z << 1]->SetWorldRotation(
755 lookAt.Rotation() * math::Quaterniond(circleRotOffset));
756 }
757 }
758 }
759}
760#endif