0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / GeometryTest / GeometryTest_PolyCommands.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 93f843d..9d582b7
@@ -1,41 +1,36 @@
 // Created on: 1995-03-06
 // Created by: Laurent PAINNOT
 // Copyright (c) 1995-1999 Matra Datavision
-// Copyright (c) 1999-2012 OPEN CASCADE SAS
+// Copyright (c) 1999-2014 OPEN CASCADE SAS
 //
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
 //
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// 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.
 //
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
 
-
-#include <GeometryTest.ixx>
-#include <Poly_Triangulation.hxx>
+#include <Draw_Appli.hxx>
+#include <DrawTrSurf.hxx>
+#include <DrawTrSurf_Polygon2D.hxx>
+#include <DrawTrSurf_Polygon3D.hxx>
+#include <DrawTrSurf_Triangulation.hxx>
+#include <GeometryTest.hxx>
+#include <Poly.hxx>
 #include <Poly_Array1OfTriangle.hxx>
+#include <Poly_Polygon2D.hxx>
+#include <Poly_Polygon3D.hxx>
 #include <Poly_Triangle.hxx>
+#include <Poly_Triangulation.hxx>
 #include <TColgp_Array1OfPnt.hxx>
 #include <TColgp_Array1OfPnt2d.hxx>
-#include <DrawTrSurf.hxx>
-#include <Draw_Appli.hxx>
-#include <DrawTrSurf_Triangulation.hxx>
-#include <DrawTrSurf_Polygon3D.hxx>
-#include <DrawTrSurf_Polygon2D.hxx>
 
-#include <Poly_Polygon3D.hxx>
-#include <Poly_Polygon2D.hxx>
-
-#ifdef WNT
+#ifdef _WIN32
 Standard_IMPORT Draw_Viewer dout;
 #endif
 //=======================================================================
@@ -48,8 +43,8 @@ static Standard_Integer polytr(Draw_Interpretor& di, Standard_Integer n, const c
   if (n < 4)
     return 1;
 
-  Standard_Integer nbNodes = atoi(a[2]);
-  Standard_Integer nbTri   = atoi(a[3]);
+  Standard_Integer nbNodes = Draw::Atoi(a[2]);
+  Standard_Integer nbTri   = Draw::Atoi(a[3]);
 
   // read the nodes
   Standard_Integer i, j = 4;
@@ -60,7 +55,7 @@ static Standard_Integer polytr(Draw_Interpretor& di, Standard_Integer n, const c
       di << "Not enough nodes";
       return 1;
     }
-    Nodes(i).SetCoord(atof(a[j]),atof(a[j+1]),atof(a[j+2]));
+    Nodes(i).SetCoord(Draw::Atof(a[j]),Draw::Atof(a[j+1]),Draw::Atof(a[j+2]));
     j += 3;
   }
 
@@ -72,7 +67,7 @@ static Standard_Integer polytr(Draw_Interpretor& di, Standard_Integer n, const c
       di << "Not enough triangles";
       return 1;
     }
-    Triangles(i).Set(atoi(a[j]),atoi(a[j+1]),atoi(a[j+2]));
+    Triangles(i).Set(Draw::Atoi(a[j]),Draw::Atoi(a[j+1]),Draw::Atoi(a[j+2]));
     j += 3;
   }
 
@@ -94,7 +89,7 @@ static Standard_Integer polygon3d(Draw_Interpretor& di, Standard_Integer n, cons
   if (n < 4)
     return 1;
 
-  Standard_Integer nbNodes = atoi(a[2]);
+  Standard_Integer nbNodes = Draw::Atoi(a[2]);
 
   // read the nodes
   Standard_Integer i, j = 3;
@@ -105,7 +100,7 @@ static Standard_Integer polygon3d(Draw_Interpretor& di, Standard_Integer n, cons
       di << "Not enough nodes";
       return 1;
     }
-    Nodes(i).SetCoord(atof(a[j]),atof(a[j+1]),atof(a[j+2]));
+    Nodes(i).SetCoord(Draw::Atof(a[j]),Draw::Atof(a[j+1]),Draw::Atof(a[j+2]));
     j += 3;
   }
 
@@ -126,7 +121,7 @@ static Standard_Integer polygon2d(Draw_Interpretor& di, Standard_Integer n, cons
   if (n < 4)
     return 1;
 
-  Standard_Integer nbNodes = atoi(a[2]);
+  Standard_Integer nbNodes = Draw::Atoi(a[2]);
 
   // read the nodes
   Standard_Integer i, j = 3;
@@ -137,7 +132,7 @@ static Standard_Integer polygon2d(Draw_Interpretor& di, Standard_Integer n, cons
       di << "Not enough nodes";
       return 1;
     }
-    Nodes(i).SetCoord(atof(a[j]),atof(a[j+1]));
+    Nodes(i).SetCoord(Draw::Atof(a[j]),Draw::Atof(a[j+1]));
     j += 2;
   }
 
@@ -189,6 +184,108 @@ static Standard_Integer shtriangles(Draw_Interpretor& , Standard_Integer n, cons
   return 0;//wnt
 }
 
+//=======================================================================
+//function : AddNode
+//purpose  : 
+//=======================================================================
+template <typename Poly, typename Point, typename PointArr>
+static inline void AddNode(const Handle(Poly)& thePolygon,
+                    const Point& thePnt,
+                    PointArr& theNodes)
+{
+  for (Standard_Integer i = thePolygon->Nodes().Lower();
+       i <= thePolygon->Nodes().Upper(); i++)
+  {
+    theNodes[i] = thePolygon->Nodes()[i];
+  }
+
+  theNodes.ChangeLast() = thePnt;
+}
+
+//=======================================================================
+//function : AddNode
+//purpose  : 
+//=======================================================================
+static Standard_Integer AddNode(Draw_Interpretor& theDI,
+                                Standard_Integer theNArg,
+                                const char** theArgVal)
+{
+  if (theNArg < 4)
+  {
+    theDI << "Not enough arguments\n";
+    return 1;
+  }
+
+  if (theNArg == 4)
+  {
+    Handle(Poly_Polygon2D) aPoly2d = DrawTrSurf::GetPolygon2D(theArgVal[1]);
+    TColgp_Array1OfPnt2d aNodes(aPoly2d->Nodes().Lower(),
+                                aPoly2d->Nodes().Upper() + 1);
+    AddNode(aPoly2d, gp_Pnt2d(Draw::Atof(theArgVal[2]),
+                              Draw::Atof(theArgVal[3])), aNodes);
+    aPoly2d.Nullify();
+    aPoly2d = new Poly_Polygon2D(aNodes);
+    DrawTrSurf::Set(theArgVal[1], aPoly2d);
+  }
+  else
+  {
+    Handle(Poly_Polygon3D) aPoly3d = DrawTrSurf::GetPolygon3D(theArgVal[1]);
+    TColgp_Array1OfPnt aNodes(aPoly3d->Nodes().Lower(),
+                                aPoly3d->Nodes().Upper() + 1);
+    AddNode(aPoly3d, gp_Pnt(Draw::Atof(theArgVal[2]),
+                            Draw::Atof(theArgVal[3]),
+                            Draw::Atof(theArgVal[4])), aNodes);
+    aPoly3d.Nullify();
+    aPoly3d = new Poly_Polygon3D(aNodes);
+    DrawTrSurf::Set(theArgVal[1], aPoly3d);
+  }
+
+  return 0;
+}
+
+//=======================================================================
+//function : PolygonProps
+//purpose  : 
+//=======================================================================
+static Standard_Integer PolygonProps(Draw_Interpretor& theDI,
+                                     Standard_Integer theNArg,
+                                     const char** theArgVal)
+{
+  if (theNArg < 2)
+  {
+    theDI << "Use: polygonprops polygon2d [-area val] [-perimeter val]\n";
+    return 1;
+  }
+
+  Handle(Poly_Polygon2D) aPoly2d = DrawTrSurf::GetPolygon2D(theArgVal[1]);
+
+  Standard_Real anArea = 0.0, aPerimeter = 0.0;
+  Poly::PolygonProperties(aPoly2d->Nodes(), anArea, aPerimeter);
+
+  theDI << "Area      = " << anArea << "\n";
+  theDI << "Perimeter = " << aPerimeter << "\n";
+
+  for (Standard_Integer i = 2; i < theNArg; i++)
+  {
+    if (!strcmp(theArgVal[i], "-area"))
+    {
+      Draw::Set(theArgVal[++i], anArea);
+      continue;
+    }
+
+    if (!strcmp(theArgVal[i], "-perimeter"))
+    {
+      Draw::Set(theArgVal[++i], aPerimeter);
+      continue;
+    }
+
+    theDI << "Error: Wrong option: \"" << theArgVal[i] << "\"\n";
+    break;
+  }
+
+  return 0;
+}
+
 //=======================================================================
 //function : PolyCommands
 //purpose  : 
@@ -202,6 +299,10 @@ void GeometryTest::PolyCommands(Draw_Interpretor& theCommands)
   theCommands.Add("polytr","polytr name nbnodes nbtri x1 y1 z1 ... n1 n2 n3 ...",__FILE__,polytr,g);
   theCommands.Add("polygon3d","polygon3d name nbnodes x1 y1 z1  ...",__FILE__,polygon3d,g);
   theCommands.Add("polygon2d","polygon2d name nbnodes x1 y1  ...",__FILE__,polygon2d,g);
+  theCommands.Add("addpolygonnode","addpolygonnode polygon3d(2d) x y [z]",__FILE__, AddNode,g);
+  theCommands.Add("polygonprops","Computes area and perimeter of 2D-polygon. "
+                  "Run \"polygonprops\" w/o any arguments to read help.\n",
+                  __FILE__, PolygonProps,g);
   theCommands.Add("shnodes","shnodes name", __FILE__,shnodes, g);
   theCommands.Add("shtriangles","shtriangles name", __FILE__,shtriangles, g);
 }