use of maspack.matrix.Vector2d in project artisynth_core by artisynth.
the class GLMouseAdapter method mousePressed.
public void mousePressed(MouseEvent e) {
int mask = (e.getModifiersEx() & ALL_MODIFIERS);
int selectionMask = getSelectionButtonMask();
// Start checking draggers
Dragger3d drawTool = viewer.myDrawTool;
boolean grabbed = false;
if (viewer.myDraggers.size() > 0 || drawTool != null) {
MouseRayEvent rayEvt = MouseRayEvent.create(e, viewer);
for (Dragger3d d : viewer.myDraggers) {
// pass event to any visible Dragger3dBase, or any other Dragger3d
if (d.isVisible()) {
updateDraggerModeAndFlags(mask, d);
if (d.mousePressed(rayEvt)) {
grabbed = true;
dragAction = DRAGGER_ACTION;
break;
}
}
}
if (drawTool != null && drawTool.isVisible()) {
updateDraggerModeAndFlags(mask, drawTool);
if (drawTool.mousePressed(rayEvt)) {
grabbed = true;
dragAction = DRAGGER_ACTION;
}
}
}
if (grabbed) {
viewer.repaint();
} else {
if (viewer.getEllipticCursorActive() && (mask & ellipticCursorResizeMask) == ellipticCursorResizeMask) {
dragAction = ELLIPTIC_CURSOR_RESIZE;
ellipticCursorStartSize = new Vector2d(viewer.getEllipticCursorSize());
} else if (mask == getTranslateButtonMask()) {
dragAction = TRANSLATE;
} else if (mask == getRotateButtonMask()) {
dragAction = ROTATE;
} else if (mask == getZoomButtonMask()) {
dragAction = ZOOM;
} else if (viewer.isSelectionEnabled() && (mask & selectionMask) == selectionMask) {
if (viewer.getSelectOnPress()) {
checkForSelection(e);
dragAction = NO_ACTION;
viewer.setDragBox(null);
} else {
if (viewer.getEllipticSelection()) {
checkForSelection(e);
}
dragAction = DRAG_SELECT;
}
} else {
dragAction = NO_ACTION;
}
}
dragStartX = lastX = e.getX();
dragStartY = lastY = e.getY();
}
use of maspack.matrix.Vector2d in project artisynth_core by artisynth.
the class FemModel3d method findNearestSurfaceElement.
/**
* Returns the nearest surface element to a specified point,
* which is found by projecting the point onto the FEM surface.
* The location of the projection is returned in <code>loc</code>.
*
* @param loc Projected location of the point onto the surface.
* @param pnt Point for which nearest surface element is desired.
* @return Nearest surface element.
*/
public FemElement3d findNearestSurfaceElement(Point3d loc, Point3d pnt) {
Vector2d coords = new Vector2d();
PolygonalMesh surf = getSurfaceMesh();
if (surf == null || surf.numFaces() == 0) {
surf = getInternalSurfaceMesh();
}
if (surf != null) {
Face face = BVFeatureQuery.getNearestFaceToPoint(loc, coords, surf, pnt);
FemElement3d elem = getSurfaceElement(face);
if (elem == null) {
throw new InternalErrorException("surface element not found for face");
}
return elem;
} else {
return null;
}
}
use of maspack.matrix.Vector2d in project artisynth_core by artisynth.
the class SkinMeshBody method createPointAttachment.
public PointSkinAttachment createPointAttachment(Point pnt) {
if (!(getMesh() instanceof PolygonalMesh)) {
return null;
}
PolygonalMesh mesh = (PolygonalMesh) getMesh();
if (!mesh.isTriangular()) {
return null;
}
// Find nearest face to the point; we'll need this to
// estimate a basePosition for the attachments from the
// start by find
BVFeatureQuery query = new BVFeatureQuery();
Point3d near = new Point3d();
Vector2d uv = new Vector2d();
Face face = query.nearestFaceToPoint(near, uv, mesh, pnt.getPosition());
// Create a new PointSkinAttachment
MeshDistCalc dcalc = new MeshDistCalc();
dcalc.computeDistancesAndWeights(pnt.getPosition(), myLastSigma);
PointSkinAttachment a = dcalc.computeDisplacementAttachment();
a.setSkinMesh(this);
// Now estimate the basePosition from the face vertices
Point3d basePos = new Point3d();
Vertex3d[] vtxs = face.getTriVertices();
double[] wgts = new double[] { 1 - uv.x - uv.y, uv.x, uv.y };
for (int i = 0; i < vtxs.length; i++) {
PointSkinAttachment va = (PointSkinAttachment) myVertexAttachments.get(vtxs[i].getIndex());
basePos.scaledAdd(wgts[i], va.getBasePosition());
}
a.setBasePosition(basePos);
a.setPoint(pnt);
return a;
}
use of maspack.matrix.Vector2d in project artisynth_core by artisynth.
the class FemMeshComp method createPointAttachment.
public PointFem3dAttachment createPointAttachment(Point pnt) {
if (!(getMesh() instanceof PolygonalMesh)) {
return null;
}
PolygonalMesh mesh = (PolygonalMesh) getMesh();
if (!mesh.isTriangular()) {
return null;
}
// Find nearest face to the point. The vertices of this face will be used
// to find the nodes and weight for the attachment.
BVFeatureQuery query = new BVFeatureQuery();
Point3d near = new Point3d();
Vector2d uv = new Vector2d();
Face face = query.nearestFaceToPoint(near, uv, mesh, pnt.getPosition());
Vertex3d[] vtxs = face.getTriVertices();
double[] wgts = new double[] { 1 - uv.x - uv.y, uv.x, uv.y };
HashMap<FemNode, Double> nodeWeights = new HashMap<FemNode, Double>();
for (int i = 0; i < vtxs.length; i++) {
PointAttachment va = myVertexAttachments.get(vtxs[i].getIndex());
if (va instanceof PointParticleAttachment) {
PointParticleAttachment ppa = (PointParticleAttachment) va;
FemNode node = (FemNode) ppa.getParticle();
accumulateNodeWeights(node, wgts[i], nodeWeights);
} else if (va instanceof PointFem3dAttachment) {
PointFem3dAttachment pfa = (PointFem3dAttachment) va;
for (int k = 0; k < pfa.numMasters(); k++) {
FemNode node = pfa.getNodes()[k];
double w = pfa.getCoordinate(k);
accumulateNodeWeights(node, w * wgts[i], nodeWeights);
}
}
}
// Create a new PointFem3dAttachment
PointFem3dAttachment ax = new PointFem3dAttachment(pnt);
VectorNd weightVec = new VectorNd();
for (Double d : nodeWeights.values()) {
weightVec.append(d);
}
ax.setFromNodes(nodeWeights.keySet(), weightVec);
return ax;
}
use of maspack.matrix.Vector2d in project artisynth_core by artisynth.
the class MeshIntersectingProbe method findNextFace.
// does not add the vertex, goes in a loop around contour until we hit a face
private Face findNextFace(Point3d pnt, LinkedList<Point3d> contour, OBBTree obbt, Intersector2d ti, Vector3d vx, Vector3d vy, Point3d o, SplitStorage info) {
Face face = null;
Vector3d dir = new Vector3d();
if (info.idx < contour.size() - 1) {
dir.sub(contour.get(info.idx + 1), contour.get(info.idx));
} else {
dir.sub(contour.get(info.idx), contour.get(info.idx - 1));
}
dir.normalize();
while (face == null && info.idx < contour.size() - 1) {
Point3d pntNext = contour.get(info.idx + 1);
Point2d pnt2d = Intersector2d.get2dCoordinate(pnt, vx, vy, o);
Point2d pnt2dNext = Intersector2d.get2dCoordinate(pntNext, vx, vy, o);
Vector2d diff2d = new Vector2d();
ArrayList<BVNode> bvNodes = new ArrayList<BVNode>();
// get close nodes
obbt.intersectLineSegment(bvNodes, pnt, pntNext);
double minDist = Double.MAX_VALUE;
Point2d minPnt = null;
for (BVNode node : bvNodes) {
for (int i = 0; i < node.getNumElements(); i++) {
Boundable ps = node.getElements()[i];
if (ps instanceof Face) {
Face f = (Face) ps;
Vertex3d[] vtxs = f.getVertices();
Point2d p0 = Intersector2d.get2dCoordinate(vtxs[0].getWorldPoint(), vx, vy, o);
Point2d p1 = Intersector2d.get2dCoordinate(vtxs[1].getWorldPoint(), vx, vy, o);
Point2d p2 = Intersector2d.get2dCoordinate(vtxs[2].getWorldPoint(), vx, vy, o);
ArrayList<Point2d> points = new ArrayList<Point2d>();
ti.intersectTriangleLineSegment(p0, p1, p2, pnt2d, pnt2dNext, points);
// check points
for (Point2d p : points) {
diff2d.sub(p, pnt2d);
if (diff2d.norm() < minDist) {
face = f;
minDist = diff2d.norm();
minPnt = p;
}
}
}
}
}
if (face == null || minDist >= pnt2dNext.distance(pnt2d)) {
face = null;
// move to next point
info.idx++;
pnt = contour.get(info.idx);
if (info.idx < contour.size() - 1) {
dir.sub(contour.get(info.idx + 1), pnt);
}
} else {
// snap to edge if within tolerance
Point3d pos = Intersector2d.get3dCoordinate(minPnt, vx, vy, o);
if (pos.distance(pnt) < ti.epsilon) {
pos.set(pnt);
}
// check if we have to make a new vertex
info.vtx = createOrGetVertex(pos, face.getVertices(), ti.epsilon);
}
}
return face;
}
Aggregations