Color SphericalEnvironment::getEnvironmentColor(const Vector& direction) const
{
	Vector normalizedDirection = direction;
	normalizedDirection.normalize();

	double longitude = atan(normalizedDirection.X() / (normalizedDirection.Z()));
	double latitude = acos(normalizedDirection.Y());

	if (normalizedDirection.Z() < 0.0)
	{
		longitude += PI;
	}

	if (normalizedDirection.X() < 0.0 && normalizedDirection.Z() > 0)
	{
		longitude += 2 * PI;
	}

	double x = (longitude / (2 * PI)) * this->sphericalEnvironmentBitmapTexture->BitmapTextureImage()->width();
	double y = (latitude / PI) * this->sphericalEnvironmentBitmapTexture->BitmapTextureImage()->height();

	x = (int)(-100 + x + this->sphericalEnvironmentBitmapTexture->BitmapTextureImage()->width() / 2.0) % this->sphericalEnvironmentBitmapTexture->BitmapTextureImage()->width();

	Color environmentColor = this->sphericalEnvironmentBitmapTexture->getPixelColor(x, y);

	return environmentColor;
}
bool Streamline::MidPointMethod(Vector CurrPos, Vector& NextPos, float fStepSize){

	Vector vDerivative(0.0f, 0.0f, 0.0f) ;

	if( !this->DerivativeAtPosition(CurrPos, vDerivative) )			// if the position is out of bound
		return false ;

	// now get the midpoint
	// k1 = h*Derivative
	// we are doing fStepSize/2 to get k1/2
	// Midpoint will have X0 + k1/2 which is mid point between the current position 
	// and next position according to euler step
	vDerivative.ScalarMult(fStepSize/2 ); 
	Vector MidPoint(0.0, 0.0, 0.0) ;
	MidPoint.VectorX = CurrPos.X() + vDerivative.X() ;
	MidPoint.VectorY = CurrPos.Y() + vDerivative.Y() ;
	MidPoint.VectorZ = CurrPos.Z() + vDerivative.Z() ;

	// Now get the derivative at midPoint
	if( !this->DerivativeAtPosition(MidPoint, vDerivative) )			// if the position is out of bound
		return false ;
	// Now find the next position according to this derivative
	vDerivative.ScalarMult(fStepSize);									// now take the full step
	NextPos.VectorX = CurrPos.X() + vDerivative.X() ;
	NextPos.VectorY = CurrPos.Y() + vDerivative.Y() ;
	NextPos.VectorZ = CurrPos.Z() + vDerivative.Z() ;

	return true;
}
Example #3
0
vmg_float Helper::InterpolateTrilinear(const Vector& point, const Grid& grid)
{
  vmg_float interpolate_vals[4], grid_vals[8];

  const Index index_global = (point - grid.Extent().Begin()) / grid.Extent().MeshWidth();
  const Index index_local = index_global - grid.Global().LocalBegin() + grid.Local().Begin();
  const Vector coord = (point - grid.Extent().Begin() - index_global * grid.Extent().MeshWidth()) / grid.Extent().MeshWidth();

  grid_vals[0] = grid.GetVal(index_local.X()  , index_local.Y()  , index_local.Z()  );
  grid_vals[1] = grid.GetVal(index_local.X()+1, index_local.Y()  , index_local.Z()  );
  grid_vals[2] = grid.GetVal(index_local.X()  , index_local.Y()+1, index_local.Z()  );
  grid_vals[3] = grid.GetVal(index_local.X()+1, index_local.Y()+1, index_local.Z()  );
  grid_vals[4] = grid.GetVal(index_local.X()  , index_local.Y()  , index_local.Z()+1);
  grid_vals[5] = grid.GetVal(index_local.X()+1, index_local.Y()  , index_local.Z()+1);
  grid_vals[6] = grid.GetVal(index_local.X()  , index_local.Y()+1, index_local.Z()+1);
  grid_vals[7] = grid.GetVal(index_local.X()+1, index_local.Y()+1, index_local.Z()+1);

  for (int i=0; i<4; ++i)
    interpolate_vals[i] = (1.0 - coord.X()) * grid_vals[2*i] + coord.X() * grid_vals[2*i+1];

  for (int i=0; i<2; ++i)
    interpolate_vals[i] = (1.0 - coord.Y()) * interpolate_vals[2*i] + coord.Y() * interpolate_vals[2*i+1];

  return (1.0 - coord.Z()) * interpolate_vals[0] + coord.Z() * interpolate_vals[1];
}
Example #4
0
	/**
	 * Determines whether contains the specified point.
	 */
	ContainmentType Contains( const Vector<Real,3>& point )
	{
		if( Min.X() <= point.X() && point.X() <= Max.X() && Min.Y() <= point.Y() && 
			point.Y() <= Max.Y() && Min.Z() <= point.Z() && point.Z() <= Max.Z() )
			return CT_Contains;

		return CT_Disjoint;
	}
// returns false if the position is out of bound
bool Streamline::DerivativeAtPosition(Vector CurrPos, Vector& vDerivative){
	int iXMin = floor(CurrPos.X()) ;
	int iXMax = ceil (CurrPos.X()) ;		
	float fDelta1 = CurrPos.X() - (float)iXMin ;

	int iYMin = floor(CurrPos.Y()) ;
	int iYMax = ceil(CurrPos.Y()) ;
	float fDelta2 = CurrPos.Y() - (float)iYMin;

	int iZMin = floor(CurrPos.Z()) ;
	int iZMax = ceil(CurrPos.Z()) ;
	float fDelta3 = CurrPos.Z() - (float)iZMin ;

	if( iXMin < 0 || iXMin >= this->cols  )			// condition check. if your current position is outside
		return false;									// the volume, then don't continue with the line
	if( iXMax < 0 || iXMax >= this->cols )				
		return false;		
	if( iYMin < 0 || iYMax >= this->rows  )			// I am not sure we need all the conditions
		return false;									// may be we can do Min with 0 and Max with ROWS/COLS/SLICES
	if( iYMax < 0 || iYMax >= this->rows )				// but right now I am not taking any chance
		return false;	
	if( iZMin < 0 || iZMin >= this->slices )
		return false;	
	if( iZMax < 0 || iZMax >= this->slices )
		return false;	

	// get four corners of the first XY plane
	//int index = iZMin*ROWS*COLS + iYMin*rows +
	Vector v1 =  vectorField[iZMin][iYMin][iXMin] ;
	Vector v2 =  vectorField[iZMin][iYMin][iXMax] ;
	Vector v3 =  vectorField[iZMin][iYMax][iXMin] ;
	Vector v4 =  vectorField[iZMin][iYMax][iXMax] ;
	biLinearInterpolate(v1, v2, v3, v4, fDelta1, fDelta2) ;		// doing bi linear interpolation in this plane	
																				// result is in v1
	// get four corners of the first XY plane
	//int index = iZMin*ROWS*COLS + iYMin*rows +
	Vector v5 =  vectorField[iZMax][iYMin][iXMin] ;
	Vector v6 =  vectorField[iZMax][iYMin][iXMax] ;
	Vector v7 =  vectorField[iZMax][iYMax][iXMin] ;
	Vector v8 =  vectorField[iZMax][iYMax][iXMax] ;
	biLinearInterpolate(v5, v6, v7, v8, fDelta1, fDelta2) ;		// doing bi linear interpolation in this plane	
																				// result is in v5	
					
	// now interpolate v1 and v5
	v1.Interpolate(v5, fDelta3) ;
	v1.Normalize() ;
	vDerivative.VectorX = v1.X() ;
	vDerivative.VectorY = v1.Y() ;
	vDerivative.VectorZ = v1.Z() ;

	return true ;			// return success
} 
Example #6
0
Vector Vector::Cross(Vector &v)
{
   float x, y, z;
   Vector cp;

   x = Y() * v.Z() - Z() * v.Y();
   y = Z() * v.X() - X() * v.Z();
   z = X() * v.Y() - Y() * v.X();

   cp.Set(x, y, z);

   return cp;
}
  void Radial_Inline_Mesh_Desc::Populate_Coords(Real * coords,
      std::vector<long long> & global_node_vector,
      std::map <long long, long long> & global_node_map,
      long long num_nodes)

    /****************************************************************************/
  {
    Real deg_to_rad = M_PI/180.0;
    Real total_theta = c_block_dist[1][inline_b[1]];
    long long global_ind[3];
    for(unsigned gnv = 0;gnv < global_node_vector.size();gnv ++){
      long long the_node = global_node_vector[gnv];
      global_ind[2] = the_node/knstride;
      global_ind[1] = (the_node-global_ind[2]*knstride)/jnstride;
      global_ind[0] = the_node - global_ind[2]*knstride-global_ind[1]*jnstride;

      long long the_local_node = get_map_entry(global_node_map,the_node);
      coords[the_local_node+0*num_nodes]= IJKcoors[0][global_ind[0]]*cos(IJKcoors[1][global_ind[1]]*deg_to_rad);
      coords[the_local_node+1*num_nodes]= IJKcoors[0][global_ind[0]]*sin(IJKcoors[1][global_ind[1]]*deg_to_rad);
      if(dimension == 3){
        coords[the_local_node+2*num_nodes]= IJKcoors[2][global_ind[2]];
      }

      if(enforce_periodic){
        Vector tv = calc_coords_periodic(total_theta, global_ind[0], global_ind[1], global_ind[2]);
        coords[the_local_node+0*num_nodes]= tv.X();
        coords[the_local_node+1*num_nodes]= tv.Y();
        if(dimension == 3){
          coords[the_local_node+2*num_nodes]= tv.Z();
        }
      }
    }
  }
Matrix& Matrix::SetRotate(const Vector& vec, float angle)
{
    if (vec.Y() == 0 && vec.Z() == 0) {
        return SetRotateX(angle * sign(vec.X()));
    } else if (vec.X() == 0 && vec.Z() == 0) {
        return SetRotateY(angle * sign(vec.Y()));
    } else if (vec.X() == 0 && vec.Y() == 0) {
        return SetRotateZ(angle * sign(vec.Z()));
    } else {
        Vector vecUnit = vec.Normalize();

        float x = vecUnit.X();
        float y = vecUnit.Y();
        float z = vecUnit.Z();

        float c = cos(angle);
        float s = sin(angle);

        float x2 = x * x;
        float y2 = y * y;
        float z2 = z * z;
        float xy = x * y * (1 - c);
        float yz = y * z * (1 - c);
        float zx = z * x * (1 - c);

        m_m[0][0] = x2 + c * (1 - x2); m_m[0][1] =        xy - z * s; m_m[0][2] =        zx + y * s; m_m[0][3] = 0;
        m_m[1][0] =        xy + z * s; m_m[1][1] = y2 + c * (1 - y2); m_m[1][2] =        yz - x * s; m_m[1][3] = 0;
        m_m[2][0] =        zx - y * s; m_m[2][1] =        yz + x * s; m_m[2][2] = z2 + c * (1 - z2); m_m[2][3] = 0;
        m_m[3][0] =                 0; m_m[3][1] =                 0; m_m[3][2] =                 0; m_m[3][3] = 1;
    }

    m_type = TransformRotate;

    return *this;
}
Example #9
0
bool Plane::intersect(const Ray& ray, IntersectionData& intersectionData)
{
	if ((ray.Start().Y() > this->Y() && ray.Direction().Y() > VerySmall) ||
		(ray.Start().Y() < this->Y() && ray.Direction().Y() < VerySmall))
	{
		return false;
	}

	double yRayDirection = ray.Direction().Y();
	double yDifference = ray.Start().Y() - this->Y();
	double multiplier = yDifference / -yRayDirection;

	if (multiplier > intersectionData.getDistance())
	{
		return false;
	}

	Vector intersectionPoint = ray.Start() + ray.Direction() * multiplier;

	if (fabs(intersectionPoint.X()) > this->Limit() || fabs(intersectionPoint.Z()) > this->Limit())
	{
		return false;
	}

	intersectionData.setIntersectionPoint(intersectionPoint);
	intersectionData.setDistance(multiplier);
	intersectionData.setNormal(Vector(0.0, 1.0, 0.0));
	intersectionData.setTextureU(intersectionData.IntersectionPoint().X());
	intersectionData.setTextureV(intersectionData.IntersectionPoint().Z());

	return true;
}
Example #10
0
ZString GetString(int indent, const Vector& vec)
{
    return
          "Vector("
        + ZString(vec.X()) + ", "
        + ZString(vec.Y()) + ", "
        + ZString(vec.Z()) + ")";
}
Example #11
0
	/**
	 * Merge a point
	 */
	void Merge( const Vector<Real,3>& point )
	{
		if (!Defined)
		{
			Max = Min = point;
			Defined = true;
			return;
		}

		if (point.X() < Min.X())	Min.X() = point.X();
		if (point.Y() < Min.Y())	Min.Y() = point.Y();
		if (point.Z() < Min.Z())	Min.X() = point.Z();

		if (point.X() > Max.X())	Max.X() = point.X();
		if (point.Y() > Max.Y())	Max.Y() = point.Y();
		if (point.Z() > Max.Z())	Max.X() = point.Z();
	}
Vector Matrix::Transform(const Vector& vec) const
{
    //#ifndef FLOATASM
        float x = vec.X();
        float y = vec.Y();
        float z = vec.Z();

        return
            Vector(
                m_m[0][0] * x + m_m[0][1] * y + m_m[0][2] * z + m_m[0][3],
                m_m[1][0] * x + m_m[1][1] * y + m_m[1][2] * z + m_m[1][3],
                m_m[2][0] * x + m_m[2][1] * y + m_m[2][2] * z + m_m[2][3]
            );
    /*
    #else
        float* pm = m_m;
        Vector result;

        __asm {
            mov     esi, vec
            lea     edi, result
            mov     eax, pm

            fld     [esi].x         \\ x
            fld     [esi].y         \\ y x
            fld     [esi].z         \\ z y x
                                
            fld     [eax]           \\ m00 z y x
            fmul    st(3)           \\ m00*x z y x
            fld     [eax + 4]       \\ m01   m00*x z y x
            fmul    st(3)           \\ m01*y m00*x z y x
            fld     [eax + 8]       \\ m02   m01*y m00*x z y x
            fmul    st(3)           \\ m02*z mo1*y m00*x z y x
            fld     [eax + 12]      \\ m03   m02*z mo1*y m00*x z y x
            faddp   st(3), st(0)    \\ m02*z mo1*y m00*x+m03 z y x
            faddp   st(1), st(0)    \\ m02*z+mo1*y m00*x+m03 z y x


            fld     [eax + 16 + 0]  \\ m10 m02*z+mo1*y m00*x+m03 z y x
            fmul    st(5)           \\ m10*x m02*z+mo1*y m00*x+m03 z y x
            fxch    st(1)           \\ m02*z+mo1*y m10*x m00*x+m03 z y x
            faddp   st(2), st(0)    \\ m10*x m02*z+mo1*y+m00*x+m03 z y x
            fld     [eax + 16 + 4]  \\ m11 m10*x m02*z+mo1*y+m00*x+m03 z y x
            fmul    st(4)           \\ m11*y m10*x m02*z+mo1*y+m00*x+m03 z y x
            fld     [eax + 16]      \\ m12 m11*4 m10*x m02*z+mo1*y+m00*x+m03 z y x
            fmul    st(4)           \\ m12*z m11*4 m10*x m02*z+mo1*y+m00*x+m03 z y x
            fxch    st(3)           \\ m02*z+mo1*y+m00*x+m03 m11*4 m10*x m12*z z y x
            fstp    [edi].x         \\ m11*4 m10*x m12*z z y x
            fadd

        }
    #endif
    */
}
Example #13
0
Vector Vector::ProjectVector(Vector &a)
{
   Vector res, b;

   b.Set(X(), Y(), Z());
   res.Set(a.X(), a.Y(), a.Z());

   res.Scale(a.Dot(b) / a.Dot(a));

   return res;
}
Vector Matrix::TransformDirection(const Vector& vec) const
{
     float x = vec.X();
     float y = vec.Y();
     float z = vec.Z();

     return
         Vector(
             m_m[0][0] * x + m_m[0][1] * y + m_m[0][2] * z,
             m_m[1][0] * x + m_m[1][1] * y + m_m[1][2] * z,
             m_m[2][0] * x + m_m[2][1] * y + m_m[2][2] * z
         );
}
Example #15
0
Vector Vector::operator+ (Vector &v)
{
   float x, y, z;
   Vector vec;

   x = X() + v.X();
   y = Y() + v.Y();
   z = Z() + v.Z();

   vec.Set(x, y, z);

   return vec;
}
// This multiplies onto the current matrix stack a look-at matrix (using
//   gluLookAt()) for light #i.
void Scene::LightLookAtMatrix( int i )
{
	Point lightPos( light[i]->GetCurrentPos() );
	Point at( camera->GetAt() );
	Vector view = at-lightPos;
	view.Normalize();
	Vector perp = abs( view.Dot( Vector::YAxis() ) ) < 0.95 ? view.Cross( Vector::YAxis() ) : view.Cross( Vector::XAxis() );
	perp.Normalize();
	Vector up = perp.Cross( view );
	gluLookAt( lightPos.X(), lightPos.Y(), lightPos.Z(), 
		       at.X(), at.Y(), at.Z(), 
			   up.X(), up.Y(), up.Z() );
}
Matrix& Matrix::SetLookAtFrom(const Vector& vecAt, const Vector& vecFrom, const Vector& vecUp)
{
    Vector vecZAxis = (vecFrom - vecAt).Normalize();

    if (vecZAxis == vecUp) {
        SetIdentity();
        m_m[0][3] = vecFrom.X();
        m_m[1][3] = vecFrom.Y();
        m_m[2][3] = vecFrom.Z();
    } else {
        Vector vecXAxis = CrossProduct(vecUp, vecZAxis).Normalize();
        Vector vecYAxis = CrossProduct(vecZAxis, vecXAxis);

        m_m[0][0] = vecXAxis.X();
        m_m[0][1] = vecYAxis.X();
        m_m[0][2] = vecZAxis.X();
        m_m[0][3] =  vecFrom.X();

        m_m[1][0] = vecXAxis.Y();
        m_m[1][1] = vecYAxis.Y();
        m_m[1][2] = vecZAxis.Y();
        m_m[1][3] =  vecFrom.Y();

        m_m[2][0] = vecXAxis.Z();
        m_m[2][1] = vecYAxis.Z();
        m_m[2][2] = vecZAxis.Z();
        m_m[2][3] =  vecFrom.Z();

        m_m[3][0] = 0;
        m_m[3][1] = 0;
        m_m[3][2] = 0;
        m_m[3][3] = 1;

        m_type = TransformRotate | TransformTranslate;
    }

    return *this;
}
Example #18
0
void Camera::LookAtMatrix( void )
{
	if (ball)
	{
		Vector rotVec = ball->ApplyTrackballMatrix( at-eye );
		gluLookAt( at.X()-rotVec.X(), at.Y()-rotVec.Y(), at.Z()-rotVec.Z(), 
				   at.X(), at.Y(), at.Z(), 
				   up.X(), up.Y(), up.Z() );
	}
	else
		gluLookAt( eye.X(), eye.Y(), eye.Z(), 
				   at.X(), at.Y(), at.Z(), 
				   up.X(), up.Y(), up.Z() );
}
// returns false if the position is out of bound
// true if the position is good. Then new postion is set in NextPos vector
bool Streamline::EulerStep(Vector CurrPos, Vector& NextPos, float fStepSize){

	Vector vDerivative(0.0f, 0.0f, 0.0f) ;

	if( !this->DerivativeAtPosition(CurrPos, vDerivative) )			// if the position is out of bound
		return false ;

	vDerivative.ScalarMult(fStepSize) ; 
	NextPos.VectorX = CurrPos.X() + vDerivative.X() ;
	NextPos.VectorY = CurrPos.Y() + vDerivative.Y() ;
	NextPos.VectorZ = CurrPos.Z() + vDerivative.Z() ;

	return true;
}
void VectorFieldRenderer::RenderStreamlines(StreamlineRenderer *streamline){

	GLfloat posX, posY, posZ;
	Vector dirV;
	Vector eigV;
	Vector strmpt;
//	GLuint vbo = 0;

//	GLuint shader = compileShader(vertexShader, spherePixelShader);

//	std::ofstream file;
//	file.open("values.txt", std::ios::out);

	int x, y, z;

	// position array
	GLfloat *verts = (GLfloat*)malloc(streamline->getSize() * 4.0 * sizeof(float));

	for(int i = 0; i < streamline->getSize(); i++){

		strmpt = streamline->getStreamlinePoint(i); // get the streamline point
		
		/* get the x, y, and z positions of the streamline point */
		x = verts[i*4+0] = (int)ceil(strmpt.X());
		y = verts[i*4+1] = (int)ceil(strmpt.Y());
		z = verts[i*4+2] = (int)ceil(strmpt.Z());
		verts[i*4+3] = 1.0;

		dirV = this->VectorField[z][y][x]  ;
		eigV = this->EigenValues[z][y][x] ;

//		file << eigV.X() << "," <<  eigV.Y() << "," << eigV.Z() << "\n"; 

		dirV.Normalize() ; 
		posX = x*boxLenX + boxLenX/2 ;
		posY = y*boxLenY + boxLenY/2 ;
		posZ = z*boxLenZ + boxLenZ/2 ;

		glPushMatrix() ;
	//	glScalef(2.0, 2.0, 2.0);
		glTranslatef(posX,posY,posZ) ;
		this->DrawSuperQuadric(eigV);				
		glPopMatrix() ; 
	} // end for i

//	create_VBO(vbo, streamline->getSize(), verts);
//	draw_VBO(vbo, shader, streamline->getSize());

//	file.close();
}
Example #21
0
void Cylinder::Preprocess( Scene *s )
{
	Vector rotateCylinderBy = Vector::ZAxis().Cross( axis );
	float rotateLength = rotateCylinderBy.Normalize();
	float dotPrd = Vector::ZAxis().Dot( axis );
	float angle = 180.0f * atan2( rotateLength, dotPrd ) / M_PI;

	displayList = glGenLists(1);
	glNewList( displayList, GL_COMPILE );
	glPushMatrix();
	glTranslatef( center.X(), center.Y(), center.Z() );
	glRotatef( angle, rotateCylinderBy.X(), rotateCylinderBy.Y(), rotateCylinderBy.Z() );
	glTranslatef( 0, 0, -0.5*height );
	gluCylinder( s->GetQuadric(), radius, radius, height, slices, stacks );
	glPopMatrix();
	glEndList();
}
Example #22
0
void 
CTransform::ComputeCompassAndClino( const Vector & g0, const Vector & m0, 
                    double & compass, double & clino, double & roll ) const
{
  Vector g;
  Vector m;

  g.X() = G[0][0] + G[0][1] * g0.X() + G[0][2] * g0.Y() + G[0][3] * g0.Z();
  g.Y() = G[1][0] + G[1][1] * g0.X() + G[1][2] * g0.Y() + G[1][3] * g0.Z();
  g.Z() = G[2][0] + G[2][1] * g0.X() + G[2][2] * g0.Y() + G[2][3] * g0.Z();

  m.X() = M[0][0] + M[0][1] * m0.X() + M[0][2] * m0.Y() + M[0][3] * m0.Z();
  m.Y() = M[1][0] + M[1][1] * m0.X() + M[1][2] * m0.Y() + M[1][3] * m0.Z();
  m.Z() = M[2][0] + M[2][1] * m0.X() + M[2][2] * m0.Y() + M[2][3] * m0.Z();
  g.normalize();
  m.normalize();
  Vector e( 1.0, 0.0, 0.0 );  // Disto axis
  Vector y = m % g;           // neg. Earth Y axis
  Vector x = g % (m % g);     // Earth X axis
                              // Earth Z axis pointing down as "g"
#if 0
  Vector e1 = g % (e % g);
  clino = acos( g.X() / g.length() ) - M_PI/2;
  // double sc = sin( clino );
  // double cc = cos( clino );
  Vector em1 = e1 % x;
  double sb = em1.length() * ( ( em1*g > 0 ) ? -1.0 : 1.0 );
  double cb = e1 * x;
  compass = atan2( sb, cb );
#else
  y.normalize();
  x.normalize();
  double ex = e*x;
  double ey = e*y;
  double ez = e*g;

  compass = atan2( -ey, ex );
  clino   = atan2( ez, sqrt(ex*ex+ey*ey) );
#endif
  roll    = atan2( g.Y(), g.Z() );
  if ( compass < 0.0 ) compass += 2*M_PI;
  if ( roll < 0.0 ) roll += 2*M_PI;
  clino   *= RAD2GRAD_FACTOR;
  compass *= RAD2GRAD_FACTOR;
  roll    *= RAD2GRAD_FACTOR;
}
Example #23
0
bool Vector::operator== (Vector &v)
{
   return X() == v.X() && Y() == v.Y() && Z() == v.Z();
}
bool Streamline::RK4Method(Vector CurrPos, Vector& NextPos, float fStepSize){

	Vector vK1, vK2, vK3, vK4;
	
	// finding K1
	if( !this->DerivativeAtPosition(CurrPos, vK1) )					// if the position is out of bound
		return false ;	
	vK1.ScalarMult(fStepSize) ;										// k1 = h*Derivative
	// now prepare for K2
	Vector vTemp1 = vK1 ;											// (k1)/2
	vTemp1.ScalarMult(0.5) ; 	
	Vector HalfK1Point;

	HalfK1Point.VectorX = CurrPos.X() + vTemp1.X() ;		// HalfK1Point = X0 + K1/2
	HalfK1Point.VectorY = CurrPos.Y() + vTemp1.Y() ;
	HalfK1Point.VectorZ = CurrPos.Z() + vTemp1.Z() ;
	// Now get the derivative at HalfK1Point which will be K2
	if( !this->DerivativeAtPosition(HalfK1Point, vK2) )				// calculating derivative at HalfK1 i.e f(X0+K1/2)
		return false ;
	
	vK2.ScalarMult(fStepSize) ;										// k2 = h*Derivative

	// Now prepare for K3
	Vector vTemp2 = vK2 ;											// vTemp = (k2)/2
	vTemp2.ScalarMult(0.5) ; 	
	Vector HalfK2Point;											
	HalfK2Point.VectorX = CurrPos.X() + vTemp2.X() ;		// HalfK2Point = X0 + K2/2
	HalfK2Point.VectorY = CurrPos.Y() + vTemp2.Y() ;
	HalfK2Point.VectorZ = CurrPos.Z() + vTemp2.Z() ;
	// Now get the derivative at HalfK2Point
	if( !this->DerivativeAtPosition(HalfK2Point, vK3) )				// calculating derivative i.e. f(X0+K2/2)
		return false ;

	vK3.ScalarMult(fStepSize) ;										// k3 = h*Derivative

	// Now prepare for K4
	Vector vTemp3 = vK3 ;											// vTemp = (K3), Note, there is no dividing by 2 here
	Vector K3Point;											
	K3Point.VectorX = CurrPos.X() + vTemp3.X() ;			// K3Point = X0 + K3
	K3Point.VectorY = CurrPos.Y() + vTemp3.Y() ;
	K3Point.VectorZ = CurrPos.Z() + vTemp3.Z() ;
	// Now get the derivative at K3Point
	if( !this->DerivativeAtPosition(K3Point, vK4) )					// calculating derivative i.e. f(X0+K3) 
		return false ;
	vK4.ScalarMult(fStepSize) ;										// k4 = h*Derivative

	// Now find the next position according to all these derivates
	vK1.ScalarMult((1.0/6.0)) ;
	vK2.ScalarMult((1.0/3.0));
	vK3.ScalarMult((1.0/3.0)) ;
	vK4.ScalarMult((1.0/6.0)) ;

	NextPos.VectorX = CurrPos.X() ;
	NextPos.VectorY = CurrPos.Y() ;
	NextPos.VectorZ = CurrPos.Z() ;

	NextPos.Add(vK1) ;						// NextPos = CurrPos + (1/6)K1
	NextPos.Add(vK2);						// NextPos = CurrPos + (1/6)K1 + (1/3)K2
	NextPos.Add(vK3);						// NextPos = CurrPos + (1/6)K1 + (1/3)K2 + (1/3)K3 
	NextPos.Add(vK4);						// NextPos = CurrPos + (1/6)K1 + (1/3)K2 + (1/3)K3 + (1/6)K4

	return true;
}
    void Render(Context* pcontext)
    {
        int count = m_data.GetCount();

        if (count > 0) {
                  Camera* pcamera = GetCamera();
                  float   focus   = pcamera->GetFocus();
            const Rect&   rect    = GetViewRect()->GetValue();

            float scale = focus * rect.XSize() / 2;
            float xc    = rect.Center().X();
            float yc    = rect.Center().Y();

            const Orientation& orient = GetCamera()->GetOrientation();

            VertexScreen* pvertex = pcontext->GetVertexScreenBuffer(count * 2);
            WORD*         pindex  = pcontext->GetIndexBuffer(count * 2);

            int index      = 0;
            int indexPoint = count * 2;
            for(int indexSource = 0; indexSource < count; indexSource++) {
                StarData& data      = m_data.Get(indexSource);
                Color&    color     = data.m_color;
                Vector&   direction = data.m_direction;
                Point&    pointOld  = data.m_pointOld;

                //
                // orient the start based on the current camera
                //

                Vector vec = orient.TimesInverse(direction);

                //
                // fold up the stars for higher density
                //

                if (vec.z > 0) {
                    vec.SetX(-vec.X());
                    vec.SetY(-vec.Y());
                    vec.SetZ(-vec.Z());
                }

                //
                // project the star onto the screen
                //

                float x = (float)(int)(scale * vec.X() + xc);
                float y = (float)(int)(yc - scale * vec.Y());
                float xold;
                float yold;

                if (data.m_bFirstFrame) {
                    xold = x;
                    yold = y;
                    data.m_bFirstFrame = false;
                } else {
                    xold = pointOld.X();
                    yold = pointOld.Y();
                }

                if (rect.Inside(Point(x, y))) {
                    float dx     = abs(x - xold);
                    float dy     = abs(y - yold);
                    float length = dx + dy;

                    if (length <= 1.0f) {
                        //
                        // Draw a point
                        //

                        indexPoint--;
                        pvertex[indexPoint].x        = x;
                        pvertex[indexPoint].y        = y;
                        pvertex[indexPoint].z        = 0;
                        pvertex[indexPoint].qw       = (float)1.0f/10000.0f;
                        pvertex[indexPoint].color    = MakeD3DCOLOR(color);
                        pvertex[indexPoint].specular = 0;
                    } else {
                        //
                        // Draw a line
                        //

                        if (length > 16.0f) {
                            float scale = 16.0f / length;

                            xold = x + (xold - x) * scale;
                            yold = y + (yold - y) * scale;
                        }

                        if (rect.Inside(Point(xold, yold))) {
                            float alpha  = 1.0f - 0.1f * length;

                            if (alpha < 0.25f) {
                                alpha = 0.25f;
                            }

                            pvertex[index * 2].x        = x;
                            pvertex[index * 2].y        = y;
                            pvertex[index * 2].z        = 0;
                            pvertex[index * 2].qw       = (float)1.0f/10000.0f;
                            pvertex[index * 2].color    = MakeD3DCOLOR(color * alpha);
                            pvertex[index * 2].specular = 0;

                            pvertex[index * 2 + 1].x        = xold;
                            pvertex[index * 2 + 1].y        = yold;
                            pvertex[index * 2 + 1].z        = 0;
                            pvertex[index * 2 + 1].qw       =(float)1.0f/10000.0f;
                            pvertex[index * 2 + 1].color    = MakeD3DCOLOR(color * alpha);
                            pvertex[index * 2 + 1].specular = 0;

                            pindex[index * 2    ] = index * 2;
                            pindex[index * 2 + 1] = index * 2 + 1;

                            index += 1;
                        }
                    }
                }

                pointOld.SetX(x);
                pointOld.SetY(y);
            };

            //
            // Do the rendering
            //

            pcontext->SetShadeMode(ShadeModeFlat);
            pcontext->DrawPoints(pvertex + indexPoint, count * 2 - indexPoint);

            if (index != 0) 
			{
                pcontext->SetBlendMode(BlendModeAdd);
                pcontext->DrawLines(pvertex, index * 2, pindex, index * 2);
            }
        }
    }
void VectorFieldRenderer::DrawSuperQuadric(Vector e){

	/* draw the superquadrics along the stream lines */

	float denom = e.X() + e.Y() + e.Z(); // common denmoinator
	float cL = ( e.X() - e.Y() )/denom; // linear
	float cP = ( 2.0 * (e.Y() - e.Z() ) )/denom; // planar
	float cS = ( 3.0 * e.Z() )/denom; // spherical

	float alpha = 0.0f;
	float beta = 0.0f;
	float gamma = 2.0f;

	float theta = 0.0f;
	float phi = 0.0f;

	int radialS = 10;
	int verticalS = 10;

	GLfloat verts[50][50][4]; 

	if(cL >= cP){ // if more linear in shape

		alpha = pow( static_cast<float>(1.0-cP), gamma);
		beta = pow( static_cast<float>(1.0-cL), gamma);

		for(int i = 0; i <= radialS; i++){
			theta = (2.0 * MY_PI * i)/radialS;			
			for(int j = 0; j <= verticalS; j++){

				phi = (MY_PI * j)/verticalS;

			verts[i][j][0] = _copysign( 1.0, cos(phi) ) * pow( fabs( cos(phi) ), beta);
			verts[i][j][1] = -_copysign( 1.0, sin(theta) ) * pow( fabs( sin(theta) ), alpha ) *
						_copysign( 1.0, sin(phi) ) * pow( fabs( sin(phi) ), beta );
			verts[i][j][2] = _copysign( 1.0, cos(theta) ) * pow( fabs( cos(theta) ), alpha ) *
						_copysign( 1.0, sin(phi) ) * pow( fabs( sin(phi) ), beta );
			verts[i][j][3] = 1.0;
			
			} // end j

		} // end i

	drawQuadric(verts, radialS, verticalS);

	}else if(cL < cP){
		
		alpha = pow( static_cast<float>(1.0-cL), gamma);
		beta = pow( static_cast<float>(1.0-cP), gamma);

		for(int i = 0; i <= radialS; i++){
			theta = (2.0 * MY_PI * i)/radialS;			
			for(int j = 0; j <= verticalS; j++){

				phi = (MY_PI * j)/verticalS;

				verts[i][j][0] = _copysign( 1.0, cos(theta) ) * pow( fabs( cos(theta) ), alpha ) *
						_copysign( 1.0, sin(phi) ) * pow( fabs( sin(phi) ), beta );
				verts[i][j][1] = _copysign( 1.0, sin(theta) ) * pow( fabs( sin(theta) ), alpha ) *
						_copysign( 1.0, sin(phi) ) * pow( fabs( sin(phi) ), beta );
				verts[i][j][2] = _copysign( 1.0, cos(phi) ) * pow( fabs( cos(phi) ), beta);
				verts[i][j][3] = 1.0;

			} // end j

		} // end i

		drawQuadric(verts, radialS, verticalS);

	} // end else
}
Example #27
0
int main( int argc, char ** argv )
{
  char * t_file = NULL;

  if ( argc < 2 ) {
    usage();
    return 0;
  }
  t_file = argv[1];

  fprintf(stderr, "Input calibration data file \"%s\"\n", t_file );
  
  InitDirections();

  // ------------------------------------------------------
  // test
  FILE * fp = fopen( t_file, "r" );
  if ( fp == NULL ) {
    fprintf(stderr, "ERROR: Cannot open test file \n");
    return 0;
  }
  // skip coeffs
  char line[256];
  int old_grp = -10;
  double compass_avg = 0.0;
  double clino_avg   = 0.0;
  int cnt_avg = 0;
  while ( fgets( line, 256, fp ) != NULL ) {
    unsigned int gx0, gy0, gz0, mx0, my0, mz0;
    int grp;
    int ignore;
    Vector g;
    Vector m;
    sscanf( line, "%x %x %x %x %x %x %d %d",
      &gx0, &gy0, &gz0, &mx0, &my0, &mz0, &grp, &ignore );
    if ( ignore == 1 ) continue;
    int16_t gx = (int16_t)gx0;
    int16_t gy = (int16_t)gy0;
    int16_t gz = (int16_t)gz0;
    int16_t mx = (int16_t)mx0;
    int16_t my = (int16_t)my0;
    int16_t mz = (int16_t)mz0;
    // printf("Raw data (%2d): %6d %6d %6d  %6d %6d %6d ",
    //   grp, gx, gy, gz, mx, my, mz );
    g.X() = gx;
    g.Y() = gy;
    g.Z() = gz;
    m.X() = mx;
    m.Y() = my;
    m.Z() = mz;
    g *= 1.0/g.length();
    m *= 1.0/m.length();
    Vector e( 1.0, 0.0, 0.0 );
    Vector m0 = g % (m % g);
    Vector e0 = g % (e % g);
    double clino = acos( g.X() / g.length() ) - M_PI/2;
    Vector em0 = e0 % m0;
    double s = em0.length() * ( ( em0*g > 0 ) ? -1.0 : 1.0 );
    double c = e0 * m0;
    double compass = atan2( s, c );
    if ( compass < 0 ) compass += 2.0 * M_PI;
    if ( grp >= 0 && grp == old_grp ) {
      if ( cnt_avg > 0 && ( fabs( compass - compass_avg / cnt_avg ) > 1.5*M_PI ) ) {
        if ( compass > M_PI ) {
          compass -= 2.0 * M_PI; // average around 0
        } else {
          compass += 2.0 * M_PI; // average around 360
        }
      }
      clino_avg   += clino;
      compass_avg += compass;
      cnt_avg     ++;
    } else {
      if ( cnt_avg > 0 ) {
          compass_avg /= cnt_avg;
          clino_avg   /= cnt_avg;
          // printf("%8.2f %8.2f cnt %d\n", compass_avg*RAD2GRAD, clino_avg*RAD2GRAD, cnt_avg );
          UpdateDirections( compass_avg, clino_avg );
      }
      clino_avg   = clino;
      compass_avg = compass;
      cnt_avg     = 1;
      old_grp = grp;
    }
  }
  if ( cnt_avg > 0 ) {
    compass_avg /= cnt_avg;
    clino_avg   /= cnt_avg;
    // printf("%8.2f %8.2f cnt %d\n", compass_avg*RAD2GRAD, clino_avg*RAD2GRAD, cnt_avg );
    UpdateDirections( compass_avg, clino_avg );
  }
  PrintDirections();
  fclose( fp );
  return 0;
}
  Vector Radial_Inline_Mesh_Desc::calc_coords_periodic(
      double total_theta,
      long long i,
      long long j,
      long long k)
  {
    // this function is used if ENFORCE PERIODIC is requested
    // it calculates all coordinates in the first 45 degrees of the domain
    // and then transforms them to the appropriate octant
    long long per=0;
    if(total_theta == 90.)per = nel_tot[1]/2;
    if(total_theta == 180.)per = nel_tot[1]/4;
    if(total_theta == 360.)per = nel_tot[1]/8;

    long long jmod = j%per;
    long long jmult = j/per;
    if(jmult %2 == 0){//even

    }
    else{
      jmod = per - jmod;
    }
    double xval,yval,zval;
    double deg_to_rad = M_PI/180.0;

    xval = IJKcoors[0][i]*cos(IJKcoors[1][jmod]*deg_to_rad);
    yval = IJKcoors[0][i]*sin(IJKcoors[1][jmod]*deg_to_rad);
    if(jmod == per){
      xval = IJKcoors[0][i]*cos(IJKcoors[1][jmod]*deg_to_rad);
      yval = xval;
    }

    zval = 0.0;
    if(dimension == 3){
      zval = IJKcoors[2][k];
    }

    Vector res = Vector( xval,yval,zval);

    //transforming back to original quadrant coordinates
    switch(jmult){
      case  0:{
                break;
              }
      case 1:{
               res = Vector(res.Y(),res.X(),res.Z());
               break;
             }
      case 2:{
               res =  Vector(-res.Y(),res.X(),res.Z());
               break;
             }
      case 3:{
               res = Vector(-res.X(),res.Y(),res.Z());
               break;
             }
      case 4:{
               res = Vector(-res.X(),-res.Y(),res.Z());
               break;
             }
      case 5:{
               res = Vector(-res.Y(),-res.X(),res.Z());
               break;
             }
      case 6:{
               res = Vector(res.Y(),-res.X(),res.Z());
               break;
             }
      case 7:{
               res = Vector(res.X(),-res.Y(),res.Z());
               break;
             }
    }
    return res;

  }