[vtkusers] Appeal for Help- My Conics Rendering doesn't render properly

Richard de Paulo rdepaulo at cox.net
Sun Oct 16 22:41:21 EDT 2005


// Can anyone help me with this code?
// It doesn't seem to render properly for different conics. I wonder if the 
lettering changes the rendering.
// I'd appreciate any clarification or direction here.

// quadrics2_demo.cpp	RICHARD DE PAULO   10-24-04

/* This program can render various polynomial quadrics

	Ax^2   By^2   Cxy		Dx		Ey		F		SHAPE

      0	   	1		0		4	   -8		1		parabola
	-1      0       0	   -2 	   -3		1		parabola
      2 	    1 	    0	   -4 	   -6		1		ellipse
      2 	   -1 	    0		4 		6		1		hyperbola
	 1     -3       5		7		9		1		hyperbola
	-1     -3      -5	   -7	   -9		1		hyperbola
	-2     -3	   -4	   -5	   -6		1		ellipse
	 1		3		5		7		9		1		quadric
	 1		1		0		0	   -9		1		circle
	 9		9		0		0	   -4		1		circle
	 1		0		0		0	   -4		1		parabola up
	-4		0		0		0		6		1		parabola down
*/
// Enter each quadric coefficient followed by the absolute
// value of that coefficent.
// Enter zero for the coefficient when the quadric is missing a term.

#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkQuadric.h"
#include "vtkSampleFunction.h"
#include "vtkContourFilter.h"
#include "vtkExtractVOI.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkProperty.h"
#include "vtkWindowToImageFilter.h"
#include "vtkBMPWriter.h"
#include "vtkLODActor.h"
#include "vtkTextMapper.h"
#include "vtkAxes.h"
#include "vtkVectorText.h"
#include "vtkLinearExtrusionFilter.h"
#include "vtkFollower.h"
#include "vtkActor2D.h"
#include "vtkProperty2D.h"
#include <stdlib.h>
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkLight.h"


int main ()
{
	vtkRenderer *renderer = vtkRenderer::New();

	vtkRenderWindow *renWin = vtkRenderWindow::New();	
		renWin->AddRenderer(renderer);

	vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
		iren->SetRenderWindow(renWin);

	//initialize equation array
	const int NUMCHARS = 22;
	vtkVectorText *eqn[NUMCHARS];
     vtkLinearExtrusionFilter *extrude[NUMCHARS];
     vtkPolyDataMapper *mappers[NUMCHARS];
     vtkActor *actors[NUMCHARS];
	static char a[NUMCHARS];

	char  A = ' ';
	char  B = ' ';
	char  C = ' ';
	char  D = ' ';
	char  E = ' ';
	char  F = ' ';

	a[0] =  '0';
	a[1] =  '=';
	a[2] =  ' ';
	a[3] =   A;
	a[4] =  'x';
	a[5] =  '2';
	a[6] =  '+';
	a[7] =   B;
	a[8] =  'y';
	a[9] =  '2';
	a[10] = '+';
	a[11] =  C;
	a[12] = 'x';
	a[13] = 'y';
	a[14] = '+';
	a[15] =  D;
	a[16] = 'x';
	a[17] = '+';
	a[18] =  E;
	a[19] = 'y';
	a[20] = '+';
	a[21] = 'F';

//Input coefficients for quadric
  	//VTK GENERAL FORM OF A POLYNOMIAL (without z component)

	float  dlow = -12;	//lower-upper domain
	float  dhigh = 12;
	float  rlow = -12;	//lower-upper range
	float  rhigh = 12;
	float  zwidth = 2.5;
     double	d5, //x^2	coefficients for vtkQuadric
			d4, //y^2
			d3, //xy
			d2, //x
			d1, //y
			d0; //constant

	cout << "\tQUADRIC DEMO\n";
	cout << "\ty = f(x,y) = Ax*x + By*y + Cxy + Dx + Ey + F\n";
	cout << "Enter quadric equation coefficients\n";

     //x^2 term
	cout << "Enter leading term floating-point coefficient A: ";
	cin >> d5;
	if (d5 < 0)
		a[2] = '-';  //reset the char array if the coefficient is negative
	cin >> A;
	a[3] = A;
	
	//y^2 term
	cout << endl;
	cout << "Enter floating-point coefficient B: ";
	cin >> d4;
	if (d4 < 0)
		a[6] = '-';
	cin >> B;
	a[7] = B;
		
	//xy term
	cout << endl;
	cout << "Enter floating-point coefficient C: ";
	cin >> d3;
	if (d3 < 0)
		a[10] = '-';
	cin >> C;
	a[11] = C;

	//x term
	cout << endl;
	cout << "Enter floating-point coefficient D: ";
	cin >> d2;
	if (d2 < 0)
		a[14] = '-';
	cin >> D;
	a[15] = D;

	//y term
	cout << endl;
	cout << "Enter floating-point coefficient E: ";
	cin >> d1;
	if (d1 < 0)
		a[17] = '-';
	cin >> E;
     a[18] = E;
		
	//constant term
	cout << "Enter floating-point coefficient F: ";
	cin >> d0;
	if (d0 < 0)
		a[20] = '-';
	cin >> F;
     a[21] = F;
	cout << endl;

	// angle of rotation of quadric  cot(theta) = (d5 - d4)/d3
	// angle theta = arctan(d3/(d5-d4))

	double theta = atan(d3/(d5-d4));
	cout << "angle of rotation: " << theta << endl;

	double range  = rhigh - rlow;
	double domain = dhigh - dlow;

	//Initialize the coefficients of vtkQuadric
	//f(x,y,z) = ax^2 + by^2 + cz^2 + dxy + exz + fyz + gx + hy +iz + 
j
	vtkQuadric *quadric = vtkQuadric::New();
		quadric->SetCoefficients(d5, d4, 0, d3, 0, 0, d2, d1, 0, d0);

	vtkSampleFunction * sample = vtkSampleFunction::New();
		sample->SetSampleDimensions(domain, range, 10);
		sample->SetImplicitFunction(quadric);
		sample->SetModelBounds(dlow, dhigh, rlow, rhigh, 0, zwidth);
		sample->SetComputeNormals(false);
	
	vtkExtractVOI * extract = vtkExtractVOI::New();
		extract->SetInput(sample->GetOutput());
		extract->SetVOI(0,100,0,100,0,100);
		extract->SetSampleRate(1,1,1);
	
	vtkContourFilter *contour = vtkContourFilter::New();
		contour->SetInput((vtkDataSet *)(extract->GetOutput()));
		contour->GenerateValues(1,0.0,zwidth);

	vtkPolyDataMapper *volMapper = vtkPolyDataMapper::New();
		volMapper->SetInput(contour->GetOutput());
		volMapper->SetScalarRange(0.0,1.0);

	vtkActor *volActor = vtkActor::New();
		volActor->SetMapper(volMapper);
		volActor->GetProperty()->SetColor( 0 , 0.5, 0 );

   //renWin->SetSize(20, 20);
   volActor->SetMapper(volMapper);
   volActor->GetProperty()->SetColor(1, 0, 0);
   volActor->GetProperty()->SetAmbient(0.3);
   volActor->GetProperty()->SetDiffuse(0.875);
   volActor->GetProperty()->SetSpecular(0.0);
   volActor->AddPosition(3.75, 1.25, 0);

   /*  UNCOMMENT IF YOU WANT TO TURN ON CAMERA LIGHTING

   // Set up the lighting.
   vtkLight *light = vtkLight::New();
	light->SetFocalPoint(1.875,0.6125,0);
	light->SetPosition(0.875,1.6125,1);

   renderer->AddLight(light);

   // We want to eliminate perspective effects on the apparent lighting.
   // Parallel camera projection will be used. To zoom in parallel projection
   // mode, the ParallelScale is set.

   renderer->GetActiveCamera()->SetFocalPoint(0,0,0);
   renderer->GetActiveCamera()->SetPosition(0,0,1);
   renderer->GetActiveCamera()->SetViewUp(0,1,0);
   renderer->GetActiveCamera()->ParallelProjectionOn();
   renderer->ResetCamera();
   renderer->GetActiveCamera()->SetParallelScale(1.5);

*/

   // Setup equation characters
	char text[2];
	text[1] = '\0';

     for (int i=0; i<NUMCHARS; i++)
     {
      text[0] = a[i];
      eqn[i] = vtkVectorText::New();
      eqn[i]->SetText(text);
      extrude[i] = vtkLinearExtrusionFilter::New();
      extrude[i]->SetInput(eqn[i]->GetOutput());
      extrude[i]->SetExtrusionType(VTK_VECTOR_EXTRUSION);
      extrude[i]->SetVector(0,0,1.0);
	 if (i == 5 || i == 9)
		 extrude[i]->SetScaleFactor(0.25);
	 else
         extrude[i]->SetScaleFactor(0.5);
      mappers[i] = vtkPolyDataMapper::New();
      mappers[i]->SetInput(extrude[i]->GetOutput());
      mappers[i]->ScalarVisibilityOn();
      actors[i] = vtkActor::New();
      actors[i]->SetMapper(mappers[i]);
      actors[i]->GetProperty()->SetColor(0.2000, 0.6300, 0.7900);
      renderer->AddActor(actors[i]);
	}

	// Position equation's character actors
	int k;
	float x = 12;
	float y = -2;

	for (k = 0; k < NUMCHARS; k++)
	{
		if (k == 5 || k == 9)
		{
			actors[k]->SetPosition(x - 0.05, y + 0.80, 0.0);
			x-= 0.45;
		}
		else
			actors[k]->SetPosition(x, y, 0.0);
		x += 1.1;
	}

	// BUILD A QUADRIC LABEL
	vtkTextMapper * textMapper = vtkTextMapper::New();

	//  f(x,y) = Ax*x + By*y + Cxy + Dx + Ey + F
	//		parabola	if C^2 - 4AB = 0
	//		ellipse		if C^2 - 4AB < 0
	//		hyperbola	if C^2 - 4AB > 0
	//  f(x,y) = d5x*x + d4y*y + d3xy + d2x + d1y + d0

     if (d5 == 0 && d4 == 0 && d3 == 0)
		textMapper->SetInput("Line");
	
	else if(d3*d3-4*d5*d4 == 0)
		textMapper->SetInput("Parabola");

	else if (d3*d3-4*d5*d4 < 0)
	{
		if (d5 == d4 && d3 == 0)
			textMapper->SetInput("Circle");
		else
		    textMapper->SetInput("Ellipse");
	}
	else if (d3*d3-4*d5*d4 > 0)
		textMapper->SetInput("Hyperbola");

	else textMapper->SetInput("Not a Quadric");

	  textMapper->SetFontSize( 30 );
	  textMapper->SetFontFamilyToTimes(); //Or Courier or Ariel
	  textMapper->BoldOn();
	  textMapper->ItalicOn();
	  textMapper->ShadowOn();
	vtkActor2D * text2 = vtkActor2D::New();
	  text2->SetMapper( textMapper );
	  text2->SetPosition( 90, 20 );
	  text2->GetProperty()->SetColor( 0, 1.0, 1.0 );

	
	//BUILD AXES DRAWING ( 4x4x4 )
	vtkAxes * axes = vtkAxes::New();
	  axes->SetOrigin( 0, 0, 0 );
	  axes->SymmetricOn();
	  axes->SetSymmetric(-20);
	  axes->SetScaleFactor( 10 );

	vtkPolyDataMapper * axesMapper = vtkPolyDataMapper::New();
	  axesMapper->SetInput( axes->GetOutput() );

	vtkActor * axesActor = vtkActor::New();
	  axesActor->SetMapper( axesMapper );


	//LABEL ORIGIN
	vtkVectorText * atext = vtkVectorText::New();
	  atext->SetText( "origin" );

	vtkPolyDataMapper * atextMapper = vtkPolyDataMapper::New();
	  atextMapper->SetInput( atext->GetOutput() );

	vtkFollower * atextActor = vtkFollower::New();
	  atextActor->SetMapper( atextMapper );
	  atextActor->SetScale( 0.82, 0.82, 0.82 );
	  atextActor->AddPosition( -1.5, -0.1, 0 );
	  atextActor->GetProperty()->SetColor( 0, 0, 0 ); //black
	

	//LABEL AXES

	// X axis label
	vtkVectorText * xtext = vtkVectorText::New();
	  xtext->SetText( "x" );

	vtkPolyDataMapper * xtextMapper = vtkPolyDataMapper::New();
	  xtextMapper->SetInput( xtext->GetOutput() );

	vtkFollower * xtextActor = vtkFollower::New();
	  xtextActor->SetMapper( xtextMapper );
	  xtextActor->SetScale( 1.15, 1.15, 0.25 );
	  xtextActor->SetPosition( 10, 0, 0);
	  xtextActor->GetProperty()->SetColor( 1, 0, 0 ); //red
	
	// Y axis label
	vtkVectorText * ytext = vtkVectorText::New();
	  ytext->SetText( "y" );

	vtkPolyDataMapper * ytextMapper = vtkPolyDataMapper::New();
	  ytextMapper->SetInput( ytext->GetOutput() );

	vtkFollower * ytextActor = vtkFollower::New();
	  ytextActor->SetMapper( ytextMapper );
	  ytextActor->SetScale( 1.15, 1.15, 0.25 );
	  ytextActor->SetPosition( 0, 10, 0);
	  ytextActor->RotateZ(0);
	  ytextActor->GetProperty()->SetColor( 1, 1, 0 ); //yellow
	
	renderer->SetBackground(1,0,1);

	renderer->AddActor2D( text2 );
	renderer->AddActor( atextActor );
	renderer->AddActor( axesActor );
	renderer->AddActor( xtextActor );
	renderer->AddActor( ytextActor );
	renderer->AddActor( volActor );
   //renderer->AddActor( ztextActor );

	renWin->FullScreenOn();
	// interact with data
	renWin->Render();
   	iren->Start();
	
	// make a bitmap file
	vtkWindowToImageFilter *w2if = vtkWindowToImageFilter::New();
	vtkBMPWriter * bmp = vtkBMPWriter::New();
   		w2if->SetInput( renWin );
		bmp->SetInput( w2if->GetOutput() );
		bmp->SetFileName( "Ax^2 + By^2 + Cxy + Dx + Ey + F.bmp" );
		bmp->Write();

	return 0;
}
Godspeed,

Richard de Paulo
Phone: (757) 432-9404
Fax: (757) 420-7210
rdepaulo at cox.net
r.a.depaulo at nsu.edu
active endorsed AP Computer Science Consultant
Mathematics/Computer Science Teacher
Hickory High School
1996 Hawk Blvd., Room 237
Chesapeake, Virginia 23322

"In matters of style, swim with the current;
  in matters of principle, stand like a rock."
  -Thomas Jefferson

----------
The information contained in this electronic message is legally privileged 
and confidential under applicable law, and is intended only for the use of 
the individual or entity named above. If you are not the intended recipient 
of this message, you are hereby notified that any use, distribution, 
copying or disclosure of this communication is strictly prohibited. If you 
have received this communication in error, please notify and return e-mail 
to rdepaulo at cox.net, and purge the communication immediately without making 
any copy or distribution.  





More information about the vtkusers mailing list