28 |
return numarray.array([x[1] * y[2] - x[2] * y[1], x[2] * y[0] - x[0] * y[2], x[0] * y[1] - x[1] * y[0]]) |
return numarray.array([x[1] * y[2] - x[2] * y[1], x[2] * y[0] - x[0] * y[2], x[0] * y[1] - x[1] * y[0]]) |
29 |
|
|
30 |
|
|
31 |
class Test_PyCAD(unittest.TestCase): |
class Test_PyCAD_Transformations(unittest.TestCase): |
32 |
ABS_TOL=1.e-8 |
ABS_TOL=1.e-8 |
33 |
def __distance(self,x,y): |
def __distance(self,x,y): |
34 |
return math.sqrt(numarray.dot(x-y,x-y)) |
return math.sqrt(numarray.dot(x-y,x-y)) |
593 |
s3=t([1,1,1]) |
s3=t([1,1,1]) |
594 |
self.failUnless(isinstance(s3,numarray.NumArray),"s3 is not a numarray object.") |
self.failUnless(isinstance(s3,numarray.NumArray),"s3 is not a numarray object.") |
595 |
self.failUnless(self.__distance(s3,numarray.array([1.,1,1.]))<self.ABS_TOL,"s3 is wrong.") |
self.failUnless(self.__distance(s3,numarray.array([1.,1,1.]))<self.ABS_TOL,"s3 is wrong.") |
596 |
|
|
597 |
|
class Test_PyCAD_Primitives(unittest.TestCase): |
598 |
|
def setUp(self): |
599 |
|
resetGlobalPrimitiveIdCounter() |
600 |
|
|
601 |
|
def test_baseclass(self): |
602 |
|
p=Primitive() |
603 |
|
|
604 |
|
id=p.getID() |
605 |
|
self.failUnless(isinstance(id,int),"id number is not an integer") |
606 |
|
self.failUnless(not id==Primitive().getID(),"id number is not unique") |
607 |
|
|
608 |
|
self.failUnless(not p.isPoint(),"generic primitive is not a point.") |
609 |
|
self.failUnless(not p.isCurve(),"generic primitive is not a curve.") |
610 |
|
self.failUnless(not p.isCurveLoop(),"generic primitive is not a curve loop.") |
611 |
|
self.failUnless(not p.isSurface(),"generic primitive is not a surface.") |
612 |
|
self.failUnless(not p.isSurfaceLoop(),"generic primitive is not a surface loop.") |
613 |
|
|
614 |
|
hs=p.getHistory() |
615 |
|
self.failUnless(isinstance(hs,set),"history must be a set") |
616 |
|
self.failUnless(len(hs)==0,"history should be empty.") |
617 |
|
|
618 |
|
ps=p.getPoints() |
619 |
|
self.failUnless(isinstance(ps,set),"point set must be a set") |
620 |
|
self.failUnless(len(ps)==0,"point set should be empty.") |
621 |
|
|
622 |
|
p.setLocalScale(1.23) |
623 |
|
|
624 |
|
def test_point(self): |
625 |
|
p=Point(1.,2.,3.,local_scale=9.) |
626 |
|
|
627 |
|
id=p.getID() |
628 |
|
self.failUnless(isinstance(id,int),"id number is not an integer") |
629 |
|
self.failUnless(not id==Primitive().getID(),"id number is not unique") |
630 |
|
|
631 |
|
# check history: |
632 |
|
hs=p.getHistory() |
633 |
|
self.failUnless(isinstance(hs,set),"history must be a set") |
634 |
|
self.failUnless(len(hs)==1,"history must have length 1.") |
635 |
|
self.failUnless(p in hs,"history must contain point p") |
636 |
|
|
637 |
|
# check incolved points: |
638 |
|
ps=p.getPoints() |
639 |
|
self.failUnless(isinstance(ps,set),"point set must be a set") |
640 |
|
self.failUnless(len(ps)==1,"point set must have length 1.") |
641 |
|
self.failUnless(p in ps,"point set must contain point p") |
642 |
|
|
643 |
|
# check coordinates: |
644 |
|
c=p.getCoordinates() |
645 |
|
self.failUnless(isinstance(c,numarray.NumArray),"coordinates are not a numarray object.") |
646 |
|
self.failUnless(c[0]==1.,"x coordinate is not 1.") |
647 |
|
self.failUnless(c[1]==2.,"y coordinate is not 2.") |
648 |
|
self.failUnless(c[2]==3.,"z coordinate is not 3.") |
649 |
|
|
650 |
|
# reset coordinates: |
651 |
|
p.setCoordinates([-1.,-2.,-3.]) |
652 |
|
c=p.getCoordinates() |
653 |
|
self.failUnless(isinstance(c,numarray.NumArray),"new coordinates are not a numarray object.") |
654 |
|
self.failUnless(c[0]==-1.,"new x coordinate is not -1.") |
655 |
|
self.failUnless(c[1]==-2.,"new y coordinate is not -2.") |
656 |
|
self.failUnless(c[2]==-3.,"new z coordinate is not -3.") |
657 |
|
|
658 |
|
# check for a colocated point: |
659 |
|
self.failUnless(p.isColocated(Point(-1.,-2.,-3.)),"colocation not detected.") |
660 |
|
self.failUnless(p.isColocated(numarray.array([-1.,-2.,-3.])),"colocation with numarray representation not detected.") |
661 |
|
self.failUnless(not p.isColocated(numarray.array([1.,-2.,-3.])),"false colocation detected.") |
662 |
|
self.failUnless(not p.isColocated(numarray.array([0.,0.,0.])),"false colocation with origin detected.") |
663 |
|
|
664 |
|
# check for local length scale |
665 |
|
l=p.getLocalScale() |
666 |
|
self.failUnless(l==9.,"refinement scale is not 9.") |
667 |
|
|
668 |
|
# check for new local length scale |
669 |
|
p.setLocalScale(3.) |
670 |
|
l=p.getLocalScale() |
671 |
|
self.failUnless(l==3.,"new refinement scale is not 3.") |
672 |
|
|
673 |
|
# negative value shouldn't work. |
674 |
|
self.failUnlessRaises(ValueError,p.setLocalScale,-3.) |
675 |
|
|
676 |
|
# copy: |
677 |
|
an_other_p=p.copy() |
678 |
|
self.failUnless(isinstance(an_other_p ,Point),"copy is not a point") |
679 |
|
self.failUnless(not an_other_p.getID() == p.getID(),"copy has same Id") |
680 |
|
self.failUnless(p.isColocated(an_other_p),"p is not colocated with its copy.") |
681 |
|
self.failUnless(an_other_p.isColocated(p),"the copy is not colocated with p.") |
682 |
|
self.failUnless(an_other_p.getLocalScale()==3.,"copy has wrong local scale.") |
683 |
|
|
684 |
|
# modify by Transformation: |
685 |
|
p.modifyBy(Dilation(-1)) |
686 |
|
self.failUnless(p.isColocated(Point(1.,2.,3.)),"in-place transformation failed") |
687 |
|
|
688 |
|
# apply Transformation: |
689 |
|
dil_p=p.apply(Dilation(4)) |
690 |
|
self.failUnless(dil_p.isColocated(Point(4.,8.,12.)),"applying transformation failed") |
691 |
|
self.failUnless(not dil_p.getID() == p.getID(),"transformed point has same Id") |
692 |
|
self.failUnless(dil_p.getLocalScale()==3.,"transformed point has wrong local scale.") |
693 |
|
|
694 |
|
# overloaded add: |
695 |
|
shift_p=p+[1,1,1] |
696 |
|
self.failUnless(shift_p.isColocated(Point(2,3.,4)),"applying shift by list failed") |
697 |
|
self.failUnless(not shift_p.getID() == p.getID(),"shift by list has same Id") |
698 |
|
self.failUnless(shift_p.getLocalScale()==3.,"shift by list has wrong local scale.") |
699 |
|
|
700 |
|
shift_p=p+numarray.array([1,1,1]) |
701 |
|
self.failUnless(shift_p.isColocated(Point(2,3.,4)),"applying shift by numarray failed") |
702 |
|
self.failUnless(not shift_p.getID() == p.getID(),"shift by numarray has same Id") |
703 |
|
self.failUnless(shift_p.getLocalScale()==3.,"shift by numarray has wrong local scale.") |
704 |
|
# overloaded minus |
705 |
|
shift_p=p-[1,1,1] |
706 |
|
self.failUnless(shift_p.isColocated(Point(0,1,2.)),"applying shift by -list failed") |
707 |
|
self.failUnless(not shift_p.getID() == p.getID(),"shift by -list has same Id") |
708 |
|
self.failUnless(shift_p.getLocalScale()==3.,"shift by -list has wrong local scale.") |
709 |
|
|
710 |
|
shift_p=p-numarray.array([1,1,1]) |
711 |
|
self.failUnless(shift_p.isColocated(Point(0,1,2.)),"applying shift by -numarray failed") |
712 |
|
self.failUnless(not shift_p.getID() == p.getID(),"shift by -numarray has same Id") |
713 |
|
self.failUnless(shift_p.getLocalScale()==3.,"shift by -numarray has wrong local scale.") |
714 |
|
# overloaded inplace add: |
715 |
|
p+=[1,1,1] |
716 |
|
self.failUnless(p.isColocated(Point(2,3.,4)),"modification by list shift failed") |
717 |
|
|
718 |
|
p+=numarray.array([1,1,1]) |
719 |
|
self.failUnless(p.isColocated(Point(3,4,5)),"modification by numarray shift failed") |
720 |
|
|
721 |
|
# overloaded inplace add: |
722 |
|
p-=[1,1,1] |
723 |
|
self.failUnless(p.isColocated(Point(2,3,4)),"modification by -list shift failed") |
724 |
|
|
725 |
|
p-=numarray.array([1,1,1]) |
726 |
|
self.failUnless(p.isColocated(Point(1,2.,3)),"modification by -numarray shift failed") |
727 |
|
|
728 |
|
#overloaded multiplication: |
729 |
|
mult_p=2*p |
730 |
|
self.failUnless(mult_p.isColocated(Point(2,4,6)),"applying int factor failed") |
731 |
|
self.failUnless(not mult_p.getID() == p.getID(),"shift by int factor has same Id") |
732 |
|
self.failUnless(mult_p.getLocalScale()==3.,"shift by int factor has wrong local scale.") |
733 |
|
|
734 |
|
mult_p=2.*p |
735 |
|
self.failUnless(mult_p.isColocated(Point(2,4,6)),"applying float factor failed") |
736 |
|
self.failUnless(not mult_p.getID() == p.getID(),"shift by float factor has same Id") |
737 |
|
self.failUnless(mult_p.getLocalScale()==3.,"shift by float factor has wrong local scale.") |
738 |
|
|
739 |
|
mult_p=Dilation(2)*p |
740 |
|
self.failUnless(mult_p.isColocated(Point(2,4,6)),"applying Dilation factor failed") |
741 |
|
self.failUnless(not mult_p.getID() == p.getID(),"shift by Dilation factor has same Id") |
742 |
|
self.failUnless(mult_p.getLocalScale()==3.,"shift by Dilation factor has wrong local scale.") |
743 |
|
|
744 |
|
#overloaded inplace multiplication: |
745 |
|
p*=2 |
746 |
|
self.failUnless(p.isColocated(Point(2,4,6)),"applying in-place int factor failed") |
747 |
|
|
748 |
|
p*=2. |
749 |
|
self.failUnless(p.isColocated(Point(4,8,12)),"applying in-place float factor failed") |
750 |
|
|
751 |
|
p*=Dilation(2) |
752 |
|
self.failUnless(p.isColocated(Point(8,16,24)),"applying in-place Dilation factor failed") |
753 |
|
|
754 |
|
# get gmsh code |
755 |
|
code=p.getGmshCommand(2.) |
756 |
|
self.failUnless("Point(1) = {8.0 , 16.0, 24.0 , 6.0 };"== code, "wrong gmsh code") |
757 |
|
|
758 |
|
|
759 |
if __name__ == '__main__': |
if __name__ == '__main__': |
760 |
suite = unittest.TestSuite() |
suite = unittest.TestSuite() |
761 |
suite.addTest(unittest.makeSuite(Test_PyCAD)) |
suite.addTest(unittest.makeSuite(Test_PyCAD_Transformations)) |
762 |
|
suite.addTest(unittest.makeSuite(Test_PyCAD_Primitives)) |
763 |
s=unittest.TextTestRunner(verbosity=2).run(suite) |
s=unittest.TextTestRunner(verbosity=2).run(suite) |
764 |
if s.wasSuccessful(): |
if s.wasSuccessful(): |
765 |
sys.exit(0) |
sys.exit(0) |