CAE Tutorial: Displaying a Color Legend

This step reviews how to create a color legend bar.

Defining a Colormap

The first step towards defining a legend bar is to setup a color map in the HOOPS/3dGS scene-graph. The mesh used to represent the legend bar will have it's vertex colors set as indices into this colormap, as will the actual fea-mesh geometry that has been 'solved' for some physical propertly. The quantity and value of the colors in the colormap are completely dependent on the needs of your CAE application.

The tutorial defines the legened in HAnalysisModel::Init and first creates a sample colormap with 13 colors that gives a good range from dark blue to reddish orange :

float colors[39] = { 0,0,1, 0, 0.333333f, 1, 0, 0.666667f, 1,
0,1,1, 0, 1, 0.666667f, 0, 1, 0.333333f,
0,1,0, 0.333333f, 1, 0, 0.666667f, 1, 0,
1,1,0, 1, 0.666667f, 0, 1, 0.333333f, 0,
1,0,0 };
{
//set up legend segment
HC_Set_Color_Map_By_Value( "RGB", 13, colors);
...
}

Defining the Legendbar

The legend bar will have two aspects to it; the text strings denoting the meaning of colormap values in the color chart, and the color chart itself. The entire legendbar should be defined within a new segment under the model segment.

The new segment should contain a new HOOPS/3dGS orthographic camera. This will cause the legend bar to be immune to other camera orbit/rotate/pan changes in the HBaseView segment that would otherwise inherit down into HBaseModel. (HOOPS/3dGS segments do not inherit their parent's attributes if they already have that attribute explicitly set.)

{
//set up legend segment
HC_Set_Color_Map_By_Value( "RGB", 13, colors);
HC_Open_Segment("legendbar");
HPoint pos(0.5, 0.0, 1.0), target(0.5, 0.0, -1.0), up(0.0, 1.0, 0.0);
HC_Set_Camera( &pos, &target, &up, 5.0, 3.0, "orthographic" );

Next, we need to set up some important attributes:

  • The legend geometry was defined with a left-handed coordinates system so we'll reflect that.
  • We don't want the legendbar to be selectable or affected by any cutting planes, shadow settings, or backplane culling.
  • Setting the depth range Rendering_Option ensures that it will get overlayed on top of the model.
  • We'll enable anti-aliasing for the text and set a default set set of colors for the legend bar components.
HC_Set_Selectability("geometry=off");
HC_Set_Visibility("cutting planes = off");
HC_Set_Rendering_Options("depth range = (0,1e-4), simple shadow=off, anti-alias = text");
HC_Set_Heuristics("no backplane culls");
HC_Set_Color("edge=line=text=R=0 G=0 B=0");

 

Now, we'll insert the legend text, left align it, and give it an 'oru' Set_Text_Font setting so that it scales and turn off tranforms for the text. The 'size tolerance' keeps it scaling smoothly:

HC_Set_Text_Font("size=.07 oru, size tolerance=0 points, no transforms, line spacing=1");
HC_Insert_Text( 0.5f, 14.0f, 0.0f, "von Mises (psi)");
HC_Insert_Text( -.5f, 0.0f, 0.0f, "9.155e+001");
HC_Insert_Text( -.5f, 1.0f, 0.0f, "9.208e+003");
HC_Insert_Text( -.5f, 2.0f, 0.0f, "1.833e+004");
HC_Insert_Text( -.5f, 3.0f, 0.0f, "2.744e+004");
HC_Insert_Text( -.5f, 4.0f, 0.0f, "3.656e+004");
HC_Insert_Text( -.5f, 5.0f, 0.0f, "4.568e+004");
HC_Insert_Text( -.5f, 6.0f, 0.0f, "5.479e+004");
HC_Insert_Text( -.5f, 7.0f, 0.0f, "6.391e+004");
HC_Insert_Text( -.5f, 8.0f, 0.0f, "7.303e+004");
HC_Insert_Text( -.5f, 9.0f, 0.0f, "8.214e+004");
HC_Insert_Text( -.5f, 10.0f, 0.0f, "9.126e+004");
HC_Insert_Text( -.5f, 11.0f, 0.0f, "1.004e+005");
HC_Insert_Text( -.5f, 12.0f, 0.0f, "1.095e+005");

Finally, we set a modelling matrix to orient the legend bar to the central-right part of the scene, and define the actual 'color chart' in a subsegment. Note that we use a HOOPS/3dGS shell to represent the color bar and specify face colors by FIndex so it has it's own color contour information.

float mm[] = { 0.1, 0.0, 0.0, 0.0,
0.0, 0.1, 0.0, 0.0,
0.0, 0.0, 0.1, 0.0,
-1.00, -.75, 0.0, 1.0};
HC_Open_Segment("chart");
float const points[3*26] =
{ 0,0,0, 1,0,0, 0,1,0, 1,1,0,
0,2,0, 1,2,0, 0,3,0, 1,3,0,
0,4,0, 1,4,0, 0,5,0, 1,5,0,
0,6,0, 1,6,0, 0,7,0, 1,7,0,
0,8,0, 1,8,0, 0,9,0, 1,9,0,
0,10,0, 1,10,0, 0,11,0, 1,11,0,
0,12,0, 1,12,0
};
int const chart_flist[5*12] =
{ 4, 0,1,3,2,
4, 2,3,5,4,
4, 4,5,7,6,
4, 6,7,9,8,
4, 8,9,11,10,
4, 10,11,13,12,
4, 12,13,15,14,
4, 14,15,17,16,
4, 16,17,19,18,
4, 18,19,21,20,
4, 20,21,23,22,
4, 22,23,25,24
};
HC_Set_Visibility("faces=on,edges=on,lights=off");
HC_KEY shell_key = HC_KInsert_Shell( 26, points, 60, chart_flist);
int i, p_count=13, flist_length=12;
float *indices, *vertex_colors;
// set a default face color
int f_count = flist_length;
indices = new float[ f_count ];
for ( i = 0 ; i < f_count ; i++ ) {
indices[i] = (float)i;
}
HC_MSet_Face_Colors_By_FIndex( shell_key, "faces", 0, f_count, indices);
delete [] indices;
}

Since the legend bar creating is performed in the HAnalysisModel::Init, it will be a part of any new model object that is created and initialized. Experiment by selecting either File->New or File->Open from the menu, which both create new models.