[vtkusers] visualizing NURBS++ surfaces (maybe a FAQ??)

Bryn Lloyd lloyd at itis.ethz.ch
Wed Aug 24 09:23:40 EDT 2011


Well, I guess you might modify the gluNurbsSurface function, and use it to
export the triangle mesh.

Otherwise, you could visualize using NURBS directly into your VTK
renderwindow using code like:


#include <vtkgl.h>

#include "vtkPolyData.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkActor.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"

#include "vtkProp3D.h"
#include "vtkObjectFactory.h"

#include "Options.h"

#include "vtkSmartPointer.h"
#define vtkNew(type,name) \
	vtkSmartPointer<type> name = vtkSmartPointer<type>::New()

// example code, replace e.g. by the NURBS++ "render" function
void callOpenGLCode(double a[3], double b[3], double c[3])
{
	glEnable(GL_COLOR_MATERIAL);
	glClearColor(0.7f, 0.9f, 1.0f, 1.0f);
	glColor4f(1.0f, 0.0f, 0.0f, 1.0f);

	glEnable(GL_CULL_FACE);
	glCullFace(GL_FRONT);

	glBegin (GL_TRIANGLES);
	glVertex3f (a[0],a[1],a[2]);
	glVertex3f (b[0],b[1],b[2]);
	glVertex3f (c[0],c[1],c[2]);
	glEnd ();

	glCullFace(GL_BACK);
	glBegin (GL_TRIANGLES);
	glVertex3f (a[0],a[1],a[2]);
	glVertex3f (b[0],b[1],b[2]);
	glVertex3f (c[0],c[1],c[2]);
	glEnd ();
}

class vtkMyProp3D : public vtkProp3D
{
public:
	vtkTypeMacro(vtkMyProp3D,vtkProp3D);
	void PrintSelf(ostream& os, vtkIndent indent) {}
	static vtkMyProp3D *New();

	int RenderOpaqueGeometry(vtkViewport *) 
	{
		int          renderedSomething = 0;
		callOpenGLCode(a,b,c);
		return renderedSomething;
	}
	void setABC(double _a[3], double _b[3], double _c[3]) 
	{
		for(int i=0; i<3; i++)
		{
			a[i] = _a[i];
			b[i] = _b[i];
			c[i] = _c[i];
		}
	}
	double* GetBounds() 
	{
		double *bounds = new double[6];
		for(int i=0; i<3; i++)
		{
			bounds[i*2]   =
vtkstd::min(vtkstd::min(a[i],b[i]),c[i]);
			bounds[i*2+1] =
vtkstd::max(vtkstd::max(a[i],b[i]),c[i]);
		}
		return bounds;
	}
	double a[3];
	double b[3];
	double c[3];
};

vtkStandardNewMacro(vtkMyProp3D);

int main (int argc, char **argv)
{

	std::string iname;
	Options opt(argc,argv);
	opt.add("i",&iname,"input file");
	opt.parse();

	double a[] = {0,0,0};
	double b[] = {1,0,0};
	double c[] = {1,1,1};
	double z = 0.3;

	// create input
	vtkNew(vtkPolyData, pdata);
	vtkNew(vtkPoints, pts);
	vtkNew(vtkCellArray, cells);
	pdata->SetPoints(pts);
	pdata->SetPolys(cells);
	pdata->GetPoints()->InsertNextPoint(a[0],a[1],a[2]+z);
	pdata->GetPoints()->InsertNextPoint(b[0],b[1],b[2]+z);
	pdata->GetPoints()->InsertNextPoint(c[0],c[1],c[2]+z);
	vtkIdType ids[] = {0,1,2};
	cells->InsertNextCell(3, ids);

	// map to graphics library
	vtkNew(vtkPolyDataMapper, map);
	map->SetInput(pdata);

	// actor coordinates geometry, properties, transformation
	vtkNew(vtkActor, aSphere);
	aSphere->SetMapper(map);
	aSphere->GetProperty()->SetColor(0,0,1);  // sphere color blue

	// a renderer and render window
	vtkNew(vtkRenderer, ren1);
	vtkNew(vtkRenderWindow, renWin);
	renWin->AddRenderer(ren1);

	// an interactor
	vtkNew(vtkRenderWindowInteractor, iren);
	iren->SetRenderWindow(renWin);

	// add the actor to the scene
	ren1->AddActor(aSphere);
	//ren1->SetBackground(1,1,1);  // Background color white

	vtkNew(vtkMyProp3D,myprop);
	myprop->setABC(a,b,c);
	ren1->AddActor(myprop);

	// render an image (lights and cameras are created automatically)
	renWin->Render();

	// begin mouse interaction
	iren->Start();

	return 0;
}





More information about the vtkusers mailing list