Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » OpenGL » General » Downloads
 
Add your own
alternative version

Basic Curves And Surfaces Modeler

, 18 Apr 2012
A basic demo of modeling curves and surfaces in OpenGL.
cadsurfexe.zip
CadSurfexe
CadSurf.exe
VKGeom.dll
VKGraphic.dll
cadsurf_demo.zip
cadsurf_src.zip
CadSurf_src
VKernel
VKGraphic
VKGraphic.dsp
VKGraphic.dsw
VKGraphic.plg
VKGeom
VKGeom.dsp
VKGeom.dsw
cadsurf
CadSurf.clw
CadSurf.dsp
cadsurf.dsw
Thumbs.db
res
bmp00001.bmp
cadsurf.ico
CADSURFDOC.ICO
new_splash.bmp
new_splash.PNG
new_splash_small.bmp
tb_geom.bmp
Thumbs.db
toolbar.bmp
toolbar1.bmp
untitled.bmp
XPStyle.manifest
CadSurf_VS2008.zip
CadSurf
cadsurf
CadSurf.suo
res
bmp00001.bmp
cadsurf.ico
CADSURFDOC.ICO
new_splash.bmp
new_splash.PNG
new_splash_small.bmp
tb_geom.bmp
Thumbs.db
toolbar.bmp
toolbar1.bmp
untitled.bmp
VKernel
VKGeom
VKGraphic
VKGraphic.plg
// BezierCurve.cpp: implementation of the CBezierCurve class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "BezierCurve.h"
#include "Point3D.h"
#include "GeomException.h"
#include "MMath.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBezierCurve::CBezierCurve()
{
	cType = BEZIER;
}

CBezierCurve::CBezierCurve(CListOfCPoint3D *aList)
{
	cType = BEZIER;
	CPoint3D curP;
	cpList = new CListOfCPoint3D;
	cpCount = 0;
	CListIteratorOfListOfCPoint3D myListIter(aList);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
	{
		curP = myListIter.Current();
		cpList->Append(curP);
		cpCount++;
	}
	if(cpCount > GetMaxDegree())
		throw CGeomException(MaxDegree);
}

CBezierCurve::~CBezierCurve()
{
	cpList->Clear();
	if(cpList)
		delete cpList;
}

void CBezierCurve::Reverse()
{
	CListOfCPoint3D *temp = new CListOfCPoint3D;
	CListIteratorOfListOfCPoint3D myListIter(cpList);

	for(myListIter.Init(); myListIter.More(); myListIter.Next())
		temp->Append(myListIter.Current());

	cpList->Clear();

	myListIter.SetList(temp);

	for(myListIter.Init(); myListIter.More(); myListIter.Next())
		cpList->Prepend(myListIter.Current());

	delete temp;
}

CPoint3D CBezierCurve::PointAtPara(const double& para)
{
	CPoint3D P;
	int cnt = 0;
	double x=0, y=0, z=0;
	double degree = cpCount-1;
	CListIteratorOfListOfCPoint3D myListIter(cpList);

	if(para == FirstParameter())
	{
		P = cpList->First();
		return P;
	}

	if(para == LastParameter())
	{
		P = cpList->Last();
		return P;
	}

	for(myListIter.Init(); myListIter.More(); myListIter.Next(), cnt++)
	{
		x += myListIter.Current().GetX()*BlendingFunc((int)degree,cnt,para);
		y += myListIter.Current().GetY()*BlendingFunc((int)degree,cnt,para);
		z += myListIter.Current().GetZ()*BlendingFunc((int)degree,cnt,para);
	}
	P.SetParam(x,y,z);
	return P;
}

double CBezierCurve::FirstParameter() const
{
	return 0.0;
}

double CBezierCurve::LastParameter() const
{
	return 1.0;
}

bool CBezierCurve::IsClosed() const
{
	return false;
}

CCurve* CBezierCurve::Copy() const
{
	CBezierCurve* C = new CBezierCurve(cpList);
	return C;
}

void CBezierCurve::Translate(const COneAxis& Ax, const double& amt)
{
	CGeometry::Translate(Ax, amt);
}

void CBezierCurve::Translate(double dx, double dy, double dz)
{
	CListOfCPoint3D *temp = new CListOfCPoint3D;
	CPoint3D curP;
	CListIteratorOfListOfCPoint3D myListIter(cpList);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
	{
		curP = myListIter.Current();
		curP.Translate(dx,dy,dz);
		temp->Append(curP);
	}
	cpList->Clear();
	myListIter.SetList(temp);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
		cpList->Append(myListIter.Current());
	delete temp;
}


void CBezierCurve::Translate(const CVector3D& V)
{
	CListOfCPoint3D *temp = new CListOfCPoint3D;
	CPoint3D curP;
	CListIteratorOfListOfCPoint3D myListIter(cpList);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
	{
		curP = myListIter.Current();
		curP.Translate(V);
		temp->Append(curP);
	}
	cpList->Clear();
	myListIter.SetList(temp);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
		cpList->Append(myListIter.Current());
	delete temp;
}

void CBezierCurve::Translate(const CPoint3D& P1, const CPoint3D& P2)
{
	CListOfCPoint3D *temp = new CListOfCPoint3D;
	CPoint3D curP;
	CListIteratorOfListOfCPoint3D myListIter(cpList);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
	{
		curP = myListIter.Current();
		curP.Translate(P1, P2);
		temp->Append(curP);
	}
	cpList->Clear();
	myListIter.SetList(temp);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
		cpList->Append(myListIter.Current());
	delete temp;
}

void CBezierCurve::Rotate(const COneAxis& Ax, double ang)
{
	CListOfCPoint3D *temp = new CListOfCPoint3D;
	CPoint3D curP;
	CListIteratorOfListOfCPoint3D myListIter(cpList);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
	{
		curP = myListIter.Current();
		curP.Rotate(Ax, ang);
		temp->Append(curP);
	}
	cpList->Clear();
	myListIter.SetList(temp);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
		cpList->Append(myListIter.Current());
	delete temp;
}

void CBezierCurve::Scale(const CPoint3D& P, double fact)
{
	CListOfCPoint3D *temp = new CListOfCPoint3D;
	CPoint3D curP;
	CListIteratorOfListOfCPoint3D myListIter(cpList);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
	{
		curP = myListIter.Current();
		curP.Scale(P, fact);
		temp->Append(curP);
	}
	cpList->Clear();
	myListIter.SetList(temp);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
		cpList->Append(myListIter.Current());
	delete temp;
}

void CBezierCurve::Mirror(const CPoint3D& P)
{
	CListOfCPoint3D *temp = new CListOfCPoint3D;
	CPoint3D curP;
	CListIteratorOfListOfCPoint3D myListIter(cpList);

	for(myListIter.Init(); myListIter.More(); myListIter.Next())
	{
		curP = myListIter.Current();
		curP.Mirror(P);
		temp->Append(curP);
	}
	cpList->Clear();
	myListIter.SetList(temp);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
		cpList->Append(myListIter.Current());
	delete temp;
}

void CBezierCurve::Mirror(const COneAxis& Ax)
{
	CListOfCPoint3D *temp = new CListOfCPoint3D;
	CPoint3D curP;
	CListIteratorOfListOfCPoint3D myListIter(cpList);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
	{
		curP = myListIter.Current();
		curP.Mirror(Ax);
		temp->Append(curP);
	}
	cpList->Clear();
	myListIter.SetList(temp);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
		cpList->Append(myListIter.Current());
	delete temp;
}

void CBezierCurve::Mirror(const CPlane& Pln)
{
	CListOfCPoint3D *temp = new CListOfCPoint3D;
	CPoint3D curP;
	CListIteratorOfListOfCPoint3D myListIter(cpList);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
	{
		curP = myListIter.Current();
		curP.Mirror(Pln);
		temp->Append(curP);
	}
	cpList->Clear();
	myListIter.SetList(temp);
	for(myListIter.Init(); myListIter.More(); myListIter.Next())
		cpList->Append(myListIter.Current());
	delete temp;
}

double CBezierCurve::BlendingFunc(int n, int r, double t)
{
	long int p=1,m=1;
	int i;
	if(t<1e-5)
		return  (r==0 ?  1.0 : 0.0);
	if(t>.999995)
		return (r==n ? 1.0 : 0.0) ;

	for(i=1;i<=r;i++)
	{
		m*=i;
		p*=(n-i+1);
	}
	return ((double)(p/m))*pow(t,r)*pow(1.0-t,n-r);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sharjith
Engineer Tata Technologies Ltd
India India
Sharjith is a Mechanical Engineer with strong passion for Automobiles, Aircrafts and Software development.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 18 Apr 2012
Article Copyright 2003 by Sharjith
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid