// Created on: 1995-07-24
// Created by: Modelistation
// Copyright (c) 1995-1999 Matra Datavision
// Copyright (c) 1999-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.

#include <Adaptor3d_Surface.hxx>
#include <Geom_BezierSurface.hxx>
#include <Geom_BSplineSurface.hxx>
#include <Graphic3d_ArrayOfPolylines.hxx>
#include <Graphic3d_Group.hxx>
#include <Prs3d_IsoAspect.hxx>
#include <StdPrs_WFPoleSurface.hxx>
#include <gp_Pnt.hxx>
#include <NCollection_Array2.hxx>

static void AddPoles(const occ::handle<Prs3d_Presentation>& aPresentation,
                     const NCollection_Array2<gp_Pnt>&      A,
                     const occ::handle<Prs3d_Drawer>&       aDrawer)
{
  int       i, j;
  const int n = A.ColLength();
  const int m = A.RowLength();

  aPresentation->CurrentGroup()->SetPrimitivesAspect(aDrawer->UIsoAspect()->Aspect());
  occ::handle<Graphic3d_ArrayOfPolylines> aPrims = new Graphic3d_ArrayOfPolylines(n * m, n);
  for (i = 1; i <= n; i++)
  {
    aPrims->AddBound(m);
    for (j = 1; j <= m; j++)
      aPrims->AddVertex(A(i, j));
  }
  aPresentation->CurrentGroup()->AddPrimitiveArray(aPrims);

  aPresentation->CurrentGroup()->SetPrimitivesAspect(aDrawer->VIsoAspect()->Aspect());
  aPrims = new Graphic3d_ArrayOfPolylines(n * m, m);
  for (j = 1; j <= m; j++)
  {
    aPrims->AddBound(n);
    for (i = 1; i <= n; i++)
      aPrims->AddVertex(A(i, j));
  }
  aPresentation->CurrentGroup()->AddPrimitiveArray(aPrims);
}

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

void StdPrs_WFPoleSurface::Add(const occ::handle<Prs3d_Presentation>& aPresentation,
                               const Adaptor3d_Surface&               aSurface,
                               const occ::handle<Prs3d_Drawer>&       aDrawer)
{

  GeomAbs_SurfaceType SType = aSurface.GetType();
  if (SType == GeomAbs_BezierSurface || SType == GeomAbs_BSplineSurface)
  {
    int n, m;
    if (SType == GeomAbs_BezierSurface)
    {
      occ::handle<Geom_BezierSurface> B = aSurface.Bezier();
      n                                 = aSurface.NbUPoles();
      m                                 = aSurface.NbVPoles();
      NCollection_Array2<gp_Pnt> A(1, n, 1, m);
      (aSurface.Bezier())->Poles(A);
      AddPoles(aPresentation, A, aDrawer);
    }
    else if (SType == GeomAbs_BSplineSurface)
    {
      occ::handle<Geom_BSplineSurface> B = aSurface.BSpline();
      n                                  = (aSurface.BSpline())->NbUPoles();
      m                                  = (aSurface.BSpline())->NbVPoles();
      NCollection_Array2<gp_Pnt> A(1, n, 1, m);
      (aSurface.BSpline())->Poles(A);
      AddPoles(aPresentation, A, aDrawer);
    }
  }
}
