00001
00002
00003 import FreeCAD, os, sys, unittest, Mesh
00004 import thread, time, tempfile
00005
00006
00007
00008
00009
00010
00011
00012 class MeshTopoTestCases(unittest.TestCase):
00013 def setUp(self):
00014
00015 self.planarMesh = []
00016 for x in range(3):
00017 for y in range(3):
00018 self.planarMesh.append( [0.0 + x, 0.0 + y,0.0000] )
00019 self.planarMesh.append( [1.0 + x, 1.0 + y,0.0000] )
00020 self.planarMesh.append( [0.0 + x, 1.0 + y,0.0000] )
00021 self.planarMesh.append( [0.0 + x, 0.0 + y,0.0000] )
00022 self.planarMesh.append( [1.0 + x, 0.0 + y,0.0000] )
00023 self.planarMesh.append( [1.0 + x, 1.0 + y,0.0000] )
00024
00025
00026 def testCollapseFacetsSingle(self):
00027 for i in range(18):
00028 planarMeshObject = Mesh.Mesh(self.planarMesh)
00029 planarMeshObject.collapseFacets([i])
00030
00031 def testCollapseFacetsMultible(self):
00032 planarMeshObject = Mesh.Mesh(self.planarMesh)
00033 planarMeshObject.collapseFacets(range(7))
00034
00035 def testCollapseFacetsAll(self):
00036 planarMeshObject = Mesh.Mesh(self.planarMesh)
00037 planarMeshObject.collapseFacets(range(18))
00038
00039
00040 class MeshGeoTestCases(unittest.TestCase):
00041 def setUp(self):
00042
00043 self.planarMesh = []
00044
00045
00046 def testIntersection(self):
00047 self.planarMesh.append( [0.9961,1.5413,4.3943] )
00048 self.planarMesh.append( [9.4796,10.024,-3.0937] )
00049 self.planarMesh.append( [1.4308,11.3841,2.6829] )
00050 self.planarMesh.append( [2.6493,2.2536,3.0679] )
00051 self.planarMesh.append( [13.1126,0.4857,-4.4417] )
00052 self.planarMesh.append( [10.2410,8.9040,-3.5002] )
00053 planarMeshObject = Mesh.Mesh(self.planarMesh)
00054 f1 = planarMeshObject.Facets[0]
00055 f2 = planarMeshObject.Facets[1]
00056 res=f1.intersect(f2)
00057 self.failUnless(len(res) == 0)
00058
00059
00060 def testIntersection2(self):
00061 self.planarMesh.append( [-16.097176,-29.891157,15.987688] )
00062 self.planarMesh.append( [-16.176304,-29.859991,15.947966] )
00063 self.planarMesh.append( [-16.071451,-29.900553,15.912505] )
00064 self.planarMesh.append( [-16.092241,-29.893408,16.020439] )
00065 self.planarMesh.append( [-16.007210,-29.926180,15.967641] )
00066 self.planarMesh.append( [-16.064457,-29.904951,16.090832] )
00067 planarMeshObject = Mesh.Mesh(self.planarMesh)
00068 f1 = planarMeshObject.Facets[0]
00069 f2 = planarMeshObject.Facets[1]
00070
00071 res=f1.intersect(f2)
00072 self.failUnless(len(res) == 0)
00073
00074 class PivyTestCases(unittest.TestCase):
00075 def setUp(self):
00076
00077 self.planarMesh = []
00078 FreeCAD.newDocument("MeshTest")
00079
00080 def testRayPick(self):
00081 if not FreeCAD.GuiUp:
00082 return
00083 self.planarMesh.append( [-16.097176,-29.891157,15.987688] )
00084 self.planarMesh.append( [-16.176304,-29.859991,15.947966] )
00085 self.planarMesh.append( [-16.071451,-29.900553,15.912505] )
00086 self.planarMesh.append( [-16.092241,-29.893408,16.020439] )
00087 self.planarMesh.append( [-16.007210,-29.926180,15.967641] )
00088 self.planarMesh.append( [-16.064457,-29.904951,16.090832] )
00089 planarMeshObject = Mesh.Mesh(self.planarMesh)
00090
00091 from pivy import coin, sogui; import FreeCADGui
00092 if not sys.modules.has_key("pivy.gui.soqt"): from pivy.gui import soqt
00093 Mesh.show(planarMeshObject)
00094 view=FreeCADGui.ActiveDocument.ActiveView.getViewer()
00095 rp=coin.SoRayPickAction(view.getViewportRegion())
00096 rp.setRay(coin.SbVec3f(-16.05,16.0,16.0),coin.SbVec3f(0,-1,0))
00097 rp.apply(view.getSceneManager().getSceneGraph())
00098 pp=rp.getPickedPoint()
00099 self.failUnless(pp != None)
00100 det=pp.getDetail()
00101 self.failUnless(det.getTypeId() == coin.SoFaceDetail.getClassTypeId())
00102 det=coin.cast(det,str(det.getTypeId().getName()))
00103 self.failUnless(det.getFaceIndex() == 1)
00104
00105 def testPrimitiveCount(self):
00106 if not FreeCAD.GuiUp:
00107 return
00108 self.planarMesh.append( [-16.097176,-29.891157,15.987688] )
00109 self.planarMesh.append( [-16.176304,-29.859991,15.947966] )
00110 self.planarMesh.append( [-16.071451,-29.900553,15.912505] )
00111 self.planarMesh.append( [-16.092241,-29.893408,16.020439] )
00112 self.planarMesh.append( [-16.007210,-29.926180,15.967641] )
00113 self.planarMesh.append( [-16.064457,-29.904951,16.090832] )
00114 planarMeshObject = Mesh.Mesh(self.planarMesh)
00115
00116 from pivy import coin, sogui; import FreeCADGui
00117 if not sys.modules.has_key("pivy.gui.soqt"): from pivy.gui import soqt
00118 Mesh.show(planarMeshObject)
00119 view=FreeCADGui.ActiveDocument.ActiveView.getViewer()
00120 pc=coin.SoGetPrimitiveCountAction()
00121 pc.apply(view.getSceneGraph())
00122 self.failUnless(pc.getTriangleCount() == 2)
00123
00124
00125 def tearDown(self):
00126
00127 FreeCAD.closeDocument("MeshTest")
00128
00129
00130
00131 def loadFile(name):
00132
00133 mesh=Mesh.Mesh()
00134 FreeCAD.Console.PrintMessage("Create mesh instance\n")
00135
00136 mesh.read(name)
00137 FreeCAD.Console.PrintMessage("Mesh loaded successfully.\n")
00138
00139 def createMesh(r,s):
00140 FreeCAD.Console.PrintMessage("Create sphere (%s,%s)...\n"%(r,s))
00141 mesh=Mesh.createSphere(r,s)
00142 FreeCAD.Console.PrintMessage("... destroy sphere\n")
00143
00144 class LoadMeshInThreadsCases(unittest.TestCase):
00145
00146 def setUp(self):
00147 pass
00148
00149 def testSphereMesh(self):
00150 for i in range(6,8):
00151 thread.start_new(createMesh,(10.0,(i+1)*20))
00152 time.sleep(10)
00153
00154 def testLoadMesh(self):
00155 mesh=Mesh.createSphere(10.0,100)
00156 name=tempfile.gettempdir() + os.sep + "mesh.stl"
00157 mesh.write(name)
00158 FreeCAD.Console.PrintMessage("Write mesh to %s\n"%(name))
00159
00160 for i in range(2):
00161 thread.start_new(loadFile,(name,))
00162 time.sleep(1)
00163
00164 def tearDown(self):
00165 pass