library(dbscan)
library(rgl)
library(cluster)
cluster=function(x,eps,mpt,dim,k,xlab,ylab,zlab,y)
{
	if(dim==1)
	{
		x[is.na(x)] <- round(mean(x, na.rm = TRUE))
	}else
	{
		for(i in 1:dim)
		{
			x[,i][is.na(x[,i])] <- round(mean(x[,i], na.rm = TRUE))
		}
	}
	if(missing(xlab)) xlab="X"
	if(missing(ylab)) ylab="Y"
	if(missing(zlab)) zlab="Z" 
	if(dim==1) n=length(x)
	if(dim>1) n=length(x[,1])
	if(missing(y)) 
	{
		y=1:n
		x1=0
	} else
	{
		x1=cbind(y,x)
	}

	d=dbscan(x,eps,mpt)
		
	mean=array(dim=c(k,dim))
	seq1=c(1:k)/(k+1)
	seq2=c(k:1)/(k+1)
	mean[,1]=quantile(x[,1],seq1)

	if(dim==2)
	{
		c=cor(x[,1],x[,2])
		if(c>0) 
		{
			mean[,2]=quantile(x[,2],seq1)
		}else
		{
			mean[,2]=quantile(x[,2],seq2)
		}
	}

	if(dim>2)
	{
		a=array(dim=c((dim-1),(dim-1)))
		for(i in 1:(dim-1))
		{
			for(j in 2:dim)
			{
				a[i,j-1]=cor(x[,i],x[,j])
			}
		}

		for(i in 1:(dim-1))
		{
			for(j in 2:dim)
			{
				if(a[i,(j-1)]>0)
				{
					mean[,j]=quantile(x[,j],seq1)
				} else
				{
					mean[,j]=quantile(x[,j],seq2)
				}
			}
		}
	}
	Kclus=array()
	clus=array(list())
	for(i in 1:n)
	{
		dist=array()
		for(j in 1:k)
		{
			dist[j]=dist(rbind(as.vector(x[i,]),as.vector(mean[j,])))
		}
		m=which(dist==min(dist))
		clus[m]=list(unlist(c(clus[m],i)))
		Kclus[i]=m
		for(i in 1:dim)
		{
			mean[m,i]=mean(c(mean[m,i],x[unlist(clus[m]),i]))
		}
	}
	ssw=0
	for (i in 1:k)
	{
		for(j in 1: length(unlist(clus[i])))
		{
			for(l in 1:dim)
			{
				ssw=ssw+((x[unlist(clus[i])[j],l]-mean[i,l])**2)
			}
		}
	}
	tss=0
	for(i in 1:dim)
	{	
		tss=tss+sum((x[,i]-mean(x[,i]))**2)
	}
	ssb=tss-ssw
	
	if(dim==1) 
	{ 
		par(mfrow=c(1,2))
		clusplot(cbind(y,x),Kclus,main="k-means clusters",color=T,shade=T,lines=0,xlab=xlab,ylab=ylab)
		clusplot(cbind(y,x),d$cluster,main="dbscan clusters",color=T,shade=T,lines=0,xlab=xlab,ylab=ylab)
	}
	if (dim==2)
	{
		if (x1==0)
		{ 
			par(mfrow=c(1,2))
			clusplot(x,Kclus,main="k-means clusters",color=T,shade=T,lines=0,xlab=xlab,ylab=ylab)
			clusplot(x,d$cluster,main="dbscan clusters",color=T,shade=T,lines=0,xlab=xlab,ylab=ylab)
		}else
		{	
			open3d()
			clk <- as.factor(Kclus)
			plot3d(x1, col=clk, main="k-means clusters",xlab=xlab,ylab=ylab,zlab=zlab)
			open3d()
			cld <- as.factor((d$cluster)+1)
			plot3d(x1, col=cld, main="dbscan clusters",xlab=xlab,ylab=ylab,zlab=zlab)
		}
	}
	
	if(dim==3)
	{	
		open3d()
		clk <- as.factor(Kclus)
		plot3d(x, col=clk, main="k-means clusters",xlab=xlab,ylab=ylab,zlab=zlab)
		open3d()
		cld <- as.factor((d$cluster)+1)
		plot3d(x, col=cld, main="dbscan clusters",xlab=xlab,ylab=ylab,zlab=zlab)
	}
	
	print("dbscan cluster")
	print(d)
	print("k-means cluster")
	num=unlist(lapply(clus,length))
	da=data.frame(1:k,num)
	colnames(da)=c("Cluster Number", "No of elements")
	print(da)
	print("SSB as a percent Of TSS")
	s=ssb*100/tss
	print(s)
	Clus=list("dbscan"=d,"dclus"=d$cluster,"KMmean"=mean,"Kclus"=Kclus,"Kmeans"=da,"SSW"=s)
	invisible(Clus)
}	

cluster(matrix,epsilon,minpoint,dimension,kmeanscluster,xlab,ylab,zlab,other)



