use of maspack.render.RenderObject in project artisynth_core by artisynth.
the class ColorBar method render.
@Override
public void render(Renderer renderer, int flags) {
if (!isSelectable() && renderer.isSelecting()) {
return;
}
RenderObject robj = myRenderObject;
VectorNd labelPos = myLabelPos;
ArrayList<String> labelText = myLabelText;
if (robj == null || labelPos == null || labelText == null) {
System.out.println((robj == null) + " " + (labelPos == null) + " " + (labelText == null));
return;
}
int screenWidth = renderer.getScreenWidth();
int screenHeight = renderer.getScreenHeight();
// turn off shading for the labels
Renderer.Shading savedShadeModel = renderer.getShading();
renderer.setShading(Renderer.Shading.NONE);
double x0 = myLoc.x;
double y0 = myLoc.y;
double w = myLoc.width;
double h = myLoc.height;
if (horizontal) {
h = myLoc.width;
w = myLoc.height;
x0 = myLoc.y;
y0 = myLoc.x;
}
// absolute or normalized -> absolute
if (Math.abs(x0) <= 1) {
x0 = x0 * screenWidth;
}
if (x0 < 0) {
x0 = screenWidth + x0;
}
if (Math.abs(y0) <= 1) {
y0 = y0 * screenHeight;
}
if (y0 < 0) {
y0 = screenHeight + y0;
}
if (w <= 1) {
w = w * screenWidth;
}
if (h <= 1) {
h = h * screenHeight;
}
renderer.pushModelMatrix();
renderer.setModelMatrix2d(0, screenWidth, 0, screenHeight);
// transform so that the colorbar occupies correct location
AffineTransform3d trans = new AffineTransform3d();
if (horizontal) {
trans.setRotation(ROT_Z_270);
trans.applyScaling(h, w, 1);
} else {
trans.applyScaling(w, h, 1);
}
trans.setTranslation(x0, y0, 0);
renderer.mulModelMatrix(trans);
renderer.setVertexColorMixing(ColorMixing.REPLACE);
renderer.drawTriangles(robj, 0);
float savedLineWidth = renderer.getLineWidth();
LineFaceRenderProps props = (LineFaceRenderProps) getRenderProps();
renderer.setLineWidth(props.getLineWidth());
renderer.setLineColoring(props, /*highlight=*/
false);
renderer.setVertexColorMixing(ColorMixing.NONE);
renderer.drawLines(robj, 0);
renderer.popModelMatrix();
// return line width
renderer.setLineWidth(savedLineWidth);
// labels
int nLabels = Math.min(labelPos.size(), labelText.size());
if (nLabels > 0) {
double tx, ty;
// // for consistency, assume line top as 3/4 font size
// double t = myTextSize*0.75;
// double vc = myTextSize* 0.25;
// double b = myTextSize*0.25;
renderer.setFaceColoring(props, isSelected());
float[] loc = new float[3];
for (int i = 0; i < nLabels; i++) {
tx = 0;
ty = 0;
// text orientation computation
String label = labelText.get(i);
Rectangle2D box = renderer.getTextBounds(myFont, label, myTextSize);
double bw = box.getWidth();
double bh = box.getHeight();
double b = box.getY();
double vc = b + bh / 2;
double t = bh + b;
if (horizontal) {
switch(hAlignment) {
case LEFT:
tx = x0 + myTextOffset.x;
break;
case CENTRE:
tx = x0 - bw / 2;
break;
case RIGHT:
tx = x0 - myTextOffset.x - bw;
break;
}
tx += w * labelPos.get(i);
switch(vAlignment) {
case BOTTOM:
ty = y0 - myTextOffset.y - t;
break;
case CENTRE:
ty = y0 - vc + h / 2;
break;
case TOP:
ty = y0 + h + myTextOffset.y + b;
break;
}
} else {
switch(hAlignment) {
case LEFT:
tx = x0 - myTextOffset.x - bw;
break;
case CENTRE:
tx = x0 - bw / 2 + w / 2;
break;
case RIGHT:
tx = x0 + myTextOffset.x + w;
break;
}
switch(vAlignment) {
case BOTTOM:
ty = y0 + myTextOffset.y;
break;
case CENTRE:
ty = y0 - vc;
break;
case TOP:
ty = y0 - myTextOffset.y - t;
break;
}
ty += h * labelPos.get(i);
}
loc[0] = (float) tx;
loc[1] = (float) ty;
renderer.drawText(myFont, label, loc, myTextSize);
}
}
renderer.setShading(savedShadeModel);
}
use of maspack.render.RenderObject in project artisynth_core by artisynth.
the class ColorBar method prerender.
@Override
public void prerender(RenderList list) {
RenderObject robj = myRenderObject;
if (robj == null || myRenderObjectRebuildRequest) {
robj = buildRenderObject(nBarDivisions, myColorMap, myLabelPos, myTickFractions);
myRenderObject = robj;
myRenderObjectRebuildRequest = false;
}
}
use of maspack.render.RenderObject in project artisynth_core by artisynth.
the class DicomViewer method buildRenderObject.
protected RenderObject buildRenderObject() {
RenderObject robj = new RenderObject();
float x = (float) getX();
float y = (float) getY();
float z = (float) getZ();
float[][] coords = { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 1, 0 } };
// xy-slice
Point2d[] texcoords = texture.getTextureCoordinates(DicomTextureContent.COL_ROW_PLANE);
robj.addNormal(0, 0, 1);
for (int i = 0; i < 4; ++i) {
robj.addPosition(coords[i][0], coords[i][1], z);
robj.addTextureCoord(texcoords[i]);
robj.addVertex();
}
// xz-slice
texcoords = texture.getTextureCoordinates(DicomTextureContent.COL_SLICE_PLANE);
robj.addNormal(0, 1, 0);
for (int i = 0; i < 4; ++i) {
robj.addPosition(coords[i][0], y, coords[i][1]);
robj.addTextureCoord(texcoords[i]);
robj.addVertex();
}
// yz-slice
texcoords = texture.getTextureCoordinates(DicomTextureContent.ROW_SLICE_PLANE);
robj.addNormal(1, 0, 0);
for (int i = 0; i < 4; ++i) {
robj.addPosition(x, coords[i][0], coords[i][1]);
robj.addTextureCoord(texcoords[i]);
robj.addVertex();
}
// three planes
for (int i = 0; i < 3; ++i) {
robj.createTriangleGroup();
int baseIdx = 4 * i;
robj.addTriangle(baseIdx, baseIdx + 1, baseIdx + 2);
robj.addTriangle(baseIdx, baseIdx + 2, baseIdx + 3);
}
// box coordinates
int vidx = robj.vertex(0, 0, 0);
robj.vertex(0, 1, 0);
robj.vertex(1, 1, 0);
robj.vertex(1, 0, 0);
robj.vertex(0, 0, 1);
robj.vertex(0, 1, 1);
robj.vertex(1, 1, 1);
robj.vertex(1, 0, 1);
final int[][] edges = { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 }, { 0, 4 }, { 1, 5 }, { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 4 }, { 2, 6 }, { 3, 7 } };
for (int[] edge : edges) {
robj.addLine(edge[0] + vidx, edge[1] + vidx);
}
return robj;
}
use of maspack.render.RenderObject in project artisynth_core by artisynth.
the class MuscleElementDescList method buildRenderObjects.
protected void buildRenderObjects() {
// allocate per-element flag storage that will be used to determine when
// the render object needs to be rebuilt
myRobFlags = new byte[size()];
for (int i = 0; i < size(); i++) {
MuscleElementDesc desc = get(i);
// note: flags default to 0 if elem.getRenderProps() != null
if (desc.getRenderProps() == null) {
myRobFlags[i] = getRobFlags(desc);
}
}
double wsize = getWidgetSize();
if (wsize > 0) {
RenderObject r = new RenderObject();
r.createTriangleGroup();
r.createTriangleGroup();
for (int i = 0; i < size(); i++) {
MuscleElementDesc desc = get(i);
if (desc.getRenderProps() == null) {
int group = desc.isSelected() ? SEL_GRP : REG_GRP;
r.triangleGroup(group);
FemElementRenderer.addWidgetFaces(r, desc.myElement);
}
}
myWidgetRob = r;
} else {
myWidgetRob = null;
}
}
use of maspack.render.RenderObject in project artisynth_core by artisynth.
the class FemElement3dList method buildRenderObjects.
protected void buildRenderObjects() {
RenderObject r = new RenderObject();
r.createLineGroup();
r.createLineGroup();
// r.createTriangleGroup();
// r.createTriangleGroup();
// r.createTriangleGroup();
myQuadEdges = null;
ComponentList<? extends FemNode> nodes = getNodeList();
if (nodes == null) {
// XXX what to do?
return;
}
// create positions and vertices for rendering lines
for (int i = 0; i < nodes.size(); i++) {
FemNode node = nodes.get(i);
r.addVertex(r.addPosition(node.myRenderCoords));
}
// allocate per-element flag storage that will be used to determine when
// the render object needs to be rebuilt
myRobFlags = new byte[size()];
boolean hasWidgets = false;
// for each element, add edge lines, plus widget triangles if the element
// has a non-zero widget size. Place these additions in the appropriate
// group (REG_GRP, SEL_GRP,INV_GRP), each of which will be rendered with
// a different color.
// two groups for edges
myEdgeFeatures = new FeatureIndexArray[2];
myEdgeFeatures[0] = new FeatureIndexArray();
myEdgeFeatures[1] = new FeatureIndexArray();
HashSet<EdgeDesc> edges = new HashSet<EdgeDesc>();
r.lineGroup(SEL_GRP);
for (int i = 0; i < size(); i++) {
FemElement3d elem = get(i);
if (elem.getRenderProps() == null) {
if (elem.isSelected()) {
myEdgeFeatures[SEL_GRP].beginFeature(i);
addEdgeLines(r, myEdgeFeatures[SEL_GRP], elem, nodes, edges);
myEdgeFeatures[SEL_GRP].endFeature();
}
}
}
r.lineGroup(REG_GRP);
for (int i = 0; i < size(); i++) {
FemElement3d elem = get(i);
if (elem.getRenderProps() == null) {
if (!elem.isSelected()) {
myEdgeFeatures[REG_GRP].beginFeature(i);
addEdgeLines(r, myEdgeFeatures[REG_GRP], elem, nodes, edges);
myEdgeFeatures[REG_GRP].endFeature();
}
byte flags = getRobFlags(elem);
if ((flags & HAS_WIDGET) != 0) {
hasWidgets = true;
}
myRobFlags[i] = flags;
}
}
myEdgeRob = r;
if (hasWidgets) {
r = new RenderObject();
r.createTriangleGroup();
r.createTriangleGroup();
r.createTriangleGroup();
myWidgetFeatures = new FeatureIndexArray[3];
for (int i = 0; i < myWidgetFeatures.length; ++i) {
myWidgetFeatures[i] = new FeatureIndexArray();
}
for (int i = 0; i < size(); i++) {
FemElement3d elem = get(i);
if (elem.getRenderProps() == null) {
byte flags = getRobFlags(elem);
if ((flags & HAS_WIDGET) != 0) {
int gidx = flags & GRP_MASK;
r.triangleGroup(gidx);
myWidgetFeatures[gidx].beginFeature(i);
FemElementRenderer.addWidgetFaces(r, myWidgetFeatures[gidx], elem);
myWidgetFeatures[gidx].endFeature();
}
}
}
myWidgetRob = r;
} else {
myWidgetRob = null;
}
}
Aggregations