Ejemplo n.º 1
0
def testConstructeurs(filename, show=False):
    debug(titre='testConstructeurs %s' % filename.name)
    p = NSplineSimple()
    debug(paragraphe='1. constructeur vide')
    debug(p=p)
    p = NSplineSimple(cpoints=np.zeros((1, 2)),
                      parent=None,
                      methode=('cubic', 'periodic'),
                      mode='rayon',
                      name='SPLINE1')
    debug(paragraphe='2. constructeur presque vide')
    print p
    # for point in p.qcpolygon :
    #     print point
    # for point in p.qdpolygon :
    #     print point
    debug(paragraphe='3. p vide => p.cpoints=points de %s' % filename.name)
    points = pointsFromFile(filename)
    p.cpoints = points
    if show: p.plot(titre='cpoints=pointsFromFile()')

    S0 = NSplineSimple(
        points=pointsFromFile(filename),
        methode=('cubic', 'periodic'),
        #                        mode='courbure',
        name=filename.name)
    debug(S0)
    if show: S0.plot(titre=filename.name)

    debug(paragraphe='4. S = NSplineSimple(**dump00) (points:%s)' %
          filename.name)
    dump00 = S0.toDump()
    debug("dump00")
    pprint(dump00)
    S = NSplineSimple(**dump00)
    dump01 = S.toDump()
    pprint(dump01)
    debug("dump00!=dump01 : %s" % dictsAreNotEqual(dump00, dump01))

    debug(paragraphe='4. S1 = S0.copy() (points:%s)' % filename.name)
    S = S0.copy()
    dump02 = S.toDump()
    debug("dump00!=dump02 : %s" % dictsAreNotEqual(dump00, dump02))
    filename = Path(RUNS_DIR, 'spline.npkl')
    S0.save(filename=filename)
    S0.open(filename)
    #     debug('S0.toDump()')
    #     pprint(S0.toDump())
    debug(S0)
    debug('S0.toDump()!=dump00', dictsAreNotEqual(S0.toDump(), dump02))
    if show: S0.plot(titre='open pkl')
    S1 = S0.copy()
    d0, d1 = dump00, S1.toDump()
    debug('d0!=d1', dictsAreNotEqual(d0, d1))
    if show: S1.plot(titre='copy')
    debug(titre='Fin testConstructeurs %s' % filename.name)
Ejemplo n.º 2
0
def testEchantillonner(filename, show=False):
    name = filename.name
    debug(titre='testEchantillonner : %s' % name)
    P = Profil(
        points=pointsFromFile(filename),
        #                       methode=('cubic',((2, 0, 0), (1, 0, -5))),
        #                       mode=['linear'],
        nbpd=[1000, 1000])
    P.normalise()
    P.elaguer(2, True)
    P.pouverture = pam, pav = -10, -40  #en % de corde
    debug(P)
    touv = P.touverture  #= (kam, tam), (kav,tav)
    debug(touv=touv)
    debug(ouverture=P.ouverture)

    debug('P._getT(%.f%%)=%f' % (pam, P._getT(pam)))
    debug('P._getT(%.1f%%)=%f' % (pav, P._getT(pav)))
    #     P.echantillonner()
    if show:
        P.plot(titre='echantillonage : pouv=%s, touv=%s' %
               (str(P.pouverture), str(P.touverture)))
    P.hardScale(2, centre=array([0, 0]))
    P.hardRotate(180, centre=(0, 0))
    #     debug(P)
    touv = P.touverture  #= (kam, tam), (kav,tav)
    debug(touv=touv)  #,P_t=sint(t))
    debug(ouverture=P.ouverture)
    debug('P._getT(%.1f%%)=%f' % (pam, P._getT(pam)))
    debug('P._getT(%.1f%%)=%f' % (pav, P._getT(pav)))
    if show:
        P.plot(titre='echantillonage : rotation 180'
               )  #%(str(P.pouverture),str(P.touverture)))
    debug(titre='Fin testEchantillonner')
Ejemplo n.º 3
0
def testElaguer(filename, show=False):
    name = filename.name
    debug(titre='testElaguer : %s' % name)
    p = Profil(points=pointsFromFile(filename), precision=[1000, 1000])
    debug(rba=p.rba)
    debug("Extrados : Se'(1.0)=%s" % (p.splines[0](1.0, 1)))
    debug("Intrados : Si'(0.0)=%s" % (p.splines[1](0.0, 1)))
    sin0 = p.splines[0].integraleCourbure(0.01, 1, 1000)
    sin1 = p.splines[1].integraleCourbure(0.01, 1, 1000)
    debug('sinuosite totale avant elagage=', (sin0, sin1))
    #     return
    p.elaguer(eps=1, replace=True)
    debug(rba=p.rba)
    sin10 = p.splines[0].integraleCourbure(0.01, 1, 1000)
    sin11 = p.splines[1].integraleCourbure(0.01, 1, 1000)
    #     sin10 = p.splines[0].integraleCourbure(0.01,1,1000)
    debug('sinuosite totale apres elagage=', (sin10, sin11))
    debug(variation_relative_sinuosite=((sin10 - sin0) / sin0,
                                        (sin11 - sin1) / sin1))
    #     debug('sinuosite totale =',p.splines[1].integraleCourbure(0.01,1,1000))
    if show:
        p.plot(titre='elagage')
        p.plotCourbure()
        plt.show()
    debug(titre='Fin testElaguer')
Ejemplo n.º 4
0
def testModifGlobales(filename, show=True):
    debug(titre="testModifGlobales : %s" % filename.name)
    #     numfig = 0
    #     plt.figure(numfig)
    S0 = NSplineSimple(points=pointsFromFile(filename),
                       methode=('cubic', ((2, 0, 0), (1, 0, -5))),
                       precision=1000,
                       mode='courbure',
                       name='SPLINE01')
    if show: S0.plot(titre='NSplineSimple(points=pointsFromFile(filename),...')
    #     numfig += 1
    #     plt.figure(numfig)
    S0.cpoints = pointsFromFile(filename)
    debug(initial=mesures(S0))
    if show: S0.plot(titre=filename.name + 'cpoints = pointsFromFile()')

    #     numfig += 1
    #     plt.figure(numfig)
    S0.symetriser(0)
    debug(symetriser_0=mesures(S0))
    if show: S0.plot(titre='symetriser(0)')

    #     numfig += 1
    #     plt.figure(numfig)
    S0.symetriser(1)
    debug(symetriser_1=mesures(S0))
    if show: S0.plot(titre='symetriser(1)')

    #     numfig += 1
    #     plt.figure(numfig)
    S0.hardRotate(30)  #en degres par defaut
    debug(hardRotate_30=mesures(S0))
    if show: S0.plot(titre='hardRotate(30)')

    #     numfig += 1
    #     plt.figure(numfig)
    S0.hardScale((2, 0.5))
    debug(hardScale_2_0point5=mesures(S0))
    if show: S0.plot(titre='hardScale((2,0.5))')

    #     numfig += 1
    #     plt.figure(numfig)
    S0.translate((2, 3))
    debug(translate_2_3=mesures(S0))
    if show: S0.plot(titre='translate((2,3))')
    #     if show : plt.show()
    debug(titre="Fin testModifGlobales : %s" % filename.name)
Ejemplo n.º 5
0
def testOuverture(filename, show=False):
    name = filename.name
    debug(titre='testOuverture : %s' % name)
    P = Profil(points=pointsFromFile(filename), name='Ouverture')
    if show: P.plot(titre='testOuverture:%s' % P.name)
    pouv = (-20, -50)
    P.pouverture = pouv
    if show: P.plot(titre='ouverture=%s' % (str(pouv)))
    debug(titre='Fin testOuverture')
Ejemplo n.º 6
0
def testDivers(filename, show=False):
    name = filename.name
    debug(titre='testDivers : %s' % name)
    if 'spl' in filename.ext:
        pass
    p = Profil(points=pointsFromFile(filename), precision=[1000, 1000])
    debug(rba=p.rba, corde=p.corde)
    p.normalise()
    debug(rba=p.rba, corde=p.corde)
    debug(titre='Fin testDivers')
Ejemplo n.º 7
0
def testMethodes(filename, show=True):
    debug(titre='testMethodes(%s)' % filename.name)
    k = 0
    for methode in (
        ('cubic', 'not-a-knot'),
        ('cubic', 'periodic'),
        ('cubic', 'natural'),
        ('cubic', ((1, 0, -5), (1, 0, -5))),
        ('cubic', ((2, 0, 0), (1, 0, -5))),  #extrados
        ('cubic', ((1, 0, -5), (2, 0, 0))),  #intrados
    ):
        k += 1
        S = NSplineSimple(points=pointsFromFile(filename),
                          methode=methode,
                          mode='courbure',
                          name='SPLINE%d' % (k + 1))
        debug(S)
        if show: S.plot(titre=filename.name + str(methode))
        if show: S.plotCourbure()


#     filename=Path(VALIDATION_DIR,'simple','simple2d2.gnu')
    for methode, mode in (
            #                     (('ius',1),'linear'),
            #                     (('ius',2),'linear'),
            #                     (('ius',3),'courbure'),
        (('ius', 4), 'courbure'),
        (('ius', 7), 'courbure'),
        (('us', 1), 'linear'),
        (('us', 2), 'linear'),
        (('us', 3), 'courbure'),
        (('us', 4), 'courbure'),
        (('us', 7), 'courbure'),
    ):
        S = NSplineSimple(points=pointsFromFile(filename),
                          methode=methode,
                          mode=mode)
        debug(S)

        if show and methode[1] <= 5:  #pour k=7 ca plante
            S.plot(titre=filename.name + str(methode))
    debug(titre='Fin testMethodes(%s)' % filename.name)
Ejemplo n.º 8
0
def testSaveAndOpen(filename, show=False):
    name = filename.name
    titre = 'testSaveAndOpen : %s' % name
    debug(titre=titre)
    if '.spl' in filename:
        par = "Ouverture fichier (.spl) %s" % name
        debug(paragraphe=par)
        #         with open(filename,'r') as f :
        #             dump = eval(f.read())
        #             lines = f.read()
        #         dump = eval(lines)
        S = Profil()
        try:
            S.open(filename)
            print S
        except IndexError as msg:
            rdebug('TODO:%s, %s' % (par, msg))
            return  #On peut pas continuer S est verolé
    else:
        debug(paragraphe="Ouverture fichier %s" % name)
        S = Profil(
            points=pointsFromFile(filename),
            #                methode=('cubic',((2, 0, 0), (1, 0, -5))),
            mode=['courbure', 'courbure'],
            precision=[3000, 3000])

    debug(paragraphe="S.normalise() %s" % name)
    debug(ouvertures=S.pouverture)
    S.normalise()
    debug(ouvertures=S.pouverture)
    debug(paragraphe="pickle.dump() et pickle.load() %s" % name)
    fname = Path(WORK_DIR, filename.namebase + 'Test.spl')
    debug(fname)
    with open(fname, 'w') as f:
        cPickle.dump(S.toDump(), f)
    with open(fname, 'r') as f:
        dump = cPickle.load(f)
        S1 = Profil(**dump)
    S1.elaguer(1, True)
    S1.rba = (-1, -1)
    if show:
        S1.plot(titre='rba=%s' % str(S1.rba))
        plt.show()
    dump = S.toDump()
    dump1 = S1.toDump()
    debug('dump==dump1 ?', dump == dump1)
    pprint(dump1)
    debug('S.rba   = %s\n'%str(S.rba)+\
          '    S1.rba  = %s\n'%str(S1.rba)+\
          '    S.erba  = %s\n'%str(S.erba)+\
          '    S1.erba = %s\n'%str(S1.erba)+\
          '    S.drba  = %s\n'%str(S.drba)+\
          '    S1.drba = %s'%str(S1.drba))
    debug(titre='Fin testSaveAndOpen')
Ejemplo n.º 9
0
def testSaveRead(filename, show=True):
    u"""Diverses manières de lire, construire, sauvegarder une NSplineSimple"""
    debug(titre="testSaveRead : %s" % filename.name)
    print "    ==> NSplineSimple::__init__()"
    S = NSplineSimple(points=pointsFromFile(filename),
                      methode=('cubic', ((2, 0, 0), (1, 0, -5))),
                      mode='courbure',
                      name='SPLINE01')
    S4 = NSplineSimple()
    S4.load(S.toDump())
    dump4 = S4.toDump()
    fname = Path(RUNS_DIR, filename.namebase + '#.pkl')
    print fname
    print "    ==> cPickle::dump()/load()"
    with open(fname, 'w') as f:
        cPickle.dump(S.toDump(), f)
    with open(fname, 'r') as f:
        dump = cPickle.load(f)
        S1 = NSplineSimple(**dump)


#     S1.plot(plt)
#     if show : plt.show()
    dump = S.toDump()
    dump1 = S1.toDump()
    print 'dump==dump1 ?', dump == dump1

    print "    ==> file::read()/write() "
    fname = Path(RUNS_DIR, filename.namebase + '#.spl')
    S2 = NSplineSimple(**dump)
    dump2 = S2.toDump()
    with open(fname, 'w') as f:
        f.write(str(S2.toDump()))
    with open(fname, 'r') as f:
        dump = eval(f.read())
    print "    ==> NSplineSimple::load()"
    S3 = NSplineSimple(**dump)
    dump3 = S3.toDump()
    #     dump3_1 = S3.toDump()
    print 'dump==dump1=dump2=dump3=dump4 ?', dump == dump1 == dump2 == dump3 == dump4
    debug(dump=dump)
    #     debug(dump1=dump1)

    #     debug(dump2=dump2)
    #     debug(dump3=dump3)
    #     debug(dump4=dump4)
    #     exit()
    debug(titre="Fin testSaveRead : %s" % filename.name)
Ejemplo n.º 10
0
def testEchantillonnage(filename, trados, show=True):
    u"""echantillonnage entre ta et tb. [ta,tb]=[0,1] => echantillonnage complet"""
    #     s = NSplineSimple(points=pointsFromFile(filename))
    u"""Extraction intrados et extrados, normalisation"""
    debug(titre="testEchantillonnage %s-trados : %s" % (trados, filename.name))

    points = pointsFromFile(filename)
    corde, nba = -1.0, np.nan
    bf = points[0]
    for k, point in enumerate(points):
        d = dist2(bf, point)
        if d > corde: corde, nba = d, k
    corde = math.sqrt(corde)
    debug(corde=corde, nba=nba)
    points *= (1000 / corde)  #en mm
    corde = 1000
    if trados == 'e':  #Extrados
        methode = ('cubic', ((2, 0, 0), (1, 0, -corde / 2)))  #extrados
        c0 = points[:1 + nba]
    elif trados == 'i':  #Intrados
        methode = ('cubic', ((1, 0, -corde / 4), (2, 0, 0)))  #intrados
        c0 = points[nba:]
    u"""Construction et élagage de la spline d'interpolation du trados"""
    precision = 1000
    #     T0 = linspace(0,1,precision)
    s0 = NSplineSimple(cpoints=c0,
                       precision=precision,
                       methode=methode,
                       mode='courbure')
    #     s0.elaguer(1, replace=True)
    ne, ta, tb = 30, 0.2, 0.8
    s0.echantillonner(nbp=ne, ta=ta, tb=tb, mode='courbure')
    debug(Te=s0.tech)
    E = s0.epoints
    if show:
        s0.plot(more=[
            (E[:, 0], E[:, 1], 'y^',
             'echantillon \nn=%d, %.2f=>%.2f' % (ne, ta, tb)),
        ])


#     plt.plot(E[:,0],E[:,1],'y^','echantillon')
#         plt.show()
    debug(titre="Fin testEchantillonnage %s-trados : %s" %
          (trados, filename.name))
Ejemplo n.º 11
0
def testElaguer(filename, trados, fonction='elaguer', show=True):
    name = filename.name
    debug(titre="testElaguer : %s" % name)
    if fonction == 'ajuster':
        msg = u"""L'élagage par "ajustement" ne marche pas, et il est très long.
        Je le supprime des tests.
        :TODO: voir au besoin si on peut l'améliorer"""
        debug(msg)
        return

    def compareCourbures(s0, s1, corde, trados):
        u"""s0 et s1 sont des intrados (ou des extrados), s0:original, s1:elagué
        On trace les deux splines et les log des abs(courbures), mises à l'échelle pour y voir qque chose"""
        s1.nbpe = s0.nbpe
        titre = ' '.join(('courbures', s0.name, s1.name))
        plt.title(titre)
        #     E = s0.epoints
        _, ax = plt.subplots()
        T = linspace(0, 1, 100)

        #         s0.echantillonner(nbpe)
        D0 = s0.dpoints
        C0 = s0.cpoints
        spline0, = ax.plot(D0[:, 0], D0[:, 1], 'r-', lw=1)
        #         echantillon, = ax.plot(E[:,0], E[:,1], 'bv', lw=1)
        control0, = ax.plot(C0[:, 0],
                            C0[:, 1],
                            'ro',
                            lw=1,
                            label=u'%d points contrôle' % len(C0))
        courbure0 = s0.courbure(T)
        courbure1 = s1.courbure(T)
        minc = min(min(courbure0), min(courbure1))
        maxc = max(max(abs(courbure0)), max(abs(courbure1)))
        courbure0 += (1.0 + abs(minc))
        courbure0 = log(courbure0)
        courbure0 /= maxc
        if trados == 'e':
            courbure0 = courbure0[::-1]
        courbure0, = ax.plot(corde * T[1:], corde * courbure0[1:])
        D1 = s1.dpoints
        C1 = s1.cpoints
        spline1, = ax.plot(D1[:, 0], D1[:, 1], 'b-', lw=1)
        control1, = ax.plot(C1[:, 0],
                            C1[:, 1],
                            'bo',
                            lw=1,
                            label=u'%d points contrôle' % len(C1))

        courbure1 += (1.0 + abs(minc))
        courbure1 = log(courbure1)
        courbure1 /= maxc
        if trados == 'e':
            courbure1 = courbure1[::-1]
        courbure1, = ax.plot(corde * T[1:], corde * courbure1[1:])
        ax.legend(loc='upper right')
        buttons = [
            'control0', 'control1', 'courbure0', 'courbure1', 'spline0',
            'spline1'
        ]
        values = [True, True, True, True, True, True]
        draws = [control0, control1, courbure0, courbure1, spline0, spline1]
        plt.subplots_adjust(left=0.2)
        plt.axis('equal')

        rax = plt.axes([0.05, 0.4, 0.1, 0.15])
        check = CheckButtons(rax, buttons, values)

        def func(label):
            if label == 'spline0':
                spline0.set_visible(not spline0.get_visible())
            elif label == 'spline1':
                spline1.set_visible(not spline1.get_visible())
            elif label == 'control0':
                control0.set_visible(not control0.get_visible())
            elif label == 'control1':
                control1.set_visible(not control1.get_visible())
            elif label == 'courbure0':
                courbure0.set_visible(not courbure0.get_visible())
            elif label == 'courbure1':
                courbure1.set_visible(not courbure1.get_visible())
            else:
                draw = draws[buttons.index(label)]
                draw.set_visible(not draw.get_visible())
            plt.draw()

        check.on_clicked(func)
        if show: plt.show()
        return plt

    def comparePointsSpline(P, s):
        u"""comparaison graphique, P famille de points s spline
        On trace les projections de P sur la spline s"""
        #         debug(s.projection(P))
        pjs = s(s.projectionObj(P)[0])
        if show:
            s.plot(more=([P[:, 0], P[:, 1], 'g.', 'C0'],
                         [pjs[:, 0], pjs[:, 1], 'y.', 'projections']))

    u"""Extraction intrados et extrados, normalisation"""
    points = pointsFromFile(filename)
    corde, nba = -1.0, np.nan
    bf = points[0]
    for k, point in enumerate(points):
        d = dist2(bf, point)
        if d > corde: corde, nba = d, k
    corde = math.sqrt(corde)
    debug(corde=corde, nba=nba)
    points *= (1000 / corde)  #en mm
    corde = 1000
    if trados == 'e':  #Extrados
        methode = ('cubic', ((2, 0, 0), (1, 0, -corde / 2)))  #extrados
        c0 = points[:1 + nba]
    elif trados == 'i':  #intrados
        methode = ('cubic', ((1, 0, -corde / 4), (2, 0, 0)))  #intrados
        c0 = points[nba:]
    u"""Construction de la spline d'interpolation du trados"""
    precision = 1000
    T0 = linspace(0, 1, precision)
    s0 = NSplineSimple(cpoints=c0,
                       precision=precision,
                       methode=methode,
                       mode='courbure')
    Tc = linspace(0, 1, 100)
    courb0 = s0.courbure(Tc)
    u"""La spline elaguée"""
    if fonction == 'elaguer':
        s1, d, (n0, n1) = s0.elaguer(1, debog=show)
    elif fonction == 'ajuster':
        s1, d, (n0, n1) = s0.ajuster(1)
    d0 = s0(T0)
    d1 = s1(T0)
    print u'  max dist(C0, s1)      = %.2e max des distances aux points de contrôle (‰) <  ################' % (
        d)
    print u'  max norme 2     D0-D1 = %.2e max des distances point à point des splines discrétisées' % max(
        norm(d0 - d1, 2, axis=1))
    print u'  norme frobenius D0-D1 = %.2e somme des distances point à point des splines discrétisées' % norm(
        d0 - d1, 'fro')
    print u'  norme frobenius D0-D1 = %.2e moyenne des distances point à point des splines discrétisées' % (
        norm(d0 - d1, 'fro') / len(d0))
    print u'  nb pts contrôle       : %d => %d' % (len(s0), len(s1))
    #     compareCourbures(s0, s1, corde, trados)
    comparePointsSpline(s0.cpoints, s1)
    debug(paragraphe='courbure Avant')
    print courb0.tolist()
    debug(paragraphe='courbure Apres')
    print s1.courbure(Tc).tolist()
    debug(titre="Fin testElaguer : %s" % filename.name)
Ejemplo n.º 12
0
def testSequenceBasique(filename, show=True):
    """Une séquence basique d'utilisation de spline"""
    debug(titre='testSequenceBasique %s' % filename.name)
    debug(show=show)
    #     exit()
    debug(paragraphe='0. NSplineSimple() ')
    S = NSplineSimple()
    debug(S=S)

    c, d, knots = S.cpoints, S.dpoints, S.knots
    print 'cpoints =', c, type(c), c.shape
    print 'dpoints =', d, type(d), d.shape
    print '  knots =', knots, type(knots), knots.shape
    debug(titre='differentes methodes %s' % filename.name)
    for k, methode in enumerate((
        ('us', 1),  #'linear'),
            #                                 ('us',2),#'linear'),
        ('us', 3),  #'courbure'),
            #                                 ('us',4),#'courbure'),
            #                                 ('us',7),#'courbure'),
        ('ius', 1),  #'courbure'),
        ('ius', 3),  #'courbure'),
            #                                 ('lsqus',1),#'linear'),
            #                                 ('lsqus',2),#'linear'),
            #                                 ('lsqus',3),#'courbure'),
            #                                 ('lsqus',4),#'courbure'),
        ('cubic', 'not-a-knot'),
        ('cubic', 'periodic'),
        ('cubic', 'natural'),
        ('cubic', ((1, 0, -5), (1, 0, -5))),
        ('cubic', ((2, 0, 0), (1, 0, -5))),  #extrados
        ('cubic', ((1, 0, -5), (2, 0, 0))),  #intrados
            #                                 ('ius',7),#'courbure'),
    )):
        debug(paragraphe='1. methode-%d=%s (fichier %s)' %
              (k, str(methode), filename.name))
        S = NSplineSimple(cpoints=pointsFromFile(filename),
                          methode=methode,
                          name=str(methode))
        debug(S)
        debug(u'    Normal, S.dpoint est calcule', lendpoints=len(S.dpoints))
        debug(paragraphe='2. S._update() methode-%d=%s  (fichier %s)' %
              (k, str(methode), filename.name))
        S._update()
        try:
            debug(S._dpoints)
        except AttributeError as msg:
            debug(u'    Normal pas de S._dpoints : %s' % str(msg))
        debug(u'    Normal, S.dpoint est calcule', lendpoints=len(S.dpoints))
        debug(S)
        if show:
            S.plot(show=True)
        debug('    Longueurs (c,d)=(%.5g,%.5g) ; nspline=%d' %
              (S.longueur('c'), S.longueur('d'), S.nbspline))

        i, p = len(S) / 2, S.centregravite
        debug(paragraphe='3. S[%d]=[%.3g, %.3g], methode-%d=%s  (fichier %s)' %
              (i, p[0], p[1], k, str(methode), filename.name))
        oldp = S[i].copy()
        S[i] = p
        debug(S[i], oldp=oldp)
        debug('    Longueurs (c,d)=(%.5g,%.5g) ; nspline=%d' %
              (S.longueur('c'), S.longueur('d'), S.nbspline))
        debug('    S(1)=%s' % S(1.0))  #ne donne pas tout a fait cpoints[-1]
        ac = absCurv(S._cpoints, normalise=True)
        print '    abscurv=', ac.shape, 'knots=', S.knots.shape, 'cpoints=', S._cpoints.shape
        print '    norm(S(ac)-S._cpoints)=', norm(S(ac) - S._cpoints)
        print '    norm(ac-S.knots)', norm(ac - S.knots)
        if show:
            S.plot(show=True,
                   titre='%s : modif S[%d]=[%.3g, %.3g]' %
                   (filename.name, i, p[0], p[1]))
        debug(S[i], oldp=oldp)
        S[i] = oldp
        debug(paragraphe='4. echantillonnage methode-%d=%s  (fichier %s)' %
              (k, str(methode), filename.name))
        S.nbpe = 120
        print 'mode=', S.mode, ', epoints=', S.epoints.shape
        e0, mode0 = S.epoints, S.mode
        print 'mode=', S.mode, ', epoints=', e0.shape
        print 'T = %s' % S.tech.tolist()
        S.mode = 'cos'  #efface tech et epoints
        e1, mode1 = S.epoints, S.mode
        print 'mode=', S.mode, ', epoints=', e0.shape
        print 'T = %s' % S.tech.tolist()
        #on regarde si ca se recalcule bien apres suppression epoints et tech
        del S._epoints, S._tech
        e2, mode2 = S.epoints, S.mode
        print 'mode=', S.mode, ', epoints=', e2.shape
        print 'T = %s' % S.tech.tolist()
        print 'epoints norme(e1-e2)=%.3g' % norm(e1 - e2)
        S.mode = 'x3'  #efface tech et epoints
        e2, mode2 = S.epoints, S.mode
        print 'mode=', S.mode, ', epoints=', e2.shape
        print 'T = %s' % S.tech.tolist()
        S.mode = 'courbure'  #efface tech et epoints
        mode3 = S.mode
        try:
            e3, mode3 = S.epoints, S.mode
            print 'mode=', S.mode, ', epoints=', e3.shape
            print 'T = %s' % S.tech.tolist()
            X3, Y3 = XY(e3)
        except ValueError as msg:  #normal
            debug('normal', str(msg))
            print 'methode %s, mode echantillonnage = %s : %s' % (str(
                S.methode), str(S.mode), str(msg))
            X3, Y3 = zeros((0, )), zeros((0, ))
        X0, Y0 = XY(e0)
        X1, Y1 = XY(e1)
        X2, Y2 = XY(e2)
        #         if show : S.plot(plt,show=True)
        if show:
            #             plt.plot(X0,Y0,'r.-',label='%s : mode=%s'%(S.name,mode0))
            plt.plot(X1, Y1, 'b.-', label='%s : mode=%s' % (S.name, mode1))
            #             plt.plot(X2,Y2,'k.-',label='%s : mode=%s'%(S.name,mode2))
            plt.plot(X3, Y3, 'm.-', label='%s : mode=%s' % (S.name, mode3))
            plt.legend()
            plt.axis('equal')
            plt.show()
    debug(titre='Fin testSequenceBasique %s' % filename.name)
Ejemplo n.º 13
0
def testDivers1(filename, show=True):
    #     show=True
    debug(titre="testDivers1 : %s" % filename.name)

    msg = u"""
    On teste les methodes NSplineSimple.projectionObj(obj) et
    NSplineSimple.projection(obj).
    """
    debug(msg)
    cpoints = pointsFromFile(filename)[::-1]
    #On recale le BA en 0,0
    corde, nba = computeCordeAndNBA(cpoints)
    debug(corde=corde, nba=nba, len_cpoints=len(cpoints))
    cpoints = cpoints - cpoints[nba]
    cpoints = cpoints[0:nba]
    #     cpoints = cpoints[0:nba+1]
    debug(BA=cpoints[-1], BF=cpoints[0], extrados=cpoints[nba / 2])
    #     S0 = NSplineSimple(cpoints=cpoints, name=filename.name)
    #     debug(S0)
    #     if show : S0.plot(show=True)
    ########################################
    #     dump = {'cpoints': [[0.5279636979103088, 0.07332829385995865], [0.7137287259101868, 0.3275330364704132], [1.268811468596966, 0.3365727279966774], [1.1390328407287598, 0.07332829385995865], [1.2571670716747245, 0.2148051133421408], [1.2206038430660453, -0.0507648238639925], [1.5545598268508911, -0.0048885527066886425]],
    # #             'methode': ('cubic', 'not-a-knot'),
    #             'methode': ('ius', 3),
    #             'name': u'test',
    #             'nbpe': 30,
    #             'precision': 1000,
    #             'mode': 'linear'}
    dump = {
        'classename': 'NSplineSimple',
        'cpoints': cpoints,
        'methode': ('cubic', 'not-a-knot'),
        'mode': 'rayon',
        'name': filename.name,
        'nbpe': 30,
        'precision': 1000,
        'role': 'NSplineSimple'
    }
    S = NSplineSimple(**dump)
    #     if show : S.plot(plt)

    #     p1, p2, p3 = [0.0,0.25], S.barycentre[0], S.centregravite
    p1, p2, p3 = [0.0, 0.25], [1.0, -0.1], [1.0, 0.1],
    debug(p1=p1, p2=p2, p3=p3)
    (t1, d1, n1,
     m1), (t2, d2, n2,
           m2), (t3, d3, n3,
                 m3) = S.projection(p1), S.projection(p2), S.projection(p3)
    pj1, pj2, pj3 = S(t1), S(t2), S(t3)

    debug(paragraphe="****  DISTANCE POINT-SPLINE discret=0  ****")
    print '    message=%-20s' % m1, 'd(S,p1)=%-10.3g' % d1, 't=%-10.3g' % t1, 'nb appels a fct=%d' % n1
    print '    message=%-20s' % m2, 'd(S,p2)=%-10.3g' % d2, 't=%-10.3g' % t2, 'nb appels a fct=%d' % n2
    print '    message=%-20s' % m3, 'd(S,p3)=%-10.3g' % d3, 't=%-10.3g' % t3, 'nb appels a fct=%d' % n3
    more = [([p1[0], pj1[0]], [p1[1],
                               pj1[1]], 'g-o', 'd(S,p1) : %.2g' % sqrt(d1)),
            ([p2[0], pj2[0]], [p2[1],
                               pj2[1]], 'g-o', 'd(S,p2) : %.2g' % sqrt(d2)),
            ([p3[0], pj3[0]], [p3[1],
                               pj3[1]], 'g-o', 'd(S,p3) : %.2g' % sqrt(d3))]

    if show:
        S.plot(more=more, titre='distance point-spline(continu)', show=True)

    (t1, d1, n1, m1) = S.projection(p1, discret=100)
    (t2, d2, n2, m2) = S.projection(p2, discret=100)
    (t3, d3, n3, m3) = S.projection(p3, discret=100)
    pj1, pj2, pj3 = S(t1), S(t2), S(t3)
    debug("paragraphe=****  DISTANCE POINT-SPLINE discret=100 ****")
    print '    message=%-20s' % m1, 'd(S,p1)=%-10.3g' % d1, 't=%-10.3g' % t1, 'nb appels a fct=%d' % n1
    print '    message=%-20s' % m2, 'd(S,p2)=%-10.3g' % d2, 't=%-10.3g' % t2, 'nb appels a fct=%d' % n2
    print '    message=%-20s' % m3, 'd(S,p3)=%-10.3g' % d3, 't=%-10.3g' % t3, 'nb appels a fct=%d' % n3
    if show:
        more = [([p1[0],
                  pj1[0]], [p1[1],
                            pj1[1]], 'g-o', 'd(S,p1) : %.2g' % sqrt(d1)),
                ([p2[0],
                  pj2[0]], [p2[1],
                            pj2[1]], 'g-o', 'd(S,p2) : %.2g' % sqrt(d2)),
                ([p3[0],
                  pj3[0]], [p3[1],
                            pj3[1]], 'g-o', 'd(S,p3) : %.2g' % sqrt(d3))]

        S.plot(more=more, titre='distance point-spline (discret)', show=True)
    T, D, N, _ = S.projectionObj(S.epoints, discret=100)
    print '    S.projectionObj(S.epoints) : norm(D)=%.3g, max(D)=%.3g' % (sqrt(
        sum(D)), sqrt(max(D)))
    #     P = asarray([p1,p2,p3])
    P = (rand(5, 2) - 0.3) / 10 + S.centregravite
    #     debug(P.tolist())
    T, D, N, _ = S.projectionObj(P, discret=1000)
    S.nbpd = 1000
    Pj = S(T)
    debug(paragraphe='Aleatoire : S.projectionObj(P,discret=%d)' % S.nbpd,
          T_D_N=zip(T, D, N))
    #     exit()

    if show:
        more = [([p[0], pj[0]], [p[1],
                                 pj[1]], 'g-o', 'd(S,p1) : %.2g' % sqrt(d))
                for p, pj, d in zip(P, Pj, D)]
        S.plot(more=more, titre='distance point-spline', show=True)
    debug(paragraphe=u'Elagage')
    s1, d, (n0, n1) = S.elaguer(eps=1.0, debog=show)
    if show:
        S.plot(titre=u'avant élagage', show=False)
        s1.plot(
            titre=
            u"Après élagage, distance=%.3g ; nb points contrôle : %d => %d" %
            (d, n0, n1),
            show=True)
    debug(titre="Fin testDivers1 : %s" % filename.name)

    return
Ejemplo n.º 14
0
def testDivers(filename, show=True):
    name = filename.name
    debug(titre="testDivers : %s" % name)

    msg = u"""Dans cet exemple, on a des points doubles p4=p5=p6 et p8=p9,
    donc la spline x(t) ne peut pas être construite.
    Que faire ?
    - supprimer p[6] <=== Oui c'est la solution adoptée ici
    - déplacer légèrement le point
    - faire une spline composée, splittée en 6
    """
    debug(msg)
    cpoints = pointsFromFile(filename)
    S0 = NSplineSimple(cpoints=cpoints, name=filename.name)
    debug(S0)
    if show: S0.plot(show=True)
    ########################################
    msg = u"""S est une NSplineSimple. Teste les méthodes :
    - S.absCurv(T) qui retourne les abscisses curvilignes VRAIES des points S(T)
    - S.longueur('x') =
        # longueur vraie(x=r) par intégration numérique
        # longueur du polyligne S.[cpoints,dpoints,epoints] 'x'=['c','d','e']
    - S.projection(p) et affiche le résultat pour 3 points
        Calcule la distance du point 'p' à la spline 'S' et le point 'h' de la spline
        le plus proche de 'p'. Retourne également la precision et le nb d'appels à fonction
        Utilise :
        "https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize_scalar.html
    """
    dump = {
        'classename': 'NSplineSimple',
        'cpoints': cpoints,
        'methode': ('cubic', 'not-a-knot'),
        #             'mode': 'segment',
        #             'methode': ('ius', 1),
        'mode': 'rayon',
        #             'mode': 'cpoints',
        'name': filename.name,
        'nbpe': 30,
        'precision': 1000,
        'role': 'NSplineSimple'
    }
    dump = {
        'cpoints': [[0.5279636979103088, 0.07332829385995865],
                    [0.7137287259101868, 0.3275330364704132],
                    [1.268811468596966, 0.3365727279966774],
                    [1.1390328407287598, 0.07332829385995865],
                    [1.2571670716747245, 0.2148051133421408],
                    [1.2206038430660453, -0.0507648238639925],
                    [1.5545598268508911, -0.0048885527066886425]],
        #             'methode': ('cubic', 'not-a-knot'),
        'methode': ('ius', 3),
        'name':
        u'test',
        'nbpe':
        30,
        'precision':
        1000,
        'mode':
        'linear'
    }
    debug(msg)
    S = NSplineSimple(**dump)
    if show: S.plot()
    knots = S.knots
    acn = S.absCurv(knots)
    acr = S.absCurv(acn)
    debug('S.absCurv(None)', acn.tolist())
    debug('S.knots', knots.tolist())
    l = S.longueur()
    debug('(acr-acn)*longueur', (acr - acn * l).tolist())
    debug('longueur=%.10g' % l)
    lr = acr[-1]
    debug('vraie abscurv[-1]=%.10g' % lr)
    ln = acn[-1]
    debug('abscurv[-1]=%.10g' % ln)
    S.nbpd = 100
    ld3 = S.longueur('d')
    debug('S.dlongueur=%g' % ld3 + u"(calculé sur %d points)" % S.precision)
    S.nbpd = 1000
    ld4 = S.longueur('d')
    debug('S.dlongueur=%g' % ld4 + u"(calculé sur %d points)" % S.precision)
    S.nbpd = 10000
    ld5 = S.longueur('d')
    debug('S.dlongueur=%g' % ld5 + u"(calculé sur %d points)" % S.precision)
    le = S.longueur('e')
    debug('S.elongueur=%g' % le)
    lc = S.longueur('c')
    debug('S.clongueur=%g' % lc)

    if show:
        diff = [l - lr, l - ln, l - ld3, l - ld4, l - ld5, l - le, l - lc]
        plt.show()
        plt.bar(range(len(diff)), diff)
        plt.xticks(range(len(diff)), 'lr,ln,ld3,ld4,ld5,le,lc'.split(','))
        plt.title('differentes longueurs calculees')
    p1, p2, p3 = [0.2, 0.01], [1.1, 0.2], [-0.1, 0.3]
    res1, res2, res3 = S.projection(p1), S.projection(p2), S.projection(p3)
    pj1, pj2, pj3 = S(res1[0]), S(res2[0]), S(res3[0])
    #     debug(str(res))
    debug(paragraphe="****  DISTANCE POINT-SPLINE  ****")
    debug('message="%s"' % res1[3], 'd(S,p1)=%g' % res1[1], 't=%g' % res1[0],
          'nb appels a fct=%d' % res1[2])
    debug('message="%s"' % res2[3], 'd(S,p2)=%g' % res2[1], 't=%g' % res2[0],
          'nb appels a fct=%d' % res2[2])
    debug('message="%s"' % res3[3], 'd(S,p3)=%g' % res3[1], 't=%g' % res3[0],
          'nb appels a fct=%d' % res3[2])
    if show:
        more = [([p1[0],
                  pj1[0]], [p1[1],
                            pj1[1]], 'g-o', 'd(S,p1) : %.2g' % sqrt(res1[1])),
                ([p2[0],
                  pj2[0]], [p2[1],
                            pj2[1]], 'g-o', 'd(S,p2) : %.2g' % sqrt(res2[1])),
                ([p3[0],
                  pj3[0]], [p3[1],
                            pj3[1]], 'g-o', 'd(S,p3) : %.2g' % sqrt(res3[1]))]
        projections = [(pj1[0], pj1[1], 'y.', ''), (pj2[0], pj2[1], 'y.', ''),
                       (pj3[0], pj3[1], 'y.', '')]
        more += projections
        S.plot(more=more, titre='distance point-spline', show=True)


#     plt.plot(p1[0],p1[1],'go', label='p1 : %g'%res1[1])
#     plt.legend()
#     if show : plt.show()
#     return
    debug('ORIGINAL', S=S)
    debug(rect=S.boundingRect())
    #     debug('CLONE', clone=S.clone())
    debug('COPY', clone=S.copy())
    S.hardScale((0.5, 0.5), S.centregravite)
    debug('APRES HARDSCALE', S=S)
    debug(rect=S.boundingRect())
    S.hardMove((10, 10))
    debug('APRES HARDMOVE', S=S)
    debug(rect=S.boundingRect())
    #     debug(top='%g'%rect.top(), left='%g'%rect.left(), width='%g'%rect.width(), height='%g'%rect.height(), )
    #     debug(bottom='%g'%rect.bottom(), top='%g'%rect.top())
    if show: S.plot(titre="**dump")
    debug(titre="Fin testDivers : %s" % filename.name)
Ejemplo n.º 15
0
def testModifLocales(filename, show=True):
    debug(titre="testModifLocales : %s" % filename.name)
    #     exit()
    S = NSplineSimple(points=pointsFromFile(filename),
                      methode=('cubic', ((2, 0, 0), (1, 0, -5))),
                      mode='courbure',
                      name='SPLINE01')
    mesure = mesures(S)
    debug('mesures')
    for key, value in mesure.items():
        exec('%s=%s' % (key, value))
        exec("print '%20s = %s'%(key,value)")
    if show: S.plot(titre=filename.name)

    try:
        i0 = S.insertPoint((0, 0))
        if show and isinstance(i0, int):
            S.plot(titre='insertPoint((0,0)) en position %d' % i0)
    except ValueError as msg:
        debug('normal', msg)

    try:
        i1 = S.appendPoint((2, 1.5))
        if show and isinstance(i1, int):
            S.plot(titre='appendPoint((2,1.5)) num=%d' % i1)
    except ValueError as msg:
        debug(msg)
        raise

    try:
        i2 = S.insertPoint((-2, 0))
        if show: S.plot(titre='insertPoint((2,-2)) en position %d' % i2)
    except ValueError as msg:
        debug(msg)
        raise

    try:
        i3 = S.insertPoint((6, 0), len(S) / 2)
        if show:
            S.plot(titre='insertPoint((6,0),%d) en position %d' %
                   (len(S) / 2, i3))
    except ValueError as msg:
        debug(msg)
        raise
    S.removePoint(i3)
    S.removePoint(i2)
    S.removePoint(i1)
    if show: S.plot(titre='removePoint(%d,%d,%d)' % (i3, i2, i1))

    S[1] = (4, -0.1)
    if show: S.plot(titre='S[1] = (4,-0.1)', show=True)
    print S[1]
    S._update()
    pprint(S.__dict__)
    dump = S.toDump()
    dump['methode'] = ('cubic', 'natural')
    S.load(dump)
    S.name = 'SPLINE3'
    if show: S.plot(titre='load(toDump())', show=True)
    print S
    debug(titre="Fin testModifLocales : %s" % filename.name)
Ejemplo n.º 16
0
def testProfil(filename, show=True):
    def expose(par):
        print p
        print 'cpoints = asarray(%s)' % p.cpoints.tolist()
        ep = p.epoints.tolist()
        print 'epoints = asarray(%s)' % ep
        print 'techext = asarray(%s)' % p.techext.tolist()
        print 'techint = asarray(%s)' % p.techint.tolist()
        if show: p.plot(titre=par, show=True)
        return ep

    name = filename.name
    debug(titre='testProfil : %s' % name)

    par = 'p = Profil()'
    debug(paragraphe=par)
    p = Profil()
    expose(par)

    par = "p = Profil(naca=['2415', 50], name=None)"
    debug(paragraphe=par)
    p = Profil(naca=['2415', 20], name=None)
    expose(par + 'verification')
    for v in p.verification():
        print v

    par = 'p.open("%s")' % name
    debug(paragraphe=par)
    try:
        p.open(filename)
        expose(par)
    except ValueError as msg:
        rdebug("TODO : %s, %s" % (par, msg))
    except IndexError as msg:
        rdebug("TODO : %s, %s" % (par, msg))
#     exit()
    par = 'Profil(points) (%s)' % name
    debug(paragraphe=par)
    p = Profil(cpoints=pointsFromFile(filename), name=name)
    expose(par)

    par = "toDump/load (%s)" % name
    debug(paragraphe=par)
    dump = p.toDump()
    p = Profil(**dump)
    expose(par)
    dump1 = p.toDump()
    debug(dump_NOT_equal_dump1=dictsAreNotEqual(dump, dump1))

    iouv = p.iba + 5, p.iba + 10
    ouv = -1.0, -10.0
    par = 'p.pouverture = %s; p.iouverture = %s' % (ouv, iouv)
    debug(paragraphe=par)
    debug(pp=p.profparam)
    p.iouverture = iouv
    p.pouverture = ouv
    ep0 = expose(par)

    par = "constructeur recopie (%s)" % name
    p = Profil(profil=p)
    ep = expose(par)
    debug(where(ep != ep0))

    pt, i = (0.55, 0.1), 10
    par = "%s : insert(%s,%d)" % (name, pt, i)
    debug(paragraphe=par)
    p.insertPoint(pt, i)
    expose(par)

    par = '%s.scaled(2.0)' % name
    debug(paragraphe=par)
    p = p.scaled(2.0)
    expose(par)

    par = '%s.hardScale((2,2))' % name
    debug(paragraphe=par)
    p.hardScale((2, 2))
    expose(par)

    par = '%s.hardRotate(30,centre=(2,2))' % name
    debug(paragraphe=par)
    p.hardRotate(30, centre=(2, 2))
    expose(par)

    par = 'p.translate((100,100)'
    debug(paragraphe=par)
    p.translate((100, 100))
    expose(par)
    #     print p.verification()
    par = 'normalise()'
    debug(paragraphe=par)
    p.normalise()
    expose(par)

    par = 'p[1] = (0.6,0.12)'
    debug(paragraphe=par)
    p[1] = (0.6, 0.12)
    expose(par)
    par = 'p.removePoint(1)'
    debug(paragraphe=par)
    p.removePoint(1)
    expose(par)
    #     return

    par = "dump('profildump.pkl') puis cPickle.load()"
    debug(paragraphe=par)
    p.dump(Path(RUNS_DIR, 'profildump.pkl'))
    f = open(Path(RUNS_DIR, 'profildump.pkl'), 'r')
    d = cPickle.load(f)
    pprint(d)
    p.load(d)
    expose(par)
    debug(p)
    p.normalise()
    expose('p.normalise')
    debug(p)
    debug(titre='Fin testprofil')