IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage C++ Discussion :

Fait un dynamic cast en spéciant le RTTI en compilation.


Sujet :

Langage C++

  1. #1
    Invité
    Invité(e)
    Par défaut Fait un dynamic cast en spéciant le RTTI en compilation.
    Salut, j'ai lu que dynamic_cast utilise le RTTI pour faire le cast d'un objet de base vers un objet dérivé.

    Alors ce que je voudrais savoir c'est si il y a moyen de faire ceci :

    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    dynamic_cast<object->getType()>(object);

    Ou getType() me renvoie juste une variable de type type_info. (Donc contenant les informations sur le type de l'objet à l'exécution)

    Le problème c'est que ça ne compile pas il me dit espected type specifier before object.

    Alors je voudrais savoir si il n'y a pas un moyen de dire au compilateur de ne pas évaluer ça à la compilation mais de laisser le code compiler pour que ça fasse le cast à l'exécution ???

  2. #2
    Membre expert
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2011
    Messages
    739
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2011
    Messages : 739
    Points : 3 627
    Points
    3 627
    Par défaut
    Non, pour la bonne raison que les casts se font sur des types et non sur des valeurs. Et il y a un constat très simple à faire : les types manipulés ne sont pas dynamique (le type réel si, mais on ne peut le connaître sans plus d'information que le type manipulé).
    Quant il faut "dynamiser les types", tu peux aller de l'avant (appeler des fonctions avec des types différents en fonction de valeur) mais jamais reculer car le type de retour ne changera pas. En fait, seul les prototypes des fonctions et un graph d'appel suffisent pour suivre l'évolution d'un type.

    Même chose pour les valeurs constexpr/template.

  3. #3
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    @Lolilolight: Non tu ne peux pas, car tu ne pourrais pas déclarer la variable dans laquelle tu mettrais le résultat (vu qu'elle n'aurait pas de type connu).
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  4. #4
    Expert éminent sénior
    Avatar de koala01
    Homme Profil pro
    aucun
    Inscrit en
    Octobre 2004
    Messages
    11 614
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : Belgique

    Informations professionnelles :
    Activité : aucun

    Informations forums :
    Inscription : Octobre 2004
    Messages : 11 614
    Points : 30 626
    Points
    30 626
    Par défaut
    Salut,

    Déjà, il faut savoir que, si tu te trouves face à une situation dans laquelle tu dois utiliser le dynamic_cast, c'est très vraisemblablement le symptôme d'un problème de conception dans le sens où "tu as été assez bête" pour en arriver à connaitre un objet selon son type de base plutôt que selon son type le plus dérivé.

    A partir de là, il y a bien quelques circonstances toutes particulières dans lesquelles le dynamic_cast peut s'avérer intéressant voire indispensable, mais de manière générale, tu ne devrais jamais envisager de l'utiliser pour déterminer si ton objet qui passe pour être du type de base est un objet d'un type dérivé donné. Les mots clé pour t'en sortir sont virtualité des fonctions, pour être en mesure de faire varier le comportement d'un objet en fonction de son type réel et double dyspatch pour pouvoir demander à l'objet d'agir selon les capacités de son type le plus dérivé
    A méditer: La solution la plus simple est toujours la moins compliquée
    Ce qui se conçoit bien s'énonce clairement, et les mots pour le dire vous viennent aisément. Nicolas Boileau
    Compiler Gcc sous windows avec MinGW
    Coder efficacement en C++ : dans les bacs le 17 février 2014
    mon tout nouveau blog

  5. #5
    Invité
    Invité(e)
    Par défaut
    Ok je vois je vais essayer de m'en sortir avec du double dispach alors..., bon, dans le cas des méthodes template qui ne peuvent pas être virtuelles là je n'ai pas le choix, je suis obligé d'utiliser une fonction qui renvoie un std::string sur le type de l'objet et de faire un cast si je veux effectuer un action sur l'objet dérivé.

    Par exemple, comme ici :

    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
     
     template <typename C>
        bool intersects(BoundingVolume<C>& other) {
            //if(static_cast<C&>(other)) {
                if (children.size() == 0 && other.children.size() == 0) {
                    return intersects(dynamic_cast<C&>(other));
                }  else if (children.size() == 0 && other.children.size() != 0) {
                      for (unsigned int i = 0; i < other.children.size(); i++) {
                          if (other.children[i]->getType() == "BoundingBox")
                              if (intersects(dynamic_cast<BoundingVolume<BoundingBox>&>(*other.children[i])))
                                    return true;
                      }
                } else {
                    for (unsigned int i = 0; i < children.size(); i++) {
                        if (other.children.size() == 0) {
                            if (children[i]->getType() == "BoundingBox")
                                if (static_cast<BoundingVolume<BoundingBox>*>(children[i])->intersects(static_cast<BoundingVolume<C>&>(other)))
                                    return true;
     
                        } else {
                            for (unsigned j = 0; j < other.children.size(); j++) {
                                if (children[i]->getType() == "BoundingBox" && other.children[i]->getType() == "BoundingBox")
                                    if (static_cast<BoundingVolume<BoundingBox>*>(children[i])->intersects(static_cast<BoundingVolume<BoundingBox>&>(*other.children[j])))
                                        return true;
                            }
                        }
                    }
                }
            //}
            return false;
        }

    Vu que mes différents types de volumes doivent être stocké dans un contenaire je suis obligé d'utiliser le pattern CRTP et type erasure, donc, je suis également obligé de faire un cast.

  6. #6
    Expert éminent sénior
    Avatar de koala01
    Homme Profil pro
    aucun
    Inscrit en
    Octobre 2004
    Messages
    11 614
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : Belgique

    Informations professionnelles :
    Activité : aucun

    Informations forums :
    Inscription : Octobre 2004
    Messages : 11 614
    Points : 30 626
    Points
    30 626
    Par défaut
    Si tu pars sur un tel principe, tu t’ampute littéralement de trois doigts!!!!

    Non, ce que tu peux faire, étant donné que tu pars visiblement sur du CRTP, c'est faire en sorte d'avoir un alias de type (DataType, par exemple) qui corresponde au paramètre template de ta classe, et une fonction qui renvoie une référence (constante au besion) sur l'objet en question.

    Cela donnerait un truc du genre de
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    template <typename T>
    class Interface{
        public:        using DataType = T;
            DataType /* const */ & data() /* const*/{return t_;}
        protected:
            Interface(T & t) : t_(t){}
            ~Interface(){}
        private:
            T & t_;
    };
     
    class MaClasse : public Interface<MaClasse>{
     
        public:
            MaClasse():Interface<MaClasse>(*this){}
            void foo():
            /*... */
    }
    template <typename T> 
    void bar(Interface<T> const &  iface){
        iface.data().foo();
    }
    Avec la spécialisation partielle, rien ne t'empêche de prévoir d'autres comportements que foo en cas de besoin
    A méditer: La solution la plus simple est toujours la moins compliquée
    Ce qui se conçoit bien s'énonce clairement, et les mots pour le dire vous viennent aisément. Nicolas Boileau
    Compiler Gcc sous windows avec MinGW
    Coder efficacement en C++ : dans les bacs le 17 février 2014
    mon tout nouveau blog

  7. #7
    Invité
    Invité(e)
    Par défaut
    Ha, je n'y avais pas pensé, je vais essayer ça.

    Merci.

  8. #8
    Invité
    Invité(e)
    Par défaut
    Et voilà.

    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
     
    #ifndef ODFAEG_BOUNDING_VOLUME_HPP
    #define ODFAEG_BOUNDING_VOLUME_HPP
    #include "../Math/vec4.h"
    #include <string>
    /**
      *\namespace odfaeg
      * the namespace of the Opensource Development Framework Adapted for Every Games.
      */
     
    namespace odfaeg {
    /**
      * \file boundingVolume.h
      * \class BoudingVolume
      * \brief Manage a bounding volume for collision detection
      * \author Duroisin.L
      * \version 1.0
      * \date 1/02/2014
      *
      * Base class of all bouding volumes of the framework used for collision detection.
      *
      */
    class BoundingVolume;
    class BaseInterface {
         public :
         void addChild(BoundingVolume* bv) {
            children.push_back(bv);
         }
         std::vector<BoundingVolume*> getChildren() {
            return children;
         }
         std::vector<BoundingVolume*> children;
    };
    template <typename T>
    class Interface : public Serializer<T>, public BaseInterface {
        public:        using DataType = T;
            DataType /* const */ & data() /* const*/{return t_;}
            ~Interface(){}
        protected:
            Interface(T & t) : t_(t){}
            //~Interface(){}
            virtual bool intersects(BaseInterface& other) {}
            virtual Interface<T>& operator= (Interface<T> interface) {
                *this = interface;
                return *this;
            }
        private:
            T & t_;
    };
    class BoundingVolume : public BaseInterface {
    public :
     
        virtual bool intersects(BaseInterface& other) {
            //if(static_cast<C&>(other)) {
                if (children.size() == 0 && other.children.size() == 0) {
                    std::cout<<"intersects"<<std::endl;
                    return intersects(other);
                }  else if (children.size() == 0 && other.children.size() != 0) {
                      for (unsigned int i = 0; i < other.children.size(); i++) {
                          if (intersects(*other.children[i]))
                                return true;
                      }
                } else {
                    for (unsigned int i = 0; i < children.size(); i++) {
                        if (other.children.size() == 0) {
                            if (children[i]->intersects(other))
                                    return true;
     
                        } else {
                            for (unsigned j = 0; j < other.children.size(); j++) {
                                 if (children[i]->intersects(*other.children[j]))
                                        return true;
                            }
                        }
                    }
                }
            //}
            return false;
        }
        virtual Vec3f getPosition() {
            return Vec3f(0, 0, 0);
        }
        virtual Vec3f getSize() {
            return Vec3f(0, 0, 0);
        }
        virtual Vec3f getCenter() {
            return Vec3f(0, 0, 0);
        }
        virtual void move (Vec3f t) {
        }
        virtual BoundingVolume* clone() {
            return new BoundingVolume(*this);
        }
        virtual ~BoundingVolume() {}
    public :
     
    };
     
    }
    #endif // BOUNDING_AREAS

    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
     
    #include "../../../include/odfaeg/Physics/boundingBox.h"
    #include "../../../include/odfaeg/Physics/boundingSphere.h"
    #include "../../../include/odfaeg/Physics/boundingEllipsoid.h"
    #include "../../../include/odfaeg/Physics/orientedBoundingBox.h"
    #include "../../../include/odfaeg/Physics/boundingPolyhedron.h"
    #include "../../../include/odfaeg/Graphics/transformMatrix.h"
    using namespace std;
    namespace odfaeg {
     
    //Create a bounding box at position 0, 0, 0 and with the dimensions 1, 1, 1.
    BoundingBox::BoundingBox() : Interface<BoundingBox>(*this) {
        int x = y = z = 0;
        width = height = depth = 1;
        vertices[0] = Vec3f(x, y, z);
        vertices[1] = Vec3f(x + width, y, z);
        vertices[2] = Vec3f(x + width, y + height, z);
        vertices[3] = Vec3f(x, y + height, z);
        vertices[4] = Vec3f(x, y, z + depth);
        vertices[5] = Vec3f(x + width, y, z + depth);
        vertices[6] = Vec3f(x + width, y + height, z + depth);
        vertices[7] = Vec3f(x, y + height, z + depth);
    }
    //Create a bounding box with the specified , y, z position and of width, height, depth dimensions.
    BoundingBox::BoundingBox (int x, int y, int z, int width, int height, int depth) : Interface<BoundingBox>(*this), center (x + width * 0.5f, y + height * 0.5f, z + depth * 0.5f) {
        this->x = x;
        this->y = y;
        this->z = z;
        this->width = width;
        this->height = height;
        this->depth = depth;
        vertices[0] = Vec3f(x, y, z);
        vertices[1] = Vec3f(x + width, y, z);
        vertices[2] = Vec3f(x + width, y + height, z);
        vertices[3] = Vec3f(x, y + height, z);
        vertices[4] = Vec3f(x, y, z + depth);
        vertices[5] = Vec3f(x + width, y, z + depth);
        vertices[6] = Vec3f(x + width, y + height, z + depth);
        vertices[7] = Vec3f(x, y + height, z + depth);
    }
    bool BoundingBox::intersects (BaseInterface& interface) {
        std::cout<<"intersects with interface"<<std::endl;
         static_cast<Interface<BoundingBox>&>(interface).data().intersects(*this);
    }
    //Test if the box intersects the specified sphere.
    bool BoundingBox::intersects (BoundingSphere &bs) {
        return bs.intersects(*this);
    }
    //Test if the box intersects the specified ellipsoid.
    bool BoundingBox::intersects (BoundingEllipsoid &be) {
        return be.intersects(*this);
    }
    //Test if the box intersects another.
    bool BoundingBox::intersects (BoundingBox &other) {
        //Get the medians of our two boxes.
        /*Vec3f hi1[6];
        hi1[0] = Vec3f(width * 0.5f, 0, 0);
        hi1[1] = Vec3f(0, height * 0.5f, 0);
        hi1[2] = Vec3f(0, 0, depth * 0.5f);
        hi1[3] = Vec3f(-width * 0.5f, 0, 0);
        hi1[4] = Vec3f(0, -height * 0.5f, 0);
        hi1[5] = Vec3f(0, 0, -depth * 0.5f);
        Vec3f hi2[6];
        hi2[0] = Vec3f(other.width * 0.5f, 0, 0);
        hi2[1] = Vec3f(0, other.height * 0.5f, 0);
        hi2[2] = Vec3f(0, 0, other.depth * 0.5f);
        hi1[3] = Vec3f(-width * 0.5f, 0, 0);
        hi1[4] = Vec3f(0, -height * 0.5f, 0);
        hi1[5] = Vec3f(0, 0, -depth * 0.5f);
        for (unsigned int i = 0; i < 6; i++) {
             Vec2f p1 = Computer::projectShapeOnAxis(hi1[i], vertices);
             Vec2f p2 = Computer::projectShapeOnAxis(hi1[i], other.vertices);
             if (!Computer::overlap(p1, p2))
                return false;
        }
        for (unsigned int i = 0; i < 6; i++) {
             Vec2f p1 = Computer::projectShapeOnAxis(hi2[i], vertices);
             Vec2f p2 = Computer::projectShapeOnAxis(hi2[i], other.vertices);
             if (!Computer::overlap(p1, p2))
                return false;
        }
        return true;*/
        //std::cout<<"other : "<<&other<<std::endl;
        int hx1 = width * 0.5;
        int hy1 = height * 0.5;
        int hz1 = depth * 0.5;
        int hx2 = other.width * 0.5;
        int hy2 = other.height * 0.5;
        int hz2 = other.depth * 0.5;
        //Check the mins and max medians positions.
     
        /*std::cout<<"width : "<<width<<" height : "<<height<<std::endl<<"center : "<<center;
        std::cout<<"width 2 : "<<other.width<<" height 2 : "<<other.height<<" center 2 : "<<other.center;*/
        float minX1 = center.x - hx1, minX2 = other.center.x - hx2;
        float minY1 = center.y - hy1, minY2 = other.center.y - hy2;
        float minZ1 = center.z - hz1, minZ2 = other.center.z - hz2;
        float maxX1 = center.x + hx1, maxX2 = other.center.x + hx2;
        float maxY1 = center.y + hy1, maxY2 = other.center.y + hy2;
        float maxZ1 = center.z + hz1, maxZ2 = other.center.z + hz2;
        //If the medians overlap, our two boxes intersects.
        for (int i = 0; i < 3; i++) {
            if (i == 0) {
                if (minX1 > maxX2 || maxX1 < minX2)
                    return false;
            } else if (i == 1) {
                if (minY1 > maxY2 || maxY1 < minY2)
                    return false;
            } else {
                if (minZ1 > maxZ2 || maxZ1 < minZ2)
                    return false;
            }
        }
        return true;
        /*Vec3f d = other.center - center;
        int c = d.magnitude() * 2;
        Vec3f hi[3];
        hi[0] = Vec3f(width * 0.5f, 0, 0);
        hi[1] = Vec3f(0, height * 0.5f, 0);
        hi[2] = Vec3f(0, 0, depth * 0.5f);
        Vec3f hi2[3];
        hi2[0] = Vec3f(other.width * 0.5f, 0, 0);
        hi2[1] = Vec3f(0, other.height * 0.5f, 0);
        hi2[2] = Vec3f(0, 0, other.depth * 0.5f);
        for (unsigned int i = 0; i < 3; i++) {
            int r1 = hi[i].projOnVector(d).magnitude();
            int r2;
            for (unsigned int j = 0; j < 3; j++) {
                r2 = hi2[j].projOnVector(d).magnitude();
            }
            int rSum = r1 + r2;
            std::cout<<rSum<<" "<<c<<std::endl;
            if (c - rSum > 0)
                return false;
        }
        return true;*/
    }
    //Test if an the box intersects with the specified oriented bounding box.
    bool BoundingBox::intersects(OrientedBoundingBox &obx) {
        return obx.intersects(*this);
    }
    //Test if the bounding box intersects with the specified bounding polyhedron.
    bool BoundingBox::intersects (BoundingPolyhedron &bp) {
         Vec3f hi[6];
         hi[0] = Vec3f(width * 0.5f, 0, 0);
         hi[1] = Vec3f(0, height * 0.5f, 0);
         hi[2] = Vec3f(0, 0, depth * 0.5f);
         hi[3] = Vec3f(-width * 0.5f, 0, 0);
         hi[4] = Vec3f(0, -height * 0.5f, 0);
         hi[5] = Vec3f(0, 0, -depth * 0.5f);
         std::vector<Vec3f> normals = bp.getNormals();
         for (unsigned int i = 0; i < 6; i++) {
             Vec2f p1 = Computer::projectShapeOnAxis(hi[i], bp.getPoints());
             Vec2f p2 = Computer::projectShapeOnAxis(hi[i], vertices);
             if (!Computer::overlap(p1, p2))
                return false;
         }
         for (unsigned int i = 0; i < normals.size(); i++) {
             Vec2f p1 = Computer::projectShapeOnAxis(normals[i], bp.getPoints());
             Vec2f p2 = Computer::projectShapeOnAxis(normals[i], vertices);
             if (!Computer::overlap(p1, p2))
                return false;
         }
         return true;
    }
    //Test if a point is inside our box.
    bool BoundingBox::isPointInside (Vec3f point) {
        //Check the min and max values of the medians of our box.
        float minX = center.x - width * 0.5f;
        float maxX = center.x + width * 0.5f;
        float minY = center.y - height * 0.5f;
        float maxY = center.y + height * 0.5f;
        float minZ = center.z - depth * 0.5f;
        float maxZ = center.z + depth * 0.5f;
        //If one of the point is on one of the x, y or z medians, the point is inside the box.
        return (point.x >= minX && point.x <= maxX && point.y >= minY && point.y <= maxY && point.z >= minZ && point.z <= maxZ);
    }
     
    bool BoundingBox::intersects (Ray &ray) {
        float tFar = INT_MAX;
        float tNear = -INT_MAX;
     
        for (int i = 0; i < 3; i++) {
            float d, orig, hi;
            if (i == 0) {
                hi = width / 2;
                d = ray.getDir().x;
                orig = ray.getOrig().x - center.x;
            } else if (i == 1) {
                hi = height / 2;
                d = ray.getDir().y;
                orig = ray.getOrig().y - center.y;
            } else {
                hi = depth / 2;
                d = ray.getDir().z;
                orig = ray.getOrig().z - center.z;
            }
     
            if (d == 0)
                if (Math::abs(orig) > hi)
                    return false;
            float t1 = (-hi - orig) / d;
            float t2 = (hi - orig) / d;
            float tmp;
            if (t1 > t2) {
                tmp = t1;
                t1 = t2;
                t2 = tmp;
            } else
                tmp = t1;
            if (t1 > tNear)
                tNear = t1;
            if (t2 < tFar)
                tFar = t2;
     
            if (tNear > tFar)
                return false;
     
            if(tFar < 0)
                return false;
        }
        return true;
     
    }
    //Acceseurs.
    Vec3f BoundingBox::getCenter () {
        return center;
    }
    float BoundingBox::getWidth () {
        return width;
    }
    float BoundingBox::getHeight () {
        return height;
    }
    float BoundingBox::getDepth() {
        return depth;
    }
    Vec3f BoundingBox::getSize() {
        return Vec3f(width, height, depth);
    }
    Vec3f BoundingBox::getPosition () {
        return Vec3f (x, y, z);
    }
    void BoundingBox::setPosition(int x, int y, int z) {
        this->x = x;
        this->y = y;
        this->z = z;
    }
    void BoundingBox::setSize(int width, int height, int depth) {
        this->width = width;
        this->height = height;
        this->depth = depth;
    }
    void BoundingBox::move(Vec3f t) {
        for (unsigned int i = 0; i < vertices.size(); i++)
            vertices[i] += t;
        x = vertices[0].x;
        y = vertices[0].y;
        z = vertices[0].z;
        center.x = x + width * 0.5f;
        center.y = y + height * 0.5f;
        center.z = z + depth * 0.5f;
    }
    }

    Je trouve que le cast d'une interface de base vers une interface dérivée est pas mal du tout. ^^

    Ca m'évite de devoir définir une méthode intersects pour chaque type de volume dans la classe de base.

    Donc bon je fais le cast dans la classe dérivée plutôt que dans la classe de base maintenant. (Ce qui est quand même, déjà mieux, car, il ne faut plus modifier la classe de base à chaque fois que je rajouter un nouveau type de volume.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. le dynamic cast
    Par jeanjack dans le forum Débuter
    Réponses: 2
    Dernier message: 13/04/2009, 20h28
  2. Problème de dynamic casting
    Par Colbix dans le forum C++
    Réponses: 5
    Dernier message: 25/03/2009, 17h55
  3. dynamic cast utilisé pour une recherche
    Par Mangacker dans le forum C++Builder
    Réponses: 3
    Dernier message: 16/02/2009, 21h17
  4. Réponses: 10
    Dernier message: 17/07/2008, 20h01
  5. dynamic cast
    Par leycho dans le forum Langage
    Réponses: 7
    Dernier message: 03/01/2006, 13h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo