#include <maya/MIOStream.h>
#include <math.h>
#include <stdlib.h>
#include <maya/MFnPlugin.h>
#include <maya/MString.h>
#include <maya/MGlobal.h>
#include <maya/M3dView.h>
#include <maya/MDagPath.h>
#include <maya/MItSelectionList.h>
#include <maya/MPxContextCommand.h>
#include <maya/MPxContext.h>
#include <maya/MCursor.h>
#include <maya/MEvent.h>
#if defined(OSMac_MachO_)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include <maya/MPoint.h>
#include <maya/MPointArray.h>
#include <maya/MItCurveCV.h>
#include <maya/MItSurfaceCV.h>
#include <maya/MItMeshVertex.h>
#include <maya/MItMeshEdge.h>
#include <maya/MItMeshPolygon.h>
#ifdef _WIN32
LPCSTR lassoToolCursor = "lassoToolCursor.cur";
#else
#include <lassoToolCursor.h>
#include <lassoToolCursorMask.h>
#define lassoToolCursor_x_hot 1
#define lassoToolCursor_y_hot 16
#endif
class coord {
public:
short h;
short v;
};
extern "C" int xycompare( coord *p1, coord *p2 );
int xycompare( coord *p1, coord *p2 )
{
if ( p1->v > p2->v )
return 1;
if ( p2->v > p1->v )
return -1;
if ( p1->h > p2->h )
return 1;
if ( p2->h > p1->h )
return -1;
return 0;
}
const int initialSize = 1024;
const int increment = 256;
const char helpString[] = "drag mouse to select points by encircling";
class lassoTool : public MPxContext
{
public:
lassoTool();
virtual ~lassoTool();
void* creator();
virtual void toolOnSetup( MEvent & event );
virtual MStatus doPress( MEvent & event );
virtual MStatus doDrag( MEvent & event );
virtual MStatus doRelease( MEvent & event );
private:
void append_lasso( short x, short y );
bool point_in_lasso( coord pt );
coord min;
coord max;
unsigned maxSize;
unsigned num_points;
coord* lasso;
MGlobal::ListAdjustment listAdjustment;
M3dView view;
MCursor lassoCursor;
};
lassoTool::lassoTool()
: maxSize(0)
, num_points(0)
, lasso(NULL)
#ifdef _WIN32
, lassoCursor(lassoToolCursor)
#else
, lassoCursor(lassoToolCursor_width,
lassoToolCursor_height,
lassoToolCursor_x_hot,
lassoToolCursor_y_hot,
lassoToolCursor_bits,
lassoToolCursorMask_bits)
#endif
{
setTitleString ( "Lasso Pick" );
setCursor(lassoCursor);
setImage("lassoTool.xpm", MPxContext::kImage1 );
}
lassoTool::~lassoTool() {}
void* lassoTool::creator()
{
return new lassoTool;
}
void lassoTool::toolOnSetup ( MEvent & )
{
setHelpString( helpString );
}
MStatus lassoTool::doPress( MEvent & event )
{
if (event.isModifierShift() || event.isModifierControl() ) {
if ( event.isModifierShift() ) {
if ( event.isModifierControl() ) {
listAdjustment = MGlobal::kAddToList;
} else {
listAdjustment = MGlobal::kXORWithList;
}
} else if ( event.isModifierControl() ) {
listAdjustment = MGlobal::kRemoveFromList;
}
} else {
listAdjustment = MGlobal::kReplaceList;
}
view = M3dView::active3dView();
view.beginGL();
view.beginOverlayDrawing();
maxSize = initialSize;
lasso = (coord*) malloc (sizeof(coord) * maxSize);
coord start;
event.getPosition( start.h, start.v );
num_points = 1;
lasso[0] = min = max = start;
return MS::kSuccess;
}
MStatus lassoTool::doDrag( MEvent & event )
{
coord currentPos;
event.getPosition( currentPos.h, currentPos.v );
append_lasso( currentPos.h, currentPos.v );
view.clearOverlayPlane();
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D(
0.0, (GLdouble) view.portWidth(),
0.0, (GLdouble) view.portHeight()
);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef(0.375, 0.375, 0.0);
glLineStipple( 1, 0x5555 );
glLineWidth( 1.0 );
glEnable( GL_LINE_STIPPLE );
glIndexi( 2 );
glBegin( GL_LINE_LOOP );
for ( unsigned i = 0; i < num_points ; i++ ){
glVertex2i( lasso[i].h, lasso[i].v );
}
glEnd();
#ifdef _WIN32
SwapBuffers( view.deviceContext() );
#elif defined (OSMac_)
::aglSwapBuffers(view.display());
#else
glXSwapBuffers( view.display(), view.window() );
#endif // _WIN32
glDisable( GL_LINE_STIPPLE );
return MS::kSuccess;
}
MStatus lassoTool::doRelease( MEvent & )
{
MStatus stat;
MSelectionList incomingList, boundingBoxList, newList;
view.clearOverlayPlane();
view.endOverlayDrawing();
view.endGL();
append_lasso(lasso[0].h, lasso[0].v);
qsort( &(lasso[0]), num_points, sizeof( coord ),
(int (*)(const void *, const void *))xycompare);
MGlobal::getActiveSelectionList(incomingList);
MGlobal::selectFromScreen( min.h, min.v, max.h, max.v,
MGlobal::kReplaceList );
MGlobal::getActiveSelectionList(boundingBoxList);
MGlobal::setActiveSelectionList(incomingList, MGlobal::kReplaceList);
MItSelectionList iter(boundingBoxList);
newList.clear();
for ( ; !iter.isDone(); iter.next() ) {
MDagPath dagPath;
MObject component;
MPoint point;
coord pt;
MObject singleComponent;
iter.getDagPath( dagPath, component );
if (component.isNull())
continue;
switch (component.apiType()) {
case MFn::kCurveCVComponent:
{
MItCurveCV curveCVIter( dagPath, component, &stat );
for ( ; !curveCVIter.isDone(); curveCVIter.next() ) {
point = curveCVIter.position(MSpace::kWorld, &stat );
view.worldToView( point, pt.h, pt.v, &stat );
if (!stat) {
stat.perror("Could not get position");
continue;
}
if ( point_in_lasso( pt ) ) {
singleComponent = curveCVIter.cv();
newList.add (dagPath, singleComponent);
}
}
break;
}
case MFn::kSurfaceCVComponent:
{
MItSurfaceCV surfCVIter( dagPath, component, true, &stat );
for ( ; !surfCVIter.isDone(); surfCVIter.next() ) {
point = surfCVIter.position(MSpace::kWorld, &stat );
view.worldToView( point, pt.h, pt.v, &stat );
if (!stat) {
stat.perror("Could not get position");
continue;
}
if ( point_in_lasso( pt ) ) {
singleComponent = surfCVIter.cv();
newList.add (dagPath, singleComponent);
}
}
break;
}
case MFn::kMeshVertComponent:
{
MItMeshVertex vertexIter( dagPath, component, &stat );
for ( ; !vertexIter.isDone(); vertexIter.next() ) {
point = vertexIter.position(MSpace::kWorld, &stat );
view.worldToView( point, pt.h, pt.v, &stat );
if (!stat) {
stat.perror("Could not get position");
continue;
}
if ( point_in_lasso( pt ) ) {
singleComponent = vertexIter.vertex();
newList.add (dagPath, singleComponent);
}
}
break;
}
case MFn::kMeshEdgeComponent:
{
MItMeshEdge edgeIter( dagPath, component, &stat );
for ( ; !edgeIter.isDone(); edgeIter.next() ) {
point = edgeIter.center(MSpace::kWorld, &stat );
view.worldToView( point, pt.h, pt.v, &stat );
if (!stat) {
stat.perror("Could not get position");
continue;
}
if ( point_in_lasso( pt ) ) {
singleComponent = edgeIter.edge();
newList.add (dagPath, singleComponent);
}
}
break;
}
case MFn::kMeshPolygonComponent:
{
MItMeshPolygon polygonIter( dagPath, component, &stat );
for ( ; !polygonIter.isDone(); polygonIter.next() ) {
point = polygonIter.center(MSpace::kWorld, &stat );
view.worldToView( point, pt.h, pt.v, &stat );
if (!stat) {
stat.perror("Could not get position");
continue;
}
if ( point_in_lasso( pt ) ) {
singleComponent = polygonIter.polygon();
newList.add (dagPath, singleComponent);
}
}
break;
}
default:
#ifdef DEBUG
cerr << "Selected unsupported type: (" << component.apiType()
<< "): " << component.apiTypeStr() << endl;
#endif
continue;
}
}
MGlobal::selectCommand(newList, listAdjustment);
free(lasso);
lasso = (coord*) 0;
maxSize = 0;
num_points = 0;
return MS::kSuccess;
}
void lassoTool::append_lasso( short x, short y )
{
int cy, iy, ix, ydif, yinc, i;
float fx, xinc;
iy = (int)lasso[num_points-1].v;
ix = (int)lasso[num_points-1].h;
ydif = abs( y - iy );
if ( ydif == 0 )
return;
if ( min.h > x )
min.h = x;
if ( max.h < x )
max.h = x;
if ( min.v > y )
min.v = y;
if ( max.v < y )
max.v = y;
if ( ((int)y - iy ) < 0 )
yinc = -1;
else
yinc = 1;
xinc = (float)((int)x - ix)/(float)ydif;
fx = (float)ix + xinc;
cy = iy + yinc;
for ( i = 0; i < ydif; i++ ) {
if ( num_points >= maxSize ) {
maxSize += increment;
lasso = (coord*) realloc (lasso, sizeof(coord) * maxSize);
}
lasso[num_points].h = (short) fx;
lasso[num_points].v = (short) cy;
fx += xinc;
cy += yinc;
num_points++;
}
return;
}
bool lassoTool::point_in_lasso( coord pt )
{
unsigned i, sides;
for ( i = 0; i < num_points; i++ ) {
if ( lasso[i].v == pt.v ) {
while ( (lasso[i].v == pt.v ) && (lasso[i].h < pt.h) )
i++;
if ( lasso[i].v != pt.v )
return( false );
sides = 0;
i++;
while ( lasso[i].v == pt.v ) {
i++;
sides++;
}
if ( sides % 2 )
return( false );
else
return( true );
}
}
return( false );
}
class lassoContextCmd : public MPxContextCommand
{
public:
lassoContextCmd() {};
virtual MPxContext* makeObj();
static void* creator();
};
MPxContext* lassoContextCmd::makeObj()
{
return new lassoTool;
}
void* lassoContextCmd::creator()
{
return new lassoContextCmd;
}
MStatus initializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj, PLUGIN_COMPANY, "3.0", "Any");
status = plugin.registerContextCommand( "lassoToolContext",
lassoContextCmd::creator );
if (!status) {
status.perror("registerContextCommand");
return status;
}
status = plugin.registerUI("lassoToolCreateUI", "lassoToolDeleteUI");
if (!status) {
status.perror("registerUIScripts");
return status;
}
return status;
}
MStatus uninitializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj );
status = plugin.deregisterContextCommand( "lassoToolContext" );
return status;
}