Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OMEdit] Heuristics for scaling vector visualizers #9389

Conversation

anotheruserofgithub
Copy link
Contributor

@anotheruserofgithub anotheruserofgithub commented Sep 17, 2022

Purpose

This PR implements methods for the automatic adjustment of radius & length scales of vector visualizers at initialization.
Some fixes are provided for non-critical issues.
Improvements in efficiency are also included.

Approach

Four new attributes are attached to each vector visualizer and can be independently enabled or disabled:

  • adjustable-radius,
  • adjustable-length,
  • scale-invariant,
  • drawn-on-top.

The first two attributes inform whether radius scale and length scale can be adjusted, respectively.
The third attribute allows to continuously maintain a constant pixel size through the auto-scale feature of OSG's AutoTransform, particularly when zooming in/out the scene, which is very convenient to be able to compare vector lengths (meaning, their relative strength) all along the simulation.
The fourth attribute serves as a reinforcement for the previous one, because it renders vectors on top of everything else drawn in the scene, therefore not only small vectors but also tips of arrow heads are always visible and comparable (although this may sometimes break the notion of perspective).

Two separate kinds of adjustments are applied:

  1. Radius scale: Browse fixed-radius vectors as well as relevant shapes to compute the median of their radii, and use this median value (± some constant factor) to scale the default radius of adjustable-radius vectors.
  2. Length scale: For each vector quantity independently, increase the length of adjustable-length vectors for them to be clearly visible, while ensuring that the following two constraints are satisfied (both ± some constant margin), the first one taking precedence over the second one:
    • shaft lengths must be greater than zero in order to be comparable,
    • final camera distance to focal center must not be greater than initial distance computed without drawing adjustable-length vectors.

Implemented heuristics are thoroughly documented above and inside method VisualizationAbstract::chooseVectorScales.
Default values of their parameters are defined at the beginning of this same method.

Tests

  • Modelica.Mechanics.MultiBody.Examples.Elementary.ForceAndTorque
  • Below model containing only visualizers (shapes and vectors)
TestScalingVectorVisualizers.mo (click to expand)
model TestScalingVectorVisualizers "Demonstrate animation of vector visualizers with automatic initial scaling"
  constant Boolean testMinimumLengthConstraint=true;
  import MB = Modelica.Mechanics.MultiBody;
  inner MB.World world;
  MB.Visualizers.Advanced.Shape s1(
    shapeType="cylinder",
    R=MB.Frames.nullRotation(),
    r={0,0,0},
    r_shape={0,0,0},
    lengthDirection={-1,0,0},
    widthDirection={0,1,0},
    length=0.45,
    width=0.035,
    height=0.035,
    extra=0.0,
    color={255,0,0},
    specularCoefficient=0.7
  );
  MB.Visualizers.Advanced.Shape s2(
    shapeType="cylinder",
    R=MB.Frames.nullRotation(),
    r={0,0,0},
    r_shape={0,0,0},
    lengthDirection={-1,0,0},
    widthDirection={0,1,0},
    length=0.45,
    width=0.035,
    height=0.035,
    extra=0.0,
    color={255,0,0},
    specularCoefficient=0.7
  );
  MB.Visualizers.Advanced.Shape s3(
    shapeType="cylinder",
    R=MB.Frames.nullRotation(),
    r={0,0,0},
    r_shape={0,0,0},
    lengthDirection={-1,0,0},
    widthDirection={0,1,0},
    length=0.45,
    width=0.035,
    height=0.035,
    extra=0.0,
    color={255,0,0},
    specularCoefficient=0.7
  );
  MB.Visualizers.Advanced.Shape s4(
    shapeType="cylinder",
    R=MB.Frames.nullRotation(),
    r={0,0,0},
    r_shape={0,0,0},
    lengthDirection={-1,0,0},
    widthDirection={0,1,0},
    length=0.45,
    width=0.035,
    height=0.035,
    extra=0.0,
    color={255,0,0},
    specularCoefficient=0.7
  );
  MB.Visualizers.Advanced.Shape s5(
    shapeType="cylinder",
    R=MB.Frames.nullRotation(),
    r={0,0,0},
    r_shape={0,0,0},
    lengthDirection={-1,0,0},
    widthDirection={0,1,0},
    length=0.45,
    width=0.035,
    height=0.035,
    extra=0.0,
    color={255,0,0},
    specularCoefficient=0.7
  );
  MB.Visualizers.Advanced.Vector v1(
    R=MB.Frames.nullRotation(),
    r={0,0,0},
    coordinates={(time+1)*1200,0,0},
    color={0,0,255},
    specularCoefficient=0.7,
    quantity=MB.Types.VectorQuantity.Force,
    headAtOrigin=false,
    twoHeadedArrow=false
  );
  MB.Visualizers.Advanced.Vector v2(
    R=MB.Frames.nullRotation(),
    r={0,0,0},
    coordinates={-(time+1)*120,0,0},
    color={0,255,0},
    specularCoefficient=0.7,
    quantity=MB.Types.VectorQuantity.Torque,
    headAtOrigin=false,
    twoHeadedArrow=false
  );
  MB.Visualizers.Advanced.Vector v3(
    R=MB.Frames.nullRotation(),
    r={0,0.1,0},
    coordinates={(time+1)*2*1200,0,0},
    color={0,0,255},
    specularCoefficient=0.7,
    quantity=MB.Types.VectorQuantity.Force,
    headAtOrigin=true,
    twoHeadedArrow=true
  );
  MB.Visualizers.Advanced.Vector v4(
    R=MB.Frames.nullRotation(),
    r={0,0.1,0},
    coordinates={-(time+1)*2*120,0,0},
    color={0,255,0},
    specularCoefficient=0.7,
    quantity=MB.Types.VectorQuantity.Torque,
    headAtOrigin=true,
    twoHeadedArrow=true
  );
  MB.Visualizers.Advanced.Vector v5(
    R=MB.Frames.nullRotation(),
    r={0,0.2,0},
    coordinates={(time+1)*8*1200,0,0},
    color={0,0,255},
    specularCoefficient=0.7,
    quantity=MB.Types.VectorQuantity.Force,
    headAtOrigin=false,
    twoHeadedArrow=false
  ) if testMinimumLengthConstraint;
  MB.Visualizers.Advanced.Vector v6(
    R=MB.Frames.nullRotation(),
    r={0,0.2,0},
    coordinates={-(time+1)*8*120,0,0},
    color={0,255,0},
    specularCoefficient=0.7,
    quantity=MB.Types.VectorQuantity.Torque,
    headAtOrigin=false,
    twoHeadedArrow=false
  ) if testMinimumLengthConstraint;
  MB.Visualizers.Advanced.Vector v7(
    R=MB.Frames.nullRotation(),
    r={-0.1,0.3,0},
    coordinates={-1,2,0},
    color={255,0,255},
    specularCoefficient=0.7,
    quantity=MB.Types.VectorQuantity.Velocity,
    headAtOrigin=false,
    twoHeadedArrow=false
  );
  MB.Visualizers.Advanced.Vector v8(
    R=MB.Frames.nullRotation(),
    r={-0.1,0.3,0},
    coordinates={-2,1,0},
    color={255,255,0},
    specularCoefficient=0.7,
    quantity=MB.Types.VectorQuantity.Acceleration,
    headAtOrigin=false,
    twoHeadedArrow=true
  );
  MB.Visualizers.Advanced.Vector v9(
    R=MB.Frames.nullRotation(),
    r={-0.1,-0.2,0},
    coordinates={-1,-2,0},
    color={255,0,155},
    specularCoefficient=0.7,
    quantity=MB.Types.VectorQuantity.AngularVelocity,
    headAtOrigin=false,
    twoHeadedArrow=false
  );
  MB.Visualizers.Advanced.Vector v10(
    R=MB.Frames.nullRotation(),
    r={-0.1,-0.2,0},
    coordinates={-2,-1,0},
    color={255,155,0},
    specularCoefficient=0.7,
    quantity=MB.Types.VectorQuantity.AngularAcceleration,
    headAtOrigin=false,
    twoHeadedArrow=true
  );
  MB.Visualizers.Advanced.Vector v11(
    R=MB.Frames.nullRotation(),
    r={0,-0.25,0},
    coordinates={0.5,0.75,0},
    color={0,255,255},
    specularCoefficient=0.7,
    quantity=MB.Types.VectorQuantity.RelativePosition,
    headAtOrigin=false,
    twoHeadedArrow=false
  );
end TestScalingVectorVisualizers;

Related Issues

@anotheruserofgithub
Copy link
Contributor Author

@adeas31 May I ask you to please have a look? This should go in v1.20 since animation of Vector will be introduced there.

Any feedback will be appreciated!

Copy link
Member

@adeas31 adeas31 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

So the pending messages thing is only for debugging and is not actually used at the moment, right?

Please re-run the PR. @adrpo has fixed the CMake build.

@anotheruserofgithub anotheruserofgithub force-pushed the omedit_multibody_visualizers_vector_scaling branch 3 times, most recently from 2b33aa8 to 4718a55 Compare September 23, 2022 09:05
@adeas31
Copy link
Member

adeas31 commented Sep 23, 2022

/usr/bin/ld: ../OMEditLIB/libOMEditLib.a(Visualization.cpp.o): undefined reference to symbol 'glClear'

Are you missing some dependency?

@anotheruserofgithub anotheruserofgithub force-pushed the omedit_multibody_visualizers_vector_scaling branch from 4718a55 to 4185d3c Compare September 23, 2022 10:02
@anotheruserofgithub
Copy link
Contributor Author

/usr/bin/ld: ../OMEditLIB/libOMEditLib.a(Visualization.cpp.o): undefined reference to symbol 'glClear'

Are you missing some dependency?

Yeah, unfortunately I have to make a direct call to glClear() because OSG has no wrapper for it, so I need to add -lGL explicitly for the linker. I think I finally got it right, let's see what happens. Weird that it was working with make but not cmake, as I was not linking to GL in configure.ac either (added it now).

@anotheruserofgithub
Copy link
Contributor Author

anotheruserofgithub commented Sep 23, 2022

Well, now that I am thinking of it, it may be possible to actually set the clear mask on the OSG render stage, or using its ClearNode, instead of calling glClear() directly ... Let me try something before merging ;)

@anotheruserofgithub
Copy link
Contributor Author

I tried both ideas but could not make them work. Anyway, how I did is probably the most efficient way (despite having to explicitly link GL) so let's keep it like that.

@anotheruserofgithub
Copy link
Contributor Author

And because the first commit changed the dumping of BCONST I also have to update the test models, let me take care of that.

@adeas31 adeas31 added the CI/Build MINGW Build pull request with Windows MSYS2 MINGW64 label Sep 23, 2022
@adeas31
Copy link
Member

adeas31 commented Sep 23, 2022

Maybe you need to fix the Windows libs linking as well. I added the Windows build. Lets see.

@anotheruserofgithub anotheruserofgithub force-pushed the omedit_multibody_visualizers_vector_scaling branch from 4185d3c to b12262b Compare September 23, 2022 11:02
@anotheruserofgithub
Copy link
Contributor Author

It passed all the builds, but I don't have a Windows installation to check that it works in practice.

@anotheruserofgithub
Copy link
Contributor Author

So the pending messages thing is only for debugging and is not actually used at the moment, right?

Exactly. I could have been more explicit in the documentation but it would have become a bit verbose.

This would typically be used for dumping the values of computed scales, or also (I had not thought about this before, but it is very unlikely) to raise warnings for the user and give hints on how to solve a problem.

Basically, it is crucial that the AutoTransform's scale is not changed during the whole adjustment process. Meaning, we must not go through a cull traversal, thus we must not render any new frame (except, of course, when triggering it explicitly with the frame argument). And the problem with GUI messages is that if the "Messages Browser" is not yet open, it will open, thus changing the aspect ratio of the animation window, therefore firing a Qt paint event hence a new OSG frame. This was one of the key issues I had with AutoTransform.

Of course, because we run single-threaded, the mutex is not necessary, but I think it is very much worth it because if someone does not know or forgets about this issue with GUI messages, then instead of seeing weird length scales (difficult to debug) they will actually encounter a deadlock, which is easy to spot directly in the code or with a debugger. Also, if one day the scene initialization or scale adjustment is decoupled from the frame rendering in separate threads, this should work straight away.

@adeas31
Copy link
Member

adeas31 commented Sep 23, 2022

I added the MINGW label that will trigger the Windows build.

@anotheruserofgithub anotheruserofgithub force-pushed the omedit_multibody_visualizers_vector_scaling branch from b12262b to db3fecd Compare September 23, 2022 12:35
@anotheruserofgithub anotheruserofgithub force-pushed the omedit_multibody_visualizers_vector_scaling branch from db3fecd to c5cd7b7 Compare September 23, 2022 12:37
@anotheruserofgithub anotheruserofgithub force-pushed the omedit_multibody_visualizers_vector_scaling branch from db3fecd to 47828d9 Compare September 23, 2022 12:44
@anotheruserofgithub anotheruserofgithub force-pushed the omedit_multibody_visualizers_vector_scaling branch from 47828d9 to c5cd7b7 Compare September 23, 2022 12:46
@anotheruserofgithub anotheruserofgithub force-pushed the omedit_multibody_visualizers_vector_scaling branch 3 times, most recently from 710faa7 to 9017eda Compare September 23, 2022 13:02
@anotheruserofgithub
Copy link
Contributor Author

What a nightmare ... Sorry for the mess, I'm not used to github PRs yet.
I think it's good now, right?

@adeas31
Copy link
Member

adeas31 commented Sep 26, 2022

@anotheruserofgithub its failing again. I restarted the job.

@anotheruserofgithub
Copy link
Contributor Author

Error: Curl error for URL https://github.com/modelica-3rdparty/Modelica_DeviceDrivers/archive/61247667adad45a1b769bfa0cd5011e33eafa513.zip: SSL connect error
Makefile:11: recipe for target '.openmodelica/libraries/20200610130629.stamp' failed
make[1]: *** [.openmodelica/libraries/20200610130629.stamp] Error 1

I guess it's not my fault ... Is it possible to rerun only the failing test or do you have to restart from the beginning?

@adeas31
Copy link
Member

adeas31 commented Sep 26, 2022

We have to rerun the whole thing. I restarted the job. We will see.

@adeas31
Copy link
Member

adeas31 commented Sep 26, 2022

Looks good now. Do you want me to merge or you have more changes?

@anotheruserofgithub
Copy link
Contributor Author

It's OK, you can merge, please :)

@adeas31 adeas31 merged commit d85e729 into OpenModelica:master Sep 26, 2022
@anotheruserofgithub anotheruserofgithub deleted the omedit_multibody_visualizers_vector_scaling branch September 26, 2022 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/Build MINGW Build pull request with Windows MSYS2 MINGW64
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants