Exemplo n.º 1
0
// Returns angle between 2 vectors in degrees
double ViroManipulator::VecAngle(osg::Vec3d a, osg::Vec3d b){
	double ang;
	a.normalize();
	b.normalize();
	ang = RadiansToDegrees( acos(a * b) );
	return fabs( ang );
}
Exemplo n.º 2
0
void CameraFlight::navigate(osg::Matrix destMat, osg::Vec3 destVec)
{
    osg::Matrix objMat = SceneManager::instance()->getObjectTransform()->getMatrix();

    switch(_flightMode)
    {
	case INSTANT:{
	    cout<<"USING INSTANT"<<endl;
	    SceneManager::instance()->setObjectMatrix(destMat);
	    break;
	}
	case SATELLITE:
	    cout<<"USING SATELLITE"<<endl;

	    t = 0.0;
	    total = 0.0;
	
    	    objMat.decompose(trans2, rot2, scale2, so2);
	    a = (maxHeight - trans2[1])/25.0;

	    map->getProfile()->getSRS()->getEllipsoid()->convertLatLongHeightToXYZ(
					destVec.x(),destVec.y(),destVec.z(),toVec.x(),toVec.y(),toVec.z());

	    fromVec = origPlanetPoint;

	    fromVec.normalize();
	    toVec.normalize();

	    origAngle = acos((fromVec * toVec)/((fromVec.length() * toVec.length())));	
	    origAngle = RadiansToDegrees(origAngle);

	    angle = origAngle;

    	    if(origAngle <= 10) {
		maxHeight = 6.5e+9;
	    }

	    else {
		maxHeight = 2.0e+10;
	    }

	    flagRot = true;
	    break;
	case AIRPLANE:
	    cout<<"USING AIRPLANE"<<endl;
	    break;
	default:
	    cout<<"PICK THE ALGORYTHM!!!!"<<endl;
	    break;
    }
}
Exemplo n.º 3
0
/**
 * Simulate a track-ball.  Project the points onto the virtual
 * trackball, then figure out the axis of rotation, which is the cross
 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
 * Note:  This is a deformed trackball-- is a trackball in the center,
 * but is deformed into a hyperbolic sheet of rotation away from the
 * center.  This particular function was chosen after trying out
 * several variations.
 *
 * It is assumed that the arguments to this routine are in the range
 * (-1.0 ... 1.0)
 */
void OrbitManipulator::trackball( osg::Vec3d& axis, float& angle, float p1x, float p1y, float p2x, float p2y )
{
    /*
        * First, figure out z-coordinates for projection of P1 and P2 to
        * deformed sphere
        */

    osg::Matrixd rotation_matrix(_rotation);

    osg::Vec3d uv = Vec3d(0.0f,1.0f,0.0f)*rotation_matrix;
    osg::Vec3d sv = Vec3d(1.0f,0.0f,0.0f)*rotation_matrix;
    osg::Vec3d lv = Vec3d(0.0f,0.0f,-1.0f)*rotation_matrix;

    osg::Vec3d p1 = sv * p1x + uv * p1y - lv * tb_project_to_sphere(_trackballSize, p1x, p1y);
    osg::Vec3d p2 = sv * p2x + uv * p2y - lv * tb_project_to_sphere(_trackballSize, p2x, p2y);

    /*
        *  Now, we want the cross product of P1 and P2
        */
    axis = p2^p1;
    axis.normalize();

    /*
        *  Figure out how much to rotate around that axis.
        */
    float t = (p2 - p1).length() / ( /*2.0*/ 1.3 * _trackballSize);

    /*
        * Avoid problems with out-of-control values...
        */
    if (t > 1.0) t = 1.0;
    if (t < -1.0) t = -1.0;
    angle = inRadians(asin(t));
}
Exemplo n.º 4
0
void
MorphologyViewerWidget::_get_transformation( unsigned int index
                                           , osg::Vec3d  & eye
                                           , osg::Vec3d  & center
                                           , double      & distance
                                           , osg::Vec3d  & up
                                           , osg::Vec3d  & look
                                           , osg::Vec3d  & side
                                           )
{
    osgViewer::View * view = _viewer -> getView(index);
    osgGA::TrackballManipulator * manipulator = dynamic_cast<osgGA::TrackballManipulator *>(view -> getCameraManipulator());
    manipulator -> getTransformation(eye, center, up);
    up.normalize();
    look = center - eye;
    distance = look.normalize();
    side = look ^ up;
    side.normalize();
}
Exemplo n.º 5
0
void ossimPlanetSceneView::getLookDirection(osg::Vec3d& direction)const
{
   osg::Vec3 eyeTemp(0,0,0);
   osg::Vec3 center(0,0,0);
   osg::Vec3 up(0,0,0);
   
   (const_cast<ossimPlanetSceneView*>(this))->getViewMatrixAsLookAt(eyeTemp, center, up);

   direction = (center-eyeTemp);

   direction.normalize();
}
Exemplo n.º 6
0
osg::Quat FaceTransform::computeQuat(osg::Vec3d direction)
{
    direction.normalize();

    double zAngle = atan2(direction.y(), direction.x());
    osg::Quat zRot(zAngle, osg::Vec3d(0,0,1));

    osg::Vec3d yAxis = zRot * osg::Vec3d(0,1,0);
    double zLength = (direction - osg::Vec3d(0,0,direction.z())).length();
    double yAngle = - atan2(direction.z(), zLength) + osg::PI/2;
    osg::Quat yRot(yAngle, yAxis);

    return zRot * yRot;
}
Exemplo n.º 7
0
// Computes all intersections in line segment (p1,p2) and returns the nearest impact point in 
// vResult and its associated surface normal.
bool ViroManipulator::Intersect(osg::Vec3d p1,osg::Vec3d p2, osg::Vec3d& vResult,osg::Vec3d& vNorm){
	osg::ref_ptr<osg::LineSegment> seg = new osg::LineSegment;
	seg->set(p1,p2);
	if ( !seg->valid() ) return false;

	osgUtil::IntersectVisitor iv;
	iv.setTraversalMode(osgUtil::IntersectVisitor::TRAVERSE_ACTIVE_CHILDREN); //Only visible children
	iv.setLODSelectionMode(osgUtil::IntersectVisitor::USE_SEGMENT_START_POINT_AS_EYE_POINT_FOR_LOD_LEVEL_SELECTION);
	iv.addLineSegment(seg.get());
	iv.setEyePoint( _vEye );

	_NODE->accept( iv );

	bool hitfound = false;
	if (iv.hits()){
		osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(seg.get());
		if (!hitList.empty()){
			for(osgUtil::IntersectVisitor::HitList::iterator hitr=hitList.begin(); hitr!=hitList.end(); ++hitr){
				if( hitr->_geode.valid() && (hitr->_geode.get()->getName()!="Contraint_OutBox_Geode") && (hitr->_geode.get()->getName()!="environment")){
						osg::Vec3d ip = hitr->getWorldIntersectPoint();
						osg::Vec3d in = hitr->getWorldIntersectNormal();

						if(!hitfound){
							vResult = ip;
							vNorm   = in;
							}
						else{
							osg::Vec3d vLastIP    = vResult;
							osg::Vec3d vCurrentIP = ip;
							double dPrev    = Vec3d(_vEye - vLastIP).length();
							double dCurrent = Vec3d(_vEye - vCurrentIP).length();

							if( dCurrent < dPrev ){
								vResult = ip;
								vNorm   = in;
								}
							}
						hitfound = true;
						}
					}
				}
			}
	if (hitfound){
		vNorm.normalize();
		vNorm = RightNormal(vResult,vNorm);
		}
	
	return hitfound;
}
Exemplo n.º 8
0
		void Manipulator::calcRotationArgs(osg::Vec3d& axis, float& angle, 
			float p1x, float p1y,float p2x, float p2y) {
			osg::Matrixd rotation_matrix(_rotation);

			osg::Vec3d uv = osg::Vec3d(0.0f,1.0f,0.0f)*rotation_matrix;
			osg::Vec3d sv = osg::Vec3d(1.0f,0.0f,0.0f)*rotation_matrix;
			osg::Vec3d lv = osg::Vec3d(0.0f,0.0f,-1.0f)*rotation_matrix;

			osg::Vec3d p1 = sv * p1x + uv * p1y - lv * tb_project_to_sphere(_trackball_size, p1x, p1y);
			osg::Vec3d p2 = sv * p2x + uv * p2y - lv * tb_project_to_sphere(_trackball_size, p2x, p2y);

			axis = p2^p1;
			axis.normalize();

			float t = (p2 - p1).length() / (2.0 * _trackball_size);

			if (t > 1.0) t = 1.0;
			if (t < -1.0) t = -1.0;
			angle = osg::inRadians(asin(t));
			//2014/4/28
			//2014/10/26,0.1
			angle = 0.02 * angle;
		}
Exemplo n.º 9
0
 CullPlaneCallback( const osg::Vec3d& planeNormal ) : _n(planeNormal) {
     _n.normalize();
 }
Exemplo n.º 10
0
bool CameraFlight::buttonEvent(int type/*, const osg::Matrix & mat*/)
{
//    osg::Matrix curMatrix = SceneManager::instance()->getObjectTransform()->getMatrix();
//    double curScale = SceneManager::instance()->getObjectScale();

 //   osg::Matrix w2o = SceneManager::instance()->getWorldToObjectTransform();
 //   osg::Matrix o2w = SceneManager::instance()->getObjectToWorldTransform();

    if(type == 'p') {
	std::cerr<<"curMatrix"<<endl;
	printMat(curMatrix, curScale);
	
	cout<<"x = "<<latLonHeight.x()<<", y = "<<latLonHeight.y()<<", z = "<<latLonHeight.z()<<endl;
//	std::cerr<<"WorldToObject"<<endl;
//	printMat(w2o, curScale);

//	std::cerr<<"ObjectToWorld"<<endl;
//	printMat(o2w, curScale);
    }

    else if(type == 'd') {
	curMatrix.decompose(trans1, rot1, scale1, so1);

	std::cerr<<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<<endl;
	cout<<"Trans = ";
	printVec(trans1);

	cout<<"Scale = ";
	printVec(scale1);

	cout<<"Rotate = ";
	printQuat(rot1);

	cout<<"Scale Orient =";
	printQuat(so1);

	std::cerr<<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<<endl;
	    //osg::Matrix _trans = osg::Matrix::rotate(oldPos,currentPos) * osg::Matrix::translate(trans);
	
	    
	    //osg::Matrix rotMat = osg::Matrix::rotate(oldPos,currentPos);
	    //osg::Matrix wr = o2w * rotMat* w2o; 
//	    osg::Matrix _tmp = w2o * osg::Matrix::rotate(oldPos,currentPos) * o2w;
	    //osg::Matrix _scale = osg::Matrix::scale(scale);
	    //osg::Matrix _temp = osg::Matrix::translate(-trans) * _scale * _trans;

    }

    else if(type == 't') {
	curMatrix.setTrans(osg::Vec3(0.0,1e+09/*6.41844e+09*/,0));
	SceneManager::instance()->setObjectMatrix(curMatrix);
    }

    else if(type == 's') {
	_origMatrix = SceneManager::instance()->getObjectTransform()->getMatrix();
	_origScale = SceneManager::instance()->getObjectScale();
    }

    else if(type == 'z') {
	tstart = time(0);
//	zIn = 1e+10; 
//	zOut = 1e+10;
	cout<<"zpressed"<<endl;
	if (flagZoom == false)
	    flagZoom = true;
    }

    else if(type == 'r') {

	if (flagRot == false) {
	    flagRot = true;
	}
	else {
	    flagRot = false;
	}

	//cout<<"Old Matrix"<<endl;
	//curMatrix = SceneManager::instance()->getObjectTransform()->getMatrix();
	//curScale = SceneManager::instance()->getObjectScale();

//	printMat(curMatrix, curScale);

	
	/*osg::Matrix mat2 = */

        osg::Matrix rotM;	
	rotM.makeRotate(DegreesToRadians(1.0),osg::Vec3(0,1,0)); 
//	printMat(rotM, curScale);
	
	curMatrix= o2w * rotM * w2o;
	
  //      printMat(curMatrix, curScale);

	//curMatrix.setTrans(trans);
	//cout<<"New Matrix"<<endl;
	//printMat(curMatrix, curScale);
	cout<<"x = "<<origPlanetPoint[0]<<", y = "<<origPlanetPoint[1]<<", z = "<<origPlanetPoint[2]<<endl;

	osg::Matrix objMat = SceneManager::instance()->getObjectTransform()->getMatrix();

	objMat.decompose(trans1,rot1,scale1,so1);
	_destMat1.decompose(trans2,rot2,scale2,so2);
//	cout<<"rotate from"<<endl;
//	printQuat(rot1);
//	cout<<"rotate to"<<endl;
//	printQuat(rot2);
	osg::Vec3 vect1, vect2;
	double ang1, ang2;
	rot1.getRotate(ang1, vect1);
	rot2.getRotate(ang2, vect2);
	osg::Vec3 vec1 = rot1.asVec3();
	osg::Vec3 vec2 = rot2.asVec3();
	
	printVec(vect1);
	printVec(vect2);
	osg::Quat rotQuat;
	rotQuat.makeRotate(vect1, vect2);
//	printQuat(rotQuat);

	rotM.makeRotate(rotQuat); 
	osg::Matrix mat = objMat* rotM;
	mat.setTrans(trans1);
	SceneManager::instance()->setObjectMatrix(mat);
    }

    else if(type == 'q') {
	cout<<"Distance = "<<distanceToSurface<<endl;
    }

    else if(type == 'g') {
	flagOut = false;
	flagIn = false;
	flagRot = false;
	flagZoom = true;
	osg::Matrix objMat = SceneManager::instance()->getObjectTransform()->getMatrix();
	
/*      osg::Matrix rotM8;
	rotM8.makeRotate(DegreesToRadians(10.0),osg::Vec3(0,1,0));
	osg::Matrix mat9 = rotM8 * objMat;

	SceneManager::instance()->setObjectMatrix(mat9);
*/

    	objMat.decompose(trans2, rot2, scale2, so2);
	double LFD, PD, FS, PS;

	a = (maxHeight - trans2[1])/25.0;
	t = 0.0;
	total = 0.0;
    }

    else if(type == 'a') {
	SceneManager::instance()->setObjectMatrix(_origMatrix);
    }

    else if(type == 'm') {

 	if(flagRot) {
	    flagRot = false;
	}

	else {
	tstart = time(0);

	zIn = 1e+10; 
	zOut = 1e+10;

	osg::Matrix objMat = SceneManager::instance()->getObjectTransform()->getMatrix();

	
	osg::Vec3d tolatLon(0.573827, -2.04617,0);
	osg::Vec3d tolatLon1(0.622566, 2.43884, 0);
	osg::Vec3d toVec1, toVec2;

	map->getProfile()->getSRS()->getEllipsoid()->convertLatLongHeightToXYZ(
					tolatLon.x(),tolatLon.y(),tolatLon.z(),
					toVec1.x(),toVec1.y(),toVec1.z());

//	map->getProfile()->getSRS()->getEllipsoid()->convertLatLongHeightToXYZ(
//					tolatLon1.x(),tolatLon1.y(),tolatLon1.z(),
//					toVec2.x(),toVec2.y(),toVec2.z());

	fromVec = origPlanetPoint;

	toVec1.normalize();
//	toVec2.normalize();
	fromVec.normalize();
/*
	osg::Vec3 offset(0.0,1.0,0.0);

	offset = offset - fromVec;
	fromVec = fromVec + offset;
	toVec1 = toVec1 + offset;
	toVec2 = toVec2 + offset;


	printVec(fromVec);
        printVec(toVec1);
        printVec(toVec2);
*/
	cout<<endl;
	toVec = toVec1;

	double dot = fromVec * toVec;
	angle = acos(dot/((fromVec.length() * toVec.length())));	

	angle = RadiansToDegrees(angle);

	rotAngle = angle/100.0;
	cout<<angle<<endl; 	
	flagRot = true;
}
//	osg::Vec3 crsVec = toVec1^toVec2;
//	crsVec.normalize();

//	cout<<"Where you at"<<endl;
//	printVec(fromVec);

//	cout<<"UCSD"<<endl;
//	printVec(toVec1);

//	cout<<"Tokyo"<<endl;
//	printVec(toVec2);

	//osg::Vec3 rotVec = (fromVec ^ toVec);
	//rotVec.normalize();
	//origPlanetPoint.normalize();
	//latLonHeight.normalize();

//	printVec(crsVec);	
 //       osg::Matrix rotM;
//	rotM.makeRotate(DegreesToRadians(1.0),crsVec);



	//printVec(origPlanetPoint);
//	printVec(origPlanetPoint);
//	printVec(latLonHeight);
//	printMat(rotM, curScale);
	//objMat.decompose(trans1,rot1,scale1,so1);
	//osg::Vec3 vect1, vect2;
	//double ang1, ang2;
	//rot1.getRotate(ang1, vect1);
	//printVec(vect1);
	
//	osg::Matrix mat = objMat* rotM;
//	mat.setTrans(trans1);
//	SceneManager::instance()->setObjectMatrix(mat);
    }

    else if(type == 'x') {
	cout<<"DRAWlING"<<endl;
	osgEarth::MapNode* outMapNode = MapNode::findMapNode(SceneManager::instance()->getObjectsRoot());

	outputMap = outMapNode->getMap();

	double lat = 0.0;
	double lon = -1.5708;
	double hght = 0.0;

	osg::Matrix objMat = SceneManager::instance()->getObjectTransform()->getMatrix();

	
	osg::Vec3d tolatLon(0.573827, -2.04617,0);
	osg::Vec3d tolatLon1(0.622566, 2.43884, 0);
	osg::Vec3d toVec1, toVec2;

	map->getProfile()->getSRS()->getEllipsoid()->convertLatLongHeightToXYZ(
					lat,lon,hght,
					toVec1.x(),toVec1.y(),toVec1.z());

	osg::Matrixd output, output2;
	map->getProfile()->getSRS()->getEllipsoid()->computeLocalToWorldTransformFromXYZ(
		toVec1.x(), toVec1.y(), toVec1.z(), output);
	
	map->getProfile()->getSRS()->getEllipsoid()->computeLocalToWorldTransformFromXYZ(
		lat, lon, hght, output2);
	printMat(output,10.0);
	printMat(output2,10.0);

	osg::Geode * geode = new osg::Geode();
	osg::Vec3 centerVec(0.0,0.0,-20000.0);
	osg::ShapeDrawable* shape = new osg::ShapeDrawable(
		new osg::Cylinder(centerVec,10000.0, 2000000.0));
	geode->addDrawable(shape);
	
	osg::MatrixTransform * mat1 = new osg::MatrixTransform();

	mat1->setMatrix(output);
	mat1->addChild(geode);
	
	SceneManager::instance()->getObjectsRoot()->addChild(mat1);

	map->getProfile()->getSRS()->getEllipsoid()->convertLatLongHeightToXYZ(
					tolatLon.x(),tolatLon.y(),tolatLon.z(),
					toVec2.x(),toVec2.y(),toVec2.z());

	osg::Matrixd output1;
//	lat = 0.573827;
//	lon = -2.04617;
	map->getProfile()->getSRS()->getEllipsoid()->computeLocalToWorldTransformFromXYZ(
		toVec2.x(), toVec2.y(), toVec2.z(), output1);

	osg::Geode * geode1 = new osg::Geode();
	osg::Vec3 centerVec1(0.0,0.0,-20000.0);
	osg::ShapeDrawable* shape1 = new osg::ShapeDrawable(
		new osg::Cylinder(centerVec1,10000.0, 2000000.0));
	geode1->addDrawable(shape1);
	
	osg::MatrixTransform * mat2 = new osg::MatrixTransform();

	mat2->setMatrix(output1);
	mat2->addChild(geode1);
	
	SceneManager::instance()->getObjectsRoot()->addChild(mat2);
	//geode->addDrawable(shape1);
    }

    return false;

}