void Trackball<Real>::Tracking(int x, int y)
	{

		Celer::Quaternion<Real> q;
		Celer::Vector3<Real> endVector(Real(x), Real(y), 0);

		MapToSphere(endVector);
		q.ToRotationArc(startVector, endVector);
		mOrientation = q * startOrientation;

		//mOrientation.Normalize();
	}
Beispiel #2
0
void Viewer::trackballRotate(const QVector2D& startCoords,
                             const QVector2D& endCoords,
                             QMatrix4x4* mat) {
  // TODO: We could get these from a common location
  QVector2D trackballCenter(width() / 2.0, height() / 2.0);
  auto diam = std::min(width(), height()) / 2.0;

  // Convert into coordinates centered at trackball center
  auto start = (startCoords - trackballCenter);
  auto end = (endCoords - trackballCenter);
  // Y coordinates are upside down
  start[1] *= -1;
  end[1] *= -1;

  end *= 2.0 / diam;
  start *= 2.0 / diam;

  // Vectors from center of "trackball" to points
  QVector3D endVector(end[0], end[1], 1 - end[0]*end[0] - end[1]*end[1]);
  QVector3D startVector(start[0], start[1], 1 - start[0]*start[0] - start[1]*start[1]);

  // Cheat because vector would be const
  for (auto vp : {&endVector, &startVector}) {
    auto& v = *vp;
    if (v[2] < 0) {
      // Mouse outside trackball: rotation about Z
      v /= std::sqrt(1 - v[2]);
      v[2] = 0;
    }
    else {
      v[2] = std::sqrt(v[2]);
    }
  }

  // Vector to rotate about. Length of vector is angle of rotation in radians.
  auto rotationVector = QVector3D::crossProduct(startVector, endVector);
  auto angleDeg = rotationVector.length() / (2 * M_PI) * 360;
  rotationVector.normalize();
  // Qt rotates on the right. We want the left.
  QMatrix4x4 rotMat;
  rotMat.rotate(angleDeg, rotationVector);
  *mat = rotMat * *mat;
}
/*鼠标拖拽响应*/
void SpaceTranformWindow::mouseMoveEvent(QMouseEvent* event)
{
    QPoint mousePos = event->pos();

    /*鼠标在图片框内*/
    if(ui->ShowImage->rect().contains(mousePos))
    {
        switch(_Mode)
        {
        case DONOTHING: /*不做处理*/
        {
            break;
        }
        case POSITION:  /*平移*/
        {
            if(mousePos != _vectorStart)
            {
                _VectorMove += (mousePos - _vectorStart);

                _ShowImageCenter += (mousePos - _vectorStart);

                _vectorStart = mousePos;
                transformImage();
            }
            break;
        }
        case ROTATION:  /*旋转*/
        {
            /*旋转角度*/
            double endAngle;
            double angleTurned;

            endAngle = atan2(mousePos.y()-_ShowImageCenter.y(),
                             mousePos.x()-_ShowImageCenter.x());
            angleTurned = endAngle - _TurnAngle;
            if(angleTurned!=0)
            {
                _TransformAngle += angleTurned;
                _TurnAngle = endAngle;
                transformImage();
            }
            break;
        }
        case RESIZE_LIMITED:    /*等比例缩放*/
        {
            QPoint strVector(_vectorStart.x()-_ShowImageCenter.x(),
                             _vectorStart.y()-_ShowImageCenter.y());
            QPoint endVector(mousePos.x()-_ShowImageCenter.x(),
                             mousePos.y()-_ShowImageCenter.y());

            if(strVector.x()!=0)
            {
                _Scale_width_ShowImage = static_cast<double>(endVector.x())/strVector.x();
                if(_Scale_width_ShowImage <=0)
                {
                    _Scale_width_ShowImage =1;
                }
            }
            else
            {
                _Scale_width_ShowImage = 1;
            }

            _Scale_height_ShowImage = _Scale_width_ShowImage;

            transformImage();
            break;
        }
        case RESIZE_UNLIMITED:  /*非等比例缩放*/
        {
            QPoint strVector(_vectorStart.x()-_ShowImageCenter.x(),
                             _vectorStart.y()-_ShowImageCenter.y());
            QPoint endVector(mousePos.x()-_ShowImageCenter.x(),
                             mousePos.y()-_ShowImageCenter.y());

            if(strVector.x()!=0)
            {
                _Scale_width_ShowImage = static_cast<double>(endVector.x())/strVector.x();
                if(_Scale_width_ShowImage <=0)
                {
                    _Scale_width_ShowImage =1;
                }
            }
            else
            {
                _Scale_width_ShowImage = 1;
            }


            if(strVector.y()!=0)
            {
                _Scale_height_ShowImage = static_cast<double>(endVector.y())/strVector.y();
                if(_Scale_height_ShowImage <=0)
                {
                    _Scale_height_ShowImage =1;
                }
            }
            else
            {
                _Scale_height_ShowImage = 1;
            }

            transformImage();
            break;
        }
        }
    }

    /*鼠标在图片框外*/
    else
    {
        mouseReleaseEvent(event);
        _Mode = DONOTHING;
    }
}
void sphericalBlendShapeVisualizerDrawOverride::addUIDrawables(
								const MDagPath& objPath,
								MHWRender::MUIDrawManager& drawManager,
								const MHWRender::MFrameContext& frameContext,
								const MUserData* data)
{
	sphericalBlendShapeVisualizerData* pLocatorData = (sphericalBlendShapeVisualizerData*)data;
	if (!pLocatorData)
	{
		return;
	}

	MMatrix spaceMatrix    = pLocatorData->fSpaceMatrix;
	MMatrix spaceInvMatrix = spaceMatrix.inverse();

	short poleAxis      = pLocatorData->fPoleAxis;
	short seamAxis      = pLocatorData->fSeamAxis;

	drawManager.beginDrawable();

	MColor lineColor, vertexColor, poleAxisColor, seamAxisColor;
	lineColor		= MColor(0.7294f, .239216f, 0.2980f, 1.0f);
	vertexColor		= MColor(0.5843f, 0.78824f, .17255f, 1.0f);
	poleAxisColor   = MColor(1.0f, 0.0f, 0.f, 1.0f);
	seamAxisColor   = MColor(0.0f, 1.0f, 0.0f, 1.0f);

	MMatrix identity;
	identity.setToIdentity();

	double radius   = 1.0;
	int numRings    = 20;
	int numSections = 20;

	MPoint sphericalPoint, xyzPoint;
	MPoint startPoint, endPoint, firstPoint;
	MPointArray points;

	drawManager.setDepthPriority(5);

	drawManager.setColor(lineColor);

	bboxPoints.clear();
	bboxPoints.setLength(numRings*numSections);

	for(int ring=0; ring<=numRings; ring++) {
		double azimuth = (double)ring / numRings * M_PI * 2;

		for(int section=0; section<=numSections; section++) {
			double zenith = (double)section / (numSections) * M_PI;

			sphericalPoint.x = radius;
			sphericalPoint.y = zenith;
			sphericalPoint.z = azimuth;
			
			if (section==0) {
				sphericalToCartesian(sphericalPoint, poleAxis, seamAxis, startPoint);
				startPoint = startPoint * spaceMatrix;
				firstPoint = startPoint;
				bboxPoints.append(firstPoint);
				continue;
			} else {
				sphericalToCartesian(sphericalPoint, poleAxis, seamAxis, endPoint);
				endPoint = endPoint * spaceMatrix;
				drawManager.line(startPoint, endPoint);
				bboxPoints.append(endPoint);
				startPoint = endPoint;
			}
		}
	}

	for(int ring=0; ring<=numRings; ring++) {
		double azimuth = (double)ring / numRings * M_PI * 2;

		for(int section=0; section<=numSections; section++) {
			double zenith = (double)section / (numSections) * M_PI;

			sphericalPoint.x = radius;
			sphericalPoint.y = azimuth;
			sphericalPoint.z = zenith;
			
			if (section==0) {
				sphericalToCartesian(sphericalPoint, poleAxis, seamAxis, startPoint);
				startPoint = startPoint * spaceMatrix;
				firstPoint = startPoint;
				continue;
			} else {
				sphericalToCartesian(sphericalPoint, poleAxis, seamAxis, endPoint);
				endPoint = endPoint * spaceMatrix;
				drawManager.line(startPoint, endPoint);
				startPoint = endPoint;
			}
		}
	}

	drawManager.setLineWidth(3.0);
	drawManager.setLineStyle(MHWRender::MUIDrawManager::kDashed);
	
	startPoint = MPoint(0, 0, 0) * spaceMatrix;


	MVector endVector(0, 0, 0);
	setAxis(endVector, poleAxis, radius);
	endPoint = MPoint(endVector) * spaceMatrix;

	drawManager.setColor(poleAxisColor);
	drawManager.line(startPoint, endPoint);

	endVector = MVector(0, 0, 0);
	setAxis(endVector, seamAxis, radius);
	endPoint = MPoint(endVector) * spaceMatrix;

	drawManager.setColor(seamAxisColor);
	drawManager.line(startPoint, endPoint);
	drawManager.endDrawable();
}