17 #ifndef GZ_RENDERING_BASE_BASEJOINTVISUAL_HH_
18 #define GZ_RENDERING_BASE_BASEJOINTVISUAL_HH_
23 #include "gz/common/Console.hh"
36 inline namespace GZ_RENDERING_VERSION_NAMESPACE {
51 protected:
virtual void Init()
override;
54 protected:
virtual void PreRender()
override;
57 protected:
virtual void Destroy()
override;
61 bool _useParentFrame)
override;
67 public:
virtual void SetParentAxis(
70 bool _useParentFrame)
override;
77 bool _useParentFrame)
override;
80 public:
virtual bool UpdateParentAxis(
82 bool _useParentFrame)
override;
97 public:
virtual void SetVisible(
bool _visible)
override;
106 bool _useParentFrame);
109 protected:
void CreateAxis();
112 protected:
void CreateParentAxis();
115 protected:
void ScaleToChild();
138 protected:
bool dirtyJointType =
false;
141 protected:
bool dirtyAxis =
false;
144 protected:
bool dirtyParentAxis =
false;
152 protected:
bool useParentFrame =
false;
155 protected:
bool updateAxis =
false;
166 protected:
bool parentAxisUseParentFrame =
false;
169 protected:
bool updateParentAxis =
false;
190 if (this->ParentAxisVisual())
192 this->ParentAxisVisual()->PreRender();
195 if (this->dirtyJointType)
197 this->UpdateAxis(this->axis, this->useParentFrame);
198 this->UpdateParentAxis(this->parentAxis,
199 this->parentAxisUseParentFrame);
201 this->dirtyJointType =
false;
207 this->dirtyAxis =
false;
210 if (this->dirtyParentAxis)
212 this->CreateParentAxis();
213 this->dirtyParentAxis =
false;
216 if (this->updateAxis)
219 !this->UpdateAxis(this->axis, this->useParentFrame);
222 if (this->updateParentAxis)
224 this->updateParentAxis =
225 !this->UpdateParentAxis(this->parentAxis,
226 this->parentAxisUseParentFrame);
237 this->AddChild(this->axisVisual);
238 this->SetInheritScale(
false);
245 if (this->arrowVisual !=
nullptr)
247 this->arrowVisual->Destroy();
248 this->arrowVisual.reset();
251 if (this->axisVisual !=
nullptr)
253 this->axisVisual->Destroy();
254 this->axisVisual.reset();
257 if (this->parentAxisVis !=
nullptr)
259 this->parentAxisVis->Destroy();
260 this->parentAxisVis.reset();
263 this->dirtyJointType =
false;
264 this->dirtyAxis =
false;
265 this->dirtyParentAxis =
false;
274 bool _useParentFrame)
277 this->useParentFrame = _useParentFrame;
278 this->dirtyAxis =
true;
285 if (this->arrowVisual)
287 this->arrowVisual->Destroy();
288 this->arrowVisual.reset();
292 this->arrowVisual->SetMaterial(
"Default/TransYellow");
293 this->arrowVisual->SetLocalPosition(0, 0, 0);
294 this->arrowVisual->SetLocalRotation(0, 0, 0);
295 this->AddChild(this->arrowVisual);
297 this->updateAxis =
true;
298 this->ScaleToChild();
306 bool _useParentFrame)
311 gzlog <<
"Joint visual is not of type Revolute2 or "
313 <<
" so the parent axis will not be shown\n";
317 this->parentAxis = _axis;
318 this->parentAxisUseParentFrame = _useParentFrame;
319 this->jointParentName = _parentName;
320 this->dirtyParentAxis =
true;
327 auto jointParentVis = this->
Scene()->
NodeByName(this->jointParentName);
328 if (jointParentVis ==
nullptr)
330 gzlog <<
"Joint parent with name " << this->jointParentName
332 <<
" so the parent axis will not be shown\n";
336 if (this->parentAxisVis)
338 this->parentAxisVis->Destroy();
339 this->parentAxisVis.reset();
343 jointParentVis->AddChild(this->parentAxisVis);
344 this->parentAxisVis->SetType(this->Type());
345 this->parentAxisVis->SetAxis(this->parentAxis,
346 this->parentAxisUseParentFrame);
348 this->updateParentAxis =
true;
349 this->ScaleToChild();
355 bool _useParentFrame)
359 this->UpdateAxisImpl(this->
ArrowVisual(), _axis, _useParentFrame);
370 bool _useParentFrame)
372 if (this->ParentAxisVisual() &&
374 this->ParentAxisVisual()->HasParent())
376 this->UpdateAxisImpl(this->ParentAxisVisual()->
ArrowVisual(),
377 _axis, _useParentFrame);
388 bool _useParentFrame)
394 double cosTheta = v.
Dot(u);
395 double angle = acos(cosTheta);
402 _arrowVisual->SetLocalRotation(quat);
407 this->Parent()->InitialLocalPose();
411 (parentInitPose * this->LocalPose()).Rot();
415 _arrowVisual->SetLocalRotation(quatFromModel.
Inverse() *
416 _arrowVisual->LocalRotation());
419 _arrowVisual->ShowArrowRotation(
425 if (this->axisVisual)
426 _arrowVisual->SetVisible(
true);
435 auto axisWorldRotation = _arrowVisual->WorldPose().Rot();
436 auto jointWorldRotation = this->WorldPose().Rot();
438 this->axisVisual->ShowAxisHead(
true);
439 _arrowVisual->ShowArrowShaft(
true);
444 this->axisVisual->ShowAxisHead(0,
false);
445 _arrowVisual->ShowArrowShaft(
false);
447 else if (axisWorld ==
450 this->axisVisual->ShowAxisHead(1,
false);
451 _arrowVisual->ShowArrowShaft(
false);
453 else if (axisWorld ==
456 this->axisVisual->ShowAxisHead(2,
false);
457 _arrowVisual->ShowArrowShaft(
false);
465 if (!this->HasParent())
470 std::dynamic_pointer_cast<Visual>(this->Parent());
475 std::max(0.1, parentVisual->BoundingBox().Size().Length());
477 childSize * 0.2, childSize * 0.2);
478 this->SetLocalScale(this->scaleToChild);
479 if (this->ParentAxisVisual())
480 this->ParentAxisVisual()->SetLocalScale(this->scaleToChild);
488 this->jointVisualType = _type;
489 this->dirtyJointType =
true;
503 return this->parentAxis;
510 return this->jointVisualType;
517 return this->parentAxisVis;
524 return this->arrowVisual;
531 T::SetVisible(_visible);
539 if (this->ParentAxisVisual())
540 this->ParentAxisVisual()->SetVisible(_visible);
543 if (this->axisVisual)
544 this->axisVisual->SetVisible(_visible);