use of maspack.matrix.VectorBase in project artisynth_core by artisynth.
the class SetHandler method initializeWidget.
public static boolean initializeWidget(LabeledComponentBase widget, Property prop) {
String name = prop.getName();
PropertyInfo info = prop.getInfo();
Class<?> type = info.getValueClass();
if (widget instanceof LabeledWidget) {
LabeledWidget lwidget = (LabeledWidget) widget;
if (textIsEmpty(lwidget.getLabelText())) {
lwidget.setLabelText(name);
}
if (textIsEmpty(lwidget.getToolTipText())) {
lwidget.setToolTipText(info.getDescription());
}
}
if (widget instanceof LabeledControl) {
removeOldListeners((LabeledControl) widget);
}
try {
if (String.class.isAssignableFrom(type)) {
// if String has a range, then use StringSelector
// otherwise, use a simple StringField
Range stringRange = prop.getRange();
if (info.isInheritable() || stringRange == null || !(stringRange instanceof StringRange) || ((StringRange) stringRange).isWildcard()) {
StringField stringField = (StringField) widget;
stringField.setColumns(20);
stringField.addValueChangeListener(new PropChangeListener(prop) {
public void valueChange(ValueChangeEvent e) {
if (e.getValue() == null || e.getValue().equals("")) {
super.valueChange(new ValueChangeEvent(e.getSource(), ""));
} else {
super.valueChange(e);
}
}
});
stringField.setStretchable(true);
} else {
String[] constants = ((StringRange) stringRange).getValidStrings();
StringSelector selector = (StringSelector) widget;
selector.setSelections(constants, null);
selector.addValueChangeListener(new PropChangeListener(prop));
}
} else if (type == double.class || type == float.class || type == Double.class || type == Float.class) {
DoubleField doubleField = (DoubleField) widget;
Range range = prop.getRange();
if (range instanceof NumericInterval) {
doubleField.setRange((NumericInterval) range);
}
// }
if (info.getPrintFormat() != null && formatIsDefault(doubleField)) {
doubleField.setFormat(info.getPrintFormat());
}
GuiUtils.setFixedWidth(doubleField.getTextField(), 100);
doubleField.addValueChangeListener(new PropChangeListener(prop));
} else if (type == int.class || type == Integer.class) {
IntegerField intField = (IntegerField) widget;
GuiUtils.setFixedWidth(intField.getTextField(), 100);
intField.addValueChangeListener(new PropChangeListener(prop));
} else if (type == boolean.class || type == Boolean.class) {
if (info.isReadOnly()) {
StringField stringField = (StringField) widget;
stringField.setColumns(5);
} else {
BooleanSelector selector = (BooleanSelector) widget;
selector.addValueChangeListener(new PropChangeListener(prop));
}
} else if (VectorBase.class.isAssignableFrom(type) && info.getDimension() != -1) {
VectorBase resultVec;
try {
resultVec = (VectorBase) type.newInstance();
} catch (Exception e) {
throw new InternalErrorException("Error creating no-args instance of " + type);
}
if (resultVec instanceof VectorNd) {
((VectorNd) resultVec).setSize(info.getDimension());
}
VectorField vectorField = (VectorField) widget;
// from scratch)
if (vectorField.getVectorSize() != info.getDimension()) {
vectorField.setVectorSize(info.getDimension());
} else {
VectorNd existingValue = vectorField.getVectorValue();
if (existingValue != null) {
resultVec.set(existingValue);
}
}
vectorField.setResultHolder(resultVec);
vectorField.addValueChangeListener(new PropChangeListener(prop));
if (info.getPrintFormat() != null && formatIsDefault(vectorField)) {
vectorField.setFormat(info.getPrintFormat());
}
vectorField.setStretchable(true);
} else if (VectoriBase.class.isAssignableFrom(type) && info.getDimension() != -1) {
VectoriBase resultVec;
try {
resultVec = (VectoriBase) type.newInstance();
} catch (Exception e) {
throw new InternalErrorException("Error creating no-args instance of " + type);
}
if (resultVec instanceof VectorNi) {
((VectorNi) resultVec).setSize(info.getDimension());
}
VectoriField vectorField = (VectoriField) widget;
// from scratch)
if (vectorField.getVectorSize() != info.getDimension()) {
vectorField.setVectorSize(info.getDimension());
} else {
VectorNi existingValue = vectorField.getVectorValue();
if (existingValue != null) {
resultVec.set(existingValue);
}
}
vectorField.setResultHolder(resultVec);
vectorField.addValueChangeListener(new PropChangeListener(prop));
if (info.getPrintFormat() != null && formatIsDefault(vectorField)) {
vectorField.setFormat(info.getPrintFormat());
}
vectorField.setStretchable(true);
} else if (VectorBase.class.isAssignableFrom(type) && info.getDimension() == -1) {
VectorBase resultVec;
try {
resultVec = (VectorBase) type.newInstance();
} catch (Exception e) {
throw new InternalErrorException("Error creating no-args instance of " + type);
}
VariableVectorField vectorField = (VariableVectorField) widget;
VectorNd existingValue = vectorField.getVectorValue();
if (vectorField.getVectorSize() != resultVec.size()) {
resultVec.setSize(vectorField.getVectorSize());
}
if (existingValue != null) {
resultVec.set(existingValue);
}
vectorField.setResultHolder(resultVec);
vectorField.addValueChangeListener(new PropChangeListener(prop));
if (info.getPrintFormat() != null && formatIsDefault(vectorField)) {
vectorField.setFormat(info.getPrintFormat());
}
vectorField.setStretchable(true);
} else if (SymmetricMatrix3d.class.isAssignableFrom(type)) {
SymmetricMatrix3dField matrixField = (SymmetricMatrix3dField) widget;
matrixField.addValueChangeListener(new PropChangeListener(prop));
if (info.getPrintFormat() != null && formatIsDefault(matrixField)) {
matrixField.setFormat(info.getPrintFormat());
}
matrixField.setStretchable(true);
} else if (RigidTransform3d.class.isAssignableFrom(type)) {
RigidTransformWidget transformField = (RigidTransformWidget) widget;
transformField.addValueChangeListener(new PropChangeListener(prop));
transformField.setStretchable(true);
} else if (AffineTransform3d.class.isAssignableFrom(type)) {
AffineTransformWidget transformField = (AffineTransformWidget) widget;
transformField.addValueChangeListener(new PropChangeListener(prop));
transformField.setStretchable(true);
} else if (Rectangle2d.class.isAssignableFrom(type)) {
RectangleField rectField = (RectangleField) widget;
rectField.addValueChangeListener(new PropChangeListener(prop));
if (info.getPrintFormat() != null && formatIsDefault(rectField)) {
rectField.setFormat(info.getPrintFormat());
}
rectField.setStretchable(true);
} else // }
if (AxisAngle.class.isAssignableFrom(type)) {
AxisAngleField orientationField = (AxisAngleField) widget;
orientationField.addValueChangeListener(new PropChangeListener(prop));
orientationField.setStretchable(true);
} else if (Enum.class.isAssignableFrom(type)) {
Enum<?>[] constants = null;
Range range = prop.getRange();
if (range != null && range instanceof EnumRange) {
constants = ((EnumRange<?>) range).getValidEnums();
} else {
constants = (Enum[]) type.getEnumConstants();
}
if (info.isReadOnly()) {
StringField stringField = (StringField) widget;
int ncols = 0;
for (int i = 0; i < constants.length; i++) {
int len = constants[i].toString().length();
if (len > ncols) {
ncols = len;
}
}
stringField.setColumns(ncols);
} else {
EnumSelector selector = (EnumSelector) widget;
selector.setSelections(constants, null);
selector.addValueChangeListener(new PropChangeListener(prop));
}
} else if (Color.class.isAssignableFrom(type)) {
ColorSelector selector = (ColorSelector) widget;
if (info.getNullValueOK()) {
selector.enableNullColors();
}
selector.addValueChangeListener(new PropChangeListener(prop));
} else if (IntegerInterval.class.isAssignableFrom(type)) {
IntegerIntervalField rangeField = (IntegerIntervalField) widget;
rangeField.addValueChangeListener(new PropChangeListener(prop));
rangeField.setStretchable(true);
} else if (NumericInterval.class.isAssignableFrom(type)) {
DoubleIntervalField rangeField = (DoubleIntervalField) widget;
rangeField.addValueChangeListener(new PropChangeListener(prop));
rangeField.setStretchable(true);
} else if (GLGridResolution.class.isAssignableFrom(type)) {
GridResolutionField resField = (GridResolutionField) widget;
resField.addValueChangeListener(new PropChangeListener(prop));
} else if (Font.class.isAssignableFrom(type)) {
FontField fontField = (FontField) widget;
fontField.addValueChangeListener(new PropChangeListener(prop));
} else if (CompositeProperty.class.isAssignableFrom(type)) {
if (widget instanceof CompositePropertyWidget) {
CompositePropertyWidget compProp = (CompositePropertyWidget) widget;
compProp.setProperty(prop);
} else {
CompositePropertyPanel compProp = (CompositePropertyPanel) widget;
compProp.setExpandState(info.getWidgetExpandState());
compProp.initializeSelection(prop);
}
} else {
return false;
}
} catch (ClassCastException e) {
throw new IllegalArgumentException("widget type " + widget.getClass() + " inappropriate for property type " + type);
}
// finishWidget (widget, prop);
return true;
}
use of maspack.matrix.VectorBase in project artisynth_core by artisynth.
the class FemModel3dAgent method createPreviewModel.
private void createPreviewModel() {
fem = new FemModel3d();
setProperties(fem, getPrototypeComponent(myComponentType));
setProperties(myPrototype, myPrototype);
FemElementType elemType = null;
FemMeshType meshType = (FemMeshType) meshSelector.getValue();
if (elemSelector.isEnabledAll()) {
elemType = (FemElementType) elemSelector.getValue();
}
switch(meshType) {
case Grid:
{
VectorBase dims = gridDimField.getVectorValue();
int[] divs = gridDivField.getVectorValue();
FemFactory.createGrid(fem, elemType, dims.get(0), dims.get(1), dims.get(2), divs[0], divs[1], divs[2]);
break;
}
case Tube:
{
VectorBase dims = tubeDimField.getVectorValue();
int[] divs = tubeDivField.getVectorValue();
FemFactory.createTube(fem, elemType, dims.get(0), dims.get(1), dims.get(2), divs[0], divs[1], divs[2]);
break;
}
case Torus:
{
VectorBase dims = torusDimField.getVectorValue();
int[] divs = torusDivField.getVectorValue();
FemFactory.createTorus(fem, elemType, dims.get(0), dims.get(1), dims.get(2), divs[0], divs[1], divs[2]);
break;
}
case Sphere:
{
int nodes = (Integer) sphereNodesField.getValue();
String meshPath;
if (nodes == SPHERE_NODE_OPTIONS[0]) {
meshPath = ArtisynthPath.getHomeRelativePath(SPHERE_54_MESH_PATH, ".");
} else if (nodes == SPHERE_NODE_OPTIONS[1]) {
meshPath = ArtisynthPath.getHomeRelativePath(SPHERE_196_MESH_PATH, ".");
} else {
EditorUtils.showError(myDisplay, "Invalid number of nodes for sphere");
return;
}
try {
TetGenReader.read(fem, 1000, meshPath + ".node", meshPath + ".ele", new Vector3d(1, 1, 1));
} catch (Exception e) {
EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
return;
}
break;
}
case Extrusion:
{
double d = extrusDepthField.getDoubleValue();
int n = extrusLayersField.getIntValue();
String meshFileName = extrusFileField.getStringValue();
try {
PolygonalMesh mesh = new PolygonalMesh(new File(meshFileName));
FemFactory.createExtrusion(fem, elemType, n, d, 0, mesh);
} catch (Exception e) {
EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
return;
}
break;
}
case AnsysMesh:
{
String nodeFileName = ansysNodeFileField.getStringValue();
String elemFileName = ansysElemFileField.getStringValue();
try {
AnsysReader.read(fem, nodeFileName, elemFileName, 1000, null, /*options=*/
0);
} catch (Exception e) {
EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
return;
}
break;
}
case TetgenMesh:
{
String nodeFileName = tetgenNodeFileField.getStringValue();
String eleFileName = tetgenEleFileField.getStringValue();
try {
TetGenReader.read(fem, 1000, nodeFileName, eleFileName, new Vector3d(1, 1, 1));
} catch (Exception e) {
EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
return;
}
break;
}
case UCDMesh:
{
String ucdFileName = ucdMeshFileField.getStringValue();
try {
UCDReader.read(fem, ucdFileName, 1000);
} catch (Exception e) {
EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
return;
}
break;
}
case SurfaceMesh:
{
String objFileName = surfaceMeshFileField.getStringValue();
PolygonalMesh surfaceMesh = null;
try {
surfaceMesh = new PolygonalMesh(new File(objFileName));
} catch (Exception e) {
EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
return;
}
try {
FemFactory.createFromMesh(fem, surfaceMesh, /*quality=*/
2.0);
} catch (Exception e) {
e.printStackTrace();
EditorUtils.showError(myDisplay, "Error tessellating mesh: " + e.getMessage());
return;
}
break;
}
default:
{
throw new InternalErrorException("Unimplemented mesh type");
}
}
RigidTransform3d X = new RigidTransform3d();
X.p.set(positionField.getVectorValue());
X.R.setAxisAngle(orientationField.getAxisAngleValue());
PolygonalMesh mesh = fem.getSurfaceMesh();
RenderProps props = mesh.createRenderProps();
props.setFaceStyle(Renderer.FaceStyle.NONE);
props.setDrawEdges(true);
props.setLineColor(Color.LIGHT_GRAY);
mesh.setRenderProps(props);
mesh.setMeshToWorld(X);
mesh.setFixed(false);
mesh.setRenderBuffered(false);
if (meshPropPanel.getComponentIndex(scaleField) != -1) {
scaleField.maskValueChangeListeners(true);
scaleField.setValue(1.0);
scaleField.maskValueChangeListeners(false);
lastScale = 1.0;
}
myMain.getWorkspace().getViewerManager().addRenderable(mesh);
rotator = new Transrotator3d();
GLViewer viewer = myMain.getMain().getViewer();
rotator.setDraggerToWorld(X);
rotator.setSize(viewer.distancePerPixel(viewer.getCenter()) * viewer.getScreenWidth() / 6);
rotator.addListener(new FemModelDraggerListener());
myMain.getWorkspace().getViewerManager().addDragger(rotator);
myMain.rerender();
}
Aggregations