//===========================================================================
// GoTools - SINTEF Geometry Tools version 1.0
//
// GoTools module: Implicitization, version 0.9
//
// Copyright (C) 2000-2005 SINTEF ICT, Applied Mathematics, Norway.
//
// This program is free software; you can redistribute it and/or          
// modify it under the terms of the GNU General Public License            
// as published by the Free Software Foundation version 2 of the License. 
//
// This program is distributed in the hope that it will be useful,        
// but WITHOUT ANY WARRANTY; without even the implied warranty of         
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
// GNU General Public License for more details.                           
//
// You should have received a copy of the GNU General Public License      
// along with this program; if not, write to the Free Software            
// Foundation, Inc.,                                                      
// 59 Temple Place - Suite 330,                                           
// Boston, MA  02111-1307, USA.                                           
//
// Contact information: e-mail: tor.dokken@sintef.no                      
// SINTEF ICT, Department of Applied Mathematics,                         
// P.O. Box 124 Blindern,                                                 
// 0314 Oslo, Norway.                                                     
//
// Other licenses are also available for this software, notably licenses
// for:
// - Building commercial software.                                        
// - Building software whose source code you wish to keep private.        
//===========================================================================
#include "ImplicitizeCurveAndVectorAlgo.h"
#include "ImplicitizeSurfaceAlgo.h"
// #include "ImplicitUtils.h"
// #include "GeometryTools.h"


using namespace std;


namespace Go {


//==========================================================================
int ImplicitizeCurveAndVectorAlgo::perform()
//==========================================================================
{
    // Make a ruled surface
    int ncoefsu = crv_.numCoefs();
    int ncoefsv = 2;
    int ordu = crv_.order();
    int ordv = 2;
    typedef vector<double>::iterator iter;
    iter knotsu_begin = crv_.basis().begin();
    double knotsv_begin[] = { 0.0, 0.0, 1.0, 1.0 };
    BoundingBox box = crv_.boundingBox();
    Point diagonal = box.high() - box.low();
    double scale = diagonal.length();
    bool rational = crv_.rational();
    int dim = 3;
    int effdim = (rational ? dim+1 : dim);
    vector<double> coefs(effdim * ncoefsu * ncoefsv);
    iter curr = crv_.coefs_begin();
    iter down = coefs.begin();
    iter up = coefs.begin() + effdim * ncoefsu;
    for (int i = 0; i < ncoefsu; ++i) {
	double w = (rational ? *(curr + dim) : 1.0);
	Point tmppt(curr, curr+dim);
	tmppt -= w * 0.5 * scale * pt_;
	copy(tmppt.begin(), tmppt.end(), down);
	down += effdim;
	tmppt += w * scale * pt_;
	copy(tmppt.begin(), tmppt.end(), up);
	up += effdim;
	curr += effdim;
    }
    SplineSurface surf(ncoefsu, ncoefsv, ordu, ordv,
		       knotsu_begin, knotsv_begin, coefs.begin(), dim,
		       crv_.rational());

    // Run surface algorithm
    ImplicitizeSurfaceAlgo algo(surf, deg_);
    algo.setTolerance(tol_);
    algo.perform();
    algo.getResultData(implicit_, bc_);

    return 0;
}


//==========================================================================


} // namespace Go
